hc
2024-05-10 748e4f3d702def1a4bff191e0cf93b6a05340f01
kernel/crypto/aes_generic.c
....@@ -61,10 +61,8 @@
6161 return x >> (n << 3);
6262 }
6363
64
-static const u32 rco_tab[10] = { 1, 2, 4, 8, 16, 32, 64, 128, 27, 54 };
65
-
6664 /* cacheline-aligned to facilitate prefetching into cache */
67
-__visible const u32 crypto_ft_tab[4][256] __cacheline_aligned = {
65
+__visible const u32 crypto_ft_tab[4][256] ____cacheline_aligned = {
6866 {
6967 0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6,
7068 0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591,
....@@ -328,7 +326,7 @@
328326 }
329327 };
330328
331
-__visible const u32 crypto_fl_tab[4][256] __cacheline_aligned = {
329
+static const u32 crypto_fl_tab[4][256] ____cacheline_aligned = {
332330 {
333331 0x00000063, 0x0000007c, 0x00000077, 0x0000007b,
334332 0x000000f2, 0x0000006b, 0x0000006f, 0x000000c5,
....@@ -592,7 +590,7 @@
592590 }
593591 };
594592
595
-__visible const u32 crypto_it_tab[4][256] __cacheline_aligned = {
593
+__visible const u32 crypto_it_tab[4][256] ____cacheline_aligned = {
596594 {
597595 0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a,
598596 0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b,
....@@ -856,7 +854,7 @@
856854 }
857855 };
858856
859
-__visible const u32 crypto_il_tab[4][256] __cacheline_aligned = {
857
+static const u32 crypto_il_tab[4][256] ____cacheline_aligned = {
860858 {
861859 0x00000052, 0x00000009, 0x0000006a, 0x000000d5,
862860 0x00000030, 0x00000036, 0x000000a5, 0x00000038,
....@@ -1121,158 +1119,7 @@
11211119 };
11221120
11231121 EXPORT_SYMBOL_GPL(crypto_ft_tab);
1124
-EXPORT_SYMBOL_GPL(crypto_fl_tab);
11251122 EXPORT_SYMBOL_GPL(crypto_it_tab);
1126
-EXPORT_SYMBOL_GPL(crypto_il_tab);
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
- */
1218
-int 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
-}
1275
-EXPORT_SYMBOL_GPL(crypto_aes_expand_key);
12761123
12771124 /**
12781125 * crypto_aes_set_key - Set the AES key.
....@@ -1280,24 +1127,18 @@
12801127 * @in_key: The input key.
12811128 * @key_len: The size of the key.
12821129 *
1283
- * 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.
1285
- * &crypto_aes_ctx _must_ be the private data embedded in @tfm which is
1286
- * retrieved with crypto_tfm_ctx().
1130
+ * This function uses aes_expand_key() to expand the key. &crypto_aes_ctx
1131
+ * _must_ be the private data embedded in @tfm which is retrieved with
1132
+ * crypto_tfm_ctx().
1133
+ *
1134
+ * Return: 0 on success; -EINVAL on failure (only happens for bad key lengths)
12871135 */
12881136 int crypto_aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
12891137 unsigned int key_len)
12901138 {
12911139 struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
1292
- u32 *flags = &tfm->crt_flags;
1293
- int ret;
12941140
1295
- ret = crypto_aes_expand_key(ctx, in_key, key_len);
1296
- if (!ret)
1297
- return 0;
1298
-
1299
- *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
1300
- return -EINVAL;
1141
+ return aes_expandkey(ctx, in_key, key_len);
13011142 }
13021143 EXPORT_SYMBOL_GPL(crypto_aes_set_key);
13031144
....@@ -1332,7 +1173,7 @@
13321173 f_rl(bo, bi, 3, k); \
13331174 } while (0)
13341175
1335
-static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1176
+static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
13361177 {
13371178 const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
13381179 u32 b0[4], b1[4];
....@@ -1402,7 +1243,7 @@
14021243 i_rl(bo, bi, 3, k); \
14031244 } while (0)
14041245
1405
-static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
1246
+static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
14061247 {
14071248 const struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
14081249 u32 b0[4], b1[4];
....@@ -1454,8 +1295,8 @@
14541295 .cia_min_keysize = AES_MIN_KEY_SIZE,
14551296 .cia_max_keysize = AES_MAX_KEY_SIZE,
14561297 .cia_setkey = crypto_aes_set_key,
1457
- .cia_encrypt = aes_encrypt,
1458
- .cia_decrypt = aes_decrypt
1298
+ .cia_encrypt = crypto_aes_encrypt,
1299
+ .cia_decrypt = crypto_aes_decrypt
14591300 }
14601301 }
14611302 };
....@@ -1470,7 +1311,7 @@
14701311 crypto_unregister_alg(&aes_alg);
14711312 }
14721313
1473
-module_init(aes_init);
1314
+subsys_initcall(aes_init);
14741315 module_exit(aes_fini);
14751316
14761317 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");