4b22b9337f359bfd063322244f5336cc7c6ffcfars/* -*- Mode: C; tab-width: 4 -*-
cda73f64f20b8a0afc4909f5ea1f055ec7913856Toomas Soome * Copyright (c) 2002-2011 Apple Inc. All rights reserved.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Licensed under the Apache License, Version 2.0 (the "License");
4b22b9337f359bfd063322244f5336cc7c6ffcfars * you may not use this file except in compliance with the License.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * You may obtain a copy of the License at
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Unless required by applicable law or agreed to in writing, software
4b22b9337f359bfd063322244f5336cc7c6ffcfars * distributed under the License is distributed on an "AS IS" BASIS,
4b22b9337f359bfd063322244f5336cc7c6ffcfars * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * See the License for the specific language governing permissions and
4b22b9337f359bfd063322244f5336cc7c6ffcfars * limitations under the License.
4b22b9337f359bfd063322244f5336cc7c6ffcfarsextern "C" {
4b22b9337f359bfd063322244f5336cc7c6ffcfars// Disable certain benign warnings with Microsoft compilers
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#if (defined(_MSC_VER))
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// Disable "conditional expression is constant" warning for debug macros.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// Otherwise, this generates warnings for the perfectly natural construct "while(1)"
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// If someone knows a variant way of writing "while(1)" that doesn't generate warning messages, please let us know
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// ***************************************************************************
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (mDNSu16)((mDNSu16)bytes[0] << 8 | (mDNSu16)bytes[1]);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (mDNSu32)((mDNSu32) bytes[0] << 24 | (mDNSu32) bytes[1] << 16 | (mDNSu32) bytes[2] << 8 | (mDNSu32)bytes[3]);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// ***************************************************************************
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* The source for the has is derived CommonCrypto files CommonDigest.h, md32_common.h, md5_locl.h, md5_locl.h, and openssl/md5.h.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * The following changes have been made to the original sources:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * replaced CC_LONG w/ mDNSu32
4b22b9337f359bfd063322244f5336cc7c6ffcfars * replaced CC_MD5* with MD5*
4b22b9337f359bfd063322244f5336cc7c6ffcfars * replaced CC_LONG w/ mDNSu32, removed conditional #defines from md5.h
4b22b9337f359bfd063322244f5336cc7c6ffcfars * removed extern decls for MD5_Init/Update/Final from CommonDigest.h
4b22b9337f359bfd063322244f5336cc7c6ffcfars * removed APPLE_COMMON_DIGEST specific #defines from md5_locl.h
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Note: machine archetecure specific conditionals from the original sources are turned off, but are left in the code
4b22b9337f359bfd063322244f5336cc7c6ffcfars * to aid in platform-specific optimizations and debugging.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Sources originally distributed under the following license headers:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * ====================================================================
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Redistribution and use in source and binary forms, with or without
4b22b9337f359bfd063322244f5336cc7c6ffcfars * modification, are permitted provided that the following conditions
4b22b9337f359bfd063322244f5336cc7c6ffcfars * are met:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 1. Redistributions of source code must retain the above copyright
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * notice, this list of conditions and the following disclaimer.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 2. Redistributions in binary form must reproduce the above copyright
4b22b9337f359bfd063322244f5336cc7c6ffcfars * notice, this list of conditions and the following disclaimer in
4b22b9337f359bfd063322244f5336cc7c6ffcfars * the documentation and/or other materials provided with the
4b22b9337f359bfd063322244f5336cc7c6ffcfars * distribution.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 3. All advertising materials mentioning features or use of this
4b22b9337f359bfd063322244f5336cc7c6ffcfars * software must display the following acknowledgment:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * "This product includes software developed by the OpenSSL Project
4b22b9337f359bfd063322244f5336cc7c6ffcfars * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
4b22b9337f359bfd063322244f5336cc7c6ffcfars * endorse or promote products derived from this software without
4b22b9337f359bfd063322244f5336cc7c6ffcfars * prior written permission. For written permission, please contact
4b22b9337f359bfd063322244f5336cc7c6ffcfars * licensing@OpenSSL.org.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 5. Products derived from this software may not be called "OpenSSL"
4b22b9337f359bfd063322244f5336cc7c6ffcfars * nor may "OpenSSL" appear in their names without prior written
4b22b9337f359bfd063322244f5336cc7c6ffcfars * permission of the OpenSSL Project.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 6. Redistributions of any form whatsoever must retain the following
4b22b9337f359bfd063322244f5336cc7c6ffcfars * acknowledgment:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * "This product includes software developed by the OpenSSL Project
4b22b9337f359bfd063322244f5336cc7c6ffcfars * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
4b22b9337f359bfd063322244f5336cc7c6ffcfars * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4b22b9337f359bfd063322244f5336cc7c6ffcfars * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4b22b9337f359bfd063322244f5336cc7c6ffcfars * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4b22b9337f359bfd063322244f5336cc7c6ffcfars * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
4b22b9337f359bfd063322244f5336cc7c6ffcfars * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4b22b9337f359bfd063322244f5336cc7c6ffcfars * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4b22b9337f359bfd063322244f5336cc7c6ffcfars * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4b22b9337f359bfd063322244f5336cc7c6ffcfars * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4b22b9337f359bfd063322244f5336cc7c6ffcfars * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4b22b9337f359bfd063322244f5336cc7c6ffcfars * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4b22b9337f359bfd063322244f5336cc7c6ffcfars * OF THE POSSIBILITY OF SUCH DAMAGE.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * ====================================================================
4b22b9337f359bfd063322244f5336cc7c6ffcfars * This product includes cryptographic software written by Eric Young
4b22b9337f359bfd063322244f5336cc7c6ffcfars * (eay@cryptsoft.com). This product includes software written by Tim
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Hudson (tjh@cryptsoft.com).
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4b22b9337f359bfd063322244f5336cc7c6ffcfars * All rights reserved.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * This package is an SSL implementation written
4b22b9337f359bfd063322244f5336cc7c6ffcfars * by Eric Young (eay@cryptsoft.com).
4b22b9337f359bfd063322244f5336cc7c6ffcfars * The implementation was written so as to conform with Netscapes SSL.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * This library is free for commercial and non-commercial use as long as
4b22b9337f359bfd063322244f5336cc7c6ffcfars * the following conditions are aheared to. The following conditions
4b22b9337f359bfd063322244f5336cc7c6ffcfars * apply to all code found in this distribution, be it the RC4, RSA,
4b22b9337f359bfd063322244f5336cc7c6ffcfars * lhash, DES, etc., code; not just the SSL code. The SSL documentation
4b22b9337f359bfd063322244f5336cc7c6ffcfars * included with this distribution is covered by the same copyright terms
4b22b9337f359bfd063322244f5336cc7c6ffcfars * except that the holder is Tim Hudson (tjh@cryptsoft.com).
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Copyright remains Eric Young's, and as such any Copyright notices in
4b22b9337f359bfd063322244f5336cc7c6ffcfars * the code are not to be removed.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * If this package is used in a product, Eric Young should be given attribution
4b22b9337f359bfd063322244f5336cc7c6ffcfars * as the author of the parts of the library used.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * This can be in the form of a textual message at program startup or
4b22b9337f359bfd063322244f5336cc7c6ffcfars * in documentation (online or textual) provided with the package.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Redistribution and use in source and binary forms, with or without
4b22b9337f359bfd063322244f5336cc7c6ffcfars * modification, are permitted provided that the following conditions
4b22b9337f359bfd063322244f5336cc7c6ffcfars * are met:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 1. Redistributions of source code must retain the copyright
4b22b9337f359bfd063322244f5336cc7c6ffcfars * notice, this list of conditions and the following disclaimer.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 2. Redistributions in binary form must reproduce the above copyright
4b22b9337f359bfd063322244f5336cc7c6ffcfars * notice, this list of conditions and the following disclaimer in the
4b22b9337f359bfd063322244f5336cc7c6ffcfars * documentation and/or other materials provided with the distribution.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 3. All advertising materials mentioning features or use of this software
4b22b9337f359bfd063322244f5336cc7c6ffcfars * must display the following acknowledgement:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * "This product includes cryptographic software written by
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Eric Young (eay@cryptsoft.com)"
4b22b9337f359bfd063322244f5336cc7c6ffcfars * The word 'cryptographic' can be left out if the rouines from the library
4b22b9337f359bfd063322244f5336cc7c6ffcfars * being used are not cryptographic related :-).
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * 4. If you include any Windows specific code (or a derivative thereof) from
4b22b9337f359bfd063322244f5336cc7c6ffcfars * the apps directory (application code) you must include an acknowledgement:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4b22b9337f359bfd063322244f5336cc7c6ffcfars * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4b22b9337f359bfd063322244f5336cc7c6ffcfars * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4b22b9337f359bfd063322244f5336cc7c6ffcfars * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4b22b9337f359bfd063322244f5336cc7c6ffcfars * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4b22b9337f359bfd063322244f5336cc7c6ffcfars * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4b22b9337f359bfd063322244f5336cc7c6ffcfars * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4b22b9337f359bfd063322244f5336cc7c6ffcfars * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4b22b9337f359bfd063322244f5336cc7c6ffcfars * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4b22b9337f359bfd063322244f5336cc7c6ffcfars * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4b22b9337f359bfd063322244f5336cc7c6ffcfars * SUCH DAMAGE.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * The licence and distribution terms for any publically available version or
4b22b9337f359bfd063322244f5336cc7c6ffcfars * derivative of this code cannot be changed. i.e. this code cannot simply be
4b22b9337f359bfd063322244f5336cc7c6ffcfars * copied and put under another distribution licence
4b22b9337f359bfd063322244f5336cc7c6ffcfars * [including the GNU Public Licence.]
4b22b9337f359bfd063322244f5336cc7c6ffcfars# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__INTEL__)
4b22b9337f359bfd063322244f5336cc7c6ffcfars# elif defined(__sparc) && defined(OPENSSL_SYS_ULTRASPARC)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soomevoid md5_block_asm_data_order_aligned (MD5_CTX *c, const mDNSu32 *p,int num);
4b22b9337f359bfd063322244f5336cc7c6ffcfars# define HASH_BLOCK_DATA_ORDER_ALIGNED md5_block_asm_data_order_aligned
4b22b9337f359bfd063322244f5336cc7c6ffcfarsvoid md5_block_host_order (MD5_CTX *c, const void *p,int num);
4b22b9337f359bfd063322244f5336cc7c6ffcfarsvoid md5_block_data_order (MD5_CTX *c, const void *p,int num);
4b22b9337f359bfd063322244f5336cc7c6ffcfars#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__INTEL__)
4b22b9337f359bfd063322244f5336cc7c6ffcfars * *_block_host_order is expected to handle aligned data while
4b22b9337f359bfd063322244f5336cc7c6ffcfars * *_block_data_order - unaligned. As algorithm and host (x86)
4b22b9337f359bfd063322244f5336cc7c6ffcfars * are in this case of the same "endianness" these two are
4b22b9337f359bfd063322244f5336cc7c6ffcfars * otherwise indistinguishable. But normally you don't want to
4b22b9337f359bfd063322244f5336cc7c6ffcfars * call the same function because unaligned access in places
4b22b9337f359bfd063322244f5336cc7c6ffcfars * where alignment is expected is usually a "Bad Thing". Indeed,
4b22b9337f359bfd063322244f5336cc7c6ffcfars * on RISCs you get punished with BUS ERROR signal or *severe*
4b22b9337f359bfd063322244f5336cc7c6ffcfars * performance degradation. Intel CPUs are in turn perfectly
4b22b9337f359bfd063322244f5336cc7c6ffcfars * capable of loading unaligned data without such drastic side
4b22b9337f359bfd063322244f5336cc7c6ffcfars * effect. Yes, they say it's slower than aligned load, but no
4b22b9337f359bfd063322244f5336cc7c6ffcfars * exception is generated and therefore performance degradation
4b22b9337f359bfd063322244f5336cc7c6ffcfars * is *incomparable* with RISCs. What we should weight here is
4b22b9337f359bfd063322244f5336cc7c6ffcfars * costs of unaligned access against costs of aligning data.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * According to my measurements allowing unaligned access results
4b22b9337f359bfd063322244f5336cc7c6ffcfars * in ~9% performance improvement on Pentium II operating at
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 266MHz. I won't be surprised if the difference will be higher
4b22b9337f359bfd063322244f5336cc7c6ffcfars * on faster systems:-)
4b22b9337f359bfd063322244f5336cc7c6ffcfars * <appro@fy.chalmers.se>
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome unsigned long ll; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define HASH_BLOCK_HOST_ORDER md5_block_host_order
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define HASH_BLOCK_DATA_ORDER md5_block_data_order
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Little-endians (Intel and Alpha) feel better without this.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * It looks like memcpy does better job than generic
4b22b9337f359bfd063322244f5336cc7c6ffcfars * md5_block_data_order on copying-n-aligning input data.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * But frankly speaking I didn't expect such result on Alpha.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * On the other hand I've got this with egcs-1.0.2 and if
4b22b9337f359bfd063322244f5336cc7c6ffcfars * program is compiled with another (better?) compiler it
4b22b9337f359bfd063322244f5336cc7c6ffcfars * might turn out other way around.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * <appro@fy.chalmers.se>
4b22b9337f359bfd063322244f5336cc7c6ffcfars * This is a generic 32 bit "collector" for message digest algorithms.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Whenever needed it collects input character stream into chunks of
4b22b9337f359bfd063322244f5336cc7c6ffcfars * 32 bit values and invokes a block function that performs actual hash
4b22b9337f359bfd063322244f5336cc7c6ffcfars * calculations.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Porting guide.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Obligatory macros:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
4b22b9337f359bfd063322244f5336cc7c6ffcfars * this macro defines byte order of input stream.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_CBLOCK
4b22b9337f359bfd063322244f5336cc7c6ffcfars * size of a unit chunk HASH_BLOCK operates on.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_LONG
4b22b9337f359bfd063322244f5336cc7c6ffcfars * has to be at lest 32 bit wide, if it's wider, then
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_LONG_LOG2 *has to* be defined along
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_CTX
4b22b9337f359bfd063322244f5336cc7c6ffcfars * context structure that at least contains following
4b22b9337f359bfd063322244f5336cc7c6ffcfars * members:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * typedef struct {
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_LONG Nl,Nh;
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_LONG data[HASH_LBLOCK];
4b22b9337f359bfd063322244f5336cc7c6ffcfars * int num;
4b22b9337f359bfd063322244f5336cc7c6ffcfars * } HASH_CTX;
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_UPDATE
4b22b9337f359bfd063322244f5336cc7c6ffcfars * name of "Update" function, implemented here.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_TRANSFORM
4b22b9337f359bfd063322244f5336cc7c6ffcfars * name of "Transform" function, implemented here.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_FINAL
4b22b9337f359bfd063322244f5336cc7c6ffcfars * name of "Final" function, implemented here.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_BLOCK_HOST_ORDER
4b22b9337f359bfd063322244f5336cc7c6ffcfars * name of "block" function treating *aligned* input message
4b22b9337f359bfd063322244f5336cc7c6ffcfars * in host byte order, implemented externally.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_BLOCK_DATA_ORDER
4b22b9337f359bfd063322244f5336cc7c6ffcfars * name of "block" function treating *unaligned* input message
4b22b9337f359bfd063322244f5336cc7c6ffcfars * in original (data) byte order, implemented externally (it
4b22b9337f359bfd063322244f5336cc7c6ffcfars * actually is optional if data and host are of the same
4b22b9337f359bfd063322244f5336cc7c6ffcfars * "endianess").
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_MAKE_STRING
4b22b9337f359bfd063322244f5336cc7c6ffcfars * macro convering context variables to an ASCII hash string.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Optional macros:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * B_ENDIAN or L_ENDIAN
4b22b9337f359bfd063322244f5336cc7c6ffcfars * defines host byte-order.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_LONG_LOG2
4b22b9337f359bfd063322244f5336cc7c6ffcfars * defaults to 2 if not states otherwise.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_LBLOCK
4b22b9337f359bfd063322244f5336cc7c6ffcfars * assumed to be HASH_CBLOCK/4 if not stated otherwise.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * HASH_BLOCK_DATA_ORDER_ALIGNED
4b22b9337f359bfd063322244f5336cc7c6ffcfars * alternative "block" function capable of treating
4b22b9337f359bfd063322244f5336cc7c6ffcfars * aligned input message in original (data) order,
4b22b9337f359bfd063322244f5336cc7c6ffcfars * implemented externally.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * MD5 example:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define DATA_ORDER_IS_LITTLE_ENDIAN
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_LONG mDNSu32
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_LONG_LOG2 mDNSu32_LOG2
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_CTX MD5_CTX
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_CBLOCK MD5_CBLOCK
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_LBLOCK MD5_LBLOCK
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_UPDATE MD5_Update
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_TRANSFORM MD5_Transform
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_FINAL MD5_Final
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_BLOCK_HOST_ORDER md5_block_host_order
4b22b9337f359bfd063322244f5336cc7c6ffcfars * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
4b22b9337f359bfd063322244f5336cc7c6ffcfars * <appro@fy.chalmers.se>
4b22b9337f359bfd063322244f5336cc7c6ffcfars#if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Moved below as it's required only if HASH_BLOCK_DATA_ORDER_ALIGNED
4b22b9337f359bfd063322244f5336cc7c6ffcfars * isn't defined.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Engage compiler specific rotate intrinsic function if available.
4b22b9337f359bfd063322244f5336cc7c6ffcfars# if 0 /* defined(_MSC_VER) */
4b22b9337f359bfd063322244f5336cc7c6ffcfars# if defined(__POWERPC__)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define ROTATE(a,n) (unsigned MD32_REG_T)__rlwinm((int)a,n,0,31)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* Motorola specific tweak. <appro@fy.chalmers.se> */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define ROTATE(a,n) (n<24 ? __rol(a,n) : __ror(a,32-n))
4b22b9337f359bfd063322244f5336cc7c6ffcfars# elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Some GNU C inline assembler templates. Note that these are
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * rotates by *constant* number of bits! But that's exactly
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * what we need here...
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * <appro@fy.chalmers.se>
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * LLVM is more strict about compatibility of types between input & output constraints,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * but we want these to be rotations of 32 bits, not 64, so we explicitly drop the
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * most significant bytes by casting to an unsigned int.
4b22b9337f359bfd063322244f5336cc7c6ffcfars# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define ROTATE(a,n) ({ register unsigned int ret; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome "roll %1,%0" \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define ROTATE(a,n) ({ register unsigned int ret; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome "rlwinm %0,%1,%2,0,31" \
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Engage compiler specific "fetch in reverse byte order"
4b22b9337f359bfd063322244f5336cc7c6ffcfars * intrinsic function if available.
4b22b9337f359bfd063322244f5336cc7c6ffcfars# if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome/* some GNU C inline assembler templates by <appro@fy.chalmers.se> */
4b22b9337f359bfd063322244f5336cc7c6ffcfars# if (defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)) && !defined(I386_ONLY)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define BE_FETCH32(a) ({ register unsigned int l=(a); \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome "bswapl %0" \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define LE_FETCH32(a) ({ register unsigned int l; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome "lwbrx %0,0,%1" \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome : "=r" (l) \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome : "r" (a)); \
4b22b9337f359bfd063322244f5336cc7c6ffcfars# elif defined(__sparc) && defined(OPENSSL_SYS_ULTRASPARC)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define LE_FETCH32(a) ({ register unsigned int l; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome "lda [%1]#ASI_PRIMARY_LITTLE,%0" \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome : "=r" (l) \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome : "r" (a)); \
4b22b9337f359bfd063322244f5336cc7c6ffcfars#endif /* PEDANTIC */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#if HASH_LONG_LOG2==2 /* Engage only if sizeof(HASH_LONG)== 4 */
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* 5 instructions with rotate instruction, else 9 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l=*(const HASH_LONG *)(a), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome ((ROTATE(l,8)&0x00FF00FF)|(ROTATE((l&0x00FF00FF),24))) \
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* 6 instructions with rotate instruction, else 8 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l=*(const HASH_LONG *)(a), \
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Originally the middle line started with l=(((l&0xFF00FF00)>>8)|...
4b22b9337f359bfd063322244f5336cc7c6ffcfars * It's rewritten as above for two reasons:
4b22b9337f359bfd063322244f5336cc7c6ffcfars * - RISCs aren't good at long constants and have to explicitely
4b22b9337f359bfd063322244f5336cc7c6ffcfars * compose 'em with several (well, usually 2) instructions in a
4b22b9337f359bfd063322244f5336cc7c6ffcfars * register before performing the actual operation and (as you
4b22b9337f359bfd063322244f5336cc7c6ffcfars * already realized:-) having same constant should inspire the
4b22b9337f359bfd063322244f5336cc7c6ffcfars * compiler to permanently allocate the only register for it;
4b22b9337f359bfd063322244f5336cc7c6ffcfars * - most modern CPUs have two ALUs, but usually only one has
4b22b9337f359bfd063322244f5336cc7c6ffcfars * circuitry for shifts:-( this minor tweak inspires compiler
4b22b9337f359bfd063322244f5336cc7c6ffcfars * to schedule shift instructions in a better way...
4b22b9337f359bfd063322244f5336cc7c6ffcfars * <appro@fy.chalmers.se>
4b22b9337f359bfd063322244f5336cc7c6ffcfars#define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
4b22b9337f359bfd063322244f5336cc7c6ffcfars * and HASH_BLOCK_HOST_ORDER ought to be the same if input data
4b22b9337f359bfd063322244f5336cc7c6ffcfars * and host are of the same "endianess". It's possible to mask
4b22b9337f359bfd063322244f5336cc7c6ffcfars * this with blank #define HASH_BLOCK_DATA_ORDER though...
4b22b9337f359bfd063322244f5336cc7c6ffcfars * <appro@fy.chalmers.se>
4b22b9337f359bfd063322244f5336cc7c6ffcfars#if defined(B_ENDIAN)
4b22b9337f359bfd063322244f5336cc7c6ffcfars# if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
4b22b9337f359bfd063322244f5336cc7c6ffcfars# if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// None of the invocations of the following macros actually use the result,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// so cast them to void to avoid any compiler warnings/errors about not using
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// the result (e.g. when using clang).
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// If the resultant values need to be used at some point, these must be changed.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define _HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l|=(((unsigned long)(*((c)++)))<<16), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l|=(((unsigned long)(*((c)++)))<< 8), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l|=(((unsigned long)(*((c)++))) ), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome switch (n) { \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome case 0: l =((unsigned long)(*((c)++)))<<24; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome case 3: l|=((unsigned long)(*((c)++))); \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome switch (sc) { \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome case 0: l =((unsigned long)(*((c)++)))<<24; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (--len == 0) break; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (--len == 0) break; \
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* NOTE the pointer is not incremented at the end of this */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l=0; (c)+=n; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome switch (n) { \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define _HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome *((c)++)=(unsigned char)(((l) )&0xff), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define _HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l|=(((unsigned long)(*((c)++)))<< 8), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l|=(((unsigned long)(*((c)++)))<<16), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l|=(((unsigned long)(*((c)++)))<<24), \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome switch (n) { \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome case 0: l =((unsigned long)(*((c)++))); \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome switch (sc) { \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome case 0: l =((unsigned long)(*((c)++))); \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (--len == 0) break; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (--len == 0) break; \
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* NOTE the pointer is not incremented at the end of this */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l=0; (c)+=n; \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome switch (n) { \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome case 1: l|=((unsigned long)(*(--(c)))); \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define _HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Time for some action:-)
4b22b9337f359bfd063322244f5336cc7c6ffcfarsint HASH_UPDATE (HASH_CTX *c, const void *data_, unsigned long len)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome const unsigned char *data=(const unsigned char *)data_;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome register unsigned long l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Wei Dai <weidai@eskimo.com> for pointing it out. */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (c->num != 0)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* drop through and do the rest */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if ((sc+len) < 4) /* ugly, add char's to a word */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * only if sizeof(HASH_LONG)==4.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* data is properly aligned so that we can cast it: */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,sw);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSPlatformMemCopy(p=c->data,data,HASH_CBLOCK);
4b22b9337f359bfd063322244f5336cc7c6ffcfarsvoid HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* data is properly aligned so that we can cast it: */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome register unsigned long l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome register int i,j;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome static const unsigned char end[4]={0x80,0x00,0x00,0x00};
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* c->num should definitly have room for at least one more byte. */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* purify often complains about the following line as an
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Uninitialized Memory Read. While this can be true, the
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * following p_c2l macro will reset l when that case is true.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * This is because j&0x03 contains the number of 'valid' bytes
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * already in p[i]. If and only if j&0x03 == 0, the UMR will
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * occur but this is also the only time p_c2l will do
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * l= *(cp++) instead of l|= *(cp++)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Many thanks to Alex Tang <altitude@cic.net> for pickup this
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * 'potential bug' */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (j==0) p[i]=0; /* Yeah, but that's not the way to fix it:-) */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome l = (j==0) ? 0 : p[i];
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome HOST_p_c2l(cp,l,j); p[i++]=l; /* i is the next 'undefined word' */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (i>(HASH_LBLOCK-2)) /* save room for Nl and Nh */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (i<HASH_LBLOCK) p[i]=0;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * but I'm not worried :-)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome OPENSSL_cleanse((void *)c,sizeof(HASH_CTX));
4b22b9337f359bfd063322244f5336cc7c6ffcfars * This comment was originaly written for MD5, which is why it
4b22b9337f359bfd063322244f5336cc7c6ffcfars * discusses A-D. But it basically applies to all 32-bit digests,
4b22b9337f359bfd063322244f5336cc7c6ffcfars * which is why it was moved to common header file.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * In case you wonder why A-D are declared as long and not
4b22b9337f359bfd063322244f5336cc7c6ffcfars * as mDNSu32. Doing so results in slight performance
4b22b9337f359bfd063322244f5336cc7c6ffcfars * boost on LP64 architectures. The catch is we don't
4b22b9337f359bfd063322244f5336cc7c6ffcfars * really care if 32 MSBs of a 64-bit register get polluted
4b22b9337f359bfd063322244f5336cc7c6ffcfars * with eventual overflows as we *save* only 32 LSBs in
4b22b9337f359bfd063322244f5336cc7c6ffcfars * *either* case. Now declaring 'em long excuses the compiler
4b22b9337f359bfd063322244f5336cc7c6ffcfars * from keeping 32 MSBs zeroed resulting in 13% performance
4b22b9337f359bfd063322244f5336cc7c6ffcfars * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Well, to be honest it should say that this *prevents*
4b22b9337f359bfd063322244f5336cc7c6ffcfars * performance degradation.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * <appro@fy.chalmers.se>
4b22b9337f359bfd063322244f5336cc7c6ffcfars * Apparently there're LP64 compilers that generate better
4b22b9337f359bfd063322244f5336cc7c6ffcfars * code if A-D are declared int. Most notably GCC-x86_64
4b22b9337f359bfd063322244f5336cc7c6ffcfars * generates better code.
4b22b9337f359bfd063322244f5336cc7c6ffcfars * <appro@fy.chalmers.se>
4b22b9337f359bfd063322244f5336cc7c6ffcfars// from md5_locl.h (continued)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome #define F(x,y,z) (((x) & (y)) | ((~(x)) & (z)))
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome #define G(x,y,z) (((x) & (z)) | ((y) & (~(z))))
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be
4b22b9337f359bfd063322244f5336cc7c6ffcfars * simplified to the code below. Wei attributes these optimizations
4b22b9337f359bfd063322244f5336cc7c6ffcfars * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define F(b,c,d) ((((c) ^ (d)) & (b)) ^ (d))
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define G(b,c,d) ((((b) ^ (c)) & (d)) ^ (c))
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define H(b,c,d) ((b) ^ (c) ^ (d))
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome#define I(b,c,d) (((~(d)) | (b)) ^ (c))
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome a+=((k)+(t)+F((b),(c),(d))); \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome a+=((k)+(t)+G((b),(c),(d))); \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome a+=((k)+(t)+H((b),(c),(d))); \
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome a+=((k)+(t)+I((b),(c),(d))); \
4b22b9337f359bfd063322244f5336cc7c6ffcfars/* Implemented from RFC1321 The MD5 Message-Digest Algorithm
4b22b9337f359bfd063322244f5336cc7c6ffcfarsvoid md5_block_host_order (MD5_CTX *c, const void *data, int num)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome register unsigned MD32_REG_T A,B,C,D;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Round 0 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Round 1 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Round 2 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Round 3 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome A = c->A += A;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome B = c->B += B;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome C = c->C += C;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome D = c->D += D;
4b22b9337f359bfd063322244f5336cc7c6ffcfarsvoid md5_block_data_order (MD5_CTX *c, const void *data_, int num)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome register unsigned MD32_REG_T A,B,C,D,l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* See comment in crypto/sha/sha_locl.h for details. */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome unsigned MD32_REG_T XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7,
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define X(i) XX ## i
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome# define X(i) XX[i]
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome HOST_c2l(data,l); X( 0)=l; HOST_c2l(data,l); X( 1)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Round 0 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(A,B,C,D,X( 0), 7,0xd76aa478L); HOST_c2l(data,l); X( 2)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(D,A,B,C,X( 1),12,0xe8c7b756L); HOST_c2l(data,l); X( 3)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(C,D,A,B,X( 2),17,0x242070dbL); HOST_c2l(data,l); X( 4)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(B,C,D,A,X( 3),22,0xc1bdceeeL); HOST_c2l(data,l); X( 5)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(A,B,C,D,X( 4), 7,0xf57c0fafL); HOST_c2l(data,l); X( 6)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(D,A,B,C,X( 5),12,0x4787c62aL); HOST_c2l(data,l); X( 7)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(C,D,A,B,X( 6),17,0xa8304613L); HOST_c2l(data,l); X( 8)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(B,C,D,A,X( 7),22,0xfd469501L); HOST_c2l(data,l); X( 9)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(A,B,C,D,X( 8), 7,0x698098d8L); HOST_c2l(data,l); X(10)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(D,A,B,C,X( 9),12,0x8b44f7afL); HOST_c2l(data,l); X(11)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(C,D,A,B,X(10),17,0xffff5bb1L); HOST_c2l(data,l); X(12)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(B,C,D,A,X(11),22,0x895cd7beL); HOST_c2l(data,l); X(13)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(A,B,C,D,X(12), 7,0x6b901122L); HOST_c2l(data,l); X(14)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome R0(D,A,B,C,X(13),12,0xfd987193L); HOST_c2l(data,l); X(15)=l;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Round 1 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Round 2 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Round 3 */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome A = c->A += A;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome B = c->B += B;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome C = c->C += C;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome D = c->D += D;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// ***************************************************************************
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soomestatic const char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4b22b9337f359bfd063322244f5336cc7c6ffcfars#define mDNSisspace(x) (x == '\t' || x == '\n' || x == '\v' || x == '\f' || x == '\r' || x == ' ')
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomemDNSlocal const char *mDNSstrchr(const char *s, int c)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (c == *s) return s;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (!*s) return mDNSNULL;
4b22b9337f359bfd063322244f5336cc7c6ffcfars// skips all whitespace anywhere.
4b22b9337f359bfd063322244f5336cc7c6ffcfars// converts characters, four at a time, starting at (or after)
4b22b9337f359bfd063322244f5336cc7c6ffcfars// src from base - 64 numbers into three 8 bit bytes in the target area.
4b22b9337f359bfd063322244f5336cc7c6ffcfars// it returns the number of data bytes stored at the target, or -1 on error.
4b22b9337f359bfd063322244f5336cc7c6ffcfars// adapted from BIND sources
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomemDNSlocal mDNSs32 DNSDigest_Base64ToBin(const char *src, mDNSu8 *target, mDNSu32 targsize)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (mDNSisspace(ch)) /* Skip whitespace anywhere. */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome target[tarindex] = (mDNSu8)((pos - Base64) << 2);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome target[tarindex+1] = (mDNSu8)(((pos - Base64) & 0x0f) << 4);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome target[tarindex+1] = (mDNSu8)(((pos - Base64) & 0x03) << 6);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * We are done decoding Base-64 chars. Let's see if we ended
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * on a byte boundary, and/or with erroneous trailing characters.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome case 0: /* Invalid = in first position */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Skip any number of spaces. */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Make sure there is another trailing = sign. */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* Fall through to "single trailing =" case. */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome /* FALLTHROUGH */
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * We know this char is an =. Is there anything but
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * whitespace after it?
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * Now make sure for cases 2 and 3 that the "extra"
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * bits that slopped past the last full byte were
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * zeros. If we don't check them, they become a
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * subliminal channel.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * We ended by seeing the end of the string. Make sure we
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome * have no partial bytes lying around.
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome return (-1);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome// ***************************************************************************
4b22b9337f359bfd063322244f5336cc7c6ffcfars// Constants
4b22b9337f359bfd063322244f5336cc7c6ffcfars#define HMAC_MD5_AlgName (*(const domainname*) "\010" "hmac-md5" "\007" "sig-alg" "\003" "reg" "\003" "int")
4b22b9337f359bfd063322244f5336cc7c6ffcfars// Adapted from Appendix, RFC 2104
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomemDNSlocal void DNSDigest_ConstructHMACKey(DomainAuthInfo *info, const mDNSu8 *key, mDNSu32 len)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // If key is longer than HMAC_LEN reset it to MD5(key)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // store key in pads
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSPlatformMemZero(info->keydata_ipad, HMAC_LEN);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSPlatformMemZero(info->keydata_opad, HMAC_LEN);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSPlatformMemCopy(info->keydata_ipad, key, len);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSPlatformMemCopy(info->keydata_opad, key, len);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // XOR key with ipad and opad values
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome for (i = 0; i < HMAC_LEN; i++)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomemDNSexport mDNSs32 DNSDigest_ConstructHMACKeyfromBase64(DomainAuthInfo *info, const char *b64key)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSs32 keylen = DNSDigest_Base64ToBin(b64key, keybuf, sizeof(keybuf));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome DNSDigest_ConstructHMACKey(info, keybuf, (mDNSu32)keylen);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomemDNSexport void DNSDigest_SignMessage(DNSMessage *msg, mDNSu8 **end, DomainAuthInfo *info, mDNSu16 tcode)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSu8 *rdata, *const countPtr = (mDNSu8 *)&msg->h.numAdditionals; // Get existing numAdditionals value
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSu16 numAdditionals = (mDNSu16)((mDNSu16)countPtr[0] << 8 | countPtr[1]);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Init MD5 context, digest inner key pad and message
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, (mDNSu8 *)msg, (unsigned long)(*end - (mDNSu8 *)msg));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Construct TSIG RR, digesting variables as apporpriate
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNS_SetupResourceRecord(&tsig, mDNSNULL, 0, kDNSType_TSIG, 0, kDNSRecordTypeKnownUnique, AuthRecordAny, mDNSNULL, mDNSNULL);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome AssignDomainName(&tsig.namestorage, &info->keyname);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, info->keyname.c, DomainNameLength(&info->keyname));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, (mDNSu8 *)&tsig.resrec.rroriginalttl, sizeof(tsig.resrec.rroriginalttl));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome AssignDomainName(&tsig.resrec.rdata->u.name, &HMAC_MD5_AlgName);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // get UTC (universal time), convert to 48-bit unsigned in network byte order
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (utc32 == (unsigned)-1) { LogMsg("ERROR: DNSDigest_SignMessage - mDNSPlatformUTC returned bad time -1"); *end = mDNSNULL; }
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // 300 sec is fudge recommended in RFC 2485
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // digest error (tcode) and other data len (zero) - we'll add them to the rdata later
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16)); // error
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16)); // other data len
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // finish the message & tsig var hash
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // perform outer MD5 (outer key pad, inner digest)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // set remaining rdata fields
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSPlatformMemCopy(rdata, digest, MD5_LEN); // MAC
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome tsig.resrec.rdlength = (mDNSu16)(rdata - tsig.resrec.rdata->u.data);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome *end = PutResourceRecordTTLJumbo(msg, ptr, &numAdditionals, &tsig.resrec, 0);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (!*end) { LogMsg("ERROR: DNSDigest_SignMessage - could not put TSIG"); *end = mDNSNULL; return; }
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Write back updated numAdditionals value
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas SoomemDNSexport mDNSBool DNSDigest_VerifyMessage(DNSMessage *msg, mDNSu8 *end, LargeCacheRecord * lcr, DomainAuthInfo *info, mDNSu16 * rcode, mDNSu16 * tcode)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome mDNSu8 * ptr = (mDNSu8*) &lcr->r.resrec.rdata->u.data;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // We only support HMAC-MD5 for now
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome LogMsg("ERROR: DNSDigest_VerifyMessage - TSIG algorithm not supported: %##s", algo->c);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Check the times
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome LogMsg("ERROR: DNSDigest_VerifyMessage - mDNSPlatformUTC returned bad time -1");
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Get the 48 bit time field, skipping over the first word
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome then = (mDNSs32)NToH32(utc48 + sizeof(mDNSu16));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome delta = (now > then) ? now - then : then - now;
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome LogMsg("ERROR: DNSDigest_VerifyMessage - time skew > %d", fudge);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Init MD5 context, digest inner key pad and message
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, (mDNSu8*) msg, (unsigned long)(end - (mDNSu8*) msg));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, lcr->r.resrec.name->c, DomainNameLength(lcr->r.resrec.name));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Class name
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome buf = mDNSOpaque16fromIntVal(lcr->r.resrec.rrclass);
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, (mDNSu8*) &lcr->r.resrec.rroriginalttl, sizeof(lcr->r.resrec.rroriginalttl));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Algorithm
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, algo->c, DomainNameLength(algo));
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Digest error and other data len (both zero) - we'll add them to the rdata later
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16)); // error
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome MD5_Update(&c, buf.b, sizeof(mDNSOpaque16)); // other data len
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // Finish the message & tsig var hash
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // perform outer MD5 (outer key pad, inner digest)
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome if (!mDNSPlatformMemSame(thisDigest, thatDigest, MD5_LEN))
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome LogMsg("ERROR: DNSDigest_VerifyMessage - bad signature");
5ffb0c9b03b5149ff4f5821a62be4a52408ada2aToomas Soome // set remaining rdata fields