| 1 |
//go:build !nosas |
| 2 |
// +build !nosas |
| 3 |
|
| 4 |
package olm |
| 5 |
|
| 6 |
// #cgo LDFLAGS: -lolm -lstdc++ |
| 7 |
// #include <olm/olm.h> |
| 8 |
// #include <olm/sas.h> |
| 9 |
import "C" |
| 10 |
|
| 11 |
import ( |
| 12 |
"crypto/rand" |
| 13 |
"unsafe" |
| 14 |
) |
| 15 |
|
| 16 |
// SAS stores an Olm Short Authentication String (SAS) object. |
| 17 |
type SAS struct { |
| 18 |
int *C.OlmSAS |
| 19 |
mem []byte |
| 20 |
} |
| 21 |
|
| 22 |
// NewBlankSAS initializes an empty SAS object. |
| 23 |
func NewBlankSAS() *SAS { |
| 24 |
memory := make([]byte, sasSize()) |
| 25 |
return &SAS{ |
| 26 |
int: C.olm_sas(unsafe.Pointer(&memory[0])), |
| 27 |
mem: memory, |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// sasSize is the size of a SAS object in bytes. |
| 32 |
func sasSize() uint { |
| 33 |
return uint(C.olm_sas_size()) |
| 34 |
} |
| 35 |
|
| 36 |
// sasRandomLength is the number of random bytes needed to create an SAS object. |
| 37 |
func (sas *SAS) sasRandomLength() uint { |
| 38 |
return uint(C.olm_create_sas_random_length(sas.int)) |
| 39 |
} |
| 40 |
|
| 41 |
// NewSAS creates a new SAS object. |
| 42 |
func NewSAS() *SAS { |
| 43 |
sas := NewBlankSAS() |
| 44 |
random := make([]byte, sas.sasRandomLength()+1) |
| 45 |
_, err := rand.Read(random) |
| 46 |
if err != nil { |
| 47 |
panic(NotEnoughGoRandom) |
| 48 |
} |
| 49 |
r := C.olm_create_sas( |
| 50 |
(*C.OlmSAS)(sas.int), |
| 51 |
unsafe.Pointer(&random[0]), |
| 52 |
C.size_t(len(random))) |
| 53 |
if r == errorVal() { |
| 54 |
panic(sas.lastError()) |
| 55 |
} else { |
| 56 |
return sas |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// clear clears the memory used to back an SAS object. |
| 61 |
func (sas *SAS) clear() uint { |
| 62 |
return uint(C.olm_clear_sas(sas.int)) |
| 63 |
} |
| 64 |
|
| 65 |
// lastError returns the most recent error to happen to an SAS object. |
| 66 |
func (sas *SAS) lastError() error { |
| 67 |
return convertError(C.GoString(C.olm_sas_last_error(sas.int))) |
| 68 |
} |
| 69 |
|
| 70 |
// pubkeyLength is the size of a public key in bytes. |
| 71 |
func (sas *SAS) pubkeyLength() uint { |
| 72 |
return uint(C.olm_sas_pubkey_length((*C.OlmSAS)(sas.int))) |
| 73 |
} |
| 74 |
|
| 75 |
// GetPubkey gets the public key for the SAS object. |
| 76 |
func (sas *SAS) GetPubkey() []byte { |
| 77 |
pubkey := make([]byte, sas.pubkeyLength()) |
| 78 |
r := C.olm_sas_get_pubkey( |
| 79 |
(*C.OlmSAS)(sas.int), |
| 80 |
unsafe.Pointer(&pubkey[0]), |
| 81 |
C.size_t(len(pubkey))) |
| 82 |
if r == errorVal() { |
| 83 |
panic(sas.lastError()) |
| 84 |
} |
| 85 |
return pubkey |
| 86 |
} |
| 87 |
|
| 88 |
// SetTheirKey sets the public key of the other user. |
| 89 |
func (sas *SAS) SetTheirKey(theirKey []byte) error { |
| 90 |
theirKeyCopy := make([]byte, len(theirKey)) |
| 91 |
copy(theirKeyCopy, theirKey) |
| 92 |
r := C.olm_sas_set_their_key( |
| 93 |
(*C.OlmSAS)(sas.int), |
| 94 |
unsafe.Pointer(&theirKeyCopy[0]), |
| 95 |
C.size_t(len(theirKeyCopy))) |
| 96 |
if r == errorVal() { |
| 97 |
return sas.lastError() |
| 98 |
} |
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
// GenerateBytes generates bytes to use for the short authentication string. |
| 103 |
func (sas *SAS) GenerateBytes(info []byte, count uint) ([]byte, error) { |
| 104 |
infoCopy := make([]byte, len(info)) |
| 105 |
copy(infoCopy, info) |
| 106 |
output := make([]byte, count) |
| 107 |
r := C.olm_sas_generate_bytes( |
| 108 |
(*C.OlmSAS)(sas.int), |
| 109 |
unsafe.Pointer(&infoCopy[0]), |
| 110 |
C.size_t(len(infoCopy)), |
| 111 |
unsafe.Pointer(&output[0]), |
| 112 |
C.size_t(len(output))) |
| 113 |
if r == errorVal() { |
| 114 |
return nil, sas.lastError() |
| 115 |
} |
| 116 |
return output, nil |
| 117 |
} |
| 118 |
|
| 119 |
// macLength is the size of a message authentication code generated by olm_sas_calculate_mac. |
| 120 |
func (sas *SAS) macLength() uint { |
| 121 |
return uint(C.olm_sas_mac_length((*C.OlmSAS)(sas.int))) |
| 122 |
} |
| 123 |
|
| 124 |
// CalculateMAC generates a message authentication code (MAC) based on the shared secret. |
| 125 |
func (sas *SAS) CalculateMAC(input []byte, info []byte) ([]byte, error) { |
| 126 |
inputCopy := make([]byte, len(input)) |
| 127 |
copy(inputCopy, input) |
| 128 |
infoCopy := make([]byte, len(info)) |
| 129 |
copy(infoCopy, info) |
| 130 |
mac := make([]byte, sas.macLength()) |
| 131 |
r := C.olm_sas_calculate_mac( |
| 132 |
(*C.OlmSAS)(sas.int), |
| 133 |
unsafe.Pointer(&inputCopy[0]), |
| 134 |
C.size_t(len(inputCopy)), |
| 135 |
unsafe.Pointer(&infoCopy[0]), |
| 136 |
C.size_t(len(infoCopy)), |
| 137 |
unsafe.Pointer(&mac[0]), |
| 138 |
C.size_t(len(mac))) |
| 139 |
if r == errorVal() { |
| 140 |
return nil, sas.lastError() |
| 141 |
} |
| 142 |
return mac, nil |
| 143 |
} |