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/chainiv.c
575 +++ b/crypto/chainiv.c
577 static int async_chainiv_schedule_work(struct async_chainiv_ctx *ctx)
580 + int err = ctx->err;
582 if (!ctx->queue.qlen) {
583 smp_mb__before_clear_bit();
592 static int async_chainiv_postpone_request(struct skcipher_givcrypt_request *req)
595 struct skcipher_givcrypt_request *req;
596 struct ablkcipher_request *subreq;
599 /* Only handle one request at a time to avoid hogging keventd. */
600 spin_lock_bh(&ctx->lock);
602 subreq = skcipher_givcrypt_reqctx(req);
603 subreq->base.flags |= CRYPTO_TFM_REQ_MAY_SLEEP;
605 - async_chainiv_givencrypt_tail(req);
606 + err = async_chainiv_givencrypt_tail(req);
608 + local_bh_disable();
609 + skcipher_givcrypt_complete(req, err);
613 static int async_chainiv_init(struct crypto_tfm *tfm)
614 --- a/crypto/crc32c.c
615 +++ b/crypto/crc32c.c
618 * This module file is a wrapper to invoke the lib/crc32c routines.
620 + * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
622 * This program is free software; you can redistribute it and/or modify it
623 * under the terms of the GNU General Public License as published by the Free
624 * Software Foundation; either version 2 of the License, or (at your option)
629 +#include <crypto/internal/hash.h>
630 #include <linux/init.h>
631 #include <linux/module.h>
632 #include <linux/string.h>
633 -#include <linux/crypto.h>
634 #include <linux/crc32c.h>
635 #include <linux/kernel.h>
637 -#define CHKSUM_BLOCK_SIZE 32
638 +#define CHKSUM_BLOCK_SIZE 1
639 #define CHKSUM_DIGEST_SIZE 4
643 *(__le32 *)out = ~cpu_to_le32(mctx->crc);
646 -static int crc32c_cra_init(struct crypto_tfm *tfm)
647 +static int crc32c_cra_init_old(struct crypto_tfm *tfm)
649 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
655 -static struct crypto_alg alg = {
656 +static struct crypto_alg old_alg = {
657 .cra_name = "crc32c",
658 .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
659 .cra_blocksize = CHKSUM_BLOCK_SIZE,
660 .cra_ctxsize = sizeof(struct chksum_ctx),
661 .cra_module = THIS_MODULE,
662 - .cra_list = LIST_HEAD_INIT(alg.cra_list),
663 - .cra_init = crc32c_cra_init,
664 + .cra_list = LIST_HEAD_INIT(old_alg.cra_list),
665 + .cra_init = crc32c_cra_init_old,
668 .dia_digestsize= CHKSUM_DIGEST_SIZE,
669 @@ -98,14 +101,125 @@
674 + * Setting the seed allows arbitrary accumulators and flexible XOR policy
675 + * If your algorithm starts with ~0, then XOR with ~0 before you set
678 +static int crc32c_setkey(struct crypto_ahash *hash, const u8 *key,
679 + unsigned int keylen)
681 + u32 *mctx = crypto_ahash_ctx(hash);
683 + if (keylen != sizeof(u32)) {
684 + crypto_ahash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
687 + *mctx = le32_to_cpup((__le32 *)key);
691 +static int crc32c_init(struct ahash_request *req)
693 + u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
694 + u32 *crcp = ahash_request_ctx(req);
700 +static int crc32c_update(struct ahash_request *req)
702 + struct crypto_hash_walk walk;
703 + u32 *crcp = ahash_request_ctx(req);
707 + for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
708 + nbytes = crypto_hash_walk_done(&walk, 0))
709 + crc = crc32c(crc, walk.data, nbytes);
715 +static int crc32c_final(struct ahash_request *req)
717 + u32 *crcp = ahash_request_ctx(req);
719 + *(__le32 *)req->result = ~cpu_to_le32p(crcp);
723 +static int crc32c_digest(struct ahash_request *req)
725 + struct crypto_hash_walk walk;
726 + u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req));
730 + for (nbytes = crypto_hash_walk_first(req, &walk); nbytes;
731 + nbytes = crypto_hash_walk_done(&walk, 0))
732 + crc = crc32c(crc, walk.data, nbytes);
734 + *(__le32 *)req->result = ~cpu_to_le32(crc);
738 +static int crc32c_cra_init(struct crypto_tfm *tfm)
740 + u32 *key = crypto_tfm_ctx(tfm);
744 + tfm->crt_ahash.reqsize = sizeof(u32);
749 +static struct crypto_alg alg = {
750 + .cra_name = "crc32c",
751 + .cra_driver_name = "crc32c-generic",
752 + .cra_priority = 100,
753 + .cra_flags = CRYPTO_ALG_TYPE_AHASH,
754 + .cra_blocksize = CHKSUM_BLOCK_SIZE,
755 + .cra_alignmask = 3,
756 + .cra_ctxsize = sizeof(u32),
757 + .cra_module = THIS_MODULE,
758 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
759 + .cra_init = crc32c_cra_init,
760 + .cra_type = &crypto_ahash_type,
763 + .digestsize = CHKSUM_DIGEST_SIZE,
764 + .setkey = crc32c_setkey,
765 + .init = crc32c_init,
766 + .update = crc32c_update,
767 + .final = crc32c_final,
768 + .digest = crc32c_digest,
773 static int __init crc32c_mod_init(void)
775 - return crypto_register_alg(&alg);
778 + err = crypto_register_alg(&old_alg);
782 + err = crypto_register_alg(&alg);
784 + crypto_unregister_alg(&old_alg);
789 static void __exit crc32c_mod_fini(void)
791 crypto_unregister_alg(&alg);
792 + crypto_unregister_alg(&old_alg);
795 module_init(crc32c_mod_init);
796 --- a/crypto/cryptd.c
797 +++ b/crypto/cryptd.c
801 #include <crypto/algapi.h>
802 +#include <crypto/internal/hash.h>
803 #include <linux/err.h>
804 #include <linux/init.h>
805 #include <linux/kernel.h>
807 crypto_completion_t complete;
810 +struct cryptd_hash_ctx {
811 + struct crypto_hash *child;
814 +struct cryptd_hash_request_ctx {
815 + crypto_completion_t complete;
818 static inline struct cryptd_state *cryptd_get_state(struct crypto_tfm *tfm)
822 rctx = ablkcipher_request_ctx(req);
824 - if (unlikely(err == -EINPROGRESS)) {
825 - rctx->complete(&req->base, err);
828 + if (unlikely(err == -EINPROGRESS))
832 desc.info = req->info;
835 req->base.complete = rctx->complete;
839 - req->base.complete(&req->base, err);
840 + rctx->complete(&req->base, err);
844 @@ -261,6 +268,240 @@
848 +static int cryptd_hash_init_tfm(struct crypto_tfm *tfm)
850 + struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
851 + struct cryptd_instance_ctx *ictx = crypto_instance_ctx(inst);
852 + struct crypto_spawn *spawn = &ictx->spawn;
853 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
854 + struct crypto_hash *cipher;
856 + cipher = crypto_spawn_hash(spawn);
857 + if (IS_ERR(cipher))
858 + return PTR_ERR(cipher);
860 + ctx->child = cipher;
861 + tfm->crt_ahash.reqsize =
862 + sizeof(struct cryptd_hash_request_ctx);
866 +static void cryptd_hash_exit_tfm(struct crypto_tfm *tfm)
868 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(tfm);
869 + struct cryptd_state *state = cryptd_get_state(tfm);
872 + mutex_lock(&state->mutex);
873 + active = ahash_tfm_in_queue(&state->queue,
874 + __crypto_ahash_cast(tfm));
875 + mutex_unlock(&state->mutex);
879 + crypto_free_hash(ctx->child);
882 +static int cryptd_hash_setkey(struct crypto_ahash *parent,
883 + const u8 *key, unsigned int keylen)
885 + struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(parent);
886 + struct crypto_hash *child = ctx->child;
889 + crypto_hash_clear_flags(child, CRYPTO_TFM_REQ_MASK);
890 + crypto_hash_set_flags(child, crypto_ahash_get_flags(parent) &
891 + CRYPTO_TFM_REQ_MASK);
892 + err = crypto_hash_setkey(child, key, keylen);
893 + crypto_ahash_set_flags(parent, crypto_hash_get_flags(child) &
894 + CRYPTO_TFM_RES_MASK);
898 +static int cryptd_hash_enqueue(struct ahash_request *req,
899 + crypto_completion_t complete)
901 + struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
902 + struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
903 + struct cryptd_state *state =
904 + cryptd_get_state(crypto_ahash_tfm(tfm));
907 + rctx->complete = req->base.complete;
908 + req->base.complete = complete;
910 + spin_lock_bh(&state->lock);
911 + err = ahash_enqueue_request(&state->queue, req);
912 + spin_unlock_bh(&state->lock);
914 + wake_up_process(state->task);
918 +static void cryptd_hash_init(struct crypto_async_request *req_async, int err)
920 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
921 + struct crypto_hash *child = ctx->child;
922 + struct ahash_request *req = ahash_request_cast(req_async);
923 + struct cryptd_hash_request_ctx *rctx;
924 + struct hash_desc desc;
926 + rctx = ahash_request_ctx(req);
928 + if (unlikely(err == -EINPROGRESS))
932 + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
934 + err = crypto_hash_crt(child)->init(&desc);
936 + req->base.complete = rctx->complete;
939 + local_bh_disable();
940 + rctx->complete(&req->base, err);
944 +static int cryptd_hash_init_enqueue(struct ahash_request *req)
946 + return cryptd_hash_enqueue(req, cryptd_hash_init);
949 +static void cryptd_hash_update(struct crypto_async_request *req_async, int err)
951 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
952 + struct crypto_hash *child = ctx->child;
953 + struct ahash_request *req = ahash_request_cast(req_async);
954 + struct cryptd_hash_request_ctx *rctx;
955 + struct hash_desc desc;
957 + rctx = ahash_request_ctx(req);
959 + if (unlikely(err == -EINPROGRESS))
963 + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
965 + err = crypto_hash_crt(child)->update(&desc,
969 + req->base.complete = rctx->complete;
972 + local_bh_disable();
973 + rctx->complete(&req->base, err);
977 +static int cryptd_hash_update_enqueue(struct ahash_request *req)
979 + return cryptd_hash_enqueue(req, cryptd_hash_update);
982 +static void cryptd_hash_final(struct crypto_async_request *req_async, int err)
984 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
985 + struct crypto_hash *child = ctx->child;
986 + struct ahash_request *req = ahash_request_cast(req_async);
987 + struct cryptd_hash_request_ctx *rctx;
988 + struct hash_desc desc;
990 + rctx = ahash_request_ctx(req);
992 + if (unlikely(err == -EINPROGRESS))
996 + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
998 + err = crypto_hash_crt(child)->final(&desc, req->result);
1000 + req->base.complete = rctx->complete;
1003 + local_bh_disable();
1004 + rctx->complete(&req->base, err);
1005 + local_bh_enable();
1008 +static int cryptd_hash_final_enqueue(struct ahash_request *req)
1010 + return cryptd_hash_enqueue(req, cryptd_hash_final);
1013 +static void cryptd_hash_digest(struct crypto_async_request *req_async, int err)
1015 + struct cryptd_hash_ctx *ctx = crypto_tfm_ctx(req_async->tfm);
1016 + struct crypto_hash *child = ctx->child;
1017 + struct ahash_request *req = ahash_request_cast(req_async);
1018 + struct cryptd_hash_request_ctx *rctx;
1019 + struct hash_desc desc;
1021 + rctx = ahash_request_ctx(req);
1023 + if (unlikely(err == -EINPROGRESS))
1027 + desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
1029 + err = crypto_hash_crt(child)->digest(&desc,
1034 + req->base.complete = rctx->complete;
1037 + local_bh_disable();
1038 + rctx->complete(&req->base, err);
1039 + local_bh_enable();
1042 +static int cryptd_hash_digest_enqueue(struct ahash_request *req)
1044 + return cryptd_hash_enqueue(req, cryptd_hash_digest);
1047 +static struct crypto_instance *cryptd_alloc_hash(
1048 + struct rtattr **tb, struct cryptd_state *state)
1050 + struct crypto_instance *inst;
1051 + struct crypto_alg *alg;
1053 + alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_HASH,
1054 + CRYPTO_ALG_TYPE_HASH_MASK);
1056 + return ERR_PTR(PTR_ERR(alg));
1058 + inst = cryptd_alloc_instance(alg, state);
1062 + inst->alg.cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC;
1063 + inst->alg.cra_type = &crypto_ahash_type;
1065 + inst->alg.cra_ahash.digestsize = alg->cra_hash.digestsize;
1066 + inst->alg.cra_ctxsize = sizeof(struct cryptd_hash_ctx);
1068 + inst->alg.cra_init = cryptd_hash_init_tfm;
1069 + inst->alg.cra_exit = cryptd_hash_exit_tfm;
1071 + inst->alg.cra_ahash.init = cryptd_hash_init_enqueue;
1072 + inst->alg.cra_ahash.update = cryptd_hash_update_enqueue;
1073 + inst->alg.cra_ahash.final = cryptd_hash_final_enqueue;
1074 + inst->alg.cra_ahash.setkey = cryptd_hash_setkey;
1075 + inst->alg.cra_ahash.digest = cryptd_hash_digest_enqueue;
1078 + crypto_mod_put(alg);
1082 static struct cryptd_state state;
1084 static struct crypto_instance *cryptd_alloc(struct rtattr **tb)
1086 switch (algt->type & algt->mask & CRYPTO_ALG_TYPE_MASK) {
1087 case CRYPTO_ALG_TYPE_BLKCIPHER:
1088 return cryptd_alloc_blkcipher(tb, &state);
1089 + case CRYPTO_ALG_TYPE_DIGEST:
1090 + return cryptd_alloc_hash(tb, &state);
1093 return ERR_PTR(-EINVAL);
1094 --- a/crypto/digest.c
1095 +++ b/crypto/digest.c
1100 +#include <crypto/internal/hash.h>
1101 #include <crypto/scatterwalk.h>
1102 #include <linux/mm.h>
1103 #include <linux/errno.h>
1105 struct hash_tfm *ops = &tfm->crt_hash;
1106 struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
1108 - if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
1109 + if (dalg->dia_digestsize > PAGE_SIZE / 8)
1113 @@ -157,3 +158,83 @@
1114 void crypto_exit_digest_ops(struct crypto_tfm *tfm)
1118 +static int digest_async_nosetkey(struct crypto_ahash *tfm_async, const u8 *key,
1119 + unsigned int keylen)
1121 + crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
1125 +static int digest_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
1126 + unsigned int keylen)
1128 + struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
1129 + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
1131 + crypto_ahash_clear_flags(tfm_async, CRYPTO_TFM_RES_MASK);
1132 + return dalg->dia_setkey(tfm, key, keylen);
1135 +static int digest_async_init(struct ahash_request *req)
1137 + struct crypto_tfm *tfm = req->base.tfm;
1138 + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
1140 + dalg->dia_init(tfm);
1144 +static int digest_async_update(struct ahash_request *req)
1146 + struct crypto_tfm *tfm = req->base.tfm;
1147 + struct hash_desc desc = {
1148 + .tfm = __crypto_hash_cast(tfm),
1149 + .flags = req->base.flags,
1152 + update(&desc, req->src, req->nbytes);
1156 +static int digest_async_final(struct ahash_request *req)
1158 + struct crypto_tfm *tfm = req->base.tfm;
1159 + struct hash_desc desc = {
1160 + .tfm = __crypto_hash_cast(tfm),
1161 + .flags = req->base.flags,
1164 + final(&desc, req->result);
1168 +static int digest_async_digest(struct ahash_request *req)
1170 + struct crypto_tfm *tfm = req->base.tfm;
1171 + struct hash_desc desc = {
1172 + .tfm = __crypto_hash_cast(tfm),
1173 + .flags = req->base.flags,
1176 + return digest(&desc, req->src, req->nbytes, req->result);
1179 +int crypto_init_digest_ops_async(struct crypto_tfm *tfm)
1181 + struct ahash_tfm *crt = &tfm->crt_ahash;
1182 + struct digest_alg *dalg = &tfm->__crt_alg->cra_digest;
1184 + if (dalg->dia_digestsize > crypto_tfm_alg_blocksize(tfm))
1187 + crt->init = digest_async_init;
1188 + crt->update = digest_async_update;
1189 + crt->final = digest_async_final;
1190 + crt->digest = digest_async_digest;
1191 + crt->setkey = dalg->dia_setkey ? digest_async_setkey :
1192 + digest_async_nosetkey;
1193 + crt->digestsize = dalg->dia_digestsize;
1200 * any later version.
1203 +#include <crypto/internal/hash.h>
1204 #include <linux/errno.h>
1205 #include <linux/kernel.h>
1206 #include <linux/module.h>
1207 @@ -59,24 +60,107 @@
1208 return alg->setkey(crt, key, keylen);
1211 -static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
1212 +static int hash_async_setkey(struct crypto_ahash *tfm_async, const u8 *key,
1213 + unsigned int keylen)
1215 + struct crypto_tfm *tfm = crypto_ahash_tfm(tfm_async);
1216 + struct crypto_hash *tfm_hash = __crypto_hash_cast(tfm);
1217 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1219 + return alg->setkey(tfm_hash, key, keylen);
1222 +static int hash_async_init(struct ahash_request *req)
1224 + struct crypto_tfm *tfm = req->base.tfm;
1225 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1226 + struct hash_desc desc = {
1227 + .tfm = __crypto_hash_cast(tfm),
1228 + .flags = req->base.flags,
1231 + return alg->init(&desc);
1234 +static int hash_async_update(struct ahash_request *req)
1236 + struct crypto_tfm *tfm = req->base.tfm;
1237 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1238 + struct hash_desc desc = {
1239 + .tfm = __crypto_hash_cast(tfm),
1240 + .flags = req->base.flags,
1243 + return alg->update(&desc, req->src, req->nbytes);
1246 +static int hash_async_final(struct ahash_request *req)
1248 + struct crypto_tfm *tfm = req->base.tfm;
1249 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1250 + struct hash_desc desc = {
1251 + .tfm = __crypto_hash_cast(tfm),
1252 + .flags = req->base.flags,
1255 + return alg->final(&desc, req->result);
1258 +static int hash_async_digest(struct ahash_request *req)
1260 + struct crypto_tfm *tfm = req->base.tfm;
1261 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1262 + struct hash_desc desc = {
1263 + .tfm = __crypto_hash_cast(tfm),
1264 + .flags = req->base.flags,
1267 + return alg->digest(&desc, req->src, req->nbytes, req->result);
1270 +static int crypto_init_hash_ops_async(struct crypto_tfm *tfm)
1272 + struct ahash_tfm *crt = &tfm->crt_ahash;
1273 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1275 + crt->init = hash_async_init;
1276 + crt->update = hash_async_update;
1277 + crt->final = hash_async_final;
1278 + crt->digest = hash_async_digest;
1279 + crt->setkey = hash_async_setkey;
1280 + crt->digestsize = alg->digestsize;
1285 +static int crypto_init_hash_ops_sync(struct crypto_tfm *tfm)
1287 struct hash_tfm *crt = &tfm->crt_hash;
1288 struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1290 - if (alg->digestsize > crypto_tfm_alg_blocksize(tfm))
1293 - crt->init = alg->init;
1294 - crt->update = alg->update;
1295 - crt->final = alg->final;
1296 - crt->digest = alg->digest;
1297 - crt->setkey = hash_setkey;
1298 + crt->init = alg->init;
1299 + crt->update = alg->update;
1300 + crt->final = alg->final;
1301 + crt->digest = alg->digest;
1302 + crt->setkey = hash_setkey;
1303 crt->digestsize = alg->digestsize;
1308 +static int crypto_init_hash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
1310 + struct hash_alg *alg = &tfm->__crt_alg->cra_hash;
1312 + if (alg->digestsize > PAGE_SIZE / 8)
1315 + if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) != CRYPTO_ALG_TYPE_HASH_MASK)
1316 + return crypto_init_hash_ops_async(tfm);
1318 + return crypto_init_hash_ops_sync(tfm);
1321 static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
1322 __attribute__ ((unused));
1323 static void crypto_hash_show(struct seq_file *m, struct crypto_alg *alg)
1327 struct crypto_instance *inst;
1328 struct crypto_alg *alg;
1332 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_HASH);
1334 @@ -236,6 +237,13 @@
1336 return ERR_CAST(alg);
1338 + inst = ERR_PTR(-EINVAL);
1339 + ds = (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
1340 + CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
1341 + alg->cra_digest.dia_digestsize;
1342 + if (ds > alg->cra_blocksize)
1345 inst = crypto_alloc_instance("hmac", alg);
1348 @@ -246,14 +254,10 @@
1349 inst->alg.cra_alignmask = alg->cra_alignmask;
1350 inst->alg.cra_type = &crypto_hash_type;
1352 - inst->alg.cra_hash.digestsize =
1353 - (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
1354 - CRYPTO_ALG_TYPE_HASH ? alg->cra_hash.digestsize :
1355 - alg->cra_digest.dia_digestsize;
1356 + inst->alg.cra_hash.digestsize = ds;
1358 inst->alg.cra_ctxsize = sizeof(struct hmac_ctx) +
1359 - ALIGN(inst->alg.cra_blocksize * 2 +
1360 - inst->alg.cra_hash.digestsize,
1361 + ALIGN(inst->alg.cra_blocksize * 2 + ds,
1364 inst->alg.cra_init = hmac_init_tfm;
1365 --- a/crypto/internal.h
1366 +++ b/crypto/internal.h
1368 struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
1370 int crypto_init_digest_ops(struct crypto_tfm *tfm);
1371 +int crypto_init_digest_ops_async(struct crypto_tfm *tfm);
1372 int crypto_init_cipher_ops(struct crypto_tfm *tfm);
1373 int crypto_init_compress_ops(struct crypto_tfm *tfm);
1379 + * PRNG: Pseudo Random Number Generator
1380 + * Based on NIST Recommended PRNG From ANSI X9.31 Appendix A.2.4 using
1381 + * AES 128 cipher in RFC3686 ctr mode
1383 + * (C) Neil Horman <nhorman@tuxdriver.com>
1385 + * This program is free software; you can redistribute it and/or modify it
1386 + * under the terms of the GNU General Public License as published by the
1387 + * Free Software Foundation; either version 2 of the License, or (at your
1388 + * any later version.
1393 +#include <linux/err.h>
1394 +#include <linux/init.h>
1395 +#include <linux/module.h>
1396 +#include <linux/mm.h>
1397 +#include <linux/slab.h>
1398 +#include <linux/fs.h>
1399 +#include <linux/scatterlist.h>
1400 +#include <linux/string.h>
1401 +#include <linux/crypto.h>
1402 +#include <linux/highmem.h>
1403 +#include <linux/moduleparam.h>
1404 +#include <linux/jiffies.h>
1405 +#include <linux/timex.h>
1406 +#include <linux/interrupt.h>
1407 +#include <linux/miscdevice.h>
1410 +#define TEST_PRNG_ON_START 0
1412 +#define DEFAULT_PRNG_KEY "0123456789abcdef1011"
1413 +#define DEFAULT_PRNG_KSZ 20
1414 +#define DEFAULT_PRNG_IV "defaultv"
1415 +#define DEFAULT_PRNG_IVSZ 8
1416 +#define DEFAULT_BLK_SZ 16
1417 +#define DEFAULT_V_SEED "zaybxcwdveuftgsh"
1420 + * Flags for the prng_context flags field
1423 +#define PRNG_FIXED_SIZE 0x1
1424 +#define PRNG_NEED_RESET 0x2
1427 + * Note: DT is our counter value
1428 + * I is our intermediate value
1429 + * V is our seed vector
1430 + * See http://csrc.nist.gov/groups/STM/cavp/documents/rng/931rngext.pdf
1431 + * for implementation details
1435 +struct prng_context {
1438 + spinlock_t prng_lock;
1439 + unsigned char rand_data[DEFAULT_BLK_SZ];
1440 + unsigned char last_rand_data[DEFAULT_BLK_SZ];
1441 + unsigned char DT[DEFAULT_BLK_SZ];
1442 + unsigned char I[DEFAULT_BLK_SZ];
1443 + unsigned char V[DEFAULT_BLK_SZ];
1444 + u32 rand_data_valid;
1445 + struct crypto_blkcipher *tfm;
1451 +static void hexdump(char *note, unsigned char *buf, unsigned int len)
1454 + printk(KERN_CRIT "%s", note);
1455 + print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
1461 +#define dbgprint(format, args...) do {if(dbg) printk(format, ##args);} while(0)
1463 +static void xor_vectors(unsigned char *in1, unsigned char *in2,
1464 + unsigned char *out, unsigned int size)
1468 + for (i=0;i<size;i++)
1469 + out[i] = in1[i] ^ in2[i];
1473 + * Returns DEFAULT_BLK_SZ bytes of random data per call
1474 + * returns 0 if generation succeded, <0 if something went wrong
1476 +static int _get_more_prng_bytes(struct prng_context *ctx)
1479 + struct blkcipher_desc desc;
1480 + struct scatterlist sg_in, sg_out;
1482 + unsigned char tmp[DEFAULT_BLK_SZ];
1484 + desc.tfm = ctx->tfm;
1488 + dbgprint(KERN_CRIT "Calling _get_more_prng_bytes for context %p\n",ctx);
1490 + hexdump("Input DT: ", ctx->DT, DEFAULT_BLK_SZ);
1491 + hexdump("Input I: ", ctx->I, DEFAULT_BLK_SZ);
1492 + hexdump("Input V: ", ctx->V, DEFAULT_BLK_SZ);
1495 + * This algorithm is a 3 stage state machine
1497 + for (i=0;i<3;i++) {
1499 + desc.tfm = ctx->tfm;
1504 + * Start by encrypting the counter value
1505 + * This gives us an intermediate value I
1507 + memcpy(tmp, ctx->DT, DEFAULT_BLK_SZ);
1508 + sg_init_one(&sg_out, &ctx->I[0], DEFAULT_BLK_SZ);
1509 + hexdump("tmp stage 0: ", tmp, DEFAULT_BLK_SZ);
1514 + * Next xor I with our secret vector V
1515 + * encrypt that result to obtain our
1516 + * pseudo random data which we output
1518 + xor_vectors(ctx->I, ctx->V, tmp, DEFAULT_BLK_SZ);
1519 + sg_init_one(&sg_out, &ctx->rand_data[0], DEFAULT_BLK_SZ);
1520 + hexdump("tmp stage 1: ", tmp, DEFAULT_BLK_SZ);
1524 + * First check that we didn't produce the same random data
1525 + * that we did last time around through this
1527 + if (!memcmp(ctx->rand_data, ctx->last_rand_data, DEFAULT_BLK_SZ)) {
1528 + printk(KERN_ERR "ctx %p Failed repetition check!\n",
1530 + ctx->flags |= PRNG_NEED_RESET;
1533 + memcpy(ctx->last_rand_data, ctx->rand_data, DEFAULT_BLK_SZ);
1536 + * Lastly xor the random data with I
1537 + * and encrypt that to obtain a new secret vector V
1539 + xor_vectors(ctx->rand_data, ctx->I, tmp, DEFAULT_BLK_SZ);
1540 + sg_init_one(&sg_out, &ctx->V[0], DEFAULT_BLK_SZ);
1541 + hexdump("tmp stage 2: ", tmp, DEFAULT_BLK_SZ);
1545 + /* Initialize our input buffer */
1546 + sg_init_one(&sg_in, &tmp[0], DEFAULT_BLK_SZ);
1548 + /* do the encryption */
1549 + ret = crypto_blkcipher_encrypt(&desc, &sg_out, &sg_in, DEFAULT_BLK_SZ);
1551 + /* And check the result */
1553 + dbgprint(KERN_CRIT "Encryption of new block failed for context %p\n",ctx);
1554 + ctx->rand_data_valid = DEFAULT_BLK_SZ;
1561 + * Now update our DT value
1563 + for (i=DEFAULT_BLK_SZ-1;i>0;i--) {
1564 + ctx->DT[i] = ctx->DT[i-1];
1568 + dbgprint("Returning new block for context %p\n",ctx);
1569 + ctx->rand_data_valid = 0;
1571 + hexdump("Output DT: ", ctx->DT, DEFAULT_BLK_SZ);
1572 + hexdump("Output I: ", ctx->I, DEFAULT_BLK_SZ);
1573 + hexdump("Output V: ", ctx->V, DEFAULT_BLK_SZ);
1574 + hexdump("New Random Data: ", ctx->rand_data, DEFAULT_BLK_SZ);
1579 +/* Our exported functions */
1580 +int get_prng_bytes(char *buf, int nbytes, struct prng_context *ctx)
1582 + unsigned long flags;
1583 + unsigned char *ptr = buf;
1584 + unsigned int byte_count = (unsigned int)nbytes;
1591 + spin_lock_irqsave(&ctx->prng_lock, flags);
1594 + if (ctx->flags & PRNG_NEED_RESET)
1598 + * If the FIXED_SIZE flag is on, only return whole blocks of
1599 + * pseudo random data
1602 + if (ctx->flags & PRNG_FIXED_SIZE) {
1603 + if (nbytes < DEFAULT_BLK_SZ)
1605 + byte_count = DEFAULT_BLK_SZ;
1610 + dbgprint(KERN_CRIT "getting %d random bytes for context %p\n",byte_count, ctx);
1614 + if (ctx->rand_data_valid == DEFAULT_BLK_SZ) {
1615 + if (_get_more_prng_bytes(ctx) < 0) {
1616 + memset(buf, 0, nbytes);
1623 + * Copy up to the next whole block size
1625 + if (byte_count < DEFAULT_BLK_SZ) {
1626 + for (;ctx->rand_data_valid < DEFAULT_BLK_SZ; ctx->rand_data_valid++) {
1627 + *ptr = ctx->rand_data[ctx->rand_data_valid];
1630 + if (byte_count == 0)
1636 + * Now copy whole blocks
1638 + for(;byte_count >= DEFAULT_BLK_SZ; byte_count -= DEFAULT_BLK_SZ) {
1639 + if (_get_more_prng_bytes(ctx) < 0) {
1640 + memset(buf, 0, nbytes);
1644 + memcpy(ptr, ctx->rand_data, DEFAULT_BLK_SZ);
1645 + ctx->rand_data_valid += DEFAULT_BLK_SZ;
1646 + ptr += DEFAULT_BLK_SZ;
1650 + * Now copy any extra partial data
1656 + spin_unlock_irqrestore(&ctx->prng_lock, flags);
1657 + dbgprint(KERN_CRIT "returning %d from get_prng_bytes in context %p\n",err, ctx);
1660 +EXPORT_SYMBOL_GPL(get_prng_bytes);
1662 +struct prng_context *alloc_prng_context(void)
1664 + struct prng_context *ctx=kzalloc(sizeof(struct prng_context), GFP_KERNEL);
1666 + spin_lock_init(&ctx->prng_lock);
1668 + if (reset_prng_context(ctx, NULL, NULL, NULL, NULL)) {
1673 + dbgprint(KERN_CRIT "returning context %p\n",ctx);
1677 +EXPORT_SYMBOL_GPL(alloc_prng_context);
1679 +void free_prng_context(struct prng_context *ctx)
1681 + crypto_free_blkcipher(ctx->tfm);
1684 +EXPORT_SYMBOL_GPL(free_prng_context);
1686 +int reset_prng_context(struct prng_context *ctx,
1687 + unsigned char *key, unsigned char *iv,
1688 + unsigned char *V, unsigned char *DT)
1694 + spin_lock(&ctx->prng_lock);
1695 + ctx->flags |= PRNG_NEED_RESET;
1698 + memcpy(ctx->prng_key,key,strlen(ctx->prng_key));
1700 + ctx->prng_key = DEFAULT_PRNG_KEY;
1703 + memcpy(ctx->prng_iv,iv, strlen(ctx->prng_iv));
1705 + ctx->prng_iv = DEFAULT_PRNG_IV;
1708 + memcpy(ctx->V,V,DEFAULT_BLK_SZ);
1710 + memcpy(ctx->V,DEFAULT_V_SEED,DEFAULT_BLK_SZ);
1713 + memcpy(ctx->DT, DT, DEFAULT_BLK_SZ);
1715 + memset(ctx->DT, 0, DEFAULT_BLK_SZ);
1717 + memset(ctx->rand_data,0,DEFAULT_BLK_SZ);
1718 + memset(ctx->last_rand_data,0,DEFAULT_BLK_SZ);
1721 + crypto_free_blkcipher(ctx->tfm);
1723 + ctx->tfm = crypto_alloc_blkcipher("rfc3686(ctr(aes))",0,0);
1725 + dbgprint(KERN_CRIT "Failed to alloc crypto tfm for context %p\n",ctx->tfm);
1729 + ctx->rand_data_valid = DEFAULT_BLK_SZ;
1731 + ret = crypto_blkcipher_setkey(ctx->tfm, ctx->prng_key, strlen(ctx->prng_key));
1733 + dbgprint(KERN_CRIT "PRNG: setkey() failed flags=%x\n",
1734 + crypto_blkcipher_get_flags(ctx->tfm));
1735 + crypto_free_blkcipher(ctx->tfm);
1739 + iv_len = crypto_blkcipher_ivsize(ctx->tfm);
1741 + crypto_blkcipher_set_iv(ctx->tfm, ctx->prng_iv, iv_len);
1744 + ctx->flags &= ~PRNG_NEED_RESET;
1746 + spin_unlock(&ctx->prng_lock);
1751 +EXPORT_SYMBOL_GPL(reset_prng_context);
1753 +/* Module initalization */
1754 +static int __init prng_mod_init(void)
1757 +#ifdef TEST_PRNG_ON_START
1759 + unsigned char tmpbuf[DEFAULT_BLK_SZ];
1761 + struct prng_context *ctx = alloc_prng_context();
1764 + for (i=0;i<16;i++) {
1765 + if (get_prng_bytes(tmpbuf, DEFAULT_BLK_SZ, ctx) < 0) {
1766 + free_prng_context(ctx);
1770 + free_prng_context(ctx);
1776 +static void __exit prng_mod_fini(void)
1781 +MODULE_LICENSE("GPL");
1782 +MODULE_DESCRIPTION("Software Pseudo Random Number Generator");
1783 +MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
1784 +module_param(dbg, int, 0);
1785 +MODULE_PARM_DESC(dbg, "Boolean to enable debugging (0/1 == off/on)");
1786 +module_init(prng_mod_init);
1787 +module_exit(prng_mod_fini);
1792 + * PRNG: Pseudo Random Number Generator
1794 + * (C) Neil Horman <nhorman@tuxdriver.com>
1796 + * This program is free software; you can redistribute it and/or modify it
1797 + * under the terms of the GNU General Public License as published by the
1798 + * Free Software Foundation; either version 2 of the License, or (at your
1799 + * any later version.
1806 +struct prng_context;
1808 +int get_prng_bytes(char *buf, int nbytes, struct prng_context *ctx);
1809 +struct prng_context *alloc_prng_context(void);
1810 +int reset_prng_context(struct prng_context *ctx,
1811 + unsigned char *key, unsigned char *iv,
1813 + unsigned char *DT);
1814 +void free_prng_context(struct prng_context *ctx);
1819 +++ b/crypto/ripemd.h
1822 + * Common values for RIPEMD algorithms
1825 +#ifndef _CRYPTO_RMD_H
1826 +#define _CRYPTO_RMD_H
1828 +#define RMD128_DIGEST_SIZE 16
1829 +#define RMD128_BLOCK_SIZE 64
1831 +#define RMD160_DIGEST_SIZE 20
1832 +#define RMD160_BLOCK_SIZE 64
1834 +#define RMD256_DIGEST_SIZE 32
1835 +#define RMD256_BLOCK_SIZE 64
1837 +#define RMD320_DIGEST_SIZE 40
1838 +#define RMD320_BLOCK_SIZE 64
1840 +/* initial values */
1841 +#define RMD_H0 0x67452301UL
1842 +#define RMD_H1 0xefcdab89UL
1843 +#define RMD_H2 0x98badcfeUL
1844 +#define RMD_H3 0x10325476UL
1845 +#define RMD_H4 0xc3d2e1f0UL
1846 +#define RMD_H5 0x76543210UL
1847 +#define RMD_H6 0xfedcba98UL
1848 +#define RMD_H7 0x89abcdefUL
1849 +#define RMD_H8 0x01234567UL
1850 +#define RMD_H9 0x3c2d1e0fUL
1853 +#define RMD_K1 0x00000000UL
1854 +#define RMD_K2 0x5a827999UL
1855 +#define RMD_K3 0x6ed9eba1UL
1856 +#define RMD_K4 0x8f1bbcdcUL
1857 +#define RMD_K5 0xa953fd4eUL
1858 +#define RMD_K6 0x50a28be6UL
1859 +#define RMD_K7 0x5c4dd124UL
1860 +#define RMD_K8 0x6d703ef3UL
1861 +#define RMD_K9 0x7a6d76e9UL
1865 +++ b/crypto/rmd128.c
1868 + * Cryptographic API.
1870 + * RIPEMD-128 - RACE Integrity Primitives Evaluation Message Digest.
1872 + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
1874 + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
1876 + * This program is free software; you can redistribute it and/or modify it
1877 + * under the terms of the GNU General Public License as published by the Free
1878 + * Software Foundation; either version 2 of the License, or (at your option)
1879 + * any later version.
1882 +#include <linux/init.h>
1883 +#include <linux/module.h>
1884 +#include <linux/mm.h>
1885 +#include <linux/crypto.h>
1886 +#include <linux/cryptohash.h>
1887 +#include <linux/types.h>
1888 +#include <asm/byteorder.h>
1890 +#include "ripemd.h"
1892 +struct rmd128_ctx {
1895 + __le32 buffer[16];
1907 +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
1908 +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
1909 +#define F3(x, y, z) ((x | ~y) ^ z)
1910 +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
1912 +#define ROUND(a, b, c, d, f, k, x, s) { \
1913 + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
1914 + (a) = rol32((a), (s)); \
1917 +static void rmd128_transform(u32 *state, const __le32 *in)
1919 + u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd;
1921 + /* Initialize left lane */
1927 + /* Initialize right lane */
1933 + /* round 1: left lane */
1934 + ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
1935 + ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
1936 + ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
1937 + ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
1938 + ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
1939 + ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
1940 + ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
1941 + ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
1942 + ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
1943 + ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
1944 + ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
1945 + ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
1946 + ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
1947 + ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
1948 + ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
1949 + ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
1951 + /* round 2: left lane */
1952 + ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
1953 + ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
1954 + ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
1955 + ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
1956 + ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
1957 + ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
1958 + ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
1959 + ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
1960 + ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
1961 + ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
1962 + ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
1963 + ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
1964 + ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
1965 + ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
1966 + ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
1967 + ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
1969 + /* round 3: left lane */
1970 + ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
1971 + ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
1972 + ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
1973 + ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
1974 + ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
1975 + ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
1976 + ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
1977 + ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
1978 + ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
1979 + ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
1980 + ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
1981 + ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
1982 + ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
1983 + ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
1984 + ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
1985 + ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
1987 + /* round 4: left lane */
1988 + ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
1989 + ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
1990 + ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
1991 + ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
1992 + ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
1993 + ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
1994 + ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
1995 + ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
1996 + ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
1997 + ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
1998 + ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
1999 + ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
2000 + ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
2001 + ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
2002 + ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
2003 + ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
2005 + /* round 1: right lane */
2006 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
2007 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
2008 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
2009 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
2010 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
2011 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
2012 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
2013 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
2014 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
2015 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
2016 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
2017 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
2018 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
2019 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
2020 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
2021 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
2023 + /* round 2: right lane */
2024 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
2025 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
2026 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
2027 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
2028 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
2029 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
2030 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
2031 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
2032 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
2033 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
2034 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
2035 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
2036 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
2037 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
2038 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
2039 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
2041 + /* round 3: right lane */
2042 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
2043 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
2044 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
2045 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
2046 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
2047 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
2048 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
2049 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
2050 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
2051 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
2052 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
2053 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
2054 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
2055 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
2056 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
2057 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
2059 + /* round 4: right lane */
2060 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
2061 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
2062 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
2063 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
2064 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
2065 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
2066 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
2067 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
2068 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
2069 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
2070 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
2071 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
2072 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
2073 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
2074 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
2075 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
2077 + /* combine results */
2078 + ddd += cc + state[1]; /* final result for state[0] */
2079 + state[1] = state[2] + dd + aaa;
2080 + state[2] = state[3] + aa + bbb;
2081 + state[3] = state[0] + bb + ccc;
2087 +static void rmd128_init(struct crypto_tfm *tfm)
2089 + struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
2091 + rctx->byte_count = 0;
2093 + rctx->state[0] = RMD_H0;
2094 + rctx->state[1] = RMD_H1;
2095 + rctx->state[2] = RMD_H2;
2096 + rctx->state[3] = RMD_H3;
2098 + memset(rctx->buffer, 0, sizeof(rctx->buffer));
2101 +static void rmd128_update(struct crypto_tfm *tfm, const u8 *data,
2104 + struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
2105 + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
2107 + rctx->byte_count += len;
2109 + /* Enough space in buffer? If so copy and we're done */
2110 + if (avail > len) {
2111 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2116 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2119 + rmd128_transform(rctx->state, rctx->buffer);
2123 + while (len >= sizeof(rctx->buffer)) {
2124 + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
2125 + rmd128_transform(rctx->state, rctx->buffer);
2126 + data += sizeof(rctx->buffer);
2127 + len -= sizeof(rctx->buffer);
2130 + memcpy(rctx->buffer, data, len);
2133 +/* Add padding and return the message digest. */
2134 +static void rmd128_final(struct crypto_tfm *tfm, u8 *out)
2136 + struct rmd128_ctx *rctx = crypto_tfm_ctx(tfm);
2137 + u32 i, index, padlen;
2139 + __le32 *dst = (__le32 *)out;
2140 + static const u8 padding[64] = { 0x80, };
2142 + bits = cpu_to_le64(rctx->byte_count << 3);
2144 + /* Pad out to 56 mod 64 */
2145 + index = rctx->byte_count & 0x3f;
2146 + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
2147 + rmd128_update(tfm, padding, padlen);
2149 + /* Append length */
2150 + rmd128_update(tfm, (const u8 *)&bits, sizeof(bits));
2152 + /* Store state in digest */
2153 + for (i = 0; i < 4; i++)
2154 + dst[i] = cpu_to_le32p(&rctx->state[i]);
2156 + /* Wipe context */
2157 + memset(rctx, 0, sizeof(*rctx));
2160 +static struct crypto_alg alg = {
2161 + .cra_name = "rmd128",
2162 + .cra_driver_name = "rmd128",
2163 + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
2164 + .cra_blocksize = RMD128_BLOCK_SIZE,
2165 + .cra_ctxsize = sizeof(struct rmd128_ctx),
2166 + .cra_module = THIS_MODULE,
2167 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
2168 + .cra_u = { .digest = {
2169 + .dia_digestsize = RMD128_DIGEST_SIZE,
2170 + .dia_init = rmd128_init,
2171 + .dia_update = rmd128_update,
2172 + .dia_final = rmd128_final } }
2175 +static int __init rmd128_mod_init(void)
2177 + return crypto_register_alg(&alg);
2180 +static void __exit rmd128_mod_fini(void)
2182 + crypto_unregister_alg(&alg);
2185 +module_init(rmd128_mod_init);
2186 +module_exit(rmd128_mod_fini);
2188 +MODULE_LICENSE("GPL");
2189 +MODULE_DESCRIPTION("RIPEMD-128 Message Digest");
2191 +MODULE_ALIAS("rmd128");
2193 +++ b/crypto/rmd160.c
2196 + * Cryptographic API.
2198 + * RIPEMD-160 - RACE Integrity Primitives Evaluation Message Digest.
2200 + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
2202 + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
2204 + * This program is free software; you can redistribute it and/or modify it
2205 + * under the terms of the GNU General Public License as published by the Free
2206 + * Software Foundation; either version 2 of the License, or (at your option)
2207 + * any later version.
2210 +#include <linux/init.h>
2211 +#include <linux/module.h>
2212 +#include <linux/mm.h>
2213 +#include <linux/crypto.h>
2214 +#include <linux/cryptohash.h>
2215 +#include <linux/types.h>
2216 +#include <asm/byteorder.h>
2218 +#include "ripemd.h"
2220 +struct rmd160_ctx {
2223 + __le32 buffer[16];
2237 +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
2238 +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
2239 +#define F3(x, y, z) ((x | ~y) ^ z)
2240 +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
2241 +#define F5(x, y, z) (x ^ (y | ~z))
2243 +#define ROUND(a, b, c, d, e, f, k, x, s) { \
2244 + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
2245 + (a) = rol32((a), (s)) + (e); \
2246 + (c) = rol32((c), 10); \
2249 +static void rmd160_transform(u32 *state, const __le32 *in)
2251 + u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee;
2253 + /* Initialize left lane */
2260 + /* Initialize right lane */
2267 + /* round 1: left lane */
2268 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11);
2269 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14);
2270 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15);
2271 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12);
2272 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5);
2273 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8);
2274 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7);
2275 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9);
2276 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11);
2277 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13);
2278 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
2279 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
2280 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6);
2281 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7);
2282 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9);
2283 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8);
2285 + /* round 2: left lane" */
2286 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
2287 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
2288 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
2289 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
2290 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
2291 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
2292 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
2293 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
2294 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
2295 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
2296 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
2297 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
2298 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
2299 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
2300 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
2301 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
2303 + /* round 3: left lane" */
2304 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
2305 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
2306 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
2307 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
2308 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
2309 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
2310 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
2311 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
2312 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
2313 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
2314 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
2315 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
2316 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
2317 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
2318 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
2319 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
2321 + /* round 4: left lane" */
2322 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
2323 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
2324 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
2325 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
2326 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
2327 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
2328 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
2329 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
2330 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
2331 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
2332 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
2333 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
2334 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
2335 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
2336 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
2337 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
2339 + /* round 5: left lane" */
2340 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
2341 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
2342 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
2343 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
2344 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
2345 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
2346 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
2347 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
2348 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
2349 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
2350 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
2351 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
2352 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
2353 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
2354 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
2355 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
2357 + /* round 1: right lane */
2358 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
2359 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
2360 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
2361 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
2362 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
2363 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
2364 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
2365 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
2366 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
2367 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
2368 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
2369 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
2370 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
2371 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
2372 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
2373 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
2375 + /* round 2: right lane */
2376 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
2377 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
2378 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
2379 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
2380 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
2381 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
2382 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
2383 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
2384 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
2385 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
2386 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
2387 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
2388 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
2389 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
2390 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
2391 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
2393 + /* round 3: right lane */
2394 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
2395 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
2396 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
2397 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
2398 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
2399 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
2400 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
2401 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
2402 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
2403 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
2404 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
2405 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
2406 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
2407 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
2408 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
2409 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
2411 + /* round 4: right lane */
2412 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
2413 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
2414 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
2415 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
2416 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
2417 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
2418 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
2419 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
2420 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
2421 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
2422 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
2423 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
2424 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
2425 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
2426 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
2427 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
2429 + /* round 5: right lane */
2430 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
2431 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
2432 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
2433 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
2434 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
2435 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
2436 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
2437 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
2438 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
2439 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
2440 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
2441 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
2442 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
2443 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
2444 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
2445 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
2447 + /* combine results */
2448 + ddd += cc + state[1]; /* final result for state[0] */
2449 + state[1] = state[2] + dd + eee;
2450 + state[2] = state[3] + ee + aaa;
2451 + state[3] = state[4] + aa + bbb;
2452 + state[4] = state[0] + bb + ccc;
2458 +static void rmd160_init(struct crypto_tfm *tfm)
2460 + struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
2462 + rctx->byte_count = 0;
2464 + rctx->state[0] = RMD_H0;
2465 + rctx->state[1] = RMD_H1;
2466 + rctx->state[2] = RMD_H2;
2467 + rctx->state[3] = RMD_H3;
2468 + rctx->state[4] = RMD_H4;
2470 + memset(rctx->buffer, 0, sizeof(rctx->buffer));
2473 +static void rmd160_update(struct crypto_tfm *tfm, const u8 *data,
2476 + struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
2477 + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
2479 + rctx->byte_count += len;
2481 + /* Enough space in buffer? If so copy and we're done */
2482 + if (avail > len) {
2483 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2488 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2491 + rmd160_transform(rctx->state, rctx->buffer);
2495 + while (len >= sizeof(rctx->buffer)) {
2496 + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
2497 + rmd160_transform(rctx->state, rctx->buffer);
2498 + data += sizeof(rctx->buffer);
2499 + len -= sizeof(rctx->buffer);
2502 + memcpy(rctx->buffer, data, len);
2505 +/* Add padding and return the message digest. */
2506 +static void rmd160_final(struct crypto_tfm *tfm, u8 *out)
2508 + struct rmd160_ctx *rctx = crypto_tfm_ctx(tfm);
2509 + u32 i, index, padlen;
2511 + __le32 *dst = (__le32 *)out;
2512 + static const u8 padding[64] = { 0x80, };
2514 + bits = cpu_to_le64(rctx->byte_count << 3);
2516 + /* Pad out to 56 mod 64 */
2517 + index = rctx->byte_count & 0x3f;
2518 + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
2519 + rmd160_update(tfm, padding, padlen);
2521 + /* Append length */
2522 + rmd160_update(tfm, (const u8 *)&bits, sizeof(bits));
2524 + /* Store state in digest */
2525 + for (i = 0; i < 5; i++)
2526 + dst[i] = cpu_to_le32p(&rctx->state[i]);
2528 + /* Wipe context */
2529 + memset(rctx, 0, sizeof(*rctx));
2532 +static struct crypto_alg alg = {
2533 + .cra_name = "rmd160",
2534 + .cra_driver_name = "rmd160",
2535 + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
2536 + .cra_blocksize = RMD160_BLOCK_SIZE,
2537 + .cra_ctxsize = sizeof(struct rmd160_ctx),
2538 + .cra_module = THIS_MODULE,
2539 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
2540 + .cra_u = { .digest = {
2541 + .dia_digestsize = RMD160_DIGEST_SIZE,
2542 + .dia_init = rmd160_init,
2543 + .dia_update = rmd160_update,
2544 + .dia_final = rmd160_final } }
2547 +static int __init rmd160_mod_init(void)
2549 + return crypto_register_alg(&alg);
2552 +static void __exit rmd160_mod_fini(void)
2554 + crypto_unregister_alg(&alg);
2557 +module_init(rmd160_mod_init);
2558 +module_exit(rmd160_mod_fini);
2560 +MODULE_LICENSE("GPL");
2561 +MODULE_DESCRIPTION("RIPEMD-160 Message Digest");
2563 +MODULE_ALIAS("rmd160");
2565 +++ b/crypto/rmd256.c
2568 + * Cryptographic API.
2570 + * RIPEMD-256 - RACE Integrity Primitives Evaluation Message Digest.
2572 + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
2574 + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
2576 + * This program is free software; you can redistribute it and/or modify it
2577 + * under the terms of the GNU General Public License as published by the Free
2578 + * Software Foundation; either version 2 of the License, or (at your option)
2579 + * any later version.
2582 +#include <linux/init.h>
2583 +#include <linux/module.h>
2584 +#include <linux/mm.h>
2585 +#include <linux/crypto.h>
2586 +#include <linux/cryptohash.h>
2587 +#include <linux/types.h>
2588 +#include <asm/byteorder.h>
2590 +#include "ripemd.h"
2592 +struct rmd256_ctx {
2595 + __le32 buffer[16];
2607 +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
2608 +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
2609 +#define F3(x, y, z) ((x | ~y) ^ z)
2610 +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
2612 +#define ROUND(a, b, c, d, f, k, x, s) { \
2613 + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
2614 + (a) = rol32((a), (s)); \
2617 +static void rmd256_transform(u32 *state, const __le32 *in)
2619 + u32 aa, bb, cc, dd, aaa, bbb, ccc, ddd, tmp;
2621 + /* Initialize left lane */
2627 + /* Initialize right lane */
2633 + /* round 1: left lane */
2634 + ROUND(aa, bb, cc, dd, F1, K1, in[0], 11);
2635 + ROUND(dd, aa, bb, cc, F1, K1, in[1], 14);
2636 + ROUND(cc, dd, aa, bb, F1, K1, in[2], 15);
2637 + ROUND(bb, cc, dd, aa, F1, K1, in[3], 12);
2638 + ROUND(aa, bb, cc, dd, F1, K1, in[4], 5);
2639 + ROUND(dd, aa, bb, cc, F1, K1, in[5], 8);
2640 + ROUND(cc, dd, aa, bb, F1, K1, in[6], 7);
2641 + ROUND(bb, cc, dd, aa, F1, K1, in[7], 9);
2642 + ROUND(aa, bb, cc, dd, F1, K1, in[8], 11);
2643 + ROUND(dd, aa, bb, cc, F1, K1, in[9], 13);
2644 + ROUND(cc, dd, aa, bb, F1, K1, in[10], 14);
2645 + ROUND(bb, cc, dd, aa, F1, K1, in[11], 15);
2646 + ROUND(aa, bb, cc, dd, F1, K1, in[12], 6);
2647 + ROUND(dd, aa, bb, cc, F1, K1, in[13], 7);
2648 + ROUND(cc, dd, aa, bb, F1, K1, in[14], 9);
2649 + ROUND(bb, cc, dd, aa, F1, K1, in[15], 8);
2651 + /* round 1: right lane */
2652 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[5], 8);
2653 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[14], 9);
2654 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[7], 9);
2655 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[0], 11);
2656 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[9], 13);
2657 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[2], 15);
2658 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[11], 15);
2659 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[4], 5);
2660 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[13], 7);
2661 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[6], 7);
2662 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[15], 8);
2663 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[8], 11);
2664 + ROUND(aaa, bbb, ccc, ddd, F4, KK1, in[1], 14);
2665 + ROUND(ddd, aaa, bbb, ccc, F4, KK1, in[10], 14);
2666 + ROUND(ccc, ddd, aaa, bbb, F4, KK1, in[3], 12);
2667 + ROUND(bbb, ccc, ddd, aaa, F4, KK1, in[12], 6);
2669 + /* Swap contents of "a" registers */
2670 + tmp = aa; aa = aaa; aaa = tmp;
2672 + /* round 2: left lane */
2673 + ROUND(aa, bb, cc, dd, F2, K2, in[7], 7);
2674 + ROUND(dd, aa, bb, cc, F2, K2, in[4], 6);
2675 + ROUND(cc, dd, aa, bb, F2, K2, in[13], 8);
2676 + ROUND(bb, cc, dd, aa, F2, K2, in[1], 13);
2677 + ROUND(aa, bb, cc, dd, F2, K2, in[10], 11);
2678 + ROUND(dd, aa, bb, cc, F2, K2, in[6], 9);
2679 + ROUND(cc, dd, aa, bb, F2, K2, in[15], 7);
2680 + ROUND(bb, cc, dd, aa, F2, K2, in[3], 15);
2681 + ROUND(aa, bb, cc, dd, F2, K2, in[12], 7);
2682 + ROUND(dd, aa, bb, cc, F2, K2, in[0], 12);
2683 + ROUND(cc, dd, aa, bb, F2, K2, in[9], 15);
2684 + ROUND(bb, cc, dd, aa, F2, K2, in[5], 9);
2685 + ROUND(aa, bb, cc, dd, F2, K2, in[2], 11);
2686 + ROUND(dd, aa, bb, cc, F2, K2, in[14], 7);
2687 + ROUND(cc, dd, aa, bb, F2, K2, in[11], 13);
2688 + ROUND(bb, cc, dd, aa, F2, K2, in[8], 12);
2690 + /* round 2: right lane */
2691 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[6], 9);
2692 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[11], 13);
2693 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[3], 15);
2694 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[7], 7);
2695 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[0], 12);
2696 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[13], 8);
2697 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[5], 9);
2698 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[10], 11);
2699 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[14], 7);
2700 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[15], 7);
2701 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[8], 12);
2702 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[12], 7);
2703 + ROUND(aaa, bbb, ccc, ddd, F3, KK2, in[4], 6);
2704 + ROUND(ddd, aaa, bbb, ccc, F3, KK2, in[9], 15);
2705 + ROUND(ccc, ddd, aaa, bbb, F3, KK2, in[1], 13);
2706 + ROUND(bbb, ccc, ddd, aaa, F3, KK2, in[2], 11);
2708 + /* Swap contents of "b" registers */
2709 + tmp = bb; bb = bbb; bbb = tmp;
2711 + /* round 3: left lane */
2712 + ROUND(aa, bb, cc, dd, F3, K3, in[3], 11);
2713 + ROUND(dd, aa, bb, cc, F3, K3, in[10], 13);
2714 + ROUND(cc, dd, aa, bb, F3, K3, in[14], 6);
2715 + ROUND(bb, cc, dd, aa, F3, K3, in[4], 7);
2716 + ROUND(aa, bb, cc, dd, F3, K3, in[9], 14);
2717 + ROUND(dd, aa, bb, cc, F3, K3, in[15], 9);
2718 + ROUND(cc, dd, aa, bb, F3, K3, in[8], 13);
2719 + ROUND(bb, cc, dd, aa, F3, K3, in[1], 15);
2720 + ROUND(aa, bb, cc, dd, F3, K3, in[2], 14);
2721 + ROUND(dd, aa, bb, cc, F3, K3, in[7], 8);
2722 + ROUND(cc, dd, aa, bb, F3, K3, in[0], 13);
2723 + ROUND(bb, cc, dd, aa, F3, K3, in[6], 6);
2724 + ROUND(aa, bb, cc, dd, F3, K3, in[13], 5);
2725 + ROUND(dd, aa, bb, cc, F3, K3, in[11], 12);
2726 + ROUND(cc, dd, aa, bb, F3, K3, in[5], 7);
2727 + ROUND(bb, cc, dd, aa, F3, K3, in[12], 5);
2729 + /* round 3: right lane */
2730 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[15], 9);
2731 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[5], 7);
2732 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[1], 15);
2733 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[3], 11);
2734 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[7], 8);
2735 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[14], 6);
2736 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[6], 6);
2737 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[9], 14);
2738 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[11], 12);
2739 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[8], 13);
2740 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[12], 5);
2741 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[2], 14);
2742 + ROUND(aaa, bbb, ccc, ddd, F2, KK3, in[10], 13);
2743 + ROUND(ddd, aaa, bbb, ccc, F2, KK3, in[0], 13);
2744 + ROUND(ccc, ddd, aaa, bbb, F2, KK3, in[4], 7);
2745 + ROUND(bbb, ccc, ddd, aaa, F2, KK3, in[13], 5);
2747 + /* Swap contents of "c" registers */
2748 + tmp = cc; cc = ccc; ccc = tmp;
2750 + /* round 4: left lane */
2751 + ROUND(aa, bb, cc, dd, F4, K4, in[1], 11);
2752 + ROUND(dd, aa, bb, cc, F4, K4, in[9], 12);
2753 + ROUND(cc, dd, aa, bb, F4, K4, in[11], 14);
2754 + ROUND(bb, cc, dd, aa, F4, K4, in[10], 15);
2755 + ROUND(aa, bb, cc, dd, F4, K4, in[0], 14);
2756 + ROUND(dd, aa, bb, cc, F4, K4, in[8], 15);
2757 + ROUND(cc, dd, aa, bb, F4, K4, in[12], 9);
2758 + ROUND(bb, cc, dd, aa, F4, K4, in[4], 8);
2759 + ROUND(aa, bb, cc, dd, F4, K4, in[13], 9);
2760 + ROUND(dd, aa, bb, cc, F4, K4, in[3], 14);
2761 + ROUND(cc, dd, aa, bb, F4, K4, in[7], 5);
2762 + ROUND(bb, cc, dd, aa, F4, K4, in[15], 6);
2763 + ROUND(aa, bb, cc, dd, F4, K4, in[14], 8);
2764 + ROUND(dd, aa, bb, cc, F4, K4, in[5], 6);
2765 + ROUND(cc, dd, aa, bb, F4, K4, in[6], 5);
2766 + ROUND(bb, cc, dd, aa, F4, K4, in[2], 12);
2768 + /* round 4: right lane */
2769 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[8], 15);
2770 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[6], 5);
2771 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[4], 8);
2772 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[1], 11);
2773 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[3], 14);
2774 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[11], 14);
2775 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[15], 6);
2776 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[0], 14);
2777 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[5], 6);
2778 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[12], 9);
2779 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[2], 12);
2780 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[13], 9);
2781 + ROUND(aaa, bbb, ccc, ddd, F1, KK4, in[9], 12);
2782 + ROUND(ddd, aaa, bbb, ccc, F1, KK4, in[7], 5);
2783 + ROUND(ccc, ddd, aaa, bbb, F1, KK4, in[10], 15);
2784 + ROUND(bbb, ccc, ddd, aaa, F1, KK4, in[14], 8);
2786 + /* Swap contents of "d" registers */
2787 + tmp = dd; dd = ddd; ddd = tmp;
2789 + /* combine results */
2802 +static void rmd256_init(struct crypto_tfm *tfm)
2804 + struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
2806 + rctx->byte_count = 0;
2808 + rctx->state[0] = RMD_H0;
2809 + rctx->state[1] = RMD_H1;
2810 + rctx->state[2] = RMD_H2;
2811 + rctx->state[3] = RMD_H3;
2812 + rctx->state[4] = RMD_H5;
2813 + rctx->state[5] = RMD_H6;
2814 + rctx->state[6] = RMD_H7;
2815 + rctx->state[7] = RMD_H8;
2817 + memset(rctx->buffer, 0, sizeof(rctx->buffer));
2820 +static void rmd256_update(struct crypto_tfm *tfm, const u8 *data,
2823 + struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
2824 + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
2826 + rctx->byte_count += len;
2828 + /* Enough space in buffer? If so copy and we're done */
2829 + if (avail > len) {
2830 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2835 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
2838 + rmd256_transform(rctx->state, rctx->buffer);
2842 + while (len >= sizeof(rctx->buffer)) {
2843 + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
2844 + rmd256_transform(rctx->state, rctx->buffer);
2845 + data += sizeof(rctx->buffer);
2846 + len -= sizeof(rctx->buffer);
2849 + memcpy(rctx->buffer, data, len);
2852 +/* Add padding and return the message digest. */
2853 +static void rmd256_final(struct crypto_tfm *tfm, u8 *out)
2855 + struct rmd256_ctx *rctx = crypto_tfm_ctx(tfm);
2856 + u32 i, index, padlen;
2858 + __le32 *dst = (__le32 *)out;
2859 + static const u8 padding[64] = { 0x80, };
2861 + bits = cpu_to_le64(rctx->byte_count << 3);
2863 + /* Pad out to 56 mod 64 */
2864 + index = rctx->byte_count & 0x3f;
2865 + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
2866 + rmd256_update(tfm, padding, padlen);
2868 + /* Append length */
2869 + rmd256_update(tfm, (const u8 *)&bits, sizeof(bits));
2871 + /* Store state in digest */
2872 + for (i = 0; i < 8; i++)
2873 + dst[i] = cpu_to_le32p(&rctx->state[i]);
2875 + /* Wipe context */
2876 + memset(rctx, 0, sizeof(*rctx));
2879 +static struct crypto_alg alg = {
2880 + .cra_name = "rmd256",
2881 + .cra_driver_name = "rmd256",
2882 + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
2883 + .cra_blocksize = RMD256_BLOCK_SIZE,
2884 + .cra_ctxsize = sizeof(struct rmd256_ctx),
2885 + .cra_module = THIS_MODULE,
2886 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
2887 + .cra_u = { .digest = {
2888 + .dia_digestsize = RMD256_DIGEST_SIZE,
2889 + .dia_init = rmd256_init,
2890 + .dia_update = rmd256_update,
2891 + .dia_final = rmd256_final } }
2894 +static int __init rmd256_mod_init(void)
2896 + return crypto_register_alg(&alg);
2899 +static void __exit rmd256_mod_fini(void)
2901 + crypto_unregister_alg(&alg);
2904 +module_init(rmd256_mod_init);
2905 +module_exit(rmd256_mod_fini);
2907 +MODULE_LICENSE("GPL");
2908 +MODULE_DESCRIPTION("RIPEMD-256 Message Digest");
2910 +MODULE_ALIAS("rmd256");
2912 +++ b/crypto/rmd320.c
2915 + * Cryptographic API.
2917 + * RIPEMD-320 - RACE Integrity Primitives Evaluation Message Digest.
2919 + * Based on the reference implementation by Antoon Bosselaers, ESAT-COSIC
2921 + * Copyright (c) 2008 Adrian-Ken Rueegsegger <rueegsegger (at) swiss-it.ch>
2923 + * This program is free software; you can redistribute it and/or modify it
2924 + * under the terms of the GNU General Public License as published by the Free
2925 + * Software Foundation; either version 2 of the License, or (at your option)
2926 + * any later version.
2929 +#include <linux/init.h>
2930 +#include <linux/module.h>
2931 +#include <linux/mm.h>
2932 +#include <linux/crypto.h>
2933 +#include <linux/cryptohash.h>
2934 +#include <linux/types.h>
2935 +#include <asm/byteorder.h>
2937 +#include "ripemd.h"
2939 +struct rmd320_ctx {
2942 + __le32 buffer[16];
2956 +#define F1(x, y, z) (x ^ y ^ z) /* XOR */
2957 +#define F2(x, y, z) (z ^ (x & (y ^ z))) /* x ? y : z */
2958 +#define F3(x, y, z) ((x | ~y) ^ z)
2959 +#define F4(x, y, z) (y ^ (z & (x ^ y))) /* z ? x : y */
2960 +#define F5(x, y, z) (x ^ (y | ~z))
2962 +#define ROUND(a, b, c, d, e, f, k, x, s) { \
2963 + (a) += f((b), (c), (d)) + le32_to_cpup(&(x)) + (k); \
2964 + (a) = rol32((a), (s)) + (e); \
2965 + (c) = rol32((c), 10); \
2968 +static void rmd320_transform(u32 *state, const __le32 *in)
2970 + u32 aa, bb, cc, dd, ee, aaa, bbb, ccc, ddd, eee, tmp;
2972 + /* Initialize left lane */
2979 + /* Initialize right lane */
2986 + /* round 1: left lane */
2987 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[0], 11);
2988 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[1], 14);
2989 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[2], 15);
2990 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[3], 12);
2991 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[4], 5);
2992 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[5], 8);
2993 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[6], 7);
2994 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[7], 9);
2995 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[8], 11);
2996 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[9], 13);
2997 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[10], 14);
2998 + ROUND(ee, aa, bb, cc, dd, F1, K1, in[11], 15);
2999 + ROUND(dd, ee, aa, bb, cc, F1, K1, in[12], 6);
3000 + ROUND(cc, dd, ee, aa, bb, F1, K1, in[13], 7);
3001 + ROUND(bb, cc, dd, ee, aa, F1, K1, in[14], 9);
3002 + ROUND(aa, bb, cc, dd, ee, F1, K1, in[15], 8);
3004 + /* round 1: right lane */
3005 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[5], 8);
3006 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[14], 9);
3007 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[7], 9);
3008 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[0], 11);
3009 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[9], 13);
3010 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[2], 15);
3011 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[11], 15);
3012 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[4], 5);
3013 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[13], 7);
3014 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[6], 7);
3015 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[15], 8);
3016 + ROUND(eee, aaa, bbb, ccc, ddd, F5, KK1, in[8], 11);
3017 + ROUND(ddd, eee, aaa, bbb, ccc, F5, KK1, in[1], 14);
3018 + ROUND(ccc, ddd, eee, aaa, bbb, F5, KK1, in[10], 14);
3019 + ROUND(bbb, ccc, ddd, eee, aaa, F5, KK1, in[3], 12);
3020 + ROUND(aaa, bbb, ccc, ddd, eee, F5, KK1, in[12], 6);
3022 + /* Swap contents of "a" registers */
3023 + tmp = aa; aa = aaa; aaa = tmp;
3025 + /* round 2: left lane" */
3026 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[7], 7);
3027 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[4], 6);
3028 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[13], 8);
3029 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[1], 13);
3030 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[10], 11);
3031 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[6], 9);
3032 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[15], 7);
3033 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[3], 15);
3034 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[12], 7);
3035 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[0], 12);
3036 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[9], 15);
3037 + ROUND(dd, ee, aa, bb, cc, F2, K2, in[5], 9);
3038 + ROUND(cc, dd, ee, aa, bb, F2, K2, in[2], 11);
3039 + ROUND(bb, cc, dd, ee, aa, F2, K2, in[14], 7);
3040 + ROUND(aa, bb, cc, dd, ee, F2, K2, in[11], 13);
3041 + ROUND(ee, aa, bb, cc, dd, F2, K2, in[8], 12);
3043 + /* round 2: right lane */
3044 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[6], 9);
3045 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[11], 13);
3046 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[3], 15);
3047 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[7], 7);
3048 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[0], 12);
3049 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[13], 8);
3050 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[5], 9);
3051 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[10], 11);
3052 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[14], 7);
3053 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[15], 7);
3054 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[8], 12);
3055 + ROUND(ddd, eee, aaa, bbb, ccc, F4, KK2, in[12], 7);
3056 + ROUND(ccc, ddd, eee, aaa, bbb, F4, KK2, in[4], 6);
3057 + ROUND(bbb, ccc, ddd, eee, aaa, F4, KK2, in[9], 15);
3058 + ROUND(aaa, bbb, ccc, ddd, eee, F4, KK2, in[1], 13);
3059 + ROUND(eee, aaa, bbb, ccc, ddd, F4, KK2, in[2], 11);
3061 + /* Swap contents of "b" registers */
3062 + tmp = bb; bb = bbb; bbb = tmp;
3064 + /* round 3: left lane" */
3065 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[3], 11);
3066 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[10], 13);
3067 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[14], 6);
3068 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[4], 7);
3069 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[9], 14);
3070 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[15], 9);
3071 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[8], 13);
3072 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[1], 15);
3073 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[2], 14);
3074 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[7], 8);
3075 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[0], 13);
3076 + ROUND(cc, dd, ee, aa, bb, F3, K3, in[6], 6);
3077 + ROUND(bb, cc, dd, ee, aa, F3, K3, in[13], 5);
3078 + ROUND(aa, bb, cc, dd, ee, F3, K3, in[11], 12);
3079 + ROUND(ee, aa, bb, cc, dd, F3, K3, in[5], 7);
3080 + ROUND(dd, ee, aa, bb, cc, F3, K3, in[12], 5);
3082 + /* round 3: right lane */
3083 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[15], 9);
3084 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[5], 7);
3085 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[1], 15);
3086 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[3], 11);
3087 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[7], 8);
3088 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[14], 6);
3089 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[6], 6);
3090 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[9], 14);
3091 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[11], 12);
3092 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[8], 13);
3093 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[12], 5);
3094 + ROUND(ccc, ddd, eee, aaa, bbb, F3, KK3, in[2], 14);
3095 + ROUND(bbb, ccc, ddd, eee, aaa, F3, KK3, in[10], 13);
3096 + ROUND(aaa, bbb, ccc, ddd, eee, F3, KK3, in[0], 13);
3097 + ROUND(eee, aaa, bbb, ccc, ddd, F3, KK3, in[4], 7);
3098 + ROUND(ddd, eee, aaa, bbb, ccc, F3, KK3, in[13], 5);
3100 + /* Swap contents of "c" registers */
3101 + tmp = cc; cc = ccc; ccc = tmp;
3103 + /* round 4: left lane" */
3104 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[1], 11);
3105 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[9], 12);
3106 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[11], 14);
3107 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[10], 15);
3108 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[0], 14);
3109 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[8], 15);
3110 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[12], 9);
3111 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[4], 8);
3112 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[13], 9);
3113 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[3], 14);
3114 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[7], 5);
3115 + ROUND(bb, cc, dd, ee, aa, F4, K4, in[15], 6);
3116 + ROUND(aa, bb, cc, dd, ee, F4, K4, in[14], 8);
3117 + ROUND(ee, aa, bb, cc, dd, F4, K4, in[5], 6);
3118 + ROUND(dd, ee, aa, bb, cc, F4, K4, in[6], 5);
3119 + ROUND(cc, dd, ee, aa, bb, F4, K4, in[2], 12);
3121 + /* round 4: right lane */
3122 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[8], 15);
3123 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[6], 5);
3124 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[4], 8);
3125 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[1], 11);
3126 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[3], 14);
3127 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[11], 14);
3128 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[15], 6);
3129 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[0], 14);
3130 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[5], 6);
3131 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[12], 9);
3132 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[2], 12);
3133 + ROUND(bbb, ccc, ddd, eee, aaa, F2, KK4, in[13], 9);
3134 + ROUND(aaa, bbb, ccc, ddd, eee, F2, KK4, in[9], 12);
3135 + ROUND(eee, aaa, bbb, ccc, ddd, F2, KK4, in[7], 5);
3136 + ROUND(ddd, eee, aaa, bbb, ccc, F2, KK4, in[10], 15);
3137 + ROUND(ccc, ddd, eee, aaa, bbb, F2, KK4, in[14], 8);
3139 + /* Swap contents of "d" registers */
3140 + tmp = dd; dd = ddd; ddd = tmp;
3142 + /* round 5: left lane" */
3143 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[4], 9);
3144 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[0], 15);
3145 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[5], 5);
3146 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[9], 11);
3147 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[7], 6);
3148 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[12], 8);
3149 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[2], 13);
3150 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[10], 12);
3151 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[14], 5);
3152 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[1], 12);
3153 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[3], 13);
3154 + ROUND(aa, bb, cc, dd, ee, F5, K5, in[8], 14);
3155 + ROUND(ee, aa, bb, cc, dd, F5, K5, in[11], 11);
3156 + ROUND(dd, ee, aa, bb, cc, F5, K5, in[6], 8);
3157 + ROUND(cc, dd, ee, aa, bb, F5, K5, in[15], 5);
3158 + ROUND(bb, cc, dd, ee, aa, F5, K5, in[13], 6);
3160 + /* round 5: right lane */
3161 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[12], 8);
3162 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[15], 5);
3163 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[10], 12);
3164 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[4], 9);
3165 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[1], 12);
3166 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[5], 5);
3167 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[8], 14);
3168 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[7], 6);
3169 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[6], 8);
3170 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[2], 13);
3171 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[13], 6);
3172 + ROUND(aaa, bbb, ccc, ddd, eee, F1, KK5, in[14], 5);
3173 + ROUND(eee, aaa, bbb, ccc, ddd, F1, KK5, in[0], 15);
3174 + ROUND(ddd, eee, aaa, bbb, ccc, F1, KK5, in[3], 13);
3175 + ROUND(ccc, ddd, eee, aaa, bbb, F1, KK5, in[9], 11);
3176 + ROUND(bbb, ccc, ddd, eee, aaa, F1, KK5, in[11], 11);
3178 + /* Swap contents of "e" registers */
3179 + tmp = ee; ee = eee; eee = tmp;
3181 + /* combine results */
3196 +static void rmd320_init(struct crypto_tfm *tfm)
3198 + struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
3200 + rctx->byte_count = 0;
3202 + rctx->state[0] = RMD_H0;
3203 + rctx->state[1] = RMD_H1;
3204 + rctx->state[2] = RMD_H2;
3205 + rctx->state[3] = RMD_H3;
3206 + rctx->state[4] = RMD_H4;
3207 + rctx->state[5] = RMD_H5;
3208 + rctx->state[6] = RMD_H6;
3209 + rctx->state[7] = RMD_H7;
3210 + rctx->state[8] = RMD_H8;
3211 + rctx->state[9] = RMD_H9;
3213 + memset(rctx->buffer, 0, sizeof(rctx->buffer));
3216 +static void rmd320_update(struct crypto_tfm *tfm, const u8 *data,
3219 + struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
3220 + const u32 avail = sizeof(rctx->buffer) - (rctx->byte_count & 0x3f);
3222 + rctx->byte_count += len;
3224 + /* Enough space in buffer? If so copy and we're done */
3225 + if (avail > len) {
3226 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
3231 + memcpy((char *)rctx->buffer + (sizeof(rctx->buffer) - avail),
3234 + rmd320_transform(rctx->state, rctx->buffer);
3238 + while (len >= sizeof(rctx->buffer)) {
3239 + memcpy(rctx->buffer, data, sizeof(rctx->buffer));
3240 + rmd320_transform(rctx->state, rctx->buffer);
3241 + data += sizeof(rctx->buffer);
3242 + len -= sizeof(rctx->buffer);
3245 + memcpy(rctx->buffer, data, len);
3248 +/* Add padding and return the message digest. */
3249 +static void rmd320_final(struct crypto_tfm *tfm, u8 *out)
3251 + struct rmd320_ctx *rctx = crypto_tfm_ctx(tfm);
3252 + u32 i, index, padlen;
3254 + __le32 *dst = (__le32 *)out;
3255 + static const u8 padding[64] = { 0x80, };
3257 + bits = cpu_to_le64(rctx->byte_count << 3);
3259 + /* Pad out to 56 mod 64 */
3260 + index = rctx->byte_count & 0x3f;
3261 + padlen = (index < 56) ? (56 - index) : ((64+56) - index);
3262 + rmd320_update(tfm, padding, padlen);
3264 + /* Append length */
3265 + rmd320_update(tfm, (const u8 *)&bits, sizeof(bits));
3267 + /* Store state in digest */
3268 + for (i = 0; i < 10; i++)
3269 + dst[i] = cpu_to_le32p(&rctx->state[i]);
3271 + /* Wipe context */
3272 + memset(rctx, 0, sizeof(*rctx));
3275 +static struct crypto_alg alg = {
3276 + .cra_name = "rmd320",
3277 + .cra_driver_name = "rmd320",
3278 + .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
3279 + .cra_blocksize = RMD320_BLOCK_SIZE,
3280 + .cra_ctxsize = sizeof(struct rmd320_ctx),
3281 + .cra_module = THIS_MODULE,
3282 + .cra_list = LIST_HEAD_INIT(alg.cra_list),
3283 + .cra_u = { .digest = {
3284 + .dia_digestsize = RMD320_DIGEST_SIZE,
3285 + .dia_init = rmd320_init,
3286 + .dia_update = rmd320_update,
3287 + .dia_final = rmd320_final } }
3290 +static int __init rmd320_mod_init(void)
3292 + return crypto_register_alg(&alg);
3295 +static void __exit rmd320_mod_fini(void)
3297 + crypto_unregister_alg(&alg);
3300 +module_init(rmd320_mod_init);
3301 +module_exit(rmd320_mod_fini);
3303 +MODULE_LICENSE("GPL");
3304 +MODULE_DESCRIPTION("RIPEMD-320 Message Digest");
3306 +MODULE_ALIAS("rmd320");
3307 --- a/crypto/tcrypt.c
3308 +++ b/crypto/tcrypt.c
3310 * Software Foundation; either version 2 of the License, or (at your option)
3311 * any later version.
3313 - * 2007-11-13 Added GCM tests
3314 - * 2007-11-13 Added AEAD support
3315 - * 2007-11-06 Added SHA-224 and SHA-224-HMAC tests
3316 - * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
3317 - * 2004-08-09 Added cipher speed tests (Reyk Floeter <reyk@vantronix.net>)
3318 - * 2003-09-14 Rewritten by Kartikey Mahendra Bhatt
3322 +#include <crypto/hash.h>
3323 #include <linux/err.h>
3324 #include <linux/init.h>
3325 #include <linux/module.h>
3327 #include <linux/scatterlist.h>
3328 #include <linux/string.h>
3329 #include <linux/crypto.h>
3330 -#include <linux/highmem.h>
3331 #include <linux/moduleparam.h>
3332 #include <linux/jiffies.h>
3333 #include <linux/timex.h>
3338 - * Need to kmalloc() memory for testing kmap().
3339 + * Need to kmalloc() memory for testing.
3341 #define TVMEMSIZE 16384
3342 #define XBUFSIZE 32768
3345 * Indexes into the xbuf to simulate cross-page access.
3353 "blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
3354 "cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
3355 "khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
3356 - "camellia", "seed", "salsa20", "lzo", "cts", NULL
3357 + "camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
3358 + "lzo", "cts", NULL
3361 static void hexdump(unsigned char *buf, unsigned int len)
3362 @@ -110,22 +104,30 @@
3363 unsigned int i, j, k, temp;
3364 struct scatterlist sg[8];
3366 - struct crypto_hash *tfm;
3367 - struct hash_desc desc;
3368 + struct crypto_ahash *tfm;
3369 + struct ahash_request *req;
3370 + struct tcrypt_result tresult;
3374 printk("\ntesting %s\n", algo);
3376 - tfm = crypto_alloc_hash(algo, 0, CRYPTO_ALG_ASYNC);
3377 + init_completion(&tresult.completion);
3379 + tfm = crypto_alloc_ahash(algo, 0, 0);
3381 printk("failed to load transform for %s: %ld\n", algo,
3388 + req = ahash_request_alloc(tfm, GFP_KERNEL);
3390 + printk(KERN_ERR "failed to allocate request for %s\n", algo);
3393 + ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3394 + tcrypt_complete, &tresult);
3396 for (i = 0; i < tcount; i++) {
3397 printk("test %u:\n", i + 1);
3399 sg_init_one(&sg[0], hash_buff, template[i].psize);
3401 if (template[i].ksize) {
3402 - ret = crypto_hash_setkey(tfm, template[i].key,
3403 - template[i].ksize);
3404 + crypto_ahash_clear_flags(tfm, ~0);
3405 + ret = crypto_ahash_setkey(tfm, template[i].key,
3406 + template[i].ksize);
3408 printk("setkey() failed ret=%d\n", ret);
3410 @@ -148,17 +151,30 @@
3414 - ret = crypto_hash_digest(&desc, sg, template[i].psize, result);
3416 + ahash_request_set_crypt(req, sg, result, template[i].psize);
3417 + ret = crypto_ahash_digest(req);
3421 + case -EINPROGRESS:
3423 + ret = wait_for_completion_interruptible(
3424 + &tresult.completion);
3425 + if (!ret && !(ret = tresult.err)) {
3426 + INIT_COMPLETION(tresult.completion);
3429 + /* fall through */
3431 printk("digest () failed ret=%d\n", ret);
3436 - hexdump(result, crypto_hash_digestsize(tfm));
3437 + hexdump(result, crypto_ahash_digestsize(tfm));
3439 memcmp(result, template[i].digest,
3440 - crypto_hash_digestsize(tfm)) ?
3441 + crypto_ahash_digestsize(tfm)) ?
3448 if (template[i].ksize) {
3449 - ret = crypto_hash_setkey(tfm, template[i].key,
3450 - template[i].ksize);
3451 + crypto_ahash_clear_flags(tfm, ~0);
3452 + ret = crypto_ahash_setkey(tfm, template[i].key,
3453 + template[i].ksize);
3456 printk("setkey() failed ret=%d\n", ret);
3457 @@ -196,29 +213,44 @@
3461 - ret = crypto_hash_digest(&desc, sg, template[i].psize,
3464 + ahash_request_set_crypt(req, sg, result,
3465 + template[i].psize);
3466 + ret = crypto_ahash_digest(req);
3470 + case -EINPROGRESS:
3472 + ret = wait_for_completion_interruptible(
3473 + &tresult.completion);
3474 + if (!ret && !(ret = tresult.err)) {
3475 + INIT_COMPLETION(tresult.completion);
3478 + /* fall through */
3480 printk("digest () failed ret=%d\n", ret);
3484 - hexdump(result, crypto_hash_digestsize(tfm));
3485 + hexdump(result, crypto_ahash_digestsize(tfm));
3487 memcmp(result, template[i].digest,
3488 - crypto_hash_digestsize(tfm)) ?
3489 + crypto_ahash_digestsize(tfm)) ?
3495 - crypto_free_hash(tfm);
3496 + ahash_request_free(req);
3498 + crypto_free_ahash(tfm);
3501 static void test_aead(char *algo, int enc, struct aead_testvec *template,
3502 unsigned int tcount)
3504 - unsigned int ret, i, j, k, temp;
3505 + unsigned int ret, i, j, k, n, temp;
3507 struct crypto_aead *tfm;
3509 @@ -344,13 +376,12 @@
3513 - q = kmap(sg_page(&sg[0])) + sg[0].offset;
3515 hexdump(q, template[i].rlen);
3517 printk(KERN_INFO "enc/dec: %s\n",
3518 memcmp(q, template[i].result,
3519 template[i].rlen) ? "fail" : "pass");
3520 - kunmap(sg_page(&sg[0]));
3522 if (!template[i].key)
3527 printk(KERN_INFO "\ntesting %s %s across pages (chunking)\n", algo, e);
3528 - memset(xbuf, 0, XBUFSIZE);
3529 memset(axbuf, 0, XBUFSIZE);
3531 for (i = 0, j = 0; i < tcount; i++) {
3536 + memset(xbuf, 0, XBUFSIZE);
3537 sg_init_table(sg, template[i].np);
3538 for (k = 0, temp = 0; k < template[i].np; k++) {
3539 memcpy(&xbuf[IDX[k]],
3542 for (k = 0, temp = 0; k < template[i].np; k++) {
3543 printk(KERN_INFO "page %u\n", k);
3544 - q = kmap(sg_page(&sg[k])) + sg[k].offset;
3545 + q = &axbuf[IDX[k]];
3546 hexdump(q, template[i].tap[k]);
3547 printk(KERN_INFO "%s\n",
3548 memcmp(q, template[i].result + temp,
3549 @@ -459,8 +490,15 @@
3553 + for (n = 0; q[template[i].tap[k] + n]; n++)
3556 + printk("Result buffer corruption %u "
3558 + hexdump(&q[template[i].tap[k]], n);
3561 temp += template[i].tap[k];
3562 - kunmap(sg_page(&sg[k]));
3567 static void test_cipher(char *algo, int enc,
3568 struct cipher_testvec *template, unsigned int tcount)
3570 - unsigned int ret, i, j, k, temp;
3571 + unsigned int ret, i, j, k, n, temp;
3573 struct crypto_ablkcipher *tfm;
3574 struct ablkcipher_request *req;
3575 @@ -569,19 +607,17 @@
3579 - q = kmap(sg_page(&sg[0])) + sg[0].offset;
3581 hexdump(q, template[i].rlen);
3584 memcmp(q, template[i].result,
3585 template[i].rlen) ? "fail" : "pass");
3586 - kunmap(sg_page(&sg[0]));
3591 printk("\ntesting %s %s across pages (chunking)\n", algo, e);
3592 - memset(xbuf, 0, XBUFSIZE);
3595 for (i = 0; i < tcount; i++) {
3597 printk("test %u (%d bit key):\n",
3598 j, template[i].klen * 8);
3600 + memset(xbuf, 0, XBUFSIZE);
3601 crypto_ablkcipher_clear_flags(tfm, ~0);
3603 crypto_ablkcipher_set_flags(
3604 @@ -657,14 +694,21 @@
3606 for (k = 0; k < template[i].np; k++) {
3607 printk("page %u\n", k);
3608 - q = kmap(sg_page(&sg[k])) + sg[k].offset;
3609 + q = &xbuf[IDX[k]];
3610 hexdump(q, template[i].tap[k]);
3612 memcmp(q, template[i].result + temp,
3613 template[i].tap[k]) ? "fail" :
3616 + for (n = 0; q[template[i].tap[k] + n]; n++)
3619 + printk("Result buffer corruption %u "
3621 + hexdump(&q[template[i].tap[k]], n);
3623 temp += template[i].tap[k];
3624 - kunmap(sg_page(&sg[k]));
3628 @@ -1180,6 +1224,14 @@
3629 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
3630 DES3_EDE_DEC_TEST_VECTORS);
3632 + test_cipher("cbc(des3_ede)", ENCRYPT,
3633 + des3_ede_cbc_enc_tv_template,
3634 + DES3_EDE_CBC_ENC_TEST_VECTORS);
3636 + test_cipher("cbc(des3_ede)", DECRYPT,
3637 + des3_ede_cbc_dec_tv_template,
3638 + DES3_EDE_CBC_DEC_TEST_VECTORS);
3640 test_hash("md4", md4_tv_template, MD4_TEST_VECTORS);
3642 test_hash("sha224", sha224_tv_template, SHA224_TEST_VECTORS);
3643 @@ -1390,6 +1442,14 @@
3644 DES3_EDE_ENC_TEST_VECTORS);
3645 test_cipher("ecb(des3_ede)", DECRYPT, des3_ede_dec_tv_template,
3646 DES3_EDE_DEC_TEST_VECTORS);
3648 + test_cipher("cbc(des3_ede)", ENCRYPT,
3649 + des3_ede_cbc_enc_tv_template,
3650 + DES3_EDE_CBC_ENC_TEST_VECTORS);
3652 + test_cipher("cbc(des3_ede)", DECRYPT,
3653 + des3_ede_cbc_dec_tv_template,
3654 + DES3_EDE_CBC_DEC_TEST_VECTORS);
3658 @@ -1558,7 +1618,7 @@
3660 test_hash("tgr128", tgr128_tv_template, TGR128_TEST_VECTORS);
3665 test_cipher("ecb(xeta)", ENCRYPT, xeta_enc_tv_template,
3666 XETA_ENC_TEST_VECTORS);
3667 @@ -1623,6 +1683,22 @@
3668 CTS_MODE_DEC_TEST_VECTORS);
3672 + test_hash("rmd128", rmd128_tv_template, RMD128_TEST_VECTORS);
3676 + test_hash("rmd160", rmd160_tv_template, RMD160_TEST_VECTORS);
3680 + test_hash("rmd256", rmd256_tv_template, RMD256_TEST_VECTORS);
3684 + test_hash("rmd320", rmd320_tv_template, RMD320_TEST_VECTORS);
3688 test_hash("hmac(md5)", hmac_md5_tv_template,
3689 HMAC_MD5_TEST_VECTORS);
3690 @@ -1658,6 +1734,16 @@
3691 XCBC_AES_TEST_VECTORS);
3695 + test_hash("hmac(rmd128)", hmac_rmd128_tv_template,
3696 + HMAC_RMD128_TEST_VECTORS);
3700 + test_hash("hmac(rmd160)", hmac_rmd160_tv_template,
3701 + HMAC_RMD160_TEST_VECTORS);
3705 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
3706 speed_template_16_24_32);
3707 @@ -1796,6 +1882,22 @@
3708 test_hash_speed("sha224", sec, generic_hash_speed_template);
3709 if (mode > 300 && mode < 400) break;
3712 + test_hash_speed("rmd128", sec, generic_hash_speed_template);
3713 + if (mode > 300 && mode < 400) break;
3716 + test_hash_speed("rmd160", sec, generic_hash_speed_template);
3717 + if (mode > 300 && mode < 400) break;
3720 + test_hash_speed("rmd256", sec, generic_hash_speed_template);
3721 + if (mode > 300 && mode < 400) break;
3724 + test_hash_speed("rmd320", sec, generic_hash_speed_template);
3725 + if (mode > 300 && mode < 400) break;
3730 --- a/crypto/tcrypt.h
3731 +++ b/crypto/tcrypt.h
3733 * Software Foundation; either version 2 of the License, or (at your option)
3734 * any later version.
3736 - * 2007-11-13 Added GCM tests
3737 - * 2007-11-13 Added AEAD support
3738 - * 2006-12-07 Added SHA384 HMAC and SHA512 HMAC tests
3739 - * 2004-08-09 Cipher speed tests by Reyk Floeter <reyk@vantronix.net>
3740 - * 2003-09-14 Changes by Kartikey Mahendra Bhatt
3743 #ifndef _CRYPTO_TCRYPT_H
3744 #define _CRYPTO_TCRYPT_H
3745 @@ -168,6 +162,271 @@
3746 .digest = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55"
3747 "\xac\x49\xda\x2e\x21\x07\xb6\x7a",
3753 + * RIPEMD-128 test vectors from ISO/IEC 10118-3:2004(E)
3755 +#define RMD128_TEST_VECTORS 10
3757 +static struct hash_testvec rmd128_tv_template[] = {
3759 + .digest = "\xcd\xf2\x62\x13\xa1\x50\xdc\x3e"
3760 + "\xcb\x61\x0f\x18\xf6\xb3\x8b\x46",
3764 + .digest = "\x86\xbe\x7a\xfa\x33\x9d\x0f\xc7"
3765 + "\xcf\xc7\x85\xe7\x2f\x57\x8d\x33",
3767 + .plaintext = "abc",
3769 + .digest = "\xc1\x4a\x12\x19\x9c\x66\xe4\xba"
3770 + "\x84\x63\x6b\x0f\x69\x14\x4c\x77",
3772 + .plaintext = "message digest",
3774 + .digest = "\x9e\x32\x7b\x3d\x6e\x52\x30\x62"
3775 + "\xaf\xc1\x13\x2d\x7d\xf9\xd1\xb8",
3777 + .plaintext = "abcdefghijklmnopqrstuvwxyz",
3779 + .digest = "\xfd\x2a\xa6\x07\xf7\x1d\xc8\xf5"
3780 + "\x10\x71\x49\x22\xb3\x71\x83\x4e",
3782 + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
3783 + "fghijklmnopqrstuvwxyz0123456789",
3785 + .digest = "\xd1\xe9\x59\xeb\x17\x9c\x91\x1f"
3786 + "\xae\xa4\x62\x4c\x60\xc5\xc7\x02",
3788 + .plaintext = "1234567890123456789012345678901234567890"
3789 + "1234567890123456789012345678901234567890",
3791 + .digest = "\x3f\x45\xef\x19\x47\x32\xc2\xdb"
3792 + "\xb2\xc4\xa2\xc7\x69\x79\x5f\xa3",
3794 + .plaintext = "abcdbcdecdefdefgefghfghighij"
3795 + "hijkijkljklmklmnlmnomnopnopq",
3797 + .digest = "\xa1\xaa\x06\x89\xd0\xfa\xfa\x2d"
3798 + "\xdc\x22\xe8\x8b\x49\x13\x3a\x06",
3800 + .tap = { 28, 28 },
3802 + .plaintext = "abcdefghbcdefghicdefghijdefghijkefghijklfghi"
3803 + "jklmghijklmnhijklmnoijklmnopjklmnopqklmnopqr"
3804 + "lmnopqrsmnopqrstnopqrstu",
3806 + .digest = "\xd4\xec\xc9\x13\xe1\xdf\x77\x6b"
3807 + "\xf4\x8d\xe9\xd5\x5b\x1f\x25\x46",
3809 + .plaintext = "abcdbcdecdefdefgefghfghighijhijk",
3811 + .digest = "\x13\xfc\x13\xe8\xef\xff\x34\x7d"
3812 + "\xe1\x93\xff\x46\xdb\xac\xcf\xd4",
3817 + * RIPEMD-160 test vectors from ISO/IEC 10118-3:2004(E)
3819 +#define RMD160_TEST_VECTORS 10
3821 +static struct hash_testvec rmd160_tv_template[] = {
3823 + .digest = "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28"
3824 + "\x08\x97\x7e\xe8\xf5\x48\xb2\x25\x8d\x31",
3828 + .digest = "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae"
3829 + "\x34\x7b\xe6\xf4\xdc\x83\x5a\x46\x7f\xfe",
3831 + .plaintext = "abc",
3833 + .digest = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04"
3834 + "\x4a\x8e\x98\xc6\xb0\x87\xf1\x5a\x0b\xfc",
3836 + .plaintext = "message digest",
3838 + .digest = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8"
3839 + "\x81\xb1\x23\xa8\x5f\xfa\x21\x59\x5f\x36",
3841 + .plaintext = "abcdefghijklmnopqrstuvwxyz",
3843 + .digest = "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb"
3844 + "\xdc\xeb\x5b\x9d\x28\x65\xb3\x70\x8d\xbc",
3846 + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
3847 + "fghijklmnopqrstuvwxyz0123456789",
3849 + .digest = "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed"
3850 + "\x3a\x87\xa5\x71\x30\x79\xb2\x1f\x51\x89",
3852 + .plaintext = "1234567890123456789012345678901234567890"
3853 + "1234567890123456789012345678901234567890",
3855 + .digest = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb"
3856 + "\xd3\x32\x3c\xab\x82\xbf\x63\x32\x6b\xfb",
3858 + .plaintext = "abcdbcdecdefdefgefghfghighij"
3859 + "hijkijkljklmklmnlmnomnopnopq",
3861 + .digest = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05"
3862 + "\xa0\x6c\x27\xdc\xf4\x9a\xda\x62\xeb\x2b",
3864 + .tap = { 28, 28 },
3866 + .plaintext = "abcdefghbcdefghicdefghijdefghijkefghijklfghi"
3867 + "jklmghijklmnhijklmnoijklmnopjklmnopqklmnopqr"
3868 + "lmnopqrsmnopqrstnopqrstu",
3870 + .digest = "\x6f\x3f\xa3\x9b\x6b\x50\x3c\x38\x4f\x91"
3871 + "\x9a\x49\xa7\xaa\x5c\x2c\x08\xbd\xfb\x45",
3873 + .plaintext = "abcdbcdecdefdefgefghfghighijhijk",
3875 + .digest = "\x94\xc2\x64\x11\x54\x04\xe6\x33\x79\x0d"
3876 + "\xfc\xc8\x7b\x58\x7d\x36\x77\x06\x7d\x9f",
3881 + * RIPEMD-256 test vectors
3883 +#define RMD256_TEST_VECTORS 8
3885 +static struct hash_testvec rmd256_tv_template[] = {
3887 + .digest = "\x02\xba\x4c\x4e\x5f\x8e\xcd\x18"
3888 + "\x77\xfc\x52\xd6\x4d\x30\xe3\x7a"
3889 + "\x2d\x97\x74\xfb\x1e\x5d\x02\x63"
3890 + "\x80\xae\x01\x68\xe3\xc5\x52\x2d",
3894 + .digest = "\xf9\x33\x3e\x45\xd8\x57\xf5\xd9"
3895 + "\x0a\x91\xba\xb7\x0a\x1e\xba\x0c"
3896 + "\xfb\x1b\xe4\xb0\x78\x3c\x9a\xcf"
3897 + "\xcd\x88\x3a\x91\x34\x69\x29\x25",
3899 + .plaintext = "abc",
3901 + .digest = "\xaf\xbd\x6e\x22\x8b\x9d\x8c\xbb"
3902 + "\xce\xf5\xca\x2d\x03\xe6\xdb\xa1"
3903 + "\x0a\xc0\xbc\x7d\xcb\xe4\x68\x0e"
3904 + "\x1e\x42\xd2\xe9\x75\x45\x9b\x65",
3906 + .plaintext = "message digest",
3908 + .digest = "\x87\xe9\x71\x75\x9a\x1c\xe4\x7a"
3909 + "\x51\x4d\x5c\x91\x4c\x39\x2c\x90"
3910 + "\x18\xc7\xc4\x6b\xc1\x44\x65\x55"
3911 + "\x4a\xfc\xdf\x54\xa5\x07\x0c\x0e",
3913 + .plaintext = "abcdefghijklmnopqrstuvwxyz",
3915 + .digest = "\x64\x9d\x30\x34\x75\x1e\xa2\x16"
3916 + "\x77\x6b\xf9\xa1\x8a\xcc\x81\xbc"
3917 + "\x78\x96\x11\x8a\x51\x97\x96\x87"
3918 + "\x82\xdd\x1f\xd9\x7d\x8d\x51\x33",
3920 + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
3921 + "fghijklmnopqrstuvwxyz0123456789",
3923 + .digest = "\x57\x40\xa4\x08\xac\x16\xb7\x20"
3924 + "\xb8\x44\x24\xae\x93\x1c\xbb\x1f"
3925 + "\xe3\x63\xd1\xd0\xbf\x40\x17\xf1"
3926 + "\xa8\x9f\x7e\xa6\xde\x77\xa0\xb8",
3928 + .plaintext = "1234567890123456789012345678901234567890"
3929 + "1234567890123456789012345678901234567890",
3931 + .digest = "\x06\xfd\xcc\x7a\x40\x95\x48\xaa"
3932 + "\xf9\x13\x68\xc0\x6a\x62\x75\xb5"
3933 + "\x53\xe3\xf0\x99\xbf\x0e\xa4\xed"
3934 + "\xfd\x67\x78\xdf\x89\xa8\x90\xdd",
3936 + .plaintext = "abcdbcdecdefdefgefghfghighij"
3937 + "hijkijkljklmklmnlmnomnopnopq",
3939 + .digest = "\x38\x43\x04\x55\x83\xaa\xc6\xc8"
3940 + "\xc8\xd9\x12\x85\x73\xe7\xa9\x80"
3941 + "\x9a\xfb\x2a\x0f\x34\xcc\xc3\x6e"
3942 + "\xa9\xe7\x2f\x16\xf6\x36\x8e\x3f",
3944 + .tap = { 28, 28 },
3949 + * RIPEMD-320 test vectors
3951 +#define RMD320_TEST_VECTORS 8
3953 +static struct hash_testvec rmd320_tv_template[] = {
3955 + .digest = "\x22\xd6\x5d\x56\x61\x53\x6c\xdc\x75\xc1"
3956 + "\xfd\xf5\xc6\xde\x7b\x41\xb9\xf2\x73\x25"
3957 + "\xeb\xc6\x1e\x85\x57\x17\x7d\x70\x5a\x0e"
3958 + "\xc8\x80\x15\x1c\x3a\x32\xa0\x08\x99\xb8",
3962 + .digest = "\xce\x78\x85\x06\x38\xf9\x26\x58\xa5\xa5"
3963 + "\x85\x09\x75\x79\x92\x6d\xda\x66\x7a\x57"
3964 + "\x16\x56\x2c\xfc\xf6\xfb\xe7\x7f\x63\x54"
3965 + "\x2f\x99\xb0\x47\x05\xd6\x97\x0d\xff\x5d",
3967 + .plaintext = "abc",
3969 + .digest = "\xde\x4c\x01\xb3\x05\x4f\x89\x30\xa7\x9d"
3970 + "\x09\xae\x73\x8e\x92\x30\x1e\x5a\x17\x08"
3971 + "\x5b\xef\xfd\xc1\xb8\xd1\x16\x71\x3e\x74"
3972 + "\xf8\x2f\xa9\x42\xd6\x4c\xdb\xc4\x68\x2d",
3974 + .plaintext = "message digest",
3976 + .digest = "\x3a\x8e\x28\x50\x2e\xd4\x5d\x42\x2f\x68"
3977 + "\x84\x4f\x9d\xd3\x16\xe7\xb9\x85\x33\xfa"
3978 + "\x3f\x2a\x91\xd2\x9f\x84\xd4\x25\xc8\x8d"
3979 + "\x6b\x4e\xff\x72\x7d\xf6\x6a\x7c\x01\x97",
3981 + .plaintext = "abcdefghijklmnopqrstuvwxyz",
3983 + .digest = "\xca\xbd\xb1\x81\x0b\x92\x47\x0a\x20\x93"
3984 + "\xaa\x6b\xce\x05\x95\x2c\x28\x34\x8c\xf4"
3985 + "\x3f\xf6\x08\x41\x97\x51\x66\xbb\x40\xed"
3986 + "\x23\x40\x04\xb8\x82\x44\x63\xe6\xb0\x09",
3988 + .plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
3989 + "fghijklmnopqrstuvwxyz0123456789",
3991 + .digest = "\xed\x54\x49\x40\xc8\x6d\x67\xf2\x50\xd2"
3992 + "\x32\xc3\x0b\x7b\x3e\x57\x70\xe0\xc6\x0c"
3993 + "\x8c\xb9\xa4\xca\xfe\x3b\x11\x38\x8a\xf9"
3994 + "\x92\x0e\x1b\x99\x23\x0b\x84\x3c\x86\xa4",
3996 + .plaintext = "1234567890123456789012345678901234567890"
3997 + "1234567890123456789012345678901234567890",
3999 + .digest = "\x55\x78\x88\xaf\x5f\x6d\x8e\xd6\x2a\xb6"
4000 + "\x69\x45\xc6\xd2\xa0\xa4\x7e\xcd\x53\x41"
4001 + "\xe9\x15\xeb\x8f\xea\x1d\x05\x24\x95\x5f"
4002 + "\x82\x5d\xc7\x17\xe4\xa0\x08\xab\x2d\x42",
4004 + .plaintext = "abcdbcdecdefdefgefghfghighij"
4005 + "hijkijkljklmklmnlmnomnopnopq",
4007 + .digest = "\xd0\x34\xa7\x95\x0c\xf7\x22\x02\x1b\xa4"
4008 + "\xb8\x4d\xf7\x69\xa5\xde\x20\x60\xe2\x59"
4009 + "\xdf\x4c\x9b\xb4\xa4\x26\x8c\x0e\x93\x5b"
4010 + "\xbc\x74\x70\xa9\x69\xc9\xd0\x72\xa1\xac",
4012 + .tap = { 28, 28 },
4017 @@ -817,6 +1076,168 @@
4021 + * HMAC-RIPEMD128 test vectors from RFC2286
4023 +#define HMAC_RMD128_TEST_VECTORS 7
4025 +static struct hash_testvec hmac_rmd128_tv_template[] = {
4027 + .key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
4029 + .plaintext = "Hi There",
4031 + .digest = "\xfb\xf6\x1f\x94\x92\xaa\x4b\xbf"
4032 + "\x81\xc1\x72\xe8\x4e\x07\x34\xdb",
4036 + .plaintext = "what do ya want for nothing?",
4038 + .digest = "\x87\x5f\x82\x88\x62\xb6\xb3\x34"
4039 + "\xb4\x27\xc5\x5f\x9f\x7f\xf0\x9b",
4041 + .tap = { 14, 14 },
4043 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
4045 + .plaintext = "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4046 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4047 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4048 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
4050 + .digest = "\x09\xf0\xb2\x84\x6d\x2f\x54\x3d"
4051 + "\xa3\x63\xcb\xec\x8d\x62\xa3\x8d",
4053 + .key = "\x01\x02\x03\x04\x05\x06\x07\x08"
4054 + "\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
4055 + "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
4057 + .plaintext = "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4058 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4059 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4060 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
4062 + .digest = "\xbd\xbb\xd7\xcf\x03\xe4\x4b\x5a"
4063 + "\xa6\x0a\xf8\x15\xbe\x4d\x22\x94",
4065 + .key = "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
4067 + .plaintext = "Test With Truncation",
4069 + .digest = "\xe7\x98\x08\xf2\x4b\x25\xfd\x03"
4070 + "\x1c\x15\x5f\x0d\x55\x1d\x9a\x3a",
4072 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4073 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4074 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4075 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4076 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4077 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4080 + .plaintext = "Test Using Larger Than Block-Size Key - Hash Key First",
4082 + .digest = "\xdc\x73\x29\x28\xde\x98\x10\x4a"
4083 + "\x1f\x59\xd3\x73\xc1\x50\xac\xbb",
4085 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4086 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4087 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4088 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4089 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4090 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4093 + .plaintext = "Test Using Larger Than Block-Size Key and Larger Than One "
4094 + "Block-Size Data",
4096 + .digest = "\x5c\x6b\xec\x96\x79\x3e\x16\xd4"
4097 + "\x06\x90\xc2\x37\x63\x5f\x30\xc5",
4102 + * HMAC-RIPEMD160 test vectors from RFC2286
4104 +#define HMAC_RMD160_TEST_VECTORS 7
4106 +static struct hash_testvec hmac_rmd160_tv_template[] = {
4108 + .key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
4110 + .plaintext = "Hi There",
4112 + .digest = "\x24\xcb\x4b\xd6\x7d\x20\xfc\x1a\x5d\x2e"
4113 + "\xd7\x73\x2d\xcc\x39\x37\x7f\x0a\x56\x68",
4117 + .plaintext = "what do ya want for nothing?",
4119 + .digest = "\xdd\xa6\xc0\x21\x3a\x48\x5a\x9e\x24\xf4"
4120 + "\x74\x20\x64\xa7\xf0\x33\xb4\x3c\x40\x69",
4122 + .tap = { 14, 14 },
4124 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
4126 + .plaintext = "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4127 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4128 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
4129 + "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
4131 + .digest = "\xb0\xb1\x05\x36\x0d\xe7\x59\x96\x0a\xb4"
4132 + "\xf3\x52\x98\xe1\x16\xe2\x95\xd8\xe7\xc1",
4134 + .key = "\x01\x02\x03\x04\x05\x06\x07\x08"
4135 + "\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
4136 + "\x11\x12\x13\x14\x15\x16\x17\x18\x19",
4138 + .plaintext = "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4139 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4140 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
4141 + "\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd",
4143 + .digest = "\xd5\xca\x86\x2f\x4d\x21\xd5\xe6\x10\xe1"
4144 + "\x8b\x4c\xf1\xbe\xb9\x7a\x43\x65\xec\xf4",
4146 + .key = "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c",
4148 + .plaintext = "Test With Truncation",
4150 + .digest = "\x76\x19\x69\x39\x78\xf9\x1d\x90\x53\x9a"
4151 + "\xe7\x86\x50\x0f\xf3\xd8\xe0\x51\x8e\x39",
4153 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4154 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4155 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4156 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4157 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4158 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4161 + .plaintext = "Test Using Larger Than Block-Size Key - Hash Key First",
4163 + .digest = "\x64\x66\xca\x07\xac\x5e\xac\x29\xe1\xbd"
4164 + "\x52\x3e\x5a\xda\x76\x05\xb7\x91\xfd\x8b",
4166 + .key = "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4167 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4168 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4169 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4170 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4171 + "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
4174 + .plaintext = "Test Using Larger Than Block-Size Key and Larger Than One "
4175 + "Block-Size Data",
4177 + .digest = "\x69\xea\x60\x79\x8d\x71\x61\x6c\xce\x5f"
4178 + "\xd0\x87\x1e\x23\x75\x4c\xd7\x5d\x5a\x0a",
4183 * HMAC-SHA1 test vectors from RFC2202
4185 #define HMAC_SHA1_TEST_VECTORS 7
4186 @@ -1442,6 +1863,8 @@
4187 #define DES_CBC_DEC_TEST_VECTORS 4
4188 #define DES3_EDE_ENC_TEST_VECTORS 3
4189 #define DES3_EDE_DEC_TEST_VECTORS 3
4190 +#define DES3_EDE_CBC_ENC_TEST_VECTORS 1
4191 +#define DES3_EDE_CBC_DEC_TEST_VECTORS 1
4193 static struct cipher_testvec des_enc_tv_template[] = {
4194 { /* From Applied Cryptography */
4195 @@ -1680,9 +2103,6 @@
4200 - * We really need some more test vectors, especially for DES3 CBC.
4202 static struct cipher_testvec des3_ede_enc_tv_template[] = {
4203 { /* These are from openssl */
4204 .key = "\x01\x23\x45\x67\x89\xab\xcd\xef"
4205 @@ -1745,6 +2165,94 @@
4209 +static struct cipher_testvec des3_ede_cbc_enc_tv_template[] = {
4210 + { /* Generated from openssl */
4211 + .key = "\xE9\xC0\xFF\x2E\x76\x0B\x64\x24"
4212 + "\x44\x4D\x99\x5A\x12\xD6\x40\xC0"
4213 + "\xEA\xC2\x84\xE8\x14\x95\xDB\xE8",
4215 + .iv = "\x7D\x33\x88\x93\x0F\x93\xB2\x42",
4216 + .input = "\x6f\x54\x20\x6f\x61\x4d\x79\x6e"
4217 + "\x53\x20\x63\x65\x65\x72\x73\x74"
4218 + "\x54\x20\x6f\x6f\x4d\x20\x6e\x61"
4219 + "\x20\x79\x65\x53\x72\x63\x74\x65"
4220 + "\x20\x73\x6f\x54\x20\x6f\x61\x4d"
4221 + "\x79\x6e\x53\x20\x63\x65\x65\x72"
4222 + "\x73\x74\x54\x20\x6f\x6f\x4d\x20"
4223 + "\x6e\x61\x20\x79\x65\x53\x72\x63"
4224 + "\x74\x65\x20\x73\x6f\x54\x20\x6f"
4225 + "\x61\x4d\x79\x6e\x53\x20\x63\x65"
4226 + "\x65\x72\x73\x74\x54\x20\x6f\x6f"
4227 + "\x4d\x20\x6e\x61\x20\x79\x65\x53"
4228 + "\x72\x63\x74\x65\x20\x73\x6f\x54"
4229 + "\x20\x6f\x61\x4d\x79\x6e\x53\x20"
4230 + "\x63\x65\x65\x72\x73\x74\x54\x20"
4231 + "\x6f\x6f\x4d\x20\x6e\x61\x0a\x79",
4233 + .result = "\x0e\x2d\xb6\x97\x3c\x56\x33\xf4"
4234 + "\x67\x17\x21\xc7\x6e\x8a\xd5\x49"
4235 + "\x74\xb3\x49\x05\xc5\x1c\xd0\xed"
4236 + "\x12\x56\x5c\x53\x96\xb6\x00\x7d"
4237 + "\x90\x48\xfc\xf5\x8d\x29\x39\xcc"
4238 + "\x8a\xd5\x35\x18\x36\x23\x4e\xd7"
4239 + "\x76\xd1\xda\x0c\x94\x67\xbb\x04"
4240 + "\x8b\xf2\x03\x6c\xa8\xcf\xb6\xea"
4241 + "\x22\x64\x47\xaa\x8f\x75\x13\xbf"
4242 + "\x9f\xc2\xc3\xf0\xc9\x56\xc5\x7a"
4243 + "\x71\x63\x2e\x89\x7b\x1e\x12\xca"
4244 + "\xe2\x5f\xaf\xd8\xa4\xf8\xc9\x7a"
4245 + "\xd6\xf9\x21\x31\x62\x44\x45\xa6"
4246 + "\xd6\xbc\x5a\xd3\x2d\x54\x43\xcc"
4247 + "\x9d\xde\xa5\x70\xe9\x42\x45\x8a"
4248 + "\x6b\xfa\xb1\x91\x13\xb0\xd9\x19",
4253 +static struct cipher_testvec des3_ede_cbc_dec_tv_template[] = {
4254 + { /* Generated from openssl */
4255 + .key = "\xE9\xC0\xFF\x2E\x76\x0B\x64\x24"
4256 + "\x44\x4D\x99\x5A\x12\xD6\x40\xC0"
4257 + "\xEA\xC2\x84\xE8\x14\x95\xDB\xE8",
4259 + .iv = "\x7D\x33\x88\x93\x0F\x93\xB2\x42",
4260 + .input = "\x0e\x2d\xb6\x97\x3c\x56\x33\xf4"
4261 + "\x67\x17\x21\xc7\x6e\x8a\xd5\x49"
4262 + "\x74\xb3\x49\x05\xc5\x1c\xd0\xed"
4263 + "\x12\x56\x5c\x53\x96\xb6\x00\x7d"
4264 + "\x90\x48\xfc\xf5\x8d\x29\x39\xcc"
4265 + "\x8a\xd5\x35\x18\x36\x23\x4e\xd7"
4266 + "\x76\xd1\xda\x0c\x94\x67\xbb\x04"
4267 + "\x8b\xf2\x03\x6c\xa8\xcf\xb6\xea"
4268 + "\x22\x64\x47\xaa\x8f\x75\x13\xbf"
4269 + "\x9f\xc2\xc3\xf0\xc9\x56\xc5\x7a"
4270 + "\x71\x63\x2e\x89\x7b\x1e\x12\xca"
4271 + "\xe2\x5f\xaf\xd8\xa4\xf8\xc9\x7a"
4272 + "\xd6\xf9\x21\x31\x62\x44\x45\xa6"
4273 + "\xd6\xbc\x5a\xd3\x2d\x54\x43\xcc"
4274 + "\x9d\xde\xa5\x70\xe9\x42\x45\x8a"
4275 + "\x6b\xfa\xb1\x91\x13\xb0\xd9\x19",
4277 + .result = "\x6f\x54\x20\x6f\x61\x4d\x79\x6e"
4278 + "\x53\x20\x63\x65\x65\x72\x73\x74"
4279 + "\x54\x20\x6f\x6f\x4d\x20\x6e\x61"
4280 + "\x20\x79\x65\x53\x72\x63\x74\x65"
4281 + "\x20\x73\x6f\x54\x20\x6f\x61\x4d"
4282 + "\x79\x6e\x53\x20\x63\x65\x65\x72"
4283 + "\x73\x74\x54\x20\x6f\x6f\x4d\x20"
4284 + "\x6e\x61\x20\x79\x65\x53\x72\x63"
4285 + "\x74\x65\x20\x73\x6f\x54\x20\x6f"
4286 + "\x61\x4d\x79\x6e\x53\x20\x63\x65"
4287 + "\x65\x72\x73\x74\x54\x20\x6f\x6f"
4288 + "\x4d\x20\x6e\x61\x20\x79\x65\x53"
4289 + "\x72\x63\x74\x65\x20\x73\x6f\x54"
4290 + "\x20\x6f\x61\x4d\x79\x6e\x53\x20"
4291 + "\x63\x65\x65\x72\x73\x74\x54\x20"
4292 + "\x6f\x6f\x4d\x20\x6e\x61\x0a\x79",
4298 * Blowfish test vectors.
4300 --- a/drivers/crypto/Kconfig
4301 +++ b/drivers/crypto/Kconfig
4302 @@ -174,4 +174,30 @@
4303 Select this option if you want to enable the random number generator
4304 on the HIFN 795x crypto adapters.
4306 +config CRYPTO_DEV_TALITOS
4307 + tristate "Talitos Freescale Security Engine (SEC)"
4308 + select CRYPTO_ALGAPI
4309 + select CRYPTO_AUTHENC
4311 + depends on FSL_SOC
4313 + Say 'Y' here to use the Freescale Security Engine (SEC)
4314 + to offload cryptographic algorithm computation.
4316 + The Freescale SEC is present on PowerQUICC 'E' processors, such
4317 + as the MPC8349E and MPC8548E.
4319 + To compile this driver as a module, choose M here: the module
4320 + will be called talitos.
4322 +config CRYPTO_DEV_IXP4XX
4323 + tristate "Driver for IXP4xx crypto hardware acceleration"
4324 + depends on ARCH_IXP4XX
4326 + select CRYPTO_ALGAPI
4327 + select CRYPTO_AUTHENC
4328 + select CRYPTO_BLKCIPHER
4330 + Driver for the IXP4xx NPE crypto engine.
4333 --- a/drivers/crypto/Makefile
4334 +++ b/drivers/crypto/Makefile
4336 obj-$(CONFIG_CRYPTO_DEV_PADLOCK_SHA) += padlock-sha.o
4337 obj-$(CONFIG_CRYPTO_DEV_GEODE) += geode-aes.o
4338 obj-$(CONFIG_CRYPTO_DEV_HIFN_795X) += hifn_795x.o
4339 +obj-$(CONFIG_CRYPTO_DEV_TALITOS) += talitos.o
4340 +obj-$(CONFIG_CRYPTO_DEV_IXP4XX) += ixp4xx_crypto.o
4341 --- a/drivers/crypto/hifn_795x.c
4342 +++ b/drivers/crypto/hifn_795x.c
4344 #include <linux/dma-mapping.h>
4345 #include <linux/scatterlist.h>
4346 #include <linux/highmem.h>
4347 -#include <linux/interrupt.h>
4348 #include <linux/crypto.h>
4349 #include <linux/hw_random.h>
4350 #include <linux/ktime.h>
4352 #define HIFN_D_DST_RSIZE 80*4
4353 #define HIFN_D_RES_RSIZE 24*4
4355 -#define HIFN_QUEUE_LENGTH HIFN_D_CMD_RSIZE-5
4356 +#define HIFN_D_DST_DALIGN 4
4358 +#define HIFN_QUEUE_LENGTH HIFN_D_CMD_RSIZE-1
4360 #define AES_MIN_KEY_SIZE 16
4361 #define AES_MAX_KEY_SIZE 32
4362 @@ -535,10 +536,10 @@
4364 struct hifn_mac_command
4366 - volatile u16 masks;
4367 - volatile u16 header_skip;
4368 - volatile u16 source_count;
4369 - volatile u16 reserved;
4370 + volatile __le16 masks;
4371 + volatile __le16 header_skip;
4372 + volatile __le16 source_count;
4373 + volatile __le16 reserved;
4376 #define HIFN_MAC_CMD_ALG_MASK 0x0001
4377 @@ -564,10 +565,10 @@
4379 struct hifn_comp_command
4381 - volatile u16 masks;
4382 - volatile u16 header_skip;
4383 - volatile u16 source_count;
4384 - volatile u16 reserved;
4385 + volatile __le16 masks;
4386 + volatile __le16 header_skip;
4387 + volatile __le16 source_count;
4388 + volatile __le16 reserved;
4391 #define HIFN_COMP_CMD_SRCLEN_M 0xc000
4392 @@ -583,10 +584,10 @@
4394 struct hifn_base_result
4396 - volatile u16 flags;
4397 - volatile u16 session;
4398 - volatile u16 src_cnt; /* 15:0 of source count */
4399 - volatile u16 dst_cnt; /* 15:0 of dest count */
4400 + volatile __le16 flags;
4401 + volatile __le16 session;
4402 + volatile __le16 src_cnt; /* 15:0 of source count */
4403 + volatile __le16 dst_cnt; /* 15:0 of dest count */
4406 #define HIFN_BASE_RES_DSTOVERRUN 0x0200 /* destination overrun */
4409 struct hifn_comp_result
4411 - volatile u16 flags;
4413 + volatile __le16 flags;
4414 + volatile __le16 crc;
4417 #define HIFN_COMP_RES_LCB_M 0xff00 /* longitudinal check byte */
4420 struct hifn_mac_result
4422 - volatile u16 flags;
4423 - volatile u16 reserved;
4424 + volatile __le16 flags;
4425 + volatile __le16 reserved;
4426 /* followed by 0, 6, 8, or 10 u16's of the MAC, then crypt */
4431 struct hifn_crypt_result
4433 - volatile u16 flags;
4434 - volatile u16 reserved;
4435 + volatile __le16 flags;
4436 + volatile __le16 reserved;
4439 #define HIFN_CRYPT_RES_SRC_NOTZERO 0x0001 /* source expired */
4440 @@ -686,12 +687,12 @@
4442 static inline void hifn_write_0(struct hifn_device *dev, u32 reg, u32 val)
4444 - writel(val, dev->bar[0] + reg);
4445 + writel((__force u32)cpu_to_le32(val), dev->bar[0] + reg);
4448 static inline void hifn_write_1(struct hifn_device *dev, u32 reg, u32 val)
4450 - writel(val, dev->bar[1] + reg);
4451 + writel((__force u32)cpu_to_le32(val), dev->bar[1] + reg);
4454 static void hifn_wait_puc(struct hifn_device *dev)
4456 char *offtbl = NULL;
4459 - for (i = 0; i < sizeof(pci2id)/sizeof(pci2id[0]); i++) {
4460 + for (i = 0; i < ARRAY_SIZE(pci2id); i++) {
4461 if (pci2id[i].pci_vendor == dev->pdev->vendor &&
4462 pci2id[i].pci_prod == dev->pdev->device) {
4463 offtbl = pci2id[i].card_id;
4464 @@ -1037,14 +1038,14 @@
4465 hifn_write_0(dev, HIFN_0_PUIER, HIFN_PUIER_DSTOVER);
4467 /* write all 4 ring address registers */
4468 - hifn_write_1(dev, HIFN_1_DMA_CRAR, __cpu_to_le32(dptr +
4469 - offsetof(struct hifn_dma, cmdr[0])));
4470 - hifn_write_1(dev, HIFN_1_DMA_SRAR, __cpu_to_le32(dptr +
4471 - offsetof(struct hifn_dma, srcr[0])));
4472 - hifn_write_1(dev, HIFN_1_DMA_DRAR, __cpu_to_le32(dptr +
4473 - offsetof(struct hifn_dma, dstr[0])));
4474 - hifn_write_1(dev, HIFN_1_DMA_RRAR, __cpu_to_le32(dptr +
4475 - offsetof(struct hifn_dma, resr[0])));
4476 + hifn_write_1(dev, HIFN_1_DMA_CRAR, dptr +
4477 + offsetof(struct hifn_dma, cmdr[0]));
4478 + hifn_write_1(dev, HIFN_1_DMA_SRAR, dptr +
4479 + offsetof(struct hifn_dma, srcr[0]));
4480 + hifn_write_1(dev, HIFN_1_DMA_DRAR, dptr +
4481 + offsetof(struct hifn_dma, dstr[0]));
4482 + hifn_write_1(dev, HIFN_1_DMA_RRAR, dptr +
4483 + offsetof(struct hifn_dma, resr[0]));
4487 @@ -1166,109 +1167,15 @@
4491 -static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
4492 - unsigned int offset, unsigned int size)
4494 - struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4498 - addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
4502 - dma->srcr[idx].p = __cpu_to_le32(addr);
4503 - dma->srcr[idx].l = __cpu_to_le32(size) | HIFN_D_VALID |
4504 - HIFN_D_MASKDONEIRQ | HIFN_D_NOINVALID | HIFN_D_LAST;
4506 - if (++idx == HIFN_D_SRC_RSIZE) {
4507 - dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
4509 - HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4516 - if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
4517 - hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
4518 - dev->flags |= HIFN_FLAG_SRC_BUSY;
4524 -static void hifn_setup_res_desc(struct hifn_device *dev)
4526 - struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4528 - dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
4529 - HIFN_D_VALID | HIFN_D_LAST);
4531 - * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
4532 - * HIFN_D_LAST | HIFN_D_NOINVALID);
4535 - if (++dma->resi == HIFN_D_RES_RSIZE) {
4536 - dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
4537 - HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4543 - if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
4544 - hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
4545 - dev->flags |= HIFN_FLAG_RES_BUSY;
4549 -static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
4550 - unsigned offset, unsigned size)
4552 - struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4556 - addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
4559 - dma->dstr[idx].p = __cpu_to_le32(addr);
4560 - dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
4561 - HIFN_D_MASKDONEIRQ | HIFN_D_NOINVALID | HIFN_D_LAST);
4563 - if (++idx == HIFN_D_DST_RSIZE) {
4564 - dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
4565 - HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
4566 - HIFN_D_LAST | HIFN_D_NOINVALID);
4572 - if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
4573 - hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
4574 - dev->flags |= HIFN_FLAG_DST_BUSY;
4578 -static int hifn_setup_dma(struct hifn_device *dev, struct page *spage, unsigned int soff,
4579 - struct page *dpage, unsigned int doff, unsigned int nbytes, void *priv,
4580 - struct hifn_context *ctx)
4581 +static int hifn_setup_cmd_desc(struct hifn_device *dev,
4582 + struct hifn_context *ctx, void *priv, unsigned int nbytes)
4584 struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4585 int cmd_len, sa_idx;
4589 - dprintk("%s: spage: %p, soffset: %u, dpage: %p, doffset: %u, nbytes: %u, priv: %p, ctx: %p.\n",
4590 - dev->name, spage, soff, dpage, doff, nbytes, priv, ctx);
4592 - sa_idx = dma->resi;
4594 - hifn_setup_src_desc(dev, spage, soff, nbytes);
4596 + sa_idx = dma->cmdi;
4597 buf_pos = buf = dma->command_bufs[dma->cmdi];
4600 @@ -1370,16 +1277,113 @@
4601 hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_C_CTRL_ENA);
4602 dev->flags |= HIFN_FLAG_CMD_BUSY;
4605 - hifn_setup_dst_desc(dev, dpage, doff, nbytes);
4606 - hifn_setup_res_desc(dev);
4614 +static int hifn_setup_src_desc(struct hifn_device *dev, struct page *page,
4615 + unsigned int offset, unsigned int size)
4617 + struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4621 + addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_TODEVICE);
4625 + dma->srcr[idx].p = __cpu_to_le32(addr);
4626 + dma->srcr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
4627 + HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4629 + if (++idx == HIFN_D_SRC_RSIZE) {
4630 + dma->srcr[idx].l = __cpu_to_le32(HIFN_D_VALID |
4632 + HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4639 + if (!(dev->flags & HIFN_FLAG_SRC_BUSY)) {
4640 + hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_S_CTRL_ENA);
4641 + dev->flags |= HIFN_FLAG_SRC_BUSY;
4647 +static void hifn_setup_res_desc(struct hifn_device *dev)
4649 + struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4651 + dma->resr[dma->resi].l = __cpu_to_le32(HIFN_USED_RESULT |
4652 + HIFN_D_VALID | HIFN_D_LAST);
4654 + * dma->resr[dma->resi].l = __cpu_to_le32(HIFN_MAX_RESULT | HIFN_D_VALID |
4658 + if (++dma->resi == HIFN_D_RES_RSIZE) {
4659 + dma->resr[HIFN_D_RES_RSIZE].l = __cpu_to_le32(HIFN_D_VALID |
4660 + HIFN_D_JUMP | HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4666 + if (!(dev->flags & HIFN_FLAG_RES_BUSY)) {
4667 + hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_R_CTRL_ENA);
4668 + dev->flags |= HIFN_FLAG_RES_BUSY;
4672 +static void hifn_setup_dst_desc(struct hifn_device *dev, struct page *page,
4673 + unsigned offset, unsigned size)
4675 + struct hifn_dma *dma = (struct hifn_dma *)dev->desc_virt;
4679 + addr = pci_map_page(dev->pdev, page, offset, size, PCI_DMA_FROMDEVICE);
4682 + dma->dstr[idx].p = __cpu_to_le32(addr);
4683 + dma->dstr[idx].l = __cpu_to_le32(size | HIFN_D_VALID |
4684 + HIFN_D_MASKDONEIRQ | HIFN_D_LAST);
4686 + if (++idx == HIFN_D_DST_RSIZE) {
4687 + dma->dstr[idx].l = __cpu_to_le32(HIFN_D_VALID |
4688 + HIFN_D_JUMP | HIFN_D_MASKDONEIRQ |
4695 + if (!(dev->flags & HIFN_FLAG_DST_BUSY)) {
4696 + hifn_write_1(dev, HIFN_1_DMA_CSR, HIFN_DMACSR_D_CTRL_ENA);
4697 + dev->flags |= HIFN_FLAG_DST_BUSY;
4701 +static int hifn_setup_dma(struct hifn_device *dev, struct page *spage, unsigned int soff,
4702 + struct page *dpage, unsigned int doff, unsigned int nbytes, void *priv,
4703 + struct hifn_context *ctx)
4705 + dprintk("%s: spage: %p, soffset: %u, dpage: %p, doffset: %u, nbytes: %u, priv: %p, ctx: %p.\n",
4706 + dev->name, spage, soff, dpage, doff, nbytes, priv, ctx);
4708 + hifn_setup_src_desc(dev, spage, soff, nbytes);
4709 + hifn_setup_cmd_desc(dev, ctx, priv, nbytes);
4710 + hifn_setup_dst_desc(dev, dpage, doff, nbytes);
4711 + hifn_setup_res_desc(dev);
4715 static int ablkcipher_walk_init(struct ablkcipher_walk *w,
4716 int num, gfp_t gfp_flags)
4718 @@ -1431,7 +1435,7 @@
4722 - copy = min(drest, src->length);
4723 + copy = min(drest, min(size, src->length));
4725 saddr = kmap_atomic(sg_page(src), KM_SOFTIRQ1);
4726 memcpy(daddr, saddr + src->offset, copy);
4727 @@ -1458,10 +1462,6 @@
4728 static int ablkcipher_walk(struct ablkcipher_request *req,
4729 struct ablkcipher_walk *w)
4731 - unsigned blocksize =
4732 - crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
4733 - unsigned alignmask =
4734 - crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
4735 struct scatterlist *src, *dst, *t;
4737 unsigned int nbytes = req->nbytes, offset, copy, diff;
4738 @@ -1477,16 +1477,14 @@
4739 dst = &req->dst[idx];
4741 dprintk("\n%s: slen: %u, dlen: %u, soff: %u, doff: %u, offset: %u, "
4742 - "blocksize: %u, nbytes: %u.\n",
4744 __func__, src->length, dst->length, src->offset,
4745 - dst->offset, offset, blocksize, nbytes);
4746 + dst->offset, offset, nbytes);
4748 - if (src->length & (blocksize - 1) ||
4749 - src->offset & (alignmask - 1) ||
4750 - dst->length & (blocksize - 1) ||
4751 - dst->offset & (alignmask - 1) ||
4753 - unsigned slen = src->length - offset;
4754 + if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
4755 + !IS_ALIGNED(dst->length, HIFN_D_DST_DALIGN) ||
4757 + unsigned slen = min(src->length - offset, nbytes);
4758 unsigned dlen = PAGE_SIZE;
4761 @@ -1498,8 +1496,8 @@
4765 - copy = slen & ~(blocksize - 1);
4766 - diff = slen & (blocksize - 1);
4767 + copy = slen & ~(HIFN_D_DST_DALIGN - 1);
4768 + diff = slen & (HIFN_D_DST_DALIGN - 1);
4770 if (dlen < nbytes) {
4772 @@ -1507,7 +1505,7 @@
4773 * to put there additional blocksized chunk,
4774 * so we mark that page as containing only
4775 * blocksize aligned chunks:
4776 - * t->length = (slen & ~(blocksize - 1));
4777 + * t->length = (slen & ~(HIFN_D_DST_DALIGN - 1));
4778 * and increase number of bytes to be processed
4781 @@ -1544,7 +1542,7 @@
4783 kunmap_atomic(daddr, KM_SOFTIRQ0);
4785 - nbytes -= src->length;
4786 + nbytes -= min(src->length, nbytes);
4790 @@ -1563,14 +1561,10 @@
4791 struct hifn_context *ctx = crypto_tfm_ctx(req->base.tfm);
4792 struct hifn_device *dev = ctx->dev;
4793 struct page *spage, *dpage;
4794 - unsigned long soff, doff, flags;
4795 + unsigned long soff, doff, dlen, flags;
4796 unsigned int nbytes = req->nbytes, idx = 0, len;
4797 int err = -EINVAL, sg_num;
4798 struct scatterlist *src, *dst, *t;
4799 - unsigned blocksize =
4800 - crypto_ablkcipher_blocksize(crypto_ablkcipher_reqtfm(req));
4801 - unsigned alignmask =
4802 - crypto_ablkcipher_alignmask(crypto_ablkcipher_reqtfm(req));
4804 if (ctx->iv && !ctx->ivsize && ctx->mode != ACRYPTO_MODE_ECB)
4806 @@ -1578,17 +1572,14 @@
4807 ctx->walk.flags = 0;
4810 - src = &req->src[idx];
4811 dst = &req->dst[idx];
4812 + dlen = min(dst->length, nbytes);
4814 - if (src->length & (blocksize - 1) ||
4815 - src->offset & (alignmask - 1) ||
4816 - dst->length & (blocksize - 1) ||
4817 - dst->offset & (alignmask - 1)) {
4818 + if (!IS_ALIGNED(dst->offset, HIFN_D_DST_DALIGN) ||
4819 + !IS_ALIGNED(dlen, HIFN_D_DST_DALIGN))
4820 ctx->walk.flags |= ASYNC_FLAGS_MISALIGNED;
4823 - nbytes -= src->length;
4828 @@ -1602,7 +1593,10 @@
4831 sg_num = ablkcipher_walk(req, &ctx->walk);
4835 + goto err_out_exit;
4837 atomic_set(&ctx->sg_num, sg_num);
4839 spin_lock_irqsave(&dev->lock, flags);
4840 @@ -1640,7 +1634,7 @@
4845 + nbytes -= min(len, nbytes);
4848 dev->active = HIFN_DEFAULT_ACTIVE_NUM;
4849 @@ -1651,7 +1645,7 @@
4851 spin_unlock_irqrestore(&dev->lock, flags);
4853 - if (err && printk_ratelimit())
4855 dprintk("%s: iv: %p [%d], key: %p [%d], mode: %u, op: %u, "
4856 "type: %u, err: %d.\n",
4857 dev->name, ctx->iv, ctx->ivsize,
4858 @@ -1745,8 +1739,7 @@
4863 - copy = min(dst->length, srest);
4864 + copy = min(srest, min(dst->length, size));
4866 daddr = kmap_atomic(sg_page(dst), KM_IRQ0);
4867 memcpy(daddr + dst->offset + offset, saddr, copy);
4868 @@ -1803,7 +1796,7 @@
4869 sg_page(dst), dst->length, nbytes);
4872 - nbytes -= dst->length;
4873 + nbytes -= min(dst->length, nbytes);
4877 @@ -2202,9 +2195,9 @@
4880 if (dev->started < HIFN_QUEUE_LENGTH && dev->queue.qlen)
4881 - err = hifn_process_queue(dev);
4882 + hifn_process_queue(dev);
4885 + return -EINPROGRESS;
4889 @@ -2364,7 +2357,7 @@
4890 * 3DES ECB, CBC, CFB and OFB modes.
4893 - .name = "cfb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
4894 + .name = "cfb(des3_ede)", .drv_name = "cfb-3des", .bsize = 8,
4896 .min_keysize = HIFN_3DES_KEY_LENGTH,
4897 .max_keysize = HIFN_3DES_KEY_LENGTH,
4898 @@ -2374,7 +2367,7 @@
4902 - .name = "ofb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
4903 + .name = "ofb(des3_ede)", .drv_name = "ofb-3des", .bsize = 8,
4905 .min_keysize = HIFN_3DES_KEY_LENGTH,
4906 .max_keysize = HIFN_3DES_KEY_LENGTH,
4907 @@ -2384,8 +2377,9 @@
4911 - .name = "cbc(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
4912 + .name = "cbc(des3_ede)", .drv_name = "cbc-3des", .bsize = 8,
4914 + .ivsize = HIFN_IV_LENGTH,
4915 .min_keysize = HIFN_3DES_KEY_LENGTH,
4916 .max_keysize = HIFN_3DES_KEY_LENGTH,
4917 .setkey = hifn_setkey,
4918 @@ -2394,7 +2388,7 @@
4922 - .name = "ecb(des3_ede)", .drv_name = "hifn-3des", .bsize = 8,
4923 + .name = "ecb(des3_ede)", .drv_name = "ecb-3des", .bsize = 8,
4925 .min_keysize = HIFN_3DES_KEY_LENGTH,
4926 .max_keysize = HIFN_3DES_KEY_LENGTH,
4927 @@ -2408,7 +2402,7 @@
4928 * DES ECB, CBC, CFB and OFB modes.
4931 - .name = "cfb(des)", .drv_name = "hifn-des", .bsize = 8,
4932 + .name = "cfb(des)", .drv_name = "cfb-des", .bsize = 8,
4934 .min_keysize = HIFN_DES_KEY_LENGTH,
4935 .max_keysize = HIFN_DES_KEY_LENGTH,
4936 @@ -2418,7 +2412,7 @@
4940 - .name = "ofb(des)", .drv_name = "hifn-des", .bsize = 8,
4941 + .name = "ofb(des)", .drv_name = "ofb-des", .bsize = 8,
4943 .min_keysize = HIFN_DES_KEY_LENGTH,
4944 .max_keysize = HIFN_DES_KEY_LENGTH,
4945 @@ -2428,8 +2422,9 @@
4949 - .name = "cbc(des)", .drv_name = "hifn-des", .bsize = 8,
4950 + .name = "cbc(des)", .drv_name = "cbc-des", .bsize = 8,
4952 + .ivsize = HIFN_IV_LENGTH,
4953 .min_keysize = HIFN_DES_KEY_LENGTH,
4954 .max_keysize = HIFN_DES_KEY_LENGTH,
4955 .setkey = hifn_setkey,
4956 @@ -2438,7 +2433,7 @@
4960 - .name = "ecb(des)", .drv_name = "hifn-des", .bsize = 8,
4961 + .name = "ecb(des)", .drv_name = "ecb-des", .bsize = 8,
4963 .min_keysize = HIFN_DES_KEY_LENGTH,
4964 .max_keysize = HIFN_DES_KEY_LENGTH,
4965 @@ -2452,7 +2447,7 @@
4966 * AES ECB, CBC, CFB and OFB modes.
4969 - .name = "ecb(aes)", .drv_name = "hifn-aes", .bsize = 16,
4970 + .name = "ecb(aes)", .drv_name = "ecb-aes", .bsize = 16,
4972 .min_keysize = AES_MIN_KEY_SIZE,
4973 .max_keysize = AES_MAX_KEY_SIZE,
4974 @@ -2462,8 +2457,9 @@
4978 - .name = "cbc(aes)", .drv_name = "hifn-aes", .bsize = 16,
4979 + .name = "cbc(aes)", .drv_name = "cbc-aes", .bsize = 16,
4981 + .ivsize = HIFN_AES_IV_LENGTH,
4982 .min_keysize = AES_MIN_KEY_SIZE,
4983 .max_keysize = AES_MAX_KEY_SIZE,
4984 .setkey = hifn_setkey,
4985 @@ -2472,7 +2468,7 @@
4989 - .name = "cfb(aes)", .drv_name = "hifn-aes", .bsize = 16,
4990 + .name = "cfb(aes)", .drv_name = "cfb-aes", .bsize = 16,
4992 .min_keysize = AES_MIN_KEY_SIZE,
4993 .max_keysize = AES_MAX_KEY_SIZE,
4994 @@ -2482,7 +2478,7 @@
4998 - .name = "ofb(aes)", .drv_name = "hifn-aes", .bsize = 16,
4999 + .name = "ofb(aes)", .drv_name = "ofb-aes", .bsize = 16,
5001 .min_keysize = AES_MIN_KEY_SIZE,
5002 .max_keysize = AES_MAX_KEY_SIZE,
5003 @@ -2514,15 +2510,14 @@
5006 snprintf(alg->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s", t->name);
5007 - snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", t->drv_name);
5008 + snprintf(alg->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s-%s",
5009 + t->drv_name, dev->name);
5011 alg->alg.cra_priority = 300;
5012 alg->alg.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC;
5013 alg->alg.cra_blocksize = t->bsize;
5014 alg->alg.cra_ctxsize = sizeof(struct hifn_context);
5015 - alg->alg.cra_alignmask = 15;
5016 - if (t->bsize == 8)
5017 - alg->alg.cra_alignmask = 3;
5018 + alg->alg.cra_alignmask = 0;
5019 alg->alg.cra_type = &crypto_ablkcipher_type;
5020 alg->alg.cra_module = THIS_MODULE;
5021 alg->alg.cra_u.ablkcipher = t->ablkcipher;
5023 +++ b/drivers/crypto/ixp4xx_crypto.c
5026 + * Intel IXP4xx NPE-C crypto driver
5028 + * Copyright (C) 2008 Christian Hohnstaedt <chohnstaedt@innominate.com>
5030 + * This program is free software; you can redistribute it and/or modify it
5031 + * under the terms of version 2 of the GNU General Public License
5032 + * as published by the Free Software Foundation.
5036 +#include <linux/platform_device.h>
5037 +#include <linux/dma-mapping.h>
5038 +#include <linux/dmapool.h>
5039 +#include <linux/crypto.h>
5040 +#include <linux/kernel.h>
5041 +#include <linux/rtnetlink.h>
5042 +#include <linux/interrupt.h>
5043 +#include <linux/spinlock.h>
5045 +#include <crypto/ctr.h>
5046 +#include <crypto/des.h>
5047 +#include <crypto/aes.h>
5048 +#include <crypto/sha.h>
5049 +#include <crypto/algapi.h>
5050 +#include <crypto/aead.h>
5051 +#include <crypto/authenc.h>
5052 +#include <crypto/scatterwalk.h>
5054 +#include <asm/arch/npe.h>
5055 +#include <asm/arch/qmgr.h>
5057 +#define MAX_KEYLEN 32
5059 +/* hash: cfgword + 2 * digestlen; crypt: keylen + cfgword */
5060 +#define NPE_CTX_LEN 80
5061 +#define AES_BLOCK128 16
5063 +#define NPE_OP_HASH_VERIFY 0x01
5064 +#define NPE_OP_CCM_ENABLE 0x04
5065 +#define NPE_OP_CRYPT_ENABLE 0x08
5066 +#define NPE_OP_HASH_ENABLE 0x10
5067 +#define NPE_OP_NOT_IN_PLACE 0x20
5068 +#define NPE_OP_HMAC_DISABLE 0x40
5069 +#define NPE_OP_CRYPT_ENCRYPT 0x80
5071 +#define NPE_OP_CCM_GEN_MIC 0xcc
5072 +#define NPE_OP_HASH_GEN_ICV 0x50
5073 +#define NPE_OP_ENC_GEN_KEY 0xc9
5075 +#define MOD_ECB 0x0000
5076 +#define MOD_CTR 0x1000
5077 +#define MOD_CBC_ENC 0x2000
5078 +#define MOD_CBC_DEC 0x3000
5079 +#define MOD_CCM_ENC 0x4000
5080 +#define MOD_CCM_DEC 0x5000
5082 +#define KEYLEN_128 4
5083 +#define KEYLEN_192 6
5084 +#define KEYLEN_256 8
5086 +#define CIPH_DECR 0x0000
5087 +#define CIPH_ENCR 0x0400
5089 +#define MOD_DES 0x0000
5090 +#define MOD_TDEA2 0x0100
5091 +#define MOD_3DES 0x0200
5092 +#define MOD_AES 0x0800
5093 +#define MOD_AES128 (0x0800 | KEYLEN_128)
5094 +#define MOD_AES192 (0x0900 | KEYLEN_192)
5095 +#define MOD_AES256 (0x0a00 | KEYLEN_256)
5097 +#define MAX_IVLEN 16
5098 +#define NPE_ID 2 /* NPE C */
5099 +#define NPE_QLEN 16
5100 +/* Space for registering when the first
5101 + * NPE_QLEN crypt_ctl are busy */
5102 +#define NPE_QLEN_TOTAL 64
5104 +#define SEND_QID 29
5105 +#define RECV_QID 30
5107 +#define CTL_FLAG_UNUSED 0x0000
5108 +#define CTL_FLAG_USED 0x1000
5109 +#define CTL_FLAG_PERFORM_ABLK 0x0001
5110 +#define CTL_FLAG_GEN_ICV 0x0002
5111 +#define CTL_FLAG_GEN_REVAES 0x0004
5112 +#define CTL_FLAG_PERFORM_AEAD 0x0008
5113 +#define CTL_FLAG_MASK 0x000f
5115 +#define HMAC_IPAD_VALUE 0x36
5116 +#define HMAC_OPAD_VALUE 0x5C
5117 +#define HMAC_PAD_BLOCKLEN SHA1_BLOCK_SIZE
5119 +#define MD5_DIGEST_SIZE 16
5121 +struct buffer_desc {
5126 + u32 __reserved[4];
5127 + struct buffer_desc *next;
5131 + u8 mode; /* NPE_OP_* operation mode */
5134 + u8 iv[MAX_IVLEN]; /* IV for CBC mode or CTR IV for CTR mode */
5135 + u32 icv_rev_aes; /* icv or rev aes */
5138 + u16 auth_offs; /* Authentication start offset */
5139 + u16 auth_len; /* Authentication data length */
5140 + u16 crypt_offs; /* Cryption start offset */
5141 + u16 crypt_len; /* Cryption data length */
5142 + u32 aadAddr; /* Additional Auth Data Addr for CCM mode */
5143 + u32 crypto_ctx; /* NPE Crypto Param structure address */
5145 + /* Used by Host: 4*4 bytes*/
5146 + unsigned ctl_flags;
5148 + struct ablkcipher_request *ablk_req;
5149 + struct aead_request *aead_req;
5150 + struct crypto_tfm *tfm;
5152 + struct buffer_desc *regist_buf;
5157 + struct buffer_desc *src;
5158 + struct buffer_desc *dst;
5159 + unsigned src_nents;
5160 + unsigned dst_nents;
5164 + struct buffer_desc *buffer;
5165 + unsigned short assoc_nents;
5166 + unsigned short src_nents;
5167 + struct scatterlist ivlist;
5168 + /* used when the hmac is not on one sg entry */
5173 +struct ix_hash_algo {
5175 + unsigned char *icv;
5179 + unsigned char *npe_ctx;
5180 + dma_addr_t npe_ctx_phys;
5186 + struct ix_sa_dir encrypt;
5187 + struct ix_sa_dir decrypt;
5189 + u8 authkey[MAX_KEYLEN];
5191 + u8 enckey[MAX_KEYLEN];
5192 + u8 salt[MAX_IVLEN];
5193 + u8 nonce[CTR_RFC3686_NONCE_SIZE];
5195 + atomic_t configuring;
5196 + struct completion completion;
5200 + struct crypto_alg crypto;
5201 + const struct ix_hash_algo *hash;
5208 +static const struct ix_hash_algo hash_alg_md5 = {
5209 + .cfgword = 0xAA010004,
5210 + .icv = "\x01\x23\x45\x67\x89\xAB\xCD\xEF"
5211 + "\xFE\xDC\xBA\x98\x76\x54\x32\x10",
5213 +static const struct ix_hash_algo hash_alg_sha1 = {
5214 + .cfgword = 0x00000005,
5215 + .icv = "\x67\x45\x23\x01\xEF\xCD\xAB\x89\x98\xBA"
5216 + "\xDC\xFE\x10\x32\x54\x76\xC3\xD2\xE1\xF0",
5219 +static struct npe *npe_c;
5220 +static struct dma_pool *buffer_pool = NULL;
5221 +static struct dma_pool *ctx_pool = NULL;
5223 +static struct crypt_ctl *crypt_virt = NULL;
5224 +static dma_addr_t crypt_phys;
5226 +static int support_aes = 1;
5228 +static void dev_release(struct device *dev)
5233 +#define DRIVER_NAME "ixp4xx_crypto"
5234 +static struct platform_device pseudo_dev = {
5235 + .name = DRIVER_NAME,
5237 + .num_resources = 0,
5239 + .coherent_dma_mask = DMA_32BIT_MASK,
5240 + .release = dev_release,
5244 +static struct device *dev = &pseudo_dev.dev;
5246 +static inline dma_addr_t crypt_virt2phys(struct crypt_ctl *virt)
5248 + return crypt_phys + (virt - crypt_virt) * sizeof(struct crypt_ctl);
5251 +static inline struct crypt_ctl *crypt_phys2virt(dma_addr_t phys)
5253 + return crypt_virt + (phys - crypt_phys) / sizeof(struct crypt_ctl);
5256 +static inline u32 cipher_cfg_enc(struct crypto_tfm *tfm)
5258 + return container_of(tfm->__crt_alg, struct ixp_alg,crypto)->cfg_enc;
5261 +static inline u32 cipher_cfg_dec(struct crypto_tfm *tfm)
5263 + return container_of(tfm->__crt_alg, struct ixp_alg,crypto)->cfg_dec;
5266 +static inline const struct ix_hash_algo *ix_hash(struct crypto_tfm *tfm)
5268 + return container_of(tfm->__crt_alg, struct ixp_alg, crypto)->hash;
5271 +static int setup_crypt_desc(void)
5273 + BUILD_BUG_ON(sizeof(struct crypt_ctl) != 64);
5274 + crypt_virt = dma_alloc_coherent(dev,
5275 + NPE_QLEN * sizeof(struct crypt_ctl),
5276 + &crypt_phys, GFP_KERNEL);
5279 + memset(crypt_virt, 0, NPE_QLEN * sizeof(struct crypt_ctl));
5283 +static spinlock_t desc_lock;
5284 +static struct crypt_ctl *get_crypt_desc(void)
5287 + static int idx = 0;
5288 + unsigned long flags;
5290 + spin_lock_irqsave(&desc_lock, flags);
5292 + if (unlikely(!crypt_virt))
5293 + setup_crypt_desc();
5294 + if (unlikely(!crypt_virt)) {
5295 + spin_unlock_irqrestore(&desc_lock, flags);
5299 + if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
5300 + if (++idx >= NPE_QLEN)
5302 + crypt_virt[i].ctl_flags = CTL_FLAG_USED;
5303 + spin_unlock_irqrestore(&desc_lock, flags);
5304 + return crypt_virt +i;
5306 + spin_unlock_irqrestore(&desc_lock, flags);
5311 +static spinlock_t emerg_lock;
5312 +static struct crypt_ctl *get_crypt_desc_emerg(void)
5315 + static int idx = NPE_QLEN;
5316 + struct crypt_ctl *desc;
5317 + unsigned long flags;
5319 + desc = get_crypt_desc();
5322 + if (unlikely(!crypt_virt))
5325 + spin_lock_irqsave(&emerg_lock, flags);
5327 + if (crypt_virt[i].ctl_flags == CTL_FLAG_UNUSED) {
5328 + if (++idx >= NPE_QLEN_TOTAL)
5330 + crypt_virt[i].ctl_flags = CTL_FLAG_USED;
5331 + spin_unlock_irqrestore(&emerg_lock, flags);
5332 + return crypt_virt +i;
5334 + spin_unlock_irqrestore(&emerg_lock, flags);
5339 +static void free_buf_chain(struct buffer_desc *buf, u32 phys)
5342 + struct buffer_desc *buf1;
5346 + phys1 = buf->phys_next;
5347 + dma_pool_free(buffer_pool, buf, phys);
5353 +static struct tasklet_struct crypto_done_tasklet;
5355 +static void finish_scattered_hmac(struct crypt_ctl *crypt)
5357 + struct aead_request *req = crypt->data.aead_req;
5358 + struct aead_ctx *req_ctx = aead_request_ctx(req);
5359 + struct crypto_aead *tfm = crypto_aead_reqtfm(req);
5360 + int authsize = crypto_aead_authsize(tfm);
5361 + int decryptlen = req->cryptlen - authsize;
5363 + if (req_ctx->encrypt) {
5364 + scatterwalk_map_and_copy(req_ctx->hmac_virt,
5365 + req->src, decryptlen, authsize, 1);
5367 + dma_pool_free(buffer_pool, req_ctx->hmac_virt, crypt->icv_rev_aes);
5370 +static void one_packet(dma_addr_t phys)
5372 + struct crypt_ctl *crypt;
5373 + struct ixp_ctx *ctx;
5375 + enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
5377 + failed = phys & 0x1 ? -EBADMSG : 0;
5379 + crypt = crypt_phys2virt(phys);
5381 + switch (crypt->ctl_flags & CTL_FLAG_MASK) {
5382 + case CTL_FLAG_PERFORM_AEAD: {
5383 + struct aead_request *req = crypt->data.aead_req;
5384 + struct aead_ctx *req_ctx = aead_request_ctx(req);
5385 + dma_unmap_sg(dev, req->assoc, req_ctx->assoc_nents,
5387 + dma_unmap_sg(dev, &req_ctx->ivlist, 1, DMA_BIDIRECTIONAL);
5388 + dma_unmap_sg(dev, req->src, req_ctx->src_nents,
5389 + DMA_BIDIRECTIONAL);
5391 + free_buf_chain(req_ctx->buffer, crypt->src_buf);
5392 + if (req_ctx->hmac_virt) {
5393 + finish_scattered_hmac(crypt);
5395 + req->base.complete(&req->base, failed);
5398 + case CTL_FLAG_PERFORM_ABLK: {
5399 + struct ablkcipher_request *req = crypt->data.ablk_req;
5400 + struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
5402 + if (req_ctx->dst) {
5403 + nents = req_ctx->dst_nents;
5404 + dma_unmap_sg(dev, req->dst, nents, DMA_FROM_DEVICE);
5405 + free_buf_chain(req_ctx->dst, crypt->dst_buf);
5406 + src_direction = DMA_TO_DEVICE;
5408 + nents = req_ctx->src_nents;
5409 + dma_unmap_sg(dev, req->src, nents, src_direction);
5410 + free_buf_chain(req_ctx->src, crypt->src_buf);
5411 + req->base.complete(&req->base, failed);
5414 + case CTL_FLAG_GEN_ICV:
5415 + ctx = crypto_tfm_ctx(crypt->data.tfm);
5416 + dma_pool_free(ctx_pool, crypt->regist_ptr,
5417 + crypt->regist_buf->phys_addr);
5418 + dma_pool_free(buffer_pool, crypt->regist_buf, crypt->src_buf);
5419 + if (atomic_dec_and_test(&ctx->configuring))
5420 + complete(&ctx->completion);
5422 + case CTL_FLAG_GEN_REVAES:
5423 + ctx = crypto_tfm_ctx(crypt->data.tfm);
5424 + *(u32*)ctx->decrypt.npe_ctx &= cpu_to_be32(~CIPH_ENCR);
5425 + if (atomic_dec_and_test(&ctx->configuring))
5426 + complete(&ctx->completion);
5431 + crypt->ctl_flags = CTL_FLAG_UNUSED;
5434 +static void irqhandler(void *_unused)
5436 + tasklet_schedule(&crypto_done_tasklet);
5439 +static void crypto_done_action(unsigned long arg)
5443 + for(i=0; i<4; i++) {
5444 + dma_addr_t phys = qmgr_get_entry(RECV_QID);
5449 + tasklet_schedule(&crypto_done_tasklet);
5452 +static int init_ixp_crypto(void)
5454 + int ret = -ENODEV;
5456 + if (! ( ~(*IXP4XX_EXP_CFG2) & (IXP4XX_FEATURE_HASH |
5457 + IXP4XX_FEATURE_AES | IXP4XX_FEATURE_DES))) {
5458 + printk(KERN_ERR "ixp_crypto: No HW crypto available\n");
5461 + npe_c = npe_request(NPE_ID);
5465 + if (!npe_running(npe_c)) {
5466 + npe_load_firmware(npe_c, npe_name(npe_c), dev);
5469 + /* buffer_pool will also be used to sometimes store the hmac,
5470 + * so assure it is large enough
5472 + BUILD_BUG_ON(SHA1_DIGEST_SIZE > sizeof(struct buffer_desc));
5473 + buffer_pool = dma_pool_create("buffer", dev,
5474 + sizeof(struct buffer_desc), 32, 0);
5476 + if (!buffer_pool) {
5479 + ctx_pool = dma_pool_create("context", dev,
5480 + NPE_CTX_LEN, 16, 0);
5484 + ret = qmgr_request_queue(SEND_QID, NPE_QLEN_TOTAL, 0, 0);
5487 + ret = qmgr_request_queue(RECV_QID, NPE_QLEN, 0, 0);
5489 + qmgr_release_queue(SEND_QID);
5492 + qmgr_set_irq(RECV_QID, QUEUE_IRQ_SRC_NOT_EMPTY, irqhandler, NULL);
5493 + tasklet_init(&crypto_done_tasklet, crypto_done_action, 0);
5495 + qmgr_enable_irq(RECV_QID);
5499 + dma_pool_destroy(ctx_pool);
5501 + dma_pool_destroy(buffer_pool);
5502 + npe_release(npe_c);
5506 +static void release_ixp_crypto(void)
5508 + qmgr_disable_irq(RECV_QID);
5509 + tasklet_kill(&crypto_done_tasklet);
5511 + qmgr_release_queue(SEND_QID);
5512 + qmgr_release_queue(RECV_QID);
5514 + dma_pool_destroy(ctx_pool);
5515 + dma_pool_destroy(buffer_pool);
5517 + npe_release(npe_c);
5520 + dma_free_coherent(dev,
5521 + NPE_QLEN_TOTAL * sizeof( struct crypt_ctl),
5522 + crypt_virt, crypt_phys);
5527 +static void reset_sa_dir(struct ix_sa_dir *dir)
5529 + memset(dir->npe_ctx, 0, NPE_CTX_LEN);
5530 + dir->npe_ctx_idx = 0;
5531 + dir->npe_mode = 0;
5534 +static int init_sa_dir(struct ix_sa_dir *dir)
5536 + dir->npe_ctx = dma_pool_alloc(ctx_pool, GFP_KERNEL, &dir->npe_ctx_phys);
5537 + if (!dir->npe_ctx) {
5540 + reset_sa_dir(dir);
5544 +static void free_sa_dir(struct ix_sa_dir *dir)
5546 + memset(dir->npe_ctx, 0, NPE_CTX_LEN);
5547 + dma_pool_free(ctx_pool, dir->npe_ctx, dir->npe_ctx_phys);
5550 +static int init_tfm(struct crypto_tfm *tfm)
5552 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5555 + atomic_set(&ctx->configuring, 0);
5556 + ret = init_sa_dir(&ctx->encrypt);
5559 + ret = init_sa_dir(&ctx->decrypt);
5561 + free_sa_dir(&ctx->encrypt);
5566 +static int init_tfm_ablk(struct crypto_tfm *tfm)
5568 + tfm->crt_ablkcipher.reqsize = sizeof(struct ablk_ctx);
5569 + return init_tfm(tfm);
5572 +static int init_tfm_aead(struct crypto_tfm *tfm)
5574 + tfm->crt_aead.reqsize = sizeof(struct aead_ctx);
5575 + return init_tfm(tfm);
5578 +static void exit_tfm(struct crypto_tfm *tfm)
5580 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5581 + free_sa_dir(&ctx->encrypt);
5582 + free_sa_dir(&ctx->decrypt);
5585 +static int register_chain_var(struct crypto_tfm *tfm, u8 xpad, u32 target,
5586 + int init_len, u32 ctx_addr, const u8 *key, int key_len)
5588 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5589 + struct crypt_ctl *crypt;
5590 + struct buffer_desc *buf;
5593 + u32 pad_phys, buf_phys;
5595 + BUILD_BUG_ON(NPE_CTX_LEN < HMAC_PAD_BLOCKLEN);
5596 + pad = dma_pool_alloc(ctx_pool, GFP_KERNEL, &pad_phys);
5599 + buf = dma_pool_alloc(buffer_pool, GFP_KERNEL, &buf_phys);
5601 + dma_pool_free(ctx_pool, pad, pad_phys);
5604 + crypt = get_crypt_desc_emerg();
5606 + dma_pool_free(ctx_pool, pad, pad_phys);
5607 + dma_pool_free(buffer_pool, buf, buf_phys);
5611 + memcpy(pad, key, key_len);
5612 + memset(pad + key_len, 0, HMAC_PAD_BLOCKLEN - key_len);
5613 + for (i = 0; i < HMAC_PAD_BLOCKLEN; i++) {
5617 + crypt->data.tfm = tfm;
5618 + crypt->regist_ptr = pad;
5619 + crypt->regist_buf = buf;
5621 + crypt->auth_offs = 0;
5622 + crypt->auth_len = HMAC_PAD_BLOCKLEN;
5623 + crypt->crypto_ctx = ctx_addr;
5624 + crypt->src_buf = buf_phys;
5625 + crypt->icv_rev_aes = target;
5626 + crypt->mode = NPE_OP_HASH_GEN_ICV;
5627 + crypt->init_len = init_len;
5628 + crypt->ctl_flags |= CTL_FLAG_GEN_ICV;
5631 + buf->buf_len = HMAC_PAD_BLOCKLEN;
5633 + buf->phys_addr = pad_phys;
5635 + atomic_inc(&ctx->configuring);
5636 + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
5637 + BUG_ON(qmgr_stat_overflow(SEND_QID));
5641 +static int setup_auth(struct crypto_tfm *tfm, int encrypt, unsigned authsize,
5642 + const u8 *key, int key_len, unsigned digest_len)
5644 + u32 itarget, otarget, npe_ctx_addr;
5645 + unsigned char *cinfo;
5646 + int init_len, ret = 0;
5648 + struct ix_sa_dir *dir;
5649 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5650 + const struct ix_hash_algo *algo;
5652 + dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
5653 + cinfo = dir->npe_ctx + dir->npe_ctx_idx;
5654 + algo = ix_hash(tfm);
5656 + /* write cfg word to cryptinfo */
5657 + cfgword = algo->cfgword | ( authsize << 6); /* (authsize/4) << 8 */
5658 + *(u32*)cinfo = cpu_to_be32(cfgword);
5659 + cinfo += sizeof(cfgword);
5661 + /* write ICV to cryptinfo */
5662 + memcpy(cinfo, algo->icv, digest_len);
5663 + cinfo += digest_len;
5665 + itarget = dir->npe_ctx_phys + dir->npe_ctx_idx
5666 + + sizeof(algo->cfgword);
5667 + otarget = itarget + digest_len;
5668 + init_len = cinfo - (dir->npe_ctx + dir->npe_ctx_idx);
5669 + npe_ctx_addr = dir->npe_ctx_phys + dir->npe_ctx_idx;
5671 + dir->npe_ctx_idx += init_len;
5672 + dir->npe_mode |= NPE_OP_HASH_ENABLE;
5675 + dir->npe_mode |= NPE_OP_HASH_VERIFY;
5677 + ret = register_chain_var(tfm, HMAC_OPAD_VALUE, otarget,
5678 + init_len, npe_ctx_addr, key, key_len);
5681 + return register_chain_var(tfm, HMAC_IPAD_VALUE, itarget,
5682 + init_len, npe_ctx_addr, key, key_len);
5685 +static int gen_rev_aes_key(struct crypto_tfm *tfm)
5687 + struct crypt_ctl *crypt;
5688 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5689 + struct ix_sa_dir *dir = &ctx->decrypt;
5691 + crypt = get_crypt_desc_emerg();
5695 + *(u32*)dir->npe_ctx |= cpu_to_be32(CIPH_ENCR);
5697 + crypt->data.tfm = tfm;
5698 + crypt->crypt_offs = 0;
5699 + crypt->crypt_len = AES_BLOCK128;
5700 + crypt->src_buf = 0;
5701 + crypt->crypto_ctx = dir->npe_ctx_phys;
5702 + crypt->icv_rev_aes = dir->npe_ctx_phys + sizeof(u32);
5703 + crypt->mode = NPE_OP_ENC_GEN_KEY;
5704 + crypt->init_len = dir->npe_ctx_idx;
5705 + crypt->ctl_flags |= CTL_FLAG_GEN_REVAES;
5707 + atomic_inc(&ctx->configuring);
5708 + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
5709 + BUG_ON(qmgr_stat_overflow(SEND_QID));
5713 +static int setup_cipher(struct crypto_tfm *tfm, int encrypt,
5714 + const u8 *key, int key_len)
5718 + u32 keylen_cfg = 0;
5719 + struct ix_sa_dir *dir;
5720 + struct ixp_ctx *ctx = crypto_tfm_ctx(tfm);
5721 + u32 *flags = &tfm->crt_flags;
5723 + dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
5724 + cinfo = dir->npe_ctx;
5727 + cipher_cfg = cipher_cfg_enc(tfm);
5728 + dir->npe_mode |= NPE_OP_CRYPT_ENCRYPT;
5730 + cipher_cfg = cipher_cfg_dec(tfm);
5732 + if (cipher_cfg & MOD_AES) {
5733 + switch (key_len) {
5734 + case 16: keylen_cfg = MOD_AES128 | KEYLEN_128; break;
5735 + case 24: keylen_cfg = MOD_AES192 | KEYLEN_192; break;
5736 + case 32: keylen_cfg = MOD_AES256 | KEYLEN_256; break;
5738 + *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
5741 + cipher_cfg |= keylen_cfg;
5742 + } else if (cipher_cfg & MOD_3DES) {
5743 + const u32 *K = (const u32 *)key;
5744 + if (unlikely(!((K[0] ^ K[2]) | (K[1] ^ K[3])) ||
5745 + !((K[2] ^ K[4]) | (K[3] ^ K[5]))))
5747 + *flags |= CRYPTO_TFM_RES_BAD_KEY_SCHED;
5751 + u32 tmp[DES_EXPKEY_WORDS];
5752 + if (des_ekey(tmp, key) == 0) {
5753 + *flags |= CRYPTO_TFM_RES_WEAK_KEY;
5756 + /* write cfg word to cryptinfo */
5757 + *(u32*)cinfo = cpu_to_be32(cipher_cfg);
5758 + cinfo += sizeof(cipher_cfg);
5760 + /* write cipher key to cryptinfo */
5761 + memcpy(cinfo, key, key_len);
5762 + /* NPE wants keylen set to DES3_EDE_KEY_SIZE even for single DES */
5763 + if (key_len < DES3_EDE_KEY_SIZE && !(cipher_cfg & MOD_AES)) {
5764 + memset(cinfo + key_len, 0, DES3_EDE_KEY_SIZE -key_len);
5765 + key_len = DES3_EDE_KEY_SIZE;
5767 + dir->npe_ctx_idx = sizeof(cipher_cfg) + key_len;
5768 + dir->npe_mode |= NPE_OP_CRYPT_ENABLE;
5769 + if ((cipher_cfg & MOD_AES) && !encrypt) {
5770 + return gen_rev_aes_key(tfm);
5775 +static int count_sg(struct scatterlist *sg, int nbytes)
5778 + for (i = 0; nbytes > 0; i++, sg = sg_next(sg))
5779 + nbytes -= sg->length;
5783 +static struct buffer_desc *chainup_buffers(struct scatterlist *sg,
5784 + unsigned nbytes, struct buffer_desc *buf, gfp_t flags)
5788 + while (nbytes > 0) {
5789 + struct buffer_desc *next_buf;
5790 + u32 next_buf_phys;
5791 + unsigned len = min(nbytes, sg_dma_len(sg));
5795 + if (!buf->phys_addr) {
5796 + buf->phys_addr = sg_dma_address(sg);
5797 + buf->buf_len = len;
5799 + buf->phys_next = 0;
5802 + /* Two consecutive chunks on one page may be handled by the old
5803 + * buffer descriptor, increased by the length of the new one
5805 + if (sg_dma_address(sg) == buf->phys_addr + buf->buf_len) {
5806 + buf->buf_len += len;
5809 + next_buf = dma_pool_alloc(buffer_pool, flags, &next_buf_phys);
5812 + buf->next = next_buf;
5813 + buf->phys_next = next_buf_phys;
5817 + buf->phys_next = 0;
5818 + buf->phys_addr = sg_dma_address(sg);
5819 + buf->buf_len = len;
5828 +static int ablk_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
5829 + unsigned int key_len)
5831 + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
5832 + u32 *flags = &tfm->base.crt_flags;
5835 + init_completion(&ctx->completion);
5836 + atomic_inc(&ctx->configuring);
5838 + reset_sa_dir(&ctx->encrypt);
5839 + reset_sa_dir(&ctx->decrypt);
5841 + ctx->encrypt.npe_mode = NPE_OP_HMAC_DISABLE;
5842 + ctx->decrypt.npe_mode = NPE_OP_HMAC_DISABLE;
5844 + ret = setup_cipher(&tfm->base, 0, key, key_len);
5847 + ret = setup_cipher(&tfm->base, 1, key, key_len);
5851 + if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
5852 + if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
5855 + *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
5859 + if (!atomic_dec_and_test(&ctx->configuring))
5860 + wait_for_completion(&ctx->completion);
5864 +static int ablk_rfc3686_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
5865 + unsigned int key_len)
5867 + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
5869 + /* the nonce is stored in bytes at end of key */
5870 + if (key_len < CTR_RFC3686_NONCE_SIZE)
5873 + memcpy(ctx->nonce, key + (key_len - CTR_RFC3686_NONCE_SIZE),
5874 + CTR_RFC3686_NONCE_SIZE);
5876 + key_len -= CTR_RFC3686_NONCE_SIZE;
5877 + return ablk_setkey(tfm, key, key_len);
5880 +static int ablk_perform(struct ablkcipher_request *req, int encrypt)
5882 + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
5883 + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
5884 + unsigned ivsize = crypto_ablkcipher_ivsize(tfm);
5885 + int ret = -ENOMEM;
5886 + struct ix_sa_dir *dir;
5887 + struct crypt_ctl *crypt;
5888 + unsigned int nbytes = req->nbytes, nents;
5889 + enum dma_data_direction src_direction = DMA_BIDIRECTIONAL;
5890 + struct ablk_ctx *req_ctx = ablkcipher_request_ctx(req);
5891 + gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
5892 + GFP_KERNEL : GFP_ATOMIC;
5894 + if (qmgr_stat_full(SEND_QID))
5896 + if (atomic_read(&ctx->configuring))
5899 + dir = encrypt ? &ctx->encrypt : &ctx->decrypt;
5901 + crypt = get_crypt_desc();
5905 + crypt->data.ablk_req = req;
5906 + crypt->crypto_ctx = dir->npe_ctx_phys;
5907 + crypt->mode = dir->npe_mode;
5908 + crypt->init_len = dir->npe_ctx_idx;
5910 + crypt->crypt_offs = 0;
5911 + crypt->crypt_len = nbytes;
5913 + BUG_ON(ivsize && !req->info);
5914 + memcpy(crypt->iv, req->info, ivsize);
5915 + if (req->src != req->dst) {
5916 + crypt->mode |= NPE_OP_NOT_IN_PLACE;
5917 + nents = count_sg(req->dst, nbytes);
5918 + /* This was never tested by Intel
5919 + * for more than one dst buffer, I think. */
5920 + BUG_ON(nents != 1);
5921 + req_ctx->dst_nents = nents;
5922 + dma_map_sg(dev, req->dst, nents, DMA_FROM_DEVICE);
5923 + req_ctx->dst = dma_pool_alloc(buffer_pool, flags,&crypt->dst_buf);
5924 + if (!req_ctx->dst)
5925 + goto unmap_sg_dest;
5926 + req_ctx->dst->phys_addr = 0;
5927 + if (!chainup_buffers(req->dst, nbytes, req_ctx->dst, flags))
5928 + goto free_buf_dest;
5929 + src_direction = DMA_TO_DEVICE;
5931 + req_ctx->dst = NULL;
5932 + req_ctx->dst_nents = 0;
5934 + nents = count_sg(req->src, nbytes);
5935 + req_ctx->src_nents = nents;
5936 + dma_map_sg(dev, req->src, nents, src_direction);
5938 + req_ctx->src = dma_pool_alloc(buffer_pool, flags, &crypt->src_buf);
5939 + if (!req_ctx->src)
5940 + goto unmap_sg_src;
5941 + req_ctx->src->phys_addr = 0;
5942 + if (!chainup_buffers(req->src, nbytes, req_ctx->src, flags))
5943 + goto free_buf_src;
5945 + crypt->ctl_flags |= CTL_FLAG_PERFORM_ABLK;
5946 + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
5947 + BUG_ON(qmgr_stat_overflow(SEND_QID));
5948 + return -EINPROGRESS;
5951 + free_buf_chain(req_ctx->src, crypt->src_buf);
5953 + dma_unmap_sg(dev, req->src, req_ctx->src_nents, src_direction);
5955 + if (req->src != req->dst) {
5956 + free_buf_chain(req_ctx->dst, crypt->dst_buf);
5958 + dma_unmap_sg(dev, req->src, req_ctx->dst_nents,
5961 + crypt->ctl_flags = CTL_FLAG_UNUSED;
5965 +static int ablk_encrypt(struct ablkcipher_request *req)
5967 + return ablk_perform(req, 1);
5970 +static int ablk_decrypt(struct ablkcipher_request *req)
5972 + return ablk_perform(req, 0);
5975 +static int ablk_rfc3686_crypt(struct ablkcipher_request *req)
5977 + struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
5978 + struct ixp_ctx *ctx = crypto_ablkcipher_ctx(tfm);
5979 + u8 iv[CTR_RFC3686_BLOCK_SIZE];
5980 + u8 *info = req->info;
5983 + /* set up counter block */
5984 + memcpy(iv, ctx->nonce, CTR_RFC3686_NONCE_SIZE);
5985 + memcpy(iv + CTR_RFC3686_NONCE_SIZE, info, CTR_RFC3686_IV_SIZE);
5987 + /* initialize counter portion of counter block */
5988 + *(__be32 *)(iv + CTR_RFC3686_NONCE_SIZE + CTR_RFC3686_IV_SIZE) =
5992 + ret = ablk_perform(req, 1);
5997 +static int hmac_inconsistent(struct scatterlist *sg, unsigned start,
5998 + unsigned int nbytes)
6006 + if (start < offset + sg->length)
6009 + offset += sg->length;
6012 + return (start + nbytes > offset + sg->length);
6015 +static int aead_perform(struct aead_request *req, int encrypt,
6016 + int cryptoffset, int eff_cryptlen, u8 *iv)
6018 + struct crypto_aead *tfm = crypto_aead_reqtfm(req);
6019 + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
6020 + unsigned ivsize = crypto_aead_ivsize(tfm);
6021 + unsigned authsize = crypto_aead_authsize(tfm);
6022 + int ret = -ENOMEM;
6023 + struct ix_sa_dir *dir;
6024 + struct crypt_ctl *crypt;
6025 + unsigned int cryptlen, nents;
6026 + struct buffer_desc *buf;
6027 + struct aead_ctx *req_ctx = aead_request_ctx(req);
6028 + gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
6029 + GFP_KERNEL : GFP_ATOMIC;
6031 + if (qmgr_stat_full(SEND_QID))
6033 + if (atomic_read(&ctx->configuring))
6037 + dir = &ctx->encrypt;
6038 + cryptlen = req->cryptlen;
6040 + dir = &ctx->decrypt;
6041 + /* req->cryptlen includes the authsize when decrypting */
6042 + cryptlen = req->cryptlen -authsize;
6043 + eff_cryptlen -= authsize;
6045 + crypt = get_crypt_desc();
6049 + crypt->data.aead_req = req;
6050 + crypt->crypto_ctx = dir->npe_ctx_phys;
6051 + crypt->mode = dir->npe_mode;
6052 + crypt->init_len = dir->npe_ctx_idx;
6054 + crypt->crypt_offs = cryptoffset;
6055 + crypt->crypt_len = eff_cryptlen;
6057 + crypt->auth_offs = 0;
6058 + crypt->auth_len = req->assoclen + ivsize + cryptlen;
6059 + BUG_ON(ivsize && !req->iv);
6060 + memcpy(crypt->iv, req->iv, ivsize);
6062 + if (req->src != req->dst) {
6063 + BUG(); /* -ENOTSUP because of my lazyness */
6066 + req_ctx->buffer = dma_pool_alloc(buffer_pool, flags, &crypt->src_buf);
6067 + if (!req_ctx->buffer)
6069 + req_ctx->buffer->phys_addr = 0;
6071 + nents = count_sg(req->assoc, req->assoclen);
6072 + req_ctx->assoc_nents = nents;
6073 + dma_map_sg(dev, req->assoc, nents, DMA_TO_DEVICE);
6074 + buf = chainup_buffers(req->assoc, req->assoclen, req_ctx->buffer,flags);
6076 + goto unmap_sg_assoc;
6078 + sg_init_table(&req_ctx->ivlist, 1);
6079 + sg_set_buf(&req_ctx->ivlist, iv, ivsize);
6080 + dma_map_sg(dev, &req_ctx->ivlist, 1, DMA_BIDIRECTIONAL);
6081 + buf = chainup_buffers(&req_ctx->ivlist, ivsize, buf, flags);
6084 + if (unlikely(hmac_inconsistent(req->src, cryptlen, authsize))) {
6085 + /* The 12 hmac bytes are scattered,
6086 + * we need to copy them into a safe buffer */
6087 + req_ctx->hmac_virt = dma_pool_alloc(buffer_pool, flags,
6088 + &crypt->icv_rev_aes);
6089 + if (unlikely(!req_ctx->hmac_virt))
6092 + scatterwalk_map_and_copy(req_ctx->hmac_virt,
6093 + req->src, cryptlen, authsize, 0);
6095 + req_ctx->encrypt = encrypt;
6097 + req_ctx->hmac_virt = NULL;
6100 + nents = count_sg(req->src, cryptlen + authsize);
6101 + req_ctx->src_nents = nents;
6102 + dma_map_sg(dev, req->src, nents, DMA_BIDIRECTIONAL);
6103 + buf = chainup_buffers(req->src, cryptlen + authsize, buf, flags);
6105 + goto unmap_sg_src;
6106 + if (!req_ctx->hmac_virt) {
6107 + crypt->icv_rev_aes = buf->phys_addr + buf->buf_len - authsize;
6109 + crypt->ctl_flags |= CTL_FLAG_PERFORM_AEAD;
6110 + qmgr_put_entry(SEND_QID, crypt_virt2phys(crypt));
6111 + BUG_ON(qmgr_stat_overflow(SEND_QID));
6112 + return -EINPROGRESS;
6114 + dma_unmap_sg(dev, req->src, req_ctx->src_nents, DMA_BIDIRECTIONAL);
6115 + if (req_ctx->hmac_virt) {
6116 + dma_pool_free(buffer_pool, req_ctx->hmac_virt,
6117 + crypt->icv_rev_aes);
6120 + dma_unmap_sg(dev, &req_ctx->ivlist, 1, DMA_BIDIRECTIONAL);
6122 + dma_unmap_sg(dev, req->assoc, req_ctx->assoc_nents, DMA_TO_DEVICE);
6123 + free_buf_chain(req_ctx->buffer, crypt->src_buf);
6125 + crypt->ctl_flags = CTL_FLAG_UNUSED;
6129 +static int aead_setup(struct crypto_aead *tfm, unsigned int authsize)
6131 + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
6132 + u32 *flags = &tfm->base.crt_flags;
6133 + unsigned digest_len = crypto_aead_alg(tfm)->maxauthsize;
6136 + if (!ctx->enckey_len && !ctx->authkey_len)
6138 + init_completion(&ctx->completion);
6139 + atomic_inc(&ctx->configuring);
6141 + reset_sa_dir(&ctx->encrypt);
6142 + reset_sa_dir(&ctx->decrypt);
6144 + ret = setup_cipher(&tfm->base, 0, ctx->enckey, ctx->enckey_len);
6147 + ret = setup_cipher(&tfm->base, 1, ctx->enckey, ctx->enckey_len);
6150 + ret = setup_auth(&tfm->base, 0, authsize, ctx->authkey,
6151 + ctx->authkey_len, digest_len);
6154 + ret = setup_auth(&tfm->base, 1, authsize, ctx->authkey,
6155 + ctx->authkey_len, digest_len);
6159 + if (*flags & CRYPTO_TFM_RES_WEAK_KEY) {
6160 + if (*flags & CRYPTO_TFM_REQ_WEAK_KEY) {
6164 + *flags &= ~CRYPTO_TFM_RES_WEAK_KEY;
6168 + if (!atomic_dec_and_test(&ctx->configuring))
6169 + wait_for_completion(&ctx->completion);
6173 +static int aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
6175 + int max = crypto_aead_alg(tfm)->maxauthsize >> 2;
6177 + if ((authsize>>2) < 1 || (authsize>>2) > max || (authsize & 3))
6179 + return aead_setup(tfm, authsize);
6182 +static int aead_setkey(struct crypto_aead *tfm, const u8 *key,
6183 + unsigned int keylen)
6185 + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
6186 + struct rtattr *rta = (struct rtattr *)key;
6187 + struct crypto_authenc_key_param *param;
6189 + if (!RTA_OK(rta, keylen))
6191 + if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
6193 + if (RTA_PAYLOAD(rta) < sizeof(*param))
6196 + param = RTA_DATA(rta);
6197 + ctx->enckey_len = be32_to_cpu(param->enckeylen);
6199 + key += RTA_ALIGN(rta->rta_len);
6200 + keylen -= RTA_ALIGN(rta->rta_len);
6202 + if (keylen < ctx->enckey_len)
6205 + ctx->authkey_len = keylen - ctx->enckey_len;
6206 + memcpy(ctx->enckey, key + ctx->authkey_len, ctx->enckey_len);
6207 + memcpy(ctx->authkey, key, ctx->authkey_len);
6209 + return aead_setup(tfm, crypto_aead_authsize(tfm));
6211 + ctx->enckey_len = 0;
6212 + crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
6216 +static int aead_encrypt(struct aead_request *req)
6218 + unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
6219 + return aead_perform(req, 1, req->assoclen + ivsize,
6220 + req->cryptlen, req->iv);
6223 +static int aead_decrypt(struct aead_request *req)
6225 + unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
6226 + return aead_perform(req, 0, req->assoclen + ivsize,
6227 + req->cryptlen, req->iv);
6230 +static int aead_givencrypt(struct aead_givcrypt_request *req)
6232 + struct crypto_aead *tfm = aead_givcrypt_reqtfm(req);
6233 + struct ixp_ctx *ctx = crypto_aead_ctx(tfm);
6234 + unsigned len, ivsize = crypto_aead_ivsize(tfm);
6237 + /* copied from eseqiv.c */
6238 + if (!ctx->salted) {
6239 + get_random_bytes(ctx->salt, ivsize);
6242 + memcpy(req->areq.iv, ctx->salt, ivsize);
6244 + if (ivsize > sizeof(u64)) {
6245 + memset(req->giv, 0, ivsize - sizeof(u64));
6246 + len = sizeof(u64);
6248 + seq = cpu_to_be64(req->seq);
6249 + memcpy(req->giv + ivsize - len, &seq, len);
6250 + return aead_perform(&req->areq, 1, req->areq.assoclen,
6251 + req->areq.cryptlen +ivsize, req->giv);
6254 +static struct ixp_alg ixp4xx_algos[] = {
6257 + .cra_name = "cbc(des)",
6258 + .cra_blocksize = DES_BLOCK_SIZE,
6259 + .cra_u = { .ablkcipher = {
6260 + .min_keysize = DES_KEY_SIZE,
6261 + .max_keysize = DES_KEY_SIZE,
6262 + .ivsize = DES_BLOCK_SIZE,
6263 + .geniv = "eseqiv",
6267 + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
6268 + .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
6272 + .cra_name = "ecb(des)",
6273 + .cra_blocksize = DES_BLOCK_SIZE,
6274 + .cra_u = { .ablkcipher = {
6275 + .min_keysize = DES_KEY_SIZE,
6276 + .max_keysize = DES_KEY_SIZE,
6280 + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_ECB | KEYLEN_192,
6281 + .cfg_dec = CIPH_DECR | MOD_DES | MOD_ECB | KEYLEN_192,
6284 + .cra_name = "cbc(des3_ede)",
6285 + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
6286 + .cra_u = { .ablkcipher = {
6287 + .min_keysize = DES3_EDE_KEY_SIZE,
6288 + .max_keysize = DES3_EDE_KEY_SIZE,
6289 + .ivsize = DES3_EDE_BLOCK_SIZE,
6290 + .geniv = "eseqiv",
6294 + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
6295 + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
6298 + .cra_name = "ecb(des3_ede)",
6299 + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
6300 + .cra_u = { .ablkcipher = {
6301 + .min_keysize = DES3_EDE_KEY_SIZE,
6302 + .max_keysize = DES3_EDE_KEY_SIZE,
6306 + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_ECB | KEYLEN_192,
6307 + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_ECB | KEYLEN_192,
6310 + .cra_name = "cbc(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",
6320 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
6321 + .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
6324 + .cra_name = "ecb(aes)",
6325 + .cra_blocksize = AES_BLOCK_SIZE,
6326 + .cra_u = { .ablkcipher = {
6327 + .min_keysize = AES_MIN_KEY_SIZE,
6328 + .max_keysize = AES_MAX_KEY_SIZE,
6332 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_ECB,
6333 + .cfg_dec = CIPH_DECR | MOD_AES | MOD_ECB,
6336 + .cra_name = "ctr(aes)",
6337 + .cra_blocksize = AES_BLOCK_SIZE,
6338 + .cra_u = { .ablkcipher = {
6339 + .min_keysize = AES_MIN_KEY_SIZE,
6340 + .max_keysize = AES_MAX_KEY_SIZE,
6341 + .ivsize = AES_BLOCK_SIZE,
6342 + .geniv = "eseqiv",
6346 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
6347 + .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
6350 + .cra_name = "rfc3686(ctr(aes))",
6351 + .cra_blocksize = AES_BLOCK_SIZE,
6352 + .cra_u = { .ablkcipher = {
6353 + .min_keysize = AES_MIN_KEY_SIZE,
6354 + .max_keysize = AES_MAX_KEY_SIZE,
6355 + .ivsize = AES_BLOCK_SIZE,
6356 + .geniv = "eseqiv",
6357 + .setkey = ablk_rfc3686_setkey,
6358 + .encrypt = ablk_rfc3686_crypt,
6359 + .decrypt = ablk_rfc3686_crypt }
6362 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CTR,
6363 + .cfg_dec = CIPH_ENCR | MOD_AES | MOD_CTR,
6366 + .cra_name = "authenc(hmac(md5),cbc(des))",
6367 + .cra_blocksize = DES_BLOCK_SIZE,
6368 + .cra_u = { .aead = {
6369 + .ivsize = DES_BLOCK_SIZE,
6370 + .maxauthsize = MD5_DIGEST_SIZE,
6374 + .hash = &hash_alg_md5,
6375 + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
6376 + .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
6379 + .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
6380 + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
6381 + .cra_u = { .aead = {
6382 + .ivsize = DES3_EDE_BLOCK_SIZE,
6383 + .maxauthsize = MD5_DIGEST_SIZE,
6387 + .hash = &hash_alg_md5,
6388 + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
6389 + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
6392 + .cra_name = "authenc(hmac(sha1),cbc(des))",
6393 + .cra_blocksize = DES_BLOCK_SIZE,
6394 + .cra_u = { .aead = {
6395 + .ivsize = DES_BLOCK_SIZE,
6396 + .maxauthsize = SHA1_DIGEST_SIZE,
6400 + .hash = &hash_alg_sha1,
6401 + .cfg_enc = CIPH_ENCR | MOD_DES | MOD_CBC_ENC | KEYLEN_192,
6402 + .cfg_dec = CIPH_DECR | MOD_DES | MOD_CBC_DEC | KEYLEN_192,
6405 + .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
6406 + .cra_blocksize = DES3_EDE_BLOCK_SIZE,
6407 + .cra_u = { .aead = {
6408 + .ivsize = DES3_EDE_BLOCK_SIZE,
6409 + .maxauthsize = SHA1_DIGEST_SIZE,
6413 + .hash = &hash_alg_sha1,
6414 + .cfg_enc = CIPH_ENCR | MOD_3DES | MOD_CBC_ENC | KEYLEN_192,
6415 + .cfg_dec = CIPH_DECR | MOD_3DES | MOD_CBC_DEC | KEYLEN_192,
6418 + .cra_name = "authenc(hmac(md5),cbc(aes))",
6419 + .cra_blocksize = AES_BLOCK_SIZE,
6420 + .cra_u = { .aead = {
6421 + .ivsize = AES_BLOCK_SIZE,
6422 + .maxauthsize = MD5_DIGEST_SIZE,
6426 + .hash = &hash_alg_md5,
6427 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
6428 + .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
6431 + .cra_name = "authenc(hmac(sha1),cbc(aes))",
6432 + .cra_blocksize = AES_BLOCK_SIZE,
6433 + .cra_u = { .aead = {
6434 + .ivsize = AES_BLOCK_SIZE,
6435 + .maxauthsize = SHA1_DIGEST_SIZE,
6439 + .hash = &hash_alg_sha1,
6440 + .cfg_enc = CIPH_ENCR | MOD_AES | MOD_CBC_ENC,
6441 + .cfg_dec = CIPH_DECR | MOD_AES | MOD_CBC_DEC,
6444 +#define IXP_POSTFIX "-ixp4xx"
6445 +static int __init ixp_module_init(void)
6447 + int num = ARRAY_SIZE(ixp4xx_algos);
6450 + if (platform_device_register(&pseudo_dev))
6453 + spin_lock_init(&desc_lock);
6454 + spin_lock_init(&emerg_lock);
6456 + err = init_ixp_crypto();
6458 + platform_device_unregister(&pseudo_dev);
6461 + for (i=0; i< num; i++) {
6462 + struct crypto_alg *cra = &ixp4xx_algos[i].crypto;
6464 + if (snprintf(cra->cra_driver_name, CRYPTO_MAX_ALG_NAME,
6465 + "%s"IXP_POSTFIX, cra->cra_name) >=
6466 + CRYPTO_MAX_ALG_NAME)
6470 + if (!support_aes && (ixp4xx_algos[i].cfg_enc & MOD_AES)) {
6473 + if (!ixp4xx_algos[i].hash) {
6474 + /* block ciphers */
6475 + cra->cra_type = &crypto_ablkcipher_type;
6476 + cra->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
6478 + if (!cra->cra_ablkcipher.setkey)
6479 + cra->cra_ablkcipher.setkey = ablk_setkey;
6480 + if (!cra->cra_ablkcipher.encrypt)
6481 + cra->cra_ablkcipher.encrypt = ablk_encrypt;
6482 + if (!cra->cra_ablkcipher.decrypt)
6483 + cra->cra_ablkcipher.decrypt = ablk_decrypt;
6484 + cra->cra_init = init_tfm_ablk;
6487 + cra->cra_type = &crypto_aead_type;
6488 + cra->cra_flags = CRYPTO_ALG_TYPE_AEAD |
6490 + cra->cra_aead.setkey = aead_setkey;
6491 + cra->cra_aead.setauthsize = aead_setauthsize;
6492 + cra->cra_aead.encrypt = aead_encrypt;
6493 + cra->cra_aead.decrypt = aead_decrypt;
6494 + cra->cra_aead.givencrypt = aead_givencrypt;
6495 + cra->cra_init = init_tfm_aead;
6497 + cra->cra_ctxsize = sizeof(struct ixp_ctx);
6498 + cra->cra_module = THIS_MODULE;
6499 + cra->cra_alignmask = 3;
6500 + cra->cra_priority = 300;
6501 + cra->cra_exit = exit_tfm;
6502 + if (crypto_register_alg(cra))
6503 + printk(KERN_ERR "Failed to register '%s'\n",
6506 + ixp4xx_algos[i].registered = 1;
6511 +static void __exit ixp_module_exit(void)
6513 + int num = ARRAY_SIZE(ixp4xx_algos);
6516 + for (i=0; i< num; i++) {
6517 + if (ixp4xx_algos[i].registered)
6518 + crypto_unregister_alg(&ixp4xx_algos[i].crypto);
6520 + release_ixp_crypto();
6521 + platform_device_unregister(&pseudo_dev);
6524 +module_init(ixp_module_init);
6525 +module_exit(ixp_module_exit);
6527 +MODULE_LICENSE("GPL");
6528 +MODULE_AUTHOR("Christian Hohnstaedt <chohnstaedt@innominate.com>");
6529 +MODULE_DESCRIPTION("IXP4xx hardware crypto");
6531 --- a/drivers/crypto/padlock-aes.c
6532 +++ b/drivers/crypto/padlock-aes.c
6533 @@ -385,12 +385,12 @@
6536 if (!cpu_has_xcrypt) {
6537 - printk(KERN_ERR PFX "VIA PadLock not detected.\n");
6538 + printk(KERN_NOTICE PFX "VIA PadLock not detected.\n");
6542 if (!cpu_has_xcrypt_enabled) {
6543 - printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
6544 + printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
6548 --- a/drivers/crypto/padlock-sha.c
6549 +++ b/drivers/crypto/padlock-sha.c
6550 @@ -254,12 +254,12 @@
6554 - printk(KERN_ERR PFX "VIA PadLock Hash Engine not detected.\n");
6555 + printk(KERN_NOTICE PFX "VIA PadLock Hash Engine not detected.\n");
6559 if (!cpu_has_phe_enabled) {
6560 - printk(KERN_ERR PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
6561 + printk(KERN_NOTICE PFX "VIA PadLock detected, but not enabled. Hmm, strange...\n");
6566 +++ b/drivers/crypto/talitos.c
6569 + * talitos - Freescale Integrated Security Engine (SEC) device driver
6571 + * Copyright (c) 2008 Freescale Semiconductor, Inc.
6573 + * Scatterlist Crypto API glue code copied from files with the following:
6574 + * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
6576 + * Crypto algorithm registration code copied from hifn driver:
6577 + * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
6578 + * All rights reserved.
6580 + * This program is free software; you can redistribute it and/or modify
6581 + * it under the terms of the GNU General Public License as published by
6582 + * the Free Software Foundation; either version 2 of the License, or
6583 + * (at your option) any later version.
6585 + * This program is distributed in the hope that it will be useful,
6586 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6587 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6588 + * GNU General Public License for more details.
6590 + * You should have received a copy of the GNU General Public License
6591 + * along with this program; if not, write to the Free Software
6592 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
6595 +#include <linux/kernel.h>
6596 +#include <linux/module.h>
6597 +#include <linux/mod_devicetable.h>
6598 +#include <linux/device.h>
6599 +#include <linux/interrupt.h>
6600 +#include <linux/crypto.h>
6601 +#include <linux/hw_random.h>
6602 +#include <linux/of_platform.h>
6603 +#include <linux/dma-mapping.h>
6604 +#include <linux/io.h>
6605 +#include <linux/spinlock.h>
6606 +#include <linux/rtnetlink.h>
6608 +#include <crypto/algapi.h>
6609 +#include <crypto/aes.h>
6610 +#include <crypto/des.h>
6611 +#include <crypto/sha.h>
6612 +#include <crypto/aead.h>
6613 +#include <crypto/authenc.h>
6615 +#include "talitos.h"
6617 +#define TALITOS_TIMEOUT 100000
6618 +#define TALITOS_MAX_DATA_LEN 65535
6620 +#define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
6621 +#define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
6622 +#define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
6624 +/* descriptor pointer entry */
6625 +struct talitos_ptr {
6626 + __be16 len; /* length */
6627 + u8 j_extent; /* jump to sg link table and/or extent */
6628 + u8 eptr; /* extended address */
6629 + __be32 ptr; /* address */
6633 +struct talitos_desc {
6634 + __be32 hdr; /* header high bits */
6635 + __be32 hdr_lo; /* header low bits */
6636 + struct talitos_ptr ptr[7]; /* ptr/len pair array */
6640 + * talitos_request - descriptor submission request
6641 + * @desc: descriptor pointer (kernel virtual)
6642 + * @dma_desc: descriptor's physical bus address
6643 + * @callback: whom to call when descriptor processing is done
6644 + * @context: caller context (optional)
6646 +struct talitos_request {
6647 + struct talitos_desc *desc;
6648 + dma_addr_t dma_desc;
6649 + void (*callback) (struct device *dev, struct talitos_desc *desc,
6650 + void *context, int error);
6654 +struct talitos_private {
6655 + struct device *dev;
6656 + struct of_device *ofdev;
6657 + void __iomem *reg;
6660 + /* SEC version geometry (from device tree node) */
6661 + unsigned int num_channels;
6662 + unsigned int chfifo_len;
6663 + unsigned int exec_units;
6664 + unsigned int desc_types;
6666 + /* next channel to be assigned next incoming descriptor */
6667 + atomic_t last_chan;
6669 + /* per-channel request fifo */
6670 + struct talitos_request **fifo;
6673 + * length of the request fifo
6674 + * fifo_len is chfifo_len rounded up to next power of 2
6675 + * so we can use bitwise ops to wrap
6677 + unsigned int fifo_len;
6679 + /* per-channel index to next free descriptor request */
6682 + /* per-channel index to next in-progress/done descriptor request */
6685 + /* per-channel request submission (head) and release (tail) locks */
6686 + spinlock_t *head_lock;
6687 + spinlock_t *tail_lock;
6689 + /* request callback tasklet */
6690 + struct tasklet_struct done_task;
6691 + struct tasklet_struct error_task;
6693 + /* list of registered algorithms */
6694 + struct list_head alg_list;
6696 + /* hwrng device */
6701 + * map virtual single (contiguous) pointer to h/w descriptor pointer
6703 +static void map_single_talitos_ptr(struct device *dev,
6704 + struct talitos_ptr *talitos_ptr,
6705 + unsigned short len, void *data,
6706 + unsigned char extent,
6707 + enum dma_data_direction dir)
6709 + talitos_ptr->len = cpu_to_be16(len);
6710 + talitos_ptr->ptr = cpu_to_be32(dma_map_single(dev, data, len, dir));
6711 + talitos_ptr->j_extent = extent;
6715 + * unmap bus single (contiguous) h/w descriptor pointer
6717 +static void unmap_single_talitos_ptr(struct device *dev,
6718 + struct talitos_ptr *talitos_ptr,
6719 + enum dma_data_direction dir)
6721 + dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
6722 + be16_to_cpu(talitos_ptr->len), dir);
6725 +static int reset_channel(struct device *dev, int ch)
6727 + struct talitos_private *priv = dev_get_drvdata(dev);
6728 + unsigned int timeout = TALITOS_TIMEOUT;
6730 + setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
6732 + while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
6736 + if (timeout == 0) {
6737 + dev_err(dev, "failed to reset channel %d\n", ch);
6741 + /* set done writeback and IRQ */
6742 + setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_CDWE |
6743 + TALITOS_CCCR_LO_CDIE);
6748 +static int reset_device(struct device *dev)
6750 + struct talitos_private *priv = dev_get_drvdata(dev);
6751 + unsigned int timeout = TALITOS_TIMEOUT;
6753 + setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
6755 + while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
6759 + if (timeout == 0) {
6760 + dev_err(dev, "failed to reset device\n");
6768 + * Reset and initialize the device
6770 +static int init_device(struct device *dev)
6772 + struct talitos_private *priv = dev_get_drvdata(dev);
6777 + * errata documentation: warning: certain SEC interrupts
6778 + * are not fully cleared by writing the MCR:SWR bit,
6779 + * set bit twice to completely reset
6781 + err = reset_device(dev);
6785 + err = reset_device(dev);
6789 + /* reset channels */
6790 + for (ch = 0; ch < priv->num_channels; ch++) {
6791 + err = reset_channel(dev, ch);
6796 + /* enable channel done and error interrupts */
6797 + setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
6798 + setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
6804 + * talitos_submit - submits a descriptor to the device for processing
6805 + * @dev: the SEC device to be used
6806 + * @desc: the descriptor to be processed by the device
6807 + * @callback: whom to call when processing is complete
6808 + * @context: a handle for use by caller (optional)
6810 + * desc must contain valid dma-mapped (bus physical) address pointers.
6811 + * callback must check err and feedback in descriptor header
6812 + * for device processing status.
6814 +static int talitos_submit(struct device *dev, struct talitos_desc *desc,
6815 + void (*callback)(struct device *dev,
6816 + struct talitos_desc *desc,
6817 + void *context, int error),
6820 + struct talitos_private *priv = dev_get_drvdata(dev);
6821 + struct talitos_request *request;
6822 + unsigned long flags, ch;
6825 + /* select done notification */
6826 + desc->hdr |= DESC_HDR_DONE_NOTIFY;
6828 + /* emulate SEC's round-robin channel fifo polling scheme */
6829 + ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1);
6831 + spin_lock_irqsave(&priv->head_lock[ch], flags);
6833 + head = priv->head[ch];
6834 + request = &priv->fifo[ch][head];
6836 + if (request->desc) {
6837 + /* request queue is full */
6838 + spin_unlock_irqrestore(&priv->head_lock[ch], flags);
6842 + /* map descriptor and save caller data */
6843 + request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
6844 + DMA_BIDIRECTIONAL);
6845 + request->callback = callback;
6846 + request->context = context;
6848 + /* increment fifo head */
6849 + priv->head[ch] = (priv->head[ch] + 1) & (priv->fifo_len - 1);
6852 + request->desc = desc;
6856 + out_be32(priv->reg + TALITOS_FF_LO(ch), request->dma_desc);
6858 + spin_unlock_irqrestore(&priv->head_lock[ch], flags);
6860 + return -EINPROGRESS;
6864 + * process what was done, notify callback of error if not
6866 +static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
6868 + struct talitos_private *priv = dev_get_drvdata(dev);
6869 + struct talitos_request *request, saved_req;
6870 + unsigned long flags;
6873 + spin_lock_irqsave(&priv->tail_lock[ch], flags);
6875 + tail = priv->tail[ch];
6876 + while (priv->fifo[ch][tail].desc) {
6877 + request = &priv->fifo[ch][tail];
6879 + /* descriptors with their done bits set don't get the error */
6881 + if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
6889 + dma_unmap_single(dev, request->dma_desc,
6890 + sizeof(struct talitos_desc), DMA_BIDIRECTIONAL);
6892 + /* copy entries so we can call callback outside lock */
6893 + saved_req.desc = request->desc;
6894 + saved_req.callback = request->callback;
6895 + saved_req.context = request->context;
6897 + /* release request entry in fifo */
6899 + request->desc = NULL;
6901 + /* increment fifo tail */
6902 + priv->tail[ch] = (tail + 1) & (priv->fifo_len - 1);
6904 + spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
6905 + saved_req.callback(dev, saved_req.desc, saved_req.context,
6907 + /* channel may resume processing in single desc error case */
6908 + if (error && !reset_ch && status == error)
6910 + spin_lock_irqsave(&priv->tail_lock[ch], flags);
6911 + tail = priv->tail[ch];
6914 + spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
6918 + * process completed requests for channels that have done status
6920 +static void talitos_done(unsigned long data)
6922 + struct device *dev = (struct device *)data;
6923 + struct talitos_private *priv = dev_get_drvdata(dev);
6926 + for (ch = 0; ch < priv->num_channels; ch++)
6927 + flush_channel(dev, ch, 0, 0);
6931 + * locate current (offending) descriptor
6933 +static struct talitos_desc *current_desc(struct device *dev, int ch)
6935 + struct talitos_private *priv = dev_get_drvdata(dev);
6936 + int tail = priv->tail[ch];
6937 + dma_addr_t cur_desc;
6939 + cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
6941 + while (priv->fifo[ch][tail].dma_desc != cur_desc) {
6942 + tail = (tail + 1) & (priv->fifo_len - 1);
6943 + if (tail == priv->tail[ch]) {
6944 + dev_err(dev, "couldn't locate current descriptor\n");
6949 + return priv->fifo[ch][tail].desc;
6953 + * user diagnostics; report root cause of error based on execution unit status
6955 +static void report_eu_error(struct device *dev, int ch, struct talitos_desc *desc)
6957 + struct talitos_private *priv = dev_get_drvdata(dev);
6960 + switch (desc->hdr & DESC_HDR_SEL0_MASK) {
6961 + case DESC_HDR_SEL0_AFEU:
6962 + dev_err(dev, "AFEUISR 0x%08x_%08x\n",
6963 + in_be32(priv->reg + TALITOS_AFEUISR),
6964 + in_be32(priv->reg + TALITOS_AFEUISR_LO));
6966 + case DESC_HDR_SEL0_DEU:
6967 + dev_err(dev, "DEUISR 0x%08x_%08x\n",
6968 + in_be32(priv->reg + TALITOS_DEUISR),
6969 + in_be32(priv->reg + TALITOS_DEUISR_LO));
6971 + case DESC_HDR_SEL0_MDEUA:
6972 + case DESC_HDR_SEL0_MDEUB:
6973 + dev_err(dev, "MDEUISR 0x%08x_%08x\n",
6974 + in_be32(priv->reg + TALITOS_MDEUISR),
6975 + in_be32(priv->reg + TALITOS_MDEUISR_LO));
6977 + case DESC_HDR_SEL0_RNG:
6978 + dev_err(dev, "RNGUISR 0x%08x_%08x\n",
6979 + in_be32(priv->reg + TALITOS_RNGUISR),
6980 + in_be32(priv->reg + TALITOS_RNGUISR_LO));
6982 + case DESC_HDR_SEL0_PKEU:
6983 + dev_err(dev, "PKEUISR 0x%08x_%08x\n",
6984 + in_be32(priv->reg + TALITOS_PKEUISR),
6985 + in_be32(priv->reg + TALITOS_PKEUISR_LO));
6987 + case DESC_HDR_SEL0_AESU:
6988 + dev_err(dev, "AESUISR 0x%08x_%08x\n",
6989 + in_be32(priv->reg + TALITOS_AESUISR),
6990 + in_be32(priv->reg + TALITOS_AESUISR_LO));
6992 + case DESC_HDR_SEL0_CRCU:
6993 + dev_err(dev, "CRCUISR 0x%08x_%08x\n",
6994 + in_be32(priv->reg + TALITOS_CRCUISR),
6995 + in_be32(priv->reg + TALITOS_CRCUISR_LO));
6997 + case DESC_HDR_SEL0_KEU:
6998 + dev_err(dev, "KEUISR 0x%08x_%08x\n",
6999 + in_be32(priv->reg + TALITOS_KEUISR),
7000 + in_be32(priv->reg + TALITOS_KEUISR_LO));
7004 + switch (desc->hdr & DESC_HDR_SEL1_MASK) {
7005 + case DESC_HDR_SEL1_MDEUA:
7006 + case DESC_HDR_SEL1_MDEUB:
7007 + dev_err(dev, "MDEUISR 0x%08x_%08x\n",
7008 + in_be32(priv->reg + TALITOS_MDEUISR),
7009 + in_be32(priv->reg + TALITOS_MDEUISR_LO));
7011 + case DESC_HDR_SEL1_CRCU:
7012 + dev_err(dev, "CRCUISR 0x%08x_%08x\n",
7013 + in_be32(priv->reg + TALITOS_CRCUISR),
7014 + in_be32(priv->reg + TALITOS_CRCUISR_LO));
7018 + for (i = 0; i < 8; i++)
7019 + dev_err(dev, "DESCBUF 0x%08x_%08x\n",
7020 + in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
7021 + in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
7025 + * recover from error interrupts
7027 +static void talitos_error(unsigned long data)
7029 + struct device *dev = (struct device *)data;
7030 + struct talitos_private *priv = dev_get_drvdata(dev);
7031 + unsigned int timeout = TALITOS_TIMEOUT;
7032 + int ch, error, reset_dev = 0, reset_ch = 0;
7033 + u32 isr, isr_lo, v, v_lo;
7035 + isr = in_be32(priv->reg + TALITOS_ISR);
7036 + isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
7038 + for (ch = 0; ch < priv->num_channels; ch++) {
7039 + /* skip channels without errors */
7040 + if (!(isr & (1 << (ch * 2 + 1))))
7045 + v = in_be32(priv->reg + TALITOS_CCPSR(ch));
7046 + v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
7048 + if (v_lo & TALITOS_CCPSR_LO_DOF) {
7049 + dev_err(dev, "double fetch fifo overflow error\n");
7053 + if (v_lo & TALITOS_CCPSR_LO_SOF) {
7054 + /* h/w dropped descriptor */
7055 + dev_err(dev, "single fetch fifo overflow error\n");
7058 + if (v_lo & TALITOS_CCPSR_LO_MDTE)
7059 + dev_err(dev, "master data transfer error\n");
7060 + if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
7061 + dev_err(dev, "s/g data length zero error\n");
7062 + if (v_lo & TALITOS_CCPSR_LO_FPZ)
7063 + dev_err(dev, "fetch pointer zero error\n");
7064 + if (v_lo & TALITOS_CCPSR_LO_IDH)
7065 + dev_err(dev, "illegal descriptor header error\n");
7066 + if (v_lo & TALITOS_CCPSR_LO_IEU)
7067 + dev_err(dev, "invalid execution unit error\n");
7068 + if (v_lo & TALITOS_CCPSR_LO_EU)
7069 + report_eu_error(dev, ch, current_desc(dev, ch));
7070 + if (v_lo & TALITOS_CCPSR_LO_GB)
7071 + dev_err(dev, "gather boundary error\n");
7072 + if (v_lo & TALITOS_CCPSR_LO_GRL)
7073 + dev_err(dev, "gather return/length error\n");
7074 + if (v_lo & TALITOS_CCPSR_LO_SB)
7075 + dev_err(dev, "scatter boundary error\n");
7076 + if (v_lo & TALITOS_CCPSR_LO_SRL)
7077 + dev_err(dev, "scatter return/length error\n");
7079 + flush_channel(dev, ch, error, reset_ch);
7082 + reset_channel(dev, ch);
7084 + setbits32(priv->reg + TALITOS_CCCR(ch),
7085 + TALITOS_CCCR_CONT);
7086 + setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
7087 + while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
7088 + TALITOS_CCCR_CONT) && --timeout)
7090 + if (timeout == 0) {
7091 + dev_err(dev, "failed to restart channel %d\n",
7097 + if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
7098 + dev_err(dev, "done overflow, internal time out, or rngu error: "
7099 + "ISR 0x%08x_%08x\n", isr, isr_lo);
7101 + /* purge request queues */
7102 + for (ch = 0; ch < priv->num_channels; ch++)
7103 + flush_channel(dev, ch, -EIO, 1);
7105 + /* reset and reinitialize the device */
7110 +static irqreturn_t talitos_interrupt(int irq, void *data)
7112 + struct device *dev = data;
7113 + struct talitos_private *priv = dev_get_drvdata(dev);
7116 + isr = in_be32(priv->reg + TALITOS_ISR);
7117 + isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
7120 + out_be32(priv->reg + TALITOS_ICR, isr);
7121 + out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
7123 + if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
7124 + talitos_error((unsigned long)data);
7126 + if (likely(isr & TALITOS_ISR_CHDONE))
7127 + tasklet_schedule(&priv->done_task);
7129 + return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
7135 +static int talitos_rng_data_present(struct hwrng *rng, int wait)
7137 + struct device *dev = (struct device *)rng->priv;
7138 + struct talitos_private *priv = dev_get_drvdata(dev);
7142 + for (i = 0; i < 20; i++) {
7143 + ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
7144 + TALITOS_RNGUSR_LO_OFL;
7153 +static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
7155 + struct device *dev = (struct device *)rng->priv;
7156 + struct talitos_private *priv = dev_get_drvdata(dev);
7158 + /* rng fifo requires 64-bit accesses */
7159 + *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
7160 + *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
7162 + return sizeof(u32);
7165 +static int talitos_rng_init(struct hwrng *rng)
7167 + struct device *dev = (struct device *)rng->priv;
7168 + struct talitos_private *priv = dev_get_drvdata(dev);
7169 + unsigned int timeout = TALITOS_TIMEOUT;
7171 + setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
7172 + while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
7175 + if (timeout == 0) {
7176 + dev_err(dev, "failed to reset rng hw\n");
7180 + /* start generating */
7181 + setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
7186 +static int talitos_register_rng(struct device *dev)
7188 + struct talitos_private *priv = dev_get_drvdata(dev);
7190 + priv->rng.name = dev_driver_string(dev),
7191 + priv->rng.init = talitos_rng_init,
7192 + priv->rng.data_present = talitos_rng_data_present,
7193 + priv->rng.data_read = talitos_rng_data_read,
7194 + priv->rng.priv = (unsigned long)dev;
7196 + return hwrng_register(&priv->rng);
7199 +static void talitos_unregister_rng(struct device *dev)
7201 + struct talitos_private *priv = dev_get_drvdata(dev);
7203 + hwrng_unregister(&priv->rng);
7209 +#define TALITOS_CRA_PRIORITY 3000
7210 +#define TALITOS_MAX_KEY_SIZE 64
7211 +#define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
7213 +#define MD5_DIGEST_SIZE 16
7215 +struct talitos_ctx {
7216 + struct device *dev;
7217 + __be32 desc_hdr_template;
7218 + u8 key[TALITOS_MAX_KEY_SIZE];
7219 + u8 iv[TALITOS_MAX_IV_LENGTH];
7220 + unsigned int keylen;
7221 + unsigned int enckeylen;
7222 + unsigned int authkeylen;
7223 + unsigned int authsize;
7226 +static int aead_authenc_setauthsize(struct crypto_aead *authenc,
7227 + unsigned int authsize)
7229 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7231 + ctx->authsize = authsize;
7236 +static int aead_authenc_setkey(struct crypto_aead *authenc,
7237 + const u8 *key, unsigned int keylen)
7239 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7240 + struct rtattr *rta = (void *)key;
7241 + struct crypto_authenc_key_param *param;
7242 + unsigned int authkeylen;
7243 + unsigned int enckeylen;
7245 + if (!RTA_OK(rta, keylen))
7248 + if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
7251 + if (RTA_PAYLOAD(rta) < sizeof(*param))
7254 + param = RTA_DATA(rta);
7255 + enckeylen = be32_to_cpu(param->enckeylen);
7257 + key += RTA_ALIGN(rta->rta_len);
7258 + keylen -= RTA_ALIGN(rta->rta_len);
7260 + if (keylen < enckeylen)
7263 + authkeylen = keylen - enckeylen;
7265 + if (keylen > TALITOS_MAX_KEY_SIZE)
7268 + memcpy(&ctx->key, key, keylen);
7270 + ctx->keylen = keylen;
7271 + ctx->enckeylen = enckeylen;
7272 + ctx->authkeylen = authkeylen;
7277 + crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
7282 + * ipsec_esp_edesc - s/w-extended ipsec_esp descriptor
7283 + * @src_nents: number of segments in input scatterlist
7284 + * @dst_nents: number of segments in output scatterlist
7285 + * @dma_len: length of dma mapped link_tbl space
7286 + * @dma_link_tbl: bus physical address of link_tbl
7287 + * @desc: h/w descriptor
7288 + * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
7290 + * if decrypting (with authcheck), or either one of src_nents or dst_nents
7291 + * is greater than 1, an integrity check value is concatenated to the end
7292 + * of link_tbl data
7294 +struct ipsec_esp_edesc {
7298 + dma_addr_t dma_link_tbl;
7299 + struct talitos_desc desc;
7300 + struct talitos_ptr link_tbl[0];
7303 +static void ipsec_esp_unmap(struct device *dev,
7304 + struct ipsec_esp_edesc *edesc,
7305 + struct aead_request *areq)
7307 + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
7308 + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
7309 + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
7310 + unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
7312 + dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
7314 + if (areq->src != areq->dst) {
7315 + dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1,
7317 + dma_unmap_sg(dev, areq->dst, edesc->dst_nents ? : 1,
7320 + dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1,
7321 + DMA_BIDIRECTIONAL);
7324 + if (edesc->dma_len)
7325 + dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
7326 + DMA_BIDIRECTIONAL);
7330 + * ipsec_esp descriptor callbacks
7332 +static void ipsec_esp_encrypt_done(struct device *dev,
7333 + struct talitos_desc *desc, void *context,
7336 + struct aead_request *areq = context;
7337 + struct ipsec_esp_edesc *edesc =
7338 + container_of(desc, struct ipsec_esp_edesc, desc);
7339 + struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
7340 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7341 + struct scatterlist *sg;
7344 + ipsec_esp_unmap(dev, edesc, areq);
7346 + /* copy the generated ICV to dst */
7347 + if (edesc->dma_len) {
7348 + icvdata = &edesc->link_tbl[edesc->src_nents +
7349 + edesc->dst_nents + 1];
7350 + sg = sg_last(areq->dst, edesc->dst_nents);
7351 + memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
7352 + icvdata, ctx->authsize);
7357 + aead_request_complete(areq, err);
7360 +static void ipsec_esp_decrypt_done(struct device *dev,
7361 + struct talitos_desc *desc, void *context,
7364 + struct aead_request *req = context;
7365 + struct ipsec_esp_edesc *edesc =
7366 + container_of(desc, struct ipsec_esp_edesc, desc);
7367 + struct crypto_aead *authenc = crypto_aead_reqtfm(req);
7368 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7369 + struct scatterlist *sg;
7372 + ipsec_esp_unmap(dev, edesc, req);
7376 + if (edesc->dma_len)
7377 + icvdata = &edesc->link_tbl[edesc->src_nents +
7378 + edesc->dst_nents + 1];
7380 + icvdata = &edesc->link_tbl[0];
7382 + sg = sg_last(req->dst, edesc->dst_nents ? : 1);
7383 + err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
7384 + ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
7389 + aead_request_complete(req, err);
7393 + * convert scatterlist to SEC h/w link table format
7394 + * stop at cryptlen bytes
7396 +static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
7397 + int cryptlen, struct talitos_ptr *link_tbl_ptr)
7399 + int n_sg = sg_count;
7402 + link_tbl_ptr->ptr = cpu_to_be32(sg_dma_address(sg));
7403 + link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
7404 + link_tbl_ptr->j_extent = 0;
7406 + cryptlen -= sg_dma_len(sg);
7410 + /* adjust (decrease) last one (or two) entry's len to cryptlen */
7412 + while (link_tbl_ptr->len <= (-cryptlen)) {
7413 + /* Empty this entry, and move to previous one */
7414 + cryptlen += be16_to_cpu(link_tbl_ptr->len);
7415 + link_tbl_ptr->len = 0;
7419 + link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
7422 + /* tag end of link table */
7423 + link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
7429 + * fill in and submit ipsec_esp descriptor
7431 +static int ipsec_esp(struct ipsec_esp_edesc *edesc, struct aead_request *areq,
7433 + void (*callback) (struct device *dev,
7434 + struct talitos_desc *desc,
7435 + void *context, int error))
7437 + struct crypto_aead *aead = crypto_aead_reqtfm(areq);
7438 + struct talitos_ctx *ctx = crypto_aead_ctx(aead);
7439 + struct device *dev = ctx->dev;
7440 + struct talitos_desc *desc = &edesc->desc;
7441 + unsigned int cryptlen = areq->cryptlen;
7442 + unsigned int authsize = ctx->authsize;
7443 + unsigned int ivsize;
7447 + map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
7448 + 0, DMA_TO_DEVICE);
7450 + map_single_talitos_ptr(dev, &desc->ptr[1], sg_virt(areq->src) -
7451 + sg_virt(areq->assoc), sg_virt(areq->assoc), 0,
7454 + ivsize = crypto_aead_ivsize(aead);
7455 + map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
7459 + map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
7460 + (char *)&ctx->key + ctx->authkeylen, 0,
7465 + * map and adjust cipher len to aead request cryptlen.
7466 + * extent is bytes of HMAC postpended to ciphertext,
7467 + * typically 12 for ipsec
7469 + desc->ptr[4].len = cpu_to_be16(cryptlen);
7470 + desc->ptr[4].j_extent = authsize;
7472 + if (areq->src == areq->dst)
7473 + sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1,
7474 + DMA_BIDIRECTIONAL);
7476 + sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1,
7479 + if (sg_count == 1) {
7480 + desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
7482 + sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
7483 + &edesc->link_tbl[0]);
7484 + if (sg_count > 1) {
7485 + desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
7486 + desc->ptr[4].ptr = cpu_to_be32(edesc->dma_link_tbl);
7487 + dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
7488 + edesc->dma_len, DMA_BIDIRECTIONAL);
7490 + /* Only one segment now, so no link tbl needed */
7491 + desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
7496 + desc->ptr[5].len = cpu_to_be16(cryptlen);
7497 + desc->ptr[5].j_extent = authsize;
7499 + if (areq->src != areq->dst) {
7500 + sg_count = dma_map_sg(dev, areq->dst, edesc->dst_nents ? : 1,
7504 + if (sg_count == 1) {
7505 + desc->ptr[5].ptr = cpu_to_be32(sg_dma_address(areq->dst));
7507 + struct talitos_ptr *link_tbl_ptr =
7508 + &edesc->link_tbl[edesc->src_nents];
7509 + struct scatterlist *sg;
7511 + desc->ptr[5].ptr = cpu_to_be32((struct talitos_ptr *)
7512 + edesc->dma_link_tbl +
7513 + edesc->src_nents);
7514 + if (areq->src == areq->dst) {
7515 + memcpy(link_tbl_ptr, &edesc->link_tbl[0],
7516 + edesc->src_nents * sizeof(struct talitos_ptr));
7518 + sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
7521 + link_tbl_ptr += sg_count - 1;
7523 + /* handle case where sg_last contains the ICV exclusively */
7524 + sg = sg_last(areq->dst, edesc->dst_nents);
7525 + if (sg->length == ctx->authsize)
7528 + link_tbl_ptr->j_extent = 0;
7530 + link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
7531 + link_tbl_ptr->len = cpu_to_be16(authsize);
7533 + /* icv data follows link tables */
7534 + link_tbl_ptr->ptr = cpu_to_be32((struct talitos_ptr *)
7535 + edesc->dma_link_tbl +
7536 + edesc->src_nents +
7537 + edesc->dst_nents + 1);
7539 + desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
7540 + dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
7541 + edesc->dma_len, DMA_BIDIRECTIONAL);
7545 + map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
7548 + return talitos_submit(dev, desc, callback, areq);
7553 + * derive number of elements in scatterlist
7555 +static int sg_count(struct scatterlist *sg_list, int nbytes)
7557 + struct scatterlist *sg = sg_list;
7562 + nbytes -= sg->length;
7570 + * allocate and map the ipsec_esp extended descriptor
7572 +static struct ipsec_esp_edesc *ipsec_esp_edesc_alloc(struct aead_request *areq,
7575 + struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
7576 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7577 + struct ipsec_esp_edesc *edesc;
7578 + int src_nents, dst_nents, alloc_len, dma_len;
7580 + if (areq->cryptlen + ctx->authsize > TALITOS_MAX_DATA_LEN) {
7581 + dev_err(ctx->dev, "cryptlen exceeds h/w max limit\n");
7582 + return ERR_PTR(-EINVAL);
7585 + src_nents = sg_count(areq->src, areq->cryptlen + ctx->authsize);
7586 + src_nents = (src_nents == 1) ? 0 : src_nents;
7588 + if (areq->dst == areq->src) {
7589 + dst_nents = src_nents;
7591 + dst_nents = sg_count(areq->dst, areq->cryptlen + ctx->authsize);
7592 + dst_nents = (dst_nents == 1) ? 0 : src_nents;
7596 + * allocate space for base edesc plus the link tables,
7597 + * allowing for a separate entry for the generated ICV (+ 1),
7598 + * and the ICV data itself
7600 + alloc_len = sizeof(struct ipsec_esp_edesc);
7601 + if (src_nents || dst_nents) {
7602 + dma_len = (src_nents + dst_nents + 1) *
7603 + sizeof(struct talitos_ptr) + ctx->authsize;
7604 + alloc_len += dma_len;
7607 + alloc_len += icv_stashing ? ctx->authsize : 0;
7610 + edesc = kmalloc(alloc_len, GFP_DMA);
7612 + dev_err(ctx->dev, "could not allocate edescriptor\n");
7613 + return ERR_PTR(-ENOMEM);
7616 + edesc->src_nents = src_nents;
7617 + edesc->dst_nents = dst_nents;
7618 + edesc->dma_len = dma_len;
7619 + edesc->dma_link_tbl = dma_map_single(ctx->dev, &edesc->link_tbl[0],
7620 + edesc->dma_len, DMA_BIDIRECTIONAL);
7625 +static int aead_authenc_encrypt(struct aead_request *req)
7627 + struct crypto_aead *authenc = crypto_aead_reqtfm(req);
7628 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7629 + struct ipsec_esp_edesc *edesc;
7631 + /* allocate extended descriptor */
7632 + edesc = ipsec_esp_edesc_alloc(req, 0);
7633 + if (IS_ERR(edesc))
7634 + return PTR_ERR(edesc);
7637 + edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
7639 + return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
7642 +static int aead_authenc_decrypt(struct aead_request *req)
7644 + struct crypto_aead *authenc = crypto_aead_reqtfm(req);
7645 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7646 + unsigned int authsize = ctx->authsize;
7647 + struct ipsec_esp_edesc *edesc;
7648 + struct scatterlist *sg;
7651 + req->cryptlen -= authsize;
7653 + /* allocate extended descriptor */
7654 + edesc = ipsec_esp_edesc_alloc(req, 1);
7655 + if (IS_ERR(edesc))
7656 + return PTR_ERR(edesc);
7658 + /* stash incoming ICV for later cmp with ICV generated by the h/w */
7659 + if (edesc->dma_len)
7660 + icvdata = &edesc->link_tbl[edesc->src_nents +
7661 + edesc->dst_nents + 1];
7663 + icvdata = &edesc->link_tbl[0];
7665 + sg = sg_last(req->src, edesc->src_nents ? : 1);
7667 + memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
7671 + edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
7673 + return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_done);
7676 +static int aead_authenc_givencrypt(
7677 + struct aead_givcrypt_request *req)
7679 + struct aead_request *areq = &req->areq;
7680 + struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
7681 + struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7682 + struct ipsec_esp_edesc *edesc;
7684 + /* allocate extended descriptor */
7685 + edesc = ipsec_esp_edesc_alloc(areq, 0);
7686 + if (IS_ERR(edesc))
7687 + return PTR_ERR(edesc);
7690 + edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
7692 + memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
7694 + return ipsec_esp(edesc, areq, req->giv, req->seq,
7695 + ipsec_esp_encrypt_done);
7698 +struct talitos_alg_template {
7699 + char name[CRYPTO_MAX_ALG_NAME];
7700 + char driver_name[CRYPTO_MAX_ALG_NAME];
7701 + unsigned int blocksize;
7702 + struct aead_alg aead;
7703 + struct device *dev;
7704 + __be32 desc_hdr_template;
7707 +static struct talitos_alg_template driver_algs[] = {
7708 + /* single-pass ipsec_esp descriptor */
7710 + .name = "authenc(hmac(sha1),cbc(aes))",
7711 + .driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
7712 + .blocksize = AES_BLOCK_SIZE,
7714 + .setkey = aead_authenc_setkey,
7715 + .setauthsize = aead_authenc_setauthsize,
7716 + .encrypt = aead_authenc_encrypt,
7717 + .decrypt = aead_authenc_decrypt,
7718 + .givencrypt = aead_authenc_givencrypt,
7719 + .geniv = "<built-in>",
7720 + .ivsize = AES_BLOCK_SIZE,
7721 + .maxauthsize = SHA1_DIGEST_SIZE,
7723 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7724 + DESC_HDR_SEL0_AESU |
7725 + DESC_HDR_MODE0_AESU_CBC |
7726 + DESC_HDR_SEL1_MDEUA |
7727 + DESC_HDR_MODE1_MDEU_INIT |
7728 + DESC_HDR_MODE1_MDEU_PAD |
7729 + DESC_HDR_MODE1_MDEU_SHA1_HMAC,
7732 + .name = "authenc(hmac(sha1),cbc(des3_ede))",
7733 + .driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
7734 + .blocksize = DES3_EDE_BLOCK_SIZE,
7736 + .setkey = aead_authenc_setkey,
7737 + .setauthsize = aead_authenc_setauthsize,
7738 + .encrypt = aead_authenc_encrypt,
7739 + .decrypt = aead_authenc_decrypt,
7740 + .givencrypt = aead_authenc_givencrypt,
7741 + .geniv = "<built-in>",
7742 + .ivsize = DES3_EDE_BLOCK_SIZE,
7743 + .maxauthsize = SHA1_DIGEST_SIZE,
7745 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7746 + DESC_HDR_SEL0_DEU |
7747 + DESC_HDR_MODE0_DEU_CBC |
7748 + DESC_HDR_MODE0_DEU_3DES |
7749 + DESC_HDR_SEL1_MDEUA |
7750 + DESC_HDR_MODE1_MDEU_INIT |
7751 + DESC_HDR_MODE1_MDEU_PAD |
7752 + DESC_HDR_MODE1_MDEU_SHA1_HMAC,
7755 + .name = "authenc(hmac(sha256),cbc(aes))",
7756 + .driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
7757 + .blocksize = AES_BLOCK_SIZE,
7759 + .setkey = aead_authenc_setkey,
7760 + .setauthsize = aead_authenc_setauthsize,
7761 + .encrypt = aead_authenc_encrypt,
7762 + .decrypt = aead_authenc_decrypt,
7763 + .givencrypt = aead_authenc_givencrypt,
7764 + .geniv = "<built-in>",
7765 + .ivsize = AES_BLOCK_SIZE,
7766 + .maxauthsize = SHA256_DIGEST_SIZE,
7768 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7769 + DESC_HDR_SEL0_AESU |
7770 + DESC_HDR_MODE0_AESU_CBC |
7771 + DESC_HDR_SEL1_MDEUA |
7772 + DESC_HDR_MODE1_MDEU_INIT |
7773 + DESC_HDR_MODE1_MDEU_PAD |
7774 + DESC_HDR_MODE1_MDEU_SHA256_HMAC,
7777 + .name = "authenc(hmac(sha256),cbc(des3_ede))",
7778 + .driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
7779 + .blocksize = DES3_EDE_BLOCK_SIZE,
7781 + .setkey = aead_authenc_setkey,
7782 + .setauthsize = aead_authenc_setauthsize,
7783 + .encrypt = aead_authenc_encrypt,
7784 + .decrypt = aead_authenc_decrypt,
7785 + .givencrypt = aead_authenc_givencrypt,
7786 + .geniv = "<built-in>",
7787 + .ivsize = DES3_EDE_BLOCK_SIZE,
7788 + .maxauthsize = SHA256_DIGEST_SIZE,
7790 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7791 + DESC_HDR_SEL0_DEU |
7792 + DESC_HDR_MODE0_DEU_CBC |
7793 + DESC_HDR_MODE0_DEU_3DES |
7794 + DESC_HDR_SEL1_MDEUA |
7795 + DESC_HDR_MODE1_MDEU_INIT |
7796 + DESC_HDR_MODE1_MDEU_PAD |
7797 + DESC_HDR_MODE1_MDEU_SHA256_HMAC,
7800 + .name = "authenc(hmac(md5),cbc(aes))",
7801 + .driver_name = "authenc-hmac-md5-cbc-aes-talitos",
7802 + .blocksize = AES_BLOCK_SIZE,
7804 + .setkey = aead_authenc_setkey,
7805 + .setauthsize = aead_authenc_setauthsize,
7806 + .encrypt = aead_authenc_encrypt,
7807 + .decrypt = aead_authenc_decrypt,
7808 + .givencrypt = aead_authenc_givencrypt,
7809 + .geniv = "<built-in>",
7810 + .ivsize = AES_BLOCK_SIZE,
7811 + .maxauthsize = MD5_DIGEST_SIZE,
7813 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7814 + DESC_HDR_SEL0_AESU |
7815 + DESC_HDR_MODE0_AESU_CBC |
7816 + DESC_HDR_SEL1_MDEUA |
7817 + DESC_HDR_MODE1_MDEU_INIT |
7818 + DESC_HDR_MODE1_MDEU_PAD |
7819 + DESC_HDR_MODE1_MDEU_MD5_HMAC,
7822 + .name = "authenc(hmac(md5),cbc(des3_ede))",
7823 + .driver_name = "authenc-hmac-md5-cbc-3des-talitos",
7824 + .blocksize = DES3_EDE_BLOCK_SIZE,
7826 + .setkey = aead_authenc_setkey,
7827 + .setauthsize = aead_authenc_setauthsize,
7828 + .encrypt = aead_authenc_encrypt,
7829 + .decrypt = aead_authenc_decrypt,
7830 + .givencrypt = aead_authenc_givencrypt,
7831 + .geniv = "<built-in>",
7832 + .ivsize = DES3_EDE_BLOCK_SIZE,
7833 + .maxauthsize = MD5_DIGEST_SIZE,
7835 + .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
7836 + DESC_HDR_SEL0_DEU |
7837 + DESC_HDR_MODE0_DEU_CBC |
7838 + DESC_HDR_MODE0_DEU_3DES |
7839 + DESC_HDR_SEL1_MDEUA |
7840 + DESC_HDR_MODE1_MDEU_INIT |
7841 + DESC_HDR_MODE1_MDEU_PAD |
7842 + DESC_HDR_MODE1_MDEU_MD5_HMAC,
7846 +struct talitos_crypto_alg {
7847 + struct list_head entry;
7848 + struct device *dev;
7849 + __be32 desc_hdr_template;
7850 + struct crypto_alg crypto_alg;
7853 +static int talitos_cra_init(struct crypto_tfm *tfm)
7855 + struct crypto_alg *alg = tfm->__crt_alg;
7856 + struct talitos_crypto_alg *talitos_alg =
7857 + container_of(alg, struct talitos_crypto_alg, crypto_alg);
7858 + struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
7860 + /* update context with ptr to dev */
7861 + ctx->dev = talitos_alg->dev;
7862 + /* copy descriptor header template value */
7863 + ctx->desc_hdr_template = talitos_alg->desc_hdr_template;
7865 + /* random first IV */
7866 + get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
7872 + * given the alg's descriptor header template, determine whether descriptor
7873 + * type and primary/secondary execution units required match the hw
7874 + * capabilities description provided in the device tree node.
7876 +static int hw_supports(struct device *dev, __be32 desc_hdr_template)
7878 + struct talitos_private *priv = dev_get_drvdata(dev);
7881 + ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
7882 + (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
7884 + if (SECONDARY_EU(desc_hdr_template))
7885 + ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
7886 + & priv->exec_units);
7891 +static int __devexit talitos_remove(struct of_device *ofdev)
7893 + struct device *dev = &ofdev->dev;
7894 + struct talitos_private *priv = dev_get_drvdata(dev);
7895 + struct talitos_crypto_alg *t_alg, *n;
7898 + list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
7899 + crypto_unregister_alg(&t_alg->crypto_alg);
7900 + list_del(&t_alg->entry);
7904 + if (hw_supports(dev, DESC_HDR_SEL0_RNG))
7905 + talitos_unregister_rng(dev);
7907 + kfree(priv->tail);
7908 + kfree(priv->head);
7911 + for (i = 0; i < priv->num_channels; i++)
7912 + kfree(priv->fifo[i]);
7914 + kfree(priv->fifo);
7915 + kfree(priv->head_lock);
7916 + kfree(priv->tail_lock);
7918 + if (priv->irq != NO_IRQ) {
7919 + free_irq(priv->irq, dev);
7920 + irq_dispose_mapping(priv->irq);
7923 + tasklet_kill(&priv->done_task);
7924 + tasklet_kill(&priv->error_task);
7926 + iounmap(priv->reg);
7928 + dev_set_drvdata(dev, NULL);
7935 +static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
7936 + struct talitos_alg_template
7939 + struct talitos_crypto_alg *t_alg;
7940 + struct crypto_alg *alg;
7942 + t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
7944 + return ERR_PTR(-ENOMEM);
7946 + alg = &t_alg->crypto_alg;
7948 + snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
7949 + snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
7950 + template->driver_name);
7951 + alg->cra_module = THIS_MODULE;
7952 + alg->cra_init = talitos_cra_init;
7953 + alg->cra_priority = TALITOS_CRA_PRIORITY;
7954 + alg->cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC;
7955 + alg->cra_blocksize = template->blocksize;
7956 + alg->cra_alignmask = 0;
7957 + alg->cra_type = &crypto_aead_type;
7958 + alg->cra_ctxsize = sizeof(struct talitos_ctx);
7959 + alg->cra_u.aead = template->aead;
7961 + t_alg->desc_hdr_template = template->desc_hdr_template;
7967 +static int talitos_probe(struct of_device *ofdev,
7968 + const struct of_device_id *match)
7970 + struct device *dev = &ofdev->dev;
7971 + struct device_node *np = ofdev->node;
7972 + struct talitos_private *priv;
7973 + const unsigned int *prop;
7976 + priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
7980 + dev_set_drvdata(dev, priv);
7982 + priv->ofdev = ofdev;
7984 + tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
7985 + tasklet_init(&priv->error_task, talitos_error, (unsigned long)dev);
7987 + priv->irq = irq_of_parse_and_map(np, 0);
7989 + if (priv->irq == NO_IRQ) {
7990 + dev_err(dev, "failed to map irq\n");
7995 + /* get the irq line */
7996 + err = request_irq(priv->irq, talitos_interrupt, 0,
7997 + dev_driver_string(dev), dev);
7999 + dev_err(dev, "failed to request irq %d\n", priv->irq);
8000 + irq_dispose_mapping(priv->irq);
8001 + priv->irq = NO_IRQ;
8005 + priv->reg = of_iomap(np, 0);
8007 + dev_err(dev, "failed to of_iomap\n");
8012 + /* get SEC version capabilities from device tree */
8013 + prop = of_get_property(np, "fsl,num-channels", NULL);
8015 + priv->num_channels = *prop;
8017 + prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
8019 + priv->chfifo_len = *prop;
8021 + prop = of_get_property(np, "fsl,exec-units-mask", NULL);
8023 + priv->exec_units = *prop;
8025 + prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
8027 + priv->desc_types = *prop;
8029 + if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
8030 + !priv->exec_units || !priv->desc_types) {
8031 + dev_err(dev, "invalid property data in device tree node\n");
8039 + priv->head_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
8041 + priv->tail_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
8043 + if (!priv->head_lock || !priv->tail_lock) {
8044 + dev_err(dev, "failed to allocate fifo locks\n");
8049 + for (i = 0; i < priv->num_channels; i++) {
8050 + spin_lock_init(&priv->head_lock[i]);
8051 + spin_lock_init(&priv->tail_lock[i]);
8054 + priv->fifo = kmalloc(sizeof(struct talitos_request *) *
8055 + priv->num_channels, GFP_KERNEL);
8056 + if (!priv->fifo) {
8057 + dev_err(dev, "failed to allocate request fifo\n");
8062 + priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
8064 + for (i = 0; i < priv->num_channels; i++) {
8065 + priv->fifo[i] = kzalloc(sizeof(struct talitos_request) *
8066 + priv->fifo_len, GFP_KERNEL);
8067 + if (!priv->fifo[i]) {
8068 + dev_err(dev, "failed to allocate request fifo %d\n", i);
8074 + priv->head = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
8075 + priv->tail = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
8076 + if (!priv->head || !priv->tail) {
8077 + dev_err(dev, "failed to allocate request index space\n");
8082 + /* reset and initialize the h/w */
8083 + err = init_device(dev);
8085 + dev_err(dev, "failed to initialize device\n");
8089 + /* register the RNG, if available */
8090 + if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
8091 + err = talitos_register_rng(dev);
8093 + dev_err(dev, "failed to register hwrng: %d\n", err);
8096 + dev_info(dev, "hwrng\n");
8099 + /* register crypto algorithms the device supports */
8100 + INIT_LIST_HEAD(&priv->alg_list);
8102 + for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
8103 + if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
8104 + struct talitos_crypto_alg *t_alg;
8106 + t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
8107 + if (IS_ERR(t_alg)) {
8108 + err = PTR_ERR(t_alg);
8112 + err = crypto_register_alg(&t_alg->crypto_alg);
8114 + dev_err(dev, "%s alg registration failed\n",
8115 + t_alg->crypto_alg.cra_driver_name);
8118 + list_add_tail(&t_alg->entry, &priv->alg_list);
8119 + dev_info(dev, "%s\n",
8120 + t_alg->crypto_alg.cra_driver_name);
8128 + talitos_remove(ofdev);
8135 +static struct of_device_id talitos_match[] = {
8137 + .compatible = "fsl,sec2.0",
8141 +MODULE_DEVICE_TABLE(of, talitos_match);
8143 +static struct of_platform_driver talitos_driver = {
8144 + .name = "talitos",
8145 + .match_table = talitos_match,
8146 + .probe = talitos_probe,
8147 + .remove = __devexit_p(talitos_remove),
8150 +static int __init talitos_init(void)
8152 + return of_register_platform_driver(&talitos_driver);
8154 +module_init(talitos_init);
8156 +static void __exit talitos_exit(void)
8158 + of_unregister_platform_driver(&talitos_driver);
8160 +module_exit(talitos_exit);
8162 +MODULE_LICENSE("GPL");
8163 +MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
8164 +MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
8166 +++ b/drivers/crypto/talitos.h
8169 + * Freescale SEC (talitos) device register and descriptor header defines
8171 + * Copyright (c) 2006-2008 Freescale Semiconductor, Inc.
8173 + * Redistribution and use in source and binary forms, with or without
8174 + * modification, are permitted provided that the following conditions
8177 + * 1. Redistributions of source code must retain the above copyright
8178 + * notice, this list of conditions and the following disclaimer.
8179 + * 2. Redistributions in binary form must reproduce the above copyright
8180 + * notice, this list of conditions and the following disclaimer in the
8181 + * documentation and/or other materials provided with the distribution.
8182 + * 3. The name of the author may not be used to endorse or promote products
8183 + * derived from this software without specific prior written permission.
8185 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
8186 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8187 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
8188 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
8189 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
8190 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8191 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8192 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8193 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
8194 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8199 + * TALITOS_xxx_LO addresses point to the low data bits (32-63) of the register
8202 +/* global register offset addresses */
8203 +#define TALITOS_MCR 0x1030 /* master control register */
8204 +#define TALITOS_MCR_LO 0x1038
8205 +#define TALITOS_MCR_SWR 0x1 /* s/w reset */
8206 +#define TALITOS_IMR 0x1008 /* interrupt mask register */
8207 +#define TALITOS_IMR_INIT 0x10fff /* enable channel IRQs */
8208 +#define TALITOS_IMR_LO 0x100C
8209 +#define TALITOS_IMR_LO_INIT 0x20000 /* allow RNGU error IRQs */
8210 +#define TALITOS_ISR 0x1010 /* interrupt status register */
8211 +#define TALITOS_ISR_CHERR 0xaa /* channel errors mask */
8212 +#define TALITOS_ISR_CHDONE 0x55 /* channel done mask */
8213 +#define TALITOS_ISR_LO 0x1014
8214 +#define TALITOS_ICR 0x1018 /* interrupt clear register */
8215 +#define TALITOS_ICR_LO 0x101C
8217 +/* channel register address stride */
8218 +#define TALITOS_CH_STRIDE 0x100
8220 +/* channel configuration register */
8221 +#define TALITOS_CCCR(ch) (ch * TALITOS_CH_STRIDE + 0x1108)
8222 +#define TALITOS_CCCR_CONT 0x2 /* channel continue */
8223 +#define TALITOS_CCCR_RESET 0x1 /* channel reset */
8224 +#define TALITOS_CCCR_LO(ch) (ch * TALITOS_CH_STRIDE + 0x110c)
8225 +#define TALITOS_CCCR_LO_CDWE 0x10 /* chan. done writeback enab. */
8226 +#define TALITOS_CCCR_LO_NT 0x4 /* notification type */
8227 +#define TALITOS_CCCR_LO_CDIE 0x2 /* channel done IRQ enable */
8229 +/* CCPSR: channel pointer status register */
8230 +#define TALITOS_CCPSR(ch) (ch * TALITOS_CH_STRIDE + 0x1110)
8231 +#define TALITOS_CCPSR_LO(ch) (ch * TALITOS_CH_STRIDE + 0x1114)
8232 +#define TALITOS_CCPSR_LO_DOF 0x8000 /* double FF write oflow error */
8233 +#define TALITOS_CCPSR_LO_SOF 0x4000 /* single FF write oflow error */
8234 +#define TALITOS_CCPSR_LO_MDTE 0x2000 /* master data transfer error */
8235 +#define TALITOS_CCPSR_LO_SGDLZ 0x1000 /* s/g data len zero error */
8236 +#define TALITOS_CCPSR_LO_FPZ 0x0800 /* fetch ptr zero error */
8237 +#define TALITOS_CCPSR_LO_IDH 0x0400 /* illegal desc hdr error */
8238 +#define TALITOS_CCPSR_LO_IEU 0x0200 /* invalid EU error */
8239 +#define TALITOS_CCPSR_LO_EU 0x0100 /* EU error detected */
8240 +#define TALITOS_CCPSR_LO_GB 0x0080 /* gather boundary error */
8241 +#define TALITOS_CCPSR_LO_GRL 0x0040 /* gather return/length error */
8242 +#define TALITOS_CCPSR_LO_SB 0x0020 /* scatter boundary error */
8243 +#define TALITOS_CCPSR_LO_SRL 0x0010 /* scatter return/length error */
8245 +/* channel fetch fifo register */
8246 +#define TALITOS_FF(ch) (ch * TALITOS_CH_STRIDE + 0x1148)
8247 +#define TALITOS_FF_LO(ch) (ch * TALITOS_CH_STRIDE + 0x114c)
8249 +/* current descriptor pointer register */
8250 +#define TALITOS_CDPR(ch) (ch * TALITOS_CH_STRIDE + 0x1140)
8251 +#define TALITOS_CDPR_LO(ch) (ch * TALITOS_CH_STRIDE + 0x1144)
8253 +/* descriptor buffer register */
8254 +#define TALITOS_DESCBUF(ch) (ch * TALITOS_CH_STRIDE + 0x1180)
8255 +#define TALITOS_DESCBUF_LO(ch) (ch * TALITOS_CH_STRIDE + 0x1184)
8257 +/* gather link table */
8258 +#define TALITOS_GATHER(ch) (ch * TALITOS_CH_STRIDE + 0x11c0)
8259 +#define TALITOS_GATHER_LO(ch) (ch * TALITOS_CH_STRIDE + 0x11c4)
8261 +/* scatter link table */
8262 +#define TALITOS_SCATTER(ch) (ch * TALITOS_CH_STRIDE + 0x11e0)
8263 +#define TALITOS_SCATTER_LO(ch) (ch * TALITOS_CH_STRIDE + 0x11e4)
8265 +/* execution unit interrupt status registers */
8266 +#define TALITOS_DEUISR 0x2030 /* DES unit */
8267 +#define TALITOS_DEUISR_LO 0x2034
8268 +#define TALITOS_AESUISR 0x4030 /* AES unit */
8269 +#define TALITOS_AESUISR_LO 0x4034
8270 +#define TALITOS_MDEUISR 0x6030 /* message digest unit */
8271 +#define TALITOS_MDEUISR_LO 0x6034
8272 +#define TALITOS_AFEUISR 0x8030 /* arc4 unit */
8273 +#define TALITOS_AFEUISR_LO 0x8034
8274 +#define TALITOS_RNGUISR 0xa030 /* random number unit */
8275 +#define TALITOS_RNGUISR_LO 0xa034
8276 +#define TALITOS_RNGUSR 0xa028 /* rng status */
8277 +#define TALITOS_RNGUSR_LO 0xa02c
8278 +#define TALITOS_RNGUSR_LO_RD 0x1 /* reset done */
8279 +#define TALITOS_RNGUSR_LO_OFL 0xff0000/* output FIFO length */
8280 +#define TALITOS_RNGUDSR 0xa010 /* data size */
8281 +#define TALITOS_RNGUDSR_LO 0xa014
8282 +#define TALITOS_RNGU_FIFO 0xa800 /* output FIFO */
8283 +#define TALITOS_RNGU_FIFO_LO 0xa804 /* output FIFO */
8284 +#define TALITOS_RNGURCR 0xa018 /* reset control */
8285 +#define TALITOS_RNGURCR_LO 0xa01c
8286 +#define TALITOS_RNGURCR_LO_SR 0x1 /* software reset */
8287 +#define TALITOS_PKEUISR 0xc030 /* public key unit */
8288 +#define TALITOS_PKEUISR_LO 0xc034
8289 +#define TALITOS_KEUISR 0xe030 /* kasumi unit */
8290 +#define TALITOS_KEUISR_LO 0xe034
8291 +#define TALITOS_CRCUISR 0xf030 /* cyclic redundancy check unit*/
8292 +#define TALITOS_CRCUISR_LO 0xf034
8295 + * talitos descriptor header (hdr) bits
8298 +/* written back when done */
8299 +#define DESC_HDR_DONE __constant_cpu_to_be32(0xff000000)
8301 +/* primary execution unit select */
8302 +#define DESC_HDR_SEL0_MASK __constant_cpu_to_be32(0xf0000000)
8303 +#define DESC_HDR_SEL0_AFEU __constant_cpu_to_be32(0x10000000)
8304 +#define DESC_HDR_SEL0_DEU __constant_cpu_to_be32(0x20000000)
8305 +#define DESC_HDR_SEL0_MDEUA __constant_cpu_to_be32(0x30000000)
8306 +#define DESC_HDR_SEL0_MDEUB __constant_cpu_to_be32(0xb0000000)
8307 +#define DESC_HDR_SEL0_RNG __constant_cpu_to_be32(0x40000000)
8308 +#define DESC_HDR_SEL0_PKEU __constant_cpu_to_be32(0x50000000)
8309 +#define DESC_HDR_SEL0_AESU __constant_cpu_to_be32(0x60000000)
8310 +#define DESC_HDR_SEL0_KEU __constant_cpu_to_be32(0x70000000)
8311 +#define DESC_HDR_SEL0_CRCU __constant_cpu_to_be32(0x80000000)
8313 +/* primary execution unit mode (MODE0) and derivatives */
8314 +#define DESC_HDR_MODE0_ENCRYPT __constant_cpu_to_be32(0x00100000)
8315 +#define DESC_HDR_MODE0_AESU_CBC __constant_cpu_to_be32(0x00200000)
8316 +#define DESC_HDR_MODE0_DEU_CBC __constant_cpu_to_be32(0x00400000)
8317 +#define DESC_HDR_MODE0_DEU_3DES __constant_cpu_to_be32(0x00200000)
8318 +#define DESC_HDR_MODE0_MDEU_INIT __constant_cpu_to_be32(0x01000000)
8319 +#define DESC_HDR_MODE0_MDEU_HMAC __constant_cpu_to_be32(0x00800000)
8320 +#define DESC_HDR_MODE0_MDEU_PAD __constant_cpu_to_be32(0x00400000)
8321 +#define DESC_HDR_MODE0_MDEU_MD5 __constant_cpu_to_be32(0x00200000)
8322 +#define DESC_HDR_MODE0_MDEU_SHA256 __constant_cpu_to_be32(0x00100000)
8323 +#define DESC_HDR_MODE0_MDEU_SHA1 __constant_cpu_to_be32(0x00000000)
8324 +#define DESC_HDR_MODE0_MDEU_MD5_HMAC (DESC_HDR_MODE0_MDEU_MD5 | \
8325 + DESC_HDR_MODE0_MDEU_HMAC)
8326 +#define DESC_HDR_MODE0_MDEU_SHA256_HMAC (DESC_HDR_MODE0_MDEU_SHA256 | \
8327 + DESC_HDR_MODE0_MDEU_HMAC)
8328 +#define DESC_HDR_MODE0_MDEU_SHA1_HMAC (DESC_HDR_MODE0_MDEU_SHA1 | \
8329 + DESC_HDR_MODE0_MDEU_HMAC)
8331 +/* secondary execution unit select (SEL1) */
8332 +#define DESC_HDR_SEL1_MASK __constant_cpu_to_be32(0x000f0000)
8333 +#define DESC_HDR_SEL1_MDEUA __constant_cpu_to_be32(0x00030000)
8334 +#define DESC_HDR_SEL1_MDEUB __constant_cpu_to_be32(0x000b0000)
8335 +#define DESC_HDR_SEL1_CRCU __constant_cpu_to_be32(0x00080000)
8337 +/* secondary execution unit mode (MODE1) and derivatives */
8338 +#define DESC_HDR_MODE1_MDEU_INIT __constant_cpu_to_be32(0x00001000)
8339 +#define DESC_HDR_MODE1_MDEU_HMAC __constant_cpu_to_be32(0x00000800)
8340 +#define DESC_HDR_MODE1_MDEU_PAD __constant_cpu_to_be32(0x00000400)
8341 +#define DESC_HDR_MODE1_MDEU_MD5 __constant_cpu_to_be32(0x00000200)
8342 +#define DESC_HDR_MODE1_MDEU_SHA256 __constant_cpu_to_be32(0x00000100)
8343 +#define DESC_HDR_MODE1_MDEU_SHA1 __constant_cpu_to_be32(0x00000000)
8344 +#define DESC_HDR_MODE1_MDEU_MD5_HMAC (DESC_HDR_MODE1_MDEU_MD5 | \
8345 + DESC_HDR_MODE1_MDEU_HMAC)
8346 +#define DESC_HDR_MODE1_MDEU_SHA256_HMAC (DESC_HDR_MODE1_MDEU_SHA256 | \
8347 + DESC_HDR_MODE1_MDEU_HMAC)
8348 +#define DESC_HDR_MODE1_MDEU_SHA1_HMAC (DESC_HDR_MODE1_MDEU_SHA1 | \
8349 + DESC_HDR_MODE1_MDEU_HMAC)
8351 +/* direction of overall data flow (DIR) */
8352 +#define DESC_HDR_DIR_INBOUND __constant_cpu_to_be32(0x00000002)
8354 +/* request done notification (DN) */
8355 +#define DESC_HDR_DONE_NOTIFY __constant_cpu_to_be32(0x00000001)
8357 +/* descriptor types */
8358 +#define DESC_HDR_TYPE_AESU_CTR_NONSNOOP __constant_cpu_to_be32(0 << 3)
8359 +#define DESC_HDR_TYPE_IPSEC_ESP __constant_cpu_to_be32(1 << 3)
8360 +#define DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU __constant_cpu_to_be32(2 << 3)
8361 +#define DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU __constant_cpu_to_be32(4 << 3)
8363 +/* link table extent field bits */
8364 +#define DESC_PTR_LNKTBL_JUMP 0x80
8365 +#define DESC_PTR_LNKTBL_RETURN 0x02
8366 +#define DESC_PTR_LNKTBL_NEXT 0x01
8368 +++ b/include/crypto/hash.h
8371 + * Hash: Hash algorithms under the crypto API
8373 + * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8375 + * This program is free software; you can redistribute it and/or modify it
8376 + * under the terms of the GNU General Public License as published by the Free
8377 + * Software Foundation; either version 2 of the License, or (at your option)
8378 + * any later version.
8382 +#ifndef _CRYPTO_HASH_H
8383 +#define _CRYPTO_HASH_H
8385 +#include <linux/crypto.h>
8387 +struct crypto_ahash {
8388 + struct crypto_tfm base;
8391 +static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
8393 + return (struct crypto_ahash *)tfm;
8396 +static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
8397 + u32 type, u32 mask)
8399 + type &= ~CRYPTO_ALG_TYPE_MASK;
8400 + mask &= ~CRYPTO_ALG_TYPE_MASK;
8401 + type |= CRYPTO_ALG_TYPE_AHASH;
8402 + mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
8404 + return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
8407 +static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
8409 + return &tfm->base;
8412 +static inline void crypto_free_ahash(struct crypto_ahash *tfm)
8414 + crypto_free_tfm(crypto_ahash_tfm(tfm));
8417 +static inline unsigned int crypto_ahash_alignmask(
8418 + struct crypto_ahash *tfm)
8420 + return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
8423 +static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
8425 + return &crypto_ahash_tfm(tfm)->crt_ahash;
8428 +static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
8430 + return crypto_ahash_crt(tfm)->digestsize;
8433 +static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
8435 + return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
8438 +static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
8440 + crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
8443 +static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
8445 + crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
8448 +static inline struct crypto_ahash *crypto_ahash_reqtfm(
8449 + struct ahash_request *req)
8451 + return __crypto_ahash_cast(req->base.tfm);
8454 +static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
8456 + return crypto_ahash_crt(tfm)->reqsize;
8459 +static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
8460 + const u8 *key, unsigned int keylen)
8462 + struct ahash_tfm *crt = crypto_ahash_crt(tfm);
8464 + return crt->setkey(tfm, key, keylen);
8467 +static inline int crypto_ahash_digest(struct ahash_request *req)
8469 + struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
8470 + return crt->digest(req);
8473 +static inline void ahash_request_set_tfm(struct ahash_request *req,
8474 + struct crypto_ahash *tfm)
8476 + req->base.tfm = crypto_ahash_tfm(tfm);
8479 +static inline struct ahash_request *ahash_request_alloc(
8480 + struct crypto_ahash *tfm, gfp_t gfp)
8482 + struct ahash_request *req;
8484 + req = kmalloc(sizeof(struct ahash_request) +
8485 + crypto_ahash_reqsize(tfm), gfp);
8488 + ahash_request_set_tfm(req, tfm);
8493 +static inline void ahash_request_free(struct ahash_request *req)
8498 +static inline struct ahash_request *ahash_request_cast(
8499 + struct crypto_async_request *req)
8501 + return container_of(req, struct ahash_request, base);
8504 +static inline void ahash_request_set_callback(struct ahash_request *req,
8506 + crypto_completion_t complete,
8509 + req->base.complete = complete;
8510 + req->base.data = data;
8511 + req->base.flags = flags;
8514 +static inline void ahash_request_set_crypt(struct ahash_request *req,
8515 + struct scatterlist *src, u8 *result,
8516 + unsigned int nbytes)
8519 + req->nbytes = nbytes;
8520 + req->result = result;
8523 +#endif /* _CRYPTO_HASH_H */
8525 +++ b/include/crypto/internal/hash.h
8528 + * Hash algorithms.
8530 + * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8532 + * This program is free software; you can redistribute it and/or modify it
8533 + * under the terms of the GNU General Public License as published by the Free
8534 + * Software Foundation; either version 2 of the License, or (at your option)
8535 + * any later version.
8539 +#ifndef _CRYPTO_INTERNAL_HASH_H
8540 +#define _CRYPTO_INTERNAL_HASH_H
8542 +#include <crypto/algapi.h>
8543 +#include <crypto/hash.h>
8545 +struct ahash_request;
8546 +struct scatterlist;
8548 +struct crypto_hash_walk {
8551 + unsigned int offset;
8552 + unsigned int alignmask;
8555 + unsigned int entrylen;
8557 + unsigned int total;
8558 + struct scatterlist *sg;
8560 + unsigned int flags;
8563 +extern const struct crypto_type crypto_ahash_type;
8565 +int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err);
8566 +int crypto_hash_walk_first(struct ahash_request *req,
8567 + struct crypto_hash_walk *walk);
8569 +static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
8571 + return crypto_tfm_ctx(&tfm->base);
8574 +static inline struct ahash_alg *crypto_ahash_alg(
8575 + struct crypto_ahash *tfm)
8577 + return &crypto_ahash_tfm(tfm)->__crt_alg->cra_ahash;
8580 +static inline int ahash_enqueue_request(struct crypto_queue *queue,
8581 + struct ahash_request *request)
8583 + return crypto_enqueue_request(queue, &request->base);
8586 +static inline struct ahash_request *ahash_dequeue_request(
8587 + struct crypto_queue *queue)
8589 + return ahash_request_cast(crypto_dequeue_request(queue));
8592 +static inline void *ahash_request_ctx(struct ahash_request *req)
8594 + return req->__ctx;
8597 +static inline int ahash_tfm_in_queue(struct crypto_queue *queue,
8598 + struct crypto_ahash *tfm)
8600 + return crypto_tfm_in_queue(queue, crypto_ahash_tfm(tfm));
8603 +#endif /* _CRYPTO_INTERNAL_HASH_H */
8605 --- a/include/linux/crypto.h
8606 +++ b/include/linux/crypto.h
8609 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
8610 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
8611 -#define CRYPTO_ALG_TYPE_DIGEST 0x00000002
8612 -#define CRYPTO_ALG_TYPE_HASH 0x00000003
8613 +#define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
8614 +#define CRYPTO_ALG_TYPE_AEAD 0x00000003
8615 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
8616 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
8617 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
8618 -#define CRYPTO_ALG_TYPE_COMPRESS 0x00000008
8619 -#define CRYPTO_ALG_TYPE_AEAD 0x00000009
8620 +#define CRYPTO_ALG_TYPE_DIGEST 0x00000008
8621 +#define CRYPTO_ALG_TYPE_HASH 0x00000009
8622 +#define CRYPTO_ALG_TYPE_AHASH 0x0000000a
8624 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
8625 +#define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
8626 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
8628 #define CRYPTO_ALG_LARVAL 0x00000010
8631 struct crypto_blkcipher;
8633 +struct crypto_ahash;
8636 struct aead_givcrypt_request;
8637 @@ -131,6 +134,16 @@
8638 void *__ctx[] CRYPTO_MINALIGN_ATTR;
8641 +struct ahash_request {
8642 + struct crypto_async_request base;
8644 + unsigned int nbytes;
8645 + struct scatterlist *src;
8648 + void *__ctx[] CRYPTO_MINALIGN_ATTR;
8652 * struct aead_request - AEAD request
8653 * @base: Common attributes for async crypto requests
8654 @@ -195,6 +208,17 @@
8655 unsigned int ivsize;
8659 + int (*init)(struct ahash_request *req);
8660 + int (*update)(struct ahash_request *req);
8661 + int (*final)(struct ahash_request *req);
8662 + int (*digest)(struct ahash_request *req);
8663 + int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
8664 + unsigned int keylen);
8666 + unsigned int digestsize;
8670 int (*setkey)(struct crypto_aead *tfm, const u8 *key,
8671 unsigned int keylen);
8673 #define cra_cipher cra_u.cipher
8674 #define cra_digest cra_u.digest
8675 #define cra_hash cra_u.hash
8676 +#define cra_ahash cra_u.ahash
8677 #define cra_compress cra_u.compress
8681 struct cipher_alg cipher;
8682 struct digest_alg digest;
8683 struct hash_alg hash;
8684 + struct ahash_alg ahash;
8685 struct compress_alg compress;
8688 @@ -383,6 +409,18 @@
8689 unsigned int digestsize;
8693 + int (*init)(struct ahash_request *req);
8694 + int (*update)(struct ahash_request *req);
8695 + int (*final)(struct ahash_request *req);
8696 + int (*digest)(struct ahash_request *req);
8697 + int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
8698 + unsigned int keylen);
8700 + unsigned int digestsize;
8701 + unsigned int reqsize;
8704 struct compress_tfm {
8705 int (*cot_compress)(struct crypto_tfm *tfm,
8706 const u8 *src, unsigned int slen,
8708 #define crt_blkcipher crt_u.blkcipher
8709 #define crt_cipher crt_u.cipher
8710 #define crt_hash crt_u.hash
8711 +#define crt_ahash crt_u.ahash
8712 #define crt_compress crt_u.compress
8716 struct blkcipher_tfm blkcipher;
8717 struct cipher_tfm cipher;
8718 struct hash_tfm hash;
8719 + struct ahash_tfm ahash;
8720 struct compress_tfm compress;