]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/md32_common.h
Make MD5 work on Alpha, and fix a bug.
[thirdparty/openssl.git] / crypto / md32_common.h
1 /* crypto/md32_common.h */
2 /* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56 /*
57 * This is a generic 32 bit "collector" for message digest algorithms.
58 * Whenever needed it collects input character stream into chunks of
59 * 32 bit values and invokes a block function that performs actual hash
60 * calculations.
61 *
62 * Porting guide.
63 *
64 * Obligatory macros:
65 *
66 * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
67 * this macro defines byte order of input stream.
68 * HASH_CBLOCK
69 * size of a unit chunk HASH_BLOCK operates on.
70 * HASH_LONG
71 * has to be at lest 32 bit wide, if it's wider, then
72 * HASH_LONG_LOG2 *has to* be defined along
73 * HASH_CTX
74 * context structure that at least contains following
75 * members:
76 * typedef struct {
77 * ...
78 * HASH_LONG Nl,Nh;
79 * HASH_LONG data[HASH_LBLOCK];
80 * int num;
81 * ...
82 * } HASH_CTX;
83 * HASH_UPDATE
84 * name of "Update" function, implemented here.
85 * HASH_TRANSFORM
86 * name of "Transform" function, implemented here.
87 * HASH_FINAL
88 * name of "Final" function, implemented here.
89 * HASH_BLOCK_HOST_ORDER
90 * name of "block" function treating *aligned* input message
91 * in host byte order, implemented externally.
92 * HASH_BLOCK_DATA_ORDER
93 * name of "block" function treating *unaligned* input message
94 * in original (data) byte order, implemented externally (it
95 * actually is optional if data and host are of the same
96 * "endianess").
97 *
98 * Optional macros:
99 *
100 * B_ENDIAN or L_ENDIAN
101 * defines host byte-order.
102 * HASH_LONG_LOG2
103 * defaults to 2 if not states otherwise.
104 * HASH_LBLOCK
105 * assumed to be HASH_CBLOCK/4 if not stated otherwise.
106 * HASH_BLOCK_DATA_ORDER_ALIGNED
107 * alternative "block" function capable of treating
108 * aligned input message in original (data) order,
109 * implemented externally.
110 *
111 * MD5 example:
112 *
113 * #define DATA_ORDER_IS_LITTLE_ENDIAN
114 *
115 * #define HASH_LONG MD5_LONG
116 * #define HASH_LONG_LOG2 MD5_LONG_LOG2
117 * #define HASH_CTX MD5_CTX
118 * #define HASH_CBLOCK MD5_CBLOCK
119 * #define HASH_LBLOCK MD5_LBLOCK
120 * #define HASH_UPDATE MD5_Update
121 * #define HASH_TRANSFORM MD5_Transform
122 * #define HASH_FINAL MD5_Final
123 * #define HASH_BLOCK_HOST_ORDER md5_block_host_order
124 * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
125 *
126 * <appro@fy.chalmers.se>
127 */
128
129 #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
130 #error "DATA_ORDER must be defined!"
131 #endif
132
133 #ifndef HASH_CBLOCK
134 #error "HASH_CBLOCK must be defined!"
135 #endif
136 #ifndef HASH_LONG
137 #error "HASH_LONG must be defined!"
138 #endif
139 #ifndef HASH_CTX
140 #error "HASH_CTX must be defined!"
141 #endif
142
143 #ifndef HASH_UPDATE
144 #error "HASH_UPDATE must be defined!"
145 #endif
146 #ifndef HASH_TRANSFORM
147 #error "HASH_TRANSFORM must be defined!"
148 #endif
149 #ifndef HASH_FINAL
150 #error "HASH_FINAL must be defined!"
151 #endif
152
153 #ifndef HASH_BLOCK_HOST_ORDER
154 #error "HASH_BLOCK_HOST_ORDER must be defined!"
155 #endif
156
157 #if 0
158 /*
159 * Moved below as it's required only if HASH_BLOCK_DATA_ORDER_ALIGNED
160 * isn't defined.
161 */
162 #ifndef HASH_BLOCK_DATA_ORDER
163 #error "HASH_BLOCK_DATA_ORDER must be defined!"
164 #endif
165 #endif
166
167 #ifndef HASH_LBLOCK
168 #define HASH_LBLOCK (HASH_CBLOCK/4)
169 #endif
170
171 #ifndef HASH_LONG_LOG2
172 #define HASH_LONG_LOG2 2
173 #endif
174
175 /*
176 * Engage compiler specific rotate intrinsic function if available.
177 */
178 #undef ROTATE
179 #ifndef PEDANTIC
180 # if defined(_MSC_VER)
181 # define ROTATE(a,n) _lrotl(a,n)
182 # elif defined(__GNUC__) && __GNUC__>=2
183 /*
184 * Some GNU C inline assembler templates. Note that these are
185 * rotates by *constant* number of bits! But that's exactly
186 * what we need here...
187 *
188 * <appro@fy.chalmers.se>
189 */
190 # if defined(__i386)
191 # define ROTATE(a,n) ({ register unsigned int ret; \
192 asm volatile ( \
193 "roll %1,%0" \
194 : "=r"(ret) \
195 : "I"(n), "0"(a) \
196 : "cc"); \
197 ret; \
198 })
199 # elif defined(__powerpc)
200 # define ROTATE(a,n) ({ register unsigned int ret; \
201 asm volatile ( \
202 "rlwinm %0,%1,%2,0,31" \
203 : "=r"(ret) \
204 : "r"(a), "I"(n)); \
205 ret; \
206 })
207 # endif
208 # endif
209
210 /*
211 * Engage compiler specific "fetch in reverse byte order"
212 * intrinsic function if available.
213 */
214 # if defined(__GNUC__) && __GNUC__>=2
215 /* some GNU C inline assembler templates by <appro@fy.chalmers.se> */
216 # if defined(__i386) && !defined(I386_ONLY)
217 # define BE_FETCH32(a) ({ register unsigned int l=(a);\
218 asm volatile ( \
219 "bswapl %0" \
220 : "=r"(l) : "0"(l)); \
221 l; \
222 })
223 # elif defined(__powerpc)
224 # define LE_FETCH32(a) ({ register unsigned int l; \
225 asm volatile ( \
226 "lwbrx %0,0,%1" \
227 : "=r"(l) \
228 : "r"(a)); \
229 l; \
230 })
231
232 # elif defined(__sparc) && defined(ULTRASPARC)
233 # define LE_FETCH32(a) ({ register unsigned int l; \
234 asm volatile ( \
235 "lda [%1]#ASI_PRIMARY_LITTLE,%0"\
236 : "=r"(l) \
237 : "r"(a)); \
238 l; \
239 })
240 # endif
241 # endif
242 #endif /* PEDANTIC */
243
244 #if HASH_LONG_LOG2==2 /* Engage only if sizeof(HASH_LONG)== 4 */
245 /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
246 #ifdef ROTATE
247 /* 5 instructions with rotate instruction, else 9 */
248 #define REVERSE_FETCH32(a,l) ( \
249 l=*(const HASH_LONG *)(a), \
250 ((ROTATE(l,8)&0x00FF00FF)|(ROTATE((l&0x00FF00FF),24))) \
251 )
252 #else
253 /* 6 instructions with rotate instruction, else 8 */
254 #define REVERSE_FETCH32(a,l) ( \
255 l=*(const HASH_LONG *)(a), \
256 l=(((l>>8)&0x00FF00FF)|((l&0x00FF00FF)<<8)), \
257 ROTATE(l,16) \
258 )
259 /*
260 * Originally the middle line started with l=(((l&0xFF00FF00)>>8)|...
261 * It's rewritten as above for two reasons:
262 * - RISCs aren't good at long constants and have to explicitely
263 * compose 'em with several (well, usually 2) instructions in a
264 * register before performing the actual operation and (as you
265 * already realized:-) having same constant should inspire the
266 * compiler to permanently allocate the only register for it;
267 * - most modern CPUs have two ALUs, but usually only one has
268 * circuitry for shifts:-( this minor tweak inspires compiler
269 * to schedule shift instructions in a better way...
270 *
271 * <appro@fy.chalmers.se>
272 */
273 #endif
274 #endif
275
276 #ifndef ROTATE
277 #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
278 #endif
279
280 /*
281 * Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
282 * and HASH_BLOCK_HOST_ORDER ought to be the same if input data
283 * and host are of the same "endianess". It's possible to mask
284 * this with blank #define HASH_BLOCK_DATA_ORDER though...
285 *
286 * <appro@fy.chalmers.se>
287 */
288 #if defined(B_ENDIAN)
289 # if defined(DATA_ORDER_IS_BIG_ENDIAN)
290 # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
291 # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
292 # endif
293 # elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
294 # ifndef HOST_FETCH32
295 # ifdef LE_FETCH32
296 # define HOST_FETCH32(p,l) LE_FETCH32(p)
297 # elif defined(REVERSE_FETCH32)
298 # define HOST_FETCH32(p,l) REVERSE_FETCH32(p,l)
299 # endif
300 # endif
301 # endif
302 #elif defined(L_ENDIAN)
303 # if defined(DATA_ORDER_IS_LITTLE_ENDIAN)
304 # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
305 # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
306 # endif
307 # elif defined(DATA_ORDER_IS_BIG_ENDIAN)
308 # ifndef HOST_FETCH32
309 # ifdef BE_FETCH32
310 # define HOST_FETCH32(p,l) BE_FETCH32(p)
311 # elif defined(REVERSE_FETCH32)
312 # define HOST_FETCH32(p,l) REVERSE_FETCH32(p,l)
313 # endif
314 # endif
315 # endif
316 #endif
317
318 #if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) || HASH_BLOCK_DATA_ORDER_ALIGNED==1
319 #ifndef HASH_BLOCK_DATA_ORDER
320 #error "HASH_BLOCK_DATA_ORDER must be defined!"
321 #endif
322 #endif
323
324 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
325
326 #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
327 l|=(((unsigned long)(*((c)++)))<<16), \
328 l|=(((unsigned long)(*((c)++)))<< 8), \
329 l|=(((unsigned long)(*((c)++))) ), \
330 l)
331 #define HOST_p_c2l(c,l,n) { \
332 switch (n) { \
333 case 0: l =((unsigned long)(*((c)++)))<<24; \
334 case 1: l|=((unsigned long)(*((c)++)))<<16; \
335 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
336 case 3: l|=((unsigned long)(*((c)++))); \
337 } }
338 #define HOST_p_c2l_p(c,l,sc,len) { \
339 switch (sc) { \
340 case 0: l =((unsigned long)(*((c)++)))<<24; \
341 if (--len == 0) break; \
342 case 1: l|=((unsigned long)(*((c)++)))<<16; \
343 if (--len == 0) break; \
344 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
345 } }
346 /* NOTE the pointer is not incremented at the end of this */
347 #define HOST_c2l_p(c,l,n) { \
348 l=0; (c)+=n; \
349 switch (n) { \
350 case 3: l =((unsigned long)(*(--(c))))<< 8; \
351 case 2: l|=((unsigned long)(*(--(c))))<<16; \
352 case 1: l|=((unsigned long)(*(--(c))))<<24; \
353 } }
354 #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
355 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
356 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
357 *((c)++)=(unsigned char)(((l) )&0xff), \
358 l)
359
360 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
361
362 #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
363 l|=(((unsigned long)(*((c)++)))<< 8), \
364 l|=(((unsigned long)(*((c)++)))<<16), \
365 l|=(((unsigned long)(*((c)++)))<<24), \
366 l)
367 #define HOST_p_c2l(c,l,n) { \
368 switch (n) { \
369 case 0: l =((unsigned long)(*((c)++))); \
370 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
371 case 2: l|=((unsigned long)(*((c)++)))<<16; \
372 case 3: l|=((unsigned long)(*((c)++)))<<24; \
373 } }
374 #define HOST_p_c2l_p(c,l,sc,len) { \
375 switch (sc) { \
376 case 0: l =((unsigned long)(*((c)++))); \
377 if (--len == 0) break; \
378 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
379 if (--len == 0) break; \
380 case 2: l|=((unsigned long)(*((c)++)))<<16; \
381 } }
382 /* NOTE the pointer is not incremented at the end of this */
383 #define HOST_c2l_p(c,l,n) { \
384 l=0; (c)+=n; \
385 switch (n) { \
386 case 3: l =((unsigned long)(*(--(c))))<<16; \
387 case 2: l|=((unsigned long)(*(--(c))))<< 8; \
388 case 1: l|=((unsigned long)(*(--(c)))); \
389 } }
390 #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
391 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
392 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
393 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
394 l)
395
396 #endif
397
398 /*
399 * Time for some action:-)
400 */
401
402 void HASH_UPDATE (HASH_CTX *c, const unsigned char *data, unsigned long len)
403 {
404 register HASH_LONG * p;
405 register unsigned long l;
406 int sw,sc,ew,ec;
407
408 if (len==0) return;
409
410 l=(c->Nl+(len<<3))&0xffffffffL;
411 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
412 * Wei Dai <weidai@eskimo.com> for pointing it out. */
413 if (l < c->Nl) /* overflow */
414 c->Nh++;
415 c->Nh+=(len>>29);
416 c->Nl=l;
417
418 if (c->num != 0)
419 {
420 p=c->data;
421 sw=c->num>>2;
422 sc=c->num&0x03;
423
424 if ((c->num+len) >= HASH_CBLOCK)
425 {
426 l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
427 for (; sw<HASH_LBLOCK; sw++)
428 {
429 HOST_c2l(data,l); p[sw]=l;
430 }
431 HASH_BLOCK_HOST_ORDER (c,p,1);
432 len-=(HASH_CBLOCK-c->num);
433 c->num=0;
434 /* drop through and do the rest */
435 }
436 else
437 {
438 c->num+=len;
439 if ((sc+len) < 4) /* ugly, add char's to a word */
440 {
441 l=p[sw]; HOST_p_c2l_p(data,l,sc,len); p[sw]=l;
442 }
443 else
444 {
445 ew=(c->num>>2);
446 ec=(c->num&0x03);
447 l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
448 for (; sw < ew; sw++)
449 {
450 HOST_c2l(data,l); p[sw]=l;
451 }
452 if (ec)
453 {
454 HOST_c2l_p(data,l,ec); p[sw]=l;
455 }
456 }
457 return;
458 }
459 }
460
461 sw=len/HASH_CBLOCK;
462 if (sw > 0)
463 {
464 #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_BLOCK_DATA_ORDER_ALIGNED!=1
465 /*
466 * Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
467 * only if sizeof(HASH_LONG)==4.
468 */
469 if ((((unsigned long)data)%4) == 0)
470 {
471 HASH_BLOCK_DATA_ORDER_ALIGNED (c,data,sw);
472 sw*=HASH_CBLOCK;
473 data+=sw;
474 len-=sw;
475 }
476 else
477 #if !defined(HASH_BLOCK_DATA_ORDER)
478 while (sw--)
479 {
480 memcpy (p=c->data,data,HASH_CBLOCK);
481 HASH_BLOCK_DATA_ORDER_ALIGNED(c,p,1);
482 data+=HASH_CBLOCK;
483 len-=HASH_CBLOCK;
484 }
485 #endif
486 #endif
487 #if defined(HASH_BLOCK_DATA_ORDER)
488 {
489 HASH_BLOCK_DATA_ORDER(c,data,sw);
490 sw*=HASH_CBLOCK;
491 data+=sw;
492 len-=sw;
493 }
494 #endif
495 }
496
497 if (len!=0)
498 {
499 p = c->data;
500 c->num = len;
501 ew=len>>2; /* words to copy */
502 ec=len&0x03;
503 for (; ew; ew--,p++)
504 {
505 HOST_c2l(data,l); *p=l;
506 }
507 HOST_c2l_p(data,l,ec);
508 *p=l;
509 }
510 }
511
512
513 void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
514 {
515 #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_BLOCK_DATA_ORDER_ALIGNED!=1
516 if ((((unsigned long)data)%4) == 0)
517 HASH_BLOCK_DATA_ORDER_ALIGNED (c,data,1);
518 else
519 #if !defined(HASH_BLOCK_DATA_ORDER)
520 {
521 memcpy (c->data,data,HASH_CBLOCK);
522 HASH_BLOCK_DATA_ORDER_ALIGNED (c,c->data,1);
523 }
524 #endif
525 #endif
526 #if defined(HASH_BLOCK_DATA_ORDER)
527 HASH_BLOCK_DATA_ORDER (c,data,1);
528 #endif
529 }
530
531
532 void HASH_FINAL (unsigned char *md, HASH_CTX *c)
533 {
534 register HASH_LONG *p;
535 register unsigned long l;
536 register int i,j;
537 static const unsigned char end[4]={0x80,0x00,0x00,0x00};
538 const unsigned char *cp=end;
539
540 /* c->num should definitly have room for at least one more byte. */
541 p=c->data;
542 i=c->num>>2;
543 j=c->num&0x03;
544
545 #if 0
546 /* purify often complains about the following line as an
547 * Uninitialized Memory Read. While this can be true, the
548 * following p_c2l macro will reset l when that case is true.
549 * This is because j&0x03 contains the number of 'valid' bytes
550 * already in p[i]. If and only if j&0x03 == 0, the UMR will
551 * occur but this is also the only time p_c2l will do
552 * l= *(cp++) instead of l|= *(cp++)
553 * Many thanks to Alex Tang <altitude@cic.net> for pickup this
554 * 'potential bug' */
555 #ifdef PURIFY
556 if (j==0) p[i]=0; /* Yeah, but that's not the way to fix it:-) */
557 #endif
558 l=p[i];
559 #else
560 l = (j==0) ? 0 : p[i];
561 #endif
562 HOST_p_c2l(cp,l,j); p[i++]=l; /* i is the next 'undefined word' */
563
564 if (i>(HASH_LBLOCK-2)) /* save room for Nl and Nh */
565 {
566 if (i<HASH_LBLOCK) p[i]=0;
567 HASH_BLOCK_HOST_ORDER (c,p,1);
568 i=0;
569 }
570 for (; i<(HASH_LBLOCK-2); i++)
571 p[i]=0;
572
573 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
574 p[HASH_LBLOCK-2]=c->Nh;
575 p[HASH_LBLOCK-1]=c->Nl;
576 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
577 p[HASH_LBLOCK-2]=c->Nl;
578 p[HASH_LBLOCK-1]=c->Nh;
579 #endif
580 HASH_BLOCK_HOST_ORDER (c,p,1);
581
582 l=c->A; HOST_l2c(l,md);
583 l=c->B; HOST_l2c(l,md);
584 l=c->C; HOST_l2c(l,md);
585 l=c->D; HOST_l2c(l,md);
586
587 c->num=0;
588 /* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
589 * but I'm not worried :-)
590 memset((void *)c,0,sizeof(HASH_CTX));
591 */
592 }