Skip to content

Commit

Permalink
goolm: simplify return statements
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
  • Loading branch information
sumnerevans committed Sep 10, 2024
1 parent ab27876 commit 636eef3
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 110 deletions.
12 changes: 2 additions & 10 deletions crypto/goolm/cipher/aes_sha256.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ func (c AESSHA256) Encrypt(key, plaintext []byte) (ciphertext []byte, err error)
if err != nil {
return nil, err
}
ciphertext, err = aescbc.Encrypt(keys.key, keys.iv, plaintext)
if err != nil {
return nil, err
}
return ciphertext, nil
return aescbc.Encrypt(keys.key, keys.iv, plaintext)
}

// Decrypt decrypts the ciphertext with the key. The key is used to derive the actual encryption key (32 bytes) as well as the iv (16 bytes).
Expand All @@ -58,11 +54,7 @@ func (c AESSHA256) Decrypt(key, ciphertext []byte) (plaintext []byte, err error)
if err != nil {
return nil, err
}
plaintext, err = aescbc.Decrypt(keys.key, keys.iv, ciphertext)
if err != nil {
return nil, err
}
return plaintext, nil
return aescbc.Decrypt(keys.key, keys.iv, ciphertext)
}

// MAC returns the MAC for the message using the key. The key is used to derive the actual mac key (32 bytes).
Expand Down
9 changes: 2 additions & 7 deletions crypto/goolm/cipher/pickle.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ func Pickle(key, input []byte) ([]byte, error) {
return nil, err
}
ciphertext = append(ciphertext, mac[:pickleMACLength]...)
encoded := goolmbase64.Encode(ciphertext)
return encoded, nil
return goolmbase64.Encode(ciphertext), nil
}

// Unpickle decodes the input from base64 and decrypts the decoded input with the key and the cipher AESSHA256.
Expand All @@ -52,9 +51,5 @@ func Unpickle(key, input []byte) ([]byte, error) {
//Set to next block size
targetCipherText := make([]byte, int(len(ciphertext)/PickleBlockSize())*PickleBlockSize())
copy(targetCipherText, ciphertext)
plaintext, err := pickleCipher.Decrypt(key, targetCipherText)
if err != nil {
return nil, err
}
return plaintext, nil
return pickleCipher.Decrypt(key, targetCipherText)
}
6 changes: 1 addition & 5 deletions crypto/goolm/crypto/curve25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,7 @@ func (c Curve25519PrivateKey) Equal(x Curve25519PrivateKey) bool {

// PubKey returns the public key derived from the private key.
func (c Curve25519PrivateKey) PubKey() (Curve25519PublicKey, error) {
publicKey, err := curve25519.X25519(c, curve25519.Basepoint)
if err != nil {
return nil, err
}
return publicKey, nil
return curve25519.X25519(c, curve25519.Basepoint)
}

// SharedSecret returns the shared secret between the private key and the given public key.
Expand Down
12 changes: 2 additions & 10 deletions crypto/goolm/pk/decryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ func (s Decryption) Decrypt(ephemeralKey, mac, ciphertext []byte) ([]byte, error
if !verified {
return nil, fmt.Errorf("decrypt: %w", olm.ErrBadMAC)
}
plaintext, err := cipher.Decrypt(sharedSecret, ciphertext)
if err != nil {
return nil, err
}
return plaintext, nil
return cipher.Decrypt(sharedSecret, ciphertext)
}

// PickleAsJSON returns an Decryption as a base64 string encrypted using the supplied key. The unencrypted representation of the Account is in JSON format.
Expand Down Expand Up @@ -136,11 +132,7 @@ func (a Decryption) Pickle(key []byte) ([]byte, error) {
if written != len(pickeledBytes) {
return nil, errors.New("number of written bytes not correct")
}
encrypted, err := cipher.Pickle(key, pickeledBytes)
if err != nil {
return nil, err
}
return encrypted, nil
return cipher.Pickle(key, pickeledBytes)
}

// PickleLibOlm encodes the Decryption into target. target has to have a size of at least PickleLen() and is written to from index 0.
Expand Down
5 changes: 1 addition & 4 deletions crypto/goolm/pk/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,5 @@ func (e Encryption) Encrypt(plaintext []byte, privateKey crypto.Curve25519Privat
return nil, nil, err
}
mac, err = cipher.MAC(sharedSecret, ciphertext)
if err != nil {
return nil, nil, err
}
return ciphertext, goolmbase64.Encode(mac), nil
return ciphertext, goolmbase64.Encode(mac), err
}
5 changes: 1 addition & 4 deletions crypto/goolm/pk/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,5 @@ func (s Signing) SignJSON(obj any) (string, error) {
objJSON, _ = sjson.DeleteBytes(objJSON, "unsigned")
objJSON, _ = sjson.DeleteBytes(objJSON, "signatures")
signature, err := s.Sign(canonicaljson.CanonicalJSONAssumeValid(objJSON))
if err != nil {
return "", err
}
return string(signature), nil
return string(signature), err
}
72 changes: 26 additions & 46 deletions crypto/goolm/ratchet/olm.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,7 @@ func (r *Ratchet) Encrypt(plaintext []byte) ([]byte, error) {
message.RatchetKey = r.SenderChains.ratchetKey().PublicKey
message.Ciphertext = encryptedText
//creating the mac is done in encode
output, err := message.EncodeAndMAC(messageKey.Key, RatchetCipher)
if err != nil {
return nil, err
}

return output, nil
return message.EncodeAndMAC(messageKey.Key, RatchetCipher)
}

