2 * Key handling functions for PPC AES implementation
4 * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
13 #include <asm/ppc_asm.h>
16 #define LOAD_KEY(d, s, off) \
19 #define LOAD_KEY(d, s, off) \
24 #define INITIALIZE_KEY \
25 stwu r1,-32(r1); /* create stack frame */ \
26 stw r14,8(r1); /* save registers */ \
30 #define FINALIZE_KEY \
31 lwz r14,8(r1); /* restore registers */ \
34 xor r5,r5,r5; /* clear sensitive data */ \
42 addi r1,r1,32; /* cleanup stack */
44 #define LS_BOX(r, t1, t2) \
45 lis t2,PPC_AES_4K_ENCTAB@h; \
46 ori t2,t2,PPC_AES_4K_ENCTAB@l; \
47 rlwimi t2,r,4,20,27; \
49 rlwimi r,t1,0,24,31; \
50 rlwimi t2,r,28,20,27; \
52 rlwimi r,t1,8,16,23; \
53 rlwimi t2,r,20,20,27; \
55 rlwimi r,t1,16,8,15; \
56 rlwimi t2,r,12,20,27; \
60 #define GF8_MUL(out, in, t1, t2) \
61 lis t1,0x8080; /* multiplication in GF8 */ \
73 * ppc_expand_key_128(u32 *key_enc, const u8 *key)
75 * Expand 128 bit key into 176 bytes encryption key. It consists of
76 * key itself plus 10 rounds with 16 bytes each
79 _GLOBAL(ppc_expand_key_128)
85 stw r5,0(r3) /* key[0..3] = input data */
89 li r16,10 /* 10 expansion rounds */
90 lis r0,0x0100 /* RCO(1) */
93 mr r14,r8 /* apply LS_BOX to 4th temp */
97 xor r5,r5,r14 /* xor next 4 keys */
101 stw r5,0(r3) /* store next 4 keys */
105 GF8_MUL(r0, r0, r4, r14) /* multiply RCO by 2 in GF */
108 bt eq,ppc_expand_128_end
109 b ppc_expand_128_loop
115 * ppc_expand_key_192(u32 *key_enc, const u8 *key)
117 * Expand 192 bit key into 208 bytes encryption key. It consists of key
118 * itself plus 12 rounds with 16 bytes each
121 _GLOBAL(ppc_expand_key_192)
135 li r16,8 /* 8 expansion rounds */
136 lis r0,0x0100 /* RCO(1) */
139 mr r14,r10 /* apply LS_BOX to 6th temp */
143 xor r5,r5,r14 /* xor next 6 keys */
154 cmpwi r16,0 /* last round early kick out */
155 bt eq,ppc_expand_192_end
158 GF8_MUL(r0, r0, r4, r14) /* multiply RCO GF8 */
159 b ppc_expand_192_loop
165 * ppc_expand_key_256(u32 *key_enc, const u8 *key)
167 * Expand 256 bit key into 240 bytes encryption key. It consists of key
168 * itself plus 14 rounds with 16 bytes each
171 _GLOBAL(ppc_expand_key_256)
189 li r16,7 /* 7 expansion rounds */
190 lis r0,0x0100 /* RCO(1) */
193 mr r14,r12 /* apply LS_BOX to 8th temp */
197 xor r5,r5,r14 /* xor 4 keys */
202 LS_BOX(r14, r15, r4) /* apply LS_BOX to 4th temp */
203 xor r9,r9,r14 /* xor 4 keys */
212 cmpwi r16,0 /* last round early kick out */
213 bt eq,ppc_expand_256_end
218 GF8_MUL(r0, r0, r4, r14)
219 b ppc_expand_256_loop
225 * ppc_generate_decrypt_key: derive decryption key from encryption key
226 * number of bytes to handle are calculated from length of key (16/24/32)
229 _GLOBAL(ppc_generate_decrypt_key)
232 lwzx r7,r4,r6 /* first/last 4 words are same */
256 ppc_generate_decrypt_block:
259 ppc_generate_decrypt_word:
261 GF8_MUL(r7, r6, r0, r7)
262 GF8_MUL(r8, r7, r0, r8)
263 GF8_MUL(r9, r8, r0, r9)
278 bdnz ppc_generate_decrypt_word
282 bt gt,ppc_generate_decrypt_block