]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/md32_common.h
RT3544: Remove MWERKS support
[thirdparty/openssl.git] / crypto / md32_common.h
CommitLineData
bd3576d2
UM
1/* crypto/md32_common.h */
2/* ====================================================================
20f7563f 3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
bd3576d2
UM
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 *
bd3576d2
UM
50 */
51
52/*
53 * This is a generic 32 bit "collector" for message digest algorithms.
54 * Whenever needed it collects input character stream into chunks of
55 * 32 bit values and invokes a block function that performs actual hash
56 * calculations.
57 *
58 * Porting guide.
59 *
60 * Obligatory macros:
61 *
62 * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
63 * this macro defines byte order of input stream.
64 * HASH_CBLOCK
65 * size of a unit chunk HASH_BLOCK operates on.
66 * HASH_LONG
f6fb2c95
AP
67 * has to be at lest 32 bit wide, if it's wider, then
68 * HASH_LONG_LOG2 *has to* be defined along
bd3576d2
UM
69 * HASH_CTX
70 * context structure that at least contains following
71 * members:
72 * typedef struct {
73 * ...
74 * HASH_LONG Nl,Nh;
c69ed6ea 75 * either {
f6fb2c95
AP
76 * HASH_LONG data[HASH_LBLOCK];
77 * unsigned char data[HASH_CBLOCK];
c69ed6ea 78 * };
16ab8a93 79 * unsigned int num;
bd3576d2
UM
80 * ...
81 * } HASH_CTX;
c69ed6ea
AP
82 * data[] vector is expected to be zeroed upon first call to
83 * HASH_UPDATE.
bd3576d2
UM
84 * HASH_UPDATE
85 * name of "Update" function, implemented here.
86 * HASH_TRANSFORM
87 * name of "Transform" function, implemented here.
88 * HASH_FINAL
89 * name of "Final" function, implemented here.
bd3576d2 90 * HASH_BLOCK_DATA_ORDER
c69ed6ea
AP
91 * name of "block" function capable of treating *unaligned* input
92 * message in original (data) byte order, implemented externally.
1cbde6e4
AP
93 * HASH_MAKE_STRING
94 * macro convering context variables to an ASCII hash string.
bd3576d2 95 *
bd3576d2
UM
96 * MD5 example:
97 *
98 * #define DATA_ORDER_IS_LITTLE_ENDIAN
99 *
100 * #define HASH_LONG MD5_LONG
f6fb2c95 101 * #define HASH_LONG_LOG2 MD5_LONG_LOG2
bd3576d2
UM
102 * #define HASH_CTX MD5_CTX
103 * #define HASH_CBLOCK MD5_CBLOCK
bd3576d2
UM
104 * #define HASH_UPDATE MD5_Update
105 * #define HASH_TRANSFORM MD5_Transform
106 * #define HASH_FINAL MD5_Final
bd3576d2
UM
107 * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
108 *
109 * <appro@fy.chalmers.se>
110 */
111
112#if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
113#error "DATA_ORDER must be defined!"
114#endif
115
116#ifndef HASH_CBLOCK
117#error "HASH_CBLOCK must be defined!"
118#endif
119#ifndef HASH_LONG
120#error "HASH_LONG must be defined!"
121#endif
122#ifndef HASH_CTX
123#error "HASH_CTX must be defined!"
124#endif
125
126#ifndef HASH_UPDATE
127#error "HASH_UPDATE must be defined!"
128#endif
129#ifndef HASH_TRANSFORM
130#error "HASH_TRANSFORM must be defined!"
131#endif
132#ifndef HASH_FINAL
133#error "HASH_FINAL must be defined!"
134#endif
135
bd3576d2
UM
136#ifndef HASH_BLOCK_DATA_ORDER
137#error "HASH_BLOCK_DATA_ORDER must be defined!"
138#endif
bd3576d2
UM
139
140/*
141 * Engage compiler specific rotate intrinsic function if available.
142 */
143#undef ROTATE
144#ifndef PEDANTIC
cf5ecc3e 145# if defined(_MSC_VER)
1cbde6e4 146# define ROTATE(a,n) _lrotl(a,n)
cf5ecc3e
AP
147# elif defined(__ICC)
148# define ROTATE(a,n) _rotl(a,n)
cf1b7d96 149# elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
bd3576d2
UM
150 /*
151 * Some GNU C inline assembler templates. Note that these are
152 * rotates by *constant* number of bits! But that's exactly
153 * what we need here...
bd3576d2
UM
154 * <appro@fy.chalmers.se>
155 */
2f98abbc 156# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
bd3576d2 157# define ROTATE(a,n) ({ register unsigned int ret; \
0fad6cb7 158 asm ( \
bd3576d2
UM
159 "roll %1,%0" \
160 : "=r"(ret) \
30ab7af2 161 : "I"(n), "0"((unsigned int)(a)) \
bd3576d2
UM
162 : "cc"); \
163 ret; \
164 })
a9c32ace
AP
165# elif defined(_ARCH_PPC) || defined(_ARCH_PPC64) || \
166 defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
bd3576d2 167# define ROTATE(a,n) ({ register unsigned int ret; \
0fad6cb7 168 asm ( \
bd3576d2
UM
169 "rlwinm %0,%1,%2,0,31" \
170 : "=r"(ret) \
171 : "r"(a), "I"(n)); \
172 ret; \
173 })
70831126 174# elif defined(__s390x__)
b38c0add
AP
175# define ROTATE(a,n) ({ register unsigned int ret; \
176 asm ("rll %0,%1,%2" \
177 : "=r"(ret) \
70831126 178 : "r"(a), "I"(n)); \
b38c0add
AP
179 ret; \
180 })
bd3576d2
UM
181# endif
182# endif
bd3576d2
UM
183#endif /* PEDANTIC */
184
bd3576d2
UM
185#ifndef ROTATE
186#define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
187#endif
188
bd3576d2
UM
189#if defined(DATA_ORDER_IS_BIG_ENDIAN)
190
a2eb9688
AP
191#ifndef PEDANTIC
192# if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
6d9c46b8 193# if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
af2c2823 194 (defined(__x86_64) || defined(__x86_64__))
6c1fc273 195# if !defined(B_ENDIAN)
a2eb9688
AP
196 /*
197 * This gives ~30-40% performance improvement in SHA-256 compiled
198 * with gcc [on P4]. Well, first macro to be frank. We can pull
199 * this trick on x86* platforms only, because these CPUs can fetch
200 * unaligned data without raising an exception.
201 */
385c8e89
AP
202# define HOST_c2l(c,l) ({ unsigned int r=*((const unsigned int *)(c)); \
203 asm ("bswapl %0":"=r"(r):"0"(r)); \
204 (c)+=4; (l)=r; })
a2eb9688
AP
205# define HOST_l2c(l,c) ({ unsigned int r=(l); \
206 asm ("bswapl %0":"=r"(r):"0"(r)); \
207 *((unsigned int *)(c))=r; (c)+=4; r; })
6c1fc273 208# endif
039081b8
AP
209# elif defined(__aarch64__)
210# if defined(__BYTE_ORDER__)
211# if defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
212# define HOST_c2l(c,l) ({ unsigned int r; \
213 asm ("rev %w0,%w1" \
214 :"=r"(r) \
215 :"r"(*((const unsigned int *)(c))));\
216 (c)+=4; (l)=r; })
217# define HOST_l2c(l,c) ({ unsigned int r; \
218 asm ("rev %w0,%w1" \
219 :"=r"(r) \
220 :"r"((unsigned int)(l)));\
221 *((unsigned int *)(c))=r; (c)+=4; r; })
222# elif defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
223# define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
224# define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
225# endif
226# endif
a2eb9688
AP
227# endif
228# endif
021e5043
AP
229# if defined(__s390__) || defined(__s390x__)
230# define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
231# define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
232# endif
b38c0add 233#endif
a2eb9688
AP
234
235#ifndef HOST_c2l
bd3576d2
UM
236#define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
237 l|=(((unsigned long)(*((c)++)))<<16), \
238 l|=(((unsigned long)(*((c)++)))<< 8), \
239 l|=(((unsigned long)(*((c)++))) ), \
240 l)
a2eb9688 241#endif
a2eb9688 242#ifndef HOST_l2c
bd3576d2
UM
243#define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
244 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
245 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
246 *((c)++)=(unsigned char)(((l) )&0xff), \
247 l)
a2eb9688 248#endif
bd3576d2
UM
249
250#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
251
b38c0add
AP
252#ifndef PEDANTIC
253# if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
254# if defined(__s390x__)
d8c7bd6e
AP
255# define HOST_c2l(c,l) ({ asm ("lrv %0,%1" \
256 :"=d"(l) :"m"(*(const unsigned int *)(c)));\
b38c0add 257 (c)+=4; (l); })
d8c7bd6e
AP
258# define HOST_l2c(l,c) ({ asm ("strv %1,%0" \
259 :"=m"(*(unsigned int *)(c)) :"d"(l));\
b38c0add
AP
260 (c)+=4; (l); })
261# endif
262# endif
021e5043
AP
263# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
264# ifndef B_ENDIAN
265 /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
266# define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
267# define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
268# endif
1110cea0 269# endif
a2eb9688
AP
270#endif
271
272#ifndef HOST_c2l
bd3576d2
UM
273#define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
274 l|=(((unsigned long)(*((c)++)))<< 8), \
275 l|=(((unsigned long)(*((c)++)))<<16), \
276 l|=(((unsigned long)(*((c)++)))<<24), \
277 l)
a2eb9688 278#endif
a2eb9688 279#ifndef HOST_l2c
bd3576d2
UM
280#define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
281 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
282 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
283 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
284 l)
a2eb9688 285#endif
bd3576d2
UM
286
287#endif
288
289/*
290 * Time for some action:-)
291 */
292
9e0aad9f 293int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
bd3576d2 294 {
bd445703 295 const unsigned char *data=data_;
c69ed6ea
AP
296 unsigned char *p;
297 HASH_LONG l;
298 size_t n;
bd3576d2 299
2dc769a1 300 if (len==0) return 1;
bd3576d2 301
9e0aad9f 302 l=(c->Nl+(((HASH_LONG)len)<<3))&0xffffffffUL;
bd3576d2
UM
303 /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
304 * Wei Dai <weidai@eskimo.com> for pointing it out. */
305 if (l < c->Nl) /* overflow */
306 c->Nh++;
5cabcf96 307 c->Nh+=(HASH_LONG)(len>>29); /* might cause compiler warning on 16-bit */
bd3576d2
UM
308 c->Nl=l;
309
c69ed6ea
AP
310 n = c->num;
311 if (n != 0)
bd3576d2 312 {
c69ed6ea 313 p=(unsigned char *)c->data;
bd3576d2 314
f8d6be3f 315 if (len >= HASH_CBLOCK || len+n >= HASH_CBLOCK)
bd3576d2 316 {
c69ed6ea
AP
317 memcpy (p+n,data,HASH_CBLOCK-n);
318 HASH_BLOCK_DATA_ORDER (c,p,1);
319 n = HASH_CBLOCK-n;
320 data += n;
321 len -= n;
322 c->num = 0;
323 memset (p,0,HASH_CBLOCK); /* keep it zeroed */
bd3576d2
UM
324 }
325 else
326 {
c69ed6ea
AP
327 memcpy (p+n,data,len);
328 c->num += (unsigned int)len;
2dc769a1 329 return 1;
bd3576d2
UM
330 }
331 }
332
c69ed6ea
AP
333 n = len/HASH_CBLOCK;
334 if (n > 0)
bd3576d2 335 {
c69ed6ea
AP
336 HASH_BLOCK_DATA_ORDER (c,data,n);
337 n *= HASH_CBLOCK;
338 data += n;
339 len -= n;
bd3576d2
UM
340 }
341
c69ed6ea 342 if (len != 0)
bd3576d2 343 {
c69ed6ea 344 p = (unsigned char *)c->data;
5cabcf96 345 c->num = (unsigned int)len;
c69ed6ea 346 memcpy (p,data,len);
bd3576d2 347 }
2dc769a1 348 return 1;
bd3576d2
UM
349 }
350
351
ac7d0785 352void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
bd3576d2 353 {
cdfb093f 354 HASH_BLOCK_DATA_ORDER (c,data,1);
bd3576d2
UM
355 }
356
357
2dc769a1 358int HASH_FINAL (unsigned char *md, HASH_CTX *c)
bd3576d2 359 {
c69ed6ea
AP
360 unsigned char *p = (unsigned char *)c->data;
361 size_t n = c->num;
bd3576d2 362
c69ed6ea
AP
363 p[n] = 0x80; /* there is always room for one */
364 n++;
365
366 if (n > (HASH_CBLOCK-8))
bd3576d2 367 {
20f7563f
AP
368 memset (p+n,0,HASH_CBLOCK-n);
369 n=0;
c69ed6ea 370 HASH_BLOCK_DATA_ORDER (c,p,1);
bd3576d2 371 }
20f7563f 372 memset (p+n,0,HASH_CBLOCK-8-n);
bd3576d2 373
c69ed6ea 374 p += HASH_CBLOCK-8;
bd3576d2 375#if defined(DATA_ORDER_IS_BIG_ENDIAN)
c69ed6ea
AP
376 (void)HOST_l2c(c->Nh,p);
377 (void)HOST_l2c(c->Nl,p);
bd3576d2 378#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
c69ed6ea
AP
379 (void)HOST_l2c(c->Nl,p);
380 (void)HOST_l2c(c->Nh,p);
bd3576d2 381#endif
c69ed6ea
AP
382 p -= HASH_CBLOCK;
383 HASH_BLOCK_DATA_ORDER (c,p,1);
384 c->num=0;
385 memset (p,0,HASH_CBLOCK);
bd3576d2 386
1cbde6e4
AP
387#ifndef HASH_MAKE_STRING
388#error "HASH_MAKE_STRING must be defined!"
389#else
390 HASH_MAKE_STRING(c,md);
391#endif
bd3576d2 392
2dc769a1 393 return 1;
bd3576d2 394 }
2f98abbc
AP
395
396#ifndef MD32_REG_T
30ab7af2 397#if defined(__alpha) || defined(__sparcv9) || defined(__mips)
2f98abbc
AP
398#define MD32_REG_T long
399/*
400 * This comment was originaly written for MD5, which is why it
401 * discusses A-D. But it basically applies to all 32-bit digests,
402 * which is why it was moved to common header file.
403 *
404 * In case you wonder why A-D are declared as long and not
405 * as MD5_LONG. Doing so results in slight performance
406 * boost on LP64 architectures. The catch is we don't
407 * really care if 32 MSBs of a 64-bit register get polluted
408 * with eventual overflows as we *save* only 32 LSBs in
409 * *either* case. Now declaring 'em long excuses the compiler
410 * from keeping 32 MSBs zeroed resulting in 13% performance
411 * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
412 * Well, to be honest it should say that this *prevents*
413 * performance degradation.
414 * <appro@fy.chalmers.se>
30ab7af2
AP
415 */
416#else
417/*
418 * Above is not absolute and there are LP64 compilers that
419 * generate better code if MD32_REG_T is defined int. The above
420 * pre-processor condition reflects the circumstances under which
421 * the conclusion was made and is subject to further extension.
2f98abbc
AP
422 * <appro@fy.chalmers.se>
423 */
30ab7af2
AP
424#define MD32_REG_T int
425#endif
2f98abbc 426#endif