// Decrypt decrypts the ciphertext and verifies the MAC. If reader is nil, crypto/rand is used for key generations.
Expand All @@ -153,53 +148,42 @@ func (r *Ratchet) Decrypt(input []byte) ([]byte, error) {
break
}
}
var result []byte
if receiverChainFromMessage == nil {
//Advancing the chain is done in this method
result, err = r.decryptForNewChain(message, input)
if err != nil {
return nil, err
}
return r.decryptForNewChain(message, input)
} else if receiverChainFromMessage.chainKey().Index > message.Counter {
// No need to advance the chain
// Chain already advanced beyond the key for this message
// Check if the message keys are in the skipped key list.
foundSkippedKey := false
for curSkippedIndex := range r.SkippedMessageKeys {
if message.Counter == r.SkippedMessageKeys[curSkippedIndex].MKey.Index {
// Found the key for this message. Check the MAC.
verified, err := message.VerifyMACInline(r.SkippedMessageKeys[curSkippedIndex].MKey.Key, RatchetCipher, input)
if err != nil {
return nil, err
}
if !verified {
return nil, fmt.Errorf("decrypt from skipped message keys: %w", olm.ErrBadMAC)
}
result, err = RatchetCipher.Decrypt(r.SkippedMessageKeys[curSkippedIndex].MKey.Key, message.Ciphertext)
if err != nil {
return nil, fmt.Errorf("cipher decrypt: %w", err)
}
if len(result) != 0 {
// Remove the key from the skipped keys now that we've
// decoded the message it corresponds to.
r.SkippedMessageKeys[curSkippedIndex] = r.SkippedMessageKeys[len(r.SkippedMessageKeys)-1]
r.SkippedMessageKeys = r.SkippedMessageKeys[:len(r.SkippedMessageKeys)-1]
}
foundSkippedKey = true
if message.Counter != r.SkippedMessageKeys[curSkippedIndex].MKey.Index {
continue
}

// Found the key for this message. Check the MAC.
verified, err := message.VerifyMACInline(r.SkippedMessageKeys[curSkippedIndex].MKey.Key, RatchetCipher, input)
if err != nil {
return nil, err
}
if !verified {
return nil, fmt.Errorf("decrypt from skipped message keys: %w", olm.ErrBadMAC)
}
result, err := RatchetCipher.Decrypt(r.SkippedMessageKeys[curSkippedIndex].MKey.Key, message.Ciphertext)
if err != nil {
return nil, fmt.Errorf("cipher decrypt: %w", err)
} else if len(result) != 0 {
// Remove the key from the skipped keys now that we've
// decoded the message it corresponds to.
r.SkippedMessageKeys[curSkippedIndex] = r.SkippedMessageKeys[len(r.SkippedMessageKeys)-1]
r.SkippedMessageKeys = r.SkippedMessageKeys[:len(r.SkippedMessageKeys)-1]
return result, nil
}
}
if !foundSkippedKey {
return nil, fmt.Errorf("decrypt: %w", olm.ErrMessageKeyNotFound)
}
return nil, fmt.Errorf("decrypt: %w", olm.ErrMessageKeyNotFound)
} else {
//Advancing the chain is done in this method
result, err = r.decryptForExistingChain(receiverChainFromMessage, message, input)
if err != nil {
return nil, err
}
return r.decryptForExistingChain(receiverChainFromMessage, message, input)
}

return result, nil
}

// advanceRootKey created the next root key and returns the next chainKey
Expand Down Expand Up @@ -281,11 +265,7 @@ func (r *Ratchet) decryptForNewChain(message *message.Message, rawMessage []byte
*/
r.SenderChains = senderChain{}

decrypted, err := r.decryptForExistingChain(&r.ReceiverChains[0], message, rawMessage)
if err != nil {
return nil, err
}
return decrypted, nil
return r.decryptForExistingChain(&r.ReceiverChains[0], message, rawMessage)
}

// PickleAsJSON returns a ratchet as a base64 string encrypted using the supplied key. The unencrypted representation of the Account is in JSON format.
Expand Down
6 changes: 1 addition & 5 deletions crypto/goolm/session/megolm_inbound_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,7 @@ func (o *MegolmInboundSession) Pickle(key []byte) ([]byte, error) {
if written != len(pickeledBytes) {
return nil, errors.New("number of written bytes not correct")
}
encrypted, err := cipher.Pickle(key, pickeledBytes)
if err != nil {
return nil, err
}
return encrypted, nil
return cipher.Pickle(key, pickeledBytes)
}

// PickleLibOlm encodes the session into target. target has to have a size of at least PickleLen() and is written to from index 0.
Expand Down
11 changes: 2 additions & 9 deletions crypto/goolm/session/megolm_outbound_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ func (o *MegolmOutboundSession) Encrypt(plaintext []byte) ([]byte, error) {
return nil, olm.ErrEmptyInput
}
encrypted, err := o.Ratchet.Encrypt(plaintext, &o.SigningKey)
if err != nil {
return nil, err
}
return goolmbase64.Encode(encrypted), nil
return goolmbase64.Encode(encrypted), err
}

// SessionID returns the base64 endoded public signing key
Expand Down Expand Up @@ -141,11 +138,7 @@ func (o *MegolmOutboundSession) Pickle(key []byte) ([]byte, error) {
if written != len(pickeledBytes) {
return nil, errors.New("number of written bytes not correct")
}
encrypted, err := cipher.Pickle(key, pickeledBytes)
if err != nil {
return nil, err
}
return encrypted, nil
return cipher.Pickle(key, pickeledBytes)
}

// PickleLibOlm encodes the session into target. target has to have a size of at least PickleLen() and is written to from index 0.
Expand Down
12 changes: 2 additions & 10 deletions crypto/goolm/session/olm_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ func OlmSessionFromJSONPickled(pickled, key []byte) (*OlmSession, error) {
return nil, fmt.Errorf("sessionFromPickled: %w", olm.ErrEmptyInput)
}
a := &OlmSession{}
err := a.UnpickleAsJSON(pickled, key)
if err != nil {
return nil, err
}
return a, nil
return a, a.UnpickleAsJSON(pickled, key)
}

// OlmSessionFromPickled loads the OlmSession details from a pickled base64 string. The input is decrypted with the supplied key.
Expand All @@ -61,11 +57,7 @@ func OlmSessionFromPickled(pickled, key []byte) (*OlmSession, error) {
return nil, fmt.Errorf("sessionFromPickled: %w", olm.ErrEmptyInput)
}
a := &OlmSession{}
err := a.Unpickle(pickled, key)
if err != nil {
return nil, err
}
return a, nil
return a, a.Unpickle(pickled, key)
}

// NewOlmSession creates a new Session.
Expand Down

0 comments on commit 636eef3

Please sign in to comment.