crypto: hash - Add import/export interface
authorHerbert Xu <herbert@gondor.apana.org.au>
Sun, 2 Nov 2008 13:38:11 +0000 (21:38 +0800)
committerHerbert Xu <herbert@gondor.apana.org.au>
Thu, 25 Dec 2008 00:01:30 +0000 (11:01 +1100)
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>
crypto/ahash.c
crypto/shash.c
include/crypto/hash.h
include/crypto/internal/hash.h
include/linux/crypto.h

index 27128f2c687a62494b8d20b60191c916376aca4a..7d4e33dfe2124379e63e303c5b722153e04e781c 100644 (file)
@@ -146,6 +146,20 @@ static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
        return ahash->setkey(tfm, key, keylen);
 }
 
+int crypto_ahash_import(struct ahash_request *req, const u8 *in)
+{
+       struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+       struct ahash_alg *alg = crypto_ahash_alg(tfm);
+
+       memcpy(ahash_request_ctx(req), in, crypto_ahash_reqsize(tfm));
+
+       if (alg->reinit)
+               alg->reinit(req);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(crypto_ahash_import);
+
 static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
                                        u32 mask)
 {
index 3f4c713a21ea9afce338297d0c293666e08a82c2..26aff3feefc034bd28987370d444be8221acda5f 100644 (file)
@@ -172,6 +172,20 @@ int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
 }
 EXPORT_SYMBOL_GPL(crypto_shash_digest);
 
+int crypto_shash_import(struct shash_desc *desc, const u8 *in)
+{
+       struct crypto_shash *tfm = desc->tfm;
+       struct shash_alg *alg = crypto_shash_alg(tfm);
+
+       memcpy(shash_desc_ctx(desc), in, crypto_shash_descsize(tfm));
+
+       if (alg->reinit)
+               alg->reinit(desc);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(crypto_shash_import);
+
 static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key,
                              unsigned int keylen)
 {
index f9b51d408953240814c6acef3cb9a8077adfd2c0..cd16d6e668ce78a523a7865f747d5078ad0ecd76 100644 (file)
@@ -24,6 +24,7 @@ struct shash_desc {
 
 struct shash_alg {
        int (*init)(struct shash_desc *desc);
+       int (*reinit)(struct shash_desc *desc);
        int (*update)(struct shash_desc *desc, const u8 *data,
                      unsigned int len);
        int (*final)(struct shash_desc *desc, u8 *out);
@@ -116,6 +117,11 @@ static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
        return crypto_ahash_crt(tfm)->reqsize;
 }
 
+static inline void *ahash_request_ctx(struct ahash_request *req)
+{
+       return req->__ctx;
+}
+
 static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
                                      const u8 *key, unsigned int keylen)
 {
@@ -130,6 +136,14 @@ static inline int crypto_ahash_digest(struct ahash_request *req)
        return crt->digest(req);
 }
 
+static inline void crypto_ahash_export(struct ahash_request *req, u8 *out)
+{
+       memcpy(out, ahash_request_ctx(req),
+              crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
+}
+
+int crypto_ahash_import(struct ahash_request *req, const u8 *in);
+
 static inline int crypto_ahash_init(struct ahash_request *req)
 {
        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,
 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
                        unsigned int len, u8 *out);
 
+static inline void crypto_shash_export(struct shash_desc *desc, u8 *out)
+{
+       memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
+}
+
+int crypto_shash_import(struct shash_desc *desc, const u8 *in);
+
 static inline int crypto_shash_init(struct shash_desc *desc)
 {
        return crypto_shash_alg(desc->tfm)->init(desc);
index 32d3a8ed06de9bd3b98df7ec8854d80bc4c5ad8a..92fbe7385856f5ebaf2dcc65b6c44d6628f1abc8 100644 (file)
@@ -66,11 +66,6 @@ static inline struct ahash_request *ahash_dequeue_request(
        return ahash_request_cast(crypto_dequeue_request(queue));
 }
 
-static inline void *ahash_request_ctx(struct ahash_request *req)
-{
-       return req->__ctx;
-}
-
 static inline int ahash_tfm_in_queue(struct crypto_queue *queue,
                                          struct crypto_ahash *tfm)
 {
index 44c72f0f9b05e7843c8becd5b7ab1c77aef39d60..77a1f3d9416d137e04fbff605fc42d83bf4bac46 100644 (file)
@@ -221,6 +221,7 @@ struct ablkcipher_alg {
 
 struct ahash_alg {
        int (*init)(struct ahash_request *req);
+       int (*reinit)(struct ahash_request *req);
        int (*update)(struct ahash_request *req);
        int (*final)(struct ahash_request *req);
        int (*digest)(struct ahash_request *req);