-
Notifications
You must be signed in to change notification settings - Fork 1
/
eme2.c
748 lines (602 loc) · 22.5 KB
/
eme2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*
* EME2: Encrypt-mix-encrypt-v2 mode
* As defined in IEEE Std 1619.2-2010
*
* Copyright (c) 2015 Ondrej Mosnacek <[email protected]>
*
* Based on ecb.c and xts.c
* Copyright (c) 2007 Rik Snel <[email protected]>
* Copyright (c) 2006 Herbert Xu <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#include <crypto/algapi.h>
#include <crypto/b128ops.h>
#include <crypto/internal/skcipher.h>
#include <linux/completion.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/scatterlist.h>
#include "eme2.h"
#include "blockwalk.h"
struct eme2_req_ctx;
typedef void (*eme2_crypt_fn)(struct crypto_cipher *, u8 *, const u8 *);
typedef int (*eme2_crypt_ecb_fn)(struct ablkcipher_request *req);
typedef int (*eme2_continue_fn)(struct eme2_req_ctx *rctx, u32 flags);
union eme2_block {
be128 b128;
u8 bytes[EME2_BLOCK_SIZE];
};
static inline void eme2_block_set_zero(union eme2_block *out)
{
/* TODO: see if this is OK (portable and stuff...): */
out->b128.a = 0U;
out->b128.b = 0U;
}
static inline void eme2_block_xor(
union eme2_block *res,
const union eme2_block *x, const union eme2_block *y)
{
be128_xor(&res->b128, &x->b128, &y->b128);
}
/* this is an inlinable version of gf128mul_x_ble() */
/* it causes significant speedup vs calling the function from gf128mul.ko */
static inline void eme2_block_gfmul(
union eme2_block *res, const union eme2_block *x)
{
u64 a = le64_to_cpu(x->b128.a);
u64 b = le64_to_cpu(x->b128.b);
u64 _tt = b & ((u64)0x01 << 63) ? 0x87 : 0;
res->b128.a = cpu_to_le64((a << 1) ^ _tt);
res->b128.b = cpu_to_le64((b << 1) | (a >> 63));
}
struct eme2_req_ctx {
struct ablkcipher_request* parent;
eme2_continue_fn next;
eme2_crypt_fn crypt_fn;
eme2_crypt_ecb_fn crypt_ecb_fn;
union eme2_block mp, ccc1;
struct ablkcipher_request ecb_req CRYPTO_MINALIGN_ATTR;
};
struct eme2_ctx {
struct crypto_cipher *child; /* the underlying cipher */
struct crypto_ablkcipher *child_ecb;
/* the underlying cipher in ECB mode */
union eme2_block key_ad; /* K_AD - the associated data key */
union eme2_block key_ecb; /* K_ECB - the ECB pass key */
};
struct eme2_instance_ctx {
struct crypto_spawn spawn;
struct crypto_skcipher_spawn ecb_spawn;
};
static void eme2_req_ctx_init(
struct eme2_req_ctx *rctx, struct ablkcipher_request *req,
eme2_crypt_fn crypt_fn, eme2_crypt_ecb_fn crypt_ecb_fn)
{
rctx->parent = req;
rctx->crypt_fn = crypt_fn;
rctx->crypt_ecb_fn = crypt_ecb_fn;
}
static int setkey(struct crypto_ablkcipher *cipher,
const u8 *key, unsigned int keylen)
{
struct crypto_tfm *parent = &cipher->base;
/* the key consists of two 16-byte keys and a cipher key */
const union eme2_block *key_ad = (const union eme2_block *)key;
const union eme2_block *key_ecb = key_ad + 1;
const u8 *key_aes = (key_ecb + 1)->bytes;
unsigned int key_aes_len = keylen - 2 * EME2_BLOCK_SIZE;
struct eme2_ctx *ctx = crypto_ablkcipher_ctx(cipher);
struct crypto_cipher *child = ctx->child;
struct crypto_ablkcipher *child_ecb = ctx->child_ecb;
u32 *flags = &parent->crt_flags;
int err;
if (keylen < 2 * EME2_BLOCK_SIZE) {
/* tell the user why there was an error */
*flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
return -EINVAL;
}
/* child cipher, uses K_AES */
crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
CRYPTO_TFM_REQ_MASK);
err = crypto_cipher_setkey(child, key_aes, key_aes_len);
if (err)
return err;
crypto_tfm_set_flags(parent, crypto_ablkcipher_get_flags(child_ecb) &
CRYPTO_TFM_RES_MASK);
crypto_ablkcipher_clear_flags(child_ecb, CRYPTO_TFM_REQ_MASK);
crypto_ablkcipher_set_flags(child_ecb, crypto_tfm_get_flags(parent) &
CRYPTO_TFM_REQ_MASK);
err = crypto_ablkcipher_setkey(child_ecb, key_aes, key_aes_len);
if (err)
return err;
crypto_tfm_set_flags(parent, crypto_ablkcipher_get_flags(child_ecb) &
CRYPTO_TFM_RES_MASK);
/* copy the "associated data" and "ECB pass" keys into context: */
ctx->key_ad = *key_ad;
ctx->key_ecb = *key_ecb;
return 0;
}
static inline void eme2_xor_padded(union eme2_block *dst, const u8 *src,
unsigned int size)
{
crypto_xor(dst->bytes, src, size);
dst->bytes[size] ^= 0x80;
}
static inline void eme2_process_assoc_data_step(
struct crypto_cipher *cipher, union eme2_block *t_star,
union eme2_block *k_ad, const union eme2_block *t)
{
union eme2_block tmp;
/* K_AD = mult-by-alpha(K_AD) */
eme2_block_gfmul(k_ad, k_ad);
/* TT_j = AES-Enc(K_AES, K_AD xor T_j) xor K_AD */
/* T_star = T_star xor TT_j */
eme2_block_xor(&tmp, k_ad, t);
crypto_cipher_encrypt_one(cipher, tmp.bytes, tmp.bytes);
eme2_block_xor(t_star, t_star, &tmp);
eme2_block_xor(t_star, t_star, k_ad);
}
/* the function "H" for preprocessing the associated data */
static inline void eme2_process_assoc_data(
const struct eme2_ctx *ctx, union eme2_block *t_star,
const u8 *ad, unsigned int ad_bytes)
{
unsigned int full_blocks = ad_bytes / EME2_BLOCK_SIZE;
unsigned int extra_bytes = ad_bytes % EME2_BLOCK_SIZE;
const union eme2_block *t = (const union eme2_block *)ad;
union eme2_block k_ad;
union eme2_block last_block;
unsigned int j;
/* special case for no associated data: */
if (ad_bytes == 0) {
/* T_star = AES-Enc(K_AES, K_AD) */
crypto_cipher_encrypt_one(ctx->child, t_star->bytes, ctx->key_ad.bytes);
return;
}
eme2_block_set_zero(t_star);
k_ad = ctx->key_ad;
for (j = 0; j < full_blocks; j++) {
eme2_process_assoc_data_step(ctx->child, t_star, &k_ad, &t[j]);
}
if (extra_bytes != 0) {
/* pad the last block: */
eme2_block_set_zero(&last_block);
memcpy(last_block.bytes, t[full_blocks].bytes, extra_bytes);
last_block.bytes[extra_bytes] = 0x80;
/* one more mult-by-alpha is required for padded block: */
/* K_AD = mult-by-alpha(K_AD) */
eme2_block_gfmul(&k_ad, &k_ad);
eme2_process_assoc_data_step(ctx->child, t_star, &k_ad, &last_block);
}
}
static int eme2_err_is_bad(struct ablkcipher_request *req, int err)
{
switch (err) {
case 0:
case -EINPROGRESS:
return 0;
case -EBUSY:
return !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG);
default:
return 1;
}
}
static int eme2_phase1(struct eme2_req_ctx *rctx, u32 flags);
static int eme2_phase2(struct eme2_req_ctx *rctx, u32 flags);
static int eme2_phase3(struct eme2_req_ctx *rctx, u32 flags);
static void eme2_callback(struct crypto_async_request *subreq, int err)
{
struct eme2_req_ctx *rctx = subreq->data;
struct ablkcipher_request *req = rctx->parent;
switch (err) {
case 0:
case -EINPROGRESS:
return;
default:
ablkcipher_request_complete(req, err);
return;
}
err = rctx->next(rctx, 0);
if (err == 0 || eme2_err_is_bad(req, err)) {
ablkcipher_request_complete(req, err);
}
}
static int eme2_crypt_start(struct eme2_req_ctx *rctx, unsigned int ivsize,
u32 flags)
{
struct ablkcipher_request *req = rctx->parent;
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
struct eme2_ctx *ctx = crypto_ablkcipher_ctx(tfm);
struct ablkcipher_request *subreq = &rctx->ecb_req;
/* input must be at least one block: */
if (unlikely(req->nbytes < EME2_BLOCK_SIZE)) {
/* TODO: see if this is the right error code to use here */
/* (xts.c uses just BUG_ON... */
return req->nbytes == 0 ? 0 : -EINVAL;
}
/* init both MP and CCC_1 to T_star: */
eme2_process_assoc_data(ctx, &rctx->mp, req->info, ivsize);
rctx->ccc1 = rctx->mp;
ablkcipher_request_set_tfm(subreq, ctx->child_ecb);
return eme2_phase1(rctx, flags);
}
static int eme2_phase1(struct eme2_req_ctx *rctx, u32 flags)
{
struct ablkcipher_request *subreq = &rctx->ecb_req;
struct ablkcipher_request *req = rctx->parent;
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
struct eme2_ctx *ctx = crypto_ablkcipher_ctx(tfm);
struct blockwalk walk;
unsigned int avail, reqsize = req->nbytes & EME2_BLOCK_MASK;
union eme2_block *cursor_in, *cursor_out;
union eme2_block buffer, l;
int err;
/* L = K_ECB */
l = ctx->key_ecb;
blockwalk_start(&walk, EME2_BLOCK_SIZE, crypto_ablkcipher_alignmask(tfm),
buffer.bytes, req->src, req->dst, reqsize);
do {
blockwalk_chunk_start(&walk);
avail = blockwalk_chunk_size(&walk);
cursor_in = blockwalk_chunk_in(&walk);
cursor_out = blockwalk_chunk_out(&walk);
while (avail >= EME2_BLOCK_SIZE) {
/* P_j' = L xor P_j */
eme2_block_xor(cursor_out, &l, cursor_in);
/* L = mult-by-alpha(L) */
eme2_block_gfmul(&l, &l);
avail -= EME2_BLOCK_SIZE;
++cursor_in;
++cursor_out;
}
blockwalk_chunk_finish(&walk);
} while (blockwalk_bytes_left(&walk));
rctx->next = eme2_phase2;
ablkcipher_request_set_crypt(subreq, req->dst, req->dst,reqsize, NULL);
ablkcipher_request_set_callback(subreq, flags, eme2_callback, rctx);
err = rctx->crypt_ecb_fn(subreq);
if (err != 0) {
return err;
}
return eme2_phase2(rctx, flags);
}
static int eme2_phase2(struct eme2_req_ctx *rctx, u32 flags)
{
struct ablkcipher_request *subreq = &rctx->ecb_req;
struct ablkcipher_request *req = rctx->parent;
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
struct eme2_ctx *ctx = crypto_ablkcipher_ctx(tfm);
struct blockwalk walk;
unsigned int avail, j, reqsize = req->nbytes & EME2_BLOCK_MASK;
union eme2_block *cursor_in, *cursor_out;
union eme2_block buffer, mc, mp, m, m1;
int err;
blockwalk_start(&walk, EME2_BLOCK_SIZE, crypto_ablkcipher_alignmask(tfm),
buffer.bytes, req->dst, req->dst, req->nbytes);
for (;;) {
blockwalk_chunk_start(&walk);
avail = blockwalk_chunk_size(&walk);
cursor_in = blockwalk_chunk_in(&walk);
cursor_out = blockwalk_chunk_out(&walk);
while (avail >= EME2_BLOCK_SIZE) {
/* MP = MP xor PPP_j */
eme2_block_xor(&rctx->mp, &rctx->mp, cursor_in);
avail -= EME2_BLOCK_SIZE;
++cursor_in;
++cursor_out;
}
if (unlikely(!blockwalk_bytes_left(&walk))) {
break;
}
blockwalk_chunk_finish(&walk);
}
if (unlikely(avail != 0)) {
/* MP = MP xor PPP_m */
eme2_xor_padded(&rctx->mp, cursor_in->bytes, avail);
/* MM = AES-Enc(K_AES, MP) */
rctx->crypt_fn(ctx->child, mc.bytes, rctx->mp.bytes);
/* C_m = P_m xor MM [truncated] */
crypto_xor(cursor_out->bytes, mc.bytes, avail);
/* CCC_1 = CCC_1 xor CCC_m */
eme2_xor_padded(&rctx->ccc1, cursor_out->bytes, avail);
/* MC = MC_1 = AES-Enc(K_AES, MM) */
rctx->crypt_fn(ctx->child, mc.bytes, mc.bytes);
} else {
/* MC = MC_1 = AES-Enc(K_AES, MP) */
rctx->crypt_fn(ctx->child, mc.bytes, rctx->mp.bytes);
}
blockwalk_chunk_finish(&walk);
/* M = M_1 = MP xor MC */
eme2_block_xor(&m1, &rctx->mp, &mc);
m = m1;
/* CCC_1 = CCC_1 xor MC */
eme2_block_xor(&rctx->ccc1, &rctx->ccc1, &mc);
/* L = K_ECB */
j = 0;
blockwalk_start(&walk, EME2_BLOCK_SIZE, crypto_ablkcipher_alignmask(tfm),
buffer.bytes, req->dst, req->dst, reqsize);
do {
blockwalk_chunk_start(&walk);
avail = blockwalk_chunk_size(&walk);
cursor_in = blockwalk_chunk_in(&walk);
cursor_out = blockwalk_chunk_out(&walk);
/* skip the first block: */
if (unlikely(j == 0)) {
++j;
avail -= EME2_BLOCK_SIZE;
++cursor_in;
++cursor_out;
}
while (avail >= EME2_BLOCK_SIZE) {
if (likely(j % 128 != 0)) {
/* M = mult-by-alpha(M) */
eme2_block_gfmul(&m, &m);
/* CCC_j = PPP_j xor M */
eme2_block_xor(cursor_out, cursor_in, &m);
} else {
/* MP = PPP_j xor M_1 */
eme2_block_xor(&mp, cursor_in, &m1);
/* MC = AES-Enc(K_AES, MP) */
rctx->crypt_fn(ctx->child, mc.bytes, mp.bytes);
/* M = MP xor MC */
eme2_block_xor(&m, &mp, &mc);
/* CCC_j = MC xor M_1 */
eme2_block_xor(cursor_out, &mc, &m1);
}
/* CCC_1 = CCC_1 xor CCC_j */
eme2_block_xor(&rctx->ccc1, &rctx->ccc1, cursor_out);
++j;
avail -= EME2_BLOCK_SIZE;
++cursor_in;
++cursor_out;
}
blockwalk_chunk_finish(&walk);
} while (blockwalk_bytes_left(&walk));
rctx->next = eme2_phase3;
ablkcipher_request_set_crypt(subreq, req->dst, req->dst, reqsize, NULL);
ablkcipher_request_set_callback(subreq, flags, eme2_callback, rctx);
err = rctx->crypt_ecb_fn(subreq);
if (err != 0) {
return err;
}
return eme2_phase3(rctx, flags);
}
static int eme2_phase3(struct eme2_req_ctx *rctx, u32 flags)
{
struct ablkcipher_request *req = rctx->parent;
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
struct eme2_ctx *ctx = crypto_ablkcipher_ctx(tfm);
struct blockwalk walk;
unsigned int avail, reqsize = req->nbytes & EME2_BLOCK_MASK;
union eme2_block *cursor_in, *cursor_out;
union eme2_block buffer, l;
int first_block = 1;
/* L = K_ECB */
l = ctx->key_ecb;
blockwalk_start(&walk, EME2_BLOCK_SIZE, crypto_ablkcipher_alignmask(tfm),
buffer.bytes, req->dst, req->dst, reqsize);
do {
blockwalk_chunk_start(&walk);
avail = blockwalk_chunk_size(&walk);
cursor_in = blockwalk_chunk_in(&walk);
cursor_out = blockwalk_chunk_out(&walk);
if (unlikely(first_block)) {
first_block = 0;
/* C_1' = AES-Enc(K_AES, CCC_1) */
rctx->crypt_fn(ctx->child, rctx->ccc1.bytes, rctx->ccc1.bytes);
/* C_1 = C_1' xor L */
eme2_block_xor(cursor_out, &rctx->ccc1, &l);
/* L = mult-by-alpha(L) */
eme2_block_gfmul(&l, &l);
avail -= EME2_BLOCK_SIZE;
++cursor_in;
++cursor_out;
}
while (avail >= EME2_BLOCK_SIZE) {
/* C_j = C_j' xor L */
eme2_block_xor(cursor_out, cursor_in, &l);
/* L = mult-by-alpha(L) */
eme2_block_gfmul(&l, &l);
avail -= EME2_BLOCK_SIZE;
++cursor_in;
++cursor_out;
}
blockwalk_chunk_finish(&walk);
} while (blockwalk_bytes_left(&walk));
return 0;
}
int eme2_encrypt(struct ablkcipher_request *req, unsigned int ivsize)
{
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
u32 flags = ablkcipher_request_flags(req);
unsigned long align = crypto_ablkcipher_alignmask(tfm);
struct eme2_req_ctx *rctx =
(void *)PTR_ALIGN((u8 *)ablkcipher_request_ctx(req), align + 1);
eme2_req_ctx_init(rctx, req, *crypto_cipher_encrypt_one,
&crypto_ablkcipher_encrypt);
return eme2_crypt_start(rctx, ivsize, flags);
}
EXPORT_SYMBOL_GPL(eme2_encrypt);
int eme2_decrypt(struct ablkcipher_request *req, unsigned int ivsize)
{
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
u32 flags = ablkcipher_request_flags(req);
unsigned long align = crypto_ablkcipher_alignmask(tfm);
struct eme2_req_ctx *rctx =
(void *)PTR_ALIGN((u8 *)ablkcipher_request_ctx(req), align + 1);
eme2_req_ctx_init(rctx, req, *crypto_cipher_decrypt_one,
&crypto_ablkcipher_decrypt);
return eme2_crypt_start(rctx, ivsize, flags);
}
EXPORT_SYMBOL_GPL(eme2_decrypt);
static int encrypt(struct ablkcipher_request *req)
{
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
u32 flags = ablkcipher_request_flags(req);
unsigned long align = crypto_ablkcipher_alignmask(tfm);
struct eme2_req_ctx *rctx =
(void *)PTR_ALIGN((u8 *)ablkcipher_request_ctx(req), align + 1);
eme2_req_ctx_init(rctx, req, *crypto_cipher_encrypt_one,
&crypto_ablkcipher_encrypt);
return eme2_crypt_start(rctx, crypto_ablkcipher_ivsize(tfm), flags);
}
static int decrypt(struct ablkcipher_request *req)
{
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
u32 flags = ablkcipher_request_flags(req);
unsigned long align = crypto_ablkcipher_alignmask(tfm);
struct eme2_req_ctx *rctx =
(void *)PTR_ALIGN((u8 *)ablkcipher_request_ctx(req), align + 1);
eme2_req_ctx_init(rctx, req, *crypto_cipher_decrypt_one,
&crypto_ablkcipher_decrypt);
return eme2_crypt_start(rctx, crypto_ablkcipher_ivsize(tfm), flags);
}
static int init_tfm(struct crypto_tfm *tfm)
{
struct crypto_cipher *cipher;
struct crypto_ablkcipher *cipher_ecb;
struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
struct eme2_instance_ctx *inst_ctx = crypto_instance_ctx(inst);
struct eme2_ctx *ctx = crypto_tfm_ctx(tfm);
unsigned int align;
cipher_ecb = crypto_spawn_skcipher(&inst_ctx->ecb_spawn);
if (IS_ERR(cipher_ecb))
return PTR_ERR(cipher_ecb);
cipher = crypto_spawn_cipher(&inst_ctx->spawn);
if (IS_ERR(cipher)) {
crypto_free_ablkcipher(cipher_ecb);
return PTR_ERR(cipher);
}
ctx->child = cipher;
ctx->child_ecb = cipher_ecb;
align = crypto_tfm_alg_alignmask(tfm);
align &= ~(crypto_tfm_ctx_alignment() - 1);
tfm->crt_ablkcipher.reqsize = align +
sizeof(struct eme2_req_ctx) +
crypto_ablkcipher_reqsize(cipher_ecb);
return 0;
}
static void exit_tfm(struct crypto_tfm *tfm)
{
struct eme2_ctx *ctx = crypto_tfm_ctx(tfm);
crypto_free_cipher(ctx->child);
crypto_free_ablkcipher(ctx->child_ecb);
/* clear the xor keys: */
memzero_explicit(&ctx->key_ad, sizeof(ctx->key_ad));
memzero_explicit(&ctx->key_ecb, sizeof(ctx->key_ecb));
}
static struct crypto_instance *alloc(struct rtattr **tb)
{
struct crypto_instance *inst;
struct eme2_instance_ctx *ctx;
struct crypto_alg *alg, *ecb_alg;
char ecb_name[CRYPTO_MAX_ALG_NAME];
int err;
err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
if (err)
return ERR_PTR(err);
alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
CRYPTO_ALG_TYPE_MASK);
if (IS_ERR(alg))
return ERR_CAST(alg);
/* we only support 16-byte blocks: */
if (alg->cra_blocksize != EME2_BLOCK_SIZE)
return ERR_PTR(-EINVAL);
inst = kzalloc(sizeof(*inst) + sizeof(struct eme2_instance_ctx),
GFP_KERNEL);
if (!inst) {
inst = ERR_PTR(-ENOMEM);
goto out_put_alg;
}
ctx = crypto_instance_ctx(inst);
/* prepare spawn for crypto_cipher: */
err = crypto_init_spawn(&ctx->spawn, alg, inst,
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
if (err)
goto err_free_inst;
/* prepare spawn for ECB mode: */
err = -ENAMETOOLONG;
if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)", alg->cra_name)
>= CRYPTO_MAX_ALG_NAME)
goto err_drop_spawn;
crypto_set_skcipher_spawn(&ctx->ecb_spawn, inst);
err = crypto_grab_skcipher(&ctx->ecb_spawn, ecb_name, 0, 0);
if (err)
goto err_drop_spawn;
/* get the crypto_alg for the ECB mode: */
ecb_alg = crypto_skcipher_spawn_alg(&ctx->ecb_spawn);
err = -ENAMETOOLONG;
if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "eme2(%s)",
alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
goto err_drop_ecb_spawn;
if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,"eme2(%s,%s)",
alg->cra_driver_name, ecb_alg->cra_driver_name)
>= CRYPTO_MAX_ALG_NAME)
goto err_drop_ecb_spawn;
inst->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
(ecb_alg->cra_flags & CRYPTO_ALG_ASYNC);
inst->alg.cra_priority = ecb_alg->cra_priority;
inst->alg.cra_blocksize = 1;
if (alg->cra_alignmask < 7)
inst->alg.cra_alignmask = 7;
else
inst->alg.cra_alignmask =
max(alg->cra_alignmask, ecb_alg->cra_alignmask);
inst->alg.cra_type = &crypto_ablkcipher_type;
/* since IV size must be fixed, we arbitrarily choose one block for it: */
inst->alg.cra_ablkcipher.ivsize = EME2_BLOCK_SIZE;
inst->alg.cra_ablkcipher.min_keysize =
2 * EME2_BLOCK_SIZE + alg->cra_cipher.cia_min_keysize;
inst->alg.cra_ablkcipher.max_keysize =
2 * EME2_BLOCK_SIZE + alg->cra_cipher.cia_max_keysize;
inst->alg.cra_ablkcipher.setkey = setkey;
inst->alg.cra_ablkcipher.encrypt = encrypt;
inst->alg.cra_ablkcipher.decrypt = decrypt;
inst->alg.cra_ctxsize = sizeof(struct eme2_ctx);
inst->alg.cra_init = init_tfm;
inst->alg.cra_exit = exit_tfm;
out_put_alg:
crypto_mod_put(alg);
return inst;
err_drop_ecb_spawn:
crypto_drop_skcipher(&ctx->ecb_spawn);
err_drop_spawn:
crypto_drop_spawn(&ctx->spawn);
err_free_inst:
kzfree(inst);
crypto_mod_put(alg);
return ERR_PTR(err);
}
static void free(struct crypto_instance *inst)
{
struct eme2_instance_ctx *ctx = crypto_instance_ctx(inst);
crypto_drop_spawn(&ctx->spawn);
crypto_drop_skcipher(&ctx->ecb_spawn);
kzfree(inst);
}
static struct crypto_template crypto_tmpl = {
.name = "eme2",
.alloc = alloc,
.free = free,
.module = THIS_MODULE,
};
static int __init crypto_module_init(void)
{
return crypto_register_template(&crypto_tmpl);
}
static void __exit crypto_module_exit(void)
{
crypto_unregister_template(&crypto_tmpl);
}
module_init(crypto_module_init);
module_exit(crypto_module_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("EME2 block cipher mode");
MODULE_ALIAS_CRYPTO("eme2");