summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2021-03-03 19:55:41 -0500
committerrsiddharth <s@ricketyspace.net>2021-03-03 19:55:41 -0500
commite9f7ddf6c40844043d517931e157f5c53f8d36b4 (patch)
tree3890f2fd7d01386ed86afd0d5f88334cadb84bf7 /lib
parent8b3ef249bc78e09ef5afb7bbc32c9c0368746848 (diff)
lib: add AESEncryptCTR
Diffstat (limited to 'lib')
-rw-r--r--lib/aes.go21
1 files changed, 16 insertions, 5 deletions
diff --git a/lib/aes.go b/lib/aes.go
index 6fdd759..82ee8ed 100644
--- a/lib/aes.go
+++ b/lib/aes.go
@@ -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