From 32da174d1e7da3366ff8165c20cf7d063e5c40b0 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sun, 22 Nov 2020 12:09:33 -0500 Subject: lib: aes: add AESInvCipher --- lib/aes.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lib/aes.go') diff --git a/lib/aes.go b/lib/aes.go index 476b366..ebad334 100644 --- a/lib/aes.go +++ b/lib/aes.go @@ -3,6 +3,40 @@ package lib +func AESInvCipher(in, ky []byte) []byte { + nb := 4 + nr := 10 + + // Generate key schedule from key. + ks := KeyExpansion(ky) + + // Make state from input and do first round key + // transformation. + state := MkState(in) + state = AddRoundKey(state, ks[(nr*nb):((nr+1)*nb)]) + + for round := nr - 1; round >= 1; round-- { + state = InvShiftRows(state) + state = InvSubBytes(state) + state = AddRoundKey(state, ks[(round*nb):((round+1)*nb)]) + state = InvMixColumns(state) + } + state = InvShiftRows(state) + state = InvSubBytes(state) + state = AddRoundKey(state, ks[0:nb]) + + // Make output. + output := make([]byte, 4*nb) + i := 0 + for c := 0; c < nb; c++ { + for r := 0; r < 4; r++ { + output[i] = state[r][c] + i++ + } + } + return output +} + func InvMixColumns(state [][]byte) [][]byte { // Initialize new state. -- cgit v1.2.3