From c0e9feb0e9d78d02c4fb71569db26f31e801d6e3 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sun, 6 Dec 2020 14:48:55 -0500 Subject: lib/aes: add aesCipher --- lib/aes.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib/aes.go') diff --git a/lib/aes.go b/lib/aes.go index 4c0c86d..e9b97d3 100644 --- a/lib/aes.go +++ b/lib/aes.go @@ -32,6 +32,42 @@ func AESDecryptECB(cipher, key []byte) []byte { return output } + +func aesCipher(in, ky []byte) []byte { + nb := 4 + nr := 10 + + // Generate key schedule from key. + ks := aesKeyExpansion(ky) + + // Make state from input and do first round key + // transformation. + state := aesMkState(in) + state = aesAddRoundKey(state, ks[0:4]) + + for round := 1; round <= nr-1; round++ { + state = aesSubBytes(state) + state = aesShiftRows(state) + state = aesMixColumns(state) + state = aesAddRoundKey(state, ks[(round*nb):((round+1)*nb)]) + } + state = aesSubBytes(state) + state = aesShiftRows(state) + state = aesAddRoundKey(state, ks[(nr*nb):((nr+1)*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 aesInvCipher(in, ky []byte) []byte { nb := 4 nr := 10 -- cgit v1.2.3