1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Key handling functions for PPC AES implementation
5 * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
8 #include <asm/ppc_asm.h>
11 #define LOAD_KEY(d, s, off) \
14 #define LOAD_KEY(d, s, off) \
19 #define INITIALIZE_KEY \
20 stwu r1,-32(r1); /* create stack frame */ \
21 stw r14,8(r1); /* save registers */ \
25 #define FINALIZE_KEY \
26 lwz r14,8(r1); /* restore registers */ \
29 xor r5,r5,r5; /* clear sensitive data */ \
37 addi r1,r1,32; /* cleanup stack */
39 #define LS_BOX(r, t1, t2) \
40 lis t2,PPC_AES_4K_ENCTAB@h; \
41 ori t2,t2,PPC_AES_4K_ENCTAB@l; \
42 rlwimi t2,r,4,20,27; \
44 rlwimi r,t1,0,24,31; \
45 rlwimi t2,r,28,20,27; \
47 rlwimi r,t1,8,16,23; \
48 rlwimi t2,r,20,20,27; \
50 rlwimi r,t1,16,8,15; \
51 rlwimi t2,r,12,20,27; \
55 #define GF8_MUL(out, in, t1, t2) \
56 lis t1,0x8080; /* multiplication in GF8 */ \
68 * ppc_expand_key_128(u32 *key_enc, const u8 *key)
70 * Expand 128 bit key into 176 bytes encryption key. It consists of
71 * key itself plus 10 rounds with 16 bytes each
74 _GLOBAL(ppc_expand_key_128)
80 stw r5,0(r3) /* key[0..3] = input data */
84 li r16,10 /* 10 expansion rounds */
85 lis r0,0x0100 /* RCO(1) */
88 mr r14,r8 /* apply LS_BOX to 4th temp */
92 xor r5,r5,r14 /* xor next 4 keys */
96 stw r5,0(r3) /* store next 4 keys */
100 GF8_MUL(r0, r0, r4, r14) /* multiply RCO by 2 in GF */
103 bt eq,ppc_expand_128_end
104 b ppc_expand_128_loop
110 * ppc_expand_key_192(u32 *key_enc, const u8 *key)
112 * Expand 192 bit key into 208 bytes encryption key. It consists of key
113 * itself plus 12 rounds with 16 bytes each
116 _GLOBAL(ppc_expand_key_192)
130 li r16,8 /* 8 expansion rounds */
131 lis r0,0x0100 /* RCO(1) */
134 mr r14,r10 /* apply LS_BOX to 6th temp */
138 xor r5,r5,r14 /* xor next 6 keys */
149 cmpwi r16,0 /* last round early kick out */
150 bt eq,ppc_expand_192_end
153 GF8_MUL(r0, r0, r4, r14) /* multiply RCO GF8 */
154 b ppc_expand_192_loop
160 * ppc_expand_key_256(u32 *key_enc, const u8 *key)
162 * Expand 256 bit key into 240 bytes encryption key. It consists of key
163 * itself plus 14 rounds with 16 bytes each
166 _GLOBAL(ppc_expand_key_256)
184 li r16,7 /* 7 expansion rounds */
185 lis r0,0x0100 /* RCO(1) */
188 mr r14,r12 /* apply LS_BOX to 8th temp */
192 xor r5,r5,r14 /* xor 4 keys */
197 LS_BOX(r14, r15, r4) /* apply LS_BOX to 4th temp */
198 xor r9,r9,r14 /* xor 4 keys */
207 cmpwi r16,0 /* last round early kick out */
208 bt eq,ppc_expand_256_end
213 GF8_MUL(r0, r0, r4, r14)
214 b ppc_expand_256_loop
220 * ppc_generate_decrypt_key: derive decryption key from encryption key
221 * number of bytes to handle are calculated from length of key (16/24/32)
224 _GLOBAL(ppc_generate_decrypt_key)
227 lwzx r7,r4,r6 /* first/last 4 words are same */
251 ppc_generate_decrypt_block:
254 ppc_generate_decrypt_word:
256 GF8_MUL(r7, r6, r0, r7)
257 GF8_MUL(r8, r7, r0, r8)
258 GF8_MUL(r9, r8, r0, r9)
273 bdnz ppc_generate_decrypt_word
277 bt gt,ppc_generate_decrypt_block