|
| 1 | +package aegis128x2 |
| 2 | + |
| 3 | +// #include <aegis.h> |
| 4 | +// #cgo CFLAGS: -I../common/libaegis/src/include |
| 5 | +import "C" |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/cipher" |
| 9 | + |
| 10 | + "github.com/jedisct1/go-libaegis/common" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + KeySize = 16 |
| 15 | + NonceSize = 16 |
| 16 | +) |
| 17 | + |
| 18 | +type Aegis128X2 struct { |
| 19 | + common.Aegis |
| 20 | +} |
| 21 | + |
| 22 | +// The nonce size, in bytes. |
| 23 | +func (aead *Aegis128X2) NonceSize() int { |
| 24 | + return NonceSize |
| 25 | +} |
| 26 | + |
| 27 | +// New returns a new AEAD that uses the provided key and tag length. |
| 28 | +// The key must be 16 bytes long. |
| 29 | +// The tag length must be 16 or 32. |
| 30 | +func New(key []byte, tagLen int) (cipher.AEAD, error) { |
| 31 | + if len(key) != KeySize { |
| 32 | + return nil, common.ErrBadKeyLength |
| 33 | + } |
| 34 | + if tagLen != 16 && tagLen != 32 { |
| 35 | + return nil, common.ErrBadTagLength |
| 36 | + } |
| 37 | + a := new(Aegis128X2) |
| 38 | + a.TagLen = tagLen |
| 39 | + a.Key = key |
| 40 | + return a, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (aead *Aegis128X2) Seal(dst, nonce, cleartext, additionalData []byte) []byte { |
| 44 | + if len(nonce) != aead.NonceSize() { |
| 45 | + panic("aegis: invalid nonce length") |
| 46 | + } |
| 47 | + outLen := len(cleartext) + aead.TagLen |
| 48 | + var buf []byte |
| 49 | + inplace := false |
| 50 | + if cap(dst)-len(dst) >= outLen { |
| 51 | + inplace = true |
| 52 | + buf = dst[len(dst) : len(dst)+outLen] |
| 53 | + } else { |
| 54 | + buf = make([]byte, outLen) |
| 55 | + } |
| 56 | + res := C.aegis128x2_encrypt((*C.uchar)(&buf[0]), C.size_t(aead.TagLen), slicePointerOrNull(cleartext), |
| 57 | + C.size_t(len(cleartext)), slicePointerOrNull(additionalData), C.size_t(len(additionalData)), (*C.uchar)(&nonce[0]), (*C.uchar)(&aead.Key[0])) |
| 58 | + if res != 0 { |
| 59 | + panic("encryption failed") |
| 60 | + } |
| 61 | + if inplace { |
| 62 | + return dst[:len(dst)+outLen] |
| 63 | + } |
| 64 | + return append(dst, buf...) |
| 65 | +} |
| 66 | + |
| 67 | +func (aead *Aegis128X2) Open(plaintext, nonce, ciphertext, additionalData []byte) ([]byte, error) { |
| 68 | + if len(nonce) != aead.NonceSize() { |
| 69 | + return nil, common.ErrBadNonceLength |
| 70 | + } |
| 71 | + if len(ciphertext) < aead.TagLen { |
| 72 | + return nil, common.ErrTruncated |
| 73 | + } |
| 74 | + outLen := len(ciphertext) - aead.TagLen |
| 75 | + var buf []byte |
| 76 | + inplace := false |
| 77 | + if cap(plaintext)-len(plaintext) >= outLen { |
| 78 | + inplace = true |
| 79 | + buf = plaintext[len(plaintext) : len(plaintext)+outLen] |
| 80 | + } else { |
| 81 | + buf = make([]byte, len(ciphertext)-aead.TagLen) |
| 82 | + } |
| 83 | + res := C.aegis128x2_decrypt((*C.uchar)(&buf[0]), slicePointerOrNull(ciphertext), |
| 84 | + C.size_t(len(ciphertext)), C.size_t(aead.TagLen), slicePointerOrNull(additionalData), C.size_t(len(additionalData)), (*C.uchar)(&nonce[0]), (*C.uchar)(&aead.Key[0])) |
| 85 | + if res != 0 { |
| 86 | + return nil, common.ErrAuth |
| 87 | + } |
| 88 | + if inplace { |
| 89 | + return plaintext[:len(plaintext)+outLen], nil |
| 90 | + } |
| 91 | + return append(plaintext, buf...), nil |
| 92 | +} |
| 93 | + |
| 94 | +func slicePointerOrNull(s []byte) (ptr *C.uchar) { |
| 95 | + if len(s) == 0 { |
| 96 | + return |
| 97 | + } |
| 98 | + return (*C.uchar)(&s[0]) |
| 99 | +} |
0 commit comments