components/openssl/openssl-1.0.1/engines/t4/eng_t4_md5.c
branchs11-update
changeset 2593 b92e6df5eaf0
parent 932 a3abdb042ced
equal deleted inserted replaced
2592:a7d8d41eeab2 2593:b92e6df5eaf0
       
     1 /*
       
     2  * This product includes cryptographic software developed by the OpenSSL
       
     3  * Project for use in the OpenSSL Toolkit (http://www.openssl.org/). This
       
     4  * product includes cryptographic software written by Eric Young
       
     5  * ([email protected]).
       
     6  */
       
     7 
       
     8 /*
       
     9  * ====================================================================
       
    10  * Copyright (c) 1998-2011 The OpenSSL Project.  All rights reserved.
       
    11  *
       
    12  * Redistribution and use in source and binary forms, with or without
       
    13  * modification, are permitted provided that the following conditions
       
    14  * are met:
       
    15  *
       
    16  * 1. Redistributions of source code must retain the above copyright
       
    17  *    notice, this list of conditions and the following disclaimer.
       
    18  *
       
    19  * 2. Redistributions in binary form must reproduce the above copyright
       
    20  *    notice, this list of conditions and the following disclaimer in
       
    21  *    the documentation and/or other materials provided with the
       
    22  *    distribution.
       
    23  *
       
    24  * 3. All advertising materials mentioning features or use of this
       
    25  *    software must display the following acknowledgment:
       
    26  *    "This product includes software developed by the OpenSSL Project
       
    27  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
       
    28  *
       
    29  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
       
    30  *    endorse or promote products derived from this software without
       
    31  *    prior written permission. For written permission, please contact
       
    32  *    [email protected].
       
    33  *
       
    34  * 5. Products derived from this software may not be called "OpenSSL"
       
    35  *    nor may "OpenSSL" appear in their names without prior written
       
    36  *    permission of the OpenSSL Project.
       
    37  *
       
    38  * 6. Redistributions of any form whatsoever must retain the following
       
    39  *    acknowledgment:
       
    40  *    "This product includes software developed by the OpenSSL Project
       
    41  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
       
    42  *
       
    43  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
       
    44  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    46  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
       
    47  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    48  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
       
    49  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    50  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    51  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
       
    52  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    53  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
       
    54  * OF THE POSSIBILITY OF SUCH DAMAGE.
       
    55  * ====================================================================
       
    56  */
       
    57 
       
    58 /*
       
    59  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
       
    60  */
       
    61 
       
    62 /*
       
    63  * This engine supports SPARC microprocessors that provide AES and other
       
    64  * cipher and hash instructions, such as the T4 microprocessor.
       
    65  *
       
    66  * This file implements the MD5 message digest operations.
       
    67  */
       
    68 
       
    69 #include <openssl/opensslconf.h>
       
    70 
       
    71 #if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_MD_T4)
       
    72 #ifndef	OPENSSL_NO_MD5
       
    73 
       
    74 #include <sys/types.h>
       
    75 #include <stdlib.h>
       
    76 #include <string.h>
       
    77 #include <errno.h>
       
    78 
       
    79 #include <openssl/aes.h>
       
    80 #include <openssl/engine.h>
       
    81 /*
       
    82  * Solaris sys/md5.h and OpenSSL openssl/md5.h both define MD5_CTX.
       
    83  * The OpenSSL MD5_CTX has an extra "num" field at the end.
       
    84  */
       
    85 #include <openssl/md5.h>
       
    86 
       
    87 #if (defined(sun4v) || defined(__sparcv9) || defined(__sparcv8plus) || \
       
    88 	defined(__sparcv8)) && !defined(OPENSSL_NO_ASM)
       
    89 #define	COMPILE_HW_T4
       
    90 #endif
       
    91 
       
    92 #ifdef	COMPILE_HW_T4
       
    93 
       
    94 /* Assembly language function; replaces C function md5_block_data_order(): */
       
    95 extern void t4_md5_multiblock(MD5_CTX *ctx, const uint8_t *input,
       
    96     unsigned int input_length_in_blocks);
       
    97 
       
    98 /* Formal declaration for functions in EVP_MD structure */
       
    99 int t4_digest_init_md5(EVP_MD_CTX *ctx);
       
   100 int t4_digest_update_md5(EVP_MD_CTX *ctx, const void *data, size_t count);
       
   101 int t4_digest_final_md5(EVP_MD_CTX *ctx, unsigned char *md);
       
   102 int t4_digest_copy_md5(EVP_MD_CTX *to, const EVP_MD_CTX *from);
       
   103 
       
   104 
       
   105 /*
       
   106  * MD5 Message Digests
       
   107  *
       
   108  * OpenSSL's libcrypto EVP stuff. This is how this engine gets wired to EVP.
       
   109  * EVP_MD is defined in evp.h.  To maintain binary compatibility the
       
   110  * definition cannot be modified.
       
   111  * Stuff specific to the t4 engine is kept in t4_cipher_ctx_t, which is
       
   112  * pointed to by the last field, app_data.
       
   113  *
       
   114  * Fields: type, pkey_type, md_size, flags,
       
   115  *	init(), update(), final(),
       
   116  *	copy(), cleanup(), sign(), verify(),
       
   117  *	required_pkey_type, block_size, ctx_size, md5_ctrl()
       
   118  */
       
   119 const EVP_MD t4_md5 = {
       
   120 	NID_md5, NID_md5WithRSAEncryption, MD5_DIGEST_LENGTH,
       
   121 	0,
       
   122 	t4_digest_init_md5, t4_digest_update_md5, t4_digest_final_md5,
       
   123 	t4_digest_copy_md5, NULL,
       
   124 	EVP_PKEY_RSA_method, MD5_CBLOCK,
       
   125 	sizeof (MD5_CTX), NULL
       
   126 	};
       
   127 
       
   128 /* These functions are implemented in md32_common.h: */
       
   129 static int t4_md5_update(MD5_CTX *c, const void *data_, size_t len);
       
   130 static void t4_md5_transform(MD5_CTX *c, const unsigned char *data);
       
   131 static int t4_md5_final(unsigned char *md, MD5_CTX *c);
       
   132 #pragma inline(t4_md5_update, t4_md5_transform, t4_md5_final)
       
   133 
       
   134 #define	DATA_ORDER_IS_LITTLE_ENDIAN
       
   135 /* HASH_LONG/MD5_LONG is a 32-bit unsigned: */
       
   136 #define	HASH_LONG		MD5_LONG
       
   137 #define	HASH_CTX		MD5_CTX
       
   138 #define	HASH_CBLOCK		MD5_CBLOCK
       
   139 #define	HASH_UPDATE		t4_md5_update
       
   140 #define	HASH_TRANSFORM		t4_md5_transform
       
   141 #define	HASH_FINAL		t4_md5_final
       
   142 #define	HASH_BLOCK_DATA_ORDER	t4_md5_multiblock
       
   143 /* Hash is already byte-swapped as Little Endian for SPARC T4: */
       
   144 #define	HASH_MAKE_STRING(c, s)	memcpy((s), (c), MD5_DIGEST_LENGTH);
       
   145 
       
   146 /* This defines HASH_UPDATE, HASH_TRANSFORM, HASH_FINAL functions: */
       
   147 #include "md32_common.h"
       
   148 
       
   149 
       
   150 /*
       
   151  * MD5 functions (RFC 1321 The MD5 Message-Digest Algorithm)
       
   152  */
       
   153 
       
   154 int
       
   155 t4_digest_init_md5(EVP_MD_CTX *ctx)
       
   156 {
       
   157 	MD5_CTX	*c = (MD5_CTX *)ctx->md_data;
       
   158 
       
   159 	/* Optimization: don't call memset(c, 0,...) or initialize c->data[] */
       
   160 	c->Nl = c->Nh = c->num = 0;
       
   161 
       
   162 	/* Big Endian for T4 */
       
   163 	c->A = 0x01234567U;
       
   164 	c->B = 0x89abcdefU;
       
   165 	c->C = 0xfedcba98U;
       
   166 	c->D = 0x76543210U;
       
   167 
       
   168 	return (1);
       
   169 }
       
   170 
       
   171 /*
       
   172  * Continue MD5 digest operation, using the message block to update context.
       
   173  * MD5 crunches in 64-byte blocks.
       
   174  */
       
   175 int
       
   176 t4_digest_update_md5(EVP_MD_CTX *ctx, const void *data, size_t len)
       
   177 {
       
   178 	return (t4_md5_update((MD5_CTX *)ctx->md_data, data, len));
       
   179 }
       
   180 
       
   181 /* End MD5 digest operation, finalizing message digest and zeroing context. */
       
   182 int
       
   183 t4_digest_final_md5(EVP_MD_CTX *ctx, unsigned char *md)
       
   184 {
       
   185 	return (t4_md5_final(md, (MD5_CTX *)ctx->md_data));
       
   186 }
       
   187 
       
   188 /* Required for Engine API */
       
   189 int
       
   190 t4_digest_copy_md5(EVP_MD_CTX *to, const EVP_MD_CTX *from)
       
   191 {
       
   192 	if ((to->md_data != NULL) && (from->md_data != NULL)) {
       
   193 		(void) memcpy(to->md_data, from->md_data, sizeof (MD5_CTX));
       
   194 	}
       
   195 	return (1);
       
   196 }
       
   197 
       
   198 #endif	/* COMPILE_HW_T4 */
       
   199 #endif	/* !OPENSSL_NO_MD5 */
       
   200 #endif	/* !OPENSSL_NO_HW && !OPENSSL_NO_HW_MD_T4 */