Deleted Added
full compact
wp_dgst.c (256281) wp_dgst.c (280304)
1/**
2 * The Whirlpool hashing function.
3 *
4 * <P>
5 * <b>References</b>
6 *
7 * <P>
8 * The Whirlpool algorithm was developed by

--- 42 unchanged lines hidden (view full) ---

51 * input. This is done for perfomance.
52 */
53
54#include "wp_locl.h"
55#include <openssl/crypto.h>
56#include <string.h>
57
58fips_md_init(WHIRLPOOL)
1/**
2 * The Whirlpool hashing function.
3 *
4 * <P>
5 * <b>References</b>
6 *
7 * <P>
8 * The Whirlpool algorithm was developed by

--- 42 unchanged lines hidden (view full) ---

51 * input. This is done for perfomance.
52 */
53
54#include "wp_locl.h"
55#include <openssl/crypto.h>
56#include <string.h>
57
58fips_md_init(WHIRLPOOL)
59 {
60 memset (c,0,sizeof(*c));
61 return(1);
62 }
59{
60 memset(c, 0, sizeof(*c));
61 return (1);
62}
63
63
64int WHIRLPOOL_Update (WHIRLPOOL_CTX *c,const void *_inp,size_t bytes)
65 {
66 /* Well, largest suitable chunk size actually is
67 * (1<<(sizeof(size_t)*8-3))-64, but below number
68 * is large enough for not to care about excessive
69 * calls to WHIRLPOOL_BitUpdate... */
70 size_t chunk = ((size_t)1)<<(sizeof(size_t)*8-4);
71 const unsigned char *inp = _inp;
64int WHIRLPOOL_Update(WHIRLPOOL_CTX *c, const void *_inp, size_t bytes)
65{
66 /*
67 * Well, largest suitable chunk size actually is
68 * (1<<(sizeof(size_t)*8-3))-64, but below number is large enough for not
69 * to care about excessive calls to WHIRLPOOL_BitUpdate...
70 */
71 size_t chunk = ((size_t)1) << (sizeof(size_t) * 8 - 4);
72 const unsigned char *inp = _inp;
72
73
73 while (bytes>=chunk)
74 {
75 WHIRLPOOL_BitUpdate(c,inp,chunk*8);
76 bytes -= chunk;
77 inp += chunk;
78 }
79 if (bytes)
80 WHIRLPOOL_BitUpdate(c,inp,bytes*8);
74 while (bytes >= chunk) {
75 WHIRLPOOL_BitUpdate(c, inp, chunk * 8);
76 bytes -= chunk;
77 inp += chunk;
78 }
79 if (bytes)
80 WHIRLPOOL_BitUpdate(c, inp, bytes * 8);
81
81
82 return(1);
83 }
82 return (1);
83}
84
84
85void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c,const void *_inp,size_t bits)
86 {
87 size_t n;
88 unsigned int bitoff = c->bitoff,
89 bitrem = bitoff%8,
90 inpgap = (8-(unsigned int)bits%8)&7;
91 const unsigned char *inp=_inp;
85void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c, const void *_inp, size_t bits)
86{
87 size_t n;
88 unsigned int bitoff = c->bitoff,
89 bitrem = bitoff % 8, inpgap = (8 - (unsigned int)bits % 8) & 7;
90 const unsigned char *inp = _inp;
92
91
93 /* This 256-bit increment procedure relies on the size_t
94 * being natural size of CPU register, so that we don't
95 * have to mask the value in order to detect overflows. */
96 c->bitlen[0] += bits;
97 if (c->bitlen[0] < bits) /* overflow */
98 {
99 n = 1;
100 do { c->bitlen[n]++;
101 } while(c->bitlen[n]==0
102 && ++n<(WHIRLPOOL_COUNTER/sizeof(size_t)));
103 }
104
92 /*
93 * This 256-bit increment procedure relies on the size_t being natural
94 * size of CPU register, so that we don't have to mask the value in order
95 * to detect overflows.
96 */
97 c->bitlen[0] += bits;
98 if (c->bitlen[0] < bits) { /* overflow */
99 n = 1;
100 do {
101 c->bitlen[n]++;
102 } while (c->bitlen[n] == 0
103 && ++n < (WHIRLPOOL_COUNTER / sizeof(size_t)));
104 }
105#ifndef OPENSSL_SMALL_FOOTPRINT
105#ifndef OPENSSL_SMALL_FOOTPRINT
106 reconsider:
107 if (inpgap==0 && bitrem==0) /* byte-oriented loop */
108 {
109 while (bits)
110 {
111 if (bitoff==0 && (n=bits/WHIRLPOOL_BBLOCK))
112 {
113 whirlpool_block(c,inp,n);
114 inp += n*WHIRLPOOL_BBLOCK/8;
115 bits %= WHIRLPOOL_BBLOCK;
116 }
117 else
118 {
119 unsigned int byteoff = bitoff/8;
106 reconsider:
107 if (inpgap == 0 && bitrem == 0) { /* byte-oriented loop */
108 while (bits) {
109 if (bitoff == 0 && (n = bits / WHIRLPOOL_BBLOCK)) {
110 whirlpool_block(c, inp, n);
111 inp += n * WHIRLPOOL_BBLOCK / 8;
112 bits %= WHIRLPOOL_BBLOCK;
113 } else {
114 unsigned int byteoff = bitoff / 8;
120
115
121 bitrem = WHIRLPOOL_BBLOCK - bitoff;/* re-use bitrem */
122 if (bits >= bitrem)
123 {
124 bits -= bitrem;
125 bitrem /= 8;
126 memcpy(c->data+byteoff,inp,bitrem);
127 inp += bitrem;
128 whirlpool_block(c,c->data,1);
129 bitoff = 0;
130 }
131 else
132 {
133 memcpy(c->data+byteoff,inp,bits/8);
134 bitoff += (unsigned int)bits;
135 bits = 0;
136 }
137 c->bitoff = bitoff;
138 }
139 }
140 }
141 else /* bit-oriented loop */
116 bitrem = WHIRLPOOL_BBLOCK - bitoff; /* re-use bitrem */
117 if (bits >= bitrem) {
118 bits -= bitrem;
119 bitrem /= 8;
120 memcpy(c->data + byteoff, inp, bitrem);
121 inp += bitrem;
122 whirlpool_block(c, c->data, 1);
123 bitoff = 0;
124 } else {
125 memcpy(c->data + byteoff, inp, bits / 8);
126 bitoff += (unsigned int)bits;
127 bits = 0;
128 }
129 c->bitoff = bitoff;
130 }
131 }
132 } else /* bit-oriented loop */
142#endif
133#endif
143 {
144 /*
145 inp
146 |
147 +-------+-------+-------
148 |||||||||||||||||||||
149 +-------+-------+-------
150 +-------+-------+-------+-------+-------
151 |||||||||||||| c->data
152 +-------+-------+-------+-------+-------
153 |
154 c->bitoff/8
155 */
156 while (bits)
157 {
158 unsigned int byteoff = bitoff/8;
159 unsigned char b;
134 {
135 /*-
136 inp
137 |
138 +-------+-------+-------
139 |||||||||||||||||||||
140 +-------+-------+-------
141 +-------+-------+-------+-------+-------
142 |||||||||||||| c->data
143 +-------+-------+-------+-------+-------
144 |
145 c->bitoff/8
146 */
147 while (bits) {
148 unsigned int byteoff = bitoff / 8;
149 unsigned char b;
160
161#ifndef OPENSSL_SMALL_FOOTPRINT
150
151#ifndef OPENSSL_SMALL_FOOTPRINT
162 if (bitrem==inpgap)
163 {
164 c->data[byteoff++] |= inp[0] & (0xff>>inpgap);
165 inpgap = 8-inpgap;
166 bitoff += inpgap; bitrem = 0; /* bitoff%8 */
167 bits -= inpgap; inpgap = 0; /* bits%8 */
168 inp++;
169 if (bitoff==WHIRLPOOL_BBLOCK)
170 {
171 whirlpool_block(c,c->data,1);
172 bitoff = 0;
173 }
174 c->bitoff = bitoff;
175 goto reconsider;
176 }
177 else
152 if (bitrem == inpgap) {
153 c->data[byteoff++] |= inp[0] & (0xff >> inpgap);
154 inpgap = 8 - inpgap;
155 bitoff += inpgap;
156 bitrem = 0; /* bitoff%8 */
157 bits -= inpgap;
158 inpgap = 0; /* bits%8 */
159 inp++;
160 if (bitoff == WHIRLPOOL_BBLOCK) {
161 whirlpool_block(c, c->data, 1);
162 bitoff = 0;
163 }
164 c->bitoff = bitoff;
165 goto reconsider;
166 } else
178#endif
167#endif
179 if (bits>=8)
180 {
181 b = ((inp[0]<<inpgap) | (inp[1]>>(8-inpgap)));
182 b &= 0xff;
183 if (bitrem) c->data[byteoff++] |= b>>bitrem;
184 else c->data[byteoff++] = b;
185 bitoff += 8;
186 bits -= 8;
187 inp++;
188 if (bitoff>=WHIRLPOOL_BBLOCK)
189 {
190 whirlpool_block(c,c->data,1);
191 byteoff = 0;
192 bitoff %= WHIRLPOOL_BBLOCK;
193 }
194 if (bitrem) c->data[byteoff] = b<<(8-bitrem);
195 }
196 else /* remaining less than 8 bits */
197 {
198 b = (inp[0]<<inpgap)&0xff;
199 if (bitrem) c->data[byteoff++] |= b>>bitrem;
200 else c->data[byteoff++] = b;
201 bitoff += (unsigned int)bits;
202 if (bitoff==WHIRLPOOL_BBLOCK)
203 {
204 whirlpool_block(c,c->data,1);
205 byteoff = 0;
206 bitoff %= WHIRLPOOL_BBLOCK;
207 }
208 if (bitrem) c->data[byteoff] = b<<(8-bitrem);
209 bits = 0;
210 }
211 c->bitoff = bitoff;
212 }
213 }
214 }
168 if (bits >= 8) {
169 b = ((inp[0] << inpgap) | (inp[1] >> (8 - inpgap)));
170 b &= 0xff;
171 if (bitrem)
172 c->data[byteoff++] |= b >> bitrem;
173 else
174 c->data[byteoff++] = b;
175 bitoff += 8;
176 bits -= 8;
177 inp++;
178 if (bitoff >= WHIRLPOOL_BBLOCK) {
179 whirlpool_block(c, c->data, 1);
180 byteoff = 0;
181 bitoff %= WHIRLPOOL_BBLOCK;
182 }
183 if (bitrem)
184 c->data[byteoff] = b << (8 - bitrem);
185 } else { /* remaining less than 8 bits */
215
186
216int WHIRLPOOL_Final (unsigned char *md,WHIRLPOOL_CTX *c)
217 {
218 unsigned int bitoff = c->bitoff,
219 byteoff = bitoff/8;
220 size_t i,j,v;
221 unsigned char *p;
187 b = (inp[0] << inpgap) & 0xff;
188 if (bitrem)
189 c->data[byteoff++] |= b >> bitrem;
190 else
191 c->data[byteoff++] = b;
192 bitoff += (unsigned int)bits;
193 if (bitoff == WHIRLPOOL_BBLOCK) {
194 whirlpool_block(c, c->data, 1);
195 byteoff = 0;
196 bitoff %= WHIRLPOOL_BBLOCK;
197 }
198 if (bitrem)
199 c->data[byteoff] = b << (8 - bitrem);
200 bits = 0;
201 }
202 c->bitoff = bitoff;
203 }
204 }
205}
222
206
223 bitoff %= 8;
224 if (bitoff) c->data[byteoff] |= 0x80>>bitoff;
225 else c->data[byteoff] = 0x80;
226 byteoff++;
207int WHIRLPOOL_Final(unsigned char *md, WHIRLPOOL_CTX *c)
208{
209 unsigned int bitoff = c->bitoff, byteoff = bitoff / 8;
210 size_t i, j, v;
211 unsigned char *p;
227
212
228 /* pad with zeros */
229 if (byteoff > (WHIRLPOOL_BBLOCK/8-WHIRLPOOL_COUNTER))
230 {
231 if (byteoff<WHIRLPOOL_BBLOCK/8)
232 memset(&c->data[byteoff],0,WHIRLPOOL_BBLOCK/8-byteoff);
233 whirlpool_block(c,c->data,1);
234 byteoff = 0;
235 }
236 if (byteoff < (WHIRLPOOL_BBLOCK/8-WHIRLPOOL_COUNTER))
237 memset(&c->data[byteoff],0,
238 (WHIRLPOOL_BBLOCK/8-WHIRLPOOL_COUNTER)-byteoff);
239 /* smash 256-bit c->bitlen in big-endian order */
240 p = &c->data[WHIRLPOOL_BBLOCK/8-1]; /* last byte in c->data */
241 for(i=0;i<WHIRLPOOL_COUNTER/sizeof(size_t);i++)
242 for(v=c->bitlen[i],j=0;j<sizeof(size_t);j++,v>>=8)
243 *p-- = (unsigned char)(v&0xff);
213 bitoff %= 8;
214 if (bitoff)
215 c->data[byteoff] |= 0x80 >> bitoff;
216 else
217 c->data[byteoff] = 0x80;
218 byteoff++;
244
219
245 whirlpool_block(c,c->data,1);
220 /* pad with zeros */
221 if (byteoff > (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER)) {
222 if (byteoff < WHIRLPOOL_BBLOCK / 8)
223 memset(&c->data[byteoff], 0, WHIRLPOOL_BBLOCK / 8 - byteoff);
224 whirlpool_block(c, c->data, 1);
225 byteoff = 0;
226 }
227 if (byteoff < (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER))
228 memset(&c->data[byteoff], 0,
229 (WHIRLPOOL_BBLOCK / 8 - WHIRLPOOL_COUNTER) - byteoff);
230 /* smash 256-bit c->bitlen in big-endian order */
231 p = &c->data[WHIRLPOOL_BBLOCK / 8 - 1]; /* last byte in c->data */
232 for (i = 0; i < WHIRLPOOL_COUNTER / sizeof(size_t); i++)
233 for (v = c->bitlen[i], j = 0; j < sizeof(size_t); j++, v >>= 8)
234 *p-- = (unsigned char)(v & 0xff);
246
235
247 if (md) {
248 memcpy(md,c->H.c,WHIRLPOOL_DIGEST_LENGTH);
249 memset(c,0,sizeof(*c));
250 return(1);
251 }
252 return(0);
253 }
236 whirlpool_block(c, c->data, 1);
254
237
255unsigned char *WHIRLPOOL(const void *inp, size_t bytes,unsigned char *md)
256 {
257 WHIRLPOOL_CTX ctx;
258 static unsigned char m[WHIRLPOOL_DIGEST_LENGTH];
238 if (md) {
239 memcpy(md, c->H.c, WHIRLPOOL_DIGEST_LENGTH);
240 memset(c, 0, sizeof(*c));
241 return (1);
242 }
243 return (0);
244}
259
245
260 if (md == NULL) md=m;
261 WHIRLPOOL_Init(&ctx);
262 WHIRLPOOL_Update(&ctx,inp,bytes);
263 WHIRLPOOL_Final(md,&ctx);
264 return(md);
265 }
246unsigned char *WHIRLPOOL(const void *inp, size_t bytes, unsigned char *md)
247{
248 WHIRLPOOL_CTX ctx;
249 static unsigned char m[WHIRLPOOL_DIGEST_LENGTH];
250
251 if (md == NULL)
252 md = m;
253 WHIRLPOOL_Init(&ctx);
254 WHIRLPOOL_Update(&ctx, inp, bytes);
255 WHIRLPOOL_Final(md, &ctx);
256 return (md);
257}