1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * ARIA Cipher Algorithm.
7 * Documentation of ARIA can be found in RFC 5794.
8 * Copyright (c) 2022 Taehee Yoo <ap420073@gmail.com>
10 * Information for ARIA
11 * http://210.104.33.10/ARIA/index-e.html (English)
12 * http://seed.kisa.or.kr/ (Korean)
14 * Public domain version is distributed above.
17 #include <crypto/aria.h>
19 static const u32 key_rc[20] = {
20 0x517cc1b7, 0x27220a94, 0xfe13abe8, 0xfa9a6ee0,
21 0x6db14acc, 0x9e21c820, 0xff28b1d5, 0xef5de2b0,
22 0xdb92371d, 0x2126e970, 0x03249775, 0x04e8c90e,
23 0x517cc1b7, 0x27220a94, 0xfe13abe8, 0xfa9a6ee0,
24 0x6db14acc, 0x9e21c820, 0xff28b1d5, 0xef5de2b0
27 static void aria_set_encrypt_key(struct aria_ctx *ctx, const u8 *in_key,
30 const __be32 *key = (const __be32 *)in_key;
31 u32 w0[4], w1[4], w2[4], w3[4];
32 u32 reg0, reg1, reg2, reg3;
36 ck = &key_rc[(key_len - 16) / 2];
38 w0[0] = be32_to_cpu(key[0]);
39 w0[1] = be32_to_cpu(key[1]);
40 w0[2] = be32_to_cpu(key[2]);
41 w0[3] = be32_to_cpu(key[3]);
48 aria_subst_diff_odd(®0, ®1, ®2, ®3);
51 w1[0] = be32_to_cpu(key[4]);
52 w1[1] = be32_to_cpu(key[5]);
54 w1[2] = be32_to_cpu(key[6]);
55 w1[3] = be32_to_cpu(key[7]);
82 aria_subst_diff_even(®0, ®1, ®2, ®3);
99 aria_subst_diff_odd(®0, ®1, ®2, ®3);
101 w3[0] = reg0 ^ w1[0];
102 w3[1] = reg1 ^ w1[1];
103 w3[2] = reg2 ^ w1[2];
104 w3[3] = reg3 ^ w1[3];
106 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 19);
108 aria_gsrk(ctx->enc_key[rkidx], w1, w2, 19);
110 aria_gsrk(ctx->enc_key[rkidx], w2, w3, 19);
112 aria_gsrk(ctx->enc_key[rkidx], w3, w0, 19);
115 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 31);
117 aria_gsrk(ctx->enc_key[rkidx], w1, w2, 31);
119 aria_gsrk(ctx->enc_key[rkidx], w2, w3, 31);
121 aria_gsrk(ctx->enc_key[rkidx], w3, w0, 31);
124 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 67);
126 aria_gsrk(ctx->enc_key[rkidx], w1, w2, 67);
128 aria_gsrk(ctx->enc_key[rkidx], w2, w3, 67);
130 aria_gsrk(ctx->enc_key[rkidx], w3, w0, 67);
133 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 97);
136 aria_gsrk(ctx->enc_key[rkidx], w1, w2, 97);
138 aria_gsrk(ctx->enc_key[rkidx], w2, w3, 97);
142 aria_gsrk(ctx->enc_key[rkidx], w3, w0, 97);
145 aria_gsrk(ctx->enc_key[rkidx], w0, w1, 109);
150 static void aria_set_decrypt_key(struct aria_ctx *ctx)
154 for (i = 0; i < 4; i++) {
155 ctx->dec_key[0][i] = ctx->enc_key[ctx->rounds][i];
156 ctx->dec_key[ctx->rounds][i] = ctx->enc_key[0][i];
159 for (i = 1; i < ctx->rounds; i++) {
160 ctx->dec_key[i][0] = aria_m(ctx->enc_key[ctx->rounds - i][0]);
161 ctx->dec_key[i][1] = aria_m(ctx->enc_key[ctx->rounds - i][1]);
162 ctx->dec_key[i][2] = aria_m(ctx->enc_key[ctx->rounds - i][2]);
163 ctx->dec_key[i][3] = aria_m(ctx->enc_key[ctx->rounds - i][3]);
165 aria_diff_word(&ctx->dec_key[i][0], &ctx->dec_key[i][1],
166 &ctx->dec_key[i][2], &ctx->dec_key[i][3]);
167 aria_diff_byte(&ctx->dec_key[i][1],
168 &ctx->dec_key[i][2], &ctx->dec_key[i][3]);
169 aria_diff_word(&ctx->dec_key[i][0], &ctx->dec_key[i][1],
170 &ctx->dec_key[i][2], &ctx->dec_key[i][3]);
174 int aria_set_key(struct crypto_tfm *tfm, const u8 *in_key, unsigned int key_len)
176 struct aria_ctx *ctx = crypto_tfm_ctx(tfm);
178 if (key_len != 16 && key_len != 24 && key_len != 32)
181 ctx->key_length = key_len;
182 ctx->rounds = (key_len + 32) / 4;
184 aria_set_encrypt_key(ctx, in_key, key_len);
185 aria_set_decrypt_key(ctx);
189 EXPORT_SYMBOL_GPL(aria_set_key);
191 static void __aria_crypt(struct aria_ctx *ctx, u8 *out, const u8 *in,
192 u32 key[][ARIA_RD_KEY_WORDS])
194 const __be32 *src = (const __be32 *)in;
195 __be32 *dst = (__be32 *)out;
196 u32 reg0, reg1, reg2, reg3;
197 int rounds, rkidx = 0;
199 rounds = ctx->rounds;
201 reg0 = be32_to_cpu(src[0]);
202 reg1 = be32_to_cpu(src[1]);
203 reg2 = be32_to_cpu(src[2]);
204 reg3 = be32_to_cpu(src[3]);
206 aria_add_round_key(key[rkidx], ®0, ®1, ®2, ®3);
209 aria_subst_diff_odd(®0, ®1, ®2, ®3);
210 aria_add_round_key(key[rkidx], ®0, ®1, ®2, ®3);
213 while ((rounds -= 2) > 0) {
214 aria_subst_diff_even(®0, ®1, ®2, ®3);
215 aria_add_round_key(key[rkidx], ®0, ®1, ®2, ®3);
218 aria_subst_diff_odd(®0, ®1, ®2, ®3);
219 aria_add_round_key(key[rkidx], ®0, ®1, ®2, ®3);
223 reg0 = key[rkidx][0] ^ make_u32((u8)(x1[get_u8(reg0, 0)]),
224 (u8)(x2[get_u8(reg0, 1)] >> 8),
225 (u8)(s1[get_u8(reg0, 2)]),
226 (u8)(s2[get_u8(reg0, 3)]));
227 reg1 = key[rkidx][1] ^ make_u32((u8)(x1[get_u8(reg1, 0)]),
228 (u8)(x2[get_u8(reg1, 1)] >> 8),
229 (u8)(s1[get_u8(reg1, 2)]),
230 (u8)(s2[get_u8(reg1, 3)]));
231 reg2 = key[rkidx][2] ^ make_u32((u8)(x1[get_u8(reg2, 0)]),
232 (u8)(x2[get_u8(reg2, 1)] >> 8),
233 (u8)(s1[get_u8(reg2, 2)]),
234 (u8)(s2[get_u8(reg2, 3)]));
235 reg3 = key[rkidx][3] ^ make_u32((u8)(x1[get_u8(reg3, 0)]),
236 (u8)(x2[get_u8(reg3, 1)] >> 8),
237 (u8)(s1[get_u8(reg3, 2)]),
238 (u8)(s2[get_u8(reg3, 3)]));
240 dst[0] = cpu_to_be32(reg0);
241 dst[1] = cpu_to_be32(reg1);
242 dst[2] = cpu_to_be32(reg2);
243 dst[3] = cpu_to_be32(reg3);
246 void aria_encrypt(void *_ctx, u8 *out, const u8 *in)
248 struct aria_ctx *ctx = (struct aria_ctx *)_ctx;
250 __aria_crypt(ctx, out, in, ctx->enc_key);
252 EXPORT_SYMBOL_GPL(aria_encrypt);
254 void aria_decrypt(void *_ctx, u8 *out, const u8 *in)
256 struct aria_ctx *ctx = (struct aria_ctx *)_ctx;
258 __aria_crypt(ctx, out, in, ctx->dec_key);
260 EXPORT_SYMBOL_GPL(aria_decrypt);
262 static void __aria_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
264 struct aria_ctx *ctx = crypto_tfm_ctx(tfm);
266 __aria_crypt(ctx, out, in, ctx->enc_key);
269 static void __aria_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
271 struct aria_ctx *ctx = crypto_tfm_ctx(tfm);
273 __aria_crypt(ctx, out, in, ctx->dec_key);
276 static struct crypto_alg aria_alg = {
278 .cra_driver_name = "aria-generic",
280 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
281 .cra_blocksize = ARIA_BLOCK_SIZE,
282 .cra_ctxsize = sizeof(struct aria_ctx),
284 .cra_module = THIS_MODULE,
287 .cia_min_keysize = ARIA_MIN_KEY_SIZE,
288 .cia_max_keysize = ARIA_MAX_KEY_SIZE,
289 .cia_setkey = aria_set_key,
290 .cia_encrypt = __aria_encrypt,
291 .cia_decrypt = __aria_decrypt
296 static int __init aria_init(void)
298 return crypto_register_alg(&aria_alg);
301 static void __exit aria_fini(void)
303 crypto_unregister_alg(&aria_alg);
306 subsys_initcall(aria_init);
307 module_exit(aria_fini);
309 MODULE_DESCRIPTION("ARIA Cipher Algorithm");
310 MODULE_LICENSE("GPL");
311 MODULE_AUTHOR("Taehee Yoo <ap420073@gmail.com>");
312 MODULE_ALIAS_CRYPTO("aria");
313 MODULE_ALIAS_CRYPTO("aria-generic");