changeset 909:e4b75744acab asm

- Call the asm with multiple blocks - Rename functions to avoid conflicting with sha1.c
author Matt Johnston <matt@ucc.asn.au>
date Sun, 06 Oct 2013 22:32:03 +0800
parents 3ca7113936c1
children 89555751c489
files sha1-asm-ltc.c
diffstat 1 files changed, 63 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/sha1-asm-ltc.c	Sun Oct 06 21:49:15 2013 +0800
+++ b/sha1-asm-ltc.c	Sun Oct 06 22:32:03 2013 +0800
@@ -18,30 +18,12 @@
 
 #ifdef DROPBEAR_SHA1_ASM
 
-const struct ltc_hash_descriptor sha1_asm_desc =
-{
-    "sha1_asm",
-    102,
-    20,
-    64,
-
-    /* OID */
-   { 1, 3, 14, 3, 2, 26,  },
-   6,
-
-    &sha1_init,
-    &sha1_process,
-    &sha1_done,
-    &sha1_test,
-    NULL
-};
-
 /**
    Initialize the hash state
    @param md   The hash state you wish to initialize
    @return CRYPT_OK if successful
 */
-int sha1_init(hash_state * md)
+static int sha1_asm_init(hash_state * md)
 {
    LTC_ARGCHK(md != NULL);
    md->sha1.state[0] = 0x67452301UL;
@@ -54,11 +36,11 @@
    return CRYPT_OK;
 }
 
-void sha1_block_data_order(void* state_vars, const unsigned char *buf, size_t num);
+void sha1_block_data_order(void* sha1s, const unsigned char *buf, size_t num);
 
-static int sha1_asm_compress(hash_state *md, unsigned char *buf)
+static int sha1_asm_compress(hash_state *md, unsigned char *buf, size_t num)
 {
-	sha1_block_data_order(&md->sha1.state, buf, 1);
+	sha1_block_data_order(&md->sha1.state, buf, num);
 	return CRYPT_OK;
 }
 
@@ -69,7 +51,42 @@
    @param inlen  The length of the data (octets)
    @return CRYPT_OK if successful
 */
-HASH_PROCESS(sha1_process, sha1_asm_compress, sha1, 64)
+int sha1_asm_process (hash_state * md, const unsigned char *in, unsigned long inlen)               \
+{                                                                                           \
+    unsigned long n;                                                                        \
+    int           err;                                                                      \
+    LTC_ARGCHK(md != NULL);                                                                 \
+    LTC_ARGCHK(in != NULL);                                                                 \
+    if (md-> sha1 .curlen > sizeof(md-> sha1 .buf)) {                             \
+       return CRYPT_INVALID_ARG;                                                            \
+    }                                                                                       \
+    while (inlen > 0) {                                                                     \
+        if (md-> sha1 .curlen == 0 && inlen >= 64) {                           \
+           //const size_t num = inlen / 64;
+           const size_t num = 1;
+           if ((err = sha1_asm_compress (md, (unsigned char *)in, num)) != CRYPT_OK) {               \
+              return err;                                                                   \
+           }                                                                                \
+           md-> sha1 .length += 64 * 8 * num;                                        \
+           in             += 64 * num;                                                    \
+           inlen          -= 64 * num;                                                    \
+        } else {                                                                            \
+           n = MIN(inlen, (64 - md-> sha1 .curlen));                           \
+           memcpy(md-> sha1 .buf + md-> sha1.curlen, in, (size_t)n);              \
+           md-> sha1 .curlen += n;                                                     \
+           in             += n;                                                             \
+           inlen          -= n;                                                             \
+           if (md-> sha1 .curlen == 64) {                                      \
+              if ((err = sha1_asm_compress (md, md-> sha1 .buf, 1)) != CRYPT_OK) {            \
+                 return err;                                                                \
+              }                                                                             \
+              md-> sha1 .length += 8*64;                                       \
+              md-> sha1 .curlen = 0;                                                   \
+           }                                                                                \
+       }                                                                                    \
+    }                                                                                       \
+    return CRYPT_OK;                                                                        \
+}
 
 /**
    Terminate the hash to get the digest
@@ -77,7 +94,7 @@
    @param out [out] The destination of the hash (20 bytes)
    @return CRYPT_OK if successful
 */
-int sha1_done(hash_state * md, unsigned char *out)
+int sha1_asm_done(hash_state * md, unsigned char *out)
 {
     int i;
 
@@ -102,7 +119,7 @@
         while (md->sha1.curlen < 64) {
             md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
         }
-        sha1_asm_compress(md, md->sha1.buf);
+        sha1_asm_compress(md, md->sha1.buf, 1);
         md->sha1.curlen = 0;
     }
 
@@ -113,7 +130,7 @@
 
     /* store length */
     STORE64H(md->sha1.length, md->sha1.buf+56);
-    sha1_asm_compress(md, md->sha1.buf);
+    sha1_asm_compress(md, md->sha1.buf, 1);
 
     /* copy output */
     for (i = 0; i < 5; i++) {
@@ -129,7 +146,7 @@
   Self-test the hash
   @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
 */  
-int  sha1_test(void)
+int  sha1_asm_test(void)
 {
  #ifndef LTC_TEST
     return CRYPT_NOP;
@@ -166,6 +183,25 @@
   #endif
 }
 
+const struct ltc_hash_descriptor sha1_asm_desc =
+{
+    "sha1_asm",
+    102,
+    20,
+    64,
+
+    /* OID */
+   { 1, 3, 14, 3, 2, 26,  },
+   6,
+
+    &sha1_asm_init,
+    &sha1_asm_process,
+    &sha1_asm_done,
+    &sha1_asm_test,
+    NULL
+};
+
+
 #endif