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)
17 #include <linux/crypto.h>
19 #include <linux/highmem.h>
20 #include <linux/slab.h>
21 #include <asm/scatterlist.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
);
31 crypto_digest_digest(tfm
, &tmp
, 1, key
);
35 int crypto_alloc_hmac_block(struct crypto_tfm
*tfm
)
39 BUG_ON(!crypto_tfm_alg_blocksize(tfm
));
41 tfm
->crt_digest
.dit_hmac_block
= kmalloc(crypto_tfm_alg_blocksize(tfm
),
43 if (tfm
->crt_digest
.dit_hmac_block
== NULL
)
50 void crypto_free_hmac_block(struct crypto_tfm
*tfm
)
52 if (tfm
->crt_digest
.dit_hmac_block
)
53 kfree(tfm
->crt_digest
.dit_hmac_block
);
56 void crypto_hmac_init(struct crypto_tfm
*tfm
, u8
*key
, unsigned int *keylen
)
59 struct scatterlist tmp
;
60 char *ipad
= tfm
->crt_digest
.dit_hmac_block
;
62 if (*keylen
> crypto_tfm_alg_blocksize(tfm
)) {
63 hash_key(tfm
, key
, *keylen
);
64 *keylen
= crypto_tfm_alg_digestsize(tfm
);
67 memset(ipad
, 0, crypto_tfm_alg_blocksize(tfm
));
68 memcpy(ipad
, key
, *keylen
);
70 for (i
= 0; i
< crypto_tfm_alg_blocksize(tfm
); i
++)
73 tmp
.page
= virt_to_page(ipad
);
74 tmp
.offset
= offset_in_page(ipad
);
75 tmp
.length
= crypto_tfm_alg_blocksize(tfm
);
77 crypto_digest_init(tfm
);
78 crypto_digest_update(tfm
, &tmp
, 1);
81 void crypto_hmac_update(struct crypto_tfm
*tfm
,
82 struct scatterlist
*sg
, unsigned int nsg
)
84 crypto_digest_update(tfm
, sg
, nsg
);
87 void crypto_hmac_final(struct crypto_tfm
*tfm
, u8
*key
,
88 unsigned int *keylen
, u8
*out
)
91 struct scatterlist tmp
;
92 char *opad
= tfm
->crt_digest
.dit_hmac_block
;
94 if (*keylen
> crypto_tfm_alg_blocksize(tfm
)) {
95 hash_key(tfm
, key
, *keylen
);
96 *keylen
= crypto_tfm_alg_digestsize(tfm
);
99 crypto_digest_final(tfm
, out
);
101 memset(opad
, 0, crypto_tfm_alg_blocksize(tfm
));
102 memcpy(opad
, key
, *keylen
);
104 for (i
= 0; i
< crypto_tfm_alg_blocksize(tfm
); i
++)
107 tmp
.page
= virt_to_page(opad
);
108 tmp
.offset
= offset_in_page(opad
);
109 tmp
.length
= crypto_tfm_alg_blocksize(tfm
);
111 crypto_digest_init(tfm
);
112 crypto_digest_update(tfm
, &tmp
, 1);
114 tmp
.page
= virt_to_page(out
);
115 tmp
.offset
= offset_in_page(out
);
116 tmp
.length
= crypto_tfm_alg_digestsize(tfm
);
118 crypto_digest_update(tfm
, &tmp
, 1);
119 crypto_digest_final(tfm
, out
);
122 void crypto_hmac(struct crypto_tfm
*tfm
, u8
*key
, unsigned int *keylen
,
123 struct scatterlist
*sg
, unsigned int nsg
, u8
*out
)
125 crypto_hmac_init(tfm
, key
, keylen
);
126 crypto_hmac_update(tfm
, sg
, nsg
);
127 crypto_hmac_final(tfm
, key
, keylen
, out
);
130 EXPORT_SYMBOL_GPL(crypto_hmac_init
);
131 EXPORT_SYMBOL_GPL(crypto_hmac_update
);
132 EXPORT_SYMBOL_GPL(crypto_hmac_final
);
133 EXPORT_SYMBOL_GPL(crypto_hmac
);