aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/aes_generic.c
diff options
context:
space:
mode:
authorArd Biesheuvel <ard.biesheuvel@linaro.org>2019-07-02 15:41:33 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2019-07-26 00:56:06 -0400
commit5bb12d7825adf0e80b849a273834f3131a6cc4e1 (patch)
tree588cb7e5183d7e194a94c944736f721bc425634c /crypto/aes_generic.c
parentc184472902d87189082e5a349051197e252ae9af (diff)
crypto: aes-generic - drop key expansion routine in favor of library version
Drop aes-generic's version of crypto_aes_expand_key(), and switch to the key expansion routine provided by the AES library. AES key expansion is not performance critical, and it is better to have a single version shared by all AES implementations. Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/aes_generic.c')
-rw-r--r--crypto/aes_generic.c153
1 files changed, 2 insertions, 151 deletions
diff --git a/crypto/aes_generic.c b/crypto/aes_generic.c
index 3aa4a715c216..426deb437f19 100644
--- a/crypto/aes_generic.c
+++ b/crypto/aes_generic.c
@@ -1125,155 +1125,6 @@ EXPORT_SYMBOL_GPL(crypto_fl_tab);
1125EXPORT_SYMBOL_GPL(crypto_it_tab); 1125EXPORT_SYMBOL_GPL(crypto_it_tab);
1126EXPORT_SYMBOL_GPL(crypto_il_tab); 1126EXPORT_SYMBOL_GPL(crypto_il_tab);
1127 1127
1128/* initialise the key schedule from the user supplied key */
1129
1130#define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
1131
1132#define imix_col(y, x) do { \
1133 u = star_x(x); \
1134 v = star_x(u); \
1135 w = star_x(v); \
1136 t = w ^ (x); \
1137 (y) = u ^ v ^ w; \
1138 (y) ^= ror32(u ^ t, 8) ^ \
1139 ror32(v ^ t, 16) ^ \
1140 ror32(t, 24); \
1141} while (0)
1142
1143#define ls_box(x) \
1144 crypto_fl_tab[0][byte(x, 0)] ^ \
1145 crypto_fl_tab[1][byte(x, 1)] ^ \
1146 crypto_fl_tab[2][byte(x, 2)] ^ \
1147 crypto_fl_tab[3][byte(x, 3)]
1148
1149#define loop4(i) do { \
1150 t = ror32(t, 8); \
1151 t = ls_box(t) ^ rco_tab[i]; \
1152 t ^= ctx->key_enc[4 * i]; \
1153 ctx->key_enc[4 * i + 4] = t; \
1154 t ^= ctx->key_enc[4 * i + 1]; \
1155 ctx->key_enc[4 * i + 5] = t; \
1156 t ^= ctx->key_enc[4 * i + 2]; \
1157 ctx->key_enc[4 * i + 6] = t; \
1158 t ^= ctx->key_enc[4 * i + 3]; \
1159 ctx->key_enc[4 * i + 7] = t; \
1160} while (0)
1161
1162#define loop6(i) do { \
1163 t = ror32(t, 8); \
1164 t = ls_box(t) ^ rco_tab[i]; \
1165 t ^= ctx->key_enc[6 * i]; \
1166 ctx->key_enc[6 * i + 6] = t; \
1167 t ^= ctx->key_enc[6 * i + 1]; \
1168 ctx->key_enc[6 * i + 7] = t; \
1169 t ^= ctx->key_enc[6 * i + 2]; \
1170 ctx->key_enc[6 * i + 8] = t; \
1171 t ^= ctx->key_enc[6 * i + 3]; \
1172 ctx->key_enc[6 * i + 9] = t; \
1173 t ^= ctx->key_enc[6 * i + 4]; \
1174 ctx->key_enc[6 * i + 10] = t; \
1175 t ^= ctx->key_enc[6 * i + 5]; \
1176 ctx->key_enc[6 * i + 11] = t; \
1177} while (0)
1178
1179#define loop8tophalf(i) do { \
1180 t = ror32(t, 8); \
1181 t = ls_box(t) ^ rco_tab[i]; \
1182 t ^= ctx->key_enc[8 * i]; \
1183 ctx->key_enc[8 * i + 8] = t; \
1184 t ^= ctx->key_enc[8 * i + 1]; \
1185 ctx->key_enc[8 * i + 9] = t; \
1186 t ^= ctx->key_enc[8 * i + 2]; \
1187 ctx->key_enc[8 * i + 10] = t; \
1188 t ^= ctx->key_enc[8 * i + 3]; \
1189 ctx->key_enc[8 * i + 11] = t; \
1190} while (0)
1191
1192#define loop8(i) do { \
1193 loop8tophalf(i); \
1194 t = ctx->key_enc[8 * i + 4] ^ ls_box(t); \
1195 ctx->key_enc[8 * i + 12] = t; \
1196 t ^= ctx->key_enc[8 * i + 5]; \
1197 ctx->key_enc[8 * i + 13] = t; \
1198 t ^= ctx->key_enc[8 * i + 6]; \
1199 ctx->key_enc[8 * i + 14] = t; \
1200 t ^= ctx->key_enc[8 * i + 7]; \
1201 ctx->key_enc[8 * i + 15] = t; \
1202} while (0)
1203
1204/**
1205 * crypto_aes_expand_key - Expands the AES key as described in FIPS-197
1206 * @ctx: The location where the computed key will be stored.
1207 * @in_key: The supplied key.
1208 * @key_len: The length of the supplied key.
1209 *
1210 * Returns 0 on success. The function fails only if an invalid key size (or
1211 * pointer) is supplied.
1212 * The expanded key size is 240 bytes (max of 14 rounds with a unique 16 bytes
1213 * key schedule plus a 16 bytes key which is used before the first round).
1214 * The decryption key is prepared for the "Equivalent Inverse Cipher" as
1215 * described in FIPS-197. The first slot (16 bytes) of each key (enc or dec) is
1216 * for the initial combination, the second slot for the first round and so on.
1217 */
1218int crypto_aes_expand_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
1219 unsigned int key_len)
1220{
1221 u32 i, t, u, v, w, j;
1222
1223 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
1224 key_len != AES_KEYSIZE_256)
1225 return -EINVAL;
1226
1227 ctx->key_length = key_len;
1228
1229 ctx->key_enc[0] = get_unaligned_le32(in_key);
1230 ctx->key_enc[1] = get_unaligned_le32(in_key + 4);
1231 ctx->key_enc[2] = get_unaligned_le32(in_key + 8);
1232 ctx->key_enc[3] = get_unaligned_le32(in_key + 12);
1233
1234 ctx->key_dec[key_len + 24] = ctx->key_enc[0];
1235 ctx->key_dec[key_len + 25] = ctx->key_enc[1];
1236 ctx->key_dec[key_len + 26] = ctx->key_enc[2];
1237 ctx->key_dec[key_len + 27] = ctx->key_enc[3];
1238
1239 switch (key_len) {
1240 case AES_KEYSIZE_128:
1241 t = ctx->key_enc[3];
1242 for (i = 0; i < 10; ++i)
1243 loop4(i);
1244 break;
1245
1246 case AES_KEYSIZE_192:
1247 ctx->key_enc[4] = get_unaligned_le32(in_key + 16);
1248 t = ctx->key_enc[5] = get_unaligned_le32(in_key + 20);
1249 for (i = 0; i < 8; ++i)
1250 loop6(i);
1251 break;
1252
1253 case AES_KEYSIZE_256:
1254 ctx->key_enc[4] = get_unaligned_le32(in_key + 16);
1255 ctx->key_enc[5] = get_unaligned_le32(in_key + 20);
1256 ctx->key_enc[6] = get_unaligned_le32(in_key + 24);
1257 t = ctx->key_enc[7] = get_unaligned_le32(in_key + 28);
1258 for (i = 0; i < 6; ++i)
1259 loop8(i);
1260 loop8tophalf(i);
1261 break;
1262 }
1263
1264 ctx->key_dec[0] = ctx->key_enc[key_len + 24];
1265 ctx->key_dec[1] = ctx->key_enc[key_len + 25];
1266 ctx->key_dec[2] = ctx->key_enc[key_len + 26];
1267 ctx->key_dec[3] = ctx->key_enc[key_len + 27];
1268
1269 for (i = 4; i < key_len + 24; ++i) {
1270 j = key_len + 24 - (i & ~3) + (i & 3);
1271 imix_col(ctx->key_dec[j], ctx->key_enc[i]);
1272 }
1273 return 0;
1274}
1275EXPORT_SYMBOL_GPL(crypto_aes_expand_key);
1276
1277/** 1128/**
1278 * crypto_aes_set_key - Set the AES key. 1129 * crypto_aes_set_key - Set the AES key.
1279 * @tfm: The %crypto_tfm that is used in the context. 1130 * @tfm: The %crypto_tfm that is used in the context.
@@ -1281,7 +1132,7 @@ EXPORT_SYMBOL_GPL(crypto_aes_expand_key);
1281 * @key_len: The size of the key. 1132 * @key_len: The size of the key.
1282 * 1133 *
1283 * Returns 0 on success, on failure the %CRYPTO_TFM_RES_BAD_KEY_LEN flag in tfm 1134 * Returns 0 on success, on failure the %CRYPTO_TFM_RES_BAD_KEY_LEN flag in tfm
1284 * is set. The function uses crypto_aes_expand_key() to expand the key. 1135 * is set. The function uses aes_expand_key() to expand the key.
1285 * &crypto_aes_ctx _must_ be the private data embedded in @tfm which is 1136 * &crypto_aes_ctx _must_ be the private data embedded in @tfm which is
1286 * retrieved with crypto_tfm_ctx(). 1137 * retrieved with crypto_tfm_ctx().
1287 */ 1138 */
@@ -1292,7 +1143,7 @@ int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
1292 u32 *flags = &tfm->crt_flags; 1143 u32 *flags = &tfm->crt_flags;
1293 int ret; 1144 int ret;
1294 1145
1295 ret = crypto_aes_expand_key(ctx, in_key, key_len); 1146 ret = aes_expandkey(ctx, in_key, key_len);
1296 if (!ret) 1147 if (!ret)
1297 return 0; 1148 return 0;
1298 1149