From 866b07783fa13e5b76ac3f8bb2221ed183ee155f Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Mon, 1 Mar 2021 17:56:45 -0500 Subject: lib: add AESDecryptCTR --- lib/aes.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'lib/aes.go') 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) -- cgit v1.2.3