xref: /netbsd/external/bsd/libbind/dist/dst/md5_locl.h (revision 6550d01e)
1 /*	$NetBSD: md5_locl.h,v 1.1.1.1 2009/04/12 15:33:31 christos Exp $	*/
2 
3 /* crypto/md/md5_locl.h */
4 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
5  * All rights reserved.
6  *
7  * This package is an SSL implementation written
8  * by Eric Young (eay@cryptsoft.com).
9  * The implementation was written so as to conform with Netscapes SSL.
10  *
11  * This library is free for commercial and non-commercial use as long as
12  * the following conditions are aheared to.  The following conditions
13  * apply to all code found in this distribution, be it the RC4, RSA,
14  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
15  * included with this distribution is covered by the same copyright terms
16  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17  *
18  * Copyright remains Eric Young's, and as such any Copyright notices in
19  * the code are not to be removed.
20  * If this package is used in a product, Eric Young should be given attribution
21  * as the author of the parts of the library used.
22  * This can be in the form of a textual message at program startup or
23  * in documentation (online or textual) provided with the package.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  * 3. All advertising materials mentioning features or use of this software
34  *    must display the following acknowledgement:
35  *    "This product includes cryptographic software written by
36  *     Eric Young (eay@cryptsoft.com)"
37  *    The word 'cryptographic' can be left out if the rouines from the library
38  *    being used are not cryptographic related :-).
39  * 4. If you include any Windows specific code (or a derivative thereof) from
40  *    the apps directory (application code) you must include an acknowledgement:
41  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42  *
43  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  *
55  * The licence and distribution terms for any publically available version or
56  * derivative of this code cannot be changed.  i.e. this code cannot simply be
57  * copied and put under another distribution licence
58  * [including the GNU Public Licence.]
59  */
60 
61 #include <stdlib.h>
62 #include <string.h>
63 #include "md5.h"
64 
65 #define ULONG	unsigned long
66 #define UCHAR	unsigned char
67 #define UINT	unsigned int
68 
69 #if defined(NOCONST)
70 #define const
71 #endif
72 
73 #undef c2l
74 #define c2l(c,l)	(l = ((unsigned long)(*((c)++)))     , \
75 			 l|=(((unsigned long)(*((c)++)))<< 8), \
76 			 l|=(((unsigned long)(*((c)++)))<<16), \
77 			 l|=(((unsigned long)(*((c)++)))<<24))
78 
79 #undef p_c2l
80 #define p_c2l(c,l,n)	{ \
81 			switch (n) { \
82 			case 0: l =((unsigned long)(*((c)++))); \
83 			case 1: l|=((unsigned long)(*((c)++)))<< 8; \
84 			case 2: l|=((unsigned long)(*((c)++)))<<16; \
85 			case 3: l|=((unsigned long)(*((c)++)))<<24; \
86 				} \
87 			}
88 
89 /* NOTE the pointer is not incremented at the end of this */
90 #undef c2l_p
91 #define c2l_p(c,l,n)	{ \
92 			l=0; \
93 			(c)+=n; \
94 			switch (n) { \
95 			case 3: l =((unsigned long)(*(--(c))))<<16; \
96 			case 2: l|=((unsigned long)(*(--(c))))<< 8; \
97 			case 1: l|=((unsigned long)(*(--(c))))    ; \
98 				} \
99 			}
100 
101 #undef p_c2l_p
102 #define p_c2l_p(c,l,sc,len) { \
103 			switch (sc) \
104 				{ \
105 			case 0: l =((unsigned long)(*((c)++))); \
106 				if (--len == 0U) break; \
107 			case 1: l|=((unsigned long)(*((c)++)))<< 8; \
108 				if (--len == 0U) break; \
109 			case 2: l|=((unsigned long)(*((c)++)))<<16; \
110 				} \
111 			}
112 
113 #undef l2c
114 #define l2c(l,c)	(*((c)++)=(unsigned char)(((l)    )&0xff), \
115 			 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
116 			 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
117 			 *((c)++)=(unsigned char)(((l)>>24)&0xff))
118 
119 /* NOTE - c is not incremented as per l2c */
120 #undef l2cn
121 #define l2cn(l1,l2,c,n)	{ \
122 			c+=n; \
123 			switch (n) { \
124 			case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
125 			case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
126 			case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
127 			case 5: *(--(c))=(unsigned char)(((l2)    )&0xff); \
128 			case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
129 			case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
130 			case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
131 			case 1: *(--(c))=(unsigned char)(((l1)    )&0xff); \
132 				} \
133 			}
134 
135 /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
136 #if defined(WIN32)
137 /* 5 instructions with rotate instruction, else 9 */
138 #define Endian_Reverse32(a) \
139 	{ \
140 	unsigned long l=(a); \
141 	(a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \
142 	}
143 #else
144 /* 6 instructions with rotate instruction, else 8 */
145 #define Endian_Reverse32(a) \
146 	{ \
147 	unsigned long l=(a); \
148 	l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \
149 	(a)=ROTATE(l,16L); \
150 	}
151 #endif
152 
153 /*%
154 #define	F(x,y,z)	(((x) & (y))  |  ((~(x)) & (z)))
155 #define	G(x,y,z)	(((x) & (z))  |  ((y) & (~(z))))
156 */
157 
158 /* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be
159  * simplified to the code below.  Wei attributes these optimisations
160  * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel.
161  */
162 #define	F(x,y,z)	((((y) ^ (z)) & (x)) ^ (z))
163 #define	G(x,y,z)	((((x) ^ (y)) & (z)) ^ (y))
164 #define	H(x,y,z)	((x) ^ (y) ^ (z))
165 #define	I(x,y,z)	(((x) | (~(z))) ^ (y))
166 
167 #undef ROTATE
168 #if defined(WIN32)
169 #define ROTATE(a,n)     _lrotl(a,n)
170 #else
171 #define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
172 #endif
173 
174 
175 #define R0(a,b,c,d,k,s,t) { \
176 	a+=((k)+(t)+F((b),(c),(d))); \
177 	a=ROTATE(a,s); \
178 	a+=b; };\
179 
180 #define R1(a,b,c,d,k,s,t) { \
181 	a+=((k)+(t)+G((b),(c),(d))); \
182 	a=ROTATE(a,s); \
183 	a+=b; };
184 
185 #define R2(a,b,c,d,k,s,t) { \
186 	a+=((k)+(t)+H((b),(c),(d))); \
187 	a=ROTATE(a,s); \
188 	a+=b; };
189 
190 #define R3(a,b,c,d,k,s,t) { \
191 	a+=((k)+(t)+I((b),(c),(d))); \
192 	a=ROTATE(a,s); \
193 	a+=b; };
194 
195 /*! \file */
196