[PATCH] ARM: 2809/1: OMAP update 7b/11: Move arch-omap to plat-omap
[linux-2.6/mini2440.git] / crypto / hmac.c
blobda0456b37109513b472f29241a26addd69bdedbb
1 /*
2 * Cryptographic API.
4 * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
6 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
8 * The HMAC implementation is derived from USAGI.
9 * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
17 #include <linux/crypto.h>
18 #include <linux/mm.h>
19 #include <linux/highmem.h>
20 #include <linux/slab.h>
21 #include <asm/scatterlist.h>
22 #include "internal.h"
24 static void hash_key(struct crypto_tfm *tfm, u8 *key, unsigned int keylen)
26 struct scatterlist tmp;
28 tmp.page = virt_to_page(key);
29 tmp.offset = offset_in_page(key);
30 tmp.length = keylen;
31 crypto_digest_digest(tfm, &tmp, 1, key);
35 int crypto_alloc_hmac_block(struct crypto_tfm *tfm)
37 int ret = 0;
39 BUG_ON(!crypto_tfm_alg_blocksize(tfm));
41 tfm->crt_digest.dit_hmac_block = kmalloc(crypto_tfm_alg_blocksize(tfm),
42 GFP_KERNEL);
43 if (tfm->crt_digest.dit_hmac_block == NULL)
44 ret = -ENOMEM;
46 return ret;
50 void crypto_free_hmac_block(struct crypto_tfm *tfm)
52 kfree(tfm->crt_digest.dit_hmac_block);
55 void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen)
57 unsigned int i;
58 struct scatterlist tmp;
59 char *ipad = tfm->crt_digest.dit_hmac_block;
61 if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
62 hash_key(tfm, key, *keylen);
63 *keylen = crypto_tfm_alg_digestsize(tfm);
66 memset(ipad, 0, crypto_tfm_alg_blocksize(tfm));
67 memcpy(ipad, key, *keylen);
69 for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
70 ipad[i] ^= 0x36;
72 tmp.page = virt_to_page(ipad);
73 tmp.offset = offset_in_page(ipad);
74 tmp.length = crypto_tfm_alg_blocksize(tfm);
76 crypto_digest_init(tfm);
77 crypto_digest_update(tfm, &tmp, 1);
80 void crypto_hmac_update(struct crypto_tfm *tfm,
81 struct scatterlist *sg, unsigned int nsg)
83 crypto_digest_update(tfm, sg, nsg);
86 void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
87 unsigned int *keylen, u8 *out)
89 unsigned int i;
90 struct scatterlist tmp;
91 char *opad = tfm->crt_digest.dit_hmac_block;
93 if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
94 hash_key(tfm, key, *keylen);
95 *keylen = crypto_tfm_alg_digestsize(tfm);
98 crypto_digest_final(tfm, out);
100 memset(opad, 0, crypto_tfm_alg_blocksize(tfm));
101 memcpy(opad, key, *keylen);
103 for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
104 opad[i] ^= 0x5c;
106 tmp.page = virt_to_page(opad);
107 tmp.offset = offset_in_page(opad);
108 tmp.length = crypto_tfm_alg_blocksize(tfm);
110 crypto_digest_init(tfm);
111 crypto_digest_update(tfm, &tmp, 1);
113 tmp.page = virt_to_page(out);
114 tmp.offset = offset_in_page(out);
115 tmp.length = crypto_tfm_alg_digestsize(tfm);
117 crypto_digest_update(tfm, &tmp, 1);
118 crypto_digest_final(tfm, out);
121 void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
122 struct scatterlist *sg, unsigned int nsg, u8 *out)
124 crypto_hmac_init(tfm, key, keylen);
125 crypto_hmac_update(tfm, sg, nsg);
126 crypto_hmac_final(tfm, key, keylen, out);
129 EXPORT_SYMBOL_GPL(crypto_hmac_init);
130 EXPORT_SYMBOL_GPL(crypto_hmac_update);
131 EXPORT_SYMBOL_GPL(crypto_hmac_final);
132 EXPORT_SYMBOL_GPL(crypto_hmac);