diff options
author | rsiddharth <s@ricketyspace.net> | 2021-03-03 19:55:41 -0500 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2021-03-03 19:55:41 -0500 |
commit | e9f7ddf6c40844043d517931e157f5c53f8d36b4 (patch) | |
tree | 3890f2fd7d01386ed86afd0d5f88334cadb84bf7 /lib | |
parent | 8b3ef249bc78e09ef5afb7bbc32c9c0368746848 (diff) |
lib: add AESEncryptCTR
Diffstat (limited to 'lib')
-rw-r--r-- | lib/aes.go | 21 |
1 files changed, 16 insertions, 5 deletions
@@ -3,12 +3,23 @@ package lib +func AESEncryptCTR(plain, key []byte, ctrFunc func() []byte) ([]byte, error) { + if len(key) != 16 { + return []byte{}, CPError{"key length != 16"} + } + return aesCipherCTR(plain, key, ctrFunc) +} + func AESDecryptCTR(cipher, key []byte, ctrFunc func() []byte) ([]byte, error) { if len(key) != 16 { return []byte{}, CPError{"key length != 16"} } - iter := len(cipher) / 16 - if len(cipher)%16 != 0 { + return aesCipherCTR(cipher, key, ctrFunc) +} + +func aesCipherCTR(in, key []byte, ctrFunc func() []byte) ([]byte, error) { + iter := len(in) / 16 + if len(in)%16 != 0 { iter += 1 } output := make([]byte, 0) @@ -19,10 +30,10 @@ func AESDecryptCTR(cipher, key []byte, ctrFunc func() []byte) ([]byte, error) { } s := (i * 16) e := (i * 16) + 16 - if e > len(cipher) { - e = len(cipher) + if e > len(in) { + e = len(in) } - c := cipher[s:e] + c := in[s:e] output = append(output, FixedXORBytes(aesCipher(ib, key)[0:len(c)], c)...) } return output, nil |