diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-05-29 16:54:03 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-05-29 16:54:03 +0300 |
commit | 03a18a12384947b76beff2aef01b085e2ee4c8a6 (patch) | |
tree | 092a155e6f457926bbef2b0fbfaf4b1cdb12c137 /src | |
parent | add test/ (diff) | |
download | aes-tools-03a18a12384947b76beff2aef01b085e2ee4c8a6.tar.gz aes-tools-03a18a12384947b76beff2aef01b085e2ee4c8a6.zip |
aes192: more comments & optimize key generation
Diffstat (limited to '')
-rw-r--r-- | src/aes192.asm | 247 |
1 files changed, 155 insertions, 92 deletions
diff --git a/src/aes192.asm b/src/aes192.asm index 9695961..9cd4cfa 100644 --- a/src/aes192.asm +++ b/src/aes192.asm @@ -53,107 +53,170 @@ inverted_key_schedule oword 13 dup(0) @raw_aes192ecb_decrypt@48 endp expand_keys_192ecb proc - ; key = k0 k1 k2 k3 k4 k5 - ; xmm1 = k0 k1 k2 k3 - ; xmm2 = 0 0 k5 k4 - - ; w[0] = k0 k1 k2 k3 - ; w[1] = k4 k5 - - - + ; A "word" (in terms of the FIPS 187 standard) is a 32-bit block. + ; Words are denoted by `w[N]`. + ; + ; A key schedule is composed of 12 "regular" keys and a dumb key for + ; the "whitening" step. + ; It's stored in `key_schedule`. + ; + ; A key schedule is thus composed of 52 "words". + ; The FIPS standard includes an algorithm to calculate these words via + ; a simple loop: + ; ; i = 6 - ; while (i < 52): + ; while i < 52: ; temp = w[i - 1] - ; if (i % 6 == 0): - ; temp = SubWord(RotWord(w[i - 1])) * Rcon - ; w[i] = w[i - 6] * temp + ; if i % 6 == 0: + ; temp = SubWord(RotWord(temp))^Rcon + ; w[i] = w[i - 6]^temp ; i = i + 1 + ; + ; The loop above may be unrolled like this: + ; + ; w[6] = SubWord(RotWord(w[5]))^Rcon^w[0] + ; w[7] = w[6]^w[1] + ; = SubWord(RotWord(w[5]))^Rcon^w[0]^w[1] + ; w[8] = w[7]^w[2] + ; = SubWord(RotWord(w[5]))^Rcon^w[0]^w[1]^w[2] + ; w[9] = w[8]^w[3] + ; = SubWord(RotWord(w[5]))^Rcon^w[0]^w[1]^w[2]^w[3] + ; w[10] = w[9]^w[4] + ; = SubWord(RotWord(w[5]))^Rcon^w[0]^w[1]^w[2]^w[3]^w[4] + ; w[11] = w[10]^w[5] + ; = SubWord(RotWord(w[5]))^Rcon^w[0]^w[1]^w[2]^w[3]^w[4]^w[5] + ; w[12] = SubWord(RotWord(w[11]))^Rcon^w[6] + ; w[13] = w[12]^w[7] + ; = SubWord(RotWord(w[11]))^Rcon^w[6]^w[7] + ; w[14] = w[13]^w[8] + ; = SubWord(RotWord(w[11]))^Rcon^w[6]^w[7]^w[8] + ; w[15] = w[14]^w[9] + ; = SubWord(RotWord(w[11]))^Rcon^w[6]^w[7]^w[8]^w[9] + ; w[16] = w[15]^w[10] + ; = SubWord(RotWord(w[11]))^Rcon^w[6]^w[7]^w[8]^w[9]^w[10] + ; w[17] = w[16]^w[11] + ; = SubWort(RotWord(w[11]))^Rcon^w[6]^w[7]^w[8]^w[9]^w[10]^w[11] + ; + ; ... and so on. + ; + ; The Intel AES-NI instruction set facilitates calculating SubWord + ; and RotWord using `aeskeygenassist`, which is used in this routine. + ; + ; Preconditions: + ; * xmm2[63:32] == w[5], + ; * xmm2[31:0] == w[4], + ; * xmm1[127:96] == w[3], + ; * xmm1[95:64] == w[2], + ; * xmm1[63:32] == w[1], + ; * xmm1[31:0] == w[0]. + + movdqa [key_schedule], xmm1 ; sets w[0], w[1], w[2], w[3] + movdqa [key_schedule + 10h], xmm2 ; sets w[4], w[5] - ; w[6] = SubWord(RotWord(w[5])) * Rcon * w[0] - ; w[7] = w[6] * w[1] - ; = SubWord(RotWord(w[5])) * Rcon * w[0] * w[1] - ; w[8] = w[7] * w[2] - ; = SubWord(RotWord(w[5])) * Rcon * w[0] * w[1] * w[2] - ; w[9] = w[8] * w[3] - ; = SubWord(RotWord(w[5])) * Rcon * w[0] * w[1] * w[2] * w[3] - ; w[10] = w[9] * w[4] - ; = SubWord(RotWord(w[5])) * Rcon * w[0] * w[1] * w[2] * w[3] * w[4] - ; w[11] = w[10] * w[5] - ; = SubWord(RotWord(w[5])) * Rcon * w[0] * w[1] * w[2] * w[3] * w[4] * w[5] - - movdqa [key_schedule], xmm1 - movdqa [key_schedule + 10h], xmm2 - - lea ecx, [key_schedule + 18h] - aeskeygenassist xmm7, xmm2, 1 - call gen_round_key - aeskeygenassist xmm7, xmm2, 2 - call gen_round_key - aeskeygenassist xmm7, xmm2, 4 - call gen_round_key - aeskeygenassist xmm7, xmm2, 8 - call gen_round_key - aeskeygenassist xmm7, xmm2, 10h - call gen_round_key - aeskeygenassist xmm7, xmm2, 20h - call gen_round_key - aeskeygenassist xmm7, xmm2, 40h - call gen_round_key - aeskeygenassist xmm7, xmm2, 80h - call gen_round_key + lea ecx, [key_schedule + 18h] ; ecx = &w[6] + aeskeygenassist xmm7, xmm2, 1 ; xmm7[63:32] = RotWord(SubWord(w[5]))^Rcon, + call gen_round_key ; sets w[6], w[7], w[8], w[9], w[10], w[11] + aeskeygenassist xmm7, xmm2, 2 ; xmm7[63:32] = RotWord(SubWord(w[11]))^Rcon + call gen_round_key ; sets w[12], w[13], w[14], w[15], w[16], w[17] + aeskeygenassist xmm7, xmm2, 4 ; xmm7[63:32] = RotWord(SubWord(w[17]))^Rcon + call gen_round_key ; sets w[18], w[19], w[20], w[21], w[22], w[23] + aeskeygenassist xmm7, xmm2, 8 ; xmm7[63:32] = RotWord(SubWord(w[23]))^Rcon + call gen_round_key ; sets w[24], w[25], w[26], w[27], w[28], w[29] + aeskeygenassist xmm7, xmm2, 10h ; xmm7[63:32] = RotWord(SubWord(w[29]))^Rcon + call gen_round_key ; sets w[30], w[31], w[32], w[33], w[34], w[35] + aeskeygenassist xmm7, xmm2, 20h ; xmm7[63:32] = RotWord(SubWord(w[35]))^Rcon + call gen_round_key ; sets w[36], w[37], w[38], w[39], w[40], w[41] + aeskeygenassist xmm7, xmm2, 40h ; xmm7[63:32] = RotWord(SubWord(w[41]))^Rcon + call gen_round_key ; sets w[42], w[43], w[44], w[45], w[46], w[47] + aeskeygenassist xmm7, xmm2, 80h ; xmm7[63:32] = RotWord(SubWord(w[49]))^Rcon + call gen_round_key ; sets w[48], w[49], w[50], w[51], w[52], w[53] // FIXME call invert_key_schedule ret gen_round_key: - ; xmm1 = x3 x2 x1 x0 - ; xmm2 = - - x5 x4 - ; xmm7 = RotWord(SubWord(-)) xor Rcon - ; SubWord(-) - ; RotWord(SubWord(x5)) xor Rcon - ; SubWord(x5) - movdqa xmm6, xmm1 - - pslldq xmm6, 4 ; xmm6 = x2 x1 x0 0 - pxor xmm1, xmm6 ; xmm1 = (x3 * x2) (x1 * x2) (x1 * x0) x0 - pslldq xmm6, 4 ; xmm6 = x1 x0 0 0 - pxor xmm1, xmm6 ; xmm1 = (x3 * x2 * x1) (x1 * x2 * x0) (x1 * x0) x0 - pslldq xmm6, 4 ; xmm6 = x0 0 0 0 - pxor xmm1, xmm6 ; xmm1 = (x3 * x2 * x1 * x0) (x1 * x2 * x0) (x1 * x0) x0 - - pshufd xmm7, xmm7, 55h ; xmm7 = RotWord(SubWord(x5)) * Rcon - ; RotWord(SubWord(x5)) * Rcon - ; RotWord(SubWord(x5)) * Rcon - ; RotWord(SubWord(x5)) * Rcon - - pxor xmm1, xmm7 ; xmm1 = RotWord(SubWord(x5)) * Rcon * x3 * x2 * x1 * x0 - ; RotWord(SubWord(x5)) * Rcon * x2 * x1 * x0 - ; RotWord(SubWord(x5)) * Rcon * x1 * x0 - ; RotWord(SubWord(x5)) * Rcon * x0 - - movq qword ptr [ecx], xmm1 - add ecx, 8 - - pshufd xmm7, xmm1, 0FFh ; xmm7 = - - ; - - ; RotWord(SubWord(x5)) * Rcon * x3 * x2 * x1 * x0 - ; RotWord(SubWord(x5)) * Rcon * x3 * x2 * x1 * x0 - pxor xmm7, xmm2 ; xmm7 = - - ; - - ; RotWord(SubWord(x5)) * Rcon * x5 * x3 * x2 * x1 * x0 - ; RotWord(SubWord(x5)) * Rcon * x4 * x3 * x2 * x1 * x0 - pslldq xmm2, 4 ; xmm2 = - k5 k4 0 - pxor xmm7, xmm2 ; xmm7 = - - ; - - ; RotWord(SubWord(x5)) * Rcon * x5 * x4 * x3 * x2 * x1 * x0 - ; RotWord(SubWord(x5)) * Rcon * x4 * x3 * x2 * x1 * x0 - - movq xmm2, xmm7 - pslldq xmm7, 8 - movdqa xmm6, xmm1 - psrldq xmm6, 8 - por xmm7, xmm6 - movdqu [ecx], xmm7 - add ecx, 10h + ; Preconditions: + ; * xmm2[127:96] == 0, + ; * xmm2[95:64] == 0, + ; * xmm2[63:32] == w[i+5], + ; * xmm2[31:0] == w[i+4], + ; * xmm1[127:96] == w[i+3], + ; * xmm1[95:64] == w[i+2], + ; * xmm1[63:32] == w[i+1], + ; * xmm1[31:0] == w[i], + ; * xmm7[63:32] == RotWord(SubWord(w[i+5]))^Rcon, + ; * ecx == &w[i+6]. + ; + ; Postconditions: + ; * xmm2[127:96] == 0, + ; * xmm2[95:64] == 0, + ; * xmm2[63:32] == w[i+11] == RotWord(SubWord(w[i+5]))^Rcon^w[i+5]^w[i+4]^w[i+3]^w[i+2]^w[i+1]^w[i], + ; * xmm2[31:0] == w[i+10] == RotWord(SubWord(w[i+5]))^Rcon^w[i+4]^w[i+3]^w[i+2]^w[i+1]^w[i], + ; * xmm1[127:96] == w[i+9] == RotWord(SubWord(w[i+5]))^Rcon^w[i+3]^w[i+2]^w[i+1]^w[i], + ; * xmm1[95:64] == w[i+8] == RotWord(SubWord(w[i+5]))^Rcon^w[i+2]^w[i+1]^w[i], + ; * xmm1[63:32] == w[i+7] == RotWord(SubWord(w[i+5]))^Rcon^w[i+1]^w[i], + ; * xmm1[31:0] == w[i+6] == RotWord(SubWord(w[i+5]))^Rcon^w[i], + ; * ecx == &w[i+12], + ; * the value in xmm6 is also modified. + + ; Calculate + ; w[i+3]^w[i+2]^w[i+1]^w[i], + ; w[i+2]^w[i+1]^w[i], + ; w[i+1]^w[i] and + ; w[i]. + movdqa xmm6, xmm1 ; xmm6 = xmm1 + pslldq xmm6, 4 ; xmm6 <<= 4 + pxor xmm1, xmm6 ; xmm1 ^= xmm6 + pslldq xmm6, 4 ; xmm6 <<= 4 + pxor xmm1, xmm6 ; xmm1 ^= xmm6 + pslldq xmm6, 4 ; xmm6 <<= 4 + pxor xmm1, xmm6 ; xmm1 ^= xmm6 + ; xmm1[127:96] == w[i+3]^w[i+2]^w[i+1]^w[i] + ; xmm1[95:64] == w[i+2]^w[i+1]^w[i] + ; xmm1[63:32] == w[i+1]^w[i] + ; xmm1[31:0] == w[i] + + ; Calculate + ; w[i+9] == RotWord(SubWord(w[i+5]))^Rcon^w[i+3]^w[i+2]^w[i+1]^w[i], + ; w[i+8] == RotWord(SubWord(w[i+5]))^Rcon^w[i+2]^w[i+1]^w[i], + ; w[i+7] == RotWord(SubWord(w[i+5]))^Rcon^w[i+1]^w[i] and + ; w[i+6] == RotWord(SubWord(w[i+5]))^Rcon^w[i]. + pshufd xmm6, xmm7, 55h ; xmm6[127:96] = xmm6[95:64] = xmm6[63:32] = xmm6[31:0] = xmm7[63:32] + pxor xmm1, xmm6 ; xmm1 ^= xmm6 + ; xmm1[127:96] == w[i+9] == RotWord(SubWord(w[i+5]))^Rcon^w[i+3]^w[i+2]^w[i+1]^w[i] + ; xmm1[95:64] == w[i+8] == RotWord(SubWord(w[i+5]))^Rcon^w[i+2]^w[i+1]^w[i] + ; xmm1[63:32] == w[i+7] == RotWord(SubWord(w[i+5]))^Rcon^w[i+1]^w[i] + ; xmm1[31:0] == w[i+6] == RotWord(SubWord(w[i+5]))^Rcon^w[i] + + ; Set w[i+6], w[i+7], w[i+8] and w[i+9]. + movdqu [ecx], xmm1 ; w[i+6] = RotWord(SubWord(w[i+5]))^Rcon^w[i] + ; w[i+7] = RotWord(SubWord(w[i+5]))^Rcon^w[i+1]^w[i] + ; w[i+8] = RotWord(SubWord(w[i+5]))^Rcon^w[i+2]^w[i+1]^w[i] + ; w[i+9] = RotWord(SubWord(w[i+5]))^Rcon^w[i+3]^w[i+2]^w[i+1]^w[i] + add ecx, 10h ; ecx = &w[i+10] + + ; Calculate + ; w[i+5]^w[i+4], + ; w[i+4]. + pshufd xmm6, xmm2, 0F3h ; xmm6 = xmm2[31:0] << 4 + pxor xmm2, xmm6 ; xmm2 ^= xmm7 + ; xmm2[63:32] == w[i+5]^w[i+4] + ; xmm2[31:0] == w[i+4] + + ; Calculate + ; w[i+10] == RotWord(SubWord(w[i+5]))^Rcon^w[i+5]^w[i+4]^w[i+3]^w[i+2]^w[i+1]^w[i], + ; w[i+11] == RotWord(SubWord(w[i+5]))^Rcon^w[i+4]^w[i+3]^w[i+2]^w[i+1]^w[i]. + pshufd xmm6, xmm1, 0FFh ; xmm6[127:96] = xmm6[95:64] = xmm6[63:32] = xmm6[31:0] = xmm1[127:96] + psrldq xmm6, 8 ; xmm6 >>= 8 + pxor xmm2, xmm6 ; xmm2 ^= xmm6 + ; xmm2[63:32] == w[i+11] == RotWord(SubWord(w[i+5]))^Rcon^w[i+5]^w[i+4]^w[i+3]^w[i+2]^w[i+1]^w[i] + ; xmm2[31:0] == w[i+10] == RotWord(SubWord(w[i+5]))^Rcon^w[i+4]^w[i+3]^w[i+2]^w[i+1]^w[i] + + ; Set w[i+10] and w[i+11]. + movq qword ptr [ecx], xmm2 ; w[i+10] = RotWord(SubWord(w[i+5]))^Rcon^w[i+4]^w[i+3]^w[i+2]^w[i+1]^w[i] + ; w[i+11] = RotWord(SubWord(w[i+5]))^Rcon^w[i+5]^w[i+4]^w[i+3]^w[i+2]^w[i+1]^w[i] + add ecx, 8 ; ecx = &w[i+12] + ret invert_key_schedule: |