1*472cd20dSToomas Soome /*
2*472cd20dSToomas Soome * Copyright (c) 2002-2019 Apple Inc. All rights reserved.
33b436d06SToomas Soome * Copyright (c) 2016 by Delphix. All rights reserved.
4c65ebfc7SToomas Soome *
5c65ebfc7SToomas Soome * Licensed under the Apache License, Version 2.0 (the "License");
6c65ebfc7SToomas Soome * you may not use this file except in compliance with the License.
7c65ebfc7SToomas Soome * You may obtain a copy of the License at
8c65ebfc7SToomas Soome *
9c65ebfc7SToomas Soome * http://www.apache.org/licenses/LICENSE-2.0
10c65ebfc7SToomas Soome *
11c65ebfc7SToomas Soome * Unless required by applicable law or agreed to in writing, software
12c65ebfc7SToomas Soome * distributed under the License is distributed on an "AS IS" BASIS,
13c65ebfc7SToomas Soome * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14c65ebfc7SToomas Soome * See the License for the specific language governing permissions and
15c65ebfc7SToomas Soome * limitations under the License.
16c65ebfc7SToomas Soome */
17c65ebfc7SToomas Soome
18c65ebfc7SToomas Soome #ifdef __cplusplus
19c65ebfc7SToomas Soome extern "C" {
20c65ebfc7SToomas Soome #endif
21c65ebfc7SToomas Soome
22c65ebfc7SToomas Soome #include "mDNSEmbeddedAPI.h"
23c65ebfc7SToomas Soome #include "DNSCommon.h"
24c65ebfc7SToomas Soome
25c65ebfc7SToomas Soome // Disable certain benign warnings with Microsoft compilers
26c65ebfc7SToomas Soome #if (defined(_MSC_VER))
27c65ebfc7SToomas Soome // Disable "conditional expression is constant" warning for debug macros.
28c65ebfc7SToomas Soome // Otherwise, this generates warnings for the perfectly natural construct "while(1)"
29c65ebfc7SToomas Soome // If someone knows a variant way of writing "while(1)" that doesn't generate warning messages, please let us know
30c65ebfc7SToomas Soome #pragma warning(disable:4127)
31c65ebfc7SToomas Soome #endif
32c65ebfc7SToomas Soome
33c65ebfc7SToomas Soome
34c65ebfc7SToomas Soome // ***************************************************************************
35c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
36c65ebfc7SToomas Soome #pragma mark - Byte Swapping Functions
37c65ebfc7SToomas Soome #endif
38c65ebfc7SToomas Soome
NToH16(mDNSu8 * bytes)39c65ebfc7SToomas Soome mDNSlocal mDNSu16 NToH16(mDNSu8 * bytes)
40c65ebfc7SToomas Soome {
41c65ebfc7SToomas Soome return (mDNSu16)((mDNSu16)bytes[0] << 8 | (mDNSu16)bytes[1]);
42c65ebfc7SToomas Soome }
43c65ebfc7SToomas Soome
NToH32(mDNSu8 * bytes)44c65ebfc7SToomas Soome mDNSlocal mDNSu32 NToH32(mDNSu8 * bytes)
45c65ebfc7SToomas Soome {
46c65ebfc7SToomas Soome return (mDNSu32)((mDNSu32) bytes[0] << 24 | (mDNSu32) bytes[1] << 16 | (mDNSu32) bytes[2] << 8 | (mDNSu32)bytes[3]);
47c65ebfc7SToomas Soome }
48c65ebfc7SToomas Soome
49c65ebfc7SToomas Soome // ***************************************************************************
50c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
51c65ebfc7SToomas Soome #pragma mark - MD5 Hash Functions
52c65ebfc7SToomas Soome #endif
53c65ebfc7SToomas Soome
54c65ebfc7SToomas Soome
55c65ebfc7SToomas Soome /* The source for the has is derived CommonCrypto files CommonDigest.h, md32_common.h, md5_locl.h, md5_locl.h, and openssl/md5.h.
56c65ebfc7SToomas Soome * The following changes have been made to the original sources:
57c65ebfc7SToomas Soome * replaced CC_LONG w/ mDNSu32
58c65ebfc7SToomas Soome * replaced CC_MD5* with MD5*
59c65ebfc7SToomas Soome * replaced CC_LONG w/ mDNSu32, removed conditional #defines from md5.h
60c65ebfc7SToomas Soome * removed extern decls for MD5_Init/Update/Final from CommonDigest.h
61c65ebfc7SToomas Soome * removed APPLE_COMMON_DIGEST specific #defines from md5_locl.h
62c65ebfc7SToomas Soome *
63c65ebfc7SToomas Soome * Note: machine archetecure specific conditionals from the original sources are turned off, but are left in the code
64c65ebfc7SToomas Soome * to aid in platform-specific optimizations and debugging.
65c65ebfc7SToomas Soome * Sources originally distributed under the following license headers:
66c65ebfc7SToomas Soome * CommonDigest.h - APSL
67c65ebfc7SToomas Soome *
68c65ebfc7SToomas Soome * md32_Common.h
69c65ebfc7SToomas Soome * ====================================================================
70c65ebfc7SToomas Soome * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
71c65ebfc7SToomas Soome *
72c65ebfc7SToomas Soome * Redistribution and use in source and binary forms, with or without
73c65ebfc7SToomas Soome * modification, are permitted provided that the following conditions
74c65ebfc7SToomas Soome * are met:
75c65ebfc7SToomas Soome *
76c65ebfc7SToomas Soome * 1. Redistributions of source code must retain the above copyright
77c65ebfc7SToomas Soome * notice, this list of conditions and the following disclaimer.
78c65ebfc7SToomas Soome *
79c65ebfc7SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
80c65ebfc7SToomas Soome * notice, this list of conditions and the following disclaimer in
81c65ebfc7SToomas Soome * the documentation and/or other materials provided with the
82c65ebfc7SToomas Soome * distribution.
83c65ebfc7SToomas Soome *
84c65ebfc7SToomas Soome * 3. All advertising materials mentioning features or use of this
85c65ebfc7SToomas Soome * software must display the following acknowledgment:
86c65ebfc7SToomas Soome * "This product includes software developed by the OpenSSL Project
87c65ebfc7SToomas Soome * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
88c65ebfc7SToomas Soome *
89c65ebfc7SToomas Soome * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
90c65ebfc7SToomas Soome * endorse or promote products derived from this software without
91c65ebfc7SToomas Soome * prior written permission. For written permission, please contact
92c65ebfc7SToomas Soome * licensing@OpenSSL.org.
93c65ebfc7SToomas Soome *
94c65ebfc7SToomas Soome * 5. Products derived from this software may not be called "OpenSSL"
95c65ebfc7SToomas Soome * nor may "OpenSSL" appear in their names without prior written
96c65ebfc7SToomas Soome * permission of the OpenSSL Project.
97c65ebfc7SToomas Soome *
98c65ebfc7SToomas Soome * 6. Redistributions of any form whatsoever must retain the following
99c65ebfc7SToomas Soome * acknowledgment:
100c65ebfc7SToomas Soome * "This product includes software developed by the OpenSSL Project
101c65ebfc7SToomas Soome * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
102c65ebfc7SToomas Soome *
103c65ebfc7SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
104c65ebfc7SToomas Soome * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
105c65ebfc7SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
106c65ebfc7SToomas Soome * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
107c65ebfc7SToomas Soome * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
108c65ebfc7SToomas Soome * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
109c65ebfc7SToomas Soome * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
110c65ebfc7SToomas Soome * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
111c65ebfc7SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
112c65ebfc7SToomas Soome * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113c65ebfc7SToomas Soome * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
114c65ebfc7SToomas Soome * OF THE POSSIBILITY OF SUCH DAMAGE.
115c65ebfc7SToomas Soome *
116c65ebfc7SToomas Soome *
117c65ebfc7SToomas Soome * md5_dgst.c, md5_locl.h
118c65ebfc7SToomas Soome * ====================================================================
119c65ebfc7SToomas Soome *
120c65ebfc7SToomas Soome * This product includes cryptographic software written by Eric Young
121c65ebfc7SToomas Soome * (eay@cryptsoft.com). This product includes software written by Tim
122c65ebfc7SToomas Soome * Hudson (tjh@cryptsoft.com).
123c65ebfc7SToomas Soome *
124c65ebfc7SToomas Soome * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
125c65ebfc7SToomas Soome * All rights reserved.
126c65ebfc7SToomas Soome *
127c65ebfc7SToomas Soome * This package is an SSL implementation written
128c65ebfc7SToomas Soome * by Eric Young (eay@cryptsoft.com).
129c65ebfc7SToomas Soome * The implementation was written so as to conform with Netscapes SSL.
130c65ebfc7SToomas Soome *
131c65ebfc7SToomas Soome * This library is free for commercial and non-commercial use as long as
132c65ebfc7SToomas Soome * the following conditions are aheared to. The following conditions
133c65ebfc7SToomas Soome * apply to all code found in this distribution, be it the RC4, RSA,
134c65ebfc7SToomas Soome * lhash, DES, etc., code; not just the SSL code. The SSL documentation
135c65ebfc7SToomas Soome * included with this distribution is covered by the same copyright terms
136c65ebfc7SToomas Soome * except that the holder is Tim Hudson (tjh@cryptsoft.com).
137c65ebfc7SToomas Soome *
138c65ebfc7SToomas Soome * Copyright remains Eric Young's, and as such any Copyright notices in
139c65ebfc7SToomas Soome * the code are not to be removed.
140c65ebfc7SToomas Soome * If this package is used in a product, Eric Young should be given attribution
141c65ebfc7SToomas Soome * as the author of the parts of the library used.
142c65ebfc7SToomas Soome * This can be in the form of a textual message at program startup or
143c65ebfc7SToomas Soome * in documentation (online or textual) provided with the package.
144c65ebfc7SToomas Soome *
145c65ebfc7SToomas Soome * Redistribution and use in source and binary forms, with or without
146c65ebfc7SToomas Soome * modification, are permitted provided that the following conditions
147c65ebfc7SToomas Soome * are met:
148c65ebfc7SToomas Soome * 1. Redistributions of source code must retain the copyright
149c65ebfc7SToomas Soome * notice, this list of conditions and the following disclaimer.
150c65ebfc7SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
151c65ebfc7SToomas Soome * notice, this list of conditions and the following disclaimer in the
152c65ebfc7SToomas Soome * documentation and/or other materials provided with the distribution.
153c65ebfc7SToomas Soome * 3. All advertising materials mentioning features or use of this software
154c65ebfc7SToomas Soome * must display the following acknowledgement:
155c65ebfc7SToomas Soome * "This product includes cryptographic software written by
156c65ebfc7SToomas Soome * Eric Young (eay@cryptsoft.com)"
157c65ebfc7SToomas Soome * The word 'cryptographic' can be left out if the rouines from the library
158c65ebfc7SToomas Soome * being used are not cryptographic related :-).
159c65ebfc7SToomas Soome * 4. If you include any Windows specific code (or a derivative thereof) from
160c65ebfc7SToomas Soome * the apps directory (application code) you must include an acknowledgement:
161c65ebfc7SToomas Soome * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
162c65ebfc7SToomas Soome *
163c65ebfc7SToomas Soome * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
164c65ebfc7SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
165c65ebfc7SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
166c65ebfc7SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
167c65ebfc7SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
168c65ebfc7SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
169c65ebfc7SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
170c65ebfc7SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
171c65ebfc7SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
172c65ebfc7SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
173c65ebfc7SToomas Soome * SUCH DAMAGE.
174c65ebfc7SToomas Soome *
175c65ebfc7SToomas Soome * The licence and distribution terms for any publically available version or
176c65ebfc7SToomas Soome * derivative of this code cannot be changed. i.e. this code cannot simply be
177c65ebfc7SToomas Soome * copied and put under another distribution licence
178c65ebfc7SToomas Soome * [including the GNU Public Licence.]
179c65ebfc7SToomas Soome *
180c65ebfc7SToomas Soome */
181c65ebfc7SToomas Soome
182c65ebfc7SToomas Soome //from CommonDigest.h
183c65ebfc7SToomas Soome
184c65ebfc7SToomas Soome
185c65ebfc7SToomas Soome
186c65ebfc7SToomas Soome // from openssl/md5.h
187c65ebfc7SToomas Soome
188c65ebfc7SToomas Soome #define MD5_CBLOCK 64
189c65ebfc7SToomas Soome #define MD5_LBLOCK (MD5_CBLOCK/4)
190c65ebfc7SToomas Soome #define MD5_DIGEST_LENGTH 16
191c65ebfc7SToomas Soome
192c65ebfc7SToomas Soome void MD5_Transform(MD5_CTX *c, const unsigned char *b);
193c65ebfc7SToomas Soome
194c65ebfc7SToomas Soome // From md5_locl.h
195c65ebfc7SToomas Soome
196c65ebfc7SToomas Soome #ifndef MD5_LONG_LOG2
197c65ebfc7SToomas Soome #define MD5_LONG_LOG2 2 /* default to 32 bits */
198c65ebfc7SToomas Soome #endif
199c65ebfc7SToomas Soome
200c65ebfc7SToomas Soome #ifdef MD5_ASM
201c65ebfc7SToomas Soome # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__INTEL__)
202c65ebfc7SToomas Soome # define md5_block_host_order md5_block_asm_host_order
203c65ebfc7SToomas Soome # elif defined(__sparc) && defined(OPENSSL_SYS_ULTRASPARC)
204c65ebfc7SToomas Soome void md5_block_asm_data_order_aligned (MD5_CTX *c, const mDNSu32 *p,int num);
205c65ebfc7SToomas Soome # define HASH_BLOCK_DATA_ORDER_ALIGNED md5_block_asm_data_order_aligned
206c65ebfc7SToomas Soome # endif
207c65ebfc7SToomas Soome #endif
208c65ebfc7SToomas Soome
209c65ebfc7SToomas Soome void md5_block_host_order (MD5_CTX *c, const void *p,int num);
210c65ebfc7SToomas Soome void md5_block_data_order (MD5_CTX *c, const void *p,int num);
211c65ebfc7SToomas Soome
212c65ebfc7SToomas Soome #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__INTEL__)
213c65ebfc7SToomas Soome /*
214c65ebfc7SToomas Soome * *_block_host_order is expected to handle aligned data while
215c65ebfc7SToomas Soome * *_block_data_order - unaligned. As algorithm and host (x86)
216c65ebfc7SToomas Soome * are in this case of the same "endianness" these two are
217c65ebfc7SToomas Soome * otherwise indistinguishable. But normally you don't want to
218c65ebfc7SToomas Soome * call the same function because unaligned access in places
219c65ebfc7SToomas Soome * where alignment is expected is usually a "Bad Thing". Indeed,
220c65ebfc7SToomas Soome * on RISCs you get punished with BUS ERROR signal or *severe*
221c65ebfc7SToomas Soome * performance degradation. Intel CPUs are in turn perfectly
222c65ebfc7SToomas Soome * capable of loading unaligned data without such drastic side
223c65ebfc7SToomas Soome * effect. Yes, they say it's slower than aligned load, but no
224c65ebfc7SToomas Soome * exception is generated and therefore performance degradation
225c65ebfc7SToomas Soome * is *incomparable* with RISCs. What we should weight here is
226c65ebfc7SToomas Soome * costs of unaligned access against costs of aligning data.
227c65ebfc7SToomas Soome * According to my measurements allowing unaligned access results
228c65ebfc7SToomas Soome * in ~9% performance improvement on Pentium II operating at
229c65ebfc7SToomas Soome * 266MHz. I won't be surprised if the difference will be higher
230c65ebfc7SToomas Soome * on faster systems:-)
231c65ebfc7SToomas Soome *
232c65ebfc7SToomas Soome * <appro@fy.chalmers.se>
233c65ebfc7SToomas Soome */
234c65ebfc7SToomas Soome #define md5_block_data_order md5_block_host_order
235c65ebfc7SToomas Soome #endif
236c65ebfc7SToomas Soome
237c65ebfc7SToomas Soome #define DATA_ORDER_IS_LITTLE_ENDIAN
238c65ebfc7SToomas Soome
239c65ebfc7SToomas Soome #define HASH_LONG mDNSu32
240c65ebfc7SToomas Soome #define HASH_LONG_LOG2 MD5_LONG_LOG2
241c65ebfc7SToomas Soome #define HASH_CTX MD5_CTX
242c65ebfc7SToomas Soome #define HASH_CBLOCK MD5_CBLOCK
243c65ebfc7SToomas Soome #define HASH_LBLOCK MD5_LBLOCK
244c65ebfc7SToomas Soome
245c65ebfc7SToomas Soome #define HASH_UPDATE MD5_Update
246c65ebfc7SToomas Soome #define HASH_TRANSFORM MD5_Transform
247c65ebfc7SToomas Soome #define HASH_FINAL MD5_Final
248c65ebfc7SToomas Soome
249c65ebfc7SToomas Soome #define HASH_MAKE_STRING(c,s) do { \
250c65ebfc7SToomas Soome unsigned long ll; \
251c65ebfc7SToomas Soome ll=(c)->A; HOST_l2c(ll,(s)); \
252c65ebfc7SToomas Soome ll=(c)->B; HOST_l2c(ll,(s)); \
253c65ebfc7SToomas Soome ll=(c)->C; HOST_l2c(ll,(s)); \
254c65ebfc7SToomas Soome ll=(c)->D; HOST_l2c(ll,(s)); \
255c65ebfc7SToomas Soome } while (0)
256c65ebfc7SToomas Soome #define HASH_BLOCK_HOST_ORDER md5_block_host_order
257c65ebfc7SToomas Soome #if !defined(L_ENDIAN) || defined(md5_block_data_order)
258c65ebfc7SToomas Soome #define HASH_BLOCK_DATA_ORDER md5_block_data_order
259c65ebfc7SToomas Soome /*
260c65ebfc7SToomas Soome * Little-endians (Intel and Alpha) feel better without this.
261c65ebfc7SToomas Soome * It looks like memcpy does better job than generic
262c65ebfc7SToomas Soome * md5_block_data_order on copying-n-aligning input data.
263c65ebfc7SToomas Soome * But frankly speaking I didn't expect such result on Alpha.
264c65ebfc7SToomas Soome * On the other hand I've got this with egcs-1.0.2 and if
265c65ebfc7SToomas Soome * program is compiled with another (better?) compiler it
266c65ebfc7SToomas Soome * might turn out other way around.
267c65ebfc7SToomas Soome *
268c65ebfc7SToomas Soome * <appro@fy.chalmers.se>
269c65ebfc7SToomas Soome */
270c65ebfc7SToomas Soome #endif
271c65ebfc7SToomas Soome
272c65ebfc7SToomas Soome
273c65ebfc7SToomas Soome // from md32_common.h
274c65ebfc7SToomas Soome
275c65ebfc7SToomas Soome /*
276c65ebfc7SToomas Soome * This is a generic 32 bit "collector" for message digest algorithms.
277c65ebfc7SToomas Soome * Whenever needed it collects input character stream into chunks of
278c65ebfc7SToomas Soome * 32 bit values and invokes a block function that performs actual hash
279c65ebfc7SToomas Soome * calculations.
280c65ebfc7SToomas Soome *
281c65ebfc7SToomas Soome * Porting guide.
282c65ebfc7SToomas Soome *
283c65ebfc7SToomas Soome * Obligatory macros:
284c65ebfc7SToomas Soome *
285c65ebfc7SToomas Soome * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
286c65ebfc7SToomas Soome * this macro defines byte order of input stream.
287c65ebfc7SToomas Soome * HASH_CBLOCK
288c65ebfc7SToomas Soome * size of a unit chunk HASH_BLOCK operates on.
289c65ebfc7SToomas Soome * HASH_LONG
290c65ebfc7SToomas Soome * has to be at lest 32 bit wide, if it's wider, then
291c65ebfc7SToomas Soome * HASH_LONG_LOG2 *has to* be defined along
292c65ebfc7SToomas Soome * HASH_CTX
293c65ebfc7SToomas Soome * context structure that at least contains following
294c65ebfc7SToomas Soome * members:
295c65ebfc7SToomas Soome * typedef struct {
296c65ebfc7SToomas Soome * ...
297c65ebfc7SToomas Soome * HASH_LONG Nl,Nh;
298c65ebfc7SToomas Soome * HASH_LONG data[HASH_LBLOCK];
299c65ebfc7SToomas Soome * int num;
300c65ebfc7SToomas Soome * ...
301c65ebfc7SToomas Soome * } HASH_CTX;
302c65ebfc7SToomas Soome * HASH_UPDATE
303c65ebfc7SToomas Soome * name of "Update" function, implemented here.
304c65ebfc7SToomas Soome * HASH_TRANSFORM
305c65ebfc7SToomas Soome * name of "Transform" function, implemented here.
306c65ebfc7SToomas Soome * HASH_FINAL
307c65ebfc7SToomas Soome * name of "Final" function, implemented here.
308c65ebfc7SToomas Soome * HASH_BLOCK_HOST_ORDER
309c65ebfc7SToomas Soome * name of "block" function treating *aligned* input message
310c65ebfc7SToomas Soome * in host byte order, implemented externally.
311c65ebfc7SToomas Soome * HASH_BLOCK_DATA_ORDER
312c65ebfc7SToomas Soome * name of "block" function treating *unaligned* input message
313c65ebfc7SToomas Soome * in original (data) byte order, implemented externally (it
314c65ebfc7SToomas Soome * actually is optional if data and host are of the same
315c65ebfc7SToomas Soome * "endianess").
316c65ebfc7SToomas Soome * HASH_MAKE_STRING
317c65ebfc7SToomas Soome * macro convering context variables to an ASCII hash string.
318c65ebfc7SToomas Soome *
319c65ebfc7SToomas Soome * Optional macros:
320c65ebfc7SToomas Soome *
321c65ebfc7SToomas Soome * B_ENDIAN or L_ENDIAN
322c65ebfc7SToomas Soome * defines host byte-order.
323c65ebfc7SToomas Soome * HASH_LONG_LOG2
324c65ebfc7SToomas Soome * defaults to 2 if not states otherwise.
325c65ebfc7SToomas Soome * HASH_LBLOCK
326c65ebfc7SToomas Soome * assumed to be HASH_CBLOCK/4 if not stated otherwise.
327c65ebfc7SToomas Soome * HASH_BLOCK_DATA_ORDER_ALIGNED
328c65ebfc7SToomas Soome * alternative "block" function capable of treating
329c65ebfc7SToomas Soome * aligned input message in original (data) order,
330c65ebfc7SToomas Soome * implemented externally.
331c65ebfc7SToomas Soome *
332c65ebfc7SToomas Soome * MD5 example:
333c65ebfc7SToomas Soome *
334c65ebfc7SToomas Soome * #define DATA_ORDER_IS_LITTLE_ENDIAN
335c65ebfc7SToomas Soome *
336c65ebfc7SToomas Soome * #define HASH_LONG mDNSu32
337c65ebfc7SToomas Soome * #define HASH_LONG_LOG2 mDNSu32_LOG2
338c65ebfc7SToomas Soome * #define HASH_CTX MD5_CTX
339c65ebfc7SToomas Soome * #define HASH_CBLOCK MD5_CBLOCK
340c65ebfc7SToomas Soome * #define HASH_LBLOCK MD5_LBLOCK
341c65ebfc7SToomas Soome * #define HASH_UPDATE MD5_Update
342c65ebfc7SToomas Soome * #define HASH_TRANSFORM MD5_Transform
343c65ebfc7SToomas Soome * #define HASH_FINAL MD5_Final
344c65ebfc7SToomas Soome * #define HASH_BLOCK_HOST_ORDER md5_block_host_order
345c65ebfc7SToomas Soome * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
346c65ebfc7SToomas Soome *
347c65ebfc7SToomas Soome * <appro@fy.chalmers.se>
348c65ebfc7SToomas Soome */
349c65ebfc7SToomas Soome
350c65ebfc7SToomas Soome #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
351c65ebfc7SToomas Soome #error "DATA_ORDER must be defined!"
352c65ebfc7SToomas Soome #endif
353c65ebfc7SToomas Soome
354c65ebfc7SToomas Soome #ifndef HASH_CBLOCK
355c65ebfc7SToomas Soome #error "HASH_CBLOCK must be defined!"
356c65ebfc7SToomas Soome #endif
357c65ebfc7SToomas Soome #ifndef HASH_LONG
358c65ebfc7SToomas Soome #error "HASH_LONG must be defined!"
359c65ebfc7SToomas Soome #endif
360c65ebfc7SToomas Soome #ifndef HASH_CTX
361c65ebfc7SToomas Soome #error "HASH_CTX must be defined!"
362c65ebfc7SToomas Soome #endif
363c65ebfc7SToomas Soome
364c65ebfc7SToomas Soome #ifndef HASH_UPDATE
365c65ebfc7SToomas Soome #error "HASH_UPDATE must be defined!"
366c65ebfc7SToomas Soome #endif
367c65ebfc7SToomas Soome #ifndef HASH_TRANSFORM
368c65ebfc7SToomas Soome #error "HASH_TRANSFORM must be defined!"
369c65ebfc7SToomas Soome #endif
370c65ebfc7SToomas Soome #ifndef HASH_FINAL
371c65ebfc7SToomas Soome #error "HASH_FINAL must be defined!"
372c65ebfc7SToomas Soome #endif
373c65ebfc7SToomas Soome
374c65ebfc7SToomas Soome #ifndef HASH_BLOCK_HOST_ORDER
375c65ebfc7SToomas Soome #error "HASH_BLOCK_HOST_ORDER must be defined!"
376c65ebfc7SToomas Soome #endif
377c65ebfc7SToomas Soome
378c65ebfc7SToomas Soome #if 0
379c65ebfc7SToomas Soome /*
380c65ebfc7SToomas Soome * Moved below as it's required only if HASH_BLOCK_DATA_ORDER_ALIGNED
381c65ebfc7SToomas Soome * isn't defined.
382c65ebfc7SToomas Soome */
383c65ebfc7SToomas Soome #ifndef HASH_BLOCK_DATA_ORDER
384c65ebfc7SToomas Soome #error "HASH_BLOCK_DATA_ORDER must be defined!"
385c65ebfc7SToomas Soome #endif
386c65ebfc7SToomas Soome #endif
387c65ebfc7SToomas Soome
388c65ebfc7SToomas Soome #ifndef HASH_LBLOCK
389c65ebfc7SToomas Soome #define HASH_LBLOCK (HASH_CBLOCK/4)
390c65ebfc7SToomas Soome #endif
391c65ebfc7SToomas Soome
392c65ebfc7SToomas Soome #ifndef HASH_LONG_LOG2
393c65ebfc7SToomas Soome #define HASH_LONG_LOG2 2
394c65ebfc7SToomas Soome #endif
395c65ebfc7SToomas Soome
396c65ebfc7SToomas Soome /*
397c65ebfc7SToomas Soome * Engage compiler specific rotate intrinsic function if available.
398c65ebfc7SToomas Soome */
399c65ebfc7SToomas Soome #undef ROTATE
400c65ebfc7SToomas Soome #ifndef PEDANTIC
401c65ebfc7SToomas Soome # if 0 /* defined(_MSC_VER) */
402c65ebfc7SToomas Soome # define ROTATE(a,n) _lrotl(a,n)
403c65ebfc7SToomas Soome # elif defined(__MWERKS__)
404c65ebfc7SToomas Soome # if defined(__POWERPC__)
405c65ebfc7SToomas Soome # define ROTATE(a,n) (unsigned MD32_REG_T)__rlwinm((int)a,n,0,31)
406c65ebfc7SToomas Soome # elif defined(__MC68K__)
407c65ebfc7SToomas Soome /* Motorola specific tweak. <appro@fy.chalmers.se> */
408c65ebfc7SToomas Soome # define ROTATE(a,n) (n<24 ? __rol(a,n) : __ror(a,32-n))
409c65ebfc7SToomas Soome # else
410c65ebfc7SToomas Soome # define ROTATE(a,n) __rol(a,n)
411c65ebfc7SToomas Soome # endif
412c65ebfc7SToomas Soome # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
413c65ebfc7SToomas Soome /*
414c65ebfc7SToomas Soome * Some GNU C inline assembler templates. Note that these are
415c65ebfc7SToomas Soome * rotates by *constant* number of bits! But that's exactly
416c65ebfc7SToomas Soome * what we need here...
417c65ebfc7SToomas Soome *
418c65ebfc7SToomas Soome * <appro@fy.chalmers.se>
419c65ebfc7SToomas Soome */
420c65ebfc7SToomas Soome /*
421c65ebfc7SToomas Soome * LLVM is more strict about compatibility of types between input & output constraints,
422c65ebfc7SToomas Soome * but we want these to be rotations of 32 bits, not 64, so we explicitly drop the
423c65ebfc7SToomas Soome * most significant bytes by casting to an unsigned int.
424c65ebfc7SToomas Soome */
425c65ebfc7SToomas Soome # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
426c65ebfc7SToomas Soome # define ROTATE(a,n) ({ register unsigned int ret; \
427c65ebfc7SToomas Soome asm ( \
428c65ebfc7SToomas Soome "roll %1,%0" \
429c65ebfc7SToomas Soome : "=r" (ret) \
430c65ebfc7SToomas Soome : "I" (n), "0" ((unsigned int)a) \
431c65ebfc7SToomas Soome : "cc"); \
432c65ebfc7SToomas Soome ret; \
433c65ebfc7SToomas Soome })
434c65ebfc7SToomas Soome # elif defined(__powerpc) || defined(__ppc)
435c65ebfc7SToomas Soome # define ROTATE(a,n) ({ register unsigned int ret; \
436c65ebfc7SToomas Soome asm ( \
437c65ebfc7SToomas Soome "rlwinm %0,%1,%2,0,31" \
438c65ebfc7SToomas Soome : "=r" (ret) \
439c65ebfc7SToomas Soome : "r" (a), "I" (n)); \
440c65ebfc7SToomas Soome ret; \
441c65ebfc7SToomas Soome })
442c65ebfc7SToomas Soome # endif
443c65ebfc7SToomas Soome # endif
444c65ebfc7SToomas Soome
445c65ebfc7SToomas Soome /*
446c65ebfc7SToomas Soome * Engage compiler specific "fetch in reverse byte order"
447c65ebfc7SToomas Soome * intrinsic function if available.
448c65ebfc7SToomas Soome */
449c65ebfc7SToomas Soome # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
450c65ebfc7SToomas Soome /* some GNU C inline assembler templates by <appro@fy.chalmers.se> */
451c65ebfc7SToomas Soome # if (defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)) && !defined(I386_ONLY)
452c65ebfc7SToomas Soome # define BE_FETCH32(a) ({ register unsigned int l=(a); \
453c65ebfc7SToomas Soome asm ( \
454c65ebfc7SToomas Soome "bswapl %0" \
455c65ebfc7SToomas Soome : "=r" (l) : "0" (l)); \
456c65ebfc7SToomas Soome l; \
457c65ebfc7SToomas Soome })
458c65ebfc7SToomas Soome # elif defined(__powerpc)
459c65ebfc7SToomas Soome # define LE_FETCH32(a) ({ register unsigned int l; \
460c65ebfc7SToomas Soome asm ( \
461c65ebfc7SToomas Soome "lwbrx %0,0,%1" \
462c65ebfc7SToomas Soome : "=r" (l) \
463c65ebfc7SToomas Soome : "r" (a)); \
464c65ebfc7SToomas Soome l; \
465c65ebfc7SToomas Soome })
466c65ebfc7SToomas Soome
467c65ebfc7SToomas Soome # elif defined(__sparc) && defined(OPENSSL_SYS_ULTRASPARC)
468c65ebfc7SToomas Soome # define LE_FETCH32(a) ({ register unsigned int l; \
469c65ebfc7SToomas Soome asm ( \
470c65ebfc7SToomas Soome "lda [%1]#ASI_PRIMARY_LITTLE,%0" \
471c65ebfc7SToomas Soome : "=r" (l) \
472c65ebfc7SToomas Soome : "r" (a)); \
473c65ebfc7SToomas Soome l; \
474c65ebfc7SToomas Soome })
475c65ebfc7SToomas Soome # endif
476c65ebfc7SToomas Soome # endif
477c65ebfc7SToomas Soome #endif /* PEDANTIC */
478c65ebfc7SToomas Soome
479c65ebfc7SToomas Soome #if HASH_LONG_LOG2==2 /* Engage only if sizeof(HASH_LONG)== 4 */
480c65ebfc7SToomas Soome /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
481c65ebfc7SToomas Soome #ifdef ROTATE
482c65ebfc7SToomas Soome /* 5 instructions with rotate instruction, else 9 */
483c65ebfc7SToomas Soome #define REVERSE_FETCH32(a,l) ( \
484c65ebfc7SToomas Soome l=*(const HASH_LONG *)(a), \
485c65ebfc7SToomas Soome ((ROTATE(l,8)&0x00FF00FF)|(ROTATE((l&0x00FF00FF),24))) \
486c65ebfc7SToomas Soome )
487c65ebfc7SToomas Soome #else
488c65ebfc7SToomas Soome /* 6 instructions with rotate instruction, else 8 */
489c65ebfc7SToomas Soome #define REVERSE_FETCH32(a,l) ( \
490c65ebfc7SToomas Soome l=*(const HASH_LONG *)(a), \
491c65ebfc7SToomas Soome l=(((l>>8)&0x00FF00FF)|((l&0x00FF00FF)<<8)), \
492c65ebfc7SToomas Soome ROTATE(l,16) \
493c65ebfc7SToomas Soome )
494c65ebfc7SToomas Soome /*
495c65ebfc7SToomas Soome * Originally the middle line started with l=(((l&0xFF00FF00)>>8)|...
496c65ebfc7SToomas Soome * It's rewritten as above for two reasons:
497c65ebfc7SToomas Soome * - RISCs aren't good at long constants and have to explicitely
498c65ebfc7SToomas Soome * compose 'em with several (well, usually 2) instructions in a
499c65ebfc7SToomas Soome * register before performing the actual operation and (as you
500c65ebfc7SToomas Soome * already realized:-) having same constant should inspire the
501c65ebfc7SToomas Soome * compiler to permanently allocate the only register for it;
502c65ebfc7SToomas Soome * - most modern CPUs have two ALUs, but usually only one has
503c65ebfc7SToomas Soome * circuitry for shifts:-( this minor tweak inspires compiler
504c65ebfc7SToomas Soome * to schedule shift instructions in a better way...
505c65ebfc7SToomas Soome *
506c65ebfc7SToomas Soome * <appro@fy.chalmers.se>
507c65ebfc7SToomas Soome */
508c65ebfc7SToomas Soome #endif
509c65ebfc7SToomas Soome #endif
510c65ebfc7SToomas Soome
511c65ebfc7SToomas Soome #ifndef ROTATE
512c65ebfc7SToomas Soome #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
513c65ebfc7SToomas Soome #endif
514c65ebfc7SToomas Soome
515c65ebfc7SToomas Soome /*
516c65ebfc7SToomas Soome * Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
517c65ebfc7SToomas Soome * and HASH_BLOCK_HOST_ORDER ought to be the same if input data
518c65ebfc7SToomas Soome * and host are of the same "endianess". It's possible to mask
519c65ebfc7SToomas Soome * this with blank #define HASH_BLOCK_DATA_ORDER though...
520c65ebfc7SToomas Soome *
521c65ebfc7SToomas Soome * <appro@fy.chalmers.se>
522c65ebfc7SToomas Soome */
523c65ebfc7SToomas Soome #if defined(B_ENDIAN)
524c65ebfc7SToomas Soome # if defined(DATA_ORDER_IS_BIG_ENDIAN)
525c65ebfc7SToomas Soome # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
526c65ebfc7SToomas Soome # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
527c65ebfc7SToomas Soome # endif
528c65ebfc7SToomas Soome # elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
529c65ebfc7SToomas Soome # ifndef HOST_FETCH32
530c65ebfc7SToomas Soome # ifdef LE_FETCH32
531c65ebfc7SToomas Soome # define HOST_FETCH32(p,l) LE_FETCH32(p)
532c65ebfc7SToomas Soome # elif defined(REVERSE_FETCH32)
533c65ebfc7SToomas Soome # define HOST_FETCH32(p,l) REVERSE_FETCH32(p,l)
534c65ebfc7SToomas Soome # endif
535c65ebfc7SToomas Soome # endif
536c65ebfc7SToomas Soome # endif
537c65ebfc7SToomas Soome #elif defined(L_ENDIAN)
538c65ebfc7SToomas Soome # if defined(DATA_ORDER_IS_LITTLE_ENDIAN)
539c65ebfc7SToomas Soome # if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
540c65ebfc7SToomas Soome # define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
541c65ebfc7SToomas Soome # endif
542c65ebfc7SToomas Soome # elif defined(DATA_ORDER_IS_BIG_ENDIAN)
543c65ebfc7SToomas Soome # ifndef HOST_FETCH32
544c65ebfc7SToomas Soome # ifdef BE_FETCH32
545c65ebfc7SToomas Soome # define HOST_FETCH32(p,l) BE_FETCH32(p)
546c65ebfc7SToomas Soome # elif defined(REVERSE_FETCH32)
547c65ebfc7SToomas Soome # define HOST_FETCH32(p,l) REVERSE_FETCH32(p,l)
548c65ebfc7SToomas Soome # endif
549c65ebfc7SToomas Soome # endif
550c65ebfc7SToomas Soome # endif
551c65ebfc7SToomas Soome #endif
552c65ebfc7SToomas Soome
553c65ebfc7SToomas Soome #if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
554c65ebfc7SToomas Soome #ifndef HASH_BLOCK_DATA_ORDER
555c65ebfc7SToomas Soome #error "HASH_BLOCK_DATA_ORDER must be defined!"
556c65ebfc7SToomas Soome #endif
557c65ebfc7SToomas Soome #endif
558c65ebfc7SToomas Soome
559c65ebfc7SToomas Soome // None of the invocations of the following macros actually use the result,
560c65ebfc7SToomas Soome // so cast them to void to avoid any compiler warnings/errors about not using
561c65ebfc7SToomas Soome // the result (e.g. when using clang).
562c65ebfc7SToomas Soome // If the resultant values need to be used at some point, these must be changed.
563c65ebfc7SToomas Soome #define HOST_c2l(c,l) ((void)_HOST_c2l(c,l))
564c65ebfc7SToomas Soome #define HOST_l2c(l,c) ((void)_HOST_l2c(l,c))
565c65ebfc7SToomas Soome
566c65ebfc7SToomas Soome #if defined(DATA_ORDER_IS_BIG_ENDIAN)
567c65ebfc7SToomas Soome
568c65ebfc7SToomas Soome #define _HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
569c65ebfc7SToomas Soome l|=(((unsigned long)(*((c)++)))<<16), \
570c65ebfc7SToomas Soome l|=(((unsigned long)(*((c)++)))<< 8), \
571c65ebfc7SToomas Soome l|=(((unsigned long)(*((c)++))) ), \
572c65ebfc7SToomas Soome l)
573c65ebfc7SToomas Soome #define HOST_p_c2l(c,l,n) { \
574c65ebfc7SToomas Soome switch (n) { \
575c65ebfc7SToomas Soome case 0: l =((unsigned long)(*((c)++)))<<24; \
576*472cd20dSToomas Soome /* FALLTHROUGH */ \
577c65ebfc7SToomas Soome case 1: l|=((unsigned long)(*((c)++)))<<16; \
578*472cd20dSToomas Soome /* FALLTHROUGH */ \
579c65ebfc7SToomas Soome case 2: l|=((unsigned long)(*((c)++)))<< 8; \
580*472cd20dSToomas Soome /* FALLTHROUGH */ \
581c65ebfc7SToomas Soome case 3: l|=((unsigned long)(*((c)++))); \
582c65ebfc7SToomas Soome } }
583c65ebfc7SToomas Soome #define HOST_p_c2l_p(c,l,sc,len) { \
584c65ebfc7SToomas Soome switch (sc) { \
585c65ebfc7SToomas Soome case 0: l =((unsigned long)(*((c)++)))<<24; \
586c65ebfc7SToomas Soome if (--len == 0) break; \
587*472cd20dSToomas Soome /* FALLTHROUGH */ \
588c65ebfc7SToomas Soome case 1: l|=((unsigned long)(*((c)++)))<<16; \
589c65ebfc7SToomas Soome if (--len == 0) break; \
590*472cd20dSToomas Soome /* FALLTHROUGH */ \
591c65ebfc7SToomas Soome case 2: l|=((unsigned long)(*((c)++)))<< 8; \
592c65ebfc7SToomas Soome } }
593c65ebfc7SToomas Soome /* NOTE the pointer is not incremented at the end of this */
594c65ebfc7SToomas Soome #define HOST_c2l_p(c,l,n) { \
595c65ebfc7SToomas Soome l=0; (c)+=n; \
596c65ebfc7SToomas Soome switch (n) { \
597c65ebfc7SToomas Soome case 3: l =((unsigned long)(*(--(c))))<< 8; \
598*472cd20dSToomas Soome /* FALLTHROUGH */ \
599c65ebfc7SToomas Soome case 2: l|=((unsigned long)(*(--(c))))<<16; \
600*472cd20dSToomas Soome /* FALLTHROUGH */ \
601c65ebfc7SToomas Soome case 1: l|=((unsigned long)(*(--(c))))<<24; \
602c65ebfc7SToomas Soome } }
603c65ebfc7SToomas Soome #define _HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
604c65ebfc7SToomas Soome *((c)++)=(unsigned char)(((l)>>16)&0xff), \
605c65ebfc7SToomas Soome *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
606c65ebfc7SToomas Soome *((c)++)=(unsigned char)(((l) )&0xff), \
607c65ebfc7SToomas Soome l)
608c65ebfc7SToomas Soome
609c65ebfc7SToomas Soome #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
610c65ebfc7SToomas Soome
611c65ebfc7SToomas Soome #define _HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
612c65ebfc7SToomas Soome l|=(((unsigned long)(*((c)++)))<< 8), \
613c65ebfc7SToomas Soome l|=(((unsigned long)(*((c)++)))<<16), \
614c65ebfc7SToomas Soome l|=(((unsigned long)(*((c)++)))<<24), \
615c65ebfc7SToomas Soome l)
616c65ebfc7SToomas Soome #define HOST_p_c2l(c,l,n) { \
617c65ebfc7SToomas Soome switch (n) { \
618c65ebfc7SToomas Soome case 0: l =((unsigned long)(*((c)++))); \
619c65ebfc7SToomas Soome /* FALLTHROUGH */ \
620c65ebfc7SToomas Soome case 1: l|=((unsigned long)(*((c)++)))<< 8; \
621c65ebfc7SToomas Soome /* FALLTHROUGH */ \
622c65ebfc7SToomas Soome case 2: l|=((unsigned long)(*((c)++)))<<16; \
623c65ebfc7SToomas Soome /* FALLTHROUGH */ \
624c65ebfc7SToomas Soome case 3: l|=((unsigned long)(*((c)++)))<<24; \
625c65ebfc7SToomas Soome } }
626c65ebfc7SToomas Soome #define HOST_p_c2l_p(c,l,sc,len) { \
627c65ebfc7SToomas Soome switch (sc) { \
628c65ebfc7SToomas Soome case 0: l =((unsigned long)(*((c)++))); \
629c65ebfc7SToomas Soome if (--len == 0) break; \
630c65ebfc7SToomas Soome /* FALLTHROUGH */ \
631c65ebfc7SToomas Soome case 1: l|=((unsigned long)(*((c)++)))<< 8; \
632c65ebfc7SToomas Soome if (--len == 0) break; \
633c65ebfc7SToomas Soome /* FALLTHROUGH */ \
634c65ebfc7SToomas Soome case 2: l|=((unsigned long)(*((c)++)))<<16; \
635c65ebfc7SToomas Soome } }
636c65ebfc7SToomas Soome /* NOTE the pointer is not incremented at the end of this */
637c65ebfc7SToomas Soome #define HOST_c2l_p(c,l,n) { \
638c65ebfc7SToomas Soome l=0; (c)+=n; \
639c65ebfc7SToomas Soome switch (n) { \
640c65ebfc7SToomas Soome case 3: l =((unsigned long)(*(--(c))))<<16; \
641c65ebfc7SToomas Soome /* FALLTHROUGH */ \
642c65ebfc7SToomas Soome case 2: l|=((unsigned long)(*(--(c))))<< 8; \
643c65ebfc7SToomas Soome /* FALLTHROUGH */ \
644c65ebfc7SToomas Soome case 1: l|=((unsigned long)(*(--(c)))); \
645c65ebfc7SToomas Soome } }
646c65ebfc7SToomas Soome #define _HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
647c65ebfc7SToomas Soome *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
648c65ebfc7SToomas Soome *((c)++)=(unsigned char)(((l)>>16)&0xff), \
649c65ebfc7SToomas Soome *((c)++)=(unsigned char)(((l)>>24)&0xff), \
650c65ebfc7SToomas Soome l)
651c65ebfc7SToomas Soome
652c65ebfc7SToomas Soome #endif
653c65ebfc7SToomas Soome
654c65ebfc7SToomas Soome /*
655c65ebfc7SToomas Soome * Time for some action:-)
656c65ebfc7SToomas Soome */
657c65ebfc7SToomas Soome
HASH_UPDATE(HASH_CTX * c,const void * data_,unsigned long len)658c65ebfc7SToomas Soome int HASH_UPDATE (HASH_CTX *c, const void *data_, unsigned long len)
659c65ebfc7SToomas Soome {
660c65ebfc7SToomas Soome const unsigned char *data=(const unsigned char *)data_;
661*472cd20dSToomas Soome const unsigned char * const data_end=(const unsigned char *)data_;
662c65ebfc7SToomas Soome register HASH_LONG * p;
663c65ebfc7SToomas Soome register unsigned long l;
664c65ebfc7SToomas Soome int sw,sc,ew,ec;
665c65ebfc7SToomas Soome
666c65ebfc7SToomas Soome if (len==0) return 1;
667c65ebfc7SToomas Soome
668c65ebfc7SToomas Soome l=(c->Nl+(len<<3))&0xffffffffL;
669c65ebfc7SToomas Soome /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
670c65ebfc7SToomas Soome * Wei Dai <weidai@eskimo.com> for pointing it out. */
671c65ebfc7SToomas Soome if (l < c->Nl) /* overflow */
672c65ebfc7SToomas Soome c->Nh++;
673c65ebfc7SToomas Soome c->Nh+=(len>>29);
674c65ebfc7SToomas Soome c->Nl=l;
675c65ebfc7SToomas Soome
676c65ebfc7SToomas Soome if (c->num != 0)
677c65ebfc7SToomas Soome {
678c65ebfc7SToomas Soome p=c->data;
679c65ebfc7SToomas Soome sw=c->num>>2;
680c65ebfc7SToomas Soome sc=c->num&0x03;
681c65ebfc7SToomas Soome
682c65ebfc7SToomas Soome if ((c->num+len) >= HASH_CBLOCK)
683c65ebfc7SToomas Soome {
684c65ebfc7SToomas Soome l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
685*472cd20dSToomas Soome for (; (sw < HASH_LBLOCK) && ((data_end - data) >= 4); sw++)
686c65ebfc7SToomas Soome {
687c65ebfc7SToomas Soome HOST_c2l(data,l); p[sw]=l;
688c65ebfc7SToomas Soome }
689c65ebfc7SToomas Soome HASH_BLOCK_HOST_ORDER (c,p,1);
690c65ebfc7SToomas Soome len-=(HASH_CBLOCK-c->num);
691c65ebfc7SToomas Soome c->num=0;
692c65ebfc7SToomas Soome /* drop through and do the rest */
693c65ebfc7SToomas Soome }
694c65ebfc7SToomas Soome else
695c65ebfc7SToomas Soome {
696c65ebfc7SToomas Soome c->num+=len;
697c65ebfc7SToomas Soome if ((sc+len) < 4) /* ugly, add char's to a word */
698c65ebfc7SToomas Soome {
699c65ebfc7SToomas Soome l=p[sw]; HOST_p_c2l_p(data,l,sc,len); p[sw]=l;
700c65ebfc7SToomas Soome }
701c65ebfc7SToomas Soome else
702c65ebfc7SToomas Soome {
703c65ebfc7SToomas Soome ew=(c->num>>2);
704c65ebfc7SToomas Soome ec=(c->num&0x03);
705c65ebfc7SToomas Soome if (sc)
706c65ebfc7SToomas Soome l=p[sw];
707c65ebfc7SToomas Soome HOST_p_c2l(data,l,sc);
708c65ebfc7SToomas Soome p[sw++]=l;
709*472cd20dSToomas Soome for (; (sw < ew) && ((data_end - data) >= 4); sw++)
710c65ebfc7SToomas Soome {
711c65ebfc7SToomas Soome HOST_c2l(data,l); p[sw]=l;
712c65ebfc7SToomas Soome }
713c65ebfc7SToomas Soome if (ec)
714c65ebfc7SToomas Soome {
715c65ebfc7SToomas Soome HOST_c2l_p(data,l,ec); p[sw]=l;
716c65ebfc7SToomas Soome }
717c65ebfc7SToomas Soome }
718c65ebfc7SToomas Soome return 1;
719c65ebfc7SToomas Soome }
720c65ebfc7SToomas Soome }
721c65ebfc7SToomas Soome
722c65ebfc7SToomas Soome sw=(int)(len/HASH_CBLOCK);
723c65ebfc7SToomas Soome if (sw > 0)
724c65ebfc7SToomas Soome {
725c65ebfc7SToomas Soome #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
726c65ebfc7SToomas Soome /*
727c65ebfc7SToomas Soome * Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
728c65ebfc7SToomas Soome * only if sizeof(HASH_LONG)==4.
729c65ebfc7SToomas Soome */
730c65ebfc7SToomas Soome if ((((unsigned long)data)%4) == 0)
731c65ebfc7SToomas Soome {
732c65ebfc7SToomas Soome /* data is properly aligned so that we can cast it: */
733c65ebfc7SToomas Soome HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,sw);
734c65ebfc7SToomas Soome sw*=HASH_CBLOCK;
735c65ebfc7SToomas Soome data+=sw;
736c65ebfc7SToomas Soome len-=sw;
737c65ebfc7SToomas Soome }
738c65ebfc7SToomas Soome else
739c65ebfc7SToomas Soome #if !defined(HASH_BLOCK_DATA_ORDER)
740c65ebfc7SToomas Soome while (sw--)
741c65ebfc7SToomas Soome {
742c65ebfc7SToomas Soome mDNSPlatformMemCopy(p=c->data,data,HASH_CBLOCK);
743c65ebfc7SToomas Soome HASH_BLOCK_DATA_ORDER_ALIGNED(c,p,1);
744c65ebfc7SToomas Soome data+=HASH_CBLOCK;
745c65ebfc7SToomas Soome len-=HASH_CBLOCK;
746c65ebfc7SToomas Soome }
747c65ebfc7SToomas Soome #endif
748c65ebfc7SToomas Soome #endif
749c65ebfc7SToomas Soome #if defined(HASH_BLOCK_DATA_ORDER)
750c65ebfc7SToomas Soome {
751c65ebfc7SToomas Soome HASH_BLOCK_DATA_ORDER(c,data,sw);
752c65ebfc7SToomas Soome sw*=HASH_CBLOCK;
753c65ebfc7SToomas Soome data+=sw;
754c65ebfc7SToomas Soome len-=sw;
755c65ebfc7SToomas Soome }
756c65ebfc7SToomas Soome #endif
757c65ebfc7SToomas Soome }
758c65ebfc7SToomas Soome
759c65ebfc7SToomas Soome if (len!=0)
760c65ebfc7SToomas Soome {
761c65ebfc7SToomas Soome p = c->data;
762c65ebfc7SToomas Soome c->num = (int)len;
763c65ebfc7SToomas Soome ew=(int)(len>>2); /* words to copy */
764c65ebfc7SToomas Soome ec=(int)(len&0x03);
765*472cd20dSToomas Soome for (; ew && ((data_end - data) >= 4); ew--,p++)
766c65ebfc7SToomas Soome {
767c65ebfc7SToomas Soome HOST_c2l(data,l); *p=l;
768c65ebfc7SToomas Soome }
769c65ebfc7SToomas Soome HOST_c2l_p(data,l,ec);
770c65ebfc7SToomas Soome *p=l;
771c65ebfc7SToomas Soome }
772c65ebfc7SToomas Soome return 1;
773c65ebfc7SToomas Soome }
774c65ebfc7SToomas Soome
775c65ebfc7SToomas Soome
HASH_TRANSFORM(HASH_CTX * c,const unsigned char * data)776c65ebfc7SToomas Soome void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
777c65ebfc7SToomas Soome {
778c65ebfc7SToomas Soome #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
779c65ebfc7SToomas Soome if ((((unsigned long)data)%4) == 0)
780c65ebfc7SToomas Soome /* data is properly aligned so that we can cast it: */
781c65ebfc7SToomas Soome HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,1);
782c65ebfc7SToomas Soome else
783c65ebfc7SToomas Soome #if !defined(HASH_BLOCK_DATA_ORDER)
784c65ebfc7SToomas Soome {
785c65ebfc7SToomas Soome mDNSPlatformMemCopy(c->data,data,HASH_CBLOCK);
786c65ebfc7SToomas Soome HASH_BLOCK_DATA_ORDER_ALIGNED (c,c->data,1);
787c65ebfc7SToomas Soome }
788c65ebfc7SToomas Soome #endif
789c65ebfc7SToomas Soome #endif
790c65ebfc7SToomas Soome #if defined(HASH_BLOCK_DATA_ORDER)
791c65ebfc7SToomas Soome HASH_BLOCK_DATA_ORDER (c,data,1);
792c65ebfc7SToomas Soome #endif
793c65ebfc7SToomas Soome }
794c65ebfc7SToomas Soome
795c65ebfc7SToomas Soome
HASH_FINAL(unsigned char * md,HASH_CTX * c)796c65ebfc7SToomas Soome int HASH_FINAL (unsigned char *md, HASH_CTX *c)
797c65ebfc7SToomas Soome {
798c65ebfc7SToomas Soome register HASH_LONG *p;
799c65ebfc7SToomas Soome register unsigned long l;
800c65ebfc7SToomas Soome register int i,j;
801c65ebfc7SToomas Soome static const unsigned char end[4]={0x80,0x00,0x00,0x00};
802c65ebfc7SToomas Soome const unsigned char *cp=end;
803c65ebfc7SToomas Soome
804c65ebfc7SToomas Soome /* c->num should definitly have room for at least one more byte. */
805c65ebfc7SToomas Soome p=c->data;
806c65ebfc7SToomas Soome i=c->num>>2;
807c65ebfc7SToomas Soome j=c->num&0x03;
808c65ebfc7SToomas Soome
809c65ebfc7SToomas Soome #if 0
810c65ebfc7SToomas Soome /* purify often complains about the following line as an
811c65ebfc7SToomas Soome * Uninitialized Memory Read. While this can be true, the
812c65ebfc7SToomas Soome * following p_c2l macro will reset l when that case is true.
813c65ebfc7SToomas Soome * This is because j&0x03 contains the number of 'valid' bytes
814c65ebfc7SToomas Soome * already in p[i]. If and only if j&0x03 == 0, the UMR will
815c65ebfc7SToomas Soome * occur but this is also the only time p_c2l will do
816c65ebfc7SToomas Soome * l= *(cp++) instead of l|= *(cp++)
817c65ebfc7SToomas Soome * Many thanks to Alex Tang <altitude@cic.net> for pickup this
818c65ebfc7SToomas Soome * 'potential bug' */
819c65ebfc7SToomas Soome #ifdef PURIFY
820c65ebfc7SToomas Soome if (j==0) p[i]=0; /* Yeah, but that's not the way to fix it:-) */
821c65ebfc7SToomas Soome #endif
822c65ebfc7SToomas Soome l=p[i];
823c65ebfc7SToomas Soome #else
824c65ebfc7SToomas Soome l = (j==0) ? 0 : p[i];
825c65ebfc7SToomas Soome #endif
826c65ebfc7SToomas Soome HOST_p_c2l(cp,l,j); p[i++]=l; /* i is the next 'undefined word' */
827c65ebfc7SToomas Soome
828c65ebfc7SToomas Soome if (i>(HASH_LBLOCK-2)) /* save room for Nl and Nh */
829c65ebfc7SToomas Soome {
830c65ebfc7SToomas Soome if (i<HASH_LBLOCK) p[i]=0;
831c65ebfc7SToomas Soome HASH_BLOCK_HOST_ORDER (c,p,1);
832c65ebfc7SToomas Soome i=0;
833c65ebfc7SToomas Soome }
834c65ebfc7SToomas Soome for (; i<(HASH_LBLOCK-2); i++)
835c65ebfc7SToomas Soome p[i]=0;
836c65ebfc7SToomas Soome
837c65ebfc7SToomas Soome #if defined(DATA_ORDER_IS_BIG_ENDIAN)
838c65ebfc7SToomas Soome p[HASH_LBLOCK-2]=c->Nh;
839c65ebfc7SToomas Soome p[HASH_LBLOCK-1]=c->Nl;
840c65ebfc7SToomas Soome #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
841c65ebfc7SToomas Soome p[HASH_LBLOCK-2]=c->Nl;
842c65ebfc7SToomas Soome p[HASH_LBLOCK-1]=c->Nh;
843c65ebfc7SToomas Soome #endif
844c65ebfc7SToomas Soome HASH_BLOCK_HOST_ORDER (c,p,1);
845c65ebfc7SToomas Soome
846c65ebfc7SToomas Soome #ifndef HASH_MAKE_STRING
847c65ebfc7SToomas Soome #error "HASH_MAKE_STRING must be defined!"
848c65ebfc7SToomas Soome #else
849c65ebfc7SToomas Soome HASH_MAKE_STRING(c,md);
850c65ebfc7SToomas Soome #endif
851c65ebfc7SToomas Soome
852c65ebfc7SToomas Soome c->num=0;
853c65ebfc7SToomas Soome /* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
854c65ebfc7SToomas Soome * but I'm not worried :-)
855c65ebfc7SToomas Soome OPENSSL_cleanse((void *)c,sizeof(HASH_CTX));
856c65ebfc7SToomas Soome */
857c65ebfc7SToomas Soome return 1;
858c65ebfc7SToomas Soome }
859c65ebfc7SToomas Soome
860c65ebfc7SToomas Soome #ifndef MD32_REG_T
861c65ebfc7SToomas Soome #define MD32_REG_T long
862c65ebfc7SToomas Soome /*
863c65ebfc7SToomas Soome * This comment was originaly written for MD5, which is why it
864c65ebfc7SToomas Soome * discusses A-D. But it basically applies to all 32-bit digests,
865c65ebfc7SToomas Soome * which is why it was moved to common header file.
866c65ebfc7SToomas Soome *
867c65ebfc7SToomas Soome * In case you wonder why A-D are declared as long and not
868c65ebfc7SToomas Soome * as mDNSu32. Doing so results in slight performance
869c65ebfc7SToomas Soome * boost on LP64 architectures. The catch is we don't
870c65ebfc7SToomas Soome * really care if 32 MSBs of a 64-bit register get polluted
871c65ebfc7SToomas Soome * with eventual overflows as we *save* only 32 LSBs in
872c65ebfc7SToomas Soome * *either* case. Now declaring 'em long excuses the compiler
873c65ebfc7SToomas Soome * from keeping 32 MSBs zeroed resulting in 13% performance
874c65ebfc7SToomas Soome * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
875c65ebfc7SToomas Soome * Well, to be honest it should say that this *prevents*
876c65ebfc7SToomas Soome * performance degradation.
877c65ebfc7SToomas Soome * <appro@fy.chalmers.se>
878c65ebfc7SToomas Soome * Apparently there're LP64 compilers that generate better
879c65ebfc7SToomas Soome * code if A-D are declared int. Most notably GCC-x86_64
880c65ebfc7SToomas Soome * generates better code.
881c65ebfc7SToomas Soome * <appro@fy.chalmers.se>
882c65ebfc7SToomas Soome */
883c65ebfc7SToomas Soome #endif
884c65ebfc7SToomas Soome
885c65ebfc7SToomas Soome
886c65ebfc7SToomas Soome // from md5_locl.h (continued)
887c65ebfc7SToomas Soome
888c65ebfc7SToomas Soome /*
889c65ebfc7SToomas Soome #define F(x,y,z) (((x) & (y)) | ((~(x)) & (z)))
890c65ebfc7SToomas Soome #define G(x,y,z) (((x) & (z)) | ((y) & (~(z))))
891c65ebfc7SToomas Soome */
892c65ebfc7SToomas Soome
893c65ebfc7SToomas Soome /* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be
894c65ebfc7SToomas Soome * simplified to the code below. Wei attributes these optimizations
895c65ebfc7SToomas Soome * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel.
896c65ebfc7SToomas Soome */
897c65ebfc7SToomas Soome #define F(b,c,d) ((((c) ^ (d)) & (b)) ^ (d))
898c65ebfc7SToomas Soome #define G(b,c,d) ((((b) ^ (c)) & (d)) ^ (c))
899c65ebfc7SToomas Soome #define H(b,c,d) ((b) ^ (c) ^ (d))
900c65ebfc7SToomas Soome #define I(b,c,d) (((~(d)) | (b)) ^ (c))
901c65ebfc7SToomas Soome
902c65ebfc7SToomas Soome #define R0(a,b,c,d,k,s,t) { \
903c65ebfc7SToomas Soome a+=((k)+(t)+F((b),(c),(d))); \
904c65ebfc7SToomas Soome a=ROTATE(a,s); \
905c65ebfc7SToomas Soome a+=b; }; \
906c65ebfc7SToomas Soome
907c65ebfc7SToomas Soome #define R1(a,b,c,d,k,s,t) { \
908c65ebfc7SToomas Soome a+=((k)+(t)+G((b),(c),(d))); \
909c65ebfc7SToomas Soome a=ROTATE(a,s); \
910c65ebfc7SToomas Soome a+=b; };
911c65ebfc7SToomas Soome
912c65ebfc7SToomas Soome #define R2(a,b,c,d,k,s,t) { \
913c65ebfc7SToomas Soome a+=((k)+(t)+H((b),(c),(d))); \
914c65ebfc7SToomas Soome a=ROTATE(a,s); \
915c65ebfc7SToomas Soome a+=b; };
916c65ebfc7SToomas Soome
917c65ebfc7SToomas Soome #define R3(a,b,c,d,k,s,t) { \
918c65ebfc7SToomas Soome a+=((k)+(t)+I((b),(c),(d))); \
919c65ebfc7SToomas Soome a=ROTATE(a,s); \
920c65ebfc7SToomas Soome a+=b; };
921c65ebfc7SToomas Soome
922c65ebfc7SToomas Soome // from md5_dgst.c
923c65ebfc7SToomas Soome
924c65ebfc7SToomas Soome
925c65ebfc7SToomas Soome /* Implemented from RFC1321 The MD5 Message-Digest Algorithm
926c65ebfc7SToomas Soome */
927c65ebfc7SToomas Soome
928c65ebfc7SToomas Soome #define INIT_DATA_A (unsigned long)0x67452301L
929c65ebfc7SToomas Soome #define INIT_DATA_B (unsigned long)0xefcdab89L
930c65ebfc7SToomas Soome #define INIT_DATA_C (unsigned long)0x98badcfeL
931c65ebfc7SToomas Soome #define INIT_DATA_D (unsigned long)0x10325476L
932c65ebfc7SToomas Soome
MD5_Init(MD5_CTX * c)933c65ebfc7SToomas Soome int MD5_Init(MD5_CTX *c)
934c65ebfc7SToomas Soome {
935c65ebfc7SToomas Soome c->A=INIT_DATA_A;
936c65ebfc7SToomas Soome c->B=INIT_DATA_B;
937c65ebfc7SToomas Soome c->C=INIT_DATA_C;
938c65ebfc7SToomas Soome c->D=INIT_DATA_D;
939c65ebfc7SToomas Soome c->Nl=0;
940c65ebfc7SToomas Soome c->Nh=0;
941c65ebfc7SToomas Soome c->num=0;
942c65ebfc7SToomas Soome return 1;
943c65ebfc7SToomas Soome }
944c65ebfc7SToomas Soome
945c65ebfc7SToomas Soome #ifndef md5_block_host_order
md5_block_host_order(MD5_CTX * c,const void * data,int num)946c65ebfc7SToomas Soome void md5_block_host_order (MD5_CTX *c, const void *data, int num)
947c65ebfc7SToomas Soome {
948c65ebfc7SToomas Soome const mDNSu32 *X=(const mDNSu32 *)data;
949c65ebfc7SToomas Soome register unsigned MD32_REG_T A,B,C,D;
950c65ebfc7SToomas Soome
951c65ebfc7SToomas Soome A=c->A;
952c65ebfc7SToomas Soome B=c->B;
953c65ebfc7SToomas Soome C=c->C;
954c65ebfc7SToomas Soome D=c->D;
955c65ebfc7SToomas Soome
956c65ebfc7SToomas Soome for (; num--; X+=HASH_LBLOCK)
957c65ebfc7SToomas Soome {
958c65ebfc7SToomas Soome /* Round 0 */
959c65ebfc7SToomas Soome R0(A,B,C,D,X[ 0], 7,0xd76aa478L);
960c65ebfc7SToomas Soome R0(D,A,B,C,X[ 1],12,0xe8c7b756L);
961c65ebfc7SToomas Soome R0(C,D,A,B,X[ 2],17,0x242070dbL);
962c65ebfc7SToomas Soome R0(B,C,D,A,X[ 3],22,0xc1bdceeeL);
963c65ebfc7SToomas Soome R0(A,B,C,D,X[ 4], 7,0xf57c0fafL);
964c65ebfc7SToomas Soome R0(D,A,B,C,X[ 5],12,0x4787c62aL);
965c65ebfc7SToomas Soome R0(C,D,A,B,X[ 6],17,0xa8304613L);
966c65ebfc7SToomas Soome R0(B,C,D,A,X[ 7],22,0xfd469501L);
967c65ebfc7SToomas Soome R0(A,B,C,D,X[ 8], 7,0x698098d8L);
968c65ebfc7SToomas Soome R0(D,A,B,C,X[ 9],12,0x8b44f7afL);
969c65ebfc7SToomas Soome R0(C,D,A,B,X[10],17,0xffff5bb1L);
970c65ebfc7SToomas Soome R0(B,C,D,A,X[11],22,0x895cd7beL);
971c65ebfc7SToomas Soome R0(A,B,C,D,X[12], 7,0x6b901122L);
972c65ebfc7SToomas Soome R0(D,A,B,C,X[13],12,0xfd987193L);
973c65ebfc7SToomas Soome R0(C,D,A,B,X[14],17,0xa679438eL);
974c65ebfc7SToomas Soome R0(B,C,D,A,X[15],22,0x49b40821L);
975c65ebfc7SToomas Soome /* Round 1 */
976c65ebfc7SToomas Soome R1(A,B,C,D,X[ 1], 5,0xf61e2562L);
977c65ebfc7SToomas Soome R1(D,A,B,C,X[ 6], 9,0xc040b340L);
978c65ebfc7SToomas Soome R1(C,D,A,B,X[11],14,0x265e5a51L);
979c65ebfc7SToomas Soome R1(B,C,D,A,X[ 0],20,0xe9b6c7aaL);
980c65ebfc7SToomas Soome R1(A,B,C,D,X[ 5], 5,0xd62f105dL);
981c65ebfc7SToomas Soome R1(D,A,B,C,X[10], 9,0x02441453L);
982c65ebfc7SToomas Soome R1(C,D,A,B,X[15],14,0xd8a1e681L);
983c65ebfc7SToomas Soome R1(B,C,D,A,X[ 4],20,0xe7d3fbc8L);
984c65ebfc7SToomas Soome R1(A,B,C,D,X[ 9], 5,0x21e1cde6L);
985c65ebfc7SToomas Soome R1(D,A,B,C,X[14], 9,0xc33707d6L);
986c65ebfc7SToomas Soome R1(C,D,A,B,X[ 3],14,0xf4d50d87L);
987c65ebfc7SToomas Soome R1(B,C,D,A,X[ 8],20,0x455a14edL);
988c65ebfc7SToomas Soome R1(A,B,C,D,X[13], 5,0xa9e3e905L);
989c65ebfc7SToomas Soome R1(D,A,B,C,X[ 2], 9,0xfcefa3f8L);
990c65ebfc7SToomas Soome R1(C,D,A,B,X[ 7],14,0x676f02d9L);
991c65ebfc7SToomas Soome R1(B,C,D,A,X[12],20,0x8d2a4c8aL);
992c65ebfc7SToomas Soome /* Round 2 */
993c65ebfc7SToomas Soome R2(A,B,C,D,X[ 5], 4,0xfffa3942L);
994c65ebfc7SToomas Soome R2(D,A,B,C,X[ 8],11,0x8771f681L);
995c65ebfc7SToomas Soome R2(C,D,A,B,X[11],16,0x6d9d6122L);
996c65ebfc7SToomas Soome R2(B,C,D,A,X[14],23,0xfde5380cL);
997c65ebfc7SToomas Soome R2(A,B,C,D,X[ 1], 4,0xa4beea44L);
998c65ebfc7SToomas Soome R2(D,A,B,C,X[ 4],11,0x4bdecfa9L);
999c65ebfc7SToomas Soome R2(C,D,A,B,X[ 7],16,0xf6bb4b60L);
1000c65ebfc7SToomas Soome R2(B,C,D,A,X[10],23,0xbebfbc70L);
1001c65ebfc7SToomas Soome R2(A,B,C,D,X[13], 4,0x289b7ec6L);
1002c65ebfc7SToomas Soome R2(D,A,B,C,X[ 0],11,0xeaa127faL);
1003c65ebfc7SToomas Soome R2(C,D,A,B,X[ 3],16,0xd4ef3085L);
1004c65ebfc7SToomas Soome R2(B,C,D,A,X[ 6],23,0x04881d05L);
1005c65ebfc7SToomas Soome R2(A,B,C,D,X[ 9], 4,0xd9d4d039L);
1006c65ebfc7SToomas Soome R2(D,A,B,C,X[12],11,0xe6db99e5L);
1007c65ebfc7SToomas Soome R2(C,D,A,B,X[15],16,0x1fa27cf8L);
1008c65ebfc7SToomas Soome R2(B,C,D,A,X[ 2],23,0xc4ac5665L);
1009c65ebfc7SToomas Soome /* Round 3 */
1010c65ebfc7SToomas Soome R3(A,B,C,D,X[ 0], 6,0xf4292244L);
1011c65ebfc7SToomas Soome R3(D,A,B,C,X[ 7],10,0x432aff97L);
1012c65ebfc7SToomas Soome R3(C,D,A,B,X[14],15,0xab9423a7L);
1013c65ebfc7SToomas Soome R3(B,C,D,A,X[ 5],21,0xfc93a039L);
1014c65ebfc7SToomas Soome R3(A,B,C,D,X[12], 6,0x655b59c3L);
1015c65ebfc7SToomas Soome R3(D,A,B,C,X[ 3],10,0x8f0ccc92L);
1016c65ebfc7SToomas Soome R3(C,D,A,B,X[10],15,0xffeff47dL);
1017c65ebfc7SToomas Soome R3(B,C,D,A,X[ 1],21,0x85845dd1L);
1018c65ebfc7SToomas Soome R3(A,B,C,D,X[ 8], 6,0x6fa87e4fL);
1019c65ebfc7SToomas Soome R3(D,A,B,C,X[15],10,0xfe2ce6e0L);
1020c65ebfc7SToomas Soome R3(C,D,A,B,X[ 6],15,0xa3014314L);
1021c65ebfc7SToomas Soome R3(B,C,D,A,X[13],21,0x4e0811a1L);
1022c65ebfc7SToomas Soome R3(A,B,C,D,X[ 4], 6,0xf7537e82L);
1023c65ebfc7SToomas Soome R3(D,A,B,C,X[11],10,0xbd3af235L);
1024c65ebfc7SToomas Soome R3(C,D,A,B,X[ 2],15,0x2ad7d2bbL);
1025c65ebfc7SToomas Soome R3(B,C,D,A,X[ 9],21,0xeb86d391L);
1026c65ebfc7SToomas Soome
1027c65ebfc7SToomas Soome A = c->A += A;
1028c65ebfc7SToomas Soome B = c->B += B;
1029c65ebfc7SToomas Soome C = c->C += C;
1030c65ebfc7SToomas Soome D = c->D += D;
1031c65ebfc7SToomas Soome }
1032c65ebfc7SToomas Soome }
1033c65ebfc7SToomas Soome #endif
1034c65ebfc7SToomas Soome
1035c65ebfc7SToomas Soome #ifndef md5_block_data_order
1036c65ebfc7SToomas Soome #ifdef X
1037c65ebfc7SToomas Soome #undef X
1038c65ebfc7SToomas Soome #endif
md5_block_data_order(MD5_CTX * c,const void * data_,int num)1039c65ebfc7SToomas Soome void md5_block_data_order (MD5_CTX *c, const void *data_, int num)
1040c65ebfc7SToomas Soome {
1041c65ebfc7SToomas Soome const unsigned char *data=data_;
1042c65ebfc7SToomas Soome register unsigned MD32_REG_T A,B,C,D,l;
1043c65ebfc7SToomas Soome #ifndef MD32_XARRAY
1044c65ebfc7SToomas Soome /* See comment in crypto/sha/sha_locl.h for details. */
1045c65ebfc7SToomas Soome unsigned MD32_REG_T XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7,
1046c65ebfc7SToomas Soome XX8, XX9,XX10,XX11,XX12,XX13,XX14,XX15;
1047c65ebfc7SToomas Soome # define X(i) XX ## i
1048c65ebfc7SToomas Soome #else
1049c65ebfc7SToomas Soome mDNSu32 XX[MD5_LBLOCK];
1050c65ebfc7SToomas Soome # define X(i) XX[i]
1051c65ebfc7SToomas Soome #endif
1052c65ebfc7SToomas Soome
1053c65ebfc7SToomas Soome A=c->A;
1054c65ebfc7SToomas Soome B=c->B;
1055c65ebfc7SToomas Soome C=c->C;
1056c65ebfc7SToomas Soome D=c->D;
1057c65ebfc7SToomas Soome
1058*472cd20dSToomas Soome #if defined(__clang_analyzer__)
1059*472cd20dSToomas Soome // Get rid of false positive analyzer warning.
1060*472cd20dSToomas Soome for (const unsigned char *_ptr = data; _ptr < &data[num * HASH_CBLOCK]; ++_ptr) {}
1061*472cd20dSToomas Soome #endif
1062c65ebfc7SToomas Soome for (; num--;)
1063c65ebfc7SToomas Soome {
1064c65ebfc7SToomas Soome HOST_c2l(data,l); X( 0)=l; HOST_c2l(data,l); X( 1)=l;
1065c65ebfc7SToomas Soome /* Round 0 */
1066c65ebfc7SToomas Soome R0(A,B,C,D,X( 0), 7,0xd76aa478L); HOST_c2l(data,l); X( 2)=l;
1067c65ebfc7SToomas Soome R0(D,A,B,C,X( 1),12,0xe8c7b756L); HOST_c2l(data,l); X( 3)=l;
1068c65ebfc7SToomas Soome R0(C,D,A,B,X( 2),17,0x242070dbL); HOST_c2l(data,l); X( 4)=l;
1069c65ebfc7SToomas Soome R0(B,C,D,A,X( 3),22,0xc1bdceeeL); HOST_c2l(data,l); X( 5)=l;
1070c65ebfc7SToomas Soome R0(A,B,C,D,X( 4), 7,0xf57c0fafL); HOST_c2l(data,l); X( 6)=l;
1071c65ebfc7SToomas Soome R0(D,A,B,C,X( 5),12,0x4787c62aL); HOST_c2l(data,l); X( 7)=l;
1072c65ebfc7SToomas Soome R0(C,D,A,B,X( 6),17,0xa8304613L); HOST_c2l(data,l); X( 8)=l;
1073c65ebfc7SToomas Soome R0(B,C,D,A,X( 7),22,0xfd469501L); HOST_c2l(data,l); X( 9)=l;
1074c65ebfc7SToomas Soome R0(A,B,C,D,X( 8), 7,0x698098d8L); HOST_c2l(data,l); X(10)=l;
1075c65ebfc7SToomas Soome R0(D,A,B,C,X( 9),12,0x8b44f7afL); HOST_c2l(data,l); X(11)=l;
1076c65ebfc7SToomas Soome R0(C,D,A,B,X(10),17,0xffff5bb1L); HOST_c2l(data,l); X(12)=l;
1077c65ebfc7SToomas Soome R0(B,C,D,A,X(11),22,0x895cd7beL); HOST_c2l(data,l); X(13)=l;
1078c65ebfc7SToomas Soome R0(A,B,C,D,X(12), 7,0x6b901122L); HOST_c2l(data,l); X(14)=l;
1079c65ebfc7SToomas Soome R0(D,A,B,C,X(13),12,0xfd987193L); HOST_c2l(data,l); X(15)=l;
1080c65ebfc7SToomas Soome R0(C,D,A,B,X(14),17,0xa679438eL);
1081c65ebfc7SToomas Soome R0(B,C,D,A,X(15),22,0x49b40821L);
1082c65ebfc7SToomas Soome /* Round 1 */
1083c65ebfc7SToomas Soome R1(A,B,C,D,X( 1), 5,0xf61e2562L);
1084c65ebfc7SToomas Soome R1(D,A,B,C,X( 6), 9,0xc040b340L);
1085c65ebfc7SToomas Soome R1(C,D,A,B,X(11),14,0x265e5a51L);
1086c65ebfc7SToomas Soome R1(B,C,D,A,X( 0),20,0xe9b6c7aaL);
1087c65ebfc7SToomas Soome R1(A,B,C,D,X( 5), 5,0xd62f105dL);
1088c65ebfc7SToomas Soome R1(D,A,B,C,X(10), 9,0x02441453L);
1089c65ebfc7SToomas Soome R1(C,D,A,B,X(15),14,0xd8a1e681L);
1090c65ebfc7SToomas Soome R1(B,C,D,A,X( 4),20,0xe7d3fbc8L);
1091c65ebfc7SToomas Soome R1(A,B,C,D,X( 9), 5,0x21e1cde6L);
1092c65ebfc7SToomas Soome R1(D,A,B,C,X(14), 9,0xc33707d6L);
1093c65ebfc7SToomas Soome R1(C,D,A,B,X( 3),14,0xf4d50d87L);
1094c65ebfc7SToomas Soome R1(B,C,D,A,X( 8),20,0x455a14edL);
1095c65ebfc7SToomas Soome R1(A,B,C,D,X(13), 5,0xa9e3e905L);
1096c65ebfc7SToomas Soome R1(D,A,B,C,X( 2), 9,0xfcefa3f8L);
1097c65ebfc7SToomas Soome R1(C,D,A,B,X( 7),14,0x676f02d9L);
1098c65ebfc7SToomas Soome R1(B,C,D,A,X(12),20,0x8d2a4c8aL);
1099c65ebfc7SToomas Soome /* Round 2 */
1100c65ebfc7SToomas Soome R2(A,B,C,D,X( 5), 4,0xfffa3942L);
1101c65ebfc7SToomas Soome R2(D,A,B,C,X( 8),11,0x8771f681L);
1102c65ebfc7SToomas Soome R2(C,D,A,B,X(11),16,0x6d9d6122L);
1103c65ebfc7SToomas Soome R2(B,C,D,A,X(14),23,0xfde5380cL);
1104c65ebfc7SToomas Soome R2(A,B,C,D,X( 1), 4,0xa4beea44L);
1105c65ebfc7SToomas Soome R2(D,A,B,C,X( 4),11,0x4bdecfa9L);
1106c65ebfc7SToomas Soome R2(C,D,A,B,X( 7),16,0xf6bb4b60L);
1107c65ebfc7SToomas Soome R2(B,C,D,A,X(10),23,0xbebfbc70L);
1108c65ebfc7SToomas Soome R2(A,B,C,D,X(13), 4,0x289b7ec6L);
1109c65ebfc7SToomas Soome R2(D,A,B,C,X( 0),11,0xeaa127faL);
1110c65ebfc7SToomas Soome R2(C,D,A,B,X( 3),16,0xd4ef3085L);
1111c65ebfc7SToomas Soome R2(B,C,D,A,X( 6),23,0x04881d05L);
1112c65ebfc7SToomas Soome R2(A,B,C,D,X( 9), 4,0xd9d4d039L);
1113c65ebfc7SToomas Soome R2(D,A,B,C,X(12),11,0xe6db99e5L);
1114c65ebfc7SToomas Soome R2(C,D,A,B,X(15),16,0x1fa27cf8L);
1115c65ebfc7SToomas Soome R2(B,C,D,A,X( 2),23,0xc4ac5665L);
1116c65ebfc7SToomas Soome /* Round 3 */
1117c65ebfc7SToomas Soome R3(A,B,C,D,X( 0), 6,0xf4292244L);
1118c65ebfc7SToomas Soome R3(D,A,B,C,X( 7),10,0x432aff97L);
1119c65ebfc7SToomas Soome R3(C,D,A,B,X(14),15,0xab9423a7L);
1120c65ebfc7SToomas Soome R3(B,C,D,A,X( 5),21,0xfc93a039L);
1121c65ebfc7SToomas Soome R3(A,B,C,D,X(12), 6,0x655b59c3L);
1122c65ebfc7SToomas Soome R3(D,A,B,C,X( 3),10,0x8f0ccc92L);
1123c65ebfc7SToomas Soome R3(C,D,A,B,X(10),15,0xffeff47dL);
1124c65ebfc7SToomas Soome R3(B,C,D,A,X( 1),21,0x85845dd1L);
1125c65ebfc7SToomas Soome R3(A,B,C,D,X( 8), 6,0x6fa87e4fL);
1126c65ebfc7SToomas Soome R3(D,A,B,C,X(15),10,0xfe2ce6e0L);
1127c65ebfc7SToomas Soome R3(C,D,A,B,X( 6),15,0xa3014314L);
1128c65ebfc7SToomas Soome R3(B,C,D,A,X(13),21,0x4e0811a1L);
1129c65ebfc7SToomas Soome R3(A,B,C,D,X( 4), 6,0xf7537e82L);
1130c65ebfc7SToomas Soome R3(D,A,B,C,X(11),10,0xbd3af235L);
1131c65ebfc7SToomas Soome R3(C,D,A,B,X( 2),15,0x2ad7d2bbL);
1132c65ebfc7SToomas Soome R3(B,C,D,A,X( 9),21,0xeb86d391L);
1133c65ebfc7SToomas Soome
1134c65ebfc7SToomas Soome A = c->A += A;
1135c65ebfc7SToomas Soome B = c->B += B;
1136c65ebfc7SToomas Soome C = c->C += C;
1137c65ebfc7SToomas Soome D = c->D += D;
1138c65ebfc7SToomas Soome }
1139c65ebfc7SToomas Soome }
1140c65ebfc7SToomas Soome #endif
1141c65ebfc7SToomas Soome
1142c65ebfc7SToomas Soome
1143c65ebfc7SToomas Soome // ***************************************************************************
1144c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
1145c65ebfc7SToomas Soome #pragma mark - base64 -> binary conversion
1146c65ebfc7SToomas Soome #endif
1147c65ebfc7SToomas Soome
1148c65ebfc7SToomas Soome static const char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1149c65ebfc7SToomas Soome static const char Pad64 = '=';
1150c65ebfc7SToomas Soome
1151c65ebfc7SToomas Soome
1152c65ebfc7SToomas Soome #define mDNSisspace(x) (x == '\t' || x == '\n' || x == '\v' || x == '\f' || x == '\r' || x == ' ')
1153c65ebfc7SToomas Soome
mDNSstrchr(const char * s,int c)1154c65ebfc7SToomas Soome mDNSlocal const char *mDNSstrchr(const char *s, int c)
1155c65ebfc7SToomas Soome {
1156c65ebfc7SToomas Soome while (1)
1157c65ebfc7SToomas Soome {
1158c65ebfc7SToomas Soome if (c == *s) return s;
1159c65ebfc7SToomas Soome if (!*s) return mDNSNULL;
1160c65ebfc7SToomas Soome s++;
1161c65ebfc7SToomas Soome }
1162c65ebfc7SToomas Soome }
1163c65ebfc7SToomas Soome
1164c65ebfc7SToomas Soome // skips all whitespace anywhere.
1165c65ebfc7SToomas Soome // converts characters, four at a time, starting at (or after)
1166c65ebfc7SToomas Soome // src from base - 64 numbers into three 8 bit bytes in the target area.
1167c65ebfc7SToomas Soome // it returns the number of data bytes stored at the target, or -1 on error.
1168c65ebfc7SToomas Soome // adapted from BIND sources
1169c65ebfc7SToomas Soome
DNSDigest_Base64ToBin(const char * src,mDNSu8 * target,mDNSu32 targsize)1170c65ebfc7SToomas Soome mDNSlocal mDNSs32 DNSDigest_Base64ToBin(const char *src, mDNSu8 *target, mDNSu32 targsize)
1171c65ebfc7SToomas Soome {
1172c65ebfc7SToomas Soome int tarindex, state, ch;
1173c65ebfc7SToomas Soome const char *pos;
1174c65ebfc7SToomas Soome
1175c65ebfc7SToomas Soome state = 0;
1176c65ebfc7SToomas Soome tarindex = 0;
1177c65ebfc7SToomas Soome
1178c65ebfc7SToomas Soome while ((ch = *src++) != '\0') {
1179c65ebfc7SToomas Soome if (mDNSisspace(ch)) /* Skip whitespace anywhere. */
1180c65ebfc7SToomas Soome continue;
1181c65ebfc7SToomas Soome
1182c65ebfc7SToomas Soome if (ch == Pad64)
1183c65ebfc7SToomas Soome break;
1184c65ebfc7SToomas Soome
1185c65ebfc7SToomas Soome pos = mDNSstrchr(Base64, ch);
1186c65ebfc7SToomas Soome if (pos == 0) /* A non-base64 character. */
1187c65ebfc7SToomas Soome return (-1);
1188c65ebfc7SToomas Soome
1189c65ebfc7SToomas Soome switch (state) {
1190c65ebfc7SToomas Soome case 0:
1191c65ebfc7SToomas Soome if (target) {
1192c65ebfc7SToomas Soome if ((mDNSu32)tarindex >= targsize)
1193c65ebfc7SToomas Soome return (-1);
1194c65ebfc7SToomas Soome target[tarindex] = (mDNSu8)((pos - Base64) << 2);
1195c65ebfc7SToomas Soome }
1196c65ebfc7SToomas Soome state = 1;
1197c65ebfc7SToomas Soome break;
1198c65ebfc7SToomas Soome case 1:
1199c65ebfc7SToomas Soome if (target) {
1200c65ebfc7SToomas Soome if ((mDNSu32)tarindex + 1 >= targsize)
1201c65ebfc7SToomas Soome return (-1);
1202c65ebfc7SToomas Soome target[tarindex] |= (pos - Base64) >> 4;
1203c65ebfc7SToomas Soome target[tarindex+1] = (mDNSu8)(((pos - Base64) & 0x0f) << 4);
1204c65ebfc7SToomas Soome }
1205c65ebfc7SToomas Soome tarindex++;
1206c65ebfc7SToomas Soome state = 2;
1207c65ebfc7SToomas Soome break;
1208c65ebfc7SToomas Soome case 2:
1209c65ebfc7SToomas Soome if (target) {
1210c65ebfc7SToomas Soome if ((mDNSu32)tarindex + 1 >= targsize)
1211c65ebfc7SToomas Soome return (-1);
1212c65ebfc7SToomas Soome target[tarindex] |= (pos - Base64) >> 2;
1213c65ebfc7SToomas Soome target[tarindex+1] = (mDNSu8)(((pos - Base64) & 0x03) << 6);
1214c65ebfc7SToomas Soome }
1215c65ebfc7SToomas Soome tarindex++;
1216c65ebfc7SToomas Soome state = 3;
1217c65ebfc7SToomas Soome break;
1218c65ebfc7SToomas Soome case 3:
1219c65ebfc7SToomas Soome if (target) {
1220c65ebfc7SToomas Soome if ((mDNSu32)tarindex >= targsize)
1221c65ebfc7SToomas Soome return (-1);
1222c65ebfc7SToomas Soome target[tarindex] |= (pos - Base64);
1223c65ebfc7SToomas Soome }
1224c65ebfc7SToomas Soome tarindex++;
1225c65ebfc7SToomas Soome state = 0;
1226c65ebfc7SToomas Soome break;
1227c65ebfc7SToomas Soome default:
1228c65ebfc7SToomas Soome return -1;
1229c65ebfc7SToomas Soome }
1230c65ebfc7SToomas Soome }
1231c65ebfc7SToomas Soome
1232c65ebfc7SToomas Soome /*
1233c65ebfc7SToomas Soome * We are done decoding Base-64 chars. Let's see if we ended
1234c65ebfc7SToomas Soome * on a byte boundary, and/or with erroneous trailing characters.
1235c65ebfc7SToomas Soome */
1236c65ebfc7SToomas Soome
1237c65ebfc7SToomas Soome if (ch == Pad64) { /* We got a pad char. */
1238c65ebfc7SToomas Soome ch = *src++; /* Skip it, get next. */
1239c65ebfc7SToomas Soome switch (state) {
1240c65ebfc7SToomas Soome case 0: /* Invalid = in first position */
1241c65ebfc7SToomas Soome case 1: /* Invalid = in second position */
1242c65ebfc7SToomas Soome return (-1);
1243c65ebfc7SToomas Soome
1244c65ebfc7SToomas Soome case 2: /* Valid, means one byte of info */
1245c65ebfc7SToomas Soome /* Skip any number of spaces. */
1246c65ebfc7SToomas Soome for ((void)mDNSNULL; ch != '\0'; ch = *src++)
1247c65ebfc7SToomas Soome if (!mDNSisspace(ch))
1248c65ebfc7SToomas Soome break;
1249c65ebfc7SToomas Soome /* Make sure there is another trailing = sign. */
1250c65ebfc7SToomas Soome if (ch != Pad64)
1251c65ebfc7SToomas Soome return (-1);
1252c65ebfc7SToomas Soome ch = *src++; /* Skip the = */
1253c65ebfc7SToomas Soome /* Fall through to "single trailing =" case. */
1254c65ebfc7SToomas Soome /* FALLTHROUGH */
1255c65ebfc7SToomas Soome
1256c65ebfc7SToomas Soome case 3: /* Valid, means two bytes of info */
1257c65ebfc7SToomas Soome /*
1258c65ebfc7SToomas Soome * We know this char is an =. Is there anything but
1259c65ebfc7SToomas Soome * whitespace after it?
1260c65ebfc7SToomas Soome */
1261c65ebfc7SToomas Soome for ((void)mDNSNULL; ch != '\0'; ch = *src++)
1262c65ebfc7SToomas Soome if (!mDNSisspace(ch))
1263c65ebfc7SToomas Soome return (-1);
1264c65ebfc7SToomas Soome
1265c65ebfc7SToomas Soome /*
1266c65ebfc7SToomas Soome * Now make sure for cases 2 and 3 that the "extra"
1267c65ebfc7SToomas Soome * bits that slopped past the last full byte were
1268c65ebfc7SToomas Soome * zeros. If we don't check them, they become a
1269c65ebfc7SToomas Soome * subliminal channel.
1270c65ebfc7SToomas Soome */
1271c65ebfc7SToomas Soome if (target && target[tarindex] != 0)
1272c65ebfc7SToomas Soome return (-1);
1273c65ebfc7SToomas Soome }
1274c65ebfc7SToomas Soome } else {
1275c65ebfc7SToomas Soome /*
1276c65ebfc7SToomas Soome * We ended by seeing the end of the string. Make sure we
1277c65ebfc7SToomas Soome * have no partial bytes lying around.
1278c65ebfc7SToomas Soome */
1279c65ebfc7SToomas Soome if (state != 0)
1280c65ebfc7SToomas Soome return (-1);
1281c65ebfc7SToomas Soome }
1282c65ebfc7SToomas Soome
1283c65ebfc7SToomas Soome return (tarindex);
1284c65ebfc7SToomas Soome }
1285c65ebfc7SToomas Soome
1286c65ebfc7SToomas Soome
1287c65ebfc7SToomas Soome // ***************************************************************************
1288c65ebfc7SToomas Soome #if COMPILER_LIKES_PRAGMA_MARK
1289c65ebfc7SToomas Soome #pragma mark - API exported to mDNS Core
1290c65ebfc7SToomas Soome #endif
1291c65ebfc7SToomas Soome
1292c65ebfc7SToomas Soome // Constants
1293c65ebfc7SToomas Soome #define HMAC_IPAD 0x36
1294c65ebfc7SToomas Soome #define HMAC_OPAD 0x5c
1295c65ebfc7SToomas Soome #define MD5_LEN 16
1296c65ebfc7SToomas Soome
1297*472cd20dSToomas Soome #define HMAC_MD5_AlgName "\010" "hmac-md5" "\007" "sig-alg" "\003" "reg" "\003" "int"
1298c65ebfc7SToomas Soome
1299c65ebfc7SToomas Soome // Adapted from Appendix, RFC 2104
DNSDigest_ConstructHMACKey(DomainAuthInfo * info,const mDNSu8 * key,mDNSu32 len)1300c65ebfc7SToomas Soome mDNSlocal void DNSDigest_ConstructHMACKey(DomainAuthInfo *info, const mDNSu8 *key, mDNSu32 len)
1301c65ebfc7SToomas Soome {
1302c65ebfc7SToomas Soome MD5_CTX k;
1303c65ebfc7SToomas Soome mDNSu8 buf[MD5_LEN];
1304c65ebfc7SToomas Soome int i;
1305c65ebfc7SToomas Soome
1306c65ebfc7SToomas Soome // If key is longer than HMAC_LEN reset it to MD5(key)
1307c65ebfc7SToomas Soome if (len > HMAC_LEN)
1308c65ebfc7SToomas Soome {
1309c65ebfc7SToomas Soome MD5_Init(&k);
1310c65ebfc7SToomas Soome MD5_Update(&k, key, len);
1311c65ebfc7SToomas Soome MD5_Final(buf, &k);
1312c65ebfc7SToomas Soome key = buf;
1313c65ebfc7SToomas Soome len = MD5_LEN;
1314c65ebfc7SToomas Soome }
1315c65ebfc7SToomas Soome
1316c65ebfc7SToomas Soome // store key in pads
1317c65ebfc7SToomas Soome mDNSPlatformMemZero(info->keydata_ipad, HMAC_LEN);
1318c65ebfc7SToomas Soome mDNSPlatformMemZero(info->keydata_opad, HMAC_LEN);
1319c65ebfc7SToomas Soome mDNSPlatformMemCopy(info->keydata_ipad, key, len);
1320c65ebfc7SToomas Soome mDNSPlatformMemCopy(info->keydata_opad, key, len);
1321c65ebfc7SToomas Soome
1322c65ebfc7SToomas Soome // XOR key with ipad and opad values
1323c65ebfc7SToomas Soome for (i = 0; i < HMAC_LEN; i++)
1324c65ebfc7SToomas Soome {
1325c65ebfc7SToomas Soome info->keydata_ipad[i] ^= HMAC_IPAD;
1326c65ebfc7SToomas Soome info->keydata_opad[i] ^= HMAC_OPAD;
1327c65ebfc7SToomas Soome }
1328c65ebfc7SToomas Soome
1329c65ebfc7SToomas Soome }
1330c65ebfc7SToomas Soome
DNSDigest_ConstructHMACKeyfromBase64(DomainAuthInfo * info,const char * b64key)1331c65ebfc7SToomas Soome mDNSexport mDNSs32 DNSDigest_ConstructHMACKeyfromBase64(DomainAuthInfo *info, const char *b64key)
1332c65ebfc7SToomas Soome {
1333c65ebfc7SToomas Soome mDNSu8 keybuf[1024];
1334c65ebfc7SToomas Soome mDNSs32 keylen = DNSDigest_Base64ToBin(b64key, keybuf, sizeof(keybuf));
1335c65ebfc7SToomas Soome if (keylen < 0) return(keylen);
1336c65ebfc7SToomas Soome DNSDigest_ConstructHMACKey(info, keybuf, (mDNSu32)keylen);
1337c65ebfc7SToomas Soome return(keylen);
1338c65ebfc7SToomas Soome }
1339c65ebfc7SToomas Soome
DNSDigest_SignMessage(DNSMessage * msg,mDNSu8 ** end,DomainAuthInfo * info,mDNSu16 tcode)1340c65ebfc7SToomas Soome mDNSexport void DNSDigest_SignMessage(DNSMessage *msg, mDNSu8 **end, DomainAuthInfo *info, mDNSu16 tcode)
1341c65ebfc7SToomas Soome {
1342c65ebfc7SToomas Soome AuthRecord tsig;
1343c65ebfc7SToomas Soome mDNSu8 *rdata, *const countPtr = (mDNSu8 *)&msg->h.numAdditionals; // Get existing numAdditionals value
1344c65ebfc7SToomas Soome mDNSu32 utc32;
1345c65ebfc7SToomas Soome mDNSu8 utc48[6];
1346c65ebfc7SToomas Soome mDNSu8 digest[MD5_LEN];
1347c65ebfc7SToomas Soome mDNSu8 *ptr = *end;
1348c65ebfc7SToomas Soome mDNSu32 len;
1349c65ebfc7SToomas Soome mDNSOpaque16 buf;
1350c65ebfc7SToomas Soome MD5_CTX c;
1351c65ebfc7SToomas Soome mDNSu16 numAdditionals = (mDNSu16)((mDNSu16)countPtr[0] << 8 | countPtr[1]);
1352c65ebfc7SToomas Soome
1353c65ebfc7SToomas Soome // Init MD5 context, digest inner key pad and message
1354c65ebfc7SToomas Soome MD5_Init(&c);
1355c65ebfc7SToomas Soome MD5_Update(&c, info->keydata_ipad, HMAC_LEN);
1356c65ebfc7SToomas Soome MD5_Update(&c, (mDNSu8 *)msg, (unsigned long)(*end - (mDNSu8 *)msg));
1357c65ebfc7SToomas Soome
1358c65ebfc7SToomas Soome // Construct TSIG RR, digesting variables as apporpriate
1359c65ebfc7SToomas Soome mDNS_SetupResourceRecord(&tsig, mDNSNULL, 0, kDNSType_TSIG, 0, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
1360c65ebfc7SToomas Soome
1361c65ebfc7SToomas Soome // key name
1362c65ebfc7SToomas Soome AssignDomainName(&tsig.namestorage, &info->keyname);
1363c65ebfc7SToomas Soome MD5_Update(&c, info->keyname.c, DomainNameLength(&info->keyname));
1364c65ebfc7SToomas Soome
1365c65ebfc7SToomas Soome // class
1366c65ebfc7SToomas Soome tsig.resrec.rrclass = kDNSQClass_ANY;
1367c65ebfc7SToomas Soome buf = mDNSOpaque16fromIntVal(kDNSQClass_ANY);
1368c65ebfc7SToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16));
1369c65ebfc7SToomas Soome
1370c65ebfc7SToomas Soome // ttl
1371c65ebfc7SToomas Soome tsig.resrec.rroriginalttl = 0;
1372c65ebfc7SToomas Soome MD5_Update(&c, (mDNSu8 *)&tsig.resrec.rroriginalttl, sizeof(tsig.resrec.rroriginalttl));
1373c65ebfc7SToomas Soome
1374c65ebfc7SToomas Soome // alg name
1375*472cd20dSToomas Soome AssignConstStringDomainName(&tsig.resrec.rdata->u.name, HMAC_MD5_AlgName);
1376*472cd20dSToomas Soome len = DomainNameLengthLimit((domainname *)HMAC_MD5_AlgName, (mDNSu8 *)HMAC_MD5_AlgName + sizeof HMAC_MD5_AlgName);
1377c65ebfc7SToomas Soome rdata = tsig.resrec.rdata->u.data + len;
1378*472cd20dSToomas Soome MD5_Update(&c, (mDNSu8 *)HMAC_MD5_AlgName, len);
1379c65ebfc7SToomas Soome
1380c65ebfc7SToomas Soome // time
1381c65ebfc7SToomas Soome // get UTC (universal time), convert to 48-bit unsigned in network byte order
1382c65ebfc7SToomas Soome utc32 = (mDNSu32)mDNSPlatformUTC();
1383c65ebfc7SToomas Soome if (utc32 == (unsigned)-1) { LogMsg("ERROR: DNSDigest_SignMessage - mDNSPlatformUTC returned bad time -1"); *end = mDNSNULL; }
1384c65ebfc7SToomas Soome utc48[0] = 0;
1385c65ebfc7SToomas Soome utc48[1] = 0;
1386c65ebfc7SToomas Soome utc48[2] = (mDNSu8)((utc32 >> 24) & 0xff);
1387c65ebfc7SToomas Soome utc48[3] = (mDNSu8)((utc32 >> 16) & 0xff);
1388c65ebfc7SToomas Soome utc48[4] = (mDNSu8)((utc32 >> 8) & 0xff);
1389c65ebfc7SToomas Soome utc48[5] = (mDNSu8)( utc32 & 0xff);
1390c65ebfc7SToomas Soome
1391c65ebfc7SToomas Soome mDNSPlatformMemCopy(rdata, utc48, 6);
1392c65ebfc7SToomas Soome rdata += 6;
1393c65ebfc7SToomas Soome MD5_Update(&c, utc48, 6);
1394c65ebfc7SToomas Soome
1395c65ebfc7SToomas Soome // 300 sec is fudge recommended in RFC 2485
1396c65ebfc7SToomas Soome rdata[0] = (mDNSu8)((300 >> 8) & 0xff);
1397c65ebfc7SToomas Soome rdata[1] = (mDNSu8)( 300 & 0xff);
1398c65ebfc7SToomas Soome MD5_Update(&c, rdata, sizeof(mDNSOpaque16));
1399c65ebfc7SToomas Soome rdata += sizeof(mDNSOpaque16);
1400c65ebfc7SToomas Soome
1401c65ebfc7SToomas Soome // digest error (tcode) and other data len (zero) - we'll add them to the rdata later
1402c65ebfc7SToomas Soome buf.b[0] = (mDNSu8)((tcode >> 8) & 0xff);
1403c65ebfc7SToomas Soome buf.b[1] = (mDNSu8)( tcode & 0xff);
1404c65ebfc7SToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16)); // error
1405c65ebfc7SToomas Soome buf.NotAnInteger = 0;
1406c65ebfc7SToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16)); // other data len
1407c65ebfc7SToomas Soome
1408c65ebfc7SToomas Soome // finish the message & tsig var hash
1409c65ebfc7SToomas Soome MD5_Final(digest, &c);
1410c65ebfc7SToomas Soome
1411c65ebfc7SToomas Soome // perform outer MD5 (outer key pad, inner digest)
1412c65ebfc7SToomas Soome MD5_Init(&c);
1413c65ebfc7SToomas Soome MD5_Update(&c, info->keydata_opad, HMAC_LEN);
1414c65ebfc7SToomas Soome MD5_Update(&c, digest, MD5_LEN);
1415c65ebfc7SToomas Soome MD5_Final(digest, &c);
1416c65ebfc7SToomas Soome
1417c65ebfc7SToomas Soome // set remaining rdata fields
1418c65ebfc7SToomas Soome rdata[0] = (mDNSu8)((MD5_LEN >> 8) & 0xff);
1419c65ebfc7SToomas Soome rdata[1] = (mDNSu8)( MD5_LEN & 0xff);
1420c65ebfc7SToomas Soome rdata += sizeof(mDNSOpaque16);
1421c65ebfc7SToomas Soome mDNSPlatformMemCopy(rdata, digest, MD5_LEN); // MAC
1422c65ebfc7SToomas Soome rdata += MD5_LEN;
1423c65ebfc7SToomas Soome rdata[0] = msg->h.id.b[0]; // original ID
1424c65ebfc7SToomas Soome rdata[1] = msg->h.id.b[1];
1425c65ebfc7SToomas Soome rdata[2] = (mDNSu8)((tcode >> 8) & 0xff);
1426c65ebfc7SToomas Soome rdata[3] = (mDNSu8)( tcode & 0xff);
1427c65ebfc7SToomas Soome rdata[4] = 0; // other data len
1428c65ebfc7SToomas Soome rdata[5] = 0;
1429c65ebfc7SToomas Soome rdata += 6;
1430c65ebfc7SToomas Soome
1431c65ebfc7SToomas Soome tsig.resrec.rdlength = (mDNSu16)(rdata - tsig.resrec.rdata->u.data);
1432c65ebfc7SToomas Soome *end = PutResourceRecordTTLJumbo(msg, ptr, &numAdditionals, &tsig.resrec, 0);
1433c65ebfc7SToomas Soome if (!*end) { LogMsg("ERROR: DNSDigest_SignMessage - could not put TSIG"); *end = mDNSNULL; return; }
1434c65ebfc7SToomas Soome
1435c65ebfc7SToomas Soome // Write back updated numAdditionals value
1436c65ebfc7SToomas Soome countPtr[0] = (mDNSu8)(numAdditionals >> 8);
1437c65ebfc7SToomas Soome countPtr[1] = (mDNSu8)(numAdditionals & 0xFF);
1438c65ebfc7SToomas Soome }
1439c65ebfc7SToomas Soome
DNSDigest_VerifyMessage(DNSMessage * msg,mDNSu8 * end,LargeCacheRecord * lcr,DomainAuthInfo * info,mDNSu16 * rcode,mDNSu16 * tcode)1440c65ebfc7SToomas Soome mDNSexport mDNSBool DNSDigest_VerifyMessage(DNSMessage *msg, mDNSu8 *end, LargeCacheRecord * lcr, DomainAuthInfo *info, mDNSu16 * rcode, mDNSu16 * tcode)
1441c65ebfc7SToomas Soome {
1442c65ebfc7SToomas Soome mDNSu8 * ptr = (mDNSu8*) &lcr->r.resrec.rdata->u.data;
1443c65ebfc7SToomas Soome mDNSs32 now;
1444c65ebfc7SToomas Soome mDNSs32 then;
1445c65ebfc7SToomas Soome mDNSu8 thisDigest[MD5_LEN];
1446c65ebfc7SToomas Soome mDNSu8 thatDigest[MD5_LEN];
1447c65ebfc7SToomas Soome mDNSOpaque16 buf;
1448c65ebfc7SToomas Soome mDNSu8 utc48[6];
1449c65ebfc7SToomas Soome mDNSs32 delta;
1450c65ebfc7SToomas Soome mDNSu16 fudge;
1451c65ebfc7SToomas Soome domainname * algo;
1452c65ebfc7SToomas Soome MD5_CTX c;
1453c65ebfc7SToomas Soome mDNSBool ok = mDNSfalse;
1454c65ebfc7SToomas Soome
1455c65ebfc7SToomas Soome // We only support HMAC-MD5 for now
1456c65ebfc7SToomas Soome
1457c65ebfc7SToomas Soome algo = (domainname*) ptr;
1458c65ebfc7SToomas Soome
1459*472cd20dSToomas Soome if (!SameDomainName(algo, (domainname *)HMAC_MD5_AlgName))
1460c65ebfc7SToomas Soome {
1461c65ebfc7SToomas Soome LogMsg("ERROR: DNSDigest_VerifyMessage - TSIG algorithm not supported: %##s", algo->c);
1462c65ebfc7SToomas Soome *rcode = kDNSFlag1_RC_NotAuth;
1463c65ebfc7SToomas Soome *tcode = TSIG_ErrBadKey;
1464c65ebfc7SToomas Soome ok = mDNSfalse;
1465c65ebfc7SToomas Soome goto exit;
1466c65ebfc7SToomas Soome }
1467c65ebfc7SToomas Soome
1468c65ebfc7SToomas Soome ptr += DomainNameLength(algo);
1469c65ebfc7SToomas Soome
1470c65ebfc7SToomas Soome // Check the times
1471c65ebfc7SToomas Soome
1472c65ebfc7SToomas Soome now = mDNSPlatformUTC();
1473c65ebfc7SToomas Soome if (now == -1)
1474c65ebfc7SToomas Soome {
1475c65ebfc7SToomas Soome LogMsg("ERROR: DNSDigest_VerifyMessage - mDNSPlatformUTC returned bad time -1");
1476c65ebfc7SToomas Soome *rcode = kDNSFlag1_RC_NotAuth;
1477c65ebfc7SToomas Soome *tcode = TSIG_ErrBadTime;
1478c65ebfc7SToomas Soome ok = mDNSfalse;
1479c65ebfc7SToomas Soome goto exit;
1480c65ebfc7SToomas Soome }
1481c65ebfc7SToomas Soome
1482c65ebfc7SToomas Soome // Get the 48 bit time field, skipping over the first word
1483c65ebfc7SToomas Soome
1484c65ebfc7SToomas Soome utc48[0] = *ptr++;
1485c65ebfc7SToomas Soome utc48[1] = *ptr++;
1486c65ebfc7SToomas Soome utc48[2] = *ptr++;
1487c65ebfc7SToomas Soome utc48[3] = *ptr++;
1488c65ebfc7SToomas Soome utc48[4] = *ptr++;
1489c65ebfc7SToomas Soome utc48[5] = *ptr++;
1490c65ebfc7SToomas Soome
1491c65ebfc7SToomas Soome then = (mDNSs32)NToH32(utc48 + sizeof(mDNSu16));
1492c65ebfc7SToomas Soome
1493c65ebfc7SToomas Soome fudge = NToH16(ptr);
1494c65ebfc7SToomas Soome
1495c65ebfc7SToomas Soome ptr += sizeof(mDNSu16);
1496c65ebfc7SToomas Soome
1497c65ebfc7SToomas Soome delta = (now > then) ? now - then : then - now;
1498c65ebfc7SToomas Soome
1499c65ebfc7SToomas Soome if (delta > fudge)
1500c65ebfc7SToomas Soome {
1501c65ebfc7SToomas Soome LogMsg("ERROR: DNSDigest_VerifyMessage - time skew > %d", fudge);
1502c65ebfc7SToomas Soome *rcode = kDNSFlag1_RC_NotAuth;
1503c65ebfc7SToomas Soome *tcode = TSIG_ErrBadTime;
1504c65ebfc7SToomas Soome ok = mDNSfalse;
1505c65ebfc7SToomas Soome goto exit;
1506c65ebfc7SToomas Soome }
1507c65ebfc7SToomas Soome
1508c65ebfc7SToomas Soome // MAC size
1509c65ebfc7SToomas Soome
1510c65ebfc7SToomas Soome ptr += sizeof(mDNSu16);
1511c65ebfc7SToomas Soome
1512c65ebfc7SToomas Soome // MAC
1513c65ebfc7SToomas Soome
1514c65ebfc7SToomas Soome mDNSPlatformMemCopy(thatDigest, ptr, MD5_LEN);
1515c65ebfc7SToomas Soome
1516c65ebfc7SToomas Soome // Init MD5 context, digest inner key pad and message
1517c65ebfc7SToomas Soome
1518c65ebfc7SToomas Soome MD5_Init(&c);
1519c65ebfc7SToomas Soome MD5_Update(&c, info->keydata_ipad, HMAC_LEN);
1520c65ebfc7SToomas Soome MD5_Update(&c, (mDNSu8*) msg, (unsigned long)(end - (mDNSu8*) msg));
1521c65ebfc7SToomas Soome
1522c65ebfc7SToomas Soome // Key name
1523c65ebfc7SToomas Soome
1524c65ebfc7SToomas Soome MD5_Update(&c, lcr->r.resrec.name->c, DomainNameLength(lcr->r.resrec.name));
1525c65ebfc7SToomas Soome
1526c65ebfc7SToomas Soome // Class name
1527c65ebfc7SToomas Soome
1528c65ebfc7SToomas Soome buf = mDNSOpaque16fromIntVal(lcr->r.resrec.rrclass);
1529c65ebfc7SToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16));
1530c65ebfc7SToomas Soome
1531c65ebfc7SToomas Soome // TTL
1532c65ebfc7SToomas Soome
1533c65ebfc7SToomas Soome MD5_Update(&c, (mDNSu8*) &lcr->r.resrec.rroriginalttl, sizeof(lcr->r.resrec.rroriginalttl));
1534c65ebfc7SToomas Soome
1535c65ebfc7SToomas Soome // Algorithm
1536c65ebfc7SToomas Soome
1537c65ebfc7SToomas Soome MD5_Update(&c, algo->c, DomainNameLength(algo));
1538c65ebfc7SToomas Soome
1539c65ebfc7SToomas Soome // Time
1540c65ebfc7SToomas Soome
1541c65ebfc7SToomas Soome MD5_Update(&c, utc48, 6);
1542c65ebfc7SToomas Soome
1543c65ebfc7SToomas Soome // Fudge
1544c65ebfc7SToomas Soome
1545c65ebfc7SToomas Soome buf = mDNSOpaque16fromIntVal(fudge);
1546c65ebfc7SToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16));
1547c65ebfc7SToomas Soome
1548c65ebfc7SToomas Soome // Digest error and other data len (both zero) - we'll add them to the rdata later
1549c65ebfc7SToomas Soome
1550c65ebfc7SToomas Soome buf.NotAnInteger = 0;
1551c65ebfc7SToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16)); // error
1552c65ebfc7SToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16)); // other data len
1553c65ebfc7SToomas Soome
1554c65ebfc7SToomas Soome // Finish the message & tsig var hash
1555c65ebfc7SToomas Soome
1556c65ebfc7SToomas Soome MD5_Final(thisDigest, &c);
1557c65ebfc7SToomas Soome
1558c65ebfc7SToomas Soome // perform outer MD5 (outer key pad, inner digest)
1559c65ebfc7SToomas Soome
1560c65ebfc7SToomas Soome MD5_Init(&c);
1561c65ebfc7SToomas Soome MD5_Update(&c, info->keydata_opad, HMAC_LEN);
1562c65ebfc7SToomas Soome MD5_Update(&c, thisDigest, MD5_LEN);
1563c65ebfc7SToomas Soome MD5_Final(thisDigest, &c);
1564c65ebfc7SToomas Soome
1565c65ebfc7SToomas Soome if (!mDNSPlatformMemSame(thisDigest, thatDigest, MD5_LEN))
1566c65ebfc7SToomas Soome {
1567c65ebfc7SToomas Soome LogMsg("ERROR: DNSDigest_VerifyMessage - bad signature");
1568c65ebfc7SToomas Soome *rcode = kDNSFlag1_RC_NotAuth;
1569c65ebfc7SToomas Soome *tcode = TSIG_ErrBadSig;
1570c65ebfc7SToomas Soome ok = mDNSfalse;
1571c65ebfc7SToomas Soome goto exit;
1572c65ebfc7SToomas Soome }
1573c65ebfc7SToomas Soome
1574c65ebfc7SToomas Soome // set remaining rdata fields
1575c65ebfc7SToomas Soome ok = mDNStrue;
1576c65ebfc7SToomas Soome
1577c65ebfc7SToomas Soome exit:
1578c65ebfc7SToomas Soome
1579c65ebfc7SToomas Soome return ok;
1580c65ebfc7SToomas Soome }
1581c65ebfc7SToomas Soome
1582c65ebfc7SToomas Soome
1583c65ebfc7SToomas Soome #ifdef __cplusplus
1584c65ebfc7SToomas Soome }
1585c65ebfc7SToomas Soome #endif
1586