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