Catch undersize bytes and panics

This commit is contained in:
Simon 2022-02-06 03:22:15 +00:00
parent 43d94fdf50
commit 7ee8da5d3e
1 changed files with 18 additions and 3 deletions

View File

@ -11,6 +11,10 @@ const noncesize int = 16
func Encrypt(key []byte, plaintext []byte, additionalData []byte) ([]byte, error) {
if len(key) < keysize {
return nil, errors.New("Failed to encrypt")
}
cipher := make([]byte, len(plaintext)+abytes+noncesize)
var cipherlen uint64 = (uint64)(len(cipher))
nonce := genNonce()
@ -27,15 +31,25 @@ func Encrypt(key []byte, plaintext []byte, additionalData []byte) ([]byte, error
)
if ret != 0 {
return nil, errors.New("Failed to decrypt")
return nil, errors.New("Failed to encrypt")
}
copy(cipher, nonce)
return cipher[:(int)(cipherlen)+noncesize], nil
}
func Decrypt(key []byte, ciphertext []byte, additionalData []byte) ([]byte, bool, error) {
func Decrypt(key []byte, ciphertext []byte, additionalData []byte) (plaintext []byte, auth bool, err error) {
plaintext := make([]byte, len(ciphertext))
if len(ciphertext) <= noncesize || len(key) < keysize {
return nil, false, errors.New("Failed to decrypt")
}
defer func() {
recover()
err = errors.New("Recoverd from panic in decrypt")
auth = false
}()
plaintext = make([]byte, len(ciphertext))
var plaintextLen uint64 = (uint64)(len(plaintext))
ret := romulus_m_decrypt(
plaintext,
@ -48,6 +62,7 @@ func Decrypt(key []byte, ciphertext []byte, additionalData []byte) ([]byte, bool
ciphertext[:noncesize],
key[:keysize],
)
if ret == -1 {
return nil, false, errors.New("Failed to authenticate")
} else if ret != 0 {