[IPSEC/CRYPTO]: Allocate work buffers instead of using kstack.
[linux-flexiantxendom0-3.2.10.git] / crypto / hmac.c
1 /*
2  * Cryptographic API.
3  *
4  * HMAC: Keyed-Hashing for Message Authentication (RFC2104).
5  *
6  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
7  *
8  * The HMAC implementation is derived from USAGI.
9  * Copyright (c) 2002 Kazunori Miyazawa <miyazawa@linux-ipv6.org> / USAGI
10  *
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.
15  *
16  */
17 #include <linux/crypto.h>
18 #include <linux/mm.h>
19 #include <linux/highmem.h>
20 #include <asm/scatterlist.h>
21 #include "internal.h"
22
23 static void hash_key(struct crypto_tfm *tfm, u8 *key, unsigned int keylen)
24 {
25         struct scatterlist tmp;
26         
27         tmp.page = virt_to_page(key);
28         tmp.offset = ((long)key & ~PAGE_MASK);
29         tmp.length = keylen;
30         crypto_digest_digest(tfm, &tmp, 1, key);
31                 
32 }
33
34 void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen)
35 {
36         unsigned int i;
37         struct scatterlist tmp;
38         char *ipad = tfm->crt_work_block;
39
40         if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
41                 hash_key(tfm, key, *keylen);
42                 *keylen = crypto_tfm_alg_digestsize(tfm);
43         }
44
45         memset(ipad, 0, crypto_tfm_alg_blocksize(tfm) + 1);
46         memcpy(ipad, key, *keylen);
47
48         for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
49                 ipad[i] ^= 0x36;
50
51         tmp.page = virt_to_page(ipad);
52         tmp.offset = ((long)ipad & ~PAGE_MASK);
53         tmp.length = crypto_tfm_alg_blocksize(tfm);
54         
55         crypto_digest_init(tfm);
56         crypto_digest_update(tfm, &tmp, 1);
57 }
58
59 void crypto_hmac_update(struct crypto_tfm *tfm,
60                         struct scatterlist *sg, unsigned int nsg)
61 {
62         crypto_digest_update(tfm, sg, nsg);
63 }
64
65 void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
66                        unsigned int *keylen, u8 *out)
67 {
68         unsigned int i;
69         struct scatterlist tmp;
70         char *opad = tfm->crt_work_block;
71
72         if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
73                 hash_key(tfm, key, *keylen);
74                 *keylen = crypto_tfm_alg_digestsize(tfm);
75         }
76
77         crypto_digest_final(tfm, out);
78
79         memset(opad, 0, crypto_tfm_alg_blocksize(tfm) + 1);
80         memcpy(opad, key, *keylen);
81                 
82         for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
83                 opad[i] ^= 0x5c;
84
85         tmp.page = virt_to_page(opad);
86         tmp.offset = ((long)opad & ~PAGE_MASK);
87         tmp.length = crypto_tfm_alg_blocksize(tfm);
88
89         crypto_digest_init(tfm);
90         crypto_digest_update(tfm, &tmp, 1);
91         
92         tmp.page = virt_to_page(out);
93         tmp.offset = ((long)out & ~PAGE_MASK);
94         tmp.length = crypto_tfm_alg_digestsize(tfm);
95         
96         crypto_digest_update(tfm, &tmp, 1);
97         crypto_digest_final(tfm, out);
98 }
99
100 void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
101                  struct scatterlist *sg, unsigned int nsg, u8 *out)
102 {
103         crypto_hmac_init(tfm, key, keylen);
104         crypto_hmac_update(tfm, sg, nsg);
105         crypto_hmac_final(tfm, key, keylen, out);
106 }
107
108 EXPORT_SYMBOL_GPL(crypto_hmac_init);
109 EXPORT_SYMBOL_GPL(crypto_hmac_update);
110 EXPORT_SYMBOL_GPL(crypto_hmac_final);
111 EXPORT_SYMBOL_GPL(crypto_hmac);
112