Resync with broadcom drivers 5.100.138.20 and utilities.
[tomato.git] / release / src-rt / bcmcrypto / md32_common.h
blob10eda0bd8063ffe3e29076b43e2fa9ead8ec26dd
1 /* crypto/md32_common.h */
2 /* ====================================================================
3 * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 * ====================================================================
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).
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.
62 * Porting guide.
64 * Obligatory macros:
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 * unsigned 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 * HASH_MAKE_STRING
98 * macro convering context variables to an ASCII hash string.
100 * Optional macros:
102 * B_ENDIAN or L_ENDIAN
103 * defines host byte-order.
104 * HASH_LONG_LOG2
105 * defaults to 2 if not states otherwise.
106 * HASH_LBLOCK
107 * assumed to be HASH_CBLOCK/4 if not stated otherwise.
108 * HASH_BLOCK_DATA_ORDER_ALIGNED
109 * alternative "block" function capable of treating
110 * aligned input message in original (data) order,
111 * implemented externally.
113 * MD5 example:
115 * #define DATA_ORDER_IS_LITTLE_ENDIAN
117 * #define HASH_LONG MD5_LONG
118 * #define HASH_LONG_LOG2 MD5_LONG_LOG2
119 * #define HASH_CTX MD5_CTX
120 * #define HASH_CBLOCK MD5_CBLOCK
121 * #define HASH_LBLOCK MD5_LBLOCK
122 * #define HASH_UPDATE MD5_Update
123 * #define HASH_TRANSFORM MD5_Transform
124 * #define HASH_FINAL MD5_Final
125 * #define HASH_BLOCK_HOST_ORDER md5_block_host_order
126 * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
128 * <appro@fy.chalmers.se>
131 /* FILE-CSTYLED */
133 #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
134 #error "DATA_ORDER must be defined!"
135 #endif
137 #ifndef HASH_CBLOCK
138 #error "HASH_CBLOCK must be defined!"
139 #endif
140 #ifndef HASH_LONG
141 #error "HASH_LONG must be defined!"
142 #endif
143 #ifndef HASH_CTX
144 #error "HASH_CTX must be defined!"
145 #endif
147 #ifndef HASH_UPDATE
148 #error "HASH_UPDATE must be defined!"
149 #endif
150 #ifndef HASH_TRANSFORM
151 #error "HASH_TRANSFORM must be defined!"
152 #endif
153 #ifndef HASH_FINAL
154 #error "HASH_FINAL must be defined!"
155 #endif
157 #ifndef HASH_BLOCK_HOST_ORDER
158 #error "HASH_BLOCK_HOST_ORDER must be defined!"
159 #endif
162 #ifndef HASH_LBLOCK
163 #define HASH_LBLOCK (HASH_CBLOCK/4)
164 #endif
166 #ifndef HASH_LONG_LOG2
167 #define HASH_LONG_LOG2 2
168 #endif
171 * Engage compiler specific rotate intrinsic function if available.
173 #undef ROTATE
174 #ifndef PEDANTIC
175 # if defined(_MSC_VER) || defined(__ICC)
176 # define ROTATE(a,n) _lrotl(a,n)
177 # elif defined(__MWERKS__)
178 # if defined(__POWERPC__)
179 # define ROTATE(a,n) __rlwinm(a,n,0,31)
180 # elif defined(__MC68K__)
181 /* Motorola specific tweak. <appro@fy.chalmers.se> */
182 # define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) )
183 # else
184 # define ROTATE(a,n) __rol(a,n)
185 # endif
186 # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && \
187 !defined(OPENSSL_NO_INLINE_ASM)
189 * Some GNU C inline assembler templates. Note that these are
190 * rotates by *constant* number of bits! But that's exactly
191 * what we need here...
192 * <appro@fy.chalmers.se>
194 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
195 # define ROTATE(a,n) ({ register unsigned int ret; \
196 asm ( \
197 "roll %1,%0" \
198 : "=r"(ret) \
199 : "I"(n), "0"(a) \
200 : "cc"); \
201 ret; \
203 # elif defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
204 # define ROTATE(a,n) ({ register unsigned int ret; \
205 asm ( \
206 "rlwinm %0,%1,%2,0,31" \
207 : "=r"(ret) \
208 : "r"(a), "I"(n)); \
209 ret; \
211 # endif
212 # endif
213 #endif /* PEDANTIC */
215 #if HASH_LONG_LOG2==2 /* Engage only if sizeof(HASH_LONG)== 4 */
216 /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
217 #ifdef ROTATE
218 /* 5 instructions with rotate instruction, else 9 */
219 #define REVERSE_FETCH32(a,l) ( \
220 l =* (const HASH_LONG *)(a), \
221 ((ROTATE(l,8)&0x00FF00FF)|(ROTATE((l&0x00FF00FF),24))) \
223 #else
224 /* 6 instructions with rotate instruction, else 8 */
225 #define REVERSE_FETCH32(a,l) ( \
226 l =* (const HASH_LONG *)(a), \
227 l = (((l>>8)&0x00FF00FF)|((l&0x00FF00FF)<<8)), \
228 ROTATE(l,16) \
231 * Originally the middle line started with l=(((l&0xFF00FF00)>>8)|...
232 * It's rewritten as above for two reasons:
233 * - RISCs aren't good at long constants and have to explicitely
234 * compose 'em with several (well, usually 2) instructions in a
235 * register before performing the actual operation and (as you
236 * already realized:-) having same constant should inspire the
237 * compiler to permanently allocate the only register for it;
238 * - most modern CPUs have two ALUs, but usually only one has
239 * circuitry for shifts:-( this minor tweak inspires compiler
240 * to schedule shift instructions in a better way...
242 * <appro@fy.chalmers.se>
244 #endif
245 #endif
247 #ifndef ROTATE
248 #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
249 #endif
252 * Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
253 * and HASH_BLOCK_HOST_ORDER ought to be the same if input data
254 * and host are of the same "endianess". It's possible to mask
255 * this with blank #define HASH_BLOCK_DATA_ORDER though...
257 * <appro@fy.chalmers.se>
259 #if defined(B_ENDIAN)
260 # if defined(DATA_ORDER_IS_BIG_ENDIAN)
261 # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
262 # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
263 # endif
264 # endif
265 #elif defined(L_ENDIAN)
266 # if defined(DATA_ORDER_IS_LITTLE_ENDIAN)
267 # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
268 # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
269 # endif
270 # endif
271 #endif
273 #if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
274 #ifndef HASH_BLOCK_DATA_ORDER
275 #error "HASH_BLOCK_DATA_ORDER must be defined!"
276 #endif
277 #endif
279 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
281 #ifndef PEDANTIC
282 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && \
283 !defined(OPENSSL_NO_INLINE_ASM)
284 # if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
285 (defined(__x86_64) || defined(__x86_64__))
287 * This gives ~30-40% performance improvement in SHA-256 compiled
288 * with gcc [on P4]. Well, first macro to be frank. We can pull
289 * this trick on x86* platforms only, because these CPUs can fetch
290 * unaligned data without raising an exception.
292 # define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
293 asm ("bswapl %0":"=r"(r):"0"(r)); \
294 (c)+=4; (l)=r; })
295 # define HOST_l2c(l,c) ({ unsigned int r=(l); \
296 asm ("bswapl %0":"=r"(r):"0"(r)); \
297 *((unsigned int *)(c))=r; (c)+=4; r; })
298 # endif
299 # endif
300 #endif
302 #ifndef HOST_c2l
303 #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
304 l|=(((unsigned long)(*((c)++)))<<16), \
305 l|=(((unsigned long)(*((c)++)))<< 8), \
306 l|=(((unsigned long)(*((c)++))) ), \
308 #endif
309 #define HOST_p_c2l(c,l,n) { \
310 switch (n) { \
311 case 0: l =((unsigned long)(*((c)++)))<<24; \
312 case 1: l|=((unsigned long)(*((c)++)))<<16; \
313 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
314 case 3: l|=((unsigned long)(*((c)++))); \
316 #define HOST_p_c2l_p(c,l,sc,len) { \
317 switch (sc) { \
318 case 0: l =((unsigned long)(*((c)++)))<<24; \
319 if (--len == 0) break; \
320 case 1: l|=((unsigned long)(*((c)++)))<<16; \
321 if (--len == 0) break; \
322 case 2: l|=((unsigned long)(*((c)++)))<< 8; \
324 /* NOTE the pointer is not incremented at the end of this */
325 #define HOST_c2l_p(c,l,n) { \
326 l=0; (c)+=n; \
327 switch (n) { \
328 case 3: l =((unsigned long)(*(--(c))))<< 8; \
329 case 2: l|=((unsigned long)(*(--(c))))<<16; \
330 case 1: l|=((unsigned long)(*(--(c))))<<24; \
332 #ifndef HOST_l2c
333 #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
334 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
335 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
336 *((c)++)=(unsigned char)(((l) )&0xff), \
338 #endif
340 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
342 #if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
343 # ifndef B_ENDIAN
344 /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
345 # define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
346 # define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
347 # endif
348 #endif
350 #ifndef HOST_c2l
351 #define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
352 l|=(((unsigned long)(*((c)++)))<< 8), \
353 l|=(((unsigned long)(*((c)++)))<<16), \
354 l|=(((unsigned long)(*((c)++)))<<24), \
356 #endif
357 #define HOST_p_c2l(c,l,n) { \
358 switch (n) { \
359 case 0: l =((unsigned long)(*((c)++))); \
360 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
361 case 2: l|=((unsigned long)(*((c)++)))<<16; \
362 case 3: l|=((unsigned long)(*((c)++)))<<24; \
364 #define HOST_p_c2l_p(c,l,sc,len) { \
365 switch (sc) { \
366 case 0: l =((unsigned long)(*((c)++))); \
367 if (--len == 0) break; \
368 case 1: l|=((unsigned long)(*((c)++)))<< 8; \
369 if (--len == 0) break; \
370 case 2: l|=((unsigned long)(*((c)++)))<<16; \
372 /* NOTE the pointer is not incremented at the end of this */
373 #define HOST_c2l_p(c,l,n) { \
374 l=0; (c)+=n; \
375 switch (n) { \
376 case 3: l =((unsigned long)(*(--(c))))<<16; \
377 case 2: l|=((unsigned long)(*(--(c))))<< 8; \
378 case 1: l|=((unsigned long)(*(--(c)))); \
380 #ifndef HOST_l2c
381 #define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
382 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
383 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
384 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
386 #endif
388 #endif
391 * Time for some action:-)
394 int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
396 const unsigned char *data=data_;
397 register HASH_LONG * p;
398 register HASH_LONG l;
399 size_t sw,sc,ew,ec;
401 if (len==0) return 1;
403 l=(c->Nl+(((HASH_LONG)len)<<3))&0xffffffffUL;
404 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
405 * Wei Dai <weidai@eskimo.com> for pointing it out. */
406 if (l < c->Nl) /* overflow */
407 c->Nh++;
408 c->Nh+=(len>>29); /* might cause compiler warning on 16-bit */
409 c->Nl=l;
411 if (c->num != 0)
413 p=c->data;
414 sw=c->num>>2;
415 sc=c->num&0x03;
417 if ((c->num+len) >= HASH_CBLOCK)
419 l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
420 for (; sw<HASH_LBLOCK; sw++)
422 HOST_c2l(data,l); p[sw]=l;
424 HASH_BLOCK_HOST_ORDER (c,p,1);
425 len-=(HASH_CBLOCK-c->num);
426 c->num=0;
427 /* drop through and do the rest */
429 else
431 c->num+=(unsigned int)len;
432 if ((sc+len) < 4) /* ugly, add char's to a word */
434 l=p[sw]; HOST_p_c2l_p(data,l,sc,len); p[sw]=l;
436 else
438 ew=(c->num>>2);
439 ec=(c->num&0x03);
440 if (sc)
441 l=p[sw];
442 HOST_p_c2l(data,l,sc);
443 p[sw++]=l;
444 for (; sw < ew; sw++)
446 HOST_c2l(data,l); p[sw]=l;
448 if (ec)
450 HOST_c2l_p(data,l,ec); p[sw]=l;
453 return 1;
457 sw=len/HASH_CBLOCK;
458 if (sw > 0)
460 #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
462 * Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
463 * only if sizeof(HASH_LONG)==4.
465 if ((((size_t)data)%4) == 0)
467 /* data is properly aligned so that we can cast it: */
468 HASH_BLOCK_DATA_ORDER_ALIGNED (c,(const HASH_LONG *)data,sw);
469 sw*=HASH_CBLOCK;
470 data+=sw;
471 len-=sw;
473 else
474 #if !defined(HASH_BLOCK_DATA_ORDER)
475 while (sw--)
477 memcpy (p=c->data,data,HASH_CBLOCK);
478 HASH_BLOCK_DATA_ORDER_ALIGNED(c,p,1);
479 data+=HASH_CBLOCK;
480 len-=HASH_CBLOCK;
482 #endif
483 #endif
484 #if defined(HASH_BLOCK_DATA_ORDER)
486 HASH_BLOCK_DATA_ORDER(c,data,sw);
487 sw*=HASH_CBLOCK;
488 data+=sw;
489 len-=sw;
491 #endif
494 if (len!=0)
496 p = c->data;
497 c->num = len;
498 ew=len>>2; /* words to copy */
499 ec=len&0x03;
500 for (; ew; ew--,p++)
502 HOST_c2l(data,l); *p=l;
504 HOST_c2l_p(data,l,ec);
505 *p=l;
507 return 1;
511 void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
513 #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
514 if ((((size_t)data)%4) == 0)
515 /* data is properly aligned so that we can cast it: */
516 HASH_BLOCK_DATA_ORDER_ALIGNED (c,(const HASH_LONG *)data,1);
517 else
518 #if !defined(HASH_BLOCK_DATA_ORDER)
520 memcpy (c->data,data,HASH_CBLOCK);
521 HASH_BLOCK_DATA_ORDER_ALIGNED (c,c->data,1);
523 #endif
524 #endif
525 #if defined(HASH_BLOCK_DATA_ORDER)
526 HASH_BLOCK_DATA_ORDER (c,data,1);
527 #endif
531 int HASH_FINAL (unsigned char *md, HASH_CTX *c)
533 register HASH_LONG *p;
534 register unsigned long l;
535 register int i,j;
536 static const unsigned char end[4]={0x80,0x00,0x00,0x00};
537 const unsigned char *cp=end;
539 /* c->num should definitly have room for at least one more byte. */
540 p=c->data;
541 i=c->num>>2;
542 j=c->num&0x03;
544 l = (j==0) ? 0 : p[i];
545 HOST_p_c2l(cp,l,j); p[i++]=l; /* i is the next 'undefined word' */
547 if (i>(HASH_LBLOCK-2)) /* save room for Nl and Nh */
549 if (i<HASH_LBLOCK) p[i]=0;
550 HASH_BLOCK_HOST_ORDER (c,p,1);
551 i=0;
553 for (; i<(HASH_LBLOCK-2); i++)
554 p[i]=0;
556 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
557 p[HASH_LBLOCK-2]=c->Nh;
558 p[HASH_LBLOCK-1]=c->Nl;
559 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
560 p[HASH_LBLOCK-2]=c->Nl;
561 p[HASH_LBLOCK-1]=c->Nh;
562 #endif
563 HASH_BLOCK_HOST_ORDER (c,p,1);
565 #ifndef HASH_MAKE_STRING
566 #error "HASH_MAKE_STRING must be defined!"
567 #else
568 HASH_MAKE_STRING(c,md);
569 #endif
571 c->num=0;
572 /* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
573 * but I'm not worried :-)
574 OPENSSL_cleanse((void *)c,sizeof(HASH_CTX));
576 return 1;
579 #ifndef MD32_REG_T
580 #define MD32_REG_T long
582 * This comment was originaly written for MD5, which is why it
583 * discusses A-D. But it basically applies to all 32-bit digests,
584 * which is why it was moved to common header file.
586 * In case you wonder why A-D are declared as long and not
587 * as MD5_LONG. Doing so results in slight performance
588 * boost on LP64 architectures. The catch is we don't
589 * really care if 32 MSBs of a 64-bit register get polluted
590 * with eventual overflows as we *save* only 32 LSBs in
591 * *either* case. Now declaring 'em long excuses the compiler
592 * from keeping 32 MSBs zeroed resulting in 13% performance
593 * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
594 * Well, to be honest it should say that this *prevents*
595 * performance degradation.
596 * <appro@fy.chalmers.se>
597 * Apparently there're LP64 compilers that generate better
598 * code if A-D are declared int. Most notably GCC-x86_64
599 * generates better code.
600 * <appro@fy.chalmers.se>
602 #endif