5 tristate "Software async crypto daemon"
6 select CRYPTO_BLKCIPHER
10 This is a generic software asynchronous crypto daemon that
14 tristate "CRC32c CRC algorithm"
15 - select CRYPTO_ALGAPI
19 Castagnoli, et al Cyclic Redundancy-Check Algorithm. Used
21 should not be used for other purposes because of the weakness
25 + tristate "RIPEMD-128 digest algorithm"
26 + select CRYPTO_ALGAPI
28 + RIPEMD-128 (ISO/IEC 10118-3:2004).
30 + RIPEMD-128 is a 128-bit cryptographic hash function. It should only
31 + to be used as a secure replacement for RIPEMD. For other use cases
32 + RIPEMD-160 should be used.
34 + Developed by Hans Dobbertin, Antoon Bosselaers and Bart Preneel.
35 + See <http://home.esat.kuleuven.be/~bosselae/ripemd160.html>
38 + tristate "RIPEMD-160 digest algorithm"
39 + select CRYPTO_ALGAPI
41 + RIPEMD-160 (ISO/IEC 10118-3:2004).
43 + RIPEMD-160 is a 160-bit cryptographic hash function. It is intended
44 + to be used as a secure replacement for the 128-bit hash functions
45 + MD4, MD5 and it's predecessor RIPEMD (not to be confused with RIPEMD-128).
47 + It's speed is comparable to SHA1 and there are no known attacks against
50 + Developed by Hans Dobbertin, Antoon Bosselaers and Bart Preneel.
51 + See <http://home.esat.kuleuven.be/~bosselae/ripemd160.html>
54 + tristate "RIPEMD-256 digest algorithm"
55 + select CRYPTO_ALGAPI
57 + RIPEMD-256 is an optional extension of RIPEMD-128 with a 256 bit hash.
58 + It is intended for applications that require longer hash-results, without
59 + needing a larger security level (than RIPEMD-128).
61 + Developed by Hans Dobbertin, Antoon Bosselaers and Bart Preneel.
62 + See <http://home.esat.kuleuven.be/~bosselae/ripemd160.html>
65 + tristate "RIPEMD-320 digest algorithm"
66 + select CRYPTO_ALGAPI
68 + RIPEMD-320 is an optional extension of RIPEMD-160 with a 320 bit hash.
69 + It is intended for applications that require longer hash-results, without
70 + needing a larger security level (than RIPEMD-160).
72 + Developed by Hans Dobbertin, Antoon Bosselaers and Bart Preneel.
73 + See <http://home.esat.kuleuven.be/~bosselae/ripemd160.html>
76 tristate "SHA1 digest algorithm"
80 This is the LZO algorithm.
82 +comment "Random Number Generation"
85 + tristate "Pseudo Random Number Generation for Cryptographic modules"
87 + This option enables the generic pseudo random number generator
88 + for cryptographic modules. Uses the Algorithm specified in
91 source "drivers/crypto/Kconfig"
97 obj-$(CONFIG_CRYPTO_SEQIV) += seqiv.o
99 crypto_hash-objs := hash.o
100 +crypto_hash-objs += ahash.o
101 obj-$(CONFIG_CRYPTO_HASH) += crypto_hash.o
103 obj-$(CONFIG_CRYPTO_MANAGER) += cryptomgr.o
105 obj-$(CONFIG_CRYPTO_NULL) += crypto_null.o
106 obj-$(CONFIG_CRYPTO_MD4) += md4.o
107 obj-$(CONFIG_CRYPTO_MD5) += md5.o
108 +obj-$(CONFIG_CRYPTO_RMD128) += rmd128.o
109 +obj-$(CONFIG_CRYPTO_RMD160) += rmd160.o
110 +obj-$(CONFIG_CRYPTO_RMD256) += rmd256.o
111 +obj-$(CONFIG_CRYPTO_RMD320) += rmd320.o
112 obj-$(CONFIG_CRYPTO_SHA1) += sha1_generic.o
113 obj-$(CONFIG_CRYPTO_SHA256) += sha256_generic.o
114 obj-$(CONFIG_CRYPTO_SHA512) += sha512_generic.o
116 obj-$(CONFIG_CRYPTO_CRC32C) += crc32c.o
117 obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o
118 obj-$(CONFIG_CRYPTO_LZO) += lzo.o
120 +obj-$(CONFIG_CRYPTO_PRNG) += prng.o
121 obj-$(CONFIG_CRYPTO_TEST) += tcrypt.o
128 + * Asynchronous Cryptographic Hash operations.
130 + * This is the asynchronous version of hash.c with notification of
131 + * completion via a callback.
133 + * Copyright (c) 2008 Loc Ho <lho@amcc.com>
135 + * This program is free software; you can redistribute it and/or modify it
136 + * under the terms of the GNU General Public License as published by the Free
137 + * Software Foundation; either version 2 of the License, or (at your option)
138 + * any later version.
142 +#include <crypto/internal/hash.h>
143 +#include <crypto/scatterwalk.h>
144 +#include <linux/err.h>
145 +#include <linux/kernel.h>
146 +#include <linux/module.h>
147 +#include <linux/sched.h>
148 +#include <linux/slab.h>
149 +#include <linux/seq_file.h>
151 +#include "internal.h"
153 +static int hash_walk_next(struct crypto_hash_walk *walk)
155 + unsigned int alignmask = walk->alignmask;
156 + unsigned int offset = walk->offset;
157 + unsigned int nbytes = min(walk->entrylen,
158 + ((unsigned int)(PAGE_SIZE)) - offset);
160 + walk->data = crypto_kmap(walk->pg, 0);
161 + walk->data += offset;
163 + if (offset & alignmask)
164 + nbytes = alignmask + 1 - (offset & alignmask);
166 + walk->entrylen -= nbytes;
170 +static int hash_walk_new_entry(struct crypto_hash_walk *walk)
172 + struct scatterlist *sg;
175 + walk->pg = sg_page(sg);
176 + walk->offset = sg->offset;
177 + walk->entrylen = sg->length;
179 + if (walk->entrylen > walk->total)
180 + walk->entrylen = walk->total;
181 + walk->total -= walk->entrylen;
183 + return hash_walk_next(walk);
186 +int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
188 + unsigned int alignmask = walk->alignmask;
189 + unsigned int nbytes = walk->entrylen;
191 + walk->data -= walk->offset;
193 + if (nbytes && walk->offset & alignmask && !err) {
194 + walk->offset += alignmask - 1;
195 + walk->offset = ALIGN(walk->offset, alignmask + 1);
196 + walk->data += walk->offset;
198 + nbytes = min(nbytes,
199 + ((unsigned int)(PAGE_SIZE)) - walk->offset);
200 + walk->entrylen -= nbytes;
205 + crypto_kunmap(walk->data, 0);
206 + crypto_yield(walk->flags);
214 + return hash_walk_next(walk);
219 + walk->sg = scatterwalk_sg_next(walk->sg);
221 + return hash_walk_new_entry(walk);
223 +EXPORT_SYMBOL_GPL(crypto_hash_walk_done);
225 +int crypto_hash_walk_first(struct ahash_request *req,
226 + struct crypto_hash_walk *walk)
228 + walk->total = req->nbytes;
233 + walk->alignmask = crypto_ahash_alignmask(crypto_ahash_reqtfm(req));
234 + walk->sg = req->src;
235 + walk->flags = req->base.flags;
237 + return hash_walk_new_entry(walk);
239 +EXPORT_SYMBOL_GPL(crypto_hash_walk_first);
241 +static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
242 + unsigned int keylen)
244 + struct ahash_alg *ahash = crypto_ahash_alg(tfm);
245 + unsigned long alignmask = crypto_ahash_alignmask(tfm);
247 + u8 *buffer, *alignbuffer;
248 + unsigned long absize;
250 + absize = keylen + alignmask;
251 + buffer = kmalloc(absize, GFP_ATOMIC);
255 + alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
256 + memcpy(alignbuffer, key, keylen);
257 + ret = ahash->setkey(tfm, alignbuffer, keylen);
258 + memset(alignbuffer, 0, keylen);
263 +static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
264 + unsigned int keylen)
266 + struct ahash_alg *ahash = crypto_ahash_alg(tfm);
267 + unsigned long alignmask = crypto_ahash_alignmask(tfm);
269 + if ((unsigned long)key & alignmask)
270 + return ahash_setkey_unaligned(tfm, key, keylen);
272 + return ahash->setkey(tfm, key, keylen);
275 +static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
278 + return alg->cra_ctxsize;
281 +static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
283 + struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
284 + struct ahash_tfm *crt = &tfm->crt_ahash;
286 + if (alg->digestsize > PAGE_SIZE / 8)
289 + crt->init = alg->init;
290 + crt->update = alg->update;
291 + crt->final = alg->final;
292 + crt->digest = alg->digest;
293 + crt->setkey = ahash_setkey;
294 + crt->digestsize = alg->digestsize;
299 +static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
300 + __attribute__ ((unused));
301 +static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
303 + seq_printf(m, "type : ahash\n");
304 + seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
306 + seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
307 + seq_printf(m, "digestsize : %u\n", alg->cra_hash.digestsize);
310 +const struct crypto_type crypto_ahash_type = {
311 + .ctxsize = crypto_ahash_ctxsize,
312 + .init = crypto_init_ahash_ops,
313 +#ifdef CONFIG_PROC_FS
314 + .show = crypto_ahash_show,
317 +EXPORT_SYMBOL_GPL(crypto_ahash_type);
319 +MODULE_LICENSE("GPL");
320 +MODULE_DESCRIPTION("Asynchronous cryptographic hash type");
324 return crypto_init_cipher_ops(tfm);
326 case CRYPTO_ALG_TYPE_DIGEST:
327 - return crypto_init_digest_ops(tfm);
329 + if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) !=
330 + CRYPTO_ALG_TYPE_HASH_MASK)
331 + return crypto_init_digest_ops_async(tfm);
333 + return crypto_init_digest_ops(tfm);
335 case CRYPTO_ALG_TYPE_COMPRESS:
336 return crypto_init_compress_ops(tfm);
338 --- a/crypto/camellia.c
339 +++ b/crypto/camellia.c
341 #include <linux/init.h>
342 #include <linux/kernel.h>
343 #include <linux/module.h>
344 +#include <linux/bitops.h>
345 +#include <asm/unaligned.h>
347 static const u32 camellia_sp1110[256] = {
348 0x70707000,0x82828200,0x2c2c2c00,0xececec00,
353 -#define GETU32(v, pt) \
355 - /* latest breed of gcc is clever enough to use move */ \
356 - memcpy(&(v), (pt), 4); \
357 - (v) = be32_to_cpu(v); \
360 -/* rotation right shift 1byte */
361 -#define ROR8(x) (((x) >> 8) + ((x) << 24))
362 -/* rotation left shift 1bit */
363 -#define ROL1(x) (((x) << 1) + ((x) >> 31))
364 -/* rotation left shift 1byte */
365 -#define ROL8(x) (((x) << 8) + ((x) >> 24))
367 #define ROLDQ(ll, lr, rl, rr, w0, w1, bits) \
371 ^ camellia_sp3033[(u8)(il >> 8)] \
372 ^ camellia_sp4404[(u8)(il )]; \
375 + yr = ror32(yr, 8); \
380 subL[7] ^= subL[1]; subR[7] ^= subR[1];
381 subL[1] ^= subR[1] & ~subR[9];
382 dw = subL[1] & subL[9],
383 - subR[1] ^= ROL1(dw); /* modified for FLinv(kl2) */
384 + subR[1] ^= rol32(dw, 1); /* modified for FLinv(kl2) */
386 subL[11] ^= subL[1]; subR[11] ^= subR[1];
389 subL[15] ^= subL[1]; subR[15] ^= subR[1];
390 subL[1] ^= subR[1] & ~subR[17];
391 dw = subL[1] & subL[17],
392 - subR[1] ^= ROL1(dw); /* modified for FLinv(kl4) */
393 + subR[1] ^= rol32(dw, 1); /* modified for FLinv(kl4) */
395 subL[19] ^= subL[1]; subR[19] ^= subR[1];
399 subL[1] ^= subR[1] & ~subR[25];
400 dw = subL[1] & subL[25],
401 - subR[1] ^= ROL1(dw); /* modified for FLinv(kl6) */
402 + subR[1] ^= rol32(dw, 1); /* modified for FLinv(kl6) */
404 subL[27] ^= subL[1]; subR[27] ^= subR[1];
407 subL[26] ^= kw4l; subR[26] ^= kw4r;
408 kw4l ^= kw4r & ~subR[24];
409 dw = kw4l & subL[24],
410 - kw4r ^= ROL1(dw); /* modified for FL(kl5) */
411 + kw4r ^= rol32(dw, 1); /* modified for FL(kl5) */
414 subL[22] ^= kw4l; subR[22] ^= kw4r;
416 subL[18] ^= kw4l; subR[18] ^= kw4r;
417 kw4l ^= kw4r & ~subR[16];
418 dw = kw4l & subL[16],
419 - kw4r ^= ROL1(dw); /* modified for FL(kl3) */
420 + kw4r ^= rol32(dw, 1); /* modified for FL(kl3) */
422 subL[14] ^= kw4l; subR[14] ^= kw4r;
425 subL[10] ^= kw4l; subR[10] ^= kw4r;
426 kw4l ^= kw4r & ~subR[8];
428 - kw4r ^= ROL1(dw); /* modified for FL(kl1) */
429 + kw4r ^= rol32(dw, 1); /* modified for FL(kl1) */
431 subL[6] ^= kw4l; subR[6] ^= kw4r;
434 SUBKEY_R(6) = subR[5] ^ subR[7];
435 tl = subL[10] ^ (subR[10] & ~subR[8]);
436 dw = tl & subL[8], /* FL(kl1) */
437 - tr = subR[10] ^ ROL1(dw);
438 + tr = subR[10] ^ rol32(dw, 1);
439 SUBKEY_L(7) = subL[6] ^ tl; /* round 6 */
440 SUBKEY_R(7) = subR[6] ^ tr;
441 SUBKEY_L(8) = subL[8]; /* FL(kl1) */
443 SUBKEY_R(9) = subR[9];
444 tl = subL[7] ^ (subR[7] & ~subR[9]);
445 dw = tl & subL[9], /* FLinv(kl2) */
446 - tr = subR[7] ^ ROL1(dw);
447 + tr = subR[7] ^ rol32(dw, 1);
448 SUBKEY_L(10) = tl ^ subL[11]; /* round 7 */
449 SUBKEY_R(10) = tr ^ subR[11];
450 SUBKEY_L(11) = subL[10] ^ subL[12]; /* round 8 */
452 SUBKEY_R(14) = subR[13] ^ subR[15];
453 tl = subL[18] ^ (subR[18] & ~subR[16]);
454 dw = tl & subL[16], /* FL(kl3) */
455 - tr = subR[18] ^ ROL1(dw);
456 + tr = subR[18] ^ rol32(dw, 1);
457 SUBKEY_L(15) = subL[14] ^ tl; /* round 12 */
458 SUBKEY_R(15) = subR[14] ^ tr;
459 SUBKEY_L(16) = subL[16]; /* FL(kl3) */
461 SUBKEY_R(17) = subR[17];
462 tl = subL[15] ^ (subR[15] & ~subR[17]);
463 dw = tl & subL[17], /* FLinv(kl4) */
464 - tr = subR[15] ^ ROL1(dw);
465 + tr = subR[15] ^ rol32(dw, 1);
466 SUBKEY_L(18) = tl ^ subL[19]; /* round 13 */
467 SUBKEY_R(18) = tr ^ subR[19];
468 SUBKEY_L(19) = subL[18] ^ subL[20]; /* round 14 */
471 tl = subL[26] ^ (subR[26] & ~subR[24]);
472 dw = tl & subL[24], /* FL(kl5) */
473 - tr = subR[26] ^ ROL1(dw);
474 + tr = subR[26] ^ rol32(dw, 1);
475 SUBKEY_L(23) = subL[22] ^ tl; /* round 18 */
476 SUBKEY_R(23) = subR[22] ^ tr;
477 SUBKEY_L(24) = subL[24]; /* FL(kl5) */
479 SUBKEY_R(25) = subR[25];
480 tl = subL[23] ^ (subR[23] & ~subR[25]);
481 dw = tl & subL[25], /* FLinv(kl6) */
482 - tr = subR[23] ^ ROL1(dw);
483 + tr = subR[23] ^ rol32(dw, 1);
484 SUBKEY_L(26) = tl ^ subL[27]; /* round 19 */
485 SUBKEY_R(26) = tr ^ subR[27];
486 SUBKEY_L(27) = subL[26] ^ subL[28]; /* round 20 */
487 @@ -573,17 +561,17 @@
488 /* apply the inverse of the last half of P-function */
491 - dw = SUBKEY_L(i + 0) ^ SUBKEY_R(i + 0); dw = ROL8(dw);/* round 1 */
492 + dw = SUBKEY_L(i + 0) ^ SUBKEY_R(i + 0); dw = rol32(dw, 8);/* round 1 */
493 SUBKEY_R(i + 0) = SUBKEY_L(i + 0) ^ dw; SUBKEY_L(i + 0) = dw;
494 - dw = SUBKEY_L(i + 1) ^ SUBKEY_R(i + 1); dw = ROL8(dw);/* round 2 */
495 + dw = SUBKEY_L(i + 1) ^ SUBKEY_R(i + 1); dw = rol32(dw, 8);/* round 2 */
496 SUBKEY_R(i + 1) = SUBKEY_L(i + 1) ^ dw; SUBKEY_L(i + 1) = dw;
497 - dw = SUBKEY_L(i + 2) ^ SUBKEY_R(i + 2); dw = ROL8(dw);/* round 3 */
498 + dw = SUBKEY_L(i + 2) ^ SUBKEY_R(i + 2); dw = rol32(dw, 8);/* round 3 */
499 SUBKEY_R(i + 2) = SUBKEY_L(i + 2) ^ dw; SUBKEY_L(i + 2) = dw;
500 - dw = SUBKEY_L(i + 3) ^ SUBKEY_R(i + 3); dw = ROL8(dw);/* round 4 */
501 + dw = SUBKEY_L(i + 3) ^ SUBKEY_R(i + 3); dw = rol32(dw, 8);/* round 4 */
502 SUBKEY_R(i + 3) = SUBKEY_L(i + 3) ^ dw; SUBKEY_L(i + 3) = dw;
503 - dw = SUBKEY_L(i + 4) ^ SUBKEY_R(i + 4); dw = ROL8(dw);/* round 5 */
504 + dw = SUBKEY_L(i + 4) ^ SUBKEY_R(i + 4); dw = rol32(dw, 9);/* round 5 */
505 SUBKEY_R(i + 4) = SUBKEY_L(i + 4) ^ dw; SUBKEY_L(i + 4) = dw;
506 - dw = SUBKEY_L(i + 5) ^ SUBKEY_R(i + 5); dw = ROL8(dw);/* round 6 */
507 + dw = SUBKEY_L(i + 5) ^ SUBKEY_R(i + 5); dw = rol32(dw, 8);/* round 6 */
508 SUBKEY_R(i + 5) = SUBKEY_L(i + 5) ^ dw; SUBKEY_L(i + 5) = dw;
511 @@ -599,10 +587,10 @@
513 * k == kll || klr || krl || krr (|| is concatenation)
516 - GETU32(klr, key + 4);
517 - GETU32(krl, key + 8);
518 - GETU32(krr, key + 12);
519 + kll = get_unaligned_be32(key);
520 + klr = get_unaligned_be32(key + 4);
521 + krl = get_unaligned_be32(key + 8);
522 + krr = get_unaligned_be32(key + 12);
524 /* generate KL dependent subkeys */
526 @@ -707,14 +695,14 @@
527 * key = (kll || klr || krl || krr || krll || krlr || krrl || krrr)
528 * (|| is concatenation)
531 - GETU32(klr, key + 4);
532 - GETU32(krl, key + 8);
533 - GETU32(krr, key + 12);
534 - GETU32(krll, key + 16);
535 - GETU32(krlr, key + 20);
536 - GETU32(krrl, key + 24);
537 - GETU32(krrr, key + 28);
538 + kll = get_unaligned_be32(key);
539 + klr = get_unaligned_be32(key + 4);
540 + krl = get_unaligned_be32(key + 8);
541 + krr = get_unaligned_be32(key + 12);
542 + krll = get_unaligned_be32(key + 16);
543 + krlr = get_unaligned_be32(key + 20);
544 + krrl = get_unaligned_be32(key + 24);
545 + krrr = get_unaligned_be32(key + 28);
547 /* generate KL dependent subkeys */
549 @@ -870,13 +858,13 @@
554 + lr ^= rol32(t0, 1); \
561 + rr ^= rol32(t3, 1); \
564 #define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir) \
569 - yr ^= ROR8(il) ^ ir; \
570 + yr ^= ror32(il, 8) ^ ir; \
573 /* max = 24: 128bit encrypt, max = 32: 256bit encrypt */
574 --- a/crypto/crc32c.c
575 +++ b/crypto/crc32c.c
578 * This module file is a wrapper to invoke the lib/crc32c routines.
580 + * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
582 * This program is free software; you can redistribute it and/or modify it
583 * under the terms of the GNU General Public License as published by the Free
584 * Software Foundation; either version 2 of the License, or (at your option)
589 +#include <crypto/internal/hash.h>
590 #include <linux/init.h>
591 #include <linux/module.h>
592 #include <linux/string.h>
593 -#include <linux/crypto.h>
594 #include <linux/crc32c.h>
595 #include <linux/kernel.h>
597 -#define CHKSUM_BLOCK_SIZE 32
598 +#define CHKSUM_BLOCK_SIZE 1
599 #define CHKSUM_DIGEST_SIZE 4
603 *(__le32 *)out = ~cpu_to_le32(mctx->crc);
606 -static int crc32c_cra_init(struct crypto_tfm *tfm)
607 +static int crc32c_cra_init_old(struct crypto_tfm *tfm)
609 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
615 -static struct crypto_alg alg = {
616 +static struct crypto_alg old_alg = {
617 .cra_name = "crc32c",
618 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
619 .cra_blocksize = CHKSUM_BLOCK_SIZE,
620 .cra_ctxsize = sizeof(struct chksum_ctx),
621 .cra_module = THIS_MODULE,
622 - .cra_list = LIST_HEAD_INIT(alg.cra_list),
623 - .cra_init = crc32c_cra_init,
624 + .cra_list = LIST_HEAD_INIT(old_alg.cra_list),
625 + .cra_init = crc32c_cra_init_old,
628 .dia_digestsize= CHKSUM_DIGEST_SIZE,
629 @@ -98,14 +101,125 @@
634 + * Setting the seed allows arbitrary accumulators and flexible XOR policy
635 + * If your algorithm starts with ~0, then XOR with ~0 before you set
638 +static int crc32c_setkey(struct crypto_ahash *hash, const u8 *key,
639 + unsigned int keylen)
641 + u32 *mctx = crypto_ahash_ctx(hash);
643 + if (keylen != sizeof(u32)) {
644 + crypto_ahash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
647 + *mctx = le32_to_cpup((__le32 *)key);
651 +static int crc32c_init(struct ahash_request *req)
653 + u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
654 + u32 *crcp = ahash_request_ctx(req);
660 +static int crc32c_update(struct ahash_request *req)
662 + struct crypto_hash_walk walk;
663 + u32 *crcp = ahash_request_ctx(req);
667 + for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
668 + nbytes = crypto_hash_walk_done(&walk, 0))
669 + crc = crc32c(crc, walk.data, nbytes);
675 +static int crc32c_final(struct ahash_request *req)
677 + u32 *crcp = ahash_request_ctx(req);
679 + *(__le32 *)req->result = ~cpu_to_le32p(crcp);
683 +static int crc32c_digest(struct ahash_request *req)
685 + struct crypto_hash_walk walk;
686 + u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
690 + for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
691 + nbytes = crypto_hash_walk_done(&walk, 0))
692 + crc = crc32c(crc, walk.data, nbytes);
694 + *(__le32 *)req->result = ~cpu_to_le32(crc);
698 +static int crc32c_cra_init(struct crypto_tfm *tfm)
700 + u32 *key = crypto_tfm_ctx(tfm);
704 + tfm->crt_ahash.reqsize = sizeof(u32);
709 +static struct crypto_alg alg = {
710 + .cra_name = "crc32c",
711 + .cra_driver_name = "crc32c-generic",
712 + .cra_priority = 100,
713 + .cra_flags = CRYPTO_ALG_TYPE_AHASH,
714 + .cra_blocksize = CHKSUM_BLOCK_SIZE,
715 + .cra_alignmask = 3,
716 + .cra_ctxsize = sizeof(u32),
717 + .cra_module = THIS_MODULE,
718 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
719 + .cra_init = crc32c_cra_init,
720 + .cra_type = &crypto_ahash_type,
723 + .digestsize = CHKSUM_DIGEST_SIZE,
724 + .setkey = crc32c_setkey,
725 + .init = crc32c_init,
726 + .update = crc32c_update,
727 + .final = crc32c_final,
728 + .digest = crc32c_digest,
733 static int __init crc32c_mod_init(void)
735 - return crypto_register_alg(&alg);
738 + err = crypto_register_alg(&old_alg);
742 + err = crypto_register_alg(&alg);
744 + crypto_unregister_alg(&old_alg);
749 static void __exit crc32c_mod_fini(void)
751 crypto_unregister_alg(&alg);
752 + crypto_unregister_alg(&old_alg);
755 module_init(crc32c_mod_init);
756 --- a/crypto/cryptd.c
757 +++ b/crypto/cryptd.c
761 #include <crypto/algapi.h>
762 +#include <crypto/internal/hash.h>
763 #include <linux/err.h>
764 #include <linux/init.h>
765 #include <linux/kernel.h>
767 crypto_completion_t complete;
770 +struct cryptd_hash_ctx {
771 + struct crypto_hash *child;
774 +struct cryptd_hash_request_ctx {
775 + crypto_completion_t complete;
778 static inline struct cryptd_state *cryptd_get_state(struct crypto_tfm *tfm)
782 rctx = ablkcipher_request_ctx(req);
784 - if (unlikely(err == -EINPROGRESS)) {
785 - rctx->complete(&req->base, err);
788 + if (unlikely(err == -EINPROGRESS))
792 desc.info = req->info;
795 req->base.complete = rctx->complete;
799 - req->base.complete(&req->base, err);
800 + rctx->complete(&req->base, err);
804 @@ -261,6 +268,240 @@
808 +static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
810 + struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
811 + struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
812 + struct crypto_spawn *spawn = &ictx->spawn;
813 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
814 + struct crypto_hash *cipher;
816 + cipher = crypto_spawn_hash(spawn);
817 + if (IS_ERR(cipher))
818 + return PTR_ERR(cipher);
820 + ctx->child = cipher;
821 + tfm->crt_ahash.reqsize =
822 + sizeof(struct cryptd_hash_request_ctx);
826 +static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
828 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
829 + struct cryptd_state *state = cryptd_get_state(tfm);
832 + mutex_lock(&state->mutex);
833 + active = ahash_tfm_in_queue(&state->queue,
834 + __crypto_ahash_cast(tfm));
835 + mutex_unlock(&state->mutex);
839 + crypto_free_hash(ctx->child);
842 +static int cryptd_hash_setkey(struct crypto_ahash *parent,
843 + const u8 *key, unsigned int keylen)
845 + struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
846 + struct crypto_hash *child = ctx->child;
849 + crypto_hash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
850 + crypto_hash_set_flags(child, crypto_ahash_get_flags(parent) &
851 + CRYPTO_TFM_REQ_MASK);
852 + err = crypto_hash_setkey(child, key, keylen);
853 + crypto_ahash_set_flags(parent, crypto_hash_get_flags(child) &
854 + CRYPTO_TFM_RES_MASK);
858 +static int cryptd_hash_enqueue(struct ahash_request *req,
859 + crypto_completion_t complete)
861 + struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
862 + struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
863 + struct cryptd_state *state =
864 + cryptd_get_state(crypto_ahash_tfm(tfm));
867 + rctx->complete = req->base.complete;
868 + req->base.complete = complete;
870 + spin_lock_bh(&state->lock);
871 + err = ahash_enqueue_request(&state->queue, req);
872 + spin_unlock_bh(&state->lock);
874 + wake_up_process(state->task);
878 +static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
880 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
881 + struct crypto_hash *child = ctx->child;
882 + struct ahash_request *req = ahash_request_cast(req_async);
883 + struct cryptd_hash_request_ctx *rctx;
884 + struct hash_desc desc;
886 + rctx = ahash_request_ctx(req);
888 + if (unlikely(err == -EINPROGRESS))
892 + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
894 + err = crypto_hash_crt(child)->init(&desc);
896 + req->base.complete = rctx->complete;
899 + local_bh_disable();
900 + rctx->complete(&req->base, err);
904 +static int cryptd_hash_init_enqueue(struct ahash_request *req)
906 + return cryptd_hash_enqueue(req, cryptd_hash_init);
909 +static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
911 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
912 + struct crypto_hash *child = ctx->child;
913 + struct ahash_request *req = ahash_request_cast(req_async);
914 + struct cryptd_hash_request_ctx *rctx;
915 + struct hash_desc desc;
917 + rctx = ahash_request_ctx(req);
919 + if (unlikely(err == -EINPROGRESS))
923 + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
925 + err = crypto_hash_crt(child)->update(&desc,
929 + req->base.complete = rctx->complete;
932 + local_bh_disable();
933 + rctx->complete(&req->base, err);
937 +static int cryptd_hash_update_enqueue(struct ahash_request *req)
939 + return cryptd_hash_enqueue(req, cryptd_hash_update);
942 +static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
944 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
945 + struct crypto_hash *child = ctx->child;
946 + struct ahash_request *req = ahash_request_cast(req_async);
947 + struct cryptd_hash_request_ctx *rctx;
948 + struct hash_desc desc;
950 + rctx = ahash_request_ctx(req);
952 + if (unlikely(err == -EINPROGRESS))
956 + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
958 + err = crypto_hash_crt(child)->final(&desc, req->result);
960 + req->base.complete = rctx->complete;
963 + local_bh_disable();
964 + rctx->complete(&req->base, err);
968 +static int cryptd_hash_final_enqueue(struct ahash_request *req)
970 + return cryptd_hash_enqueue(req, cryptd_hash_final);
973 +static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
975 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
976 + struct crypto_hash *child = ctx->child;
977 + struct ahash_request *req = ahash_request_cast(req_async);
978 + struct cryptd_hash_request_ctx *rctx;
979 + struct hash_desc desc;
981 + rctx = ahash_request_ctx(req);
983 + if (unlikely(err == -EINPROGRESS))
987 + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
989 + err = crypto_hash_crt(child)->digest(&desc,
994 + req->base.complete = rctx->complete;
997 + local_bh_disable();
998 + rctx->complete(&req->base, err);
1002 +static int cryptd_hash_digest_enqueue(struct ahash_request *req)
1004 + return cryptd_hash_enqueue(req, cryptd_hash_digest);
1007 +static struct crypto_instance *cryptd_alloc_hash(
1008 + struct rtattr **tb, struct cryptd_state *state)
1010 + struct crypto_instance *inst;
1011 + struct crypto_alg *alg;
1013 + alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
1014 + CRYPTO_ALG_TYPE_HASH_MASK);
1016 + return ERR_PTR(PTR_ERR(alg));
1018 + inst = cryptd_alloc_instance(alg, state);
1022 + inst->alg.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
1023 + inst->alg.cra_type = &crypto_ahash_type;
1025 + inst->alg.cra_ahash.digestsize = alg->cra_hash.digestsize;
1026 + inst->alg.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
1028 + inst->alg.cra_init = cryptd_hash_init_tfm;
1029 + inst->alg.cra_exit = cryptd_hash_exit_tfm;
1031 + inst->alg.cra_ahash.init = cryptd_hash_init_enqueue;
1032 + inst->alg.cra_ahash.update = cryptd_hash_update_enqueue;
1033 + inst->alg.cra_ahash.final = cryptd_hash_final_enqueue;
1034 + inst->alg.cra_ahash.setkey = cryptd_hash_setkey;
1035 + inst->alg.cra_ahash.digest = cryptd_hash_digest_enqueue;
1038 + crypto_mod_put(alg);
1042 static struct cryptd_state state;
1044 static struct crypto_instance *cryptd_alloc(struct rtattr **tb)
1046 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
1047 case CRYPTO_ALG_TYPE_BLKCIPHER:
1048 return cryptd_alloc_blkcipher(tb, &state);
1049 + case CRYPTO_ALG_TYPE_DIGEST:
1050 + return cryptd_alloc_hash(tb, &state);
1053 return ERR_PTR(-EINVAL);
1054 --- a/crypto/digest.c
1055 +++ b/crypto/digest.c
1060 +#include <crypto/internal/hash.h>
1061 #include <crypto/scatterwalk.h>
1062 #include <linux/mm.h>
1063 #include <linux/errno.h>
1065 struct hash_tfm *ops = &tfm->crt_hash;
1066 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
1068 - if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
1069 + if (dalg->dia_digestsize > PAGE_SIZE / 8)
1073 @@ -157,3 +158,83 @@
1074 void crypto_exit_digest_ops(struct crypto_tfm *tfm)
1078 +static int digest_async_nosetkey(struct crypto_ahash *tfm_async, const u8 *key,
1079 + unsigned int keylen)
1081 + crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
1085 +static int digest_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
1086 + unsigned int keylen)
1088 + struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
1089 + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
1091 + crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
1092 + return dalg->dia_setkey(tfm, key, keylen);
1095 +static int digest_async_init(struct ahash_request *req)
1097 + struct crypto_tfm *tfm = req->base.tfm;
1098 + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
1100 + dalg->dia_init(tfm);
1104 +static int digest_async_update(struct ahash_request *req)
1106 + struct crypto_tfm *tfm = req->base.tfm;
1107 + struct hash_desc desc = {
1108 + .tfm = __crypto_hash_cast(tfm),
1109 + .flags = req->base.flags,
1112 + update(&desc, req->src, req->nbytes);
1116 +static int digest_async_final(struct ahash_request *req)
1118 + struct crypto_tfm *tfm = req->base.tfm;
1119 + struct hash_desc desc = {
1120 + .tfm = __crypto_hash_cast(tfm),
1121 + .flags = req->base.flags,
1124 + final(&desc, req->result);
1128 +static int digest_async_digest(struct ahash_request *req)
1130 + struct crypto_tfm *tfm = req->base.tfm;
1131 + struct hash_desc desc = {
1132 + .tfm = __crypto_hash_cast(tfm),
1133 + .flags = req->base.flags,
1136 + return digest(&desc, req->src, req->nbytes, req->result);
1139 +int crypto_init_digest_ops_async(struct crypto_tfm *tfm)
1141 + struct ahash_tfm *crt = &tfm->crt_ahash;
1142 + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
1144 + if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
1147 + crt->init = digest_async_init;
1148 + crt->update = digest_async_update;
1149 + crt->final = digest_async_final;
1150 + crt->digest = digest_async_digest;
1151 + crt->setkey = dalg->dia_setkey ? digest_async_setkey :
1152 + digest_async_nosetkey;
1153 + crt->digestsize = dalg->dia_digestsize;
1160 * any later version.
1163 +#include <crypto/internal/hash.h>
1164 #include <linux/errno.h>
1165 #include <linux/kernel.h>
1166 #include <linux/module.h>
1167 @@ -59,24 +60,107 @@
1168 return alg->setkey(crt, key, keylen);
1171 -static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
1172 +static int hash_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
1173 + unsigned int keylen)
1175 + struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
1176 + struct crypto_hash *tfm_hash = __crypto_hash_cast(tfm);
1177 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1179 + return alg->setkey(tfm_hash, key, keylen);
1182 +static int hash_async_init(struct ahash_request *req)
1184 + struct crypto_tfm *tfm = req->base.tfm;
1185 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1186 + struct hash_desc desc = {
1187 + .tfm = __crypto_hash_cast(tfm),
1188 + .flags = req->base.flags,
1191 + return alg->init(&desc);
1194 +static int hash_async_update(struct ahash_request *req)
1196 + struct crypto_tfm *tfm = req->base.tfm;
1197 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1198 + struct hash_desc desc = {
1199 + .tfm = __crypto_hash_cast(tfm),
1200 + .flags = req->base.flags,
1203 + return alg->update(&desc, req->src, req->nbytes);
1206 +static int hash_async_final(struct ahash_request *req)
1208 + struct crypto_tfm *tfm = req->base.tfm;
1209 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1210 + struct hash_desc desc = {
1211 + .tfm = __crypto_hash_cast(tfm),
1212 + .flags = req->base.flags,
1215 + return alg->final(&desc, req->result);
1218 +static int hash_async_digest(struct ahash_request *req)
1220 + struct crypto_tfm *tfm = req->base.tfm;
1221 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1222 + struct hash_desc desc = {
1223 + .tfm = __crypto_hash_cast(tfm),
1224 + .flags = req->base.flags,
1227 + return alg->digest(&desc, req->src, req->nbytes, req->result);
1230 +static int crypto_init_hash_ops_async(struct crypto_tfm *tfm)
1232 + struct ahash_tfm *crt = &tfm->crt_ahash;
1233 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1235 + crt->init = hash_async_init;
1236 + crt->update = hash_async_update;
1237 + crt->final = hash_async_final;
1238 + crt->digest = hash_async_digest;
1239 + crt->setkey = hash_async_setkey;
1240 + crt->digestsize = alg->digestsize;
1245 +static int crypto_init_hash_ops_sync(struct crypto_tfm *tfm)
1247 struct hash_tfm *crt = &tfm->crt_hash;
1248 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1250 - if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
1253 - crt->init = alg->init;
1254 - crt->update = alg->update;
1255 - crt->final = alg->final;
1256 - crt->digest = alg->digest;
1257 - crt->setkey = hash_setkey;
1258 + crt->init = alg->init;
1259 + crt->update = alg->update;
1260 + crt->final = alg->final;
1261 + crt->digest = alg->digest;
1262 + crt->setkey = hash_setkey;
1263 crt->digestsize = alg->digestsize;
1268 +static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
1270 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1272 + if (alg->digestsize > PAGE_SIZE / 8)
1275 + if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK)
1276 + return crypto_init_hash_ops_async(tfm);
1278 + return crypto_init_hash_ops_sync(tfm);
1281 static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
1282 __attribute__ ((unused));
1283 static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
1287 struct crypto_instance *inst;
1288 struct crypto_alg *alg;
1292 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH);
1294 @@ -236,6 +237,13 @@
1296 return ERR_CAST(alg);
1298 + inst = ERR_PTR(-EINVAL);
1299 + ds = (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
1300 + CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
1301 + alg->cra_digest.dia_digestsize;
1302 + if (ds > alg->cra_blocksize)
1305 inst = crypto_alloc_instance("hmac", alg);
1308 @@ -246,14 +254,10 @@
1309 inst->alg.cra_alignmask = alg->cra_alignmask;
1310 inst->alg.cra_type = &crypto_hash_type;
1312 - inst->alg.cra_hash.digestsize =
1313 - (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
1314 - CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
1315 - alg->cra_digest.dia_digestsize;
1316 + inst->alg.cra_hash.digestsize = ds;
1318 inst->alg.cra_ctxsize = sizeof(struct hmac_ctx) +
1319 - ALIGN(inst->alg.cra_blocksize * 2 +
1320 - inst->alg.cra_hash.digestsize,
1321 + ALIGN(inst->alg.cra_blocksize * 2 + ds,
1324 inst->alg.cra_init = hmac_init_tfm;
1325 --- a/crypto/internal.h
1326 +++ b/crypto/internal.h
1328 struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
1330 int crypto_init_digest_ops(struct crypto_tfm *tfm);
1331 +int crypto_init_digest_ops_async(struct crypto_tfm *tfm);
1332 int crypto_init_cipher_ops(struct crypto_tfm *tfm);
1333 int crypto_init_compress_ops(struct crypto_tfm *tfm);
1339 + * PRNG: Pseudo Random Number Generator
1340 + * Based on NIST Recommended PRNG From ANSI X9.31 Appendix A.2.4 using
1341 + * AES 128 cipher in RFC3686 ctr mode
1343 + * (C) Neil Horman <nhorman@tuxdriver.com>
1345 + * This program is free software; you can redistribute it and/or modify it
1346 + * under the terms of the GNU General Public License as published by the
1347 + * Free Software Foundation; either version 2 of the License, or (at your
1348 + * any later version.
1353 +#include <linux/err.h>
1354 +#include <linux/init.h>
1355 +#include <linux/module.h>
1356 +#include <linux/mm.h>
1357 +#include <linux/slab.h>
1358 +#include <linux/fs.h>
1359 +#include <linux/scatterlist.h>
1360 +#include <linux/string.h>
1361 +#include <linux/crypto.h>
1362 +#include <linux/highmem.h>
1363 +#include <linux/moduleparam.h>
1364 +#include <linux/jiffies.h>
1365 +#include <linux/timex.h>
1366 +#include <linux/interrupt.h>
1367 +#include <linux/miscdevice.h>
1370 +#define TEST_PRNG_ON_START 0
1372 +#define DEFAULT_PRNG_KEY "0123456789abcdef1011"
1373 +#define DEFAULT_PRNG_KSZ 20
1374 +#define DEFAULT_PRNG_IV "defaultv"
1375 +#define DEFAULT_PRNG_IVSZ 8
1376 +#define DEFAULT_BLK_SZ 16
1377 +#define DEFAULT_V_SEED "zaybxcwdveuftgsh"
1380 + * Flags for the prng_context flags field
1383 +#define PRNG_FIXED_SIZE 0x1
1384 +#define PRNG_NEED_RESET 0x2
1387 + * Note: DT is our counter value
1388 + * I is our intermediate value
1389 + * V is our seed vector
1390 + * See http://csrc.nist.gov/groups/STM/cavp/documents/rng/931rngext.pdf
1391 + * for implementation details
1395 +struct prng_context {
1398 + spinlock_t prng_lock;
1399 + unsigned char rand_data[DEFAULT_BLK_SZ];
1400 + unsigned char last_rand_data[DEFAULT_BLK_SZ];
1401 + unsigned char DT[DEFAULT_BLK_SZ];
1402 + unsigned char I[DEFAULT_BLK_SZ];
1403 + unsigned char V[DEFAULT_BLK_SZ];
1404 + u32 rand_data_valid;
1405 + struct crypto_blkcipher *tfm;
1411 +static void hexdump(char *note, unsigned char *buf, unsigned int len)
1414 + printk(KERN_CRIT "%s", note);
1415 + print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
1421 +#define dbgprint(format, args...) do {if(dbg) printk(format, ##args);} while(0)
1423 +static void xor_vectors(unsigned char *in1, unsigned char *in2,
1424 + unsigned char *out, unsigned int size)
1428 + for (i=0;i<size;i++)
1429 + out[i] = in1[i] ^ in2[i];
1433 + * Returns DEFAULT_BLK_SZ bytes of random data per call
1434 + * returns 0 if generation succeded, <0 if something went wrong
1436 +static int _get_more_prng_bytes(struct prng_context *ctx)
1439 + struct blkcipher_desc desc;
1440 + struct scatterlist sg_in, sg_out;
1442 + unsigned char tmp[DEFAULT_BLK_SZ];
1444 + desc.tfm = ctx->tfm;
1448 + dbgprint(KERN_CRIT "Calling _get_more_prng_bytes for context %p\n",ctx);
1450 + hexdump("Input DT: ", ctx->DT, DEFAULT_BLK_SZ);
1451 + hexdump("Input I: ", ctx->I, DEFAULT_BLK_SZ);
1452 + hexdump("Input V: ", ctx->V, DEFAULT_BLK_SZ);
1455 + * This algorithm is a 3 stage state machine
1457 + for (i=0;i<3;i++) {
1459 + desc.tfm = ctx->tfm;
1464 + * Start by encrypting the counter value
1465 + * This gives us an intermediate value I
1467 + memcpy(tmp, ctx->DT, DEFAULT_BLK_SZ);
1468 + sg_init_one(&sg_out, &ctx->I[0], DEFAULT_BLK_SZ);
1469 + hexdump("tmp stage 0: ", tmp, DEFAULT_BLK_SZ);
1474 + * Next xor I with our secret vector V
1475 + * encrypt that result to obtain our
1476 + * pseudo random data which we output
1478 + xor_vectors(ctx->I, ctx->V, tmp, DEFAULT_BLK_SZ);
1479 + sg_init_one(&sg_out, &ctx->rand_data[0], DEFAULT_BLK_SZ);
1480 + hexdump("tmp stage 1: ", tmp, DEFAULT_BLK_SZ);
1484 + * First check that we didn't produce the same random data
1485 + * that we did last time around through this
1487 + if (!memcmp(ctx->rand_data, ctx->last_rand_data, DEFAULT_BLK_SZ)) {
1488 + printk(KERN_ERR "ctx %p Failed repetition check!\n",
1490 + ctx->flags |= PRNG_NEED_RESET;
1493 + memcpy(ctx->last_rand_data, ctx->rand_data, DEFAULT_BLK_SZ);
1496 + * Lastly xor the random data with I
1497 + * and encrypt that to obtain a new secret vector V
1499 + xor_vectors(ctx->rand_data, ctx->I, tmp, DEFAULT_BLK_SZ);
1500 + sg_init_one(&sg_out, &ctx->V[0], DEFAULT_BLK_SZ);
1501 + hexdump("tmp stage 2: ", tmp, DEFAULT_BLK_SZ);
1505 + /* Initialize our input buffer */
1506 + sg_init_one(&sg_in, &tmp[0], DEFAULT_BLK_SZ);
1508 + /* do the encryption */
1509 + ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, DEFAULT_BLK_SZ);
1511 + /* And check the result */
1513 + dbgprint(KERN_CRIT "Encryption of new block failed for context %p\n",ctx);
1514 + ctx->rand_data_valid = DEFAULT_BLK_SZ;
1521 + * Now update our DT value
1523 + for (i=DEFAULT_BLK_SZ-1;i>0;i--) {
1524 + ctx->DT[i] = ctx->DT[i-1];
1528 + dbgprint("Returning new block for context %p\n",ctx);
1529 + ctx->rand_data_valid = 0;
1531 + hexdump("Output DT: ", ctx->DT, DEFAULT_BLK_SZ);
1532 + hexdump("Output I: ", ctx->I, DEFAULT_BLK_SZ);
1533 + hexdump("Output V: ", ctx->V, DEFAULT_BLK_SZ);
1534 + hexdump("New Random Data: ", ctx->rand_data, DEFAULT_BLK_SZ);
1539 +/* Our exported functions */
1540 +int get_prng_bytes(char *buf, int nbytes, struct prng_context *ctx)
1542 + unsigned long flags;
1543 + unsigned char *ptr = buf;
1544 + unsigned int byte_count = (unsigned int)nbytes;
1551 + spin_lock_irqsave(&ctx->prng_lock, flags);
1554 + if (ctx->flags & PRNG_NEED_RESET)
1558 + * If the FIXED_SIZE flag is on, only return whole blocks of
1559 + * pseudo random data
1562 + if (ctx->flags & PRNG_FIXED_SIZE) {
1563 + if (nbytes < DEFAULT_BLK_SZ)
1565 + byte_count = DEFAULT_BLK_SZ;
1570 + dbgprint(KERN_CRIT "getting %d random bytes for context %p\n",byte_count, ctx);
1574 + if (ctx->rand_data_valid == DEFAULT_BLK_SZ) {
1575 + if (_get_more_prng_bytes(ctx) < 0) {
1576 + memset(buf, 0, nbytes);
1583 + * Copy up to the next whole block size
1585 + if (byte_count < DEFAULT_BLK_SZ) {
1586 + for (;ctx->rand_data_valid < DEFAULT_BLK_SZ; ctx->rand_data_valid++) {
1587 + *ptr = ctx->rand_data[ctx->rand_data_valid];
1590 + if (byte_count == 0)
1596 + * Now copy whole blocks
1598 + for(;byte_count >= DEFAULT_BLK_SZ; byte_count -= DEFAULT_BLK_SZ) {
1599 + if (_get_more_prng_bytes(ctx) < 0) {
1600 + memset(buf, 0, nbytes);
1604 + memcpy(ptr, ctx->rand_data, DEFAULT_BLK_SZ);
1605 + ctx->rand_data_valid += DEFAULT_BLK_SZ;
1606 + ptr += DEFAULT_BLK_SZ;
1610 + * Now copy any extra partial data
1616 + spin_unlock_irqrestore(&ctx->prng_lock, flags);
1617 + dbgprint(KERN_CRIT "returning %d from get_prng_bytes in context %p\n",err, ctx);
1620 +EXPORT_SYMBOL_GPL(get_prng_bytes);
1622 +struct prng_context *alloc_prng_context(void)
1624 + struct prng_context *ctx=kzalloc(sizeof(struct prng_context), GFP_KERNEL);
1626 + spin_lock_init(&ctx->prng_lock);
1628 + if (reset_prng_context(ctx, NULL, NULL, NULL, NULL)) {
1633 + dbgprint(KERN_CRIT "returning context %p\n",ctx);
1637 +EXPORT_SYMBOL_GPL(alloc_prng_context);
1639 +void free_prng_context(struct prng_context *ctx)
1641 + crypto_free_blkcipher(ctx->tfm);
1644 +EXPORT_SYMBOL_GPL(free_prng_context);
1646 +int reset_prng_context(struct prng_context *ctx,
1647 + unsigned char *key, unsigned char *iv,
1648 + unsigned char *V, unsigned char *DT)
1654 + spin_lock(&ctx->prng_lock);
1655 + ctx->flags |= PRNG_NEED_RESET;
1658 + memcpy(ctx->prng_key,key,strlen(ctx->prng_key));
1660 + ctx->prng_key = DEFAULT_PRNG_KEY;
1663 + memcpy(ctx->prng_iv,iv, strlen(ctx->prng_iv));
1665 + ctx->prng_iv = DEFAULT_PRNG_IV;
1668 + memcpy(ctx->V,V,DEFAULT_BLK_SZ);
1670 + memcpy(ctx->V,DEFAULT_V_SEED,DEFAULT_BLK_SZ);
1673 + memcpy(ctx->DT, DT, DEFAULT_BLK_SZ);
1675 + memset(ctx->DT, 0, DEFAULT_BLK_SZ);
1677 + memset(ctx->rand_data,0,DEFAULT_BLK_SZ);
1678 + memset(ctx->last_rand_data,0,DEFAULT_BLK_SZ);
1681 + crypto_free_blkcipher(ctx->tfm);
1683 + ctx->tfm = crypto_alloc_blkcipher("rfc3686(ctr(aes))",0,0);
1685 + dbgprint(KERN_CRIT "Failed to alloc crypto tfm for context %p\n",ctx->tfm);
1689 + ctx->rand_data_valid = DEFAULT_BLK_SZ;
1691 + ret = crypto_blkcipher_setkey(ctx->tfm, ctx->prng_key, strlen(ctx->prng_key));
1693 + dbgprint(KERN_CRIT "PRNG: setkey() failed flags=%x\n",
1694 + crypto_blkcipher_get_flags(ctx->tfm));
1695 + crypto_free_blkcipher(ctx->tfm);
1699 + iv_len = crypto_blkcipher_ivsize(ctx->tfm);
1701 + crypto_blkcipher_set_iv(ctx->tfm, ctx->prng_iv, iv_len);
1704 + ctx->flags &= ~PRNG_NEED_RESET;
1706 + spin_unlock(&ctx->prng_lock);
1711 +EXPORT_SYMBOL_GPL(reset_prng_context);
1713 +/* Module initalization */
1714 +static int __init prng_mod_init(void)
1717 +#ifdef TEST_PRNG_ON_START
1719 + unsigned char tmpbuf[DEFAULT_BLK_SZ];
1721 + struct prng_context *ctx = alloc_prng_context();
1724 + for (i=0;i<16;i++) {
1725 + if (get_prng_bytes(tmpbuf, DEFAULT_BLK_SZ, ctx) < 0) {
1726 + free_prng_context(ctx);
1730 + free_prng_context(ctx);
1736 +static void __exit prng_mod_fini(void)
1741 +MODULE_LICENSE("GPL");
1742 +MODULE_DESCRIPTION("Software Pseudo Random Number Generator");
1743 +MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
1744 +module_param(dbg, int, 0);
1745 +MODULE_PARM_DESC(dbg, "Boolean to enable debugging (0/1 == off/on)");
1746 +module_init(prng_mod_init);
1747 +module_exit(prng_mod_fini);
1752 + * PRNG: Pseudo Random Number Generator
1754 + * (C) Neil Horman <nhorman@tuxdriver.com>
1756 + * This program is free software; you can redistribute it and/or modify it
1757 + * under the terms of the GNU General Public License as published by the
1758 + * Free Software Foundation; either version 2 of the License, or (at your
1759 + * any later version.
1766 +struct prng_context;
1768 +int get_prng_bytes(char *buf, int nbytes, struct prng_context *ctx);
1769 +struct prng_context *alloc_prng_context(void);
1770 +int reset_prng_context(struct prng_context *ctx,
1771 + unsigned char *key, unsigned char *iv,
1773 + unsigned char *DT);
1774 +void free_prng_context(struct prng_context *ctx);
1779 +++ b/crypto/ripemd.h
1782 + * Common values for RIPEMD algorithms
1785 +#ifndef _CRYPTO_RMD_H
1786 +#define _CRYPTO_RMD_H
1788 +#define RMD128_DIGEST_SIZE 16
1789 +#define RMD128_BLOCK_SIZE 64
1791 +#define RMD160_DIGEST_SIZE 20
1792 +#define RMD160_BLOCK_SIZE 64
1794 +#define RMD256_DIGEST_SIZE 32
1795 +#define RMD256_BLOCK_SIZE 64
1797 +#define RMD320_DIGEST_SIZE 40
1798 +#define RMD320_BLOCK_SIZE 64
1800 +/* initial values */
1801 +#define RMD_H0 0x67452301UL
1802 +#define RMD_H1 0xefcdab89UL
1803 +#define RMD_H2 0x98badcfeUL
1804 +#define RMD_H3 0x10325476UL
1805 +#define RMD_H4 0xc3d2e1f0UL
1806 +#define RMD_H5 0x76543210UL
1807 +#define RMD_H6 0xfedcba98UL
1808 +#define RMD_H7 0x89abcdefUL
1809 +#define RMD_H8 0x01234567UL
1810 +#define RMD_H9 0x3c2d1e0fUL
1813 +#define RMD_K1 0x00000000UL
1814 +#define RMD_K2 0x5a827999UL
1815 +#define RMD_K3 0x6ed9eba1UL
1816 +#define RMD_K4 0x8f1bbcdcUL
1817 +#define RMD_K5 0xa953fd4eUL
1818 +#define RMD_K6 0x50a28be6UL
1819 +#define RMD_K7 0x5c4dd124UL
1820 +#define RMD_K8 0x6d703ef3UL
1821 +#define RMD_K9 0x7a6d76e9UL
1825 +++ b/crypto/rmd128.c
1828 + * Cryptographic API.
1830 + * RIPEMD-128 - RACE Integrity Primitives Evaluation Message Digest.
1832 + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
1834 + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
1836 + * This program is free software; you can redistribute it and/or modify it
1837 + * under the terms of the GNU General Public License as published by the Free
1838 + * Software Foundation; either version 2 of the License, or (at your option)
1839 + * any later version.
1842 +#include <linux/init.h>
1843 +#include <linux/module.h>
1844 +#include <linux/mm.h>
1845 +#include <linux/crypto.h>
1846 +#include <linux/cryptohash.h>
1847 +#include <linux/types.h>
1848 +#include <asm/byteorder.h>
1850 +#include "ripemd.h"
1852 +struct rmd128_ctx {
1855 + __le32 buffer[16];
1867 +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
1868 +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
1869 +#define F3(x, y, z) ((x | ~y) ^ z)
1870 +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
1872 +#define ROUND(a, b, c, d, f, k, x, s) { \
1873 + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
1874 + (a) = rol32((a), (s)); \
1877 +static void rmd128_transform(u32 *state, const __le32 *in)
1879 + u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
1881 + /* Initialize left lane */
1887 + /* Initialize right lane */
1893 + /* round 1: left lane */
1894 + ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
1895 + ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
1896 + ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
1897 + ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
1898 + ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
1899 + ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
1900 + ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
1901 + ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
1902 + ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
1903 + ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
1904 + ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
1905 + ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
1906 + ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
1907 + ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
1908 + ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
1909 + ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
1911 + /* round 2: left lane */
1912 + ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
1913 + ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
1914 + ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
1915 + ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
1916 + ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
1917 + ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
1918 + ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
1919 + ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
1920 + ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
1921 + ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
1922 + ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
1923 + ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
1924 + ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
1925 + ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
1926 + ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
1927 + ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
1929 + /* round 3: left lane */
1930 + ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
1931 + ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
1932 + ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
1933 + ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
1934 + ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
1935 + ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
1936 + ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
1937 + ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
1938 + ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
1939 + ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
1940 + ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
1941 + ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
1942 + ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
1943 + ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
1944 + ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
1945 + ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
1947 + /* round 4: left lane */
1948 + ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
1949 + ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
1950 + ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
1951 + ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
1952 + ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
1953 + ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
1954 + ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
1955 + ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
1956 + ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
1957 + ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
1958 + ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
1959 + ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
1960 + ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
1961 + ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
1962 + ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
1963 + ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
1965 + /* round 1: right lane */
1966 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
1967 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
1968 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
1969 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
1970 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
1971 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
1972 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
1973 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
1974 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
1975 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
1976 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
1977 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
1978 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
1979 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
1980 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
1981 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
1983 + /* round 2: right lane */
1984 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
1985 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
1986 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
1987 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
1988 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
1989 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
1990 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
1991 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
1992 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
1993 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
1994 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
1995 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
1996 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
1997 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
1998 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
1999 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
2001 + /* round 3: right lane */
2002 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
2003 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
2004 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
2005 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
2006 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
2007 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
2008 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
2009 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
2010 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
2011 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
2012 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
2013 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
2014 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
2015 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
2016 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
2017 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
2019 + /* round 4: right lane */
2020 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
2021 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
2022 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
2023 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
2024 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
2025 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
2026 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
2027 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
2028 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
2029 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
2030 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
2031 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
2032 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
2033 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
2034 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
2035 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
2037 + /* combine results */
2038 + ddd += cc + state[1]; /* final result for state[0] */
2039 + state[1] = state[2] + dd + aaa;
2040 + state[2] = state[3] + aa + bbb;
2041 + state[3] = state[0] + bb + ccc;
2047 +static void rmd128_init(struct crypto_tfm *tfm)
2049 + struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
2051 + rctx->byte_count = 0;
2053 + rctx->state[0] = RMD_H0;
2054 + rctx->state[1] = RMD_H1;
2055 + rctx->state[2] = RMD_H2;
2056 + rctx->state[3] = RMD_H3;
2058 + memset(rctx->buffer, 0, sizeof(rctx->buffer));
2061 +static void rmd128_update(struct crypto_tfm *tfm, const u8 *data,
2064 + struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
2065 + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
2067 + rctx->byte_count += len;
2069 + /* Enough space in buffer? If so copy and we're done */
2070 + if (avail > len) {
2071 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2076 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2079 + rmd128_transform(rctx->state, rctx->buffer);
2083 + while (len >= sizeof(rctx->buffer)) {
2084 + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
2085 + rmd128_transform(rctx->state, rctx->buffer);
2086 + data += sizeof(rctx->buffer);
2087 + len -= sizeof(rctx->buffer);
2090 + memcpy(rctx->buffer, data, len);
2093 +/* Add padding and return the message digest. */
2094 +static void rmd128_final(struct crypto_tfm *tfm, u8 *out)
2096 + struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
2097 + u32 i, index, padlen;
2099 + __le32 *dst = (__le32 *)out;
2100 + static const u8 padding[64] = { 0x80, };
2102 + bits = cpu_to_le64(rctx->byte_count << 3);
2104 + /* Pad out to 56 mod 64 */
2105 + index = rctx->byte_count & 0x3f;
2106 + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
2107 + rmd128_update(tfm, padding, padlen);
2109 + /* Append length */
2110 + rmd128_update(tfm, (const u8 *)&bits, sizeof(bits));
2112 + /* Store state in digest */
2113 + for (i = 0; i < 4; i++)
2114 + dst[i] = cpu_to_le32p(&rctx->state[i]);
2116 + /* Wipe context */
2117 + memset(rctx, 0, sizeof(*rctx));
2120 +static struct crypto_alg alg = {
2121 + .cra_name = "rmd128",
2122 + .cra_driver_name = "rmd128",
2123 + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
2124 + .cra_blocksize = RMD128_BLOCK_SIZE,
2125 + .cra_ctxsize = sizeof(struct rmd128_ctx),
2126 + .cra_module = THIS_MODULE,
2127 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
2128 + .cra_u = { .digest = {
2129 + .dia_digestsize = RMD128_DIGEST_SIZE,
2130 + .dia_init = rmd128_init,
2131 + .dia_update = rmd128_update,
2132 + .dia_final = rmd128_final } }
2135 +static int __init rmd128_mod_init(void)
2137 + return crypto_register_alg(&alg);
2140 +static void __exit rmd128_mod_fini(void)
2142 + crypto_unregister_alg(&alg);
2145 +module_init(rmd128_mod_init);
2146 +module_exit(rmd128_mod_fini);
2148 +MODULE_LICENSE("GPL");
2149 +MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
2151 +MODULE_ALIAS("rmd128");
2153 +++ b/crypto/rmd160.c
2156 + * Cryptographic API.
2158 + * RIPEMD-160 - RACE Integrity Primitives Evaluation Message Digest.
2160 + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
2162 + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
2164 + * This program is free software; you can redistribute it and/or modify it
2165 + * under the terms of the GNU General Public License as published by the Free
2166 + * Software Foundation; either version 2 of the License, or (at your option)
2167 + * any later version.
2170 +#include <linux/init.h>
2171 +#include <linux/module.h>
2172 +#include <linux/mm.h>
2173 +#include <linux/crypto.h>
2174 +#include <linux/cryptohash.h>
2175 +#include <linux/types.h>
2176 +#include <asm/byteorder.h>
2178 +#include "ripemd.h"
2180 +struct rmd160_ctx {
2183 + __le32 buffer[16];
2197 +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
2198 +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
2199 +#define F3(x, y, z) ((x | ~y) ^ z)
2200 +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
2201 +#define F5(x, y, z) (x ^ (y | ~z))
2203 +#define ROUND(a, b, c, d, e, f, k, x, s) { \
2204 + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
2205 + (a) = rol32((a), (s)) + (e); \
2206 + (c) = rol32((c), 10); \
2209 +static void rmd160_transform(u32 *state, const __le32 *in)
2211 + u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
2213 + /* Initialize left lane */
2220 + /* Initialize right lane */
2227 + /* round 1: left lane */
2228 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11);
2229 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14);
2230 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15);
2231 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12);
2232 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5);
2233 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8);
2234 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7);
2235 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9);
2236 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11);
2237 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13);
2238 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
2239 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
2240 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6);
2241 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7);
2242 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9);
2243 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8);
2245 + /* round 2: left lane" */
2246 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
2247 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
2248 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
2249 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
2250 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
2251 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
2252 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
2253 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
2254 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
2255 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
2256 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
2257 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
2258 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
2259 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
2260 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
2261 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
2263 + /* round 3: left lane" */
2264 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
2265 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
2266 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
2267 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
2268 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
2269 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
2270 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
2271 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
2272 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
2273 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
2274 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
2275 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
2276 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
2277 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
2278 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
2279 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
2281 + /* round 4: left lane" */
2282 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
2283 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
2284 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
2285 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
2286 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
2287 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
2288 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
2289 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
2290 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
2291 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
2292 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
2293 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
2294 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
2295 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
2296 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
2297 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
2299 + /* round 5: left lane" */
2300 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
2301 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
2302 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
2303 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
2304 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
2305 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
2306 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
2307 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
2308 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
2309 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
2310 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
2311 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
2312 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
2313 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
2314 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
2315 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
2317 + /* round 1: right lane */
2318 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
2319 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
2320 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
2321 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
2322 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
2323 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
2324 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
2325 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
2326 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
2327 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
2328 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
2329 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
2330 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
2331 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
2332 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
2333 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
2335 + /* round 2: right lane */
2336 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
2337 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
2338 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
2339 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
2340 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
2341 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
2342 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
2343 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
2344 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
2345 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
2346 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
2347 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
2348 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
2349 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
2350 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
2351 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
2353 + /* round 3: right lane */
2354 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
2355 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
2356 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
2357 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
2358 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
2359 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
2360 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
2361 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
2362 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
2363 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
2364 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
2365 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
2366 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
2367 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
2368 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
2369 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
2371 + /* round 4: right lane */
2372 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
2373 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
2374 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
2375 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
2376 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
2377 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
2378 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
2379 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
2380 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
2381 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
2382 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
2383 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
2384 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
2385 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
2386 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
2387 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
2389 + /* round 5: right lane */
2390 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
2391 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
2392 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
2393 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
2394 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
2395 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
2396 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
2397 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
2398 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
2399 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
2400 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
2401 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
2402 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
2403 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
2404 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
2405 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
2407 + /* combine results */
2408 + ddd += cc + state[1]; /* final result for state[0] */
2409 + state[1] = state[2] + dd + eee;
2410 + state[2] = state[3] + ee + aaa;
2411 + state[3] = state[4] + aa + bbb;
2412 + state[4] = state[0] + bb + ccc;
2418 +static void rmd160_init(struct crypto_tfm *tfm)
2420 + struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
2422 + rctx->byte_count = 0;
2424 + rctx->state[0] = RMD_H0;
2425 + rctx->state[1] = RMD_H1;
2426 + rctx->state[2] = RMD_H2;
2427 + rctx->state[3] = RMD_H3;
2428 + rctx->state[4] = RMD_H4;
2430 + memset(rctx->buffer, 0, sizeof(rctx->buffer));
2433 +static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
2436 + struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
2437 + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
2439 + rctx->byte_count += len;
2441 + /* Enough space in buffer? If so copy and we're done */
2442 + if (avail > len) {
2443 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2448 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2451 + rmd160_transform(rctx->state, rctx->buffer);
2455 + while (len >= sizeof(rctx->buffer)) {
2456 + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
2457 + rmd160_transform(rctx->state, rctx->buffer);
2458 + data += sizeof(rctx->buffer);
2459 + len -= sizeof(rctx->buffer);
2462 + memcpy(rctx->buffer, data, len);
2465 +/* Add padding and return the message digest. */
2466 +static void rmd160_final(struct crypto_tfm *tfm, u8 *out)
2468 + struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
2469 + u32 i, index, padlen;
2471 + __le32 *dst = (__le32 *)out;
2472 + static const u8 padding[64] = { 0x80, };
2474 + bits = cpu_to_le64(rctx->byte_count << 3);
2476 + /* Pad out to 56 mod 64 */
2477 + index = rctx->byte_count & 0x3f;
2478 + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
2479 + rmd160_update(tfm, padding, padlen);
2481 + /* Append length */
2482 + rmd160_update(tfm, (const u8 *)&bits, sizeof(bits));
2484 + /* Store state in digest */
2485 + for (i = 0; i < 5; i++)
2486 + dst[i] = cpu_to_le32p(&rctx->state[i]);
2488 + /* Wipe context */
2489 + memset(rctx, 0, sizeof(*rctx));
2492 +static struct crypto_alg alg = {
2493 + .cra_name = "rmd160",
2494 + .cra_driver_name = "rmd160",
2495 + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
2496 + .cra_blocksize = RMD160_BLOCK_SIZE,
2497 + .cra_ctxsize = sizeof(struct rmd160_ctx),
2498 + .cra_module = THIS_MODULE,
2499 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
2500 + .cra_u = { .digest = {
2501 + .dia_digestsize = RMD160_DIGEST_SIZE,
2502 + .dia_init = rmd160_init,
2503 + .dia_update = rmd160_update,
2504 + .dia_final = rmd160_final } }
2507 +static int __init rmd160_mod_init(void)
2509 + return crypto_register_alg(&alg);
2512 +static void __exit rmd160_mod_fini(void)
2514 + crypto_unregister_alg(&alg);
2517 +module_init(rmd160_mod_init);
2518 +module_exit(rmd160_mod_fini);
2520 +MODULE_LICENSE("GPL");
2521 +MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
2523 +MODULE_ALIAS("rmd160");
2525 +++ b/crypto/rmd256.c
2528 + * Cryptographic API.
2530 + * RIPEMD-256 - RACE Integrity Primitives Evaluation Message Digest.
2532 + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
2534 + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
2536 + * This program is free software; you can redistribute it and/or modify it
2537 + * under the terms of the GNU General Public License as published by the Free
2538 + * Software Foundation; either version 2 of the License, or (at your option)
2539 + * any later version.
2542 +#include <linux/init.h>
2543 +#include <linux/module.h>
2544 +#include <linux/mm.h>
2545 +#include <linux/crypto.h>
2546 +#include <linux/cryptohash.h>
2547 +#include <linux/types.h>
2548 +#include <asm/byteorder.h>
2550 +#include "ripemd.h"
2552 +struct rmd256_ctx {
2555 + __le32 buffer[16];
2567 +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
2568 +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
2569 +#define F3(x, y, z) ((x | ~y) ^ z)
2570 +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
2572 +#define ROUND(a, b, c, d, f, k, x, s) { \
2573 + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
2574 + (a) = rol32((a), (s)); \
2577 +static void rmd256_transform(u32 *state, const __le32 *in)
2579 + u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd, tmp;
2581 + /* Initialize left lane */
2587 + /* Initialize right lane */
2593 + /* round 1: left lane */
2594 + ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
2595 + ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
2596 + ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
2597 + ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
2598 + ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
2599 + ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
2600 + ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
2601 + ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
2602 + ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
2603 + ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
2604 + ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
2605 + ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
2606 + ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
2607 + ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
2608 + ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
2609 + ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
2611 + /* round 1: right lane */
2612 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
2613 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
2614 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
2615 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
2616 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
2617 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
2618 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
2619 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
2620 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
2621 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
2622 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
2623 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
2624 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
2625 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
2626 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
2627 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
2629 + /* Swap contents of "a" registers */
2630 + tmp = aa; aa = aaa; aaa = tmp;
2632 + /* round 2: left lane */
2633 + ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
2634 + ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
2635 + ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
2636 + ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
2637 + ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
2638 + ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
2639 + ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
2640 + ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
2641 + ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
2642 + ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
2643 + ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
2644 + ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
2645 + ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
2646 + ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
2647 + ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
2648 + ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
2650 + /* round 2: right lane */
2651 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
2652 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
2653 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
2654 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
2655 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
2656 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
2657 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
2658 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
2659 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
2660 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
2661 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
2662 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
2663 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
2664 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
2665 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
2666 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
2668 + /* Swap contents of "b" registers */
2669 + tmp = bb; bb = bbb; bbb = tmp;
2671 + /* round 3: left lane */
2672 + ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
2673 + ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
2674 + ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
2675 + ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
2676 + ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
2677 + ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
2678 + ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
2679 + ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
2680 + ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
2681 + ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
2682 + ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
2683 + ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
2684 + ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
2685 + ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
2686 + ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
2687 + ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
2689 + /* round 3: right lane */
2690 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
2691 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
2692 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
2693 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
2694 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
2695 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
2696 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
2697 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
2698 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
2699 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
2700 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
2701 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
2702 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
2703 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
2704 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
2705 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
2707 + /* Swap contents of "c" registers */
2708 + tmp = cc; cc = ccc; ccc = tmp;
2710 + /* round 4: left lane */
2711 + ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
2712 + ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
2713 + ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
2714 + ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
2715 + ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
2716 + ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
2717 + ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
2718 + ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
2719 + ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
2720 + ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
2721 + ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
2722 + ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
2723 + ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
2724 + ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
2725 + ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
2726 + ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
2728 + /* round 4: right lane */
2729 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
2730 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
2731 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
2732 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
2733 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
2734 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
2735 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
2736 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
2737 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
2738 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
2739 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
2740 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
2741 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
2742 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
2743 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
2744 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
2746 + /* Swap contents of "d" registers */
2747 + tmp = dd; dd = ddd; ddd = tmp;
2749 + /* combine results */
2762 +static void rmd256_init(struct crypto_tfm *tfm)
2764 + struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
2766 + rctx->byte_count = 0;
2768 + rctx->state[0] = RMD_H0;
2769 + rctx->state[1] = RMD_H1;
2770 + rctx->state[2] = RMD_H2;
2771 + rctx->state[3] = RMD_H3;
2772 + rctx->state[4] = RMD_H5;
2773 + rctx->state[5] = RMD_H6;
2774 + rctx->state[6] = RMD_H7;
2775 + rctx->state[7] = RMD_H8;
2777 + memset(rctx->buffer, 0, sizeof(rctx->buffer));
2780 +static void rmd256_update(struct crypto_tfm *tfm, const u8 *data,
2783 + struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
2784 + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
2786 + rctx->byte_count += len;
2788 + /* Enough space in buffer? If so copy and we're done */
2789 + if (avail > len) {
2790 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2795 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2798 + rmd256_transform(rctx->state, rctx->buffer);
2802 + while (len >= sizeof(rctx->buffer)) {
2803 + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
2804 + rmd256_transform(rctx->state, rctx->buffer);
2805 + data += sizeof(rctx->buffer);
2806 + len -= sizeof(rctx->buffer);
2809 + memcpy(rctx->buffer, data, len);
2812 +/* Add padding and return the message digest. */
2813 +static void rmd256_final(struct crypto_tfm *tfm, u8 *out)
2815 + struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
2816 + u32 i, index, padlen;
2818 + __le32 *dst = (__le32 *)out;
2819 + static const u8 padding[64] = { 0x80, };
2821 + bits = cpu_to_le64(rctx->byte_count << 3);
2823 + /* Pad out to 56 mod 64 */
2824 + index = rctx->byte_count & 0x3f;
2825 + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
2826 + rmd256_update(tfm, padding, padlen);
2828 + /* Append length */
2829 + rmd256_update(tfm, (const u8 *)&bits, sizeof(bits));
2831 + /* Store state in digest */
2832 + for (i = 0; i < 8; i++)
2833 + dst[i] = cpu_to_le32p(&rctx->state[i]);
2835 + /* Wipe context */
2836 + memset(rctx, 0, sizeof(*rctx));
2839 +static struct crypto_alg alg = {
2840 + .cra_name = "rmd256",
2841 + .cra_driver_name = "rmd256",
2842 + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
2843 + .cra_blocksize = RMD256_BLOCK_SIZE,
2844 + .cra_ctxsize = sizeof(struct rmd256_ctx),
2845 + .cra_module = THIS_MODULE,
2846 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
2847 + .cra_u = { .digest = {
2848 + .dia_digestsize = RMD256_DIGEST_SIZE,
2849 + .dia_init = rmd256_init,
2850 + .dia_update = rmd256_update,
2851 + .dia_final = rmd256_final } }
2854 +static int __init rmd256_mod_init(void)
2856 + return crypto_register_alg(&alg);
2859 +static void __exit rmd256_mod_fini(void)
2861 + crypto_unregister_alg(&alg);
2864 +module_init(rmd256_mod_init);
2865 +module_exit(rmd256_mod_fini);
2867 +MODULE_LICENSE("GPL");
2868 +MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
2870 +MODULE_ALIAS("rmd256");
2872 +++ b/crypto/rmd320.c
2875 + * Cryptographic API.
2877 + * RIPEMD-320 - RACE Integrity Primitives Evaluation Message Digest.
2879 + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
2881 + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
2883 + * This program is free software; you can redistribute it and/or modify it
2884 + * under the terms of the GNU General Public License as published by the Free
2885 + * Software Foundation; either version 2 of the License, or (at your option)
2886 + * any later version.
2889 +#include <linux/init.h>
2890 +#include <linux/module.h>
2891 +#include <linux/mm.h>
2892 +#include <linux/crypto.h>
2893 +#include <linux/cryptohash.h>
2894 +#include <linux/types.h>
2895 +#include <asm/byteorder.h>
2897 +#include "ripemd.h"
2899 +struct rmd320_ctx {
2902 + __le32 buffer[16];
2916 +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
2917 +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
2918 +#define F3(x, y, z) ((x | ~y) ^ z)
2919 +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
2920 +#define F5(x, y, z) (x ^ (y | ~z))
2922 +#define ROUND(a, b, c, d, e, f, k, x, s) { \
2923 + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
2924 + (a) = rol32((a), (s)) + (e); \
2925 + (c) = rol32((c), 10); \
2928 +static void rmd320_transform(u32 *state, const __le32 *in)
2930 + u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee, tmp;
2932 + /* Initialize left lane */
2939 + /* Initialize right lane */
2946 + /* round 1: left lane */
2947 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11);
2948 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14);
2949 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15);
2950 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12);
2951 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5);
2952 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8);
2953 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7);
2954 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9);
2955 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11);
2956 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13);
2957 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
2958 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
2959 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6);
2960 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7);
2961 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9);
2962 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8);
2964 + /* round 1: right lane */
2965 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
2966 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
2967 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
2968 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
2969 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
2970 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
2971 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
2972 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
2973 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
2974 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
2975 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
2976 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
2977 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
2978 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
2979 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
2980 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
2982 + /* Swap contents of "a" registers */
2983 + tmp = aa; aa = aaa; aaa = tmp;
2985 + /* round 2: left lane" */
2986 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
2987 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
2988 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
2989 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
2990 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
2991 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
2992 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
2993 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
2994 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
2995 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
2996 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
2997 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
2998 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
2999 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
3000 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
3001 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
3003 + /* round 2: right lane */
3004 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
3005 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
3006 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
3007 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
3008 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
3009 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
3010 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
3011 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
3012 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
3013 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
3014 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
3015 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
3016 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
3017 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
3018 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
3019 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
3021 + /* Swap contents of "b" registers */
3022 + tmp = bb; bb = bbb; bbb = tmp;
3024 + /* round 3: left lane" */
3025 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
3026 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
3027 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
3028 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
3029 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
3030 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
3031 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
3032 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
3033 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
3034 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
3035 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
3036 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
3037 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
3038 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
3039 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
3040 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
3042 + /* round 3: right lane */
3043 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
3044 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
3045 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
3046 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
3047 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
3048 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
3049 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
3050 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
3051 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
3052 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
3053 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
3054 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
3055 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
3056 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
3057 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
3058 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
3060 + /* Swap contents of "c" registers */
3061 + tmp = cc; cc = ccc; ccc = tmp;
3063 + /* round 4: left lane" */
3064 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
3065 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
3066 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
3067 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
3068 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
3069 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
3070 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
3071 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
3072 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
3073 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
3074 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
3075 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
3076 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
3077 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
3078 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
3079 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
3081 + /* round 4: right lane */
3082 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
3083 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
3084 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
3085 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
3086 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
3087 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
3088 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
3089 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
3090 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
3091 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
3092 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
3093 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
3094 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
3095 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
3096 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
3097 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
3099 + /* Swap contents of "d" registers */
3100 + tmp = dd; dd = ddd; ddd = tmp;
3102 + /* round 5: left lane" */
3103 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
3104 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
3105 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
3106 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
3107 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
3108 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
3109 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
3110 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
3111 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
3112 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
3113 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
3114 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
3115 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
3116 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
3117 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
3118 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
3120 + /* round 5: right lane */
3121 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
3122 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
3123 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
3124 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
3125 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
3126 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
3127 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
3128 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
3129 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
3130 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
3131 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
3132 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
3133 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
3134 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
3135 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
3136 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
3138 + /* Swap contents of "e" registers */
3139 + tmp = ee; ee = eee; eee = tmp;
3141 + /* combine results */
3156 +static void rmd320_init(struct crypto_tfm *tfm)
3158 + struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
3160 + rctx->byte_count = 0;
3162 + rctx->state[0] = RMD_H0;
3163 + rctx->state[1] = RMD_H1;
3164 + rctx->state[2] = RMD_H2;
3165 + rctx->state[3] = RMD_H3;
3166 + rctx->state[4] = RMD_H4;
3167 + rctx->state[5] = RMD_H5;
3168 + rctx->state[6] = RMD_H6;
3169 + rctx->state[7] = RMD_H7;
3170 + rctx->state[8] = RMD_H8;
3171 + rctx->state[9] = RMD_H9;
3173 + memset(rctx->buffer, 0, sizeof(rctx->buffer));
3176 +static void rmd320_update(struct crypto_tfm *tfm, const u8 *data,
3179 + struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
3180 + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
3182 + rctx->byte_count += len;
3184 + /* Enough space in buffer? If so copy and we're done */
3185 + if (avail > len) {
3186 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
3191 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
3194 + rmd320_transform(rctx->state, rctx->buffer);
3198 + while (len >= sizeof(rctx->buffer)) {
3199 + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
3200 + rmd320_transform(rctx->state, rctx->buffer);
3201 + data += sizeof(rctx->buffer);
3202 + len -= sizeof(rctx->buffer);
3205 + memcpy(rctx->buffer, data, len);
3208 +/* Add padding and return the message digest. */
3209 +static void rmd320_final(struct crypto_tfm *tfm, u8 *out)
3211 + struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
3212 + u32 i, index, padlen;
3214 + __le32 *dst = (__le32 *)out;
3215 + static const u8 padding[64] = { 0x80, };
3217 + bits = cpu_to_le64(rctx->byte_count << 3);
3219 + /* Pad out to 56 mod 64 */
3220 + index = rctx->byte_count & 0x3f;
3221 + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
3222 + rmd320_update(tfm, padding, padlen);
3224 + /* Append length */
3225 + rmd320_update(tfm, (const u8 *)&bits, sizeof(bits));
3227 + /* Store state in digest */
3228 + for (i = 0; i < 10; i++)
3229 + dst[i] = cpu_to_le32p(&rctx->state[i]);
3231 + /* Wipe context */
3232 + memset(rctx, 0, sizeof(*rctx));
3235 +static struct crypto_alg alg = {
3236 + .cra_name = "rmd320",
3237 + .cra_driver_name = "rmd320",
3238 + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
3239 + .cra_blocksize = RMD320_BLOCK_SIZE,
3240 + .cra_ctxsize = sizeof(struct rmd320_ctx),
3241 + .cra_module = THIS_MODULE,
3242 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
3243 + .cra_u = { .digest = {
3244 + .dia_digestsize = RMD320_DIGEST_SIZE,
3245 + .dia_init = rmd320_init,
3246 + .dia_update = rmd320_update,
3247 + .dia_final = rmd320_final } }
3250 +static int __init rmd320_mod_init(void)
3252 + return crypto_register_alg(&alg);
3255 +static void __exit rmd320_mod_fini(void)
3257 + crypto_unregister_alg(&alg);
3260 +module_init(rmd320_mod_init);
3261 +module_exit(rmd320_mod_fini);
3263 +MODULE_LICENSE("GPL");
3264 +MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
3266 +MODULE_ALIAS("rmd320");
3267 --- a/crypto/tcrypt.c
3268 +++ b/crypto/tcrypt.c
3270 * Software Foundation; either version 2 of the License, or (at your option)
3271 * any later version.
3273 - * 2007-11-13 Added GCM tests
3274 - * 2007-11-13 Added AEAD support
3275 - * 2007-11-06 Added SHA-224 and SHA-224-HMAC tests
3276 - * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
3277 - * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
3278 - * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
3282 +#include <crypto/hash.h>
3283 #include <linux/err.h>
3284 #include <linux/init.h>
3285 #include <linux/module.h>
3287 #include <linux/scatterlist.h>
3288 #include <linux/string.h>
3289 #include <linux/crypto.h>
3290 -#include <linux/highmem.h>
3291 #include <linux/moduleparam.h>
3292 #include <linux/jiffies.h>
3293 #include <linux/timex.h>
3298 - * Need to kmalloc() memory for testing kmap().
3299 + * Need to kmalloc() memory for testing.
3301 #define TVMEMSIZE 16384
3302 #define XBUFSIZE 32768
3305 * Indexes into the xbuf to simulate cross-page access.
3313 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
3314 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
3315 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
3316 - "camellia", "seed", "salsa20", "lzo", "cts", NULL
3317 + "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
3318 + "lzo", "cts", NULL
3321 static void hexdump(unsigned char *buf, unsigned int len)
3322 @@ -110,22 +104,30 @@
3323 unsigned int i, j, k, temp;
3324 struct scatterlist sg[8];
3326 - struct crypto_hash *tfm;
3327 - struct hash_desc desc;
3328 + struct crypto_ahash *tfm;
3329 + struct ahash_request *req;
3330 + struct tcrypt_result tresult;
3334 printk("\ntesting %s\n", algo);
3336 - tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
3337 + init_completion(&tresult.completion);
3339 + tfm = crypto_alloc_ahash(algo, 0, 0);
3341 printk("failed to load transform for %s: %ld\n", algo,
3348 + req = ahash_request_alloc(tfm, GFP_KERNEL);
3350 + printk(KERN_ERR "failed to allocate request for %s\n", algo);
3353 + ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3354 + tcrypt_complete, &tresult);
3356 for (i = 0; i < tcount; i++) {
3357 printk("test %u:\n", i + 1);
3359 sg_init_one(&sg[0], hash_buff, template[i].psize);
3361 if (template[i].ksize) {
3362 - ret = crypto_hash_setkey(tfm, template[i].key,
3363 - template[i].ksize);
3364 + crypto_ahash_clear_flags(tfm, ~0);
3365 + ret = crypto_ahash_setkey(tfm, template[i].key,
3366 + template[i].ksize);
3368 printk("setkey() failed ret=%d\n", ret);
3370 @@ -148,17 +151,30 @@
3374 - ret = crypto_hash_digest(&desc, sg, template[i].psize, result);
3376 + ahash_request_set_crypt(req, sg, result, template[i].psize);
3377 + ret = crypto_ahash_digest(req);
3381 + case -EINPROGRESS:
3383 + ret = wait_for_completion_interruptible(
3384 + &tresult.completion);
3385 + if (!ret && !(ret = tresult.err)) {
3386 + INIT_COMPLETION(tresult.completion);
3389 + /* fall through */
3391 printk("digest () failed ret=%d\n", ret);
3396 - hexdump(result, crypto_hash_digestsize(tfm));
3397 + hexdump(result, crypto_ahash_digestsize(tfm));
3399 memcmp(result, template[i].digest,
3400 - crypto_hash_digestsize(tfm)) ?
3401 + crypto_ahash_digestsize(tfm)) ?
3408 if (template[i].ksize) {
3409 - ret = crypto_hash_setkey(tfm, template[i].key,
3410 - template[i].ksize);
3411 + crypto_ahash_clear_flags(tfm, ~0);
3412 + ret = crypto_ahash_setkey(tfm, template[i].key,
3413 + template[i].ksize);
3416 printk("setkey() failed ret=%d\n", ret);
3417 @@ -196,29 +213,44 @@
3421 - ret = crypto_hash_digest(&desc, sg, template[i].psize,
3424 + ahash_request_set_crypt(req, sg, result,
3425 + template[i].psize);
3426 + ret = crypto_ahash_digest(req);
3430 + case -EINPROGRESS:
3432 + ret = wait_for_completion_interruptible(
3433 + &tresult.completion);
3434 + if (!ret && !(ret = tresult.err)) {
3435 + INIT_COMPLETION(tresult.completion);
3438 + /* fall through */
3440 printk("digest () failed ret=%d\n", ret);
3444 - hexdump(result, crypto_hash_digestsize(tfm));
3445 + hexdump(result, crypto_ahash_digestsize(tfm));
3447 memcmp(result, template[i].digest,
3448 - crypto_hash_digestsize(tfm)) ?
3449 + crypto_ahash_digestsize(tfm)) ?
3455 - crypto_free_hash(tfm);
3456 + ahash_request_free(req);
3458 + crypto_free_ahash(tfm);
3461 static void test_aead(char *algo, int enc, struct aead_testvec *template,
3462 unsigned int tcount)
3464 - unsigned int ret, i, j, k, temp;
3465 + unsigned int ret, i, j, k, n, temp;
3467 struct crypto_aead *tfm;
3469 @@ -344,13 +376,12 @@
3473 - q = kmap(sg_page(&sg[0])) + sg[0].offset;
3475 hexdump(q, template[i].rlen);
3477 printk(KERN_INFO "enc/dec: %s\n",
3478 memcmp(q, template[i].result,
3479 template[i].rlen) ? "fail" : "pass");
3480 - kunmap(sg_page(&sg[0]));
3482 if (!template[i].key)
3487 printk(KERN_INFO "\ntesting %s %s across pages (chunking)\n", algo, e);
3488 - memset(xbuf, 0, XBUFSIZE);
3489 memset(axbuf, 0, XBUFSIZE);
3491 for (i = 0, j = 0; i < tcount; i++) {
3496 + memset(xbuf, 0, XBUFSIZE);
3497 sg_init_table(sg, template[i].np);
3498 for (k = 0, temp = 0; k < template[i].np; k++) {
3499 memcpy(&xbuf[IDX[k]],
3502 for (k = 0, temp = 0; k < template[i].np; k++) {
3503 printk(KERN_INFO "page %u\n", k);
3504 - q = kmap(sg_page(&sg[k])) + sg[k].offset;
3505 + q = &axbuf[IDX[k]];
3506 hexdump(q, template[i].tap[k]);
3507 printk(KERN_INFO "%s\n",
3508 memcmp(q, template[i].result + temp,
3509 @@ -459,8 +490,15 @@
3513 + for (n = 0; q[template[i].tap[k] + n]; n++)
3516 + printk("Result buffer corruption %u "
3518 + hexdump(&q[template[i].tap[k]], n);
3521 temp += template[i].tap[k];
3522 - kunmap(sg_page(&sg[k]));
3527 static void test_cipher(char *algo, int enc,
3528 struct cipher_testvec *template, unsigned int tcount)
3530 - unsigned int ret, i, j, k, temp;
3531 + unsigned int ret, i, j, k, n, temp;
3533 struct crypto_ablkcipher *tfm;
3534 struct ablkcipher_request *req;
3535 @@ -569,19 +607,17 @@
3539 - q = kmap(sg_page(&sg[0])) + sg[0].offset;
3541 hexdump(q, template[i].rlen);
3544 memcmp(q, template[i].result,
3545 template[i].rlen) ? "fail" : "pass");
3546 - kunmap(sg_page(&sg[0]));
3551 printk("\ntesting %s %s across pages (chunking)\n", algo, e);
3552 - memset(xbuf, 0, XBUFSIZE);
3555 for (i = 0; i < tcount; i++) {
3557 printk("test %u (%d bit key):\n",
3558 j, template[i].klen * 8);
3560 + memset(xbuf, 0, XBUFSIZE);
3561 crypto_ablkcipher_clear_flags(tfm, ~0);
3563 crypto_ablkcipher_set_flags(
3564 @@ -649,14 +686,21 @@
3566 for (k = 0; k < template[i].np; k++) {
3567 printk("page %u\n", k);
3568 - q = kmap(sg_page(&sg[k])) + sg[k].offset;
3569 + q = &xbuf[IDX[k]];
3570 hexdump(q, template[i].tap[k]);
3572 memcmp(q, template[i].result + temp,
3573 template[i].tap[k]) ? "fail" :
3576 + for (n = 0; q[template[i].tap[k] + n]; n++)
3579 + printk("Result buffer corruption %u "
3581 + hexdump(&q[template[i].tap[k]], n);
3583 temp += template[i].tap[k];
3584 - kunmap(sg_page(&sg[k]));
3588 @@ -1172,6 +1216,14 @@
3589 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
3590 DES3_EDE_DEC_TEST_VECTORS);
3592 + test_cipher("cbc(des3_ede)", ENCRYPT,
3593 + des3_ede_cbc_enc_tv_template,
3594 + DES3_EDE_CBC_ENC_TEST_VECTORS);
3596 + test_cipher("cbc(des3_ede)", DECRYPT,
3597 + des3_ede_cbc_dec_tv_template,
3598 + DES3_EDE_CBC_DEC_TEST_VECTORS);
3600 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
3602 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
3603 @@ -1382,6 +1434,14 @@
3604 DES3_EDE_ENC_TEST_VECTORS);
3605 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
3606 DES3_EDE_DEC_TEST_VECTORS);
3608 + test_cipher("cbc(des3_ede)", ENCRYPT,
3609 + des3_ede_cbc_enc_tv_template,
3610 + DES3_EDE_CBC_ENC_TEST_VECTORS);
3612 + test_cipher("cbc(des3_ede)", DECRYPT,
3613 + des3_ede_cbc_dec_tv_template,
3614 + DES3_EDE_CBC_DEC_TEST_VECTORS);
3618 @@ -1550,7 +1610,7 @@
3620 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
3625 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
3626 XETA_ENC_TEST_VECTORS);
3627 @@ -1615,6 +1675,22 @@
3628 CTS_MODE_DEC_TEST_VECTORS);
3632 + test_hash("rmd128", rmd128_tv_template, RMD128_TEST_VECTORS);
3636 + test_hash("rmd160", rmd160_tv_template, RMD160_TEST_VECTORS);
3640 + test_hash("rmd256", rmd256_tv_template, RMD256_TEST_VECTORS);
3644 + test_hash("rmd320", rmd320_tv_template, RMD320_TEST_VECTORS);
3648 test_hash("hmac(md5)", hmac_md5_tv_template,
3649 HMAC_MD5_TEST_VECTORS);
3650 @@ -1650,6 +1726,16 @@
3651 XCBC_AES_TEST_VECTORS);
3655 + test_hash("hmac(rmd128)", hmac_rmd128_tv_template,
3656 + HMAC_RMD128_TEST_VECTORS);
3660 + test_hash("hmac(rmd160)", hmac_rmd160_tv_template,
3661 + HMAC_RMD160_TEST_VECTORS);
3665 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
3666 speed_template_16_24_32);
3667 @@ -1788,6 +1874,22 @@
3668 test_hash_speed("sha224", sec, generic_hash_speed_template);
3669 if (mode > 300 && mode < 400) break;
3672 + test_hash_speed("rmd128", sec, generic_hash_speed_template);
3673 + if (mode > 300 && mode < 400) break;
3676 + test_hash_speed("rmd160", sec, generic_hash_speed_template);
3677 + if (mode > 300 && mode < 400) break;
3680 + test_hash_speed("rmd256", sec, generic_hash_speed_template);
3681 + if (mode > 300 && mode < 400) break;
3684 + test_hash_speed("rmd320", sec, generic_hash_speed_template);
3685 + if (mode > 300 && mode < 400) break;
3690 --- a/crypto/tcrypt.h
3691 +++ b/crypto/tcrypt.h
3693 * Software Foundation; either version 2 of the License, or (at your option)
3694 * any later version.
3696 - * 2007-11-13 Added GCM tests
3697 - * 2007-11-13 Added AEAD support
3698 - * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
3699 - * 2004-08-09 Cipher speed tests by Reyk Floeter <reyk@vantronix.net>
3700 - * 2003-09-14 Changes by Kartikey Mahendra Bhatt
3703 #ifndef _CRYPTO_TCRYPT_H
3704 #define _CRYPTO_TCRYPT_H
3705 @@ -168,6 +162,271 @@
3706 .digest = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55"
3707 "\xac\x49\xda\x2e\x21\x07\xb6\x7a",
3713 + * RIPEMD-128 test vectors from ISO/IEC 10118-3:2004(E)
3715 +#define RMD128_TEST_VECTORS 10
3717 +static struct hash_testvec rmd128_tv_template[] = {
3719 + .digest = "\xcd\xf2\x62\x13\xa1\x50\xdc\x3e"
3720 + "\xcb\x61\x0f\x18\xf6\xb3\x8b\x46",
3724 + .digest = "\x86\xbe\x7a\xfa\x33\x9d\x0f\xc7"
3725 + "\xcf\xc7\x85\xe7\x2f\x57\x8d\x33",
3727 + .plaintext = "abc",
3729 + .digest = "\xc1\x4a\x12\x19\x9c\x66\xe4\xba"
3730 + "\x84\x63\x6b\x0f\x69\x14\x4c\x77",
3732 + .plaintext = "message digest",
3734 + .digest = "\x9e\x32\x7b\x3d\x6e\x52\x30\x62"
3735 + "\xaf\xc1\x13\x2d\x7d\xf9\xd1\xb8",
3737 + .plaintext = "abcdefghijklmnopqrstuvwxyz",
3739 + .digest = "\xfd\x2a\xa6\x07\xf7\x1d\xc8\xf5"
3740 + "\x10\x71\x49\x22\xb3\x71\x83\x4e",
3742 + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
3743 + "fghijklmnopqrstuvwxyz0123456789",
3745 + .digest = "\xd1\xe9\x59\xeb\x17\x9c\x91\x1f"
3746 + "\xae\xa4\x62\x4c\x60\xc5\xc7\x02",
3748 + .plaintext = "1234567890123456789012345678901234567890"
3749 + "1234567890123456789012345678901234567890",
3751 + .digest = "\x3f\x45\xef\x19\x47\x32\xc2\xdb"
3752 + "\xb2\xc4\xa2\xc7\x69\x79\x5f\xa3",
3754 + .plaintext = "abcdbcdecdefdefgefghfghighij"
3755 + "hijkijkljklmklmnlmnomnopnopq",
3757 + .digest = "\xa1\xaa\x06\x89\xd0\xfa\xfa\x2d"
3758 + "\xdc\x22\xe8\x8b\x49\x13\x3a\x06",
3760 + .tap = { 28, 28 },
3762 + .plaintext = "abcdefghbcdefghicdefghijdefghijkefghijklfghi"
3763 + "jklmghijklmnhijklmnoijklmnopjklmnopqklmnopqr"
3764 + "lmnopqrsmnopqrstnopqrstu",
3766 + .digest = "\xd4\xec\xc9\x13\xe1\xdf\x77\x6b"
3767 + "\xf4\x8d\xe9\xd5\x5b\x1f\x25\x46",
3769 + .plaintext = "abcdbcdecdefdefgefghfghighijhijk",
3771 + .digest = "\x13\xfc\x13\xe8\xef\xff\x34\x7d"
3772 + "\xe1\x93\xff\x46\xdb\xac\xcf\xd4",
3777 + * RIPEMD-160 test vectors from ISO/IEC 10118-3:2004(E)
3779 +#define RMD160_TEST_VECTORS 10
3781 +static struct hash_testvec rmd160_tv_template[] = {
3783 + .digest = "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28"
3784 + "\x08\x97\x7e\xe8\xf5\x48\xb2\x25\x8d\x31",
3788 + .digest = "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae"
3789 + "\x34\x7b\xe6\xf4\xdc\x83\x5a\x46\x7f\xfe",
3791 + .plaintext = "abc",
3793 + .digest = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04"
3794 + "\x4a\x8e\x98\xc6\xb0\x87\xf1\x5a\x0b\xfc",
3796 + .plaintext = "message digest",
3798 + .digest = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8"
3799 + "\x81\xb1\x23\xa8\x5f\xfa\x21\x59\x5f\x36",
3801 + .plaintext = "abcdefghijklmnopqrstuvwxyz",
3803 + .digest = "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb"
3804 + "\xdc\xeb\x5b\x9d\x28\x65\xb3\x70\x8d\xbc",
3806 + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
3807 + "fghijklmnopqrstuvwxyz0123456789",
3809 + .digest = "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed"
3810 + "\x3a\x87\xa5\x71\x30\x79\xb2\x1f\x51\x89",
3812 + .plaintext = "1234567890123456789012345678901234567890"
3813 + "1234567890123456789012345678901234567890",
3815 + .digest = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb"
3816 + "\xd3\x32\x3c\xab\x82\xbf\x63\x32\x6b\xfb",
3818 + .plaintext = "abcdbcdecdefdefgefghfghighij"
3819 + "hijkijkljklmklmnlmnomnopnopq",
3821 + .digest = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05"
3822 + "\xa0\x6c\x27\xdc\xf4\x9a\xda\x62\xeb\x2b",
3824 + .tap = { 28, 28 },
3826 + .plaintext = "abcdefghbcdefghicdefghijdefghijkefghijklfghi"
3827 + "jklmghijklmnhijklmnoijklmnopjklmnopqklmnopqr"
3828 + "lmnopqrsmnopqrstnopqrstu",
3830 + .digest = "\x6f\x3f\xa3\x9b\x6b\x50\x3c\x38\x4f\x91"
3831 + "\x9a\x49\xa7\xaa\x5c\x2c\x08\xbd\xfb\x45",
3833 + .plaintext = "abcdbcdecdefdefgefghfghighijhijk",
3835 + .digest = "\x94\xc2\x64\x11\x54\x04\xe6\x33\x79\x0d"
3836 + "\xfc\xc8\x7b\x58\x7d\x36\x77\x06\x7d\x9f",
3841 + * RIPEMD-256 test vectors
3843 +#define RMD256_TEST_VECTORS 8
3845 +static struct hash_testvec rmd256_tv_template[] = {
3847 + .digest = "\x02\xba\x4c\x4e\x5f\x8e\xcd\x18"
3848 + "\x77\xfc\x52\xd6\x4d\x30\xe3\x7a"
3849 + "\x2d\x97\x74\xfb\x1e\x5d\x02\x63"
3850 + "\x80\xae\x01\x68\xe3\xc5\x52\x2d",
3854 + .digest = "\xf9\x33\x3e\x45\xd8\x57\xf5\xd9"
3855 + "\x0a\x91\xba\xb7\x0a\x1e\xba\x0c"
3856 + "\xfb\x1b\xe4\xb0\x78\x3c\x9a\xcf"
3857 + "\xcd\x88\x3a\x91\x34\x69\x29\x25",
3859 + .plaintext = "abc",
3861 + .digest = "\xaf\xbd\x6e\x22\x8b\x9d\x8c\xbb"
3862 + "\xce\xf5\xca\x2d\x03\xe6\xdb\xa1"
3863 + "\x0a\xc0\xbc\x7d\xcb\xe4\x68\x0e"
3864 + "\x1e\x42\xd2\xe9\x75\x45\x9b\x65",
3866 + .plaintext = "message digest",
3868 + .digest = "\x87\xe9\x71\x75\x9a\x1c\xe4\x7a"
3869 + "\x51\x4d\x5c\x91\x4c\x39\x2c\x90"
3870 + "\x18\xc7\xc4\x6b\xc1\x44\x65\x55"
3871 + "\x4a\xfc\xdf\x54\xa5\x07\x0c\x0e",
3873 + .plaintext = "abcdefghijklmnopqrstuvwxyz",
3875 + .digest = "\x64\x9d\x30\x34\x75\x1e\xa2\x16"
3876 + "\x77\x6b\xf9\xa1\x8a\xcc\x81\xbc"
3877 + "\x78\x96\x11\x8a\x51\x97\x96\x87"
3878 + "\x82\xdd\x1f\xd9\x7d\x8d\x51\x33",
3880 + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
3881 + "fghijklmnopqrstuvwxyz0123456789",
3883 + .digest = "\x57\x40\xa4\x08\xac\x16\xb7\x20"
3884 + "\xb8\x44\x24\xae\x93\x1c\xbb\x1f"
3885 + "\xe3\x63\xd1\xd0\xbf\x40\x17\xf1"
3886 + "\xa8\x9f\x7e\xa6\xde\x77\xa0\xb8",
3888 + .plaintext = "1234567890123456789012345678901234567890"
3889 + "1234567890123456789012345678901234567890",
3891 + .digest = "\x06\xfd\xcc\x7a\x40\x95\x48\xaa"
3892 + "\xf9\x13\x68\xc0\x6a\x62\x75\xb5"
3893 + "\x53\xe3\xf0\x99\xbf\x0e\xa4\xed"
3894 + "\xfd\x67\x78\xdf\x89\xa8\x90\xdd",
3896 + .plaintext = "abcdbcdecdefdefgefghfghighij"
3897 + "hijkijkljklmklmnlmnomnopnopq",
3899 + .digest = "\x38\x43\x04\x55\x83\xaa\xc6\xc8"
3900 + "\xc8\xd9\x12\x85\x73\xe7\xa9\x80"
3901 + "\x9a\xfb\x2a\x0f\x34\xcc\xc3\x6e"
3902 + "\xa9\xe7\x2f\x16\xf6\x36\x8e\x3f",
3904 + .tap = { 28, 28 },
3909 + * RIPEMD-320 test vectors
3911 +#define RMD320_TEST_VECTORS 8
3913 +static struct hash_testvec rmd320_tv_template[] = {
3915 + .digest = "\x22\xd6\x5d\x56\x61\x53\x6c\xdc\x75\xc1"
3916 + "\xfd\xf5\xc6\xde\x7b\x41\xb9\xf2\x73\x25"
3917 + "\xeb\xc6\x1e\x85\x57\x17\x7d\x70\x5a\x0e"
3918 + "\xc8\x80\x15\x1c\x3a\x32\xa0\x08\x99\xb8",
3922 + .digest = "\xce\x78\x85\x06\x38\xf9\x26\x58\xa5\xa5"
3923 + "\x85\x09\x75\x79\x92\x6d\xda\x66\x7a\x57"
3924 + "\x16\x56\x2c\xfc\xf6\xfb\xe7\x7f\x63\x54"
3925 + "\x2f\x99\xb0\x47\x05\xd6\x97\x0d\xff\x5d",
3927 + .plaintext = "abc",
3929 + .digest = "\xde\x4c\x01\xb3\x05\x4f\x89\x30\xa7\x9d"
3930 + "\x09\xae\x73\x8e\x92\x30\x1e\x5a\x17\x08"
3931 + "\x5b\xef\xfd\xc1\xb8\xd1\x16\x71\x3e\x74"
3932 + "\xf8\x2f\xa9\x42\xd6\x4c\xdb\xc4\x68\x2d",
3934 + .plaintext = "message digest",
3936 + .digest = "\x3a\x8e\x28\x50\x2e\xd4\x5d\x42\x2f\x68"
3937 + "\x84\x4f\x9d\xd3\x16\xe7\xb9\x85\x33\xfa"
3938 + "\x3f\x2a\x91\xd2\x9f\x84\xd4\x25\xc8\x8d"
3939 + "\x6b\x4e\xff\x72\x7d\xf6\x6a\x7c\x01\x97",
3941 + .plaintext = "abcdefghijklmnopqrstuvwxyz",
3943 + .digest = "\xca\xbd\xb1\x81\x0b\x92\x47\x0a\x20\x93"
3944 + "\xaa\x6b\xce\x05\x95\x2c\x28\x34\x8c\xf4"
3945 + "\x3f\xf6\x08\x41\x97\x51\x66\xbb\x40\xed"
3946 + "\x23\x40\x04\xb8\x82\x44\x63\xe6\xb0\x09",
3948 + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
3949 + "fghijklmnopqrstuvwxyz0123456789",
3951 + .digest = "\xed\x54\x49\x40\xc8\x6d\x67\xf2\x50\xd2"
3952 + "\x32\xc3\x0b\x7b\x3e\x57\x70\xe0\xc6\x0c"
3953 + "\x8c\xb9\xa4\xca\xfe\x3b\x11\x38\x8a\xf9"
3954 + "\x92\x0e\x1b\x99\x23\x0b\x84\x3c\x86\xa4",
3956 + .plaintext = "1234567890123456789012345678901234567890"
3957 + "1234567890123456789012345678901234567890",
3959 + .digest = "\x55\x78\x88\xaf\x5f\x6d\x8e\xd6\x2a\xb6"
3960 + "\x69\x45\xc6\xd2\xa0\xa4\x7e\xcd\x53\x41"
3961 + "\xe9\x15\xeb\x8f\xea\x1d\x05\x24\x95\x5f"
3962 + "\x82\x5d\xc7\x17\xe4\xa0\x08\xab\x2d\x42",
3964 + .plaintext = "abcdbcdecdefdefgefghfghighij"
3965 + "hijkijkljklmklmnlmnomnopnopq",
3967 + .digest = "\xd0\x34\xa7\x95\x0c\xf7\x22\x02\x1b\xa4"
3968 + "\xb8\x4d\xf7\x69\xa5\xde\x20\x60\xe2\x59"
3969 + "\xdf\x4c\x9b\xb4\xa4\x26\x8c\x0e\x93\x5b"
3970 + "\xbc\x74\x70\xa9\x69\xc9\xd0\x72\xa1\xac",
3972 + .tap = { 28, 28 },
3977 @@ -817,6 +1076,168 @@
3981 + * HMAC-RIPEMD128 test vectors from RFC2286
3983 +#define HMAC_RMD128_TEST_VECTORS 7
3985 +static struct hash_testvec hmac_rmd128_tv_template[] = {
3987 + .key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
3989 + .plaintext = "Hi There",
3991 + .digest = "\xfb\xf6\x1f\x94\x92\xaa\x4b\xbf"
3992 + "\x81\xc1\x72\xe8\x4e\x07\x34\xdb",
3996 + .plaintext = "what do ya want for nothing?",
3998 + .digest = "\x87\x5f\x82\x88\x62\xb6\xb3\x34"
3999 + "\xb4\x27\xc5\x5f\x9f\x7f\xf0\x9b",
4001 + .tap = { 14, 14 },
4003 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
4005 + .plaintext = "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4006 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4007 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4008 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
4010 + .digest = "\x09\xf0\xb2\x84\x6d\x2f\x54\x3d"
4011 + "\xa3\x63\xcb\xec\x8d\x62\xa3\x8d",
4013 + .key = "\x01\x02\x03\x04\x05\x06\x07\x08"
4014 + "\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
4015 + "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
4017 + .plaintext = "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4018 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4019 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4020 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
4022 + .digest = "\xbd\xbb\xd7\xcf\x03\xe4\x4b\x5a"
4023 + "\xa6\x0a\xf8\x15\xbe\x4d\x22\x94",
4025 + .key = "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
4027 + .plaintext = "Test With Truncation",
4029 + .digest = "\xe7\x98\x08\xf2\x4b\x25\xfd\x03"
4030 + "\x1c\x15\x5f\x0d\x55\x1d\x9a\x3a",
4032 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4033 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4034 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4035 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4036 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4037 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4040 + .plaintext = "Test Using Larger Than Block-Size Key - Hash Key First",
4042 + .digest = "\xdc\x73\x29\x28\xde\x98\x10\x4a"
4043 + "\x1f\x59\xd3\x73\xc1\x50\xac\xbb",
4045 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4046 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4047 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4048 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4049 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4050 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4053 + .plaintext = "Test Using Larger Than Block-Size Key and Larger Than One "
4054 + "Block-Size Data",
4056 + .digest = "\x5c\x6b\xec\x96\x79\x3e\x16\xd4"
4057 + "\x06\x90\xc2\x37\x63\x5f\x30\xc5",
4062 + * HMAC-RIPEMD160 test vectors from RFC2286
4064 +#define HMAC_RMD160_TEST_VECTORS 7
4066 +static struct hash_testvec hmac_rmd160_tv_template[] = {
4068 + .key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
4070 + .plaintext = "Hi There",
4072 + .digest = "\x24\xcb\x4b\xd6\x7d\x20\xfc\x1a\x5d\x2e"
4073 + "\xd7\x73\x2d\xcc\x39\x37\x7f\x0a\x56\x68",
4077 + .plaintext = "what do ya want for nothing?",
4079 + .digest = "\xdd\xa6\xc0\x21\x3a\x48\x5a\x9e\x24\xf4"
4080 + "\x74\x20\x64\xa7\xf0\x33\xb4\x3c\x40\x69",
4082 + .tap = { 14, 14 },
4084 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
4086 + .plaintext = "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4087 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4088 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4089 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
4091 + .digest = "\xb0\xb1\x05\x36\x0d\xe7\x59\x96\x0a\xb4"
4092 + "\xf3\x52\x98\xe1\x16\xe2\x95\xd8\xe7\xc1",
4094 + .key = "\x01\x02\x03\x04\x05\x06\x07\x08"
4095 + "\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
4096 + "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
4098 + .plaintext = "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4099 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4100 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4101 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
4103 + .digest = "\xd5\xca\x86\x2f\x4d\x21\xd5\xe6\x10\xe1"
4104 + "\x8b\x4c\xf1\xbe\xb9\x7a\x43\x65\xec\xf4",
4106 + .key = "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
4108 + .plaintext = "Test With Truncation",
4110 + .digest = "\x76\x19\x69\x39\x78\xf9\x1d\x90\x53\x9a"
4111 + "\xe7\x86\x50\x0f\xf3\xd8\xe0\x51\x8e\x39",
4113 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4114 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4115 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4116 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4117 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4118 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4121 + .plaintext = "Test Using Larger Than Block-Size Key - Hash Key First",
4123 + .digest = "\x64\x66\xca\x07\xac\x5e\xac\x29\xe1\xbd"
4124 + "\x52\x3e\x5a\xda\x76\x05\xb7\x91\xfd\x8b",
4126 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4127 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4128 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4129 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4130 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4131 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4134 + .plaintext = "Test Using Larger Than Block-Size Key and Larger Than One "
4135 + "Block-Size Data",
4137 + .digest = "\x69\xea\x60\x79\x8d\x71\x61\x6c\xce\x5f"
4138 + "\xd0\x87\x1e\x23\x75\x4c\xd7\x5d\x5a\x0a",
4143 * HMAC-SHA1 test vectors from RFC2202
4145 #define HMAC_SHA1_TEST_VECTORS 7
4146 @@ -1442,6 +1863,8 @@
4147 #define DES_CBC_DEC_TEST_VECTORS 4
4148 #define DES3_EDE_ENC_TEST_VECTORS 3
4149 #define DES3_EDE_DEC_TEST_VECTORS 3
4150 +#define DES3_EDE_CBC_ENC_TEST_VECTORS 1
4151 +#define DES3_EDE_CBC_DEC_TEST_VECTORS 1
4153 static struct cipher_testvec des_enc_tv_template[] = {
4154 { /* From Applied Cryptography */
4155 @@ -1680,9 +2103,6 @@
4160 - * We really need some more test vectors, especially for DES3 CBC.
4162 static struct cipher_testvec des3_ede_enc_tv_template[] = {
4163 { /* These are from openssl */
4164 .key = "\x01\x23\x45\x67\x89\xab\xcd\xef"
4165 @@ -1745,6 +2165,94 @@
4169 +static struct cipher_testvec des3_ede_cbc_enc_tv_template[] = {
4170 + { /* Generated from openssl */
4171 + .key = "\xE9\xC0\xFF\x2E\x76\x0B\x64\x24"
4172 + "\x44\x4D\x99\x5A\x12\xD6\x40\xC0"
4173 + "\xEA\xC2\x84\xE8\x14\x95\xDB\xE8",
4175 + .iv = "\x7D\x33\x88\x93\x0F\x93\xB2\x42",
4176 + .input = "\x6f\x54\x20\x6f\x61\x4d\x79\x6e"
4177 + "\x53\x20\x63\x65\x65\x72\x73\x74"
4178 + "\x54\x20\x6f\x6f\x4d\x20\x6e\x61"
4179 + "\x20\x79\x65\x53\x72\x63\x74\x65"
4180 + "\x20\x73\x6f\x54\x20\x6f\x61\x4d"
4181 + "\x79\x6e\x53\x20\x63\x65\x65\x72"
4182 + "\x73\x74\x54\x20\x6f\x6f\x4d\x20"
4183 + "\x6e\x61\x20\x79\x65\x53\x72\x63"
4184 + "\x74\x65\x20\x73\x6f\x54\x20\x6f"
4185 + "\x61\x4d\x79\x6e\x53\x20\x63\x65"
4186 + "\x65\x72\x73\x74\x54\x20\x6f\x6f"
4187 + "\x4d\x20\x6e\x61\x20\x79\x65\x53"
4188 + "\x72\x63\x74\x65\x20\x73\x6f\x54"
4189 + "\x20\x6f\x61\x4d\x79\x6e\x53\x20"
4190 + "\x63\x65\x65\x72\x73\x74\x54\x20"
4191 + "\x6f\x6f\x4d\x20\x6e\x61\x0a\x79",
4193 + .result = "\x0e\x2d\xb6\x97\x3c\x56\x33\xf4"
4194 + "\x67\x17\x21\xc7\x6e\x8a\xd5\x49"
4195 + "\x74\xb3\x49\x05\xc5\x1c\xd0\xed"
4196 + "\x12\x56\x5c\x53\x96\xb6\x00\x7d"
4197 + "\x90\x48\xfc\xf5\x8d\x29\x39\xcc"
4198 + "\x8a\xd5\x35\x18\x36\x23\x4e\xd7"
4199 + "\x76\xd1\xda\x0c\x94\x67\xbb\x04"
4200 + "\x8b\xf2\x03\x6c\xa8\xcf\xb6\xea"
4201 + "\x22\x64\x47\xaa\x8f\x75\x13\xbf"
4202 + "\x9f\xc2\xc3\xf0\xc9\x56\xc5\x7a"
4203 + "\x71\x63\x2e\x89\x7b\x1e\x12\xca"
4204 + "\xe2\x5f\xaf\xd8\xa4\xf8\xc9\x7a"
4205 + "\xd6\xf9\x21\x31\x62\x44\x45\xa6"
4206 + "\xd6\xbc\x5a\xd3\x2d\x54\x43\xcc"
4207 + "\x9d\xde\xa5\x70\xe9\x42\x45\x8a"
4208 + "\x6b\xfa\xb1\x91\x13\xb0\xd9\x19",
4213 +static struct cipher_testvec des3_ede_cbc_dec_tv_template[] = {
4214 + { /* Generated from openssl */
4215 + .key = "\xE9\xC0\xFF\x2E\x76\x0B\x64\x24"
4216 + "\x44\x4D\x99\x5A\x12\xD6\x40\xC0"
4217 + "\xEA\xC2\x84\xE8\x14\x95\xDB\xE8",
4219 + .iv = "\x7D\x33\x88\x93\x0F\x93\xB2\x42",
4220 + .input = "\x0e\x2d\xb6\x97\x3c\x56\x33\xf4"
4221 + "\x67\x17\x21\xc7\x6e\x8a\xd5\x49"
4222 + "\x74\xb3\x49\x05\xc5\x1c\xd0\xed"
4223 + "\x12\x56\x5c\x53\x96\xb6\x00\x7d"
4224 + "\x90\x48\xfc\xf5\x8d\x29\x39\xcc"
4225 + "\x8a\xd5\x35\x18\x36\x23\x4e\xd7"
4226 + "\x76\xd1\xda\x0c\x94\x67\xbb\x04"
4227 + "\x8b\xf2\x03\x6c\xa8\xcf\xb6\xea"
4228 + "\x22\x64\x47\xaa\x8f\x75\x13\xbf"
4229 + "\x9f\xc2\xc3\xf0\xc9\x56\xc5\x7a"
4230 + "\x71\x63\x2e\x89\x7b\x1e\x12\xca"
4231 + "\xe2\x5f\xaf\xd8\xa4\xf8\xc9\x7a"
4232 + "\xd6\xf9\x21\x31\x62\x44\x45\xa6"
4233 + "\xd6\xbc\x5a\xd3\x2d\x54\x43\xcc"
4234 + "\x9d\xde\xa5\x70\xe9\x42\x45\x8a"
4235 + "\x6b\xfa\xb1\x91\x13\xb0\xd9\x19",
4237 + .result = "\x6f\x54\x20\x6f\x61\x4d\x79\x6e"
4238 + "\x53\x20\x63\x65\x65\x72\x73\x74"
4239 + "\x54\x20\x6f\x6f\x4d\x20\x6e\x61"
4240 + "\x20\x79\x65\x53\x72\x63\x74\x65"
4241 + "\x20\x73\x6f\x54\x20\x6f\x61\x4d"
4242 + "\x79\x6e\x53\x20\x63\x65\x65\x72"
4243 + "\x73\x74\x54\x20\x6f\x6f\x4d\x20"
4244 + "\x6e\x61\x20\x79\x65\x53\x72\x63"
4245 + "\x74\x65\x20\x73\x6f\x54\x20\x6f"
4246 + "\x61\x4d\x79\x6e\x53\x20\x63\x65"
4247 + "\x65\x72\x73\x74\x54\x20\x6f\x6f"
4248 + "\x4d\x20\x6e\x61\x20\x79\x65\x53"
4249 + "\x72\x63\x74\x65\x20\x73\x6f\x54"
4250 + "\x20\x6f\x61\x4d\x79\x6e\x53\x20"
4251 + "\x63\x65\x65\x72\x73\x74\x54\x20"
4252 + "\x6f\x6f\x4d\x20\x6e\x61\x0a\x79",
4258 * Blowfish test vectors.
4260 --- a/drivers/crypto/Kconfig
4261 +++ b/drivers/crypto/Kconfig
4262 @@ -174,4 +174,30 @@
4263 Select this option if you want to enable the random number generator
4264 on the HIFN 795x crypto adapters.
4266 +config CRYPTO_DEV_TALITOS
4267 + tristate "Talitos Freescale Security Engine (SEC)"
4268 + select CRYPTO_ALGAPI
4269 + select CRYPTO_AUTHENC
4271 + depends on FSL_SOC
4273 + Say 'Y' here to use the Freescale Security Engine (SEC)
4274 + to offload cryptographic algorithm computation.
4276 + The Freescale SEC is present on PowerQUICC 'E' processors, such
4277 + as the MPC8349E and MPC8548E.
4279 + To compile this driver as a module, choose M here: the module
4280 + will be called talitos.
4282 +config CRYPTO_DEV_IXP4XX
4283 + tristate "Driver for IXP4xx crypto hardware acceleration"
4284 + depends on ARCH_IXP4XX
4286 + select CRYPTO_ALGAPI
4287 + select CRYPTO_AUTHENC
4288 + select CRYPTO_BLKCIPHER
4290 + Driver for the IXP4xx NPE crypto engine.
4293 --- a/drivers/crypto/Makefile
4294 +++ b/drivers/crypto/Makefile
4296 obj-$(CONFIG_CRYPTO_DEV_PADLOCK_SHA) += padlock-sha.o
4297 obj-$(CONFIG_CRYPTO_DEV_GEODE) += geode-aes.o
4298 obj-$(CONFIG_CRYPTO_DEV_HIFN_795X) += hifn_795x.o
4299 +obj-$(CONFIG_CRYPTO_DEV_TALITOS) += talitos.o
4300 +obj-$(CONFIG_CRYPTO_DEV_IXP4XX) += ixp4xx_crypto.o
4301 --- a/drivers/crypto/hifn_795x.c
4302 +++ b/drivers/crypto/hifn_795x.c
4304 #include <linux/dma-mapping.h>
4305 #include <linux/scatterlist.h>
4306 #include <linux/highmem.h>
4307 -#include <linux/interrupt.h>
4308 #include <linux/crypto.h>
4309 #include <linux/hw_random.h>
4310 #include <linux/ktime.h>
4312 #define HIFN_D_DST_RSIZE 80*4
4313 #define HIFN_D_RES_RSIZE 24*4
4315 -#define HIFN_QUEUE_LENGTH HIFN_D_CMD_RSIZE-5
4316 +#define HIFN_D_DST_DALIGN 4
4318 +#define HIFN_QUEUE_LENGTH HIFN_D_CMD_RSIZE-1
4320 #define AES_MIN_KEY_SIZE 16
4321 #define AES_MAX_KEY_SIZE 32
4322 @@ -535,10 +536,10 @@
4324 struct hifn_mac_command
4326 - volatile u16 masks;
4327 - volatile u16 header_skip;
4328 - volatile u16 source_count;
4329 - volatile u16 reserved;
4330 + volatile __le16 masks;
4331 + volatile __le16 header_skip;
4332 + volatile __le16 source_count;
4333 + volatile __le16 reserved;
4336 #define HIFN_MAC_CMD_ALG_MASK 0x0001
4337 @@ -564,10 +565,10 @@
4339 struct hifn_comp_command
4341 - volatile u16 masks;
4342 - volatile u16 header_skip;
4343 - volatile u16 source_count;
4344 - volatile u16 reserved;
4345 + volatile __le16 masks;
4346 + volatile __le16 header_skip;
4347 + volatile __le16 source_count;
4348 + volatile __le16 reserved;
4351 #define HIFN_COMP_CMD_SRCLEN_M 0xc000
4352 @@ -583,10 +584,10 @@
4354 struct hifn_base_result
4356 - volatile u16 flags;
4357 - volatile u16 session;
4358 - volatile u16 src_cnt; /* 15:0 of source count */
4359 - volatile u16 dst_cnt; /* 15:0 of dest count */
4360 + volatile __le16 flags;
4361 + volatile __le16 session;
4362 + volatile __le16 src_cnt; /* 15:0 of source count */
4363 + volatile __le16 dst_cnt; /* 15:0 of dest count */
4366 #define HIFN_BASE_RES_DSTOVERRUN 0x0200 /* destination overrun */
4369 struct hifn_comp_result
4371 - volatile u16 flags;
4373 + volatile __le16 flags;
4374 + volatile __le16 crc;
4377 #define HIFN_COMP_RES_LCB_M 0xff00 /* longitudinal check byte */
4380 struct hifn_mac_result
4382 - volatile u16 flags;
4383 - volatile u16 reserved;
4384 + volatile __le16 flags;
4385 + volatile __le16 reserved;
4386 /* followed by 0, 6, 8, or 10 u16's of the MAC, then crypt */
4391 struct hifn_crypt_result
4393 - volatile u16 flags;
4394 - volatile u16 reserved;
4395 + volatile __le16 flags;
4396 + volatile __le16 reserved;
4399 #define HIFN_CRYPT_RES_SRC_NOTZERO 0x0001 /* source expired */
4400 @@ -686,12 +687,12 @@
4402 static inline void hifn_write_0(struct hifn_device *dev, u32 reg, u32 val)
4404 - writel(val, dev->bar[0] + reg);
4405 + writel((__force u32)cpu_to_le32(val), dev->bar[0] + reg);
4408 static inline void hifn_write_1(struct hifn_device *dev, u32 reg, u32 val)
4410 - writel(val, dev->bar[1] + reg);
4411 + writel((__force u32)cpu_to_le32(val), dev->bar[1] + reg);
4414 static void hifn_wait_puc(struct hifn_device *dev)
4416 char *offtbl = NULL;
4419 - for (i = 0; i < sizeof(pci2id)/sizeof(pci2id[0]); i++) {
4420 + for (i = 0; i < ARRAY_SIZE(pci2id); i++) {
4421 if (pci2id[i].pci_vendor == dev->pdev->vendor &&
4422 pci2id[i].pci_prod == dev->pdev->device) {
4423 offtbl = pci2id[i].card_id;
4424 @@ -1037,14 +1038,14 @@
4425 hifn_write_0(dev, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
4427 /* write all 4 ring address registers */
4428 - hifn_write_1(dev, HIFN_1_DMA_CRAR, __cpu_to_le32(dptr +
4429 - offsetof(struct hifn_dma, cmdr[0])));
4430 - hifn_write_1(dev, HIFN_1_DMA_SRAR, __cpu_to_le32(dptr +
4431 - offsetof(struct hifn_dma, srcr[0])));
4432 - hifn_write_1(dev, HIFN_1_DMA_DRAR, __cpu_to_le32(dptr +
4433 - offsetof(struct hifn_dma, dstr[0])));
4434 - hifn_write_1(dev, HIFN_1_DMA_RRAR, __cpu_to_le32(dptr +
4435 - offsetof(struct hifn_dma, resr[0])));
4436 + hifn_write_1(dev, HIFN_1_DMA_CRAR, dptr +
4437 + offsetof(struct hifn_dma, cmdr[0]));
4438 + hifn_write_1(dev, HIFN_1_DMA_SRAR, dptr +
4439 + offsetof(struct hifn_dma, srcr[0]));
4440 + hifn_write_1(dev, HIFN_1_DMA_DRAR, dptr +
4441 + offsetof(struct hifn_dma, dstr[0]));
4442 + hifn_write_1(dev, HIFN_1_DMA_RRAR, dptr +
4443 + offsetof(struct hifn_dma, resr[0]));
4447 @@ -1166,109 +1167,15 @@
4451 -static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
4452 - unsigned int offset, unsigned int size)
4454 - struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4458 - addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
4462 - dma->srcr[idx].p = __cpu_to_le32(addr);
4463 - dma->srcr[idx].l = __cpu_to_le32(size) | HIFN_D_VALID |
4464 - HIFN_D_MASKDONEIRQ | HIFN_D_NOINVALID | HIFN_D_LAST;
4466 - if (++idx == HIFN_D_SRC_RSIZE) {
4467 - dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
4469 - HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4476 - if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
4477 - hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
4478 - dev->flags |= HIFN_FLAG_SRC_BUSY;
4484 -static void hifn_setup_res_desc(struct hifn_device *dev)
4486 - struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4488 - dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
4489 - HIFN_D_VALID | HIFN_D_LAST);
4491 - * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
4492 - * HIFN_D_LAST | HIFN_D_NOINVALID);
4495 - if (++dma->resi == HIFN_D_RES_RSIZE) {
4496 - dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
4497 - HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4503 - if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
4504 - hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
4505 - dev->flags |= HIFN_FLAG_RES_BUSY;
4509 -static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
4510 - unsigned offset, unsigned size)
4512 - struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4516 - addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
4519 - dma->dstr[idx].p = __cpu_to_le32(addr);
4520 - dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
4521 - HIFN_D_MASKDONEIRQ | HIFN_D_NOINVALID | HIFN_D_LAST);
4523 - if (++idx == HIFN_D_DST_RSIZE) {
4524 - dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
4525 - HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
4526 - HIFN_D_LAST | HIFN_D_NOINVALID);
4532 - if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
4533 - hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
4534 - dev->flags |= HIFN_FLAG_DST_BUSY;
4538 -static int hifn_setup_dma(struct hifn_device *dev, struct page *spage, unsigned int soff,
4539 - struct page *dpage, unsigned int doff, unsigned int nbytes, void *priv,
4540 - struct hifn_context *ctx)
4541 +static int hifn_setup_cmd_desc(struct hifn_device *dev,
4542 + struct hifn_context *ctx, void *priv, unsigned int nbytes)
4544 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4545 int cmd_len, sa_idx;
4549 - dprintk("%s: spage: %p, soffset: %u, dpage: %p, doffset: %u, nbytes: %u, priv: %p, ctx: %p.\n",
4550 - dev->name, spage, soff, dpage, doff, nbytes, priv, ctx);
4552 - sa_idx = dma->resi;
4554 - hifn_setup_src_desc(dev, spage, soff, nbytes);
4556 + sa_idx = dma->cmdi;
4557 buf_pos = buf = dma->command_bufs[dma->cmdi];
4560 @@ -1370,16 +1277,113 @@
4561 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
4562 dev->flags |= HIFN_FLAG_CMD_BUSY;
4565 - hifn_setup_dst_desc(dev, dpage, doff, nbytes);
4566 - hifn_setup_res_desc(dev);
4574 +static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
4575 + unsigned int offset, unsigned int size)
4577 + struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4581 + addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
4585 + dma->srcr[idx].p = __cpu_to_le32(addr);
4586 + dma->srcr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
4587 + HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4589 + if (++idx == HIFN_D_SRC_RSIZE) {
4590 + dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
4592 + HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4599 + if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
4600 + hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
4601 + dev->flags |= HIFN_FLAG_SRC_BUSY;
4607 +static void hifn_setup_res_desc(struct hifn_device *dev)
4609 + struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4611 + dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
4612 + HIFN_D_VALID | HIFN_D_LAST);
4614 + * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
4618 + if (++dma->resi == HIFN_D_RES_RSIZE) {
4619 + dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
4620 + HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4626 + if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
4627 + hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
4628 + dev->flags |= HIFN_FLAG_RES_BUSY;
4632 +static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
4633 + unsigned offset, unsigned size)
4635 + struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4639 + addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
4642 + dma->dstr[idx].p = __cpu_to_le32(addr);
4643 + dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
4644 + HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4646 + if (++idx == HIFN_D_DST_RSIZE) {
4647 + dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
4648 + HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
4655 + if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
4656 + hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
4657 + dev->flags |= HIFN_FLAG_DST_BUSY;
4661 +static int hifn_setup_dma(struct hifn_device *dev, struct page *spage, unsigned int soff,
4662 + struct page *dpage, unsigned int doff, unsigned int nbytes, void *priv,
4663 + struct hifn_context *ctx)
4665 + dprintk("%s: spage: %p, soffset: %u, dpage: %p, doffset: %u, nbytes: %u, priv: %p, ctx: %p.\n",
4666 + dev->name, spage, soff, dpage, doff, nbytes, priv, ctx);
4668 + hifn_setup_src_desc(dev, spage, soff, nbytes);
4669 + hifn_setup_cmd_desc(dev, ctx, priv, nbytes);
4670 + hifn_setup_dst_desc(dev, dpage, doff, nbytes);
4671 + hifn_setup_res_desc(dev);
4675 static int ablkcipher_walk_init(struct ablkcipher_walk *w,
4676 int num, gfp_t gfp_flags)
4678 @@ -1431,7 +1435,7 @@
4682 - copy = min(drest, src->length);
4683 + copy = min(drest, min(size, src->length));
4685 saddr = kmap_atomic(sg_page(src), KM_SOFTIRQ1);
4686 memcpy(daddr, saddr + src->offset, copy);
4687 @@ -1458,10 +1462,6 @@
4688 static int ablkcipher_walk(struct ablkcipher_request *req,
4689 struct ablkcipher_walk *w)
4691 - unsigned blocksize =
4692 - crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
4693 - unsigned alignmask =
4694 - crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
4695 struct scatterlist *src, *dst, *t;
4697 unsigned int nbytes = req->nbytes, offset, copy, diff;
4698 @@ -1477,16 +1477,14 @@
4699 dst = &req->dst[idx];
4701 dprintk("\n%s: slen: %u, dlen: %u, soff: %u, doff: %u, offset: %u, "
4702 - "blocksize: %u, nbytes: %u.\n",
4704 __func__, src->length, dst->length, src->offset,
4705 - dst->offset, offset, blocksize, nbytes);
4706 + dst->offset, offset, nbytes);
4708 - if (src->length & (blocksize - 1) ||
4709 - src->offset & (alignmask - 1) ||
4710 - dst->length & (blocksize - 1) ||
4711 - dst->offset & (alignmask - 1) ||
4713 - unsigned slen = src->length - offset;
4714 + if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
4715 + !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN) ||
4717 + unsigned slen = min(src->length - offset, nbytes);
4718 unsigned dlen = PAGE_SIZE;
4721 @@ -1498,8 +1496,8 @@
4725 - copy = slen & ~(blocksize - 1);
4726 - diff = slen & (blocksize - 1);
4727 + copy = slen & ~(HIFN_D_DST_DALIGN - 1);
4728 + diff = slen & (HIFN_D_DST_DALIGN - 1);
4730 if (dlen < nbytes) {
4732 @@ -1507,7 +1505,7 @@
4733 * to put there additional blocksized chunk,
4734 * so we mark that page as containing only
4735 * blocksize aligned chunks:
4736 - * t->length = (slen & ~(blocksize - 1));
4737 + * t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
4738 * and increase number of bytes to be processed
4741 @@ -1544,7 +1542,7 @@
4743 kunmap_atomic(daddr, KM_SOFTIRQ0);
4745 - nbytes -= src->length;
4746 + nbytes -= min(src->length, nbytes);
4750 @@ -1563,14 +1561,10 @@
4751 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
4752 struct hifn_device *dev = ctx->dev;
4753 struct page *spage, *dpage;
4754 - unsigned long soff, doff, flags;
4755 + unsigned long soff, doff, dlen, flags;
4756 unsigned int nbytes = req->nbytes, idx = 0, len;
4757 int err = -EINVAL, sg_num;
4758 struct scatterlist *src, *dst, *t;
4759 - unsigned blocksize =
4760 - crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
4761 - unsigned alignmask =
4762 - crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
4764 if (ctx->iv && !ctx->ivsize && ctx->mode != ACRYPTO_MODE_ECB)
4766 @@ -1578,17 +1572,14 @@
4767 ctx->walk.flags = 0;
4770 - src = &req->src[idx];
4771 dst = &req->dst[idx];
4772 + dlen = min(dst->length, nbytes);
4774 - if (src->length & (blocksize - 1) ||
4775 - src->offset & (alignmask - 1) ||
4776 - dst->length & (blocksize - 1) ||
4777 - dst->offset & (alignmask - 1)) {
4778 + if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
4779 + !IS_ALIGNED(dlen, HIFN_D_DST_DALIGN))
4780 ctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
4783 - nbytes -= src->length;
4788 @@ -1602,7 +1593,10 @@
4791 sg_num = ablkcipher_walk(req, &ctx->walk);
4795 + goto err_out_exit;
4797 atomic_set(&ctx->sg_num, sg_num);
4799 spin_lock_irqsave(&dev->lock, flags);
4800 @@ -1640,7 +1634,7 @@
4805 + nbytes -= min(len, nbytes);
4808 dev->active = HIFN_DEFAULT_ACTIVE_NUM;
4809 @@ -1651,7 +1645,7 @@
4811 spin_unlock_irqrestore(&dev->lock, flags);
4813 - if (err && printk_ratelimit())
4815 dprintk("%s: iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
4816 "type: %u, err: %d.\n",
4817 dev->name, ctx->iv, ctx->ivsize,
4818 @@ -1745,8 +1739,7 @@
4823 - copy = min(dst->length, srest);
4824 + copy = min(srest, min(dst->length, size));
4826 daddr = kmap_atomic(sg_page(dst), KM_IRQ0);
4827 memcpy(daddr + dst->offset + offset, saddr, copy);
4828 @@ -1803,7 +1796,7 @@
4829 sg_page(dst), dst->length, nbytes);
4832 - nbytes -= dst->length;
4833 + nbytes -= min(dst->length, nbytes);
4837 @@ -2202,9 +2195,9 @@
4840 if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
4841 - err = hifn_process_queue(dev);
4842 + hifn_process_queue(dev);
4845 + return -EINPROGRESS;
4849 @@ -2364,7 +2357,7 @@
4850 * 3DES ECB, CBC, CFB and OFB modes.
4853 - .name = "cfb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
4854 + .name = "cfb(des3_ede)", .drv_name = "cfb-3des", .bsize = 8,
4856 .min_keysize = HIFN_3DES_KEY_LENGTH,
4857 .max_keysize = HIFN_3DES_KEY_LENGTH,
4858 @@ -2374,7 +2367,7 @@
4862 - .name = "ofb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
4863 + .name = "ofb(des3_ede)", .drv_name = "ofb-3des", .bsize = 8,
4865 .min_keysize = HIFN_3DES_KEY_LENGTH,
4866 .max_keysize = HIFN_3DES_KEY_LENGTH,
4867 @@ -2384,8 +2377,9 @@
4871 - .name = "cbc(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
4872 + .name = "cbc(des3_ede)", .drv_name = "cbc-3des", .bsize = 8,
4874 + .ivsize = HIFN_IV_LENGTH,
4875 .min_keysize = HIFN_3DES_KEY_LENGTH,
4876 .max_keysize = HIFN_3DES_KEY_LENGTH,
4877 .setkey = hifn_setkey,
4878 @@ -2394,7 +2388,7 @@
4882 - .name = "ecb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
4883 + .name = "ecb(des3_ede)", .drv_name = "ecb-3des", .bsize = 8,
4885 .min_keysize = HIFN_3DES_KEY_LENGTH,
4886 .max_keysize = HIFN_3DES_KEY_LENGTH,
4887 @@ -2408,7 +2402,7 @@
4888 * DES ECB, CBC, CFB and OFB modes.
4891 - .name = "cfb(des)", .drv_name = "hifn-des", .bsize = 8,
4892 + .name = "cfb(des)", .drv_name = "cfb-des", .bsize = 8,
4894 .min_keysize = HIFN_DES_KEY_LENGTH,
4895 .max_keysize = HIFN_DES_KEY_LENGTH,
4896 @@ -2418,7 +2412,7 @@
4900 - .name = "ofb(des)", .drv_name = "hifn-des", .bsize = 8,
4901 + .name = "ofb(des)", .drv_name = "ofb-des", .bsize = 8,
4903 .min_keysize = HIFN_DES_KEY_LENGTH,
4904 .max_keysize = HIFN_DES_KEY_LENGTH,
4905 @@ -2428,8 +2422,9 @@
4909 - .name = "cbc(des)", .drv_name = "hifn-des", .bsize = 8,
4910 + .name = "cbc(des)", .drv_name = "cbc-des", .bsize = 8,
4912 + .ivsize = HIFN_IV_LENGTH,
4913 .min_keysize = HIFN_DES_KEY_LENGTH,
4914 .max_keysize = HIFN_DES_KEY_LENGTH,
4915 .setkey = hifn_setkey,
4916 @@ -2438,7 +2433,7 @@
4920 - .name = "ecb(des)", .drv_name = "hifn-des", .bsize = 8,
4921 + .name = "ecb(des)", .drv_name = "ecb-des", .bsize = 8,
4923 .min_keysize = HIFN_DES_KEY_LENGTH,
4924 .max_keysize = HIFN_DES_KEY_LENGTH,
4925 @@ -2452,7 +2447,7 @@
4926 * AES ECB, CBC, CFB and OFB modes.
4929 - .name = "ecb(aes)", .drv_name = "hifn-aes", .bsize = 16,
4930 + .name = "ecb(aes)", .drv_name = "ecb-aes", .bsize = 16,
4932 .min_keysize = AES_MIN_KEY_SIZE,
4933 .max_keysize = AES_MAX_KEY_SIZE,
4934 @@ -2462,8 +2457,9 @@
4938 - .name = "cbc(aes)", .drv_name = "hifn-aes", .bsize = 16,
4939 + .name = "cbc(aes)", .drv_name = "cbc-aes", .bsize = 16,
4941 + .ivsize = HIFN_AES_IV_LENGTH,
4942 .min_keysize = AES_MIN_KEY_SIZE,
4943 .max_keysize = AES_MAX_KEY_SIZE,
4944 .setkey = hifn_setkey,
4945 @@ -2472,7 +2468,7 @@
4949 - .name = "cfb(aes)", .drv_name = "hifn-aes", .bsize = 16,
4950 + .name = "cfb(aes)", .drv_name = "cfb-aes", .bsize = 16,
4952 .min_keysize = AES_MIN_KEY_SIZE,
4953 .max_keysize = AES_MAX_KEY_SIZE,
4954 @@ -2482,7 +2478,7 @@
4958 - .name = "ofb(aes)", .drv_name = "hifn-aes", .bsize = 16,
4959 + .name = "ofb(aes)", .drv_name = "ofb-aes", .bsize = 16,
4961 .min_keysize = AES_MIN_KEY_SIZE,
4962 .max_keysize = AES_MAX_KEY_SIZE,
4963 @@ -2514,15 +2510,14 @@
4966 snprintf(alg->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s", t->name);
4967 - snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", t->drv_name);
4968 + snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-%s",
4969 + t->drv_name, dev->name);
4971 alg->alg.cra_priority = 300;
4972 alg->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
4973 alg->alg.cra_blocksize = t->bsize;
4974 alg->alg.cra_ctxsize = sizeof(struct hifn_context);
4975 - alg->alg.cra_alignmask = 15;
4976 - if (t->bsize == 8)
4977 - alg->alg.cra_alignmask = 3;
4978 + alg->alg.cra_alignmask = 0;
4979 alg->alg.cra_type = &crypto_ablkcipher_type;
4980 alg->alg.cra_module = THIS_MODULE;
4981 alg->alg.cra_u.ablkcipher = t->ablkcipher;
4983 +++ b/drivers/crypto/ixp4xx_crypto.c
4986 + * Intel IXP4xx NPE-C crypto driver
4988 + * Copyright (C) 2008 Christian Hohnstaedt <chohnstaedt@innominate.com>
4990 + * This program is free software; you can redistribute it and/or modify it
4991 + * under the terms of version 2 of the GNU General Public License
4992 + * as published by the Free Software Foundation.
4996 +#include <linux/platform_device.h>
4997 +#include <linux/dma-mapping.h>
4998 +#include <linux/dmapool.h>
4999 +#include <linux/crypto.h>
5000 +#include <linux/kernel.h>
5001 +#include <linux/rtnetlink.h>
5002 +#include <linux/interrupt.h>
5003 +#include <linux/spinlock.h>
5005 +#include <crypto/ctr.h>
5006 +#include <crypto/des.h>
5007 +#include <crypto/aes.h>
5008 +#include <crypto/sha.h>
5009 +#include <crypto/algapi.h>
5010 +#include <crypto/aead.h>
5011 +#include <crypto/authenc.h>
5012 +#include <crypto/scatterwalk.h>
5014 +#include <asm/arch/npe.h>
5015 +#include <asm/arch/qmgr.h>
5017 +#define MAX_KEYLEN 32
5019 +/* hash: cfgword + 2 * digestlen; crypt: keylen + cfgword */
5020 +#define NPE_CTX_LEN 80
5021 +#define AES_BLOCK128 16
5023 +#define NPE_OP_HASH_VERIFY 0x01
5024 +#define NPE_OP_CCM_ENABLE 0x04
5025 +#define NPE_OP_CRYPT_ENABLE 0x08
5026 +#define NPE_OP_HASH_ENABLE 0x10
5027 +#define NPE_OP_NOT_IN_PLACE 0x20
5028 +#define NPE_OP_HMAC_DISABLE 0x40
5029 +#define NPE_OP_CRYPT_ENCRYPT 0x80
5031 +#define NPE_OP_CCM_GEN_MIC 0xcc
5032 +#define NPE_OP_HASH_GEN_ICV 0x50
5033 +#define NPE_OP_ENC_GEN_KEY 0xc9
5035 +#define MOD_ECB 0x0000
5036 +#define MOD_CTR 0x1000
5037 +#define MOD_CBC_ENC 0x2000
5038 +#define MOD_CBC_DEC 0x3000
5039 +#define MOD_CCM_ENC 0x4000
5040 +#define MOD_CCM_DEC 0x5000
5042 +#define KEYLEN_128 4
5043 +#define KEYLEN_192 6
5044 +#define KEYLEN_256 8
5046 +#define CIPH_DECR 0x0000
5047 +#define CIPH_ENCR 0x0400
5049 +#define MOD_DES 0x0000
5050 +#define MOD_TDEA2 0x0100
5051 +#define MOD_3DES 0x0200
5052 +#define MOD_AES 0x0800
5053 +#define MOD_AES128 (0x0800 | KEYLEN_128)
5054 +#define MOD_AES192 (0x0900 | KEYLEN_192)
5055 +#define MOD_AES256 (0x0a00 | KEYLEN_256)
5057 +#define MAX_IVLEN 16
5058 +#define NPE_ID 2 /* NPE C */
5059 +#define NPE_QLEN 16
5060 +/* Space for registering when the first
5061 + * NPE_QLEN crypt_ctl are busy */
5062 +#define NPE_QLEN_TOTAL 64
5064 +#define SEND_QID 29
5065 +#define RECV_QID 30
5067 +#define CTL_FLAG_UNUSED 0x0000
5068 +#define CTL_FLAG_USED 0x1000
5069 +#define CTL_FLAG_PERFORM_ABLK 0x0001
5070 +#define CTL_FLAG_GEN_ICV 0x0002
5071 +#define CTL_FLAG_GEN_REVAES 0x0004
5072 +#define CTL_FLAG_PERFORM_AEAD 0x0008
5073 +#define CTL_FLAG_MASK 0x000f
5075 +#define HMAC_IPAD_VALUE 0x36
5076 +#define HMAC_OPAD_VALUE 0x5C
5077 +#define HMAC_PAD_BLOCKLEN SHA1_BLOCK_SIZE
5079 +#define MD5_DIGEST_SIZE 16
5081 +struct buffer_desc {
5086 + u32 __reserved[4];
5087 + struct buffer_desc *next;
5091 + u8 mode; /* NPE_OP_* operation mode */
5094 + u8 iv[MAX_IVLEN]; /* IV for CBC mode or CTR IV for CTR mode */
5095 + u32 icv_rev_aes; /* icv or rev aes */
5098 + u16 auth_offs; /* Authentication start offset */
5099 + u16 auth_len; /* Authentication data length */
5100 + u16 crypt_offs; /* Cryption start offset */
5101 + u16 crypt_len; /* Cryption data length */
5102 + u32 aadAddr; /* Additional Auth Data Addr for CCM mode */
5103 + u32 crypto_ctx; /* NPE Crypto Param structure address */
5105 + /* Used by Host: 4*4 bytes*/
5106 + unsigned ctl_flags;
5108 + struct ablkcipher_request *ablk_req;
5109 + struct aead_request *aead_req;
5110 + struct crypto_tfm *tfm;
5112 + struct buffer_desc *regist_buf;
5117 + struct buffer_desc *src;
5118 + struct buffer_desc *dst;
5119 + unsigned src_nents;
5120 + unsigned dst_nents;
5124 + struct buffer_desc *buffer;
5125 + unsigned short assoc_nents;
5126 + unsigned short src_nents;
5127 + struct scatterlist ivlist;
5128 + /* used when the hmac is not on one sg entry */
5133 +struct ix_hash_algo {
5135 + unsigned char *icv;
5139 + unsigned char *npe_ctx;
5140 + dma_addr_t npe_ctx_phys;
5146 + struct ix_sa_dir encrypt;
5147 + struct ix_sa_dir decrypt;
5149 + u8 authkey[MAX_KEYLEN];
5151 + u8 enckey[MAX_KEYLEN];
5152 + u8 salt[MAX_IVLEN];
5153 + u8 nonce[CTR_RFC3686_NONCE_SIZE];
5155 + atomic_t configuring;
5156 + struct completion completion;
5160 + struct crypto_alg crypto;
5161 + const struct ix_hash_algo *hash;
5168 +static const struct ix_hash_algo hash_alg_md5 = {
5169 + .cfgword = 0xAA010004,
5170 + .icv = "\x01\x23\x45\x67\x89\xAB\xCD\xEF"
5171 + "\xFE\xDC\xBA\x98\x76\x54\x32\x10",
5173 +static const struct ix_hash_algo hash_alg_sha1 = {
5174 + .cfgword = 0x00000005,
5175 + .icv = "\x67\x45\x23\x01\xEF\xCD\xAB\x89\x98\xBA"
5176 + "\xDC\xFE\x10\x32\x54\x76\xC3\xD2\xE1\xF0",
5179 +static struct npe *npe_c;
5180 +static struct dma_pool *buffer_pool = NULL;
5181 +static struct dma_pool *ctx_pool = NULL;
5183 +static struct crypt_ctl *crypt_virt = NULL;
5184 +static dma_addr_t crypt_phys;
5186 +static int support_aes = 1;
5188 +static void dev_release(struct device *dev)
5193 +#define DRIVER_NAME "ixp4xx_crypto"
5194 +static struct platform_device pseudo_dev = {
5195 + .name = DRIVER_NAME,
5197 + .num_resources = 0,
5199 + .coherent_dma_mask = DMA_32BIT_MASK,
5200 + .release = dev_release,
5204 +static struct device *dev = &pseudo_dev.dev;
5206 +static inline dma_addr_t crypt_virt2phys(struct crypt_ctl *virt)
5208 + return crypt_phys + (virt - crypt_virt) * sizeof(struct crypt_ctl);
5211 +static inline struct crypt_ctl *crypt_phys2virt(dma_addr_t phys)
5213 + return crypt_virt + (phys - crypt_phys) / sizeof(struct crypt_ctl);
5216 +static inline u32 cipher_cfg_enc(struct crypto_tfm *tfm)
5218 + return container_of(tfm->__crt_alg, struct ixp_alg,crypto)->cfg_enc;
5221 +static inline u32 cipher_cfg_dec(struct crypto_tfm *tfm)
5223 + return container_of(tfm->__crt_alg, struct ixp_alg,crypto)->cfg_dec;
5226 +static inline const struct ix_hash_algo *ix_hash(struct crypto_tfm *tfm)
5228 + return container_of(tfm->__crt_alg, struct ixp_alg, crypto)->hash;
5231 +static int setup_crypt_desc(void)
5233 + BUILD_BUG_ON(sizeof(struct crypt_ctl) != 64);
5234 + crypt_virt = dma_alloc_coherent(dev,
5235 + NPE_QLEN * sizeof(struct crypt_ctl),
5236 + &crypt_phys, GFP_KERNEL);
5239 + memset(crypt_virt, 0, NPE_QLEN * sizeof(struct crypt_ctl));
5243 +static spinlock_t desc_lock;
5244 +static struct crypt_ctl *get_crypt_desc(void)
5247 + static int idx = 0;
5248 + unsigned long flags;
5250 + spin_lock_irqsave(&desc_lock, flags);
5252 + if (unlikely(!crypt_virt))
5253 + setup_crypt_desc();
5254 + if (unlikely(!crypt_virt)) {
5255 + spin_unlock_irqrestore(&desc_lock, flags);
5259 + if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
5260 + if (++idx >= NPE_QLEN)
5262 + crypt_virt[i].ctl_flags = CTL_FLAG_USED;
5263 + spin_unlock_irqrestore(&desc_lock, flags);
5264 + return crypt_virt +i;
5266 + spin_unlock_irqrestore(&desc_lock, flags);
5271 +static spinlock_t emerg_lock;
5272 +static struct crypt_ctl *get_crypt_desc_emerg(void)
5275 + static int idx = NPE_QLEN;
5276 + struct crypt_ctl *desc;
5277 + unsigned long flags;
5279 + desc = get_crypt_desc();
5282 + if (unlikely(!crypt_virt))
5285 + spin_lock_irqsave(&emerg_lock, flags);
5287 + if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
5288 + if (++idx >= NPE_QLEN_TOTAL)
5290 + crypt_virt[i].ctl_flags = CTL_FLAG_USED;
5291 + spin_unlock_irqrestore(&emerg_lock, flags);
5292 + return crypt_virt +i;
5294 + spin_unlock_irqrestore(&emerg_lock, flags);
5299 +static void free_buf_chain(struct buffer_desc *buf, u32 phys)
5302 + struct buffer_desc *buf1;
5306 + phys1 = buf->phys_next;
5307 + dma_pool_free(buffer_pool, buf, phys);
5313 +static struct tasklet_struct crypto_done_tasklet;
5315 +static void finish_scattered_hmac(struct crypt_ctl *crypt)
5317 + struct aead_request *req = crypt->data.aead_req;
5318 + struct aead_ctx *req_ctx = aead_request_ctx(req);
5319 + struct crypto_aead *tfm = crypto_aead_reqtfm(req);
5320 + int authsize = crypto_aead_authsize(tfm);
5321 + int decryptlen = req->cryptlen - authsize;
5323 + if (req_ctx->encrypt) {
5324 + scatterwalk_map_and_copy(req_ctx->hmac_virt,
5325 + req->src, decryptlen, authsize, 1);
5327 + dma_pool_free(buffer_pool, req_ctx->hmac_virt, crypt->icv_rev_aes);
5330 +static void one_packet(dma_addr_t phys)
5332 + struct crypt_ctl *crypt;
5333 + struct ixp_ctx *ctx;
5335 + enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
5337 + failed = phys & 0x1 ? -EBADMSG : 0;
5339 + crypt = crypt_phys2virt(phys);
5341 + switch (crypt->ctl_flags & CTL_FLAG_MASK) {
5342 + case CTL_FLAG_PERFORM_AEAD: {
5343 + struct aead_request *req = crypt->data.aead_req;
5344 + struct aead_ctx *req_ctx = aead_request_ctx(req);
5345 + dma_unmap_sg(dev, req->assoc, req_ctx->assoc_nents,
5347 + dma_unmap_sg(dev, &req_ctx->ivlist, 1, DMA_BIDIRECTIONAL);
5348 + dma_unmap_sg(dev, req->src, req_ctx->src_nents,
5349 + DMA_BIDIRECTIONAL);
5351 + free_buf_chain(req_ctx->buffer, crypt->src_buf);
5352 + if (req_ctx->hmac_virt) {
5353 + finish_scattered_hmac(crypt);
5355 + req->base.complete(&req->base, failed);
5358 + case CTL_FLAG_PERFORM_ABLK: {
5359 + struct ablkcipher_request *req = crypt->data.ablk_req;
5360 + struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
5362 + if (req_ctx->dst) {
5363 + nents = req_ctx->dst_nents;
5364 + dma_unmap_sg(dev, req->dst, nents, DMA_FROM_DEVICE);
5365 + free_buf_chain(req_ctx->dst, crypt->dst_buf);
5366 + src_direction = DMA_TO_DEVICE;
5368 + nents = req_ctx->src_nents;
5369 + dma_unmap_sg(dev, req->src, nents, src_direction);
5370 + free_buf_chain(req_ctx->src, crypt->src_buf);
5371 + req->base.complete(&req->base, failed);
5374 + case CTL_FLAG_GEN_ICV:
5375 + ctx = crypto_tfm_ctx(crypt->data.tfm);
5376 + dma_pool_free(ctx_pool, crypt->regist_ptr,
5377 + crypt->regist_buf->phys_addr);
5378 + dma_pool_free(buffer_pool, crypt->regist_buf, crypt->src_buf);
5379 + if (atomic_dec_and_test(&ctx->configuring))
5380 + complete(&ctx->completion);
5382 + case CTL_FLAG_GEN_REVAES:
5383 + ctx = crypto_tfm_ctx(crypt->data.tfm);
5384 + *(u32*)ctx->decrypt.npe_ctx &= cpu_to_be32(~CIPH_ENCR);
5385 + if (atomic_dec_and_test(&ctx->configuring))
5386 + complete(&ctx->completion);
5391 + crypt->ctl_flags = CTL_FLAG_UNUSED;
5394 +static void irqhandler(void *_unused)
5396 + tasklet_schedule(&crypto_done_tasklet);
5399 +static void crypto_done_action(unsigned long arg)
5403 + for(i=0; i<4; i++) {
5404 + dma_addr_t phys = qmgr_get_entry(RECV_QID);
5409 + tasklet_schedule(&crypto_done_tasklet);
5412 +static int init_ixp_crypto(void)
5414 + int ret = -ENODEV;
5416 + if (! ( ~(*IXP4XX_EXP_CFG2) & (IXP4XX_FEATURE_HASH |
5417 + IXP4XX_FEATURE_AES | IXP4XX_FEATURE_DES))) {
5418 + printk(KERN_ERR "ixp_crypto: No HW crypto available\n");
5421 + npe_c = npe_request(NPE_ID);
5425 + if (!npe_running(npe_c)) {
5426 + npe_load_firmware(npe_c, npe_name(npe_c), dev);
5429 + /* buffer_pool will also be used to sometimes store the hmac,
5430 + * so assure it is large enough
5432 + BUILD_BUG_ON(SHA1_DIGEST_SIZE > sizeof(struct buffer_desc));
5433 + buffer_pool = dma_pool_create("buffer", dev,
5434 + sizeof(struct buffer_desc), 32, 0);
5436 + if (!buffer_pool) {
5439 + ctx_pool = dma_pool_create("context", dev,
5440 + NPE_CTX_LEN, 16, 0);
5444 + ret = qmgr_request_queue(SEND_QID, NPE_QLEN_TOTAL, 0, 0);
5447 + ret = qmgr_request_queue(RECV_QID, NPE_QLEN, 0, 0);
5449 + qmgr_release_queue(SEND_QID);
5452 + qmgr_set_irq(RECV_QID, QUEUE_IRQ_SRC_NOT_EMPTY, irqhandler, NULL);
5453 + tasklet_init(&crypto_done_tasklet, crypto_done_action, 0);
5455 + qmgr_enable_irq(RECV_QID);
5459 + dma_pool_destroy(ctx_pool);
5461 + dma_pool_destroy(buffer_pool);
5462 + npe_release(npe_c);
5466 +static void release_ixp_crypto(void)
5468 + qmgr_disable_irq(RECV_QID);
5469 + tasklet_kill(&crypto_done_tasklet);
5471 + qmgr_release_queue(SEND_QID);
5472 + qmgr_release_queue(RECV_QID);
5474 + dma_pool_destroy(ctx_pool);
5475 + dma_pool_destroy(buffer_pool);
5477 + npe_release(npe_c);
5480 + dma_free_coherent(dev,
5481 + NPE_QLEN_TOTAL * sizeof( struct crypt_ctl),
5482 + crypt_virt, crypt_phys);
5487 +static void reset_sa_dir(struct ix_sa_dir *dir)
5489 + memset(dir->npe_ctx, 0, NPE_CTX_LEN);
5490 + dir->npe_ctx_idx = 0;
5491 + dir->npe_mode = 0;
5494 +static int init_sa_dir(struct ix_sa_dir *dir)
5496 + dir->npe_ctx = dma_pool_alloc(ctx_pool, GFP_KERNEL, &dir->npe_ctx_phys);
5497 + if (!dir->npe_ctx) {
5500 + reset_sa_dir(dir);
5504 +static void free_sa_dir(struct ix_sa_dir *dir)
5506 + memset(dir->npe_ctx, 0, NPE_CTX_LEN);
5507 + dma_pool_free(ctx_pool, dir->npe_ctx, dir->npe_ctx_phys);
5510 +static int init_tfm(struct crypto_tfm *tfm)
5512 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5515 + atomic_set(&ctx->configuring, 0);
5516 + ret = init_sa_dir(&ctx->encrypt);
5519 + ret = init_sa_dir(&ctx->decrypt);
5521 + free_sa_dir(&ctx->encrypt);
5526 +static int init_tfm_ablk(struct crypto_tfm *tfm)
5528 + tfm->crt_ablkcipher.reqsize = sizeof(struct ablk_ctx);
5529 + return init_tfm(tfm);
5532 +static int init_tfm_aead(struct crypto_tfm *tfm)
5534 + tfm->crt_aead.reqsize = sizeof(struct aead_ctx);
5535 + return init_tfm(tfm);
5538 +static void exit_tfm(struct crypto_tfm *tfm)
5540 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5541 + free_sa_dir(&ctx->encrypt);
5542 + free_sa_dir(&ctx->decrypt);
5545 +static int register_chain_var(struct crypto_tfm *tfm, u8 xpad, u32 target,
5546 + int init_len, u32 ctx_addr, const u8 *key, int key_len)
5548 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5549 + struct crypt_ctl *crypt;
5550 + struct buffer_desc *buf;
5553 + u32 pad_phys, buf_phys;
5555 + BUILD_BUG_ON(NPE_CTX_LEN < HMAC_PAD_BLOCKLEN);
5556 + pad = dma_pool_alloc(ctx_pool, GFP_KERNEL, &pad_phys);
5559 + buf = dma_pool_alloc(buffer_pool, GFP_KERNEL, &buf_phys);
5561 + dma_pool_free(ctx_pool, pad, pad_phys);
5564 + crypt = get_crypt_desc_emerg();
5566 + dma_pool_free(ctx_pool, pad, pad_phys);
5567 + dma_pool_free(buffer_pool, buf, buf_phys);
5571 + memcpy(pad, key, key_len);
5572 + memset(pad + key_len, 0, HMAC_PAD_BLOCKLEN - key_len);
5573 + for (i = 0; i < HMAC_PAD_BLOCKLEN; i++) {
5577 + crypt->data.tfm = tfm;
5578 + crypt->regist_ptr = pad;
5579 + crypt->regist_buf = buf;
5581 + crypt->auth_offs = 0;
5582 + crypt->auth_len = HMAC_PAD_BLOCKLEN;
5583 + crypt->crypto_ctx = ctx_addr;
5584 + crypt->src_buf = buf_phys;
5585 + crypt->icv_rev_aes = target;
5586 + crypt->mode = NPE_OP_HASH_GEN_ICV;
5587 + crypt->init_len = init_len;
5588 + crypt->ctl_flags |= CTL_FLAG_GEN_ICV;
5591 + buf->buf_len = HMAC_PAD_BLOCKLEN;
5593 + buf->phys_addr = pad_phys;
5595 + atomic_inc(&ctx->configuring);
5596 + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
5597 + BUG_ON(qmgr_stat_overflow(SEND_QID));
5601 +static int setup_auth(struct crypto_tfm *tfm, int encrypt, unsigned authsize,
5602 + const u8 *key, int key_len, unsigned digest_len)
5604 + u32 itarget, otarget, npe_ctx_addr;
5605 + unsigned char *cinfo;
5606 + int init_len, ret = 0;
5608 + struct ix_sa_dir *dir;
5609 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5610 + const struct ix_hash_algo *algo;
5612 + dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
5613 + cinfo = dir->npe_ctx + dir->npe_ctx_idx;
5614 + algo = ix_hash(tfm);
5616 + /* write cfg word to cryptinfo */
5617 + cfgword = algo->cfgword | ( authsize << 6); /* (authsize/4) << 8 */
5618 + *(u32*)cinfo = cpu_to_be32(cfgword);
5619 + cinfo += sizeof(cfgword);
5621 + /* write ICV to cryptinfo */
5622 + memcpy(cinfo, algo->icv, digest_len);
5623 + cinfo += digest_len;
5625 + itarget = dir->npe_ctx_phys + dir->npe_ctx_idx
5626 + + sizeof(algo->cfgword);
5627 + otarget = itarget + digest_len;
5628 + init_len = cinfo - (dir->npe_ctx + dir->npe_ctx_idx);
5629 + npe_ctx_addr = dir->npe_ctx_phys + dir->npe_ctx_idx;
5631 + dir->npe_ctx_idx += init_len;
5632 + dir->npe_mode |= NPE_OP_HASH_ENABLE;
5635 + dir->npe_mode |= NPE_OP_HASH_VERIFY;
5637 + ret = register_chain_var(tfm, HMAC_OPAD_VALUE, otarget,
5638 + init_len, npe_ctx_addr, key, key_len);
5641 + return register_chain_var(tfm, HMAC_IPAD_VALUE, itarget,
5642 + init_len, npe_ctx_addr, key, key_len);
5645 +static int gen_rev_aes_key(struct crypto_tfm *tfm)
5647 + struct crypt_ctl *crypt;
5648 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5649 + struct ix_sa_dir *dir = &ctx->decrypt;
5651 + crypt = get_crypt_desc_emerg();
5655 + *(u32*)dir->npe_ctx |= cpu_to_be32(CIPH_ENCR);
5657 + crypt->data.tfm = tfm;
5658 + crypt->crypt_offs = 0;
5659 + crypt->crypt_len = AES_BLOCK128;
5660 + crypt->src_buf = 0;
5661 + crypt->crypto_ctx = dir->npe_ctx_phys;
5662 + crypt->icv_rev_aes = dir->npe_ctx_phys + sizeof(u32);
5663 + crypt->mode = NPE_OP_ENC_GEN_KEY;
5664 + crypt->init_len = dir->npe_ctx_idx;
5665 + crypt->ctl_flags |= CTL_FLAG_GEN_REVAES;
5667 + atomic_inc(&ctx->configuring);
5668 + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
5669 + BUG_ON(qmgr_stat_overflow(SEND_QID));
5673 +static int setup_cipher(struct crypto_tfm *tfm, int encrypt,
5674 + const u8 *key, int key_len)
5678 + u32 keylen_cfg = 0;
5679 + struct ix_sa_dir *dir;
5680 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5681 + u32 *flags = &tfm->crt_flags;
5683 + dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
5684 + cinfo = dir->npe_ctx;
5687 + cipher_cfg = cipher_cfg_enc(tfm);
5688 + dir->npe_mode |= NPE_OP_CRYPT_ENCRYPT;
5690 + cipher_cfg = cipher_cfg_dec(tfm);
5692 + if (cipher_cfg & MOD_AES) {
5693 + switch (key_len) {
5694 + case 16: keylen_cfg = MOD_AES128 | KEYLEN_128; break;
5695 + case 24: keylen_cfg = MOD_AES192 | KEYLEN_192; break;
5696 + case 32: keylen_cfg = MOD_AES256 | KEYLEN_256; break;
5698 + *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
5701 + cipher_cfg |= keylen_cfg;
5702 + } else if (cipher_cfg & MOD_3DES) {
5703 + const u32 *K = (const u32 *)key;
5704 + if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
5705 + !((K[2] ^ K[4]) | (K[3] ^ K[5]))))
5707 + *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
5711 + u32 tmp[DES_EXPKEY_WORDS];
5712 + if (des_ekey(tmp, key) == 0) {
5713 + *flags |= CRYPTO_TFM_RES_WEAK_KEY;
5716 + /* write cfg word to cryptinfo */
5717 + *(u32*)cinfo = cpu_to_be32(cipher_cfg);
5718 + cinfo += sizeof(cipher_cfg);
5720 + /* write cipher key to cryptinfo */
5721 + memcpy(cinfo, key, key_len);
5722 + /* NPE wants keylen set to DES3_EDE_KEY_SIZE even for single DES */
5723 + if (key_len < DES3_EDE_KEY_SIZE && !(cipher_cfg & MOD_AES)) {
5724 + memset(cinfo + key_len, 0, DES3_EDE_KEY_SIZE -key_len);
5725 + key_len = DES3_EDE_KEY_SIZE;
5727 + dir->npe_ctx_idx = sizeof(cipher_cfg) + key_len;
5728 + dir->npe_mode |= NPE_OP_CRYPT_ENABLE;
5729 + if ((cipher_cfg & MOD_AES) && !encrypt) {
5730 + return gen_rev_aes_key(tfm);
5735 +static int count_sg(struct scatterlist *sg, int nbytes)
5738 + for (i = 0; nbytes > 0; i++, sg = sg_next(sg))
5739 + nbytes -= sg->length;
5743 +static struct buffer_desc *chainup_buffers(struct scatterlist *sg,
5744 + unsigned nbytes, struct buffer_desc *buf, gfp_t flags)
5748 + while (nbytes > 0) {
5749 + struct buffer_desc *next_buf;
5750 + u32 next_buf_phys;
5751 + unsigned len = min(nbytes, sg_dma_len(sg));
5755 + if (!buf->phys_addr) {
5756 + buf->phys_addr = sg_dma_address(sg);
5757 + buf->buf_len = len;
5759 + buf->phys_next = 0;
5762 + /* Two consecutive chunks on one page may be handled by the old
5763 + * buffer descriptor, increased by the length of the new one
5765 + if (sg_dma_address(sg) == buf->phys_addr + buf->buf_len) {
5766 + buf->buf_len += len;
5769 + next_buf = dma_pool_alloc(buffer_pool, flags, &next_buf_phys);
5772 + buf->next = next_buf;
5773 + buf->phys_next = next_buf_phys;
5777 + buf->phys_next = 0;
5778 + buf->phys_addr = sg_dma_address(sg);
5779 + buf->buf_len = len;
5788 +static int ablk_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
5789 + unsigned int key_len)
5791 + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
5792 + u32 *flags = &tfm->base.crt_flags;
5795 + init_completion(&ctx->completion);
5796 + atomic_inc(&ctx->configuring);
5798 + reset_sa_dir(&ctx->encrypt);
5799 + reset_sa_dir(&ctx->decrypt);
5801 + ctx->encrypt.npe_mode = NPE_OP_HMAC_DISABLE;
5802 + ctx->decrypt.npe_mode = NPE_OP_HMAC_DISABLE;
5804 + ret = setup_cipher(&tfm->base, 0, key, key_len);
5807 + ret = setup_cipher(&tfm->base, 1, key, key_len);
5811 + if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
5812 + if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
5815 + *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
5819 + if (!atomic_dec_and_test(&ctx->configuring))
5820 + wait_for_completion(&ctx->completion);
5824 +static int ablk_rfc3686_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
5825 + unsigned int key_len)
5827 + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
5829 + /* the nonce is stored in bytes at end of key */
5830 + if (key_len < CTR_RFC3686_NONCE_SIZE)
5833 + memcpy(ctx->nonce, key + (key_len - CTR_RFC3686_NONCE_SIZE),
5834 + CTR_RFC3686_NONCE_SIZE);
5836 + key_len -= CTR_RFC3686_NONCE_SIZE;
5837 + return ablk_setkey(tfm, key, key_len);
5840 +static int ablk_perform(struct ablkcipher_request *req, int encrypt)
5842 + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
5843 + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
5844 + unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
5845 + int ret = -ENOMEM;
5846 + struct ix_sa_dir *dir;
5847 + struct crypt_ctl *crypt;
5848 + unsigned int nbytes = req->nbytes, nents;
5849 + enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
5850 + struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
5851 + gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
5852 + GFP_KERNEL : GFP_ATOMIC;
5854 + if (qmgr_stat_full(SEND_QID))
5856 + if (atomic_read(&ctx->configuring))
5859 + dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
5861 + crypt = get_crypt_desc();
5865 + crypt->data.ablk_req = req;
5866 + crypt->crypto_ctx = dir->npe_ctx_phys;
5867 + crypt->mode = dir->npe_mode;
5868 + crypt->init_len = dir->npe_ctx_idx;
5870 + crypt->crypt_offs = 0;
5871 + crypt->crypt_len = nbytes;
5873 + BUG_ON(ivsize && !req->info);
5874 + memcpy(crypt->iv, req->info, ivsize);
5875 + if (req->src != req->dst) {
5876 + crypt->mode |= NPE_OP_NOT_IN_PLACE;
5877 + nents = count_sg(req->dst, nbytes);
5878 + /* This was never tested by Intel
5879 + * for more than one dst buffer, I think. */
5880 + BUG_ON(nents != 1);
5881 + req_ctx->dst_nents = nents;
5882 + dma_map_sg(dev, req->dst, nents, DMA_FROM_DEVICE);
5883 + req_ctx->dst = dma_pool_alloc(buffer_pool, flags,&crypt->dst_buf);
5884 + if (!req_ctx->dst)
5885 + goto unmap_sg_dest;
5886 + req_ctx->dst->phys_addr = 0;
5887 + if (!chainup_buffers(req->dst, nbytes, req_ctx->dst, flags))
5888 + goto free_buf_dest;
5889 + src_direction = DMA_TO_DEVICE;
5891 + req_ctx->dst = NULL;
5892 + req_ctx->dst_nents = 0;
5894 + nents = count_sg(req->src, nbytes);
5895 + req_ctx->src_nents = nents;
5896 + dma_map_sg(dev, req->src, nents, src_direction);
5898 + req_ctx->src = dma_pool_alloc(buffer_pool, flags, &crypt->src_buf);
5899 + if (!req_ctx->src)
5900 + goto unmap_sg_src;
5901 + req_ctx->src->phys_addr = 0;
5902 + if (!chainup_buffers(req->src, nbytes, req_ctx->src, flags))
5903 + goto free_buf_src;
5905 + crypt->ctl_flags |= CTL_FLAG_PERFORM_ABLK;
5906 + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
5907 + BUG_ON(qmgr_stat_overflow(SEND_QID));
5908 + return -EINPROGRESS;
5911 + free_buf_chain(req_ctx->src, crypt->src_buf);
5913 + dma_unmap_sg(dev, req->src, req_ctx->src_nents, src_direction);
5915 + if (req->src != req->dst) {
5916 + free_buf_chain(req_ctx->dst, crypt->dst_buf);
5918 + dma_unmap_sg(dev, req->src, req_ctx->dst_nents,
5921 + crypt->ctl_flags = CTL_FLAG_UNUSED;
5925 +static int ablk_encrypt(struct ablkcipher_request *req)
5927 + return ablk_perform(req, 1);
5930 +static int ablk_decrypt(struct ablkcipher_request *req)
5932 + return ablk_perform(req, 0);
5935 +static int ablk_rfc3686_crypt(struct ablkcipher_request *req)
5937 + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
5938 + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
5939 + u8 iv[CTR_RFC3686_BLOCK_SIZE];
5940 + u8 *info = req->info;
5943 + /* set up counter block */
5944 + memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
5945 + memcpy(iv + CTR_RFC3686_NONCE_SIZE, info, CTR_RFC3686_IV_SIZE);
5947 + /* initialize counter portion of counter block */
5948 + *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
5952 + ret = ablk_perform(req, 1);
5957 +static int hmac_inconsistent(struct scatterlist *sg, unsigned start,
5958 + unsigned int nbytes)
5966 + if (start < offset + sg->length)
5969 + offset += sg->length;
5972 + return (start + nbytes > offset + sg->length);
5975 +static int aead_perform(struct aead_request *req, int encrypt,
5976 + int cryptoffset, int eff_cryptlen, u8 *iv)
5978 + struct crypto_aead *tfm = crypto_aead_reqtfm(req);
5979 + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
5980 + unsigned ivsize = crypto_aead_ivsize(tfm);
5981 + unsigned authsize = crypto_aead_authsize(tfm);
5982 + int ret = -ENOMEM;
5983 + struct ix_sa_dir *dir;
5984 + struct crypt_ctl *crypt;
5985 + unsigned int cryptlen, nents;
5986 + struct buffer_desc *buf;
5987 + struct aead_ctx *req_ctx = aead_request_ctx(req);
5988 + gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
5989 + GFP_KERNEL : GFP_ATOMIC;
5991 + if (qmgr_stat_full(SEND_QID))
5993 + if (atomic_read(&ctx->configuring))
5997 + dir = &ctx->encrypt;
5998 + cryptlen = req->cryptlen;
6000 + dir = &ctx->decrypt;
6001 + /* req->cryptlen includes the authsize when decrypting */
6002 + cryptlen = req->cryptlen -authsize;
6003 + eff_cryptlen -= authsize;
6005 + crypt = get_crypt_desc();
6009 + crypt->data.aead_req = req;
6010 + crypt->crypto_ctx = dir->npe_ctx_phys;
6011 + crypt->mode = dir->npe_mode;
6012 + crypt->init_len = dir->npe_ctx_idx;
6014 + crypt->crypt_offs = cryptoffset;
6015 + crypt->crypt_len = eff_cryptlen;
6017 + crypt->auth_offs = 0;
6018 + crypt->auth_len = req->assoclen + ivsize + cryptlen;
6019 + BUG_ON(ivsize && !req->iv);
6020 + memcpy(crypt->iv, req->iv, ivsize);
6022 + if (req->src != req->dst) {
6023 + BUG(); /* -ENOTSUP because of my lazyness */
6026 + req_ctx->buffer = dma_pool_alloc(buffer_pool, flags, &crypt->src_buf);
6027 + if (!req_ctx->buffer)
6029 + req_ctx->buffer->phys_addr = 0;
6031 + nents = count_sg(req->assoc, req->assoclen);
6032 + req_ctx->assoc_nents = nents;
6033 + dma_map_sg(dev, req->assoc, nents, DMA_TO_DEVICE);
6034 + buf = chainup_buffers(req->assoc, req->assoclen, req_ctx->buffer,flags);
6036 + goto unmap_sg_assoc;
6038 + sg_init_table(&req_ctx->ivlist, 1);
6039 + sg_set_buf(&req_ctx->ivlist, iv, ivsize);
6040 + dma_map_sg(dev, &req_ctx->ivlist, 1, DMA_BIDIRECTIONAL);
6041 + buf = chainup_buffers(&req_ctx->ivlist, ivsize, buf, flags);
6044 + if (unlikely(hmac_inconsistent(req->src, cryptlen, authsize))) {
6045 + /* The 12 hmac bytes are scattered,
6046 + * we need to copy them into a safe buffer */
6047 + req_ctx->hmac_virt = dma_pool_alloc(buffer_pool, flags,
6048 + &crypt->icv_rev_aes);
6049 + if (unlikely(!req_ctx->hmac_virt))
6052 + scatterwalk_map_and_copy(req_ctx->hmac_virt,
6053 + req->src, cryptlen, authsize, 0);
6055 + req_ctx->encrypt = encrypt;
6057 + req_ctx->hmac_virt = NULL;
6060 + nents = count_sg(req->src, cryptlen + authsize);
6061 + req_ctx->src_nents = nents;
6062 + dma_map_sg(dev, req->src, nents, DMA_BIDIRECTIONAL);
6063 + buf = chainup_buffers(req->src, cryptlen + authsize, buf, flags);
6065 + goto unmap_sg_src;
6066 + if (!req_ctx->hmac_virt) {
6067 + crypt->icv_rev_aes = buf->phys_addr + buf->buf_len - authsize;
6069 + crypt->ctl_flags |= CTL_FLAG_PERFORM_AEAD;
6070 + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
6071 + BUG_ON(qmgr_stat_overflow(SEND_QID));
6072 + return -EINPROGRESS;
6074 + dma_unmap_sg(dev, req->src, req_ctx->src_nents, DMA_BIDIRECTIONAL);
6075 + if (req_ctx->hmac_virt) {
6076 + dma_pool_free(buffer_pool, req_ctx->hmac_virt,
6077 + crypt->icv_rev_aes);
6080 + dma_unmap_sg(dev, &req_ctx->ivlist, 1, DMA_BIDIRECTIONAL);
6082 + dma_unmap_sg(dev, req->assoc, req_ctx->assoc_nents, DMA_TO_DEVICE);
6083 + free_buf_chain(req_ctx->buffer, crypt->src_buf);
6085 + crypt->ctl_flags = CTL_FLAG_UNUSED;
6089 +static int aead_setup(struct crypto_aead *tfm, unsigned int authsize)
6091 + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
6092 + u32 *flags = &tfm->base.crt_flags;
6093 + unsigned digest_len = crypto_aead_alg(tfm)->maxauthsize;
6096 + if (!ctx->enckey_len && !ctx->authkey_len)
6098 + init_completion(&ctx->completion);
6099 + atomic_inc(&ctx->configuring);
6101 + reset_sa_dir(&ctx->encrypt);
6102 + reset_sa_dir(&ctx->decrypt);
6104 + ret = setup_cipher(&tfm->base, 0, ctx->enckey, ctx->enckey_len);
6107 + ret = setup_cipher(&tfm->base, 1, ctx->enckey, ctx->enckey_len);
6110 + ret = setup_auth(&tfm->base, 0, authsize, ctx->authkey,
6111 + ctx->authkey_len, digest_len);
6114 + ret = setup_auth(&tfm->base, 1, authsize, ctx->authkey,
6115 + ctx->authkey_len, digest_len);
6119 + if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
6120 + if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
6124 + *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
6128 + if (!atomic_dec_and_test(&ctx->configuring))
6129 + wait_for_completion(&ctx->completion);
6133 +static int aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
6135 + int max = crypto_aead_alg(tfm)->maxauthsize >> 2;
6137 + if ((authsize>>2) < 1 || (authsize>>2) > max || (authsize & 3))
6139 + return aead_setup(tfm, authsize);
6142 +static int aead_setkey(struct crypto_aead *tfm, const u8 *key,
6143 + unsigned int keylen)
6145 + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
6146 + struct rtattr *rta = (struct rtattr *)key;
6147 + struct crypto_authenc_key_param *param;
6149 + if (!RTA_OK(rta, keylen))
6151 + if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
6153 + if (RTA_PAYLOAD(rta) < sizeof(*param))
6156 + param = RTA_DATA(rta);
6157 + ctx->enckey_len = be32_to_cpu(param->enckeylen);
6159 + key += RTA_ALIGN(rta->rta_len);
6160 + keylen -= RTA_ALIGN(rta->rta_len);
6162 + if (keylen < ctx->enckey_len)
6165 + ctx->authkey_len = keylen - ctx->enckey_len;
6166 + memcpy(ctx->enckey, key + ctx->authkey_len, ctx->enckey_len);
6167 + memcpy(ctx->authkey, key, ctx->authkey_len);
6169 + return aead_setup(tfm, crypto_aead_authsize(tfm));
6171 + ctx->enckey_len = 0;
6172 + crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
6176 +static int aead_encrypt(struct aead_request *req)
6178 + unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
6179 + return aead_perform(req, 1, req->assoclen + ivsize,
6180 + req->cryptlen, req->iv);
6183 +static int aead_decrypt(struct aead_request *req)
6185 + unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
6186 + return aead_perform(req, 0, req->assoclen + ivsize,
6187 + req->cryptlen, req->iv);
6190 +static int aead_givencrypt(struct aead_givcrypt_request *req)
6192 + struct crypto_aead *tfm = aead_givcrypt_reqtfm(req);
6193 + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
6194 + unsigned len, ivsize = crypto_aead_ivsize(tfm);
6197 + /* copied from eseqiv.c */
6198 + if (!ctx->salted) {
6199 + get_random_bytes(ctx->salt, ivsize);
6202 + memcpy(req->areq.iv, ctx->salt, ivsize);
6204 + if (ivsize > sizeof(u64)) {
6205 + memset(req->giv, 0, ivsize - sizeof(u64));
6206 + len = sizeof(u64);
6208 + seq = cpu_to_be64(req->seq);
6209 + memcpy(req->giv + ivsize - len, &seq, len);
6210 + return aead_perform(&req->areq, 1, req->areq.assoclen,
6211 + req->areq.cryptlen +ivsize, req->giv);
6214 +static struct ixp_alg ixp4xx_algos[] = {
6217 + .cra_name = "cbc(des)",
6218 + .cra_blocksize = DES_BLOCK_SIZE,
6219 + .cra_u = { .ablkcipher = {
6220 + .min_keysize = DES_KEY_SIZE,
6221 + .max_keysize = DES_KEY_SIZE,
6222 + .ivsize = DES_BLOCK_SIZE,
6223 + .geniv = "eseqiv",
6227 + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
6228 + .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
6232 + .cra_name = "ecb(des)",
6233 + .cra_blocksize = DES_BLOCK_SIZE,
6234 + .cra_u = { .ablkcipher = {
6235 + .min_keysize = DES_KEY_SIZE,
6236 + .max_keysize = DES_KEY_SIZE,
6240 + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_ECB | KEYLEN_192,
6241 + .cfg_dec = CIPH_DECR | MOD_DES | MOD_ECB | KEYLEN_192,
6244 + .cra_name = "cbc(des3_ede)",
6245 + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
6246 + .cra_u = { .ablkcipher = {
6247 + .min_keysize = DES3_EDE_KEY_SIZE,
6248 + .max_keysize = DES3_EDE_KEY_SIZE,
6249 + .ivsize = DES3_EDE_BLOCK_SIZE,
6250 + .geniv = "eseqiv",
6254 + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
6255 + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
6258 + .cra_name = "ecb(des3_ede)",
6259 + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
6260 + .cra_u = { .ablkcipher = {
6261 + .min_keysize = DES3_EDE_KEY_SIZE,
6262 + .max_keysize = DES3_EDE_KEY_SIZE,
6266 + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_ECB | KEYLEN_192,
6267 + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_ECB | KEYLEN_192,
6270 + .cra_name = "cbc(aes)",
6271 + .cra_blocksize = AES_BLOCK_SIZE,
6272 + .cra_u = { .ablkcipher = {
6273 + .min_keysize = AES_MIN_KEY_SIZE,
6274 + .max_keysize = AES_MAX_KEY_SIZE,
6275 + .ivsize = AES_BLOCK_SIZE,
6276 + .geniv = "eseqiv",
6280 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
6281 + .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
6284 + .cra_name = "ecb(aes)",
6285 + .cra_blocksize = AES_BLOCK_SIZE,
6286 + .cra_u = { .ablkcipher = {
6287 + .min_keysize = AES_MIN_KEY_SIZE,
6288 + .max_keysize = AES_MAX_KEY_SIZE,
6292 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_ECB,
6293 + .cfg_dec = CIPH_DECR | MOD_AES | MOD_ECB,
6296 + .cra_name = "ctr(aes)",
6297 + .cra_blocksize = AES_BLOCK_SIZE,
6298 + .cra_u = { .ablkcipher = {
6299 + .min_keysize = AES_MIN_KEY_SIZE,
6300 + .max_keysize = AES_MAX_KEY_SIZE,
6301 + .ivsize = AES_BLOCK_SIZE,
6302 + .geniv = "eseqiv",
6306 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
6307 + .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
6310 + .cra_name = "rfc3686(ctr(aes))",
6311 + .cra_blocksize = AES_BLOCK_SIZE,
6312 + .cra_u = { .ablkcipher = {
6313 + .min_keysize = AES_MIN_KEY_SIZE,
6314 + .max_keysize = AES_MAX_KEY_SIZE,
6315 + .ivsize = AES_BLOCK_SIZE,
6316 + .geniv = "eseqiv",
6317 + .setkey = ablk_rfc3686_setkey,
6318 + .encrypt = ablk_rfc3686_crypt,
6319 + .decrypt = ablk_rfc3686_crypt }
6322 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
6323 + .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
6326 + .cra_name = "authenc(hmac(md5),cbc(des))",
6327 + .cra_blocksize = DES_BLOCK_SIZE,
6328 + .cra_u = { .aead = {
6329 + .ivsize = DES_BLOCK_SIZE,
6330 + .maxauthsize = MD5_DIGEST_SIZE,
6334 + .hash = &hash_alg_md5,
6335 + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
6336 + .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
6339 + .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
6340 + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
6341 + .cra_u = { .aead = {
6342 + .ivsize = DES3_EDE_BLOCK_SIZE,
6343 + .maxauthsize = MD5_DIGEST_SIZE,
6347 + .hash = &hash_alg_md5,
6348 + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
6349 + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
6352 + .cra_name = "authenc(hmac(sha1),cbc(des))",
6353 + .cra_blocksize = DES_BLOCK_SIZE,
6354 + .cra_u = { .aead = {
6355 + .ivsize = DES_BLOCK_SIZE,
6356 + .maxauthsize = SHA1_DIGEST_SIZE,
6360 + .hash = &hash_alg_sha1,
6361 + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
6362 + .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
6365 + .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
6366 + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
6367 + .cra_u = { .aead = {
6368 + .ivsize = DES3_EDE_BLOCK_SIZE,
6369 + .maxauthsize = SHA1_DIGEST_SIZE,
6373 + .hash = &hash_alg_sha1,
6374 + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
6375 + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
6378 + .cra_name = "authenc(hmac(md5),cbc(aes))",
6379 + .cra_blocksize = AES_BLOCK_SIZE,
6380 + .cra_u = { .aead = {
6381 + .ivsize = AES_BLOCK_SIZE,
6382 + .maxauthsize = MD5_DIGEST_SIZE,
6386 + .hash = &hash_alg_md5,
6387 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
6388 + .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
6391 + .cra_name = "authenc(hmac(sha1),cbc(aes))",
6392 + .cra_blocksize = AES_BLOCK_SIZE,
6393 + .cra_u = { .aead = {
6394 + .ivsize = AES_BLOCK_SIZE,
6395 + .maxauthsize = SHA1_DIGEST_SIZE,
6399 + .hash = &hash_alg_sha1,
6400 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
6401 + .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
6404 +#define IXP_POSTFIX "-ixp4xx"
6405 +static int __init ixp_module_init(void)
6407 + int num = ARRAY_SIZE(ixp4xx_algos);
6410 + if (platform_device_register(&pseudo_dev))
6413 + spin_lock_init(&desc_lock);
6414 + spin_lock_init(&emerg_lock);
6416 + err = init_ixp_crypto();
6418 + platform_device_unregister(&pseudo_dev);
6421 + for (i=0; i< num; i++) {
6422 + struct crypto_alg *cra = &ixp4xx_algos[i].crypto;
6424 + if (snprintf(cra->cra_driver_name, CRYPTO_MAX_ALG_NAME,
6425 + "%s"IXP_POSTFIX, cra->cra_name) >=
6426 + CRYPTO_MAX_ALG_NAME)
6430 + if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES)) {
6433 + if (!ixp4xx_algos[i].hash) {
6434 + /* block ciphers */
6435 + cra->cra_type = &crypto_ablkcipher_type;
6436 + cra->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
6438 + if (!cra->cra_ablkcipher.setkey)
6439 + cra->cra_ablkcipher.setkey = ablk_setkey;
6440 + if (!cra->cra_ablkcipher.encrypt)
6441 + cra->cra_ablkcipher.encrypt = ablk_encrypt;
6442 + if (!cra->cra_ablkcipher.decrypt)
6443 + cra->cra_ablkcipher.decrypt = ablk_decrypt;
6444 + cra->cra_init = init_tfm_ablk;
6447 + cra->cra_type = &crypto_aead_type;
6448 + cra->cra_flags = CRYPTO_ALG_TYPE_AEAD |
6450 + cra->cra_aead.setkey = aead_setkey;
6451 + cra->cra_aead.setauthsize = aead_setauthsize;
6452 + cra->cra_aead.encrypt = aead_encrypt;
6453 + cra->cra_aead.decrypt = aead_decrypt;
6454 + cra->cra_aead.givencrypt = aead_givencrypt;
6455 + cra->cra_init = init_tfm_aead;
6457 + cra->cra_ctxsize = sizeof(struct ixp_ctx);
6458 + cra->cra_module = THIS_MODULE;
6459 + cra->cra_alignmask = 3;
6460 + cra->cra_priority = 300;
6461 + cra->cra_exit = exit_tfm;
6462 + if (crypto_register_alg(cra))
6463 + printk(KERN_ERR "Failed to register '%s'\n",
6466 + ixp4xx_algos[i].registered = 1;
6471 +static void __exit ixp_module_exit(void)
6473 + int num = ARRAY_SIZE(ixp4xx_algos);
6476 + for (i=0; i< num; i++) {
6477 + if (ixp4xx_algos[i].registered)
6478 + crypto_unregister_alg(&ixp4xx_algos[i].crypto);
6480 + release_ixp_crypto();
6481 + platform_device_unregister(&pseudo_dev);
6484 +module_init(ixp_module_init);
6485 +module_exit(ixp_module_exit);
6487 +MODULE_LICENSE("GPL");
6488 +MODULE_AUTHOR("Christian Hohnstaedt <chohnstaedt@innominate.com>");
6489 +MODULE_DESCRIPTION("IXP4xx hardware crypto");
6491 --- a/drivers/crypto/padlock-aes.c
6492 +++ b/drivers/crypto/padlock-aes.c
6493 @@ -385,12 +385,12 @@
6496 if (!cpu_has_xcrypt) {
6497 - printk(KERN_ERR PFX "VIA PadLock not detected.\n");
6498 + printk(KERN_NOTICE PFX "VIA PadLock not detected.\n");
6502 if (!cpu_has_xcrypt_enabled) {
6503 - printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
6504 + printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
6508 --- a/drivers/crypto/padlock-sha.c
6509 +++ b/drivers/crypto/padlock-sha.c
6510 @@ -254,12 +254,12 @@
6514 - printk(KERN_ERR PFX "VIA PadLock Hash Engine not detected.\n");
6515 + printk(KERN_NOTICE PFX "VIA PadLock Hash Engine not detected.\n");
6519 if (!cpu_has_phe_enabled) {
6520 - printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
6521 + printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
6526 +++ b/drivers/crypto/talitos.c
6529 + * talitos - Freescale Integrated Security Engine (SEC) device driver
6531 + * Copyright (c) 2008 Freescale Semiconductor, Inc.
6533 + * Scatterlist Crypto API glue code copied from files with the following:
6534 + * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
6536 + * Crypto algorithm registration code copied from hifn driver:
6537 + * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
6538 + * All rights reserved.
6540 + * This program is free software; you can redistribute it and/or modify
6541 + * it under the terms of the GNU General Public License as published by
6542 + * the Free Software Foundation; either version 2 of the License, or
6543 + * (at your option) any later version.
6545 + * This program is distributed in the hope that it will be useful,
6546 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6547 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6548 + * GNU General Public License for more details.
6550 + * You should have received a copy of the GNU General Public License
6551 + * along with this program; if not, write to the Free Software
6552 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6555 +#include <linux/kernel.h>
6556 +#include <linux/module.h>
6557 +#include <linux/mod_devicetable.h>
6558 +#include <linux/device.h>
6559 +#include <linux/interrupt.h>
6560 +#include <linux/crypto.h>
6561 +#include <linux/hw_random.h>
6562 +#include <linux/of_platform.h>
6563 +#include <linux/dma-mapping.h>
6564 +#include <linux/io.h>
6565 +#include <linux/spinlock.h>
6566 +#include <linux/rtnetlink.h>
6568 +#include <crypto/algapi.h>
6569 +#include <crypto/aes.h>
6570 +#include <crypto/des.h>
6571 +#include <crypto/sha.h>
6572 +#include <crypto/aead.h>
6573 +#include <crypto/authenc.h>
6575 +#include "talitos.h"
6577 +#define TALITOS_TIMEOUT 100000
6578 +#define TALITOS_MAX_DATA_LEN 65535
6580 +#define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
6581 +#define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
6582 +#define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
6584 +/* descriptor pointer entry */
6585 +struct talitos_ptr {
6586 + __be16 len; /* length */
6587 + u8 j_extent; /* jump to sg link table and/or extent */
6588 + u8 eptr; /* extended address */
6589 + __be32 ptr; /* address */
6593 +struct talitos_desc {
6594 + __be32 hdr; /* header high bits */
6595 + __be32 hdr_lo; /* header low bits */
6596 + struct talitos_ptr ptr[7]; /* ptr/len pair array */
6600 + * talitos_request - descriptor submission request
6601 + * @desc: descriptor pointer (kernel virtual)
6602 + * @dma_desc: descriptor's physical bus address
6603 + * @callback: whom to call when descriptor processing is done
6604 + * @context: caller context (optional)
6606 +struct talitos_request {
6607 + struct talitos_desc *desc;
6608 + dma_addr_t dma_desc;
6609 + void (*callback) (struct device *dev, struct talitos_desc *desc,
6610 + void *context, int error);
6614 +struct talitos_private {
6615 + struct device *dev;
6616 + struct of_device *ofdev;
6617 + void __iomem *reg;
6620 + /* SEC version geometry (from device tree node) */
6621 + unsigned int num_channels;
6622 + unsigned int chfifo_len;
6623 + unsigned int exec_units;
6624 + unsigned int desc_types;
6626 + /* next channel to be assigned next incoming descriptor */
6627 + atomic_t last_chan;
6629 + /* per-channel request fifo */
6630 + struct talitos_request **fifo;
6633 + * length of the request fifo
6634 + * fifo_len is chfifo_len rounded up to next power of 2
6635 + * so we can use bitwise ops to wrap
6637 + unsigned int fifo_len;
6639 + /* per-channel index to next free descriptor request */
6642 + /* per-channel index to next in-progress/done descriptor request */
6645 + /* per-channel request submission (head) and release (tail) locks */
6646 + spinlock_t *head_lock;
6647 + spinlock_t *tail_lock;
6649 + /* request callback tasklet */
6650 + struct tasklet_struct done_task;
6651 + struct tasklet_struct error_task;
6653 + /* list of registered algorithms */
6654 + struct list_head alg_list;
6656 + /* hwrng device */
6661 + * map virtual single (contiguous) pointer to h/w descriptor pointer
6663 +static void map_single_talitos_ptr(struct device *dev,
6664 + struct talitos_ptr *talitos_ptr,
6665 + unsigned short len, void *data,
6666 + unsigned char extent,
6667 + enum dma_data_direction dir)
6669 + talitos_ptr->len = cpu_to_be16(len);
6670 + talitos_ptr->ptr = cpu_to_be32(dma_map_single(dev, data, len, dir));
6671 + talitos_ptr->j_extent = extent;
6675 + * unmap bus single (contiguous) h/w descriptor pointer
6677 +static void unmap_single_talitos_ptr(struct device *dev,
6678 + struct talitos_ptr *talitos_ptr,
6679 + enum dma_data_direction dir)
6681 + dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
6682 + be16_to_cpu(talitos_ptr->len), dir);
6685 +static int reset_channel(struct device *dev, int ch)
6687 + struct talitos_private *priv = dev_get_drvdata(dev);
6688 + unsigned int timeout = TALITOS_TIMEOUT;
6690 + setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
6692 + while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
6696 + if (timeout == 0) {
6697 + dev_err(dev, "failed to reset channel %d\n", ch);
6701 + /* set done writeback and IRQ */
6702 + setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_CDWE |
6703 + TALITOS_CCCR_LO_CDIE);
6708 +static int reset_device(struct device *dev)
6710 + struct talitos_private *priv = dev_get_drvdata(dev);
6711 + unsigned int timeout = TALITOS_TIMEOUT;
6713 + setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
6715 + while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
6719 + if (timeout == 0) {
6720 + dev_err(dev, "failed to reset device\n");
6728 + * Reset and initialize the device
6730 +static int init_device(struct device *dev)
6732 + struct talitos_private *priv = dev_get_drvdata(dev);
6737 + * errata documentation: warning: certain SEC interrupts
6738 + * are not fully cleared by writing the MCR:SWR bit,
6739 + * set bit twice to completely reset
6741 + err = reset_device(dev);
6745 + err = reset_device(dev);
6749 + /* reset channels */
6750 + for (ch = 0; ch < priv->num_channels; ch++) {
6751 + err = reset_channel(dev, ch);
6756 + /* enable channel done and error interrupts */
6757 + setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
6758 + setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
6764 + * talitos_submit - submits a descriptor to the device for processing
6765 + * @dev: the SEC device to be used
6766 + * @desc: the descriptor to be processed by the device
6767 + * @callback: whom to call when processing is complete
6768 + * @context: a handle for use by caller (optional)
6770 + * desc must contain valid dma-mapped (bus physical) address pointers.
6771 + * callback must check err and feedback in descriptor header
6772 + * for device processing status.
6774 +static int talitos_submit(struct device *dev, struct talitos_desc *desc,
6775 + void (*callback)(struct device *dev,
6776 + struct talitos_desc *desc,
6777 + void *context, int error),
6780 + struct talitos_private *priv = dev_get_drvdata(dev);
6781 + struct talitos_request *request;
6782 + unsigned long flags, ch;
6785 + /* select done notification */
6786 + desc->hdr |= DESC_HDR_DONE_NOTIFY;
6788 + /* emulate SEC's round-robin channel fifo polling scheme */
6789 + ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1);
6791 + spin_lock_irqsave(&priv->head_lock[ch], flags);
6793 + head = priv->head[ch];
6794 + request = &priv->fifo[ch][head];
6796 + if (request->desc) {
6797 + /* request queue is full */
6798 + spin_unlock_irqrestore(&priv->head_lock[ch], flags);
6802 + /* map descriptor and save caller data */
6803 + request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
6804 + DMA_BIDIRECTIONAL);
6805 + request->callback = callback;
6806 + request->context = context;
6808 + /* increment fifo head */
6809 + priv->head[ch] = (priv->head[ch] + 1) & (priv->fifo_len - 1);
6812 + request->desc = desc;
6816 + out_be32(priv->reg + TALITOS_FF_LO(ch), request->dma_desc);
6818 + spin_unlock_irqrestore(&priv->head_lock[ch], flags);
6820 + return -EINPROGRESS;
6824 + * process what was done, notify callback of error if not
6826 +static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
6828 + struct talitos_private *priv = dev_get_drvdata(dev);
6829 + struct talitos_request *request, saved_req;
6830 + unsigned long flags;
6833 + spin_lock_irqsave(&priv->tail_lock[ch], flags);
6835 + tail = priv->tail[ch];
6836 + while (priv->fifo[ch][tail].desc) {
6837 + request = &priv->fifo[ch][tail];
6839 + /* descriptors with their done bits set don't get the error */
6841 + if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
6849 + dma_unmap_single(dev, request->dma_desc,
6850 + sizeof(struct talitos_desc), DMA_BIDIRECTIONAL);
6852 + /* copy entries so we can call callback outside lock */
6853 + saved_req.desc = request->desc;
6854 + saved_req.callback = request->callback;
6855 + saved_req.context = request->context;
6857 + /* release request entry in fifo */
6859 + request->desc = NULL;
6861 + /* increment fifo tail */
6862 + priv->tail[ch] = (tail + 1) & (priv->fifo_len - 1);
6864 + spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
6865 + saved_req.callback(dev, saved_req.desc, saved_req.context,
6867 + /* channel may resume processing in single desc error case */
6868 + if (error && !reset_ch && status == error)
6870 + spin_lock_irqsave(&priv->tail_lock[ch], flags);
6871 + tail = priv->tail[ch];
6874 + spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
6878 + * process completed requests for channels that have done status
6880 +static void talitos_done(unsigned long data)
6882 + struct device *dev = (struct device *)data;
6883 + struct talitos_private *priv = dev_get_drvdata(dev);
6886 + for (ch = 0; ch < priv->num_channels; ch++)
6887 + flush_channel(dev, ch, 0, 0);
6891 + * locate current (offending) descriptor
6893 +static struct talitos_desc *current_desc(struct device *dev, int ch)
6895 + struct talitos_private *priv = dev_get_drvdata(dev);
6896 + int tail = priv->tail[ch];
6897 + dma_addr_t cur_desc;
6899 + cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
6901 + while (priv->fifo[ch][tail].dma_desc != cur_desc) {
6902 + tail = (tail + 1) & (priv->fifo_len - 1);
6903 + if (tail == priv->tail[ch]) {
6904 + dev_err(dev, "couldn't locate current descriptor\n");
6909 + return priv->fifo[ch][tail].desc;
6913 + * user diagnostics; report root cause of error based on execution unit status
6915 +static void report_eu_error(struct device *dev, int ch, struct talitos_desc *desc)
6917 + struct talitos_private *priv = dev_get_drvdata(dev);
6920 + switch (desc->hdr & DESC_HDR_SEL0_MASK) {
6921 + case DESC_HDR_SEL0_AFEU:
6922 + dev_err(dev, "AFEUISR 0x%08x_%08x\n",
6923 + in_be32(priv->reg + TALITOS_AFEUISR),
6924 + in_be32(priv->reg + TALITOS_AFEUISR_LO));
6926 + case DESC_HDR_SEL0_DEU:
6927 + dev_err(dev, "DEUISR 0x%08x_%08x\n",
6928 + in_be32(priv->reg + TALITOS_DEUISR),
6929 + in_be32(priv->reg + TALITOS_DEUISR_LO));
6931 + case DESC_HDR_SEL0_MDEUA:
6932 + case DESC_HDR_SEL0_MDEUB:
6933 + dev_err(dev, "MDEUISR 0x%08x_%08x\n",
6934 + in_be32(priv->reg + TALITOS_MDEUISR),
6935 + in_be32(priv->reg + TALITOS_MDEUISR_LO));
6937 + case DESC_HDR_SEL0_RNG:
6938 + dev_err(dev, "RNGUISR 0x%08x_%08x\n",
6939 + in_be32(priv->reg + TALITOS_RNGUISR),
6940 + in_be32(priv->reg + TALITOS_RNGUISR_LO));
6942 + case DESC_HDR_SEL0_PKEU:
6943 + dev_err(dev, "PKEUISR 0x%08x_%08x\n",
6944 + in_be32(priv->reg + TALITOS_PKEUISR),
6945 + in_be32(priv->reg + TALITOS_PKEUISR_LO));
6947 + case DESC_HDR_SEL0_AESU:
6948 + dev_err(dev, "AESUISR 0x%08x_%08x\n",
6949 + in_be32(priv->reg + TALITOS_AESUISR),
6950 + in_be32(priv->reg + TALITOS_AESUISR_LO));
6952 + case DESC_HDR_SEL0_CRCU:
6953 + dev_err(dev, "CRCUISR 0x%08x_%08x\n",
6954 + in_be32(priv->reg + TALITOS_CRCUISR),
6955 + in_be32(priv->reg + TALITOS_CRCUISR_LO));
6957 + case DESC_HDR_SEL0_KEU:
6958 + dev_err(dev, "KEUISR 0x%08x_%08x\n",
6959 + in_be32(priv->reg + TALITOS_KEUISR),
6960 + in_be32(priv->reg + TALITOS_KEUISR_LO));
6964 + switch (desc->hdr & DESC_HDR_SEL1_MASK) {
6965 + case DESC_HDR_SEL1_MDEUA:
6966 + case DESC_HDR_SEL1_MDEUB:
6967 + dev_err(dev, "MDEUISR 0x%08x_%08x\n",
6968 + in_be32(priv->reg + TALITOS_MDEUISR),
6969 + in_be32(priv->reg + TALITOS_MDEUISR_LO));
6971 + case DESC_HDR_SEL1_CRCU:
6972 + dev_err(dev, "CRCUISR 0x%08x_%08x\n",
6973 + in_be32(priv->reg + TALITOS_CRCUISR),
6974 + in_be32(priv->reg + TALITOS_CRCUISR_LO));
6978 + for (i = 0; i < 8; i++)
6979 + dev_err(dev, "DESCBUF 0x%08x_%08x\n",
6980 + in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
6981 + in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
6985 + * recover from error interrupts
6987 +static void talitos_error(unsigned long data)
6989 + struct device *dev = (struct device *)data;
6990 + struct talitos_private *priv = dev_get_drvdata(dev);
6991 + unsigned int timeout = TALITOS_TIMEOUT;
6992 + int ch, error, reset_dev = 0, reset_ch = 0;
6993 + u32 isr, isr_lo, v, v_lo;
6995 + isr = in_be32(priv->reg + TALITOS_ISR);
6996 + isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
6998 + for (ch = 0; ch < priv->num_channels; ch++) {
6999 + /* skip channels without errors */
7000 + if (!(isr & (1 << (ch * 2 + 1))))
7005 + v = in_be32(priv->reg + TALITOS_CCPSR(ch));
7006 + v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
7008 + if (v_lo & TALITOS_CCPSR_LO_DOF) {
7009 + dev_err(dev, "double fetch fifo overflow error\n");
7013 + if (v_lo & TALITOS_CCPSR_LO_SOF) {
7014 + /* h/w dropped descriptor */
7015 + dev_err(dev, "single fetch fifo overflow error\n");
7018 + if (v_lo & TALITOS_CCPSR_LO_MDTE)
7019 + dev_err(dev, "master data transfer error\n");
7020 + if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
7021 + dev_err(dev, "s/g data length zero error\n");
7022 + if (v_lo & TALITOS_CCPSR_LO_FPZ)
7023 + dev_err(dev, "fetch pointer zero error\n");
7024 + if (v_lo & TALITOS_CCPSR_LO_IDH)
7025 + dev_err(dev, "illegal descriptor header error\n");
7026 + if (v_lo & TALITOS_CCPSR_LO_IEU)
7027 + dev_err(dev, "invalid execution unit error\n");
7028 + if (v_lo & TALITOS_CCPSR_LO_EU)
7029 + report_eu_error(dev, ch, current_desc(dev, ch));
7030 + if (v_lo & TALITOS_CCPSR_LO_GB)
7031 + dev_err(dev, "gather boundary error\n");
7032 + if (v_lo & TALITOS_CCPSR_LO_GRL)
7033 + dev_err(dev, "gather return/length error\n");
7034 + if (v_lo & TALITOS_CCPSR_LO_SB)
7035 + dev_err(dev, "scatter boundary error\n");
7036 + if (v_lo & TALITOS_CCPSR_LO_SRL)
7037 + dev_err(dev, "scatter return/length error\n");
7039 + flush_channel(dev, ch, error, reset_ch);
7042 + reset_channel(dev, ch);
7044 + setbits32(priv->reg + TALITOS_CCCR(ch),
7045 + TALITOS_CCCR_CONT);
7046 + setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
7047 + while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
7048 + TALITOS_CCCR_CONT) && --timeout)
7050 + if (timeout == 0) {
7051 + dev_err(dev, "failed to restart channel %d\n",
7057 + if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
7058 + dev_err(dev, "done overflow, internal time out, or rngu error: "
7059 + "ISR 0x%08x_%08x\n", isr, isr_lo);
7061 + /* purge request queues */
7062 + for (ch = 0; ch < priv->num_channels; ch++)
7063 + flush_channel(dev, ch, -EIO, 1);
7065 + /* reset and reinitialize the device */
7070 +static irqreturn_t talitos_interrupt(int irq, void *data)
7072 + struct device *dev = data;
7073 + struct talitos_private *priv = dev_get_drvdata(dev);
7076 + isr = in_be32(priv->reg + TALITOS_ISR);
7077 + isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
7080 + out_be32(priv->reg + TALITOS_ICR, isr);
7081 + out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
7083 + if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
7084 + talitos_error((unsigned long)data);
7086 + if (likely(isr & TALITOS_ISR_CHDONE))
7087 + tasklet_schedule(&priv->done_task);
7089 + return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
7095 +static int talitos_rng_data_present(struct hwrng *rng, int wait)
7097 + struct device *dev = (struct device *)rng->priv;
7098 + struct talitos_private *priv = dev_get_drvdata(dev);
7102 + for (i = 0; i < 20; i++) {
7103 + ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
7104 + TALITOS_RNGUSR_LO_OFL;
7113 +static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
7115 + struct device *dev = (struct device *)rng->priv;
7116 + struct talitos_private *priv = dev_get_drvdata(dev);
7118 + /* rng fifo requires 64-bit accesses */
7119 + *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
7120 + *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
7122 + return sizeof(u32);
7125 +static int talitos_rng_init(struct hwrng *rng)
7127 + struct device *dev = (struct device *)rng->priv;
7128 + struct talitos_private *priv = dev_get_drvdata(dev);
7129 + unsigned int timeout = TALITOS_TIMEOUT;
7131 + setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
7132 + while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
7135 + if (timeout == 0) {
7136 + dev_err(dev, "failed to reset rng hw\n");
7140 + /* start generating */
7141 + setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
7146 +static int talitos_register_rng(struct device *dev)
7148 + struct talitos_private *priv = dev_get_drvdata(dev);
7150 + priv->rng.name = dev_driver_string(dev),
7151 + priv->rng.init = talitos_rng_init,
7152 + priv->rng.data_present = talitos_rng_data_present,
7153 + priv->rng.data_read = talitos_rng_data_read,
7154 + priv->rng.priv = (unsigned long)dev;
7156 + return hwrng_register(&priv->rng);
7159 +static void talitos_unregister_rng(struct device *dev)
7161 + struct talitos_private *priv = dev_get_drvdata(dev);
7163 + hwrng_unregister(&priv->rng);
7169 +#define TALITOS_CRA_PRIORITY 3000
7170 +#define TALITOS_MAX_KEY_SIZE 64
7171 +#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
7173 +#define MD5_DIGEST_SIZE 16
7175 +struct talitos_ctx {
7176 + struct device *dev;
7177 + __be32 desc_hdr_template;
7178 + u8 key[TALITOS_MAX_KEY_SIZE];
7179 + u8 iv[TALITOS_MAX_IV_LENGTH];
7180 + unsigned int keylen;
7181 + unsigned int enckeylen;
7182 + unsigned int authkeylen;
7183 + unsigned int authsize;
7186 +static int aead_authenc_setauthsize(struct crypto_aead *authenc,
7187 + unsigned int authsize)
7189 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7191 + ctx->authsize = authsize;
7196 +static int aead_authenc_setkey(struct crypto_aead *authenc,
7197 + const u8 *key, unsigned int keylen)
7199 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7200 + struct rtattr *rta = (void *)key;
7201 + struct crypto_authenc_key_param *param;
7202 + unsigned int authkeylen;
7203 + unsigned int enckeylen;
7205 + if (!RTA_OK(rta, keylen))
7208 + if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
7211 + if (RTA_PAYLOAD(rta) < sizeof(*param))
7214 + param = RTA_DATA(rta);
7215 + enckeylen = be32_to_cpu(param->enckeylen);
7217 + key += RTA_ALIGN(rta->rta_len);
7218 + keylen -= RTA_ALIGN(rta->rta_len);
7220 + if (keylen < enckeylen)
7223 + authkeylen = keylen - enckeylen;
7225 + if (keylen > TALITOS_MAX_KEY_SIZE)
7228 + memcpy(&ctx->key, key, keylen);
7230 + ctx->keylen = keylen;
7231 + ctx->enckeylen = enckeylen;
7232 + ctx->authkeylen = authkeylen;
7237 + crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
7242 + * ipsec_esp_edesc - s/w-extended ipsec_esp descriptor
7243 + * @src_nents: number of segments in input scatterlist
7244 + * @dst_nents: number of segments in output scatterlist
7245 + * @dma_len: length of dma mapped link_tbl space
7246 + * @dma_link_tbl: bus physical address of link_tbl
7247 + * @desc: h/w descriptor
7248 + * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
7250 + * if decrypting (with authcheck), or either one of src_nents or dst_nents
7251 + * is greater than 1, an integrity check value is concatenated to the end
7252 + * of link_tbl data
7254 +struct ipsec_esp_edesc {
7258 + dma_addr_t dma_link_tbl;
7259 + struct talitos_desc desc;
7260 + struct talitos_ptr link_tbl[0];
7263 +static void ipsec_esp_unmap(struct device *dev,
7264 + struct ipsec_esp_edesc *edesc,
7265 + struct aead_request *areq)
7267 + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
7268 + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
7269 + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
7270 + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
7272 + dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
7274 + if (areq->src != areq->dst) {
7275 + dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1,
7277 + dma_unmap_sg(dev, areq->dst, edesc->dst_nents ? : 1,
7280 + dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1,
7281 + DMA_BIDIRECTIONAL);
7284 + if (edesc->dma_len)
7285 + dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
7286 + DMA_BIDIRECTIONAL);
7290 + * ipsec_esp descriptor callbacks
7292 +static void ipsec_esp_encrypt_done(struct device *dev,
7293 + struct talitos_desc *desc, void *context,
7296 + struct aead_request *areq = context;
7297 + struct ipsec_esp_edesc *edesc =
7298 + container_of(desc, struct ipsec_esp_edesc, desc);
7299 + struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
7300 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7301 + struct scatterlist *sg;
7304 + ipsec_esp_unmap(dev, edesc, areq);
7306 + /* copy the generated ICV to dst */
7307 + if (edesc->dma_len) {
7308 + icvdata = &edesc->link_tbl[edesc->src_nents +
7309 + edesc->dst_nents + 1];
7310 + sg = sg_last(areq->dst, edesc->dst_nents);
7311 + memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
7312 + icvdata, ctx->authsize);
7317 + aead_request_complete(areq, err);
7320 +static void ipsec_esp_decrypt_done(struct device *dev,
7321 + struct talitos_desc *desc, void *context,
7324 + struct aead_request *req = context;
7325 + struct ipsec_esp_edesc *edesc =
7326 + container_of(desc, struct ipsec_esp_edesc, desc);
7327 + struct crypto_aead *authenc = crypto_aead_reqtfm(req);
7328 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7329 + struct scatterlist *sg;
7332 + ipsec_esp_unmap(dev, edesc, req);
7336 + if (edesc->dma_len)
7337 + icvdata = &edesc->link_tbl[edesc->src_nents +
7338 + edesc->dst_nents + 1];
7340 + icvdata = &edesc->link_tbl[0];
7342 + sg = sg_last(req->dst, edesc->dst_nents ? : 1);
7343 + err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
7344 + ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
7349 + aead_request_complete(req, err);
7353 + * convert scatterlist to SEC h/w link table format
7354 + * stop at cryptlen bytes
7356 +static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
7357 + int cryptlen, struct talitos_ptr *link_tbl_ptr)
7359 + int n_sg = sg_count;
7362 + link_tbl_ptr->ptr = cpu_to_be32(sg_dma_address(sg));
7363 + link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
7364 + link_tbl_ptr->j_extent = 0;
7366 + cryptlen -= sg_dma_len(sg);
7370 + /* adjust (decrease) last one (or two) entry's len to cryptlen */
7372 + while (link_tbl_ptr->len <= (-cryptlen)) {
7373 + /* Empty this entry, and move to previous one */
7374 + cryptlen += be16_to_cpu(link_tbl_ptr->len);
7375 + link_tbl_ptr->len = 0;
7379 + link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
7382 + /* tag end of link table */
7383 + link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
7389 + * fill in and submit ipsec_esp descriptor
7391 +static int ipsec_esp(struct ipsec_esp_edesc *edesc, struct aead_request *areq,
7393 + void (*callback) (struct device *dev,
7394 + struct talitos_desc *desc,
7395 + void *context, int error))
7397 + struct crypto_aead *aead = crypto_aead_reqtfm(areq);
7398 + struct talitos_ctx *ctx = crypto_aead_ctx(aead);
7399 + struct device *dev = ctx->dev;
7400 + struct talitos_desc *desc = &edesc->desc;
7401 + unsigned int cryptlen = areq->cryptlen;
7402 + unsigned int authsize = ctx->authsize;
7403 + unsigned int ivsize;
7407 + map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
7408 + 0, DMA_TO_DEVICE);
7410 + map_single_talitos_ptr(dev, &desc->ptr[1], sg_virt(areq->src) -
7411 + sg_virt(areq->assoc), sg_virt(areq->assoc), 0,
7414 + ivsize = crypto_aead_ivsize(aead);
7415 + map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
7419 + map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
7420 + (char *)&ctx->key + ctx->authkeylen, 0,
7425 + * map and adjust cipher len to aead request cryptlen.
7426 + * extent is bytes of HMAC postpended to ciphertext,
7427 + * typically 12 for ipsec
7429 + desc->ptr[4].len = cpu_to_be16(cryptlen);
7430 + desc->ptr[4].j_extent = authsize;
7432 + if (areq->src == areq->dst)
7433 + sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1,
7434 + DMA_BIDIRECTIONAL);
7436 + sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1,
7439 + if (sg_count == 1) {
7440 + desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
7442 + sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
7443 + &edesc->link_tbl[0]);
7444 + if (sg_count > 1) {
7445 + desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
7446 + desc->ptr[4].ptr = cpu_to_be32(edesc->dma_link_tbl);
7447 + dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
7448 + edesc->dma_len, DMA_BIDIRECTIONAL);
7450 + /* Only one segment now, so no link tbl needed */
7451 + desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
7456 + desc->ptr[5].len = cpu_to_be16(cryptlen);
7457 + desc->ptr[5].j_extent = authsize;
7459 + if (areq->src != areq->dst) {
7460 + sg_count = dma_map_sg(dev, areq->dst, edesc->dst_nents ? : 1,
7464 + if (sg_count == 1) {
7465 + desc->ptr[5].ptr = cpu_to_be32(sg_dma_address(areq->dst));
7467 + struct talitos_ptr *link_tbl_ptr =
7468 + &edesc->link_tbl[edesc->src_nents];
7469 + struct scatterlist *sg;
7471 + desc->ptr[5].ptr = cpu_to_be32((struct talitos_ptr *)
7472 + edesc->dma_link_tbl +
7473 + edesc->src_nents);
7474 + if (areq->src == areq->dst) {
7475 + memcpy(link_tbl_ptr, &edesc->link_tbl[0],
7476 + edesc->src_nents * sizeof(struct talitos_ptr));
7478 + sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
7481 + link_tbl_ptr += sg_count - 1;
7483 + /* handle case where sg_last contains the ICV exclusively */
7484 + sg = sg_last(areq->dst, edesc->dst_nents);
7485 + if (sg->length == ctx->authsize)
7488 + link_tbl_ptr->j_extent = 0;
7490 + link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
7491 + link_tbl_ptr->len = cpu_to_be16(authsize);
7493 + /* icv data follows link tables */
7494 + link_tbl_ptr->ptr = cpu_to_be32((struct talitos_ptr *)
7495 + edesc->dma_link_tbl +
7496 + edesc->src_nents +
7497 + edesc->dst_nents + 1);
7499 + desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
7500 + dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
7501 + edesc->dma_len, DMA_BIDIRECTIONAL);
7505 + map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
7508 + return talitos_submit(dev, desc, callback, areq);
7513 + * derive number of elements in scatterlist
7515 +static int sg_count(struct scatterlist *sg_list, int nbytes)
7517 + struct scatterlist *sg = sg_list;
7522 + nbytes -= sg->length;
7530 + * allocate and map the ipsec_esp extended descriptor
7532 +static struct ipsec_esp_edesc *ipsec_esp_edesc_alloc(struct aead_request *areq,
7535 + struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
7536 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7537 + struct ipsec_esp_edesc *edesc;
7538 + int src_nents, dst_nents, alloc_len, dma_len;
7540 + if (areq->cryptlen + ctx->authsize > TALITOS_MAX_DATA_LEN) {
7541 + dev_err(ctx->dev, "cryptlen exceeds h/w max limit\n");
7542 + return ERR_PTR(-EINVAL);
7545 + src_nents = sg_count(areq->src, areq->cryptlen + ctx->authsize);
7546 + src_nents = (src_nents == 1) ? 0 : src_nents;
7548 + if (areq->dst == areq->src) {
7549 + dst_nents = src_nents;
7551 + dst_nents = sg_count(areq->dst, areq->cryptlen + ctx->authsize);
7552 + dst_nents = (dst_nents == 1) ? 0 : src_nents;
7556 + * allocate space for base edesc plus the link tables,
7557 + * allowing for a separate entry for the generated ICV (+ 1),
7558 + * and the ICV data itself
7560 + alloc_len = sizeof(struct ipsec_esp_edesc);
7561 + if (src_nents || dst_nents) {
7562 + dma_len = (src_nents + dst_nents + 1) *
7563 + sizeof(struct talitos_ptr) + ctx->authsize;
7564 + alloc_len += dma_len;
7567 + alloc_len += icv_stashing ? ctx->authsize : 0;
7570 + edesc = kmalloc(alloc_len, GFP_DMA);
7572 + dev_err(ctx->dev, "could not allocate edescriptor\n");
7573 + return ERR_PTR(-ENOMEM);
7576 + edesc->src_nents = src_nents;
7577 + edesc->dst_nents = dst_nents;
7578 + edesc->dma_len = dma_len;
7579 + edesc->dma_link_tbl = dma_map_single(ctx->dev, &edesc->link_tbl[0],
7580 + edesc->dma_len, DMA_BIDIRECTIONAL);
7585 +static int aead_authenc_encrypt(struct aead_request *req)
7587 + struct crypto_aead *authenc = crypto_aead_reqtfm(req);
7588 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7589 + struct ipsec_esp_edesc *edesc;
7591 + /* allocate extended descriptor */
7592 + edesc = ipsec_esp_edesc_alloc(req, 0);
7593 + if (IS_ERR(edesc))
7594 + return PTR_ERR(edesc);
7597 + edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
7599 + return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
7602 +static int aead_authenc_decrypt(struct aead_request *req)
7604 + struct crypto_aead *authenc = crypto_aead_reqtfm(req);
7605 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7606 + unsigned int authsize = ctx->authsize;
7607 + struct ipsec_esp_edesc *edesc;
7608 + struct scatterlist *sg;
7611 + req->cryptlen -= authsize;
7613 + /* allocate extended descriptor */
7614 + edesc = ipsec_esp_edesc_alloc(req, 1);
7615 + if (IS_ERR(edesc))
7616 + return PTR_ERR(edesc);
7618 + /* stash incoming ICV for later cmp with ICV generated by the h/w */
7619 + if (edesc->dma_len)
7620 + icvdata = &edesc->link_tbl[edesc->src_nents +
7621 + edesc->dst_nents + 1];
7623 + icvdata = &edesc->link_tbl[0];
7625 + sg = sg_last(req->src, edesc->src_nents ? : 1);
7627 + memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
7631 + edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
7633 + return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_done);
7636 +static int aead_authenc_givencrypt(
7637 + struct aead_givcrypt_request *req)
7639 + struct aead_request *areq = &req->areq;
7640 + struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
7641 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7642 + struct ipsec_esp_edesc *edesc;
7644 + /* allocate extended descriptor */
7645 + edesc = ipsec_esp_edesc_alloc(areq, 0);
7646 + if (IS_ERR(edesc))
7647 + return PTR_ERR(edesc);
7650 + edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
7652 + memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
7654 + return ipsec_esp(edesc, areq, req->giv, req->seq,
7655 + ipsec_esp_encrypt_done);
7658 +struct talitos_alg_template {
7659 + char name[CRYPTO_MAX_ALG_NAME];
7660 + char driver_name[CRYPTO_MAX_ALG_NAME];
7661 + unsigned int blocksize;
7662 + struct aead_alg aead;
7663 + struct device *dev;
7664 + __be32 desc_hdr_template;
7667 +static struct talitos_alg_template driver_algs[] = {
7668 + /* single-pass ipsec_esp descriptor */
7670 + .name = "authenc(hmac(sha1),cbc(aes))",
7671 + .driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
7672 + .blocksize = AES_BLOCK_SIZE,
7674 + .setkey = aead_authenc_setkey,
7675 + .setauthsize = aead_authenc_setauthsize,
7676 + .encrypt = aead_authenc_encrypt,
7677 + .decrypt = aead_authenc_decrypt,
7678 + .givencrypt = aead_authenc_givencrypt,
7679 + .geniv = "<built-in>",
7680 + .ivsize = AES_BLOCK_SIZE,
7681 + .maxauthsize = SHA1_DIGEST_SIZE,
7683 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7684 + DESC_HDR_SEL0_AESU |
7685 + DESC_HDR_MODE0_AESU_CBC |
7686 + DESC_HDR_SEL1_MDEUA |
7687 + DESC_HDR_MODE1_MDEU_INIT |
7688 + DESC_HDR_MODE1_MDEU_PAD |
7689 + DESC_HDR_MODE1_MDEU_SHA1_HMAC,
7692 + .name = "authenc(hmac(sha1),cbc(des3_ede))",
7693 + .driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
7694 + .blocksize = DES3_EDE_BLOCK_SIZE,
7696 + .setkey = aead_authenc_setkey,
7697 + .setauthsize = aead_authenc_setauthsize,
7698 + .encrypt = aead_authenc_encrypt,
7699 + .decrypt = aead_authenc_decrypt,
7700 + .givencrypt = aead_authenc_givencrypt,
7701 + .geniv = "<built-in>",
7702 + .ivsize = DES3_EDE_BLOCK_SIZE,
7703 + .maxauthsize = SHA1_DIGEST_SIZE,
7705 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7706 + DESC_HDR_SEL0_DEU |
7707 + DESC_HDR_MODE0_DEU_CBC |
7708 + DESC_HDR_MODE0_DEU_3DES |
7709 + DESC_HDR_SEL1_MDEUA |
7710 + DESC_HDR_MODE1_MDEU_INIT |
7711 + DESC_HDR_MODE1_MDEU_PAD |
7712 + DESC_HDR_MODE1_MDEU_SHA1_HMAC,
7715 + .name = "authenc(hmac(sha256),cbc(aes))",
7716 + .driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
7717 + .blocksize = AES_BLOCK_SIZE,
7719 + .setkey = aead_authenc_setkey,
7720 + .setauthsize = aead_authenc_setauthsize,
7721 + .encrypt = aead_authenc_encrypt,
7722 + .decrypt = aead_authenc_decrypt,
7723 + .givencrypt = aead_authenc_givencrypt,
7724 + .geniv = "<built-in>",
7725 + .ivsize = AES_BLOCK_SIZE,
7726 + .maxauthsize = SHA256_DIGEST_SIZE,
7728 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7729 + DESC_HDR_SEL0_AESU |
7730 + DESC_HDR_MODE0_AESU_CBC |
7731 + DESC_HDR_SEL1_MDEUA |
7732 + DESC_HDR_MODE1_MDEU_INIT |
7733 + DESC_HDR_MODE1_MDEU_PAD |
7734 + DESC_HDR_MODE1_MDEU_SHA256_HMAC,
7737 + .name = "authenc(hmac(sha256),cbc(des3_ede))",
7738 + .driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
7739 + .blocksize = DES3_EDE_BLOCK_SIZE,
7741 + .setkey = aead_authenc_setkey,
7742 + .setauthsize = aead_authenc_setauthsize,
7743 + .encrypt = aead_authenc_encrypt,
7744 + .decrypt = aead_authenc_decrypt,
7745 + .givencrypt = aead_authenc_givencrypt,
7746 + .geniv = "<built-in>",
7747 + .ivsize = DES3_EDE_BLOCK_SIZE,
7748 + .maxauthsize = SHA256_DIGEST_SIZE,
7750 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7751 + DESC_HDR_SEL0_DEU |
7752 + DESC_HDR_MODE0_DEU_CBC |
7753 + DESC_HDR_MODE0_DEU_3DES |
7754 + DESC_HDR_SEL1_MDEUA |
7755 + DESC_HDR_MODE1_MDEU_INIT |
7756 + DESC_HDR_MODE1_MDEU_PAD |
7757 + DESC_HDR_MODE1_MDEU_SHA256_HMAC,
7760 + .name = "authenc(hmac(md5),cbc(aes))",
7761 + .driver_name = "authenc-hmac-md5-cbc-aes-talitos",
7762 + .blocksize = AES_BLOCK_SIZE,
7764 + .setkey = aead_authenc_setkey,
7765 + .setauthsize = aead_authenc_setauthsize,
7766 + .encrypt = aead_authenc_encrypt,
7767 + .decrypt = aead_authenc_decrypt,
7768 + .givencrypt = aead_authenc_givencrypt,
7769 + .geniv = "<built-in>",
7770 + .ivsize = AES_BLOCK_SIZE,
7771 + .maxauthsize = MD5_DIGEST_SIZE,
7773 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7774 + DESC_HDR_SEL0_AESU |
7775 + DESC_HDR_MODE0_AESU_CBC |
7776 + DESC_HDR_SEL1_MDEUA |
7777 + DESC_HDR_MODE1_MDEU_INIT |
7778 + DESC_HDR_MODE1_MDEU_PAD |
7779 + DESC_HDR_MODE1_MDEU_MD5_HMAC,
7782 + .name = "authenc(hmac(md5),cbc(des3_ede))",
7783 + .driver_name = "authenc-hmac-md5-cbc-3des-talitos",
7784 + .blocksize = DES3_EDE_BLOCK_SIZE,
7786 + .setkey = aead_authenc_setkey,
7787 + .setauthsize = aead_authenc_setauthsize,
7788 + .encrypt = aead_authenc_encrypt,
7789 + .decrypt = aead_authenc_decrypt,
7790 + .givencrypt = aead_authenc_givencrypt,
7791 + .geniv = "<built-in>",
7792 + .ivsize = DES3_EDE_BLOCK_SIZE,
7793 + .maxauthsize = MD5_DIGEST_SIZE,
7795 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7796 + DESC_HDR_SEL0_DEU |
7797 + DESC_HDR_MODE0_DEU_CBC |
7798 + DESC_HDR_MODE0_DEU_3DES |
7799 + DESC_HDR_SEL1_MDEUA |
7800 + DESC_HDR_MODE1_MDEU_INIT |
7801 + DESC_HDR_MODE1_MDEU_PAD |
7802 + DESC_HDR_MODE1_MDEU_MD5_HMAC,
7806 +struct talitos_crypto_alg {
7807 + struct list_head entry;
7808 + struct device *dev;
7809 + __be32 desc_hdr_template;
7810 + struct crypto_alg crypto_alg;
7813 +static int talitos_cra_init(struct crypto_tfm *tfm)
7815 + struct crypto_alg *alg = tfm->__crt_alg;
7816 + struct talitos_crypto_alg *talitos_alg =
7817 + container_of(alg, struct talitos_crypto_alg, crypto_alg);
7818 + struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
7820 + /* update context with ptr to dev */
7821 + ctx->dev = talitos_alg->dev;
7822 + /* copy descriptor header template value */
7823 + ctx->desc_hdr_template = talitos_alg->desc_hdr_template;
7825 + /* random first IV */
7826 + get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
7832 + * given the alg's descriptor header template, determine whether descriptor
7833 + * type and primary/secondary execution units required match the hw
7834 + * capabilities description provided in the device tree node.
7836 +static int hw_supports(struct device *dev, __be32 desc_hdr_template)
7838 + struct talitos_private *priv = dev_get_drvdata(dev);
7841 + ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
7842 + (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
7844 + if (SECONDARY_EU(desc_hdr_template))
7845 + ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
7846 + & priv->exec_units);
7851 +static int __devexit talitos_remove(struct of_device *ofdev)
7853 + struct device *dev = &ofdev->dev;
7854 + struct talitos_private *priv = dev_get_drvdata(dev);
7855 + struct talitos_crypto_alg *t_alg, *n;
7858 + list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
7859 + crypto_unregister_alg(&t_alg->crypto_alg);
7860 + list_del(&t_alg->entry);
7864 + if (hw_supports(dev, DESC_HDR_SEL0_RNG))
7865 + talitos_unregister_rng(dev);
7867 + kfree(priv->tail);
7868 + kfree(priv->head);
7871 + for (i = 0; i < priv->num_channels; i++)
7872 + kfree(priv->fifo[i]);
7874 + kfree(priv->fifo);
7875 + kfree(priv->head_lock);
7876 + kfree(priv->tail_lock);
7878 + if (priv->irq != NO_IRQ) {
7879 + free_irq(priv->irq, dev);
7880 + irq_dispose_mapping(priv->irq);
7883 + tasklet_kill(&priv->done_task);
7884 + tasklet_kill(&priv->error_task);
7886 + iounmap(priv->reg);
7888 + dev_set_drvdata(dev, NULL);
7895 +static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
7896 + struct talitos_alg_template
7899 + struct talitos_crypto_alg *t_alg;
7900 + struct crypto_alg *alg;
7902 + t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
7904 + return ERR_PTR(-ENOMEM);
7906 + alg = &t_alg->crypto_alg;
7908 + snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
7909 + snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
7910 + template->driver_name);
7911 + alg->cra_module = THIS_MODULE;
7912 + alg->cra_init = talitos_cra_init;
7913 + alg->cra_priority = TALITOS_CRA_PRIORITY;
7914 + alg->cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
7915 + alg->cra_blocksize = template->blocksize;
7916 + alg->cra_alignmask = 0;
7917 + alg->cra_type = &crypto_aead_type;
7918 + alg->cra_ctxsize = sizeof(struct talitos_ctx);
7919 + alg->cra_u.aead = template->aead;
7921 + t_alg->desc_hdr_template = template->desc_hdr_template;
7927 +static int talitos_probe(struct of_device *ofdev,
7928 + const struct of_device_id *match)
7930 + struct device *dev = &ofdev->dev;
7931 + struct device_node *np = ofdev->node;
7932 + struct talitos_private *priv;
7933 + const unsigned int *prop;
7936 + priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
7940 + dev_set_drvdata(dev, priv);
7942 + priv->ofdev = ofdev;
7944 + tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
7945 + tasklet_init(&priv->error_task, talitos_error, (unsigned long)dev);
7947 + priv->irq = irq_of_parse_and_map(np, 0);
7949 + if (priv->irq == NO_IRQ) {
7950 + dev_err(dev, "failed to map irq\n");
7955 + /* get the irq line */
7956 + err = request_irq(priv->irq, talitos_interrupt, 0,
7957 + dev_driver_string(dev), dev);
7959 + dev_err(dev, "failed to request irq %d\n", priv->irq);
7960 + irq_dispose_mapping(priv->irq);
7961 + priv->irq = NO_IRQ;
7965 + priv->reg = of_iomap(np, 0);
7967 + dev_err(dev, "failed to of_iomap\n");
7972 + /* get SEC version capabilities from device tree */
7973 + prop = of_get_property(np, "fsl,num-channels", NULL);
7975 + priv->num_channels = *prop;
7977 + prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
7979 + priv->chfifo_len = *prop;
7981 + prop = of_get_property(np, "fsl,exec-units-mask", NULL);
7983 + priv->exec_units = *prop;
7985 + prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
7987 + priv->desc_types = *prop;
7989 + if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
7990 + !priv->exec_units || !priv->desc_types) {
7991 + dev_err(dev, "invalid property data in device tree node\n");
7999 + priv->head_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
8001 + priv->tail_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
8003 + if (!priv->head_lock || !priv->tail_lock) {
8004 + dev_err(dev, "failed to allocate fifo locks\n");
8009 + for (i = 0; i < priv->num_channels; i++) {
8010 + spin_lock_init(&priv->head_lock[i]);
8011 + spin_lock_init(&priv->tail_lock[i]);
8014 + priv->fifo = kmalloc(sizeof(struct talitos_request *) *
8015 + priv->num_channels, GFP_KERNEL);
8016 + if (!priv->fifo) {
8017 + dev_err(dev, "failed to allocate request fifo\n");
8022 + priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
8024 + for (i = 0; i < priv->num_channels; i++) {
8025 + priv->fifo[i] = kzalloc(sizeof(struct talitos_request) *
8026 + priv->fifo_len, GFP_KERNEL);
8027 + if (!priv->fifo[i]) {
8028 + dev_err(dev, "failed to allocate request fifo %d\n", i);
8034 + priv->head = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
8035 + priv->tail = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
8036 + if (!priv->head || !priv->tail) {
8037 + dev_err(dev, "failed to allocate request index space\n");
8042 + /* reset and initialize the h/w */
8043 + err = init_device(dev);
8045 + dev_err(dev, "failed to initialize device\n");
8049 + /* register the RNG, if available */
8050 + if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
8051 + err = talitos_register_rng(dev);
8053 + dev_err(dev, "failed to register hwrng: %d\n", err);
8056 + dev_info(dev, "hwrng\n");
8059 + /* register crypto algorithms the device supports */
8060 + INIT_LIST_HEAD(&priv->alg_list);
8062 + for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
8063 + if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
8064 + struct talitos_crypto_alg *t_alg;
8066 + t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
8067 + if (IS_ERR(t_alg)) {
8068 + err = PTR_ERR(t_alg);
8072 + err = crypto_register_alg(&t_alg->crypto_alg);
8074 + dev_err(dev, "%s alg registration failed\n",
8075 + t_alg->crypto_alg.cra_driver_name);
8078 + list_add_tail(&t_alg->entry, &priv->alg_list);
8079 + dev_info(dev, "%s\n",
8080 + t_alg->crypto_alg.cra_driver_name);
8088 + talitos_remove(ofdev);
8095 +static struct of_device_id talitos_match[] = {
8097 + .compatible = "fsl,sec2.0",
8101 +MODULE_DEVICE_TABLE(of, talitos_match);
8103 +static struct of_platform_driver talitos_driver = {
8104 + .name = "talitos",
8105 + .match_table = talitos_match,
8106 + .probe = talitos_probe,
8107 + .remove = __devexit_p(talitos_remove),
8110 +static int __init talitos_init(void)
8112 + return of_register_platform_driver(&talitos_driver);
8114 +module_init(talitos_init);
8116 +static void __exit talitos_exit(void)
8118 + of_unregister_platform_driver(&talitos_driver);
8120 +module_exit(talitos_exit);
8122 +MODULE_LICENSE("GPL");
8123 +MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
8124 +MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
8126 +++ b/drivers/crypto/talitos.h
8129 + * Freescale SEC (talitos) device register and descriptor header defines
8131 + * Copyright (c) 2006-2008 Freescale Semiconductor, Inc.
8133 + * Redistribution and use in source and binary forms, with or without
8134 + * modification, are permitted provided that the following conditions
8137 + * 1. Redistributions of source code must retain the above copyright
8138 + * notice, this list of conditions and the following disclaimer.
8139 + * 2. Redistributions in binary form must reproduce the above copyright
8140 + * notice, this list of conditions and the following disclaimer in the
8141 + * documentation and/or other materials provided with the distribution.
8142 + * 3. The name of the author may not be used to endorse or promote products
8143 + * derived from this software without specific prior written permission.
8145 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
8146 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8147 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
8148 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
8149 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
8150 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8151 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8152 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8153 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
8154 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8159 + * TALITOS_xxx_LO addresses point to the low data bits (32-63) of the register
8162 +/* global register offset addresses */
8163 +#define TALITOS_MCR 0x1030 /* master control register */
8164 +#define TALITOS_MCR_LO 0x1038
8165 +#define TALITOS_MCR_SWR 0x1 /* s/w reset */
8166 +#define TALITOS_IMR 0x1008 /* interrupt mask register */
8167 +#define TALITOS_IMR_INIT 0x10fff /* enable channel IRQs */
8168 +#define TALITOS_IMR_LO 0x100C
8169 +#define TALITOS_IMR_LO_INIT 0x20000 /* allow RNGU error IRQs */
8170 +#define TALITOS_ISR 0x1010 /* interrupt status register */
8171 +#define TALITOS_ISR_CHERR 0xaa /* channel errors mask */
8172 +#define TALITOS_ISR_CHDONE 0x55 /* channel done mask */
8173 +#define TALITOS_ISR_LO 0x1014
8174 +#define TALITOS_ICR 0x1018 /* interrupt clear register */
8175 +#define TALITOS_ICR_LO 0x101C
8177 +/* channel register address stride */
8178 +#define TALITOS_CH_STRIDE 0x100
8180 +/* channel configuration register */
8181 +#define TALITOS_CCCR(ch) (ch * TALITOS_CH_STRIDE + 0x1108)
8182 +#define TALITOS_CCCR_CONT 0x2 /* channel continue */
8183 +#define TALITOS_CCCR_RESET 0x1 /* channel reset */
8184 +#define TALITOS_CCCR_LO(ch) (ch * TALITOS_CH_STRIDE + 0x110c)
8185 +#define TALITOS_CCCR_LO_CDWE 0x10 /* chan. done writeback enab. */
8186 +#define TALITOS_CCCR_LO_NT 0x4 /* notification type */
8187 +#define TALITOS_CCCR_LO_CDIE 0x2 /* channel done IRQ enable */
8189 +/* CCPSR: channel pointer status register */
8190 +#define TALITOS_CCPSR(ch) (ch * TALITOS_CH_STRIDE + 0x1110)
8191 +#define TALITOS_CCPSR_LO(ch) (ch * TALITOS_CH_STRIDE + 0x1114)
8192 +#define TALITOS_CCPSR_LO_DOF 0x8000 /* double FF write oflow error */
8193 +#define TALITOS_CCPSR_LO_SOF 0x4000 /* single FF write oflow error */
8194 +#define TALITOS_CCPSR_LO_MDTE 0x2000 /* master data transfer error */
8195 +#define TALITOS_CCPSR_LO_SGDLZ 0x1000 /* s/g data len zero error */
8196 +#define TALITOS_CCPSR_LO_FPZ 0x0800 /* fetch ptr zero error */
8197 +#define TALITOS_CCPSR_LO_IDH 0x0400 /* illegal desc hdr error */
8198 +#define TALITOS_CCPSR_LO_IEU 0x0200 /* invalid EU error */
8199 +#define TALITOS_CCPSR_LO_EU 0x0100 /* EU error detected */
8200 +#define TALITOS_CCPSR_LO_GB 0x0080 /* gather boundary error */
8201 +#define TALITOS_CCPSR_LO_GRL 0x0040 /* gather return/length error */
8202 +#define TALITOS_CCPSR_LO_SB 0x0020 /* scatter boundary error */
8203 +#define TALITOS_CCPSR_LO_SRL 0x0010 /* scatter return/length error */
8205 +/* channel fetch fifo register */
8206 +#define TALITOS_FF(ch) (ch * TALITOS_CH_STRIDE + 0x1148)
8207 +#define TALITOS_FF_LO(ch) (ch * TALITOS_CH_STRIDE + 0x114c)
8209 +/* current descriptor pointer register */
8210 +#define TALITOS_CDPR(ch) (ch * TALITOS_CH_STRIDE + 0x1140)
8211 +#define TALITOS_CDPR_LO(ch) (ch * TALITOS_CH_STRIDE + 0x1144)
8213 +/* descriptor buffer register */
8214 +#define TALITOS_DESCBUF(ch) (ch * TALITOS_CH_STRIDE + 0x1180)
8215 +#define TALITOS_DESCBUF_LO(ch) (ch * TALITOS_CH_STRIDE + 0x1184)
8217 +/* gather link table */
8218 +#define TALITOS_GATHER(ch) (ch * TALITOS_CH_STRIDE + 0x11c0)
8219 +#define TALITOS_GATHER_LO(ch) (ch * TALITOS_CH_STRIDE + 0x11c4)
8221 +/* scatter link table */
8222 +#define TALITOS_SCATTER(ch) (ch * TALITOS_CH_STRIDE + 0x11e0)
8223 +#define TALITOS_SCATTER_LO(ch) (ch * TALITOS_CH_STRIDE + 0x11e4)
8225 +/* execution unit interrupt status registers */
8226 +#define TALITOS_DEUISR 0x2030 /* DES unit */
8227 +#define TALITOS_DEUISR_LO 0x2034
8228 +#define TALITOS_AESUISR 0x4030 /* AES unit */
8229 +#define TALITOS_AESUISR_LO 0x4034
8230 +#define TALITOS_MDEUISR 0x6030 /* message digest unit */
8231 +#define TALITOS_MDEUISR_LO 0x6034
8232 +#define TALITOS_AFEUISR 0x8030 /* arc4 unit */
8233 +#define TALITOS_AFEUISR_LO 0x8034
8234 +#define TALITOS_RNGUISR 0xa030 /* random number unit */
8235 +#define TALITOS_RNGUISR_LO 0xa034
8236 +#define TALITOS_RNGUSR 0xa028 /* rng status */
8237 +#define TALITOS_RNGUSR_LO 0xa02c
8238 +#define TALITOS_RNGUSR_LO_RD 0x1 /* reset done */
8239 +#define TALITOS_RNGUSR_LO_OFL 0xff0000/* output FIFO length */
8240 +#define TALITOS_RNGUDSR 0xa010 /* data size */
8241 +#define TALITOS_RNGUDSR_LO 0xa014
8242 +#define TALITOS_RNGU_FIFO 0xa800 /* output FIFO */
8243 +#define TALITOS_RNGU_FIFO_LO 0xa804 /* output FIFO */
8244 +#define TALITOS_RNGURCR 0xa018 /* reset control */
8245 +#define TALITOS_RNGURCR_LO 0xa01c
8246 +#define TALITOS_RNGURCR_LO_SR 0x1 /* software reset */
8247 +#define TALITOS_PKEUISR 0xc030 /* public key unit */
8248 +#define TALITOS_PKEUISR_LO 0xc034
8249 +#define TALITOS_KEUISR 0xe030 /* kasumi unit */
8250 +#define TALITOS_KEUISR_LO 0xe034
8251 +#define TALITOS_CRCUISR 0xf030 /* cyclic redundancy check unit*/
8252 +#define TALITOS_CRCUISR_LO 0xf034
8255 + * talitos descriptor header (hdr) bits
8258 +/* written back when done */
8259 +#define DESC_HDR_DONE __constant_cpu_to_be32(0xff000000)
8261 +/* primary execution unit select */
8262 +#define DESC_HDR_SEL0_MASK __constant_cpu_to_be32(0xf0000000)
8263 +#define DESC_HDR_SEL0_AFEU __constant_cpu_to_be32(0x10000000)
8264 +#define DESC_HDR_SEL0_DEU __constant_cpu_to_be32(0x20000000)
8265 +#define DESC_HDR_SEL0_MDEUA __constant_cpu_to_be32(0x30000000)
8266 +#define DESC_HDR_SEL0_MDEUB __constant_cpu_to_be32(0xb0000000)
8267 +#define DESC_HDR_SEL0_RNG __constant_cpu_to_be32(0x40000000)
8268 +#define DESC_HDR_SEL0_PKEU __constant_cpu_to_be32(0x50000000)
8269 +#define DESC_HDR_SEL0_AESU __constant_cpu_to_be32(0x60000000)
8270 +#define DESC_HDR_SEL0_KEU __constant_cpu_to_be32(0x70000000)
8271 +#define DESC_HDR_SEL0_CRCU __constant_cpu_to_be32(0x80000000)
8273 +/* primary execution unit mode (MODE0) and derivatives */
8274 +#define DESC_HDR_MODE0_ENCRYPT __constant_cpu_to_be32(0x00100000)
8275 +#define DESC_HDR_MODE0_AESU_CBC __constant_cpu_to_be32(0x00200000)
8276 +#define DESC_HDR_MODE0_DEU_CBC __constant_cpu_to_be32(0x00400000)
8277 +#define DESC_HDR_MODE0_DEU_3DES __constant_cpu_to_be32(0x00200000)
8278 +#define DESC_HDR_MODE0_MDEU_INIT __constant_cpu_to_be32(0x01000000)
8279 +#define DESC_HDR_MODE0_MDEU_HMAC __constant_cpu_to_be32(0x00800000)
8280 +#define DESC_HDR_MODE0_MDEU_PAD __constant_cpu_to_be32(0x00400000)
8281 +#define DESC_HDR_MODE0_MDEU_MD5 __constant_cpu_to_be32(0x00200000)
8282 +#define DESC_HDR_MODE0_MDEU_SHA256 __constant_cpu_to_be32(0x00100000)
8283 +#define DESC_HDR_MODE0_MDEU_SHA1 __constant_cpu_to_be32(0x00000000)
8284 +#define DESC_HDR_MODE0_MDEU_MD5_HMAC (DESC_HDR_MODE0_MDEU_MD5 | \
8285 + DESC_HDR_MODE0_MDEU_HMAC)
8286 +#define DESC_HDR_MODE0_MDEU_SHA256_HMAC (DESC_HDR_MODE0_MDEU_SHA256 | \
8287 + DESC_HDR_MODE0_MDEU_HMAC)
8288 +#define DESC_HDR_MODE0_MDEU_SHA1_HMAC (DESC_HDR_MODE0_MDEU_SHA1 | \
8289 + DESC_HDR_MODE0_MDEU_HMAC)
8291 +/* secondary execution unit select (SEL1) */
8292 +#define DESC_HDR_SEL1_MASK __constant_cpu_to_be32(0x000f0000)
8293 +#define DESC_HDR_SEL1_MDEUA __constant_cpu_to_be32(0x00030000)
8294 +#define DESC_HDR_SEL1_MDEUB __constant_cpu_to_be32(0x000b0000)
8295 +#define DESC_HDR_SEL1_CRCU __constant_cpu_to_be32(0x00080000)
8297 +/* secondary execution unit mode (MODE1) and derivatives */
8298 +#define DESC_HDR_MODE1_MDEU_INIT __constant_cpu_to_be32(0x00001000)
8299 +#define DESC_HDR_MODE1_MDEU_HMAC __constant_cpu_to_be32(0x00000800)
8300 +#define DESC_HDR_MODE1_MDEU_PAD __constant_cpu_to_be32(0x00000400)
8301 +#define DESC_HDR_MODE1_MDEU_MD5 __constant_cpu_to_be32(0x00000200)
8302 +#define DESC_HDR_MODE1_MDEU_SHA256 __constant_cpu_to_be32(0x00000100)
8303 +#define DESC_HDR_MODE1_MDEU_SHA1 __constant_cpu_to_be32(0x00000000)
8304 +#define DESC_HDR_MODE1_MDEU_MD5_HMAC (DESC_HDR_MODE1_MDEU_MD5 | \
8305 + DESC_HDR_MODE1_MDEU_HMAC)
8306 +#define DESC_HDR_MODE1_MDEU_SHA256_HMAC (DESC_HDR_MODE1_MDEU_SHA256 | \
8307 + DESC_HDR_MODE1_MDEU_HMAC)
8308 +#define DESC_HDR_MODE1_MDEU_SHA1_HMAC (DESC_HDR_MODE1_MDEU_SHA1 | \
8309 + DESC_HDR_MODE1_MDEU_HMAC)
8311 +/* direction of overall data flow (DIR) */
8312 +#define DESC_HDR_DIR_INBOUND __constant_cpu_to_be32(0x00000002)
8314 +/* request done notification (DN) */
8315 +#define DESC_HDR_DONE_NOTIFY __constant_cpu_to_be32(0x00000001)
8317 +/* descriptor types */
8318 +#define DESC_HDR_TYPE_AESU_CTR_NONSNOOP __constant_cpu_to_be32(0 << 3)
8319 +#define DESC_HDR_TYPE_IPSEC_ESP __constant_cpu_to_be32(1 << 3)
8320 +#define DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU __constant_cpu_to_be32(2 << 3)
8321 +#define DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU __constant_cpu_to_be32(4 << 3)
8323 +/* link table extent field bits */
8324 +#define DESC_PTR_LNKTBL_JUMP 0x80
8325 +#define DESC_PTR_LNKTBL_RETURN 0x02
8326 +#define DESC_PTR_LNKTBL_NEXT 0x01
8328 +++ b/include/crypto/hash.h
8331 + * Hash: Hash algorithms under the crypto API
8333 + * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8335 + * This program is free software; you can redistribute it and/or modify it
8336 + * under the terms of the GNU General Public License as published by the Free
8337 + * Software Foundation; either version 2 of the License, or (at your option)
8338 + * any later version.
8342 +#ifndef _CRYPTO_HASH_H
8343 +#define _CRYPTO_HASH_H
8345 +#include <linux/crypto.h>
8347 +struct crypto_ahash {
8348 + struct crypto_tfm base;
8351 +static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
8353 + return (struct crypto_ahash *)tfm;
8356 +static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
8357 + u32 type, u32 mask)
8359 + type &= ~CRYPTO_ALG_TYPE_MASK;
8360 + mask &= ~CRYPTO_ALG_TYPE_MASK;
8361 + type |= CRYPTO_ALG_TYPE_AHASH;
8362 + mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
8364 + return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
8367 +static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
8369 + return &tfm->base;
8372 +static inline void crypto_free_ahash(struct crypto_ahash *tfm)
8374 + crypto_free_tfm(crypto_ahash_tfm(tfm));
8377 +static inline unsigned int crypto_ahash_alignmask(
8378 + struct crypto_ahash *tfm)
8380 + return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
8383 +static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
8385 + return &crypto_ahash_tfm(tfm)->crt_ahash;
8388 +static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
8390 + return crypto_ahash_crt(tfm)->digestsize;
8393 +static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
8395 + return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
8398 +static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
8400 + crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
8403 +static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
8405 + crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
8408 +static inline struct crypto_ahash *crypto_ahash_reqtfm(
8409 + struct ahash_request *req)
8411 + return __crypto_ahash_cast(req->base.tfm);
8414 +static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
8416 + return crypto_ahash_crt(tfm)->reqsize;
8419 +static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
8420 + const u8 *key, unsigned int keylen)
8422 + struct ahash_tfm *crt = crypto_ahash_crt(tfm);
8424 + return crt->setkey(tfm, key, keylen);
8427 +static inline int crypto_ahash_digest(struct ahash_request *req)
8429 + struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
8430 + return crt->digest(req);
8433 +static inline void ahash_request_set_tfm(struct ahash_request *req,
8434 + struct crypto_ahash *tfm)
8436 + req->base.tfm = crypto_ahash_tfm(tfm);
8439 +static inline struct ahash_request *ahash_request_alloc(
8440 + struct crypto_ahash *tfm, gfp_t gfp)
8442 + struct ahash_request *req;
8444 + req = kmalloc(sizeof(struct ahash_request) +
8445 + crypto_ahash_reqsize(tfm), gfp);
8448 + ahash_request_set_tfm(req, tfm);
8453 +static inline void ahash_request_free(struct ahash_request *req)
8458 +static inline struct ahash_request *ahash_request_cast(
8459 + struct crypto_async_request *req)
8461 + return container_of(req, struct ahash_request, base);
8464 +static inline void ahash_request_set_callback(struct ahash_request *req,
8466 + crypto_completion_t complete,
8469 + req->base.complete = complete;
8470 + req->base.data = data;
8471 + req->base.flags = flags;
8474 +static inline void ahash_request_set_crypt(struct ahash_request *req,
8475 + struct scatterlist *src, u8 *result,
8476 + unsigned int nbytes)
8479 + req->nbytes = nbytes;
8480 + req->result = result;
8483 +#endif /* _CRYPTO_HASH_H */
8485 +++ b/include/crypto/internal/hash.h
8488 + * Hash algorithms.
8490 + * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8492 + * This program is free software; you can redistribute it and/or modify it
8493 + * under the terms of the GNU General Public License as published by the Free
8494 + * Software Foundation; either version 2 of the License, or (at your option)
8495 + * any later version.
8499 +#ifndef _CRYPTO_INTERNAL_HASH_H
8500 +#define _CRYPTO_INTERNAL_HASH_H
8502 +#include <crypto/algapi.h>
8503 +#include <crypto/hash.h>
8505 +struct ahash_request;
8506 +struct scatterlist;
8508 +struct crypto_hash_walk {
8511 + unsigned int offset;
8512 + unsigned int alignmask;
8515 + unsigned int entrylen;
8517 + unsigned int total;
8518 + struct scatterlist *sg;
8520 + unsigned int flags;
8523 +extern const struct crypto_type crypto_ahash_type;
8525 +int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err);
8526 +int crypto_hash_walk_first(struct ahash_request *req,
8527 + struct crypto_hash_walk *walk);
8529 +static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
8531 + return crypto_tfm_ctx(&tfm->base);
8534 +static inline struct ahash_alg *crypto_ahash_alg(
8535 + struct crypto_ahash *tfm)
8537 + return &crypto_ahash_tfm(tfm)->__crt_alg->cra_ahash;
8540 +static inline int ahash_enqueue_request(struct crypto_queue *queue,
8541 + struct ahash_request *request)
8543 + return crypto_enqueue_request(queue, &request->base);
8546 +static inline struct ahash_request *ahash_dequeue_request(
8547 + struct crypto_queue *queue)
8549 + return ahash_request_cast(crypto_dequeue_request(queue));
8552 +static inline void *ahash_request_ctx(struct ahash_request *req)
8554 + return req->__ctx;
8557 +static inline int ahash_tfm_in_queue(struct crypto_queue *queue,
8558 + struct crypto_ahash *tfm)
8560 + return crypto_tfm_in_queue(queue, crypto_ahash_tfm(tfm));
8563 +#endif /* _CRYPTO_INTERNAL_HASH_H */
8565 --- a/include/linux/crypto.h
8566 +++ b/include/linux/crypto.h
8569 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
8570 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
8571 -#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
8572 -#define CRYPTO_ALG_TYPE_HASH 0x00000003
8573 +#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
8574 +#define CRYPTO_ALG_TYPE_AEAD 0x00000003
8575 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
8576 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
8577 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
8578 -#define CRYPTO_ALG_TYPE_COMPRESS 0x00000008
8579 -#define CRYPTO_ALG_TYPE_AEAD 0x00000009
8580 +#define CRYPTO_ALG_TYPE_DIGEST 0x00000008
8581 +#define CRYPTO_ALG_TYPE_HASH 0x00000009
8582 +#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
8584 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
8585 +#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
8586 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
8588 #define CRYPTO_ALG_LARVAL 0x00000010
8591 struct crypto_blkcipher;
8593 +struct crypto_ahash;
8596 struct aead_givcrypt_request;
8597 @@ -131,6 +134,16 @@
8598 void *__ctx[] CRYPTO_MINALIGN_ATTR;
8601 +struct ahash_request {
8602 + struct crypto_async_request base;
8604 + unsigned int nbytes;
8605 + struct scatterlist *src;
8608 + void *__ctx[] CRYPTO_MINALIGN_ATTR;
8612 * struct aead_request - AEAD request
8613 * @base: Common attributes for async crypto requests
8614 @@ -195,6 +208,17 @@
8615 unsigned int ivsize;
8619 + int (*init)(struct ahash_request *req);
8620 + int (*update)(struct ahash_request *req);
8621 + int (*final)(struct ahash_request *req);
8622 + int (*digest)(struct ahash_request *req);
8623 + int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
8624 + unsigned int keylen);
8626 + unsigned int digestsize;
8630 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
8631 unsigned int keylen);
8633 #define cra_cipher cra_u.cipher
8634 #define cra_digest cra_u.digest
8635 #define cra_hash cra_u.hash
8636 +#define cra_ahash cra_u.ahash
8637 #define cra_compress cra_u.compress
8641 struct cipher_alg cipher;
8642 struct digest_alg digest;
8643 struct hash_alg hash;
8644 + struct ahash_alg ahash;
8645 struct compress_alg compress;
8648 @@ -383,6 +409,18 @@
8649 unsigned int digestsize;
8653 + int (*init)(struct ahash_request *req);
8654 + int (*update)(struct ahash_request *req);
8655 + int (*final)(struct ahash_request *req);
8656 + int (*digest)(struct ahash_request *req);
8657 + int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
8658 + unsigned int keylen);
8660 + unsigned int digestsize;
8661 + unsigned int reqsize;
8664 struct compress_tfm {
8665 int (*cot_compress)(struct crypto_tfm *tfm,
8666 const u8 *src, unsigned int slen,
8668 #define crt_blkcipher crt_u.blkcipher
8669 #define crt_cipher crt_u.cipher
8670 #define crt_hash crt_u.hash
8671 +#define crt_ahash crt_u.ahash
8672 #define crt_compress crt_u.compress
8676 struct blkcipher_tfm blkcipher;
8677 struct cipher_tfm cipher;
8678 struct hash_tfm hash;
8679 + struct ahash_tfm ahash;
8680 struct compress_tfm compress;