summaryrefslogtreecommitdiffstats
path: root/lib/aes.go
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2021-03-01 17:56:45 -0500
committerrsiddharth <s@ricketyspace.net>2021-03-01 17:56:45 -0500
commit866b07783fa13e5b76ac3f8bb2221ed183ee155f (patch)
treedd0722e2c6a444060ced10b4f7b6ccfcf6030ed1 /lib/aes.go
parente4f381aabc00c44ff669792510d71da11e7afa42 (diff)
lib: add AESDecryptCTR
Diffstat (limited to 'lib/aes.go')
-rw-r--r--lib/aes.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/aes.go b/lib/aes.go
index 8c4dd26..6fdd759 100644
--- a/lib/aes.go
+++ b/lib/aes.go
@@ -3,6 +3,31 @@
package lib
+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 {
+ iter += 1
+ }
+ output := make([]byte, 0)
+ for i := 0; i < iter; i++ {
+ ib := ctrFunc()
+ if len(ib) != 16 {
+ return []byte{}, CPError{"ctr length != 16"}
+ }
+ s := (i * 16)
+ e := (i * 16) + 16
+ if e > len(cipher) {
+ e = len(cipher)
+ }
+ c := cipher[s:e]
+ output = append(output, FixedXORBytes(aesCipher(ib, key)[0:len(c)], c)...)
+ }
+ return output, nil
+}
+
func AESEncryptCBC(plain, key, iv []byte) []byte {
// Pad input
plain = Pkcs7Padding(plain, 16)