crypto: serpent - export common functions for x86_64/i386-sse2 assembler implementations
[linux-flexiantxendom0-3.2.10.git] / crypto / sha1_generic.c
index 9efef20..4279480 100644 (file)
 #include <crypto/sha.h>
 #include <asm/byteorder.h>
 
-struct sha1_ctx {
-        u64 count;
-        u32 state[5];
-        u8 buffer[64];
-};
-
 static int sha1_init(struct shash_desc *desc)
 {
-       struct sha1_ctx *sctx = shash_desc_ctx(desc);
+       struct sha1_state *sctx = shash_desc_ctx(desc);
 
-       static const struct sha1_ctx initstate = {
-         0,
-         { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
-         { 0, }
+       *sctx = (struct sha1_state){
+               .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
        };
 
-       *sctx = initstate;
-
        return 0;
 }
 
-static int sha1_update(struct shash_desc *desc, const u8 *data,
+int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
                        unsigned int len)
 {
-       struct sha1_ctx *sctx = shash_desc_ctx(desc);
+       struct sha1_state *sctx = shash_desc_ctx(desc);
        unsigned int partial, done;
        const u8 *src;
 
-       partial = sctx->count & 0x3f;
+       partial = sctx->count % SHA1_BLOCK_SIZE;
        sctx->count += len;
        done = 0;
        src = data;
 
-       if ((partial + len) > 63) {
+       if ((partial + len) >= SHA1_BLOCK_SIZE) {
                u32 temp[SHA_WORKSPACE_WORDS];
 
                if (partial) {
                        done = -partial;
-                       memcpy(sctx->buffer + partial, data, done + 64);
+                       memcpy(sctx->buffer + partial, data,
+                              done + SHA1_BLOCK_SIZE);
                        src = sctx->buffer;
                }
 
                do {
                        sha_transform(sctx->state, src, temp);
-                       done += 64;
+                       done += SHA1_BLOCK_SIZE;
                        src = data + done;
-               } while (done + 63 < len);
+               } while (done + SHA1_BLOCK_SIZE <= len);
 
                memset(temp, 0, sizeof(temp));
                partial = 0;
@@ -80,12 +71,13 @@ static int sha1_update(struct shash_desc *desc, const u8 *data,
 
        return 0;
 }
+EXPORT_SYMBOL(crypto_sha1_update);
 
 
 /* Add padding and return the message digest. */
 static int sha1_final(struct shash_desc *desc, u8 *out)
 {
-       struct sha1_ctx *sctx = shash_desc_ctx(desc);
+       struct sha1_state *sctx = shash_desc_ctx(desc);
        __be32 *dst = (__be32 *)out;
        u32 i, index, padlen;
        __be64 bits;
@@ -96,10 +88,10 @@ static int sha1_final(struct shash_desc *desc, u8 *out)
        /* Pad out to 56 mod 64 */
        index = sctx->count & 0x3f;
        padlen = (index < 56) ? (56 - index) : ((64+56) - index);
-       sha1_update(desc, padding, padlen);
+       crypto_sha1_update(desc, padding, padlen);
 
        /* Append length */
-       sha1_update(desc, (const u8 *)&bits, sizeof(bits));
+       crypto_sha1_update(desc, (const u8 *)&bits, sizeof(bits));
 
        /* Store state in digest */
        for (i = 0; i < 5; i++)
@@ -111,12 +103,31 @@ static int sha1_final(struct shash_desc *desc, u8 *out)
        return 0;
 }
 
+static int sha1_export(struct shash_desc *desc, void *out)
+{
+       struct sha1_state *sctx = shash_desc_ctx(desc);
+
+       memcpy(out, sctx, sizeof(*sctx));
+       return 0;
+}
+
+static int sha1_import(struct shash_desc *desc, const void *in)
+{
+       struct sha1_state *sctx = shash_desc_ctx(desc);
+
+       memcpy(sctx, in, sizeof(*sctx));
+       return 0;
+}
+
 static struct shash_alg alg = {
        .digestsize     =       SHA1_DIGEST_SIZE,
        .init           =       sha1_init,
-       .update         =       sha1_update,
+       .update         =       crypto_sha1_update,
        .final          =       sha1_final,
-       .descsize       =       sizeof(struct sha1_ctx),
+       .export         =       sha1_export,
+       .import         =       sha1_import,
+       .descsize       =       sizeof(struct sha1_state),
+       .statesize      =       sizeof(struct sha1_state),
        .base           =       {
                .cra_name       =       "sha1",
                .cra_driver_name=       "sha1-generic",