Commit | Line | Data |
---|---|---|
8b3d7f30 | 1 | /* -*-c-*- |
8b3d7f30 | 2 | * |
3 | * Low-level stuff for all Rijndael block sizes | |
4 | * | |
5 | * (c) 2001 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
8b3d7f30 | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
12 | * Catacomb is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU Library General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
45c0fd36 | 16 | * |
8b3d7f30 | 17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
45c0fd36 | 21 | * |
8b3d7f30 | 22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free | |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
8b3d7f30 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
226639f3 MW |
30 | #include "config.h" |
31 | ||
8b3d7f30 | 32 | #include <assert.h> |
33 | #include <stdio.h> | |
34 | ||
35 | #include <mLib/bits.h> | |
36 | ||
37 | #include "blkc.h" | |
226639f3 | 38 | #include "dispatch.h" |
8b3d7f30 | 39 | #include "gcipher.h" |
40 | #include "rijndael.h" | |
41 | #include "rijndael-base.h" | |
8b3d7f30 | 42 | |
43 | /*----- Global variables --------------------------------------------------*/ | |
44 | ||
45 | const octet rijndael_keysz[] = { KSZ_RANGE, RIJNDAEL_KEYSZ, 4, 32, 4 }; | |
46 | ||
8b3d7f30 | 47 | /*----- Main code ---------------------------------------------------------*/ |
48 | ||
49 | /* --- @rijndael_setup@ --- * | |
50 | * | |
51 | * Arguments: @rijndael_ctx *k@ = pointer to context to initialize | |
52 | * @unsigned nb@ = number of words in the block | |
53 | * @const void *buf@ = pointer to buffer of key material | |
54 | * @size_t sz@ = size of the key material | |
55 | * | |
56 | * Returns: --- | |
57 | * | |
58 | * Use: Low-level key-scheduling. | |
59 | */ | |
60 | ||
226639f3 MW |
61 | static void simple_setup(rijndael_ctx *k, unsigned nb, |
62 | const void *buf, unsigned nk) | |
8b3d7f30 | 63 | { |
226639f3 | 64 | unsigned nr = k->nr, nw; |
8b3d7f30 | 65 | unsigned i, j, jj; |
66 | const octet *p; | |
67 | uint32 ww; | |
68 | ||
8b3d7f30 | 69 | /* --- Fetch the first key words out --- */ |
70 | ||
71 | p = buf; | |
72 | for (i = 0; i < nk; i++) { | |
38333dc2 | 73 | k->w[i] = LOAD32_B(p); |
8b3d7f30 | 74 | p += 4; |
75 | } | |
76 | ||
77 | /* --- Expand this material to fill the rest of the table --- */ | |
78 | ||
79 | nw = (nr + 1) * nb; | |
80 | ww = k->w[i - 1]; | |
81 | p = RCON; | |
82 | for (; i < nw; i++) { | |
83 | uint32 w = k->w[i - nk]; | |
84 | if (i % nk == 0) { | |
38333dc2 MW |
85 | ww = ROL32(ww, 8); |
86 | w ^= SUB(S, ww, ww, ww, ww) ^ (*p++ << 24); | |
8b3d7f30 | 87 | } else if (nk > 6 && i % nk == 4) |
88 | w ^= SUB(S, ww, ww, ww, ww); | |
89 | else | |
90 | w ^= ww; | |
91 | k->w[i] = ww = w; | |
92 | } | |
93 | ||
94 | /* --- Make the decryption keys --- */ | |
95 | ||
96 | j = nw; i = 0; | |
97 | ||
98 | j -= nb; jj = 0; | |
99 | for (; i < nb; i++) | |
100 | k->wi[i] = k->w[j + jj++]; | |
101 | ||
102 | for (; i < nw - nb; i += nb) { | |
103 | j -= nb; | |
104 | for (jj = 0; jj < nb; jj++) { | |
105 | uint32 w = k->w[j + jj]; | |
106 | k->wi[i + jj] = MIX(U, w, w, w, w); | |
107 | } | |
108 | } | |
109 | ||
110 | j -= nb; jj = 0; | |
111 | for (; i < nw; i++) | |
112 | k->wi[i] = k->w[j + jj++]; | |
113 | } | |
114 | ||
8a1aa284 MW |
115 | CPU_DISPATCH(static, EMPTY, void, setup, |
116 | (rijndael_ctx *k, unsigned nb, const void *buf, unsigned nk), | |
226639f3 MW |
117 | (k, nb, buf, nk), pick_setup, simple_setup) |
118 | ||
0f23f75f MW |
119 | #if CPUFAM_X86 || CPUFAM_AMD64 |
120 | extern setup__functype rijndael_setup_x86ish_aesni; | |
226639f3 | 121 | #endif |
26e182fc MW |
122 | #if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO |
123 | extern setup__functype rijndael_setup_arm_crypto; | |
124 | #endif | |
226639f3 MW |
125 | |
126 | static setup__functype *pick_setup(void) | |
127 | { | |
0f23f75f MW |
128 | #if CPUFAM_X86 || CPUFAM_AMD64 |
129 | DISPATCH_PICK_COND(rijndael_setup, rijndael_setup_x86ish_aesni, | |
fac645f7 | 130 | cpu_feature_p(CPUFEAT_X86_AESNI)); |
26e182fc MW |
131 | #endif |
132 | #if CPUFAM_ARMEL && HAVE_AS_ARMV8_CRYPTO | |
133 | DISPATCH_PICK_COND(rijndael_setup, rijndael_setup_arm_crypto, | |
134 | cpu_feature_p(CPUFEAT_ARM_AES)); | |
226639f3 | 135 | #endif |
fac645f7 | 136 | DISPATCH_PICK_FALLBACK(rijndael_setup, simple_setup); |
226639f3 MW |
137 | } |
138 | ||
139 | void rijndael_setup(rijndael_ctx *k, unsigned nb, const void *buf, size_t sz) | |
140 | { | |
141 | unsigned nk, nr; | |
142 | ||
143 | /* --- Sort out the key size --- */ | |
144 | ||
145 | KSZ_ASSERT(rijndael, sz); | |
146 | nk = sz / 4; | |
147 | ||
148 | /* --- Select the number of rounds --- */ | |
149 | ||
150 | nr = (nk > nb ? nk : nb) + 6; | |
151 | if (nr < 10) | |
152 | nr = 10; | |
153 | k->nr = nr; | |
154 | ||
155 | /* --- Do the main setup --- */ | |
156 | ||
157 | setup(k, nb, buf, nk); | |
158 | } | |
159 | ||
8b3d7f30 | 160 | /*----- That's all, folks -------------------------------------------------*/ |