comparison sha1-asm-ltc.c @ 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
comparison
equal deleted inserted replaced
908:3ca7113936c1 909:e4b75744acab
16 */ 16 */
17 17
18 18
19 #ifdef DROPBEAR_SHA1_ASM 19 #ifdef DROPBEAR_SHA1_ASM
20 20
21 const struct ltc_hash_descriptor sha1_asm_desc =
22 {
23 "sha1_asm",
24 102,
25 20,
26 64,
27
28 /* OID */
29 { 1, 3, 14, 3, 2, 26, },
30 6,
31
32 &sha1_init,
33 &sha1_process,
34 &sha1_done,
35 &sha1_test,
36 NULL
37 };
38
39 /** 21 /**
40 Initialize the hash state 22 Initialize the hash state
41 @param md The hash state you wish to initialize 23 @param md The hash state you wish to initialize
42 @return CRYPT_OK if successful 24 @return CRYPT_OK if successful
43 */ 25 */
44 int sha1_init(hash_state * md) 26 static int sha1_asm_init(hash_state * md)
45 { 27 {
46 LTC_ARGCHK(md != NULL); 28 LTC_ARGCHK(md != NULL);
47 md->sha1.state[0] = 0x67452301UL; 29 md->sha1.state[0] = 0x67452301UL;
48 md->sha1.state[1] = 0xefcdab89UL; 30 md->sha1.state[1] = 0xefcdab89UL;
49 md->sha1.state[2] = 0x98badcfeUL; 31 md->sha1.state[2] = 0x98badcfeUL;
52 md->sha1.curlen = 0; 34 md->sha1.curlen = 0;
53 md->sha1.length = 0; 35 md->sha1.length = 0;
54 return CRYPT_OK; 36 return CRYPT_OK;
55 } 37 }
56 38
57 void sha1_block_data_order(void* state_vars, const unsigned char *buf, size_t num); 39 void sha1_block_data_order(void* sha1s, const unsigned char *buf, size_t num);
58 40
59 static int sha1_asm_compress(hash_state *md, unsigned char *buf) 41 static int sha1_asm_compress(hash_state *md, unsigned char *buf, size_t num)
60 { 42 {
61 sha1_block_data_order(&md->sha1.state, buf, 1); 43 sha1_block_data_order(&md->sha1.state, buf, num);
62 return CRYPT_OK; 44 return CRYPT_OK;
63 } 45 }
64 46
65 /** 47 /**
66 Process a block of memory though the hash 48 Process a block of memory though the hash
67 @param md The hash state 49 @param md The hash state
68 @param in The data to hash 50 @param in The data to hash
69 @param inlen The length of the data (octets) 51 @param inlen The length of the data (octets)
70 @return CRYPT_OK if successful 52 @return CRYPT_OK if successful
71 */ 53 */
72 HASH_PROCESS(sha1_process, sha1_asm_compress, sha1, 64) 54 int sha1_asm_process (hash_state * md, const unsigned char *in, unsigned long inlen) \
55 { \
56 unsigned long n; \
57 int err; \
58 LTC_ARGCHK(md != NULL); \
59 LTC_ARGCHK(in != NULL); \
60 if (md-> sha1 .curlen > sizeof(md-> sha1 .buf)) { \
61 return CRYPT_INVALID_ARG; \
62 } \
63 while (inlen > 0) { \
64 if (md-> sha1 .curlen == 0 && inlen >= 64) { \
65 //const size_t num = inlen / 64;
66 const size_t num = 1;
67 if ((err = sha1_asm_compress (md, (unsigned char *)in, num)) != CRYPT_OK) { \
68 return err; \
69 } \
70 md-> sha1 .length += 64 * 8 * num; \
71 in += 64 * num; \
72 inlen -= 64 * num; \
73 } else { \
74 n = MIN(inlen, (64 - md-> sha1 .curlen)); \
75 memcpy(md-> sha1 .buf + md-> sha1.curlen, in, (size_t)n); \
76 md-> sha1 .curlen += n; \
77 in += n; \
78 inlen -= n; \
79 if (md-> sha1 .curlen == 64) { \
80 if ((err = sha1_asm_compress (md, md-> sha1 .buf, 1)) != CRYPT_OK) { \
81 return err; \
82 } \
83 md-> sha1 .length += 8*64; \
84 md-> sha1 .curlen = 0; \
85 } \
86 } \
87 } \
88 return CRYPT_OK; \
89 }
73 90
74 /** 91 /**
75 Terminate the hash to get the digest 92 Terminate the hash to get the digest
76 @param md The hash state 93 @param md The hash state
77 @param out [out] The destination of the hash (20 bytes) 94 @param out [out] The destination of the hash (20 bytes)
78 @return CRYPT_OK if successful 95 @return CRYPT_OK if successful
79 */ 96 */
80 int sha1_done(hash_state * md, unsigned char *out) 97 int sha1_asm_done(hash_state * md, unsigned char *out)
81 { 98 {
82 int i; 99 int i;
83 100
84 LTC_ARGCHK(md != NULL); 101 LTC_ARGCHK(md != NULL);
85 LTC_ARGCHK(out != NULL); 102 LTC_ARGCHK(out != NULL);
100 */ 117 */
101 if (md->sha1.curlen > 56) { 118 if (md->sha1.curlen > 56) {
102 while (md->sha1.curlen < 64) { 119 while (md->sha1.curlen < 64) {
103 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0; 120 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
104 } 121 }
105 sha1_asm_compress(md, md->sha1.buf); 122 sha1_asm_compress(md, md->sha1.buf, 1);
106 md->sha1.curlen = 0; 123 md->sha1.curlen = 0;
107 } 124 }
108 125
109 /* pad upto 56 bytes of zeroes */ 126 /* pad upto 56 bytes of zeroes */
110 while (md->sha1.curlen < 56) { 127 while (md->sha1.curlen < 56) {
111 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0; 128 md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
112 } 129 }
113 130
114 /* store length */ 131 /* store length */
115 STORE64H(md->sha1.length, md->sha1.buf+56); 132 STORE64H(md->sha1.length, md->sha1.buf+56);
116 sha1_asm_compress(md, md->sha1.buf); 133 sha1_asm_compress(md, md->sha1.buf, 1);
117 134
118 /* copy output */ 135 /* copy output */
119 for (i = 0; i < 5; i++) { 136 for (i = 0; i < 5; i++) {
120 STORE32H(md->sha1.state[i], out+(4*i)); 137 STORE32H(md->sha1.state[i], out+(4*i));
121 } 138 }
127 144
128 /** 145 /**
129 Self-test the hash 146 Self-test the hash
130 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled 147 @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
131 */ 148 */
132 int sha1_test(void) 149 int sha1_asm_test(void)
133 { 150 {
134 #ifndef LTC_TEST 151 #ifndef LTC_TEST
135 return CRYPT_NOP; 152 return CRYPT_NOP;
136 #else 153 #else
137 static const struct { 154 static const struct {
164 } 181 }
165 return CRYPT_OK; 182 return CRYPT_OK;
166 #endif 183 #endif
167 } 184 }
168 185
186 const struct ltc_hash_descriptor sha1_asm_desc =
187 {
188 "sha1_asm",
189 102,
190 20,
191 64,
192
193 /* OID */
194 { 1, 3, 14, 3, 2, 26, },
195 6,
196
197 &sha1_asm_init,
198 &sha1_asm_process,
199 &sha1_asm_done,
200 &sha1_asm_test,
201 NULL
202 };
203
204
169 #endif 205 #endif
170 206
171 207
172 208
173 /* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */ 209 /* $Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */