components/openssl/openssl-1.0.0/engines/t4/eng_t4_sha1.c
branchs11-update
changeset 2593 b92e6df5eaf0
parent 2592 a7d8d41eeab2
child 2594 27f414f634e9
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, 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 SHA-1 message digest operations.
       
    67  */
       
    68 
       
    69 #include <openssl/opensslconf.h>
       
    70 
       
    71 #if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_MD_T4)
       
    72 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
       
    73 #include <sys/types.h>
       
    74 #include <stdlib.h>
       
    75 #include <string.h>
       
    76 #include <errno.h>
       
    77 
       
    78 /*
       
    79  * For SHA1, OpenSSL SHA_CTX has an extra num field at the end,
       
    80  * while the Solaris SHA1_CTX does not have this field.
       
    81  */
       
    82 #include <openssl/engine.h>
       
    83 #include <openssl/sha.h>
       
    84 
       
    85 #if (defined(sun4v) || defined(__sparcv9) || defined(__sparcv8plus) || \
       
    86 	defined(__sparcv8)) && !defined(OPENSSL_NO_ASM)
       
    87 #define	COMPILE_HW_T4
       
    88 #endif
       
    89 
       
    90 #ifdef	COMPILE_HW_T4
       
    91 
       
    92 /* Assembly language function; replaces C function sha1_block_data_order(): */
       
    93 extern void t4_sha1_multiblock(SHA_CTX *ctx, const void *input, size_t num);
       
    94 
       
    95 /* Formal declaration for functions in EVP_MD structure */
       
    96 static int t4_digest_init_sha1(EVP_MD_CTX *ctx);
       
    97 static int t4_digest_update_sha1(EVP_MD_CTX *ctx, const void *data,
       
    98     size_t len);
       
    99 static int t4_digest_final_sha1(EVP_MD_CTX *ctx, unsigned char *md);
       
   100 static int t4_digest_copy_sha1(EVP_MD_CTX *to, const EVP_MD_CTX *from);
       
   101 
       
   102 /*
       
   103  * OpenSSL's libcrypto EVP stuff. This is how this engine gets wired to EVP.
       
   104  * EVP_MD is defined in evp.h.  To maintain binary compatibility the
       
   105  * definition cannot be modified.
       
   106  * Stuff specific to the t4 engine is kept in t4_cipher_ctx_t, which is
       
   107  * pointed to by the last field, app_data.
       
   108  *
       
   109  * Fields: type, pkey_type, md_size, flags,
       
   110  *	init(), update(), final(),
       
   111  *	copy(), cleanup(), sign(), verify(),
       
   112  *	required_pkey_type, block_size, ctx_size, md5_ctrl()
       
   113  */
       
   114 const EVP_MD t4_sha1 = {
       
   115 	NID_sha1, NID_sha1WithRSAEncryption, SHA_DIGEST_LENGTH,
       
   116 	EVP_MD_FLAG_PKEY_METHOD_SIGNATURE | EVP_MD_FLAG_DIGALGID_ABSENT,
       
   117 	t4_digest_init_sha1, t4_digest_update_sha1, t4_digest_final_sha1,
       
   118 	t4_digest_copy_sha1, NULL,
       
   119 	EVP_PKEY_RSA_method, SHA_CBLOCK,
       
   120 	sizeof (SHA_CTX), NULL
       
   121 	};
       
   122 
       
   123 /* These functions are defined in md32_common.h: */
       
   124 static int t4_sha1_update(SHA_CTX *c, const void *data_, size_t len);
       
   125 static void t4_sha1_transform(SHA_CTX *c, const unsigned char *data);
       
   126 static int t4_sha1_final(unsigned char *md, SHA_CTX *c);
       
   127 #pragma inline(t4_sha1_update, t4_sha1_transform, t4_sha1_final)
       
   128 
       
   129 #define	DATA_ORDER_IS_BIG_ENDIAN
       
   130 /* HASH_LONG/SHA_LONG is unsigned int (32 bits): */
       
   131 #define	HASH_LONG			SHA_LONG
       
   132 #define	HASH_CTX			SHA_CTX
       
   133 #define	HASH_CBLOCK			SHA_CBLOCK
       
   134 #define	HASH_UPDATE			t4_sha1_update
       
   135 #define	HASH_TRANSFORM			t4_sha1_transform
       
   136 #define	HASH_FINAL			t4_sha1_final
       
   137 #define	HASH_BLOCK_DATA_ORDER		t4_sha1_multiblock
       
   138 #define	HASH_MAKE_STRING(c, s)   do {	\
       
   139 		unsigned int ll;		\
       
   140 		ll = (c)->h0; HOST_l2c(ll, (s));	\
       
   141 		ll = (c)->h1; HOST_l2c(ll, (s));	\
       
   142 		ll = (c)->h2; HOST_l2c(ll, (s));	\
       
   143 		ll = (c)->h3; HOST_l2c(ll, (s));	\
       
   144 		ll = (c)->h4; HOST_l2c(ll, (s));	\
       
   145 	} while (0)
       
   146 
       
   147 /* This defines HASH_UPDATE, HASH_TRANSFORM, HASH_FINAL functions: */
       
   148 #include "md32_common.h"
       
   149 
       
   150 
       
   151 /*
       
   152  * SHA-1 functions (FIPS 180-1 Secure Hash Standard)
       
   153  */
       
   154 
       
   155 static int
       
   156 t4_digest_init_sha1(EVP_MD_CTX *ctx)
       
   157 {
       
   158 	SHA_CTX	*c = (SHA_CTX *)ctx->md_data;
       
   159 
       
   160 	/* Optimization: don't call memset(c, 0,...) or initialize c->data[] */
       
   161 	c->Nl = c->Nh = c->num = 0;
       
   162 	c->h0 = 0x67452301U;
       
   163 	c->h1 = 0xefcdab89U;
       
   164 	c->h2 = 0x98badcfeU;
       
   165 	c->h3 = 0x10325476U;
       
   166 	c->h4 = 0xc3d2e1f0U;
       
   167 
       
   168 	return (1);
       
   169 }
       
   170 
       
   171 /* Continue SHA1 digest operation, using the message block to update context. */
       
   172 static int
       
   173 t4_digest_update_sha1(EVP_MD_CTX *ctx, const void *data, size_t len)
       
   174 {
       
   175 	return (t4_sha1_update((SHA_CTX *)ctx->md_data, data, len));
       
   176 }
       
   177 
       
   178 /* End SHA1 digest operation, finalizing message digest and zeroing context. */
       
   179 static int
       
   180 t4_digest_final_sha1(EVP_MD_CTX *ctx, unsigned char *md)
       
   181 {
       
   182 	return (t4_sha1_final(md, (SHA_CTX *)ctx->md_data));
       
   183 }
       
   184 
       
   185 /* Required for Engine API */
       
   186 static int
       
   187 t4_digest_copy_sha1(EVP_MD_CTX *to, const EVP_MD_CTX *from)
       
   188 {
       
   189 	if ((to->md_data != NULL) && (from->md_data != NULL)) {
       
   190 		(void) memcpy(to->md_data, from->md_data, sizeof (SHA_CTX));
       
   191 	}
       
   192 	return (1);
       
   193 }
       
   194 
       
   195 #endif	/* COMPILE_HW_T4 */
       
   196 #endif	/* !OPENSSL_NO_SHA && !OPENSSL_NO_SHA1 */
       
   197 #endif	/* !OPENSSL_NO_HW && !OPENSSL_NO_HW_MD_T4 */