Skip to content

Commit dec8b78

Browse files
committed
crypto: hash - Add import/export interface
It is often useful to save the partial state of a hash function so that it can be used as a base for two or more computations. The most prominent example is HMAC where all hashes start from a base determined by the key. Having an import/export interface means that we only have to compute that base once rather than for each message. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3b2f6df commit dec8b78

5 files changed

Lines changed: 50 additions & 5 deletions

File tree

‎crypto/ahash.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
146146
return ahash->setkey(tfm, key, keylen);
147147
}
148148

149+
int crypto_ahash_import(struct ahash_request *req, const u8 *in)
150+
{
151+
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
152+
struct ahash_alg *alg = crypto_ahash_alg(tfm);
153+
154+
memcpy(ahash_request_ctx(req), in, crypto_ahash_reqsize(tfm));
155+
156+
if (alg->reinit)
157+
alg->reinit(req);
158+
159+
return 0;
160+
}
161+
EXPORT_SYMBOL_GPL(crypto_ahash_import);
162+
149163
static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
150164
u32 mask)
151165
{

‎crypto/shash.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
172172
}
173173
EXPORT_SYMBOL_GPL(crypto_shash_digest);
174174

175+
int crypto_shash_import(struct shash_desc *desc, const u8 *in)
176+
{
177+
struct crypto_shash *tfm = desc->tfm;
178+
struct shash_alg *alg = crypto_shash_alg(tfm);
179+
180+
memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm));
181+
182+
if (alg->reinit)
183+
alg->reinit(desc);
184+
185+
return 0;
186+
}
187+
EXPORT_SYMBOL_GPL(crypto_shash_import);
188+
175189
static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
176190
unsigned int keylen)
177191
{

‎include/crypto/hash.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct shash_desc {
2424

2525
struct shash_alg {
2626
int (*init)(struct shash_desc *desc);
27+
int (*reinit)(struct shash_desc *desc);
2728
int (*update)(struct shash_desc *desc, const u8 *data,
2829
unsigned int len);
2930
int (*final)(struct shash_desc *desc, u8 *out);
@@ -116,6 +117,11 @@ static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
116117
return crypto_ahash_crt(tfm)->reqsize;
117118
}
118119

120+
static inline void *ahash_request_ctx(struct ahash_request *req)
121+
{
122+
return req->__ctx;
123+
}
124+
119125
static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
120126
const u8 *key, unsigned int keylen)
121127
{
@@ -130,6 +136,14 @@ static inline int crypto_ahash_digest(struct ahash_request *req)
130136
return crt->digest(req);
131137
}
132138

139+
static inline void crypto_ahash_export(struct ahash_request *req, u8 *out)
140+
{
141+
memcpy(out, ahash_request_ctx(req),
142+
crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
143+
}
144+
145+
int crypto_ahash_import(struct ahash_request *req, const u8 *in);
146+
133147
static inline int crypto_ahash_init(struct ahash_request *req)
134148
{
135149
struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
@@ -262,6 +276,13 @@ int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
262276
int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
263277
unsigned int len, u8 *out);
264278

279+
static inline void crypto_shash_export(struct shash_desc *desc, u8 *out)
280+
{
281+
memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
282+
}
283+
284+
int crypto_shash_import(struct shash_desc *desc, const u8 *in);
285+
265286
static inline int crypto_shash_init(struct shash_desc *desc)
266287
{
267288
return crypto_shash_alg(desc->tfm)->init(desc);

‎include/crypto/internal/hash.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ static inline struct ahash_request *ahash_dequeue_request(
6666
return ahash_request_cast(crypto_dequeue_request(queue));
6767
}
6868

69-
static inline void *ahash_request_ctx(struct ahash_request *req)
70-
{
71-
return req->__ctx;
72-
}
73-
7469
static inline int ahash_tfm_in_queue(struct crypto_queue *queue,
7570
struct crypto_ahash *tfm)
7671
{

‎include/linux/crypto.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ struct ablkcipher_alg {
221221

222222
struct ahash_alg {
223223
int (*init)(struct ahash_request *req);
224+
int (*reinit)(struct ahash_request *req);
224225
int (*update)(struct ahash_request *req);
225226
int (*final)(struct ahash_request *req);
226227
int (*digest)(struct ahash_request *req);

0 commit comments

Comments
 (0)