aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--crypto/Kconfig2
-rw-r--r--crypto/digest.c3
-rw-r--r--crypto/hmac.c101
-rw-r--r--crypto/internal.h13
-rw-r--r--include/linux/crypto.h16
5 files changed, 2 insertions, 133 deletions
diff --git a/crypto/Kconfig b/crypto/Kconfig
index f07d9237950f..1e2f39c21180 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -33,7 +33,7 @@ config CRYPTO_MANAGER
33 cbc(aes). 33 cbc(aes).
34 34
35config CRYPTO_HMAC 35config CRYPTO_HMAC
36 bool "HMAC support" 36 tristate "HMAC support"
37 select CRYPTO_HASH 37 select CRYPTO_HASH
38 help 38 help
39 HMAC: Keyed-Hashing for Message Authentication (RFC2104). 39 HMAC: Keyed-Hashing for Message Authentication (RFC2104).
diff --git a/crypto/digest.c b/crypto/digest.c
index 5873063db840..0155a94e4b15 100644
--- a/crypto/digest.c
+++ b/crypto/digest.c
@@ -191,10 +191,9 @@ int crypto_init_digest_ops(struct crypto_tfm *tfm)
191 ops->setkey = dalg->dia_setkey ? setkey : nosetkey; 191 ops->setkey = dalg->dia_setkey ? setkey : nosetkey;
192 ops->digestsize = dalg->dia_digestsize; 192 ops->digestsize = dalg->dia_digestsize;
193 193
194 return crypto_alloc_hmac_block(tfm); 194 return 0;
195} 195}
196 196
197void crypto_exit_digest_ops(struct crypto_tfm *tfm) 197void crypto_exit_digest_ops(struct crypto_tfm *tfm)
198{ 198{
199 crypto_free_hmac_block(tfm);
200} 199}
diff --git a/crypto/hmac.c b/crypto/hmac.c
index eac77e294740..f403b6946047 100644
--- a/crypto/hmac.c
+++ b/crypto/hmac.c
@@ -29,107 +29,6 @@ struct hmac_ctx {
29 struct crypto_hash *child; 29 struct crypto_hash *child;
30}; 30};
31 31
32static void hash_key(struct crypto_tfm *tfm, u8 *key, unsigned int keylen)
33{
34 struct scatterlist tmp;
35
36 sg_set_buf(&tmp, key, keylen);
37 crypto_digest_digest(tfm, &tmp, 1, key);
38}
39
40int crypto_alloc_hmac_block(struct crypto_tfm *tfm)
41{
42 int ret = 0;
43
44 BUG_ON(!crypto_tfm_alg_blocksize(tfm));
45
46 tfm->crt_hash.hmac_block = kmalloc(crypto_tfm_alg_blocksize(tfm),
47 GFP_KERNEL);
48 if (tfm->crt_hash.hmac_block == NULL)
49 ret = -ENOMEM;
50
51 return ret;
52
53}
54
55void crypto_free_hmac_block(struct crypto_tfm *tfm)
56{
57 kfree(tfm->crt_hash.hmac_block);
58}
59
60void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen)
61{
62 unsigned int i;
63 struct scatterlist tmp;
64 char *ipad = tfm->crt_hash.hmac_block;
65
66 if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
67 hash_key(tfm, key, *keylen);
68 *keylen = crypto_tfm_alg_digestsize(tfm);
69 }
70
71 memset(ipad, 0, crypto_tfm_alg_blocksize(tfm));
72 memcpy(ipad, key, *keylen);
73
74 for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
75 ipad[i] ^= 0x36;
76
77 sg_set_buf(&tmp, ipad, crypto_tfm_alg_blocksize(tfm));
78
79 crypto_digest_init(tfm);
80 crypto_digest_update(tfm, &tmp, 1);
81}
82
83void crypto_hmac_update(struct crypto_tfm *tfm,
84 struct scatterlist *sg, unsigned int nsg)
85{
86 crypto_digest_update(tfm, sg, nsg);
87}
88
89void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
90 unsigned int *keylen, u8 *out)
91{
92 unsigned int i;
93 struct scatterlist tmp;
94 char *opad = tfm->crt_hash.hmac_block;
95
96 if (*keylen > crypto_tfm_alg_blocksize(tfm)) {
97 hash_key(tfm, key, *keylen);
98 *keylen = crypto_tfm_alg_digestsize(tfm);
99 }
100
101 crypto_digest_final(tfm, out);
102
103 memset(opad, 0, crypto_tfm_alg_blocksize(tfm));
104 memcpy(opad, key, *keylen);
105
106 for (i = 0; i < crypto_tfm_alg_blocksize(tfm); i++)
107 opad[i] ^= 0x5c;
108
109 sg_set_buf(&tmp, opad, crypto_tfm_alg_blocksize(tfm));
110
111 crypto_digest_init(tfm);
112 crypto_digest_update(tfm, &tmp, 1);
113
114 sg_set_buf(&tmp, out, crypto_tfm_alg_digestsize(tfm));
115
116 crypto_digest_update(tfm, &tmp, 1);
117 crypto_digest_final(tfm, out);
118}
119
120void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
121 struct scatterlist *sg, unsigned int nsg, u8 *out)
122{
123 crypto_hmac_init(tfm, key, keylen);
124 crypto_hmac_update(tfm, sg, nsg);
125 crypto_hmac_final(tfm, key, keylen, out);
126}
127
128EXPORT_SYMBOL_GPL(crypto_hmac_init);
129EXPORT_SYMBOL_GPL(crypto_hmac_update);
130EXPORT_SYMBOL_GPL(crypto_hmac_final);
131EXPORT_SYMBOL_GPL(crypto_hmac);
132
133static inline void *align_ptr(void *p, unsigned int align) 32static inline void *align_ptr(void *p, unsigned int align)
134{ 33{
135 return (void *)ALIGN((unsigned long)p, align); 34 return (void *)ALIGN((unsigned long)p, align);
diff --git a/crypto/internal.h b/crypto/internal.h
index 93d9b10ff914..2da6ad4f3593 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -73,19 +73,6 @@ static inline void crypto_yield(u32 flags)
73 cond_resched(); 73 cond_resched();
74} 74}
75 75
76#ifdef CONFIG_CRYPTO_HMAC
77int crypto_alloc_hmac_block(struct crypto_tfm *tfm);
78void crypto_free_hmac_block(struct crypto_tfm *tfm);
79#else
80static inline int crypto_alloc_hmac_block(struct crypto_tfm *tfm)
81{
82 return 0;
83}
84
85static inline void crypto_free_hmac_block(struct crypto_tfm *tfm)
86{ }
87#endif
88
89#ifdef CONFIG_PROC_FS 76#ifdef CONFIG_PROC_FS
90void __init crypto_init_proc(void); 77void __init crypto_init_proc(void);
91void __exit crypto_exit_proc(void); 78void __exit crypto_exit_proc(void);
diff --git a/include/linux/crypto.h b/include/linux/crypto.h
index 40c0aab8ad4c..929fb9ad1314 100644
--- a/include/linux/crypto.h
+++ b/include/linux/crypto.h
@@ -295,9 +295,6 @@ struct hash_tfm {
295 unsigned int nsg, u8 *out); 295 unsigned int nsg, u8 *out);
296 int (*setkey)(struct crypto_hash *tfm, const u8 *key, 296 int (*setkey)(struct crypto_hash *tfm, const u8 *key,
297 unsigned int keylen); 297 unsigned int keylen);
298#ifdef CONFIG_CRYPTO_HMAC
299 void *hmac_block;
300#endif
301 unsigned int digestsize; 298 unsigned int digestsize;
302}; 299};
303 300
@@ -872,18 +869,5 @@ static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
872 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen); 869 return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
873} 870}
874 871
875/*
876 * HMAC support.
877 */
878#ifdef CONFIG_CRYPTO_HMAC
879void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
880void crypto_hmac_update(struct crypto_tfm *tfm,
881 struct scatterlist *sg, unsigned int nsg);
882void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
883 unsigned int *keylen, u8 *out);
884void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
885 struct scatterlist *sg, unsigned int nsg, u8 *out);
886#endif /* CONFIG_CRYPTO_HMAC */
887
888#endif /* _LINUX_CRYPTO_H */ 872#endif /* _LINUX_CRYPTO_H */
889 873