Skip to content

Commit

Permalink
Add OlmPkDecryption functions (#141)
Browse files Browse the repository at this point in the history
* Add OlmPkDecryption functions

* Trim result to the valid size
  • Loading branch information
bradtgmurray authored Sep 5, 2023
1 parent 3fffe3f commit aafd22e
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions crypto/olm/pk.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,62 @@ func (p *PkSigning) SignJSON(obj interface{}) (string, error) {
func (p *PkSigning) lastError() error {
return convertError(C.GoString(C.olm_pk_signing_last_error((*C.OlmPkSigning)(p.int))))
}

type PkDecryption struct {
int *C.OlmPkDecryption
mem []byte
PublicKey []byte
}

func pkDecryptionSize() uint {
return uint(C.olm_pk_decryption_size())
}

func pkDecryptionPublicKeySize() uint {
return uint(C.olm_pk_key_length())
}

func NewPkDecryption(privateKey []byte) (*PkDecryption, error) {
memory := make([]byte, pkDecryptionSize())
p := &PkDecryption{
int: C.olm_pk_decryption(unsafe.Pointer(&memory[0])),
mem: memory,
}
p.Clear()
pubKey := make([]byte, pkDecryptionPublicKeySize())

if C.olm_pk_key_from_private((*C.OlmPkDecryption)(p.int),
unsafe.Pointer(&pubKey[0]), C.size_t(len(pubKey)),
unsafe.Pointer(&privateKey[0]), C.size_t(len(privateKey))) == errorVal() {
return nil, p.lastError()
}
p.PublicKey = pubKey

return p, nil
}

func (p *PkDecryption) Decrypt(ephemeralKey []byte, mac []byte, ciphertext []byte) ([]byte, error) {
maxPlaintextLength := uint(C.olm_pk_max_plaintext_length((*C.OlmPkDecryption)(p.int), C.size_t(len(ciphertext))))
plaintext := make([]byte, maxPlaintextLength)

size := C.olm_pk_decrypt((*C.OlmPkDecryption)(p.int),
unsafe.Pointer(&ephemeralKey[0]), C.size_t(len(ephemeralKey)),
unsafe.Pointer(&mac[0]), C.size_t(len(mac)),
unsafe.Pointer(&ciphertext[0]), C.size_t(len(ciphertext)),
unsafe.Pointer(&plaintext[0]), C.size_t(len(plaintext)))
if size == errorVal() {
return nil, p.lastError()
}

return plaintext[:size], nil
}

// Clear clears the underlying memory of a PkDecryption object.
func (p *PkDecryption) Clear() {
C.olm_clear_pk_decryption((*C.OlmPkDecryption)(p.int))
}

// lastError returns the last error that happened in relation to this PkDecryption object.
func (p *PkDecryption) lastError() error {
return convertError(C.GoString(C.olm_pk_decryption_last_error((*C.OlmPkDecryption)(p.int))))
}

0 comments on commit aafd22e

Please sign in to comment.