Skip to content

Commit c6a12f3

Browse files
committed
crypto: hash - Add export_core and import_core hooks
Add export_core and import_core hooks. These are intended to be used by algorithms which are wrappers around block-only algorithms, but are not themselves block-only, e.g., hmac. Signed-off-by: Herbert Xu <[email protected]>
1 parent cd5a4d5 commit c6a12f3

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

crypto/ahash.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ int crypto_ahash_export_core(struct ahash_request *req, void *out)
704704

705705
if (likely(tfm->using_shash))
706706
return crypto_shash_export_core(ahash_request_ctx(req), out);
707-
return crypto_ahash_alg(tfm)->export(req, out);
707+
return crypto_ahash_alg(tfm)->export_core(req, out);
708708
}
709709
EXPORT_SYMBOL_GPL(crypto_ahash_export_core);
710710

@@ -727,7 +727,7 @@ int crypto_ahash_import_core(struct ahash_request *req, const void *in)
727727
in);
728728
if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
729729
return -ENOKEY;
730-
return crypto_ahash_alg(tfm)->import(req, in);
730+
return crypto_ahash_alg(tfm)->import_core(req, in);
731731
}
732732
EXPORT_SYMBOL_GPL(crypto_ahash_import_core);
733733

@@ -739,7 +739,7 @@ int crypto_ahash_import(struct ahash_request *req, const void *in)
739739
return crypto_shash_import(prepare_shash_desc(req, tfm), in);
740740
if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
741741
return -ENOKEY;
742-
return crypto_ahash_import_core(req, in);
742+
return crypto_ahash_alg(tfm)->import(req, in);
743743
}
744744
EXPORT_SYMBOL_GPL(crypto_ahash_import);
745745

@@ -971,6 +971,16 @@ struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *hash)
971971
}
972972
EXPORT_SYMBOL_GPL(crypto_clone_ahash);
973973

974+
static int ahash_default_export_core(struct ahash_request *req, void *out)
975+
{
976+
return -ENOSYS;
977+
}
978+
979+
static int ahash_default_import_core(struct ahash_request *req, const void *in)
980+
{
981+
return -ENOSYS;
982+
}
983+
974984
static int ahash_prepare_alg(struct ahash_alg *alg)
975985
{
976986
struct crypto_alg *base = &alg->halg.base;
@@ -996,6 +1006,12 @@ static int ahash_prepare_alg(struct ahash_alg *alg)
9961006
if (!alg->setkey)
9971007
alg->setkey = ahash_nosetkey;
9981008

1009+
if (!alg->export_core || !alg->import_core) {
1010+
alg->export_core = ahash_default_export_core;
1011+
alg->import_core = ahash_default_import_core;
1012+
base->cra_flags |= CRYPTO_AHASH_ALG_NO_EXPORT_CORE;
1013+
}
1014+
9991015
return 0;
10001016
}
10011017

crypto/shash.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,10 @@ int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data,
203203
}
204204
EXPORT_SYMBOL_GPL(crypto_shash_tfm_digest);
205205

206-
int crypto_shash_export_core(struct shash_desc *desc, void *out)
206+
static int __crypto_shash_export(struct shash_desc *desc, void *out,
207+
int (*export)(struct shash_desc *desc,
208+
void *out))
207209
{
208-
int (*export)(struct shash_desc *desc, void *out);
209210
struct crypto_shash *tfm = desc->tfm;
210211
u8 *buf = shash_desc_ctx(desc);
211212
unsigned int plen, ss;
@@ -214,14 +215,19 @@ int crypto_shash_export_core(struct shash_desc *desc, void *out)
214215
ss = crypto_shash_statesize(tfm);
215216
if (crypto_shash_block_only(tfm))
216217
ss -= plen;
217-
export = crypto_shash_alg(tfm)->export;
218218
if (!export) {
219219
memcpy(out, buf, ss);
220220
return 0;
221221
}
222222

223223
return export(desc, out);
224224
}
225+
226+
int crypto_shash_export_core(struct shash_desc *desc, void *out)
227+
{
228+
return __crypto_shash_export(desc, out,
229+
crypto_shash_alg(desc->tfm)->export_core);
230+
}
225231
EXPORT_SYMBOL_GPL(crypto_shash_export_core);
226232

227233
int crypto_shash_export(struct shash_desc *desc, void *out)
@@ -236,13 +242,14 @@ int crypto_shash_export(struct shash_desc *desc, void *out)
236242

237243
memcpy(out + ss - plen, buf + descsize - plen, plen);
238244
}
239-
return crypto_shash_export_core(desc, out);
245+
return __crypto_shash_export(desc, out, crypto_shash_alg(tfm)->export);
240246
}
241247
EXPORT_SYMBOL_GPL(crypto_shash_export);
242248

243-
int crypto_shash_import_core(struct shash_desc *desc, const void *in)
249+
static int __crypto_shash_import(struct shash_desc *desc, const void *in,
250+
int (*import)(struct shash_desc *desc,
251+
const void *in))
244252
{
245-
int (*import)(struct shash_desc *desc, const void *in);
246253
struct crypto_shash *tfm = desc->tfm;
247254
unsigned int descsize, plen, ss;
248255
u8 *buf = shash_desc_ctx(desc);
@@ -256,22 +263,27 @@ int crypto_shash_import_core(struct shash_desc *desc, const void *in)
256263
buf[descsize - 1] = 0;
257264
if (crypto_shash_block_only(tfm))
258265
ss -= plen;
259-
import = crypto_shash_alg(tfm)->import;
260266
if (!import) {
261267
memcpy(buf, in, ss);
262268
return 0;
263269
}
264270

265271
return import(desc, in);
266272
}
273+
274+
int crypto_shash_import_core(struct shash_desc *desc, const void *in)
275+
{
276+
return __crypto_shash_import(desc, in,
277+
crypto_shash_alg(desc->tfm)->import_core);
278+
}
267279
EXPORT_SYMBOL_GPL(crypto_shash_import_core);
268280

269281
int crypto_shash_import(struct shash_desc *desc, const void *in)
270282
{
271283
struct crypto_shash *tfm = desc->tfm;
272284
int err;
273285

274-
err = crypto_shash_import_core(desc, in);
286+
err = __crypto_shash_import(desc, in, crypto_shash_alg(tfm)->import);
275287
if (crypto_shash_block_only(tfm)) {
276288
unsigned int plen = crypto_shash_blocksize(tfm) + 1;
277289
unsigned int descsize = crypto_shash_descsize(tfm);
@@ -436,6 +448,16 @@ int hash_prepare_alg(struct hash_alg_common *alg)
436448
return 0;
437449
}
438450

451+
static int shash_default_export_core(struct shash_desc *desc, void *out)
452+
{
453+
return -ENOSYS;
454+
}
455+
456+
static int shash_default_import_core(struct shash_desc *desc, const void *in)
457+
{
458+
return -ENOSYS;
459+
}
460+
439461
static int shash_prepare_alg(struct shash_alg *alg)
440462
{
441463
struct crypto_alg *base = &alg->halg.base;
@@ -476,6 +498,12 @@ static int shash_prepare_alg(struct shash_alg *alg)
476498
BUILD_BUG_ON(MAX_ALGAPI_BLOCKSIZE >= 256);
477499
alg->descsize += base->cra_blocksize + 1;
478500
alg->statesize += base->cra_blocksize + 1;
501+
alg->export_core = alg->export;
502+
alg->import_core = alg->import;
503+
} else if (!alg->export_core || !alg->import_core) {
504+
alg->export_core = shash_default_export_core;
505+
alg->import_core = shash_default_import_core;
506+
base->cra_flags |= CRYPTO_AHASH_ALG_NO_EXPORT_CORE;
479507
}
480508

481509
if (alg->descsize > HASH_MAX_DESCSIZE)

include/crypto/hash.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ struct ahash_request {
129129
* data so the transformation can continue from this point onward. No
130130
* data processing happens at this point. Driver must not use
131131
* req->result.
132+
* @export_core: Export partial state without partial block. Only defined
133+
* for algorithms that are not block-only.
134+
* @import_core: Import partial state without partial block. Only defined
135+
* for algorithms that are not block-only.
132136
* @init_tfm: Initialize the cryptographic transformation object.
133137
* This function is called only once at the instantiation
134138
* time, right after the transformation context was
@@ -151,6 +155,8 @@ struct ahash_alg {
151155
int (*digest)(struct ahash_request *req);
152156
int (*export)(struct ahash_request *req, void *out);
153157
int (*import)(struct ahash_request *req, const void *in);
158+
int (*export_core)(struct ahash_request *req, void *out);
159+
int (*import_core)(struct ahash_request *req, const void *in);
154160
int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
155161
unsigned int keylen);
156162
int (*init_tfm)(struct crypto_ahash *tfm);
@@ -200,6 +206,8 @@ struct shash_desc {
200206
* @digest: see struct ahash_alg
201207
* @export: see struct ahash_alg
202208
* @import: see struct ahash_alg
209+
* @export_core: see struct ahash_alg
210+
* @import_core: see struct ahash_alg
203211
* @setkey: see struct ahash_alg
204212
* @init_tfm: Initialize the cryptographic transformation object.
205213
* This function is called only once at the instantiation
@@ -230,6 +238,8 @@ struct shash_alg {
230238
unsigned int len, u8 *out);
231239
int (*export)(struct shash_desc *desc, void *out);
232240
int (*import)(struct shash_desc *desc, const void *in);
241+
int (*export_core)(struct shash_desc *desc, void *out);
242+
int (*import_core)(struct shash_desc *desc, const void *in);
233243
int (*setkey)(struct crypto_shash *tfm, const u8 *key,
234244
unsigned int keylen);
235245
int (*init_tfm)(struct crypto_shash *tfm);

include/crypto/internal/hash.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
/* Set this bit if finup can deal with multiple blocks. */
2121
#define CRYPTO_AHASH_ALG_FINUP_MAX 0x04000000
2222

23+
/* This bit is set by the Crypto API if export_core is not supported. */
24+
#define CRYPTO_AHASH_ALG_NO_EXPORT_CORE 0x08000000
25+
2326
#define HASH_FBREQ_ON_STACK(name, req) \
2427
char __##name##_req[sizeof(struct ahash_request) + \
2528
MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \

0 commit comments

Comments
 (0)