components/openssl/openssl-1.0.1/engines/t4/eng_t4.c
branchs11-update
changeset 2593 b92e6df5eaf0
parent 682 c6e4f94fd35d
child 2674 4801864231c8
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/).
       
     4  */
       
     5 
       
     6 /*
       
     7  * ====================================================================
       
     8  * Copyright (c) 1998-2011 The OpenSSL Project.  All rights reserved.
       
     9  *
       
    10  * Redistribution and use in source and binary forms, with or without
       
    11  * modification, are permitted provided that the following conditions
       
    12  * are met:
       
    13  *
       
    14  * 1. Redistributions of source code must retain the above copyright
       
    15  *    notice, this list of conditions and the following disclaimer.
       
    16  *
       
    17  * 2. Redistributions in binary form must reproduce the above copyright
       
    18  *    notice, this list of conditions and the following disclaimer in
       
    19  *    the documentation and/or other materials provided with the
       
    20  *    distribution.
       
    21  *
       
    22  * 3. All advertising materials mentioning features or use of this
       
    23  *    software must display the following acknowledgment:
       
    24  *    "This product includes software developed by the OpenSSL Project
       
    25  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
       
    26  *
       
    27  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
       
    28  *    endorse or promote products derived from this software without
       
    29  *    prior written permission. For written permission, please contact
       
    30  *    [email protected].
       
    31  *
       
    32  * 5. Products derived from this software may not be called "OpenSSL"
       
    33  *    nor may "OpenSSL" appear in their names without prior written
       
    34  *    permission of the OpenSSL Project.
       
    35  *
       
    36  * 6. Redistributions of any form whatsoever must retain the following
       
    37  *    acknowledgment:
       
    38  *    "This product includes software developed by the OpenSSL Project
       
    39  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
       
    40  *
       
    41  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
       
    42  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    44  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
       
    45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
       
    47  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
       
    48  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
       
    49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
       
    50  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    51  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
       
    52  * OF THE POSSIBILITY OF SUCH DAMAGE.
       
    53  * ====================================================================
       
    54  */
       
    55 
       
    56 /*
       
    57  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
       
    58  */
       
    59 
       
    60 /*
       
    61  * This engine supports SPARC microprocessors that provide AES and other
       
    62  * cipher and hash instructions, such as the T4 microprocessor.
       
    63  */
       
    64 
       
    65 #include <openssl/opensslconf.h>
       
    66 
       
    67 #if !defined(OPENSSL_NO_HW) && !defined(OPENSSL_NO_HW_AES_T4) && \
       
    68 	!defined(OPENSSL_NO_AES)
       
    69 #include <sys/types.h>
       
    70 #include <sys/auxv.h>	/* getisax() */
       
    71 #include <stdlib.h>
       
    72 #include <stdbool.h>
       
    73 #include <string.h>
       
    74 #include <errno.h>
       
    75 #include <openssl/aes.h>
       
    76 #include <openssl/engine.h>
       
    77 #include "eng_t4_aes_asm.h"
       
    78 
       
    79 #define	T4_LIB_NAME "SPARC T4 engine"
       
    80 #include "eng_t4_err.c"
       
    81 
       
    82 /* Copied from Solaris aes_impl.h */
       
    83 #ifndef	MAX_AES_NR
       
    84 #define	MAX_AES_NR		14 /* Maximum number of rounds */
       
    85 #endif
       
    86 #ifndef	MAX_AES_NB
       
    87 #define	MAX_AES_NB		4  /* Number of columns comprising a state */
       
    88 #endif
       
    89 
       
    90 /* Index for the supported ciphers */
       
    91 typedef enum {
       
    92 	T4_AES_128_CBC,
       
    93 	T4_AES_192_CBC,
       
    94 	T4_AES_256_CBC,
       
    95 #ifndef	SOLARIS_NO_AES_CFB128
       
    96 	T4_AES_128_CFB128,
       
    97 	T4_AES_192_CFB128,
       
    98 	T4_AES_256_CFB128,
       
    99 #endif	/* !SOLARIS_NO_AES_CFB128 */
       
   100 	T4_AES_128_CTR,
       
   101 	T4_AES_192_CTR,
       
   102 	T4_AES_256_CTR,
       
   103 	T4_AES_128_ECB,
       
   104 	T4_AES_192_ECB,
       
   105 	T4_AES_256_ECB,
       
   106 	T4_CIPHER_MAX
       
   107 } t4_cipher_id;
       
   108 
       
   109 /* T4 cipher context; must be 8-byte aligned (last field must be uint64_t)  */
       
   110 typedef struct t4_cipher_ctx {
       
   111 	t4_cipher_id	index;
       
   112 	uint64_t	*iv;
       
   113 	uint64_t	aligned_iv_buffer[2]; /* use if original IV unaligned */
       
   114 	/* Encryption and decryption key schedule are the same: */
       
   115 	uint64_t	t4_ks[((MAX_AES_NR) + 1) * (MAX_AES_NB)];
       
   116 } t4_cipher_ctx_t;
       
   117 
       
   118 typedef struct t4_cipher {
       
   119 	t4_cipher_id	id;
       
   120 	int		nid;
       
   121 	int		iv_len;
       
   122 	int		min_key_len;
       
   123 	int		max_key_len;
       
   124 	unsigned long	flags;
       
   125 } t4_cipher_t;
       
   126 
       
   127 /* Constants used when creating the ENGINE */
       
   128 static const char *ENGINE_T4_ID = "t4";
       
   129 static const char *ENGINE_T4_NAME = "SPARC T4 engine support";
       
   130 static const char *ENGINE_NO_T4_NAME = "SPARC T4 engine support (no T4)";
       
   131 
       
   132 
       
   133 #if (defined(sun4v) || defined(__sparcv9) || defined(__sparcv8plus) || \
       
   134 	defined(__sparcv8)) && !defined(OPENSSL_NO_ASM)
       
   135 #define	COMPILE_HW_T4
       
   136 static int t4_bind_helper(ENGINE *e, const char *id);
       
   137 #pragma inline(t4_bind_helper)
       
   138 #endif
       
   139 
       
   140 /*
       
   141  * This makes the engine "built-in" with OpenSSL.
       
   142  * On non-T4 CPUs this just returns.
       
   143  * Called by ENGINE_load_builtin_engines().
       
   144  */
       
   145 void
       
   146 ENGINE_load_t4(void)
       
   147 {
       
   148 #ifdef	COMPILE_HW_T4
       
   149 	ENGINE *toadd = ENGINE_new();
       
   150 	if (toadd != NULL) {
       
   151 		if (t4_bind_helper(toadd, ENGINE_T4_ID) != 0) {
       
   152 			(void) ENGINE_add(toadd);
       
   153 			(void) ENGINE_free(toadd);
       
   154 			ERR_clear_error();
       
   155 		} else {
       
   156 			(void) ENGINE_free(toadd);
       
   157 		}
       
   158 	}
       
   159 #endif
       
   160 }
       
   161 
       
   162 
       
   163 #ifdef	COMPILE_HW_T4
       
   164 static int t4_bind(ENGINE *e);
       
   165 #ifndef	DYNAMIC_ENGINE
       
   166 #pragma inline(t4_bind)
       
   167 #endif
       
   168 static t4_cipher_id get_cipher_index_by_nid(int nid);
       
   169 #pragma inline(get_cipher_index_by_nid)
       
   170 static void t4_instructions_present(_Bool *aes_present, _Bool *des_present,
       
   171     _Bool *digest_present, _Bool *montmul_present);
       
   172 #pragma inline(t4_instructions_present)
       
   173 
       
   174 /* Digest registration function. Called by ENGINE_set_ciphers() */
       
   175 int t4_get_all_digests(ENGINE *e, const EVP_MD **digest,
       
   176     const int **nids, int nid);
       
   177 
       
   178 /* RSA_METHOD structure used by ENGINE_set_RSA() */
       
   179 extern RSA_METHOD *t4_RSA(void);
       
   180 
       
   181 /* DH_METHOD structure used by ENGINE_set_DH() */
       
   182 extern DH_METHOD *t4_DH(void);
       
   183 
       
   184 /* DSA_METHOD structure used by ENGINE_set_DSA() */
       
   185 extern DSA_METHOD *t4_DSA(void);
       
   186 
       
   187 /* Static variables */
       
   188 /* This can't be const as NID*ctr is inserted when the engine is initialized */
       
   189 static int t4_cipher_nids[] = {
       
   190 	NID_aes_128_cbc, NID_aes_192_cbc, NID_aes_256_cbc,
       
   191 #ifndef	SOLARIS_NO_AES_CFB128
       
   192 	NID_aes_128_cfb128, NID_aes_192_cfb128, NID_aes_256_cfb128,
       
   193 #endif
       
   194 	NID_aes_128_ctr, NID_aes_192_ctr, NID_aes_256_ctr,
       
   195 	NID_aes_128_ecb, NID_aes_192_ecb, NID_aes_256_ecb,
       
   196 #ifndef	OPENSSL_NO_DES
       
   197 	/* Must be at end of list (see t4_des_cipher_count in t4_bind() */
       
   198 	NID_des_cbc, NID_des_ede3_cbc, NID_des_ecb, NID_des_ede3_ecb,
       
   199 #endif
       
   200 };
       
   201 static const int t4_des_cipher_count = 4;
       
   202 static int t4_cipher_count =
       
   203 	(sizeof (t4_cipher_nids) / sizeof (t4_cipher_nids[0]));
       
   204 
       
   205 /*
       
   206  * Cipher Table for all supported symmetric ciphers.
       
   207  * Must be in same order as t4_cipher_id.
       
   208  */
       
   209 static t4_cipher_t t4_cipher_table[] = {
       
   210 	/* ID			NID			IV min- max-key flags */
       
   211 	{T4_AES_128_CBC,	NID_aes_128_cbc,	16, 16, 16, 0},
       
   212 	{T4_AES_192_CBC,	NID_aes_192_cbc,	16, 24, 24, 0},
       
   213 	{T4_AES_256_CBC,	NID_aes_256_cbc,	16, 32, 32, 0},
       
   214 #ifndef	SOLARIS_NO_AES_CFB128
       
   215 	{T4_AES_128_CFB128,	NID_aes_128_cfb128,	16, 16, 16,
       
   216 							EVP_CIPH_NO_PADDING},
       
   217 	{T4_AES_192_CFB128,	NID_aes_192_cfb128,	16, 24, 24,
       
   218 							EVP_CIPH_NO_PADDING},
       
   219 	{T4_AES_256_CFB128,	NID_aes_256_cfb128,	16, 32, 32,
       
   220 							EVP_CIPH_NO_PADDING},
       
   221 #endif
       
   222 	{T4_AES_128_CTR,	NID_aes_128_ctr,	16, 16, 16,
       
   223 							EVP_CIPH_NO_PADDING},
       
   224 	{T4_AES_192_CTR,	NID_aes_192_ctr,	16, 24, 24,
       
   225 							EVP_CIPH_NO_PADDING},
       
   226 	{T4_AES_256_CTR,	NID_aes_256_ctr,	16, 32, 32,
       
   227 							EVP_CIPH_NO_PADDING},
       
   228 	{T4_AES_128_ECB,	NID_aes_128_ecb,	0, 16, 16, 0},
       
   229 	{T4_AES_192_ECB,	NID_aes_192_ecb,	0, 24, 24, 0},
       
   230 	{T4_AES_256_ECB,	NID_aes_256_ecb,	0, 32, 32, 0},
       
   231 };
       
   232 
       
   233 
       
   234 /* Formal declaration for functions in EVP_CIPHER structure */
       
   235 static int t4_cipher_init_aes(EVP_CIPHER_CTX *ctx, const unsigned char *key,
       
   236     const unsigned char *iv, int enc);
       
   237 
       
   238 static int t4_cipher_do_aes_128_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   239     const unsigned char *in, size_t inl);
       
   240 static int t4_cipher_do_aes_192_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   241     const unsigned char *in, size_t inl);
       
   242 static int t4_cipher_do_aes_256_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   243     const unsigned char *in, size_t inl);
       
   244 #ifndef	SOLARIS_NO_AES_CFB128
       
   245 static int t4_cipher_do_aes_128_cfb128(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   246     const unsigned char *in, size_t inl);
       
   247 static int t4_cipher_do_aes_192_cfb128(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   248     const unsigned char *in, size_t inl);
       
   249 static int t4_cipher_do_aes_256_cfb128(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   250     const unsigned char *in, size_t inl);
       
   251 #endif
       
   252 static int t4_cipher_do_aes_128_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   253     const unsigned char *in, size_t inl);
       
   254 static int t4_cipher_do_aes_192_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   255     const unsigned char *in, size_t inl);
       
   256 static int t4_cipher_do_aes_256_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   257     const unsigned char *in, size_t inl);
       
   258 static int t4_cipher_do_aes_128_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   259     const unsigned char *in, size_t inl);
       
   260 static int t4_cipher_do_aes_192_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   261     const unsigned char *in, size_t inl);
       
   262 static int t4_cipher_do_aes_256_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
       
   263     const unsigned char *in, size_t inl);
       
   264 
       
   265 
       
   266 /*
       
   267  * Cipher Algorithms
       
   268  *
       
   269  * OpenSSL's libcrypto EVP stuff. This is how this engine gets wired to EVP.
       
   270  * EVP_CIPHER is defined in evp.h.  To maintain binary compatibility the
       
   271  * definition cannot be modified.
       
   272  * Stuff specific to the t4 engine is kept in t4_cipher_ctx_t, which is
       
   273  * pointed to by cipher_data or md_data
       
   274  *
       
   275  * Fields: nid, block_size, key_len, iv_len, flags,
       
   276  *	init(), do_cipher(), cleanup(),
       
   277  *	ctx_size,
       
   278  *	set_asn1_parameters(), get_asn1_parameters(), ctrl(), app_data
       
   279  */
       
   280 
       
   281 static const EVP_CIPHER t4_aes_128_cbc = {
       
   282 	NID_aes_128_cbc,
       
   283 	16, 16, 16,
       
   284 	EVP_CIPH_CBC_MODE,
       
   285 	t4_cipher_init_aes, t4_cipher_do_aes_128_cbc, NULL,
       
   286 	sizeof (t4_cipher_ctx_t),
       
   287 	EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv,
       
   288 	NULL, NULL
       
   289 };
       
   290 static const EVP_CIPHER t4_aes_192_cbc = {
       
   291 	NID_aes_192_cbc,
       
   292 	16, 24, 16,
       
   293 	EVP_CIPH_CBC_MODE,
       
   294 	t4_cipher_init_aes, t4_cipher_do_aes_192_cbc, NULL,
       
   295 	sizeof (t4_cipher_ctx_t),
       
   296 	EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv,
       
   297 	NULL, NULL
       
   298 };
       
   299 static const EVP_CIPHER t4_aes_256_cbc = {
       
   300 	NID_aes_256_cbc,
       
   301 	16, 32, 16,
       
   302 	EVP_CIPH_CBC_MODE,
       
   303 	t4_cipher_init_aes, t4_cipher_do_aes_256_cbc, NULL,
       
   304 	sizeof (t4_cipher_ctx_t),
       
   305 	EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv,
       
   306 	NULL, NULL
       
   307 };
       
   308 
       
   309 #ifndef	SOLARIS_NO_AES_CFB128
       
   310 static const EVP_CIPHER t4_aes_128_cfb128 = {
       
   311 	NID_aes_128_cfb128,
       
   312 	16, 16, 16,
       
   313 	EVP_CIPH_CFB_MODE,
       
   314 	t4_cipher_init_aes, t4_cipher_do_aes_128_cfb128, NULL,
       
   315 	sizeof (t4_cipher_ctx_t),
       
   316 	EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv,
       
   317 	NULL, NULL
       
   318 };
       
   319 static const EVP_CIPHER t4_aes_192_cfb128 = {
       
   320 	NID_aes_192_cfb128,
       
   321 	16, 24, 16,
       
   322 	EVP_CIPH_CFB_MODE,
       
   323 	t4_cipher_init_aes, t4_cipher_do_aes_192_cfb128, NULL,
       
   324 	sizeof (t4_cipher_ctx_t),
       
   325 	EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv,
       
   326 	NULL, NULL
       
   327 };
       
   328 static const EVP_CIPHER t4_aes_256_cfb128 = {
       
   329 	NID_aes_256_cfb128,
       
   330 	16, 32, 16,
       
   331 	EVP_CIPH_CFB_MODE,
       
   332 	t4_cipher_init_aes, t4_cipher_do_aes_256_cfb128, NULL,
       
   333 	sizeof (t4_cipher_ctx_t),
       
   334 	EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv,
       
   335 	NULL, NULL
       
   336 };
       
   337 #endif	/* !SOLARIS_NO_AES_CFB128 */
       
   338 
       
   339 static EVP_CIPHER t4_aes_128_ctr = {
       
   340 	NID_aes_128_ctr,
       
   341 	16, 16, 16,
       
   342 	EVP_CIPH_CTR_MODE,
       
   343 	t4_cipher_init_aes, t4_cipher_do_aes_128_ctr, NULL,
       
   344 	sizeof (t4_cipher_ctx_t),
       
   345 	EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv,
       
   346 	NULL, NULL
       
   347 };
       
   348 static EVP_CIPHER t4_aes_192_ctr = {
       
   349 	NID_aes_192_ctr,
       
   350 	16, 24, 16,
       
   351 	EVP_CIPH_CTR_MODE,
       
   352 	t4_cipher_init_aes, t4_cipher_do_aes_192_ctr, NULL,
       
   353 	sizeof (t4_cipher_ctx_t),
       
   354 	EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv,
       
   355 	NULL, NULL
       
   356 };
       
   357 static EVP_CIPHER t4_aes_256_ctr = {
       
   358 	NID_aes_256_ctr,
       
   359 	16, 32, 16,
       
   360 	EVP_CIPH_CTR_MODE,
       
   361 	t4_cipher_init_aes, t4_cipher_do_aes_256_ctr, NULL,
       
   362 	sizeof (t4_cipher_ctx_t),
       
   363 	EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv,
       
   364 	NULL, NULL
       
   365 };
       
   366 
       
   367 /*
       
   368  * ECB modes don't use an Initial Vector, so that's why set_asn1_parameters,
       
   369  * get_asn1_parameters, and cleanup fields are set to NULL.
       
   370  */
       
   371 static const EVP_CIPHER t4_aes_128_ecb = {
       
   372 	NID_aes_128_ecb,
       
   373 	16, 16, 0,
       
   374 	EVP_CIPH_ECB_MODE,
       
   375 	t4_cipher_init_aes, t4_cipher_do_aes_128_ecb, NULL,
       
   376 	sizeof (t4_cipher_ctx_t),
       
   377 	NULL, NULL, NULL, NULL
       
   378 };
       
   379 static const EVP_CIPHER t4_aes_192_ecb = {
       
   380 	NID_aes_192_ecb,
       
   381 	16, 24, 0,
       
   382 	EVP_CIPH_ECB_MODE,
       
   383 	t4_cipher_init_aes, t4_cipher_do_aes_192_ecb, NULL,
       
   384 	sizeof (t4_cipher_ctx_t),
       
   385 	NULL, NULL, NULL, NULL
       
   386 };
       
   387 static const EVP_CIPHER t4_aes_256_ecb = {
       
   388 	NID_aes_256_ecb,
       
   389 	16, 32, 0,
       
   390 	EVP_CIPH_ECB_MODE,
       
   391 	t4_cipher_init_aes, t4_cipher_do_aes_256_ecb, NULL,
       
   392 	sizeof (t4_cipher_ctx_t),
       
   393 	NULL, NULL, NULL, NULL
       
   394 };
       
   395 
       
   396 #ifndef	OPENSSL_NO_DES
       
   397 extern const EVP_CIPHER t4_des_cbc;
       
   398 extern const EVP_CIPHER t4_des3_cbc;
       
   399 extern const EVP_CIPHER t4_des_ecb;
       
   400 extern const EVP_CIPHER t4_des3_ecb;
       
   401 #endif	/* OPENSSL_NO_DES */
       
   402 
       
   403 
       
   404 /*
       
   405  * Message Digest variables
       
   406  */
       
   407 static const int t4_digest_nids[] = {
       
   408 #ifndef	OPENSSL_NO_MD5
       
   409 	NID_md5,
       
   410 #endif
       
   411 #ifndef	OPENSSL_NO_SHA
       
   412 #ifndef	OPENSSL_NO_SHA1
       
   413 	NID_sha1,
       
   414 #endif
       
   415 #ifndef	OPENSSL_NO_SHA256
       
   416 	NID_sha224,
       
   417 	NID_sha256,
       
   418 #endif
       
   419 #ifndef	OPENSSL_NO_SHA512
       
   420 	NID_sha384,
       
   421 	NID_sha512,
       
   422 #endif
       
   423 #endif	/* !OPENSSL_NO_SHA */
       
   424 };
       
   425 static const int t4_digest_count =
       
   426 	(sizeof (t4_digest_nids) / sizeof (t4_digest_nids[0]));
       
   427 
       
   428 #ifndef	OPENSSL_NO_MD5
       
   429 extern const EVP_MD t4_md5;
       
   430 #endif
       
   431 #ifndef	OPENSSL_NO_SHA
       
   432 #ifndef	OPENSSL_NO_SHA1
       
   433 extern const EVP_MD t4_sha1;
       
   434 #endif
       
   435 #ifndef	OPENSSL_NO_SHA256
       
   436 extern const EVP_MD t4_sha224;
       
   437 extern const EVP_MD t4_sha256;
       
   438 #endif
       
   439 #ifndef	OPENSSL_NO_SHA512
       
   440 extern const EVP_MD t4_sha384;
       
   441 extern const EVP_MD t4_sha512;
       
   442 #endif
       
   443 #endif	/* !OPENSSL_NO_SHA */
       
   444 
       
   445 /*
       
   446  * Message Digest functions
       
   447  */
       
   448 
       
   449 /*
       
   450  * Registered by the ENGINE with ENGINE_set_digests().
       
   451  * Finds out how to deal with a particular digest NID in the ENGINE.
       
   452  */
       
   453 /* ARGSUSED */
       
   454 int
       
   455 t4_get_all_digests(ENGINE *e, const EVP_MD **digest,
       
   456     const int **nids, int nid)
       
   457 {
       
   458 	if (digest == NULL) { /* return a list of all supported digests */
       
   459 		*nids = (t4_digest_count > 0) ? t4_digest_nids : NULL;
       
   460 		return (t4_digest_count);
       
   461 	}
       
   462 
       
   463 	switch (nid) {
       
   464 #ifndef	OPENSSL_NO_MD5
       
   465 	case NID_md5:
       
   466 		*digest = &t4_md5;
       
   467 		break;
       
   468 #endif
       
   469 #ifndef	OPENSSL_NO_SHA
       
   470 #ifndef	OPENSSL_NO_SHA1
       
   471 	/*
       
   472 	 * A special case. For "openssl dgst -dss1 ...",
       
   473 	 * OpenSSL calls EVP_get_digestbyname() on "dss1" which ends up
       
   474 	 * calling t4_get_all_digests() for NID_dsa. Internally, if an
       
   475 	 * engine is not used, OpenSSL uses SHA1_Init() as expected for
       
   476 	 * DSA. So, we must return t4_sha1 for NID_dsa as well. Note
       
   477 	 * that this must have changed between 0.9.8 and 1.0.0 since we
       
   478 	 * did not have the problem with the 0.9.8 version.
       
   479 	 */
       
   480 	case NID_dsa:
       
   481 	case NID_sha1:
       
   482 		*digest = &t4_sha1;
       
   483 		break;
       
   484 #endif
       
   485 #ifndef	OPENSSL_NO_SHA256
       
   486 	case NID_sha224:
       
   487 		*digest = &t4_sha224;
       
   488 		break;
       
   489 	case NID_sha256:
       
   490 		*digest = &t4_sha256;
       
   491 		break;
       
   492 #endif
       
   493 #ifndef	OPENSSL_NO_SHA512
       
   494 	case NID_sha384:
       
   495 		*digest = &t4_sha384;
       
   496 		break;
       
   497 	case NID_sha512:
       
   498 		*digest = &t4_sha512;
       
   499 		break;
       
   500 #endif
       
   501 #endif	/* !OPENSSL_NO_SHA */
       
   502 	default:
       
   503 		/* digest not supported */
       
   504 		*digest = NULL;
       
   505 		return (0);
       
   506 	}
       
   507 
       
   508 	return (1);
       
   509 }
       
   510 
       
   511 
       
   512 /*
       
   513  * Utility Functions
       
   514  */
       
   515 
       
   516 /*
       
   517  * Set aes_present, des_present, digest_present and montmul_present
       
   518  * to B_FALSE or B_TRUE depending on
       
   519  * whether the current SPARC processor supports AES, DES,
       
   520  * MD5/SHA1/SHA256/SHA512 and MONTMUL, respectively.
       
   521  */
       
   522 static void
       
   523 t4_instructions_present(_Bool *aes_present, _Bool *des_present,
       
   524     _Bool *digest_present, _Bool *montmul_present)
       
   525 {
       
   526 #ifdef	OPENSSL_NO_DES
       
   527 #undef	AV_SPARC_DES
       
   528 #define	AV_SPARC_DES	0
       
   529 #endif
       
   530 #ifdef	OPENSSL_NO_MD5
       
   531 #undef	AV_SPARC_MD5
       
   532 #define	AV_SPARC_MD5	0
       
   533 #endif
       
   534 #ifndef	OPENSSL_NO_SHA
       
   535 #ifdef	OPENSSL_NO_SHA1
       
   536 #undef	AV_SPARC_SHA1
       
   537 #define	AV_SPARC_SHA1	0
       
   538 #endif
       
   539 #ifdef	OPENSSL_NO_SHA256
       
   540 #undef	AV_SPARC_SHA256
       
   541 #define	AV_SPARC_SHA256	0
       
   542 #endif
       
   543 #ifdef	OPENSSL_NO_SHA512
       
   544 #undef	AV_SPARC_SHA512
       
   545 #define	AV_SPARC_SHA512	0
       
   546 #endif
       
   547 #else
       
   548 #undef	AV_SPARC_SHA1
       
   549 #undef	AV_SPARC_SHA256
       
   550 #undef	AV_SPARC_SHA512
       
   551 #define	AV_SPARC_SHA1	0
       
   552 #define	AV_SPARC_SHA256	0
       
   553 #define	AV_SPARC_SHA512	0
       
   554 #endif	/* !OPENSSL_NO_SHA */
       
   555 
       
   556 #define	DIGEST_MASK	(AV_SPARC_MD5 | AV_SPARC_SHA1 | AV_SPARC_SHA256 | \
       
   557 	AV_SPARC_SHA512)
       
   558 	uint_t		ui;
       
   559 
       
   560 	(void) getisax(&ui, 1);
       
   561 	*aes_present = ((ui & AV_SPARC_AES) != 0);
       
   562 	*des_present = ((ui & AV_SPARC_DES) != 0);
       
   563 	*digest_present = ((ui & DIGEST_MASK) == DIGEST_MASK);
       
   564 	*montmul_present = ((ui & AV_SPARC_MONT) != 0);
       
   565 }
       
   566 
       
   567 
       
   568 /*
       
   569  * Cipher functions
       
   570  */
       
   571 
       
   572 
       
   573 /*
       
   574  * Registered by the ENGINE with ENGINE_set_ciphers().
       
   575  * Finds out how to deal with a particular cipher NID in the ENGINE.
       
   576  */
       
   577 /* ARGSUSED */
       
   578 static int
       
   579 t4_get_all_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
       
   580     const int **nids, int nid)
       
   581 {
       
   582 	if (cipher == NULL) { /* return a list of all supported ciphers */
       
   583 		*nids = (t4_cipher_count > 0) ? t4_cipher_nids : NULL;
       
   584 		return (t4_cipher_count);
       
   585 	}
       
   586 
       
   587 	switch (nid) {
       
   588 	case NID_aes_128_cbc:
       
   589 		*cipher = &t4_aes_128_cbc;
       
   590 		break;
       
   591 	case NID_aes_192_cbc:
       
   592 		*cipher = &t4_aes_192_cbc;
       
   593 		break;
       
   594 	case NID_aes_256_cbc:
       
   595 		*cipher = &t4_aes_256_cbc;
       
   596 		break;
       
   597 	case NID_aes_128_ecb:
       
   598 		*cipher = &t4_aes_128_ecb;
       
   599 		break;
       
   600 	case NID_aes_192_ecb:
       
   601 		*cipher = &t4_aes_192_ecb;
       
   602 		break;
       
   603 	case NID_aes_256_ecb:
       
   604 		*cipher = &t4_aes_256_ecb;
       
   605 		break;
       
   606 #ifndef	SOLARIS_NO_AES_CFB128
       
   607 	case NID_aes_128_cfb128:
       
   608 		*cipher = &t4_aes_128_cfb128;
       
   609 		break;
       
   610 	case NID_aes_192_cfb128:
       
   611 		*cipher = &t4_aes_192_cfb128;
       
   612 		break;
       
   613 	case NID_aes_256_cfb128:
       
   614 		*cipher = &t4_aes_256_cfb128;
       
   615 		break;
       
   616 #endif	/* !SOLARIS_NO_AES_CFB128 */
       
   617 #ifndef	OPENSSL_NO_DES
       
   618 	case NID_des_cbc:
       
   619 		*cipher = &t4_des_cbc;
       
   620 		break;
       
   621 	case NID_des_ede3_cbc:
       
   622 		*cipher = &t4_des3_cbc;
       
   623 		break;
       
   624 	case NID_des_ecb:
       
   625 		*cipher = &t4_des_ecb;
       
   626 		break;
       
   627 	case NID_des_ede3_ecb:
       
   628 		*cipher = &t4_des3_ecb;
       
   629 		break;
       
   630 #endif	/* !OPENSSL_NO_DES */
       
   631 	case NID_aes_128_ctr:
       
   632 		*cipher = &t4_aes_128_ctr;
       
   633 		break;
       
   634 	case NID_aes_192_ctr:
       
   635 		*cipher = &t4_aes_192_ctr;
       
   636 		break;
       
   637 	case NID_aes_256_ctr:
       
   638 		*cipher = &t4_aes_256_ctr;
       
   639 		break;
       
   640 	default:
       
   641 		/* cipher not supported */
       
   642 		*cipher = NULL;
       
   643 		return (0);
       
   644 	}
       
   645 
       
   646 	return (1);
       
   647 }
       
   648 
       
   649 
       
   650 /* Called by t4_cipher_init_aes() */
       
   651 static t4_cipher_id
       
   652 get_cipher_index_by_nid(int nid)
       
   653 {
       
   654 	t4_cipher_id i;
       
   655 
       
   656 	for (i = (t4_cipher_id)0; i < T4_CIPHER_MAX; ++i)
       
   657 		if (t4_cipher_table[i].nid == nid)
       
   658 			return (i);
       
   659 	return (T4_CIPHER_MAX);
       
   660 }
       
   661 
       
   662 
       
   663 /* ARGSUSED2 */
       
   664 static int
       
   665 t4_cipher_init_aes(EVP_CIPHER_CTX *ctx, const unsigned char *key,
       
   666     const unsigned char *iv, int enc)
       
   667 {
       
   668 	t4_cipher_ctx_t	*tctx = ctx->cipher_data;
       
   669 	uint64_t	*t4_ks = tctx->t4_ks;
       
   670 	t4_cipher_t	*t4_cipher;
       
   671 	t4_cipher_id	index;
       
   672 	int		key_len = ctx->key_len;
       
   673 	uint64_t	aligned_key_buffer[4]; /* 16, 24, or 32 bytes long */
       
   674 	uint64_t	*aligned_key;
       
   675 
       
   676 	if (key == NULL) {
       
   677 		T4err(T4_F_CIPHER_INIT_AES, T4_R_CIPHER_KEY);
       
   678 		return (0);
       
   679 	}
       
   680 
       
   681 	/* Get the cipher entry index in t4_cipher_table from nid */
       
   682 	index = get_cipher_index_by_nid(ctx->cipher->nid);
       
   683 	if (index >= T4_CIPHER_MAX) {
       
   684 		T4err(T4_F_CIPHER_INIT_AES, T4_R_CIPHER_NID);
       
   685 		return (0); /* Error */
       
   686 	}
       
   687 	t4_cipher = &t4_cipher_table[index];
       
   688 
       
   689 	/* Check key size and iv size */
       
   690 	if (ctx->cipher->iv_len < t4_cipher->iv_len) {
       
   691 		T4err(T4_F_CIPHER_INIT_AES, T4_R_IV_LEN_INCORRECT);
       
   692 		return (0); /* Error */
       
   693 	}
       
   694 	if ((key_len < t4_cipher->min_key_len) ||
       
   695 	    (key_len > t4_cipher->max_key_len)) {
       
   696 		T4err(T4_F_CIPHER_INIT_AES, T4_R_KEY_LEN_INCORRECT);
       
   697 		return (0); /* Error */
       
   698 	}
       
   699 
       
   700 	/* Set cipher flags, if any */
       
   701 	ctx->flags |= t4_cipher->flags;
       
   702 
       
   703 	/* Align the key */
       
   704 	if (((unsigned long)key & 0x7) == 0) /* already aligned */
       
   705 		aligned_key = (uint64_t *)key;
       
   706 	else { /* key is not 8-byte aligned */
       
   707 #ifdef	DEBUG_T4
       
   708 		(void) fprintf(stderr, "T4: key is not 8 byte aligned\n");
       
   709 #endif
       
   710 		(void) memcpy(aligned_key_buffer, key, key_len);
       
   711 		aligned_key = aligned_key_buffer;
       
   712 	}
       
   713 
       
   714 
       
   715 	/*
       
   716 	 * Expand the key schedule.
       
   717 	 * Copy original key to start of t4_ks key schedule. Note that the
       
   718 	 * encryption and decryption key schedule are the same for T4.
       
   719 	 */
       
   720 	switch (key_len) {
       
   721 		case 16:
       
   722 			t4_aes_expand128(&t4_ks[2],
       
   723 			    (const uint32_t *)aligned_key);
       
   724 			t4_ks[0] = aligned_key[0];
       
   725 			t4_ks[1] = aligned_key[1];
       
   726 			break;
       
   727 		case 24:
       
   728 			t4_aes_expand192(&t4_ks[3],
       
   729 			    (const uint32_t *)aligned_key);
       
   730 			t4_ks[0] = aligned_key[0];
       
   731 			t4_ks[1] = aligned_key[1];
       
   732 			t4_ks[2] = aligned_key[2];
       
   733 			break;
       
   734 		case 32:
       
   735 			t4_aes_expand256(&t4_ks[4],
       
   736 			    (const uint32_t *)aligned_key);
       
   737 			t4_ks[0] = aligned_key[0];
       
   738 			t4_ks[1] = aligned_key[1];
       
   739 			t4_ks[2] = aligned_key[2];
       
   740 			t4_ks[3] = aligned_key[3];
       
   741 			break;
       
   742 		default:
       
   743 			T4err(T4_F_CIPHER_INIT_AES, T4_R_CIPHER_KEY);
       
   744 			return (0);
       
   745 	}
       
   746 
       
   747 	/* Save index to cipher */
       
   748 	tctx->index = index;
       
   749 
       
   750 	/* Align IV, if needed */
       
   751 	if (t4_cipher->iv_len <= 0) { /* no IV (such as with ECB mode) */
       
   752 		tctx->iv = NULL;
       
   753 	} else if (((unsigned long)ctx->iv & 0x7) == 0) { /* already aligned */
       
   754 		tctx->iv = (uint64_t *)ctx->iv;
       
   755 	} else {
       
   756 		/* IV is not 8 byte aligned */
       
   757 		(void) memcpy(tctx->aligned_iv_buffer, ctx->iv,
       
   758 		    ctx->cipher->iv_len);
       
   759 		tctx->iv = tctx->aligned_iv_buffer;
       
   760 #ifdef	DEBUG_T4
       
   761 		(void) fprintf(stderr,
       
   762 		    "t4_cipher_init_aes: IV is not 8 byte aligned\n");
       
   763 		(void) fprintf(stderr,
       
   764 		    "t4_cipher_init_aes: ctx->cipher->iv_len =%d\n",
       
   765 		    ctx->cipher->iv_len);
       
   766 		(void) fprintf(stderr, "t4_cipher_init_aes: after "
       
   767 		    "re-alignment, tctx->iv = %p\n", (void *)tctx->iv);
       
   768 #endif	/* DEBUG_T4 */
       
   769 	}
       
   770 
       
   771 	return (1);
       
   772 }
       
   773 
       
   774 
       
   775 /*
       
   776  * ENCRYPT_UPDATE or DECRYPT_UPDATE
       
   777  */
       
   778 #define	T4_CIPHER_DO_AES(t4_cipher_do_aes, t4_aes_load_keys_for_encrypt, \
       
   779     t4_aes_encrypt, t4_aes_load_keys_for_decrypt, t4_aes_decrypt, iv)	\
       
   780 static int								\
       
   781 t4_cipher_do_aes(EVP_CIPHER_CTX *ctx, unsigned char *out,		\
       
   782     const unsigned char *in, size_t inl)				\
       
   783 {									\
       
   784 	t4_cipher_ctx_t	*tctx = ctx->cipher_data;			\
       
   785 	uint64_t	*t4_ks = tctx->t4_ks;				\
       
   786 	unsigned long	outl = inl;					\
       
   787 	unsigned char	*bufin_alloc = NULL, *bufout_alloc = NULL;	\
       
   788 	unsigned char	*bufin, *bufout;				\
       
   789 									\
       
   790 	/* "in" and "out" must be 8 byte aligned */			\
       
   791 	if (((unsigned long)in & 0x7) == 0) { /* already aligned */	\
       
   792 		bufin = (unsigned char *)in;				\
       
   793 	} else { /* "in" is not 8 byte aligned */			\
       
   794 		if (((unsigned long)out & 0x7) == 0) { /* aligned */	\
       
   795 			/* use output buffer for input */		\
       
   796 			bufin = out;					\
       
   797 		} else {						\
       
   798 			bufin = bufin_alloc = OPENSSL_malloc(inl);	\
       
   799 			if (bufin_alloc == NULL)			\
       
   800 				return (0); /* error */			\
       
   801 		}							\
       
   802 		(void) memcpy(bufin, in, inl);				\
       
   803 	}								\
       
   804 									\
       
   805 	if (((unsigned long)out & 0x7) == 0) { /* already aligned */	\
       
   806 		bufout = out;						\
       
   807 	} else { /* "out" is not 8 byte aligned */			\
       
   808 		if (bufin_alloc != NULL) {				\
       
   809 			/* use allocated input buffer for output */	\
       
   810 			bufout = bufin_alloc;				\
       
   811 		} else {						\
       
   812 			bufout = bufout_alloc = OPENSSL_malloc(outl);	\
       
   813 			if (bufout_alloc == NULL) {			\
       
   814 				OPENSSL_free(bufin_alloc);		\
       
   815 				return (0); /* error */			\
       
   816 			}						\
       
   817 		}							\
       
   818 	}								\
       
   819 									\
       
   820 	/* Data length must be an even multiple of block size. */	\
       
   821 	if ((inl & 0xf) != 0) {						\
       
   822 		OPENSSL_free(bufout_alloc);				\
       
   823 		OPENSSL_free(bufin_alloc);				\
       
   824 		T4err(T4_F_CIPHER_DO_AES, T4_R_NOT_BLOCKSIZE_LENGTH);	\
       
   825 		return (0);						\
       
   826 	}								\
       
   827 									\
       
   828 	if (ctx->encrypt) {						\
       
   829 		t4_aes_load_keys_for_encrypt(t4_ks);			\
       
   830 		t4_aes_encrypt(t4_ks, (uint64_t *)bufin,		\
       
   831 		    (uint64_t *)bufout, (size_t)inl, iv);		\
       
   832 	} else { /* decrypt */						\
       
   833 		t4_aes_load_keys_for_decrypt(t4_ks);			\
       
   834 		t4_aes_decrypt(t4_ks, (uint64_t *)bufin,		\
       
   835 		    (uint64_t *)bufout, (size_t)inl, iv);		\
       
   836 	}								\
       
   837 									\
       
   838 	/* Cleanup */							\
       
   839 	if (bufin_alloc != NULL) {					\
       
   840 		if (bufout == bufin_alloc)				\
       
   841 			(void) memcpy(out, bufout, outl);		\
       
   842 		OPENSSL_free(bufin_alloc);				\
       
   843 	}								\
       
   844 	if (bufout_alloc != NULL) {					\
       
   845 		(void) memcpy(out, bufout_alloc, outl);			\
       
   846 		OPENSSL_free(bufout_alloc);				\
       
   847 	}								\
       
   848 									\
       
   849 	return (1);							\
       
   850 }
       
   851 
       
   852 
       
   853 /* AES CBC mode. */
       
   854 T4_CIPHER_DO_AES(t4_cipher_do_aes_128_cbc,
       
   855 	t4_aes128_load_keys_for_encrypt, t4_aes128_cbc_encrypt,
       
   856 	t4_aes128_load_keys_for_decrypt, t4_aes128_cbc_decrypt, tctx->iv)
       
   857 T4_CIPHER_DO_AES(t4_cipher_do_aes_192_cbc,
       
   858 	t4_aes192_load_keys_for_encrypt, t4_aes192_cbc_encrypt,
       
   859 	t4_aes192_load_keys_for_decrypt, t4_aes192_cbc_decrypt, tctx->iv)
       
   860 T4_CIPHER_DO_AES(t4_cipher_do_aes_256_cbc,
       
   861 	t4_aes256_load_keys_for_encrypt, t4_aes256_cbc_encrypt,
       
   862 	t4_aes256_load_keys_for_decrypt, t4_aes256_cbc_decrypt, tctx->iv)
       
   863 
       
   864 /*
       
   865  * AES CFB128 mode.
       
   866  * CFB128 decrypt uses load_keys_for_encrypt() as the mode uses
       
   867  * the raw AES encrypt operation for the decryption, too.
       
   868  */
       
   869 #ifndef	SOLARIS_NO_AES_CFB128
       
   870 T4_CIPHER_DO_AES(t4_cipher_do_aes_128_cfb128,
       
   871 	t4_aes128_load_keys_for_encrypt, t4_aes128_cfb128_encrypt,
       
   872 	t4_aes128_load_keys_for_encrypt, t4_aes128_cfb128_decrypt, tctx->iv)
       
   873 T4_CIPHER_DO_AES(t4_cipher_do_aes_192_cfb128,
       
   874 	t4_aes192_load_keys_for_encrypt, t4_aes192_cfb128_encrypt,
       
   875 	t4_aes192_load_keys_for_encrypt, t4_aes192_cfb128_decrypt, tctx->iv)
       
   876 T4_CIPHER_DO_AES(t4_cipher_do_aes_256_cfb128,
       
   877 	t4_aes256_load_keys_for_encrypt, t4_aes256_cfb128_encrypt,
       
   878 	t4_aes256_load_keys_for_encrypt, t4_aes256_cfb128_decrypt, tctx->iv)
       
   879 #endif	/* !SOLARIS_NO_AES_CFB128 */
       
   880 
       
   881 /* AES CTR mode. */
       
   882 T4_CIPHER_DO_AES(t4_cipher_do_aes_128_ctr,
       
   883 	t4_aes128_load_keys_for_encrypt, t4_aes128_ctr_crypt,
       
   884 	t4_aes128_load_keys_for_decrypt, t4_aes128_ctr_crypt, tctx->iv)
       
   885 T4_CIPHER_DO_AES(t4_cipher_do_aes_192_ctr,
       
   886 	t4_aes192_load_keys_for_encrypt, t4_aes192_ctr_crypt,
       
   887 	t4_aes192_load_keys_for_decrypt, t4_aes192_ctr_crypt, tctx->iv)
       
   888 T4_CIPHER_DO_AES(t4_cipher_do_aes_256_ctr,
       
   889 	t4_aes256_load_keys_for_encrypt, t4_aes256_ctr_crypt,
       
   890 	t4_aes256_load_keys_for_decrypt, t4_aes256_ctr_crypt, tctx->iv)
       
   891 
       
   892 /* AES ECB mode. */
       
   893 T4_CIPHER_DO_AES(t4_cipher_do_aes_128_ecb,
       
   894 	t4_aes128_load_keys_for_encrypt, t4_aes128_ecb_encrypt,
       
   895 	t4_aes128_load_keys_for_decrypt, t4_aes128_ecb_decrypt, NULL)
       
   896 T4_CIPHER_DO_AES(t4_cipher_do_aes_192_ecb,
       
   897 	t4_aes192_load_keys_for_encrypt, t4_aes192_ecb_encrypt,
       
   898 	t4_aes192_load_keys_for_decrypt, t4_aes192_ecb_decrypt, NULL)
       
   899 T4_CIPHER_DO_AES(t4_cipher_do_aes_256_ecb,
       
   900 	t4_aes256_load_keys_for_encrypt, t4_aes256_ecb_encrypt,
       
   901 	t4_aes256_load_keys_for_decrypt, t4_aes256_ecb_decrypt, NULL)
       
   902 
       
   903 
       
   904 /*
       
   905  * Is the t4 engine available?
       
   906  * Passed to ENGINE_set_init_function().
       
   907  */
       
   908 /* ARGSUSED */
       
   909 static int
       
   910 t4_init(ENGINE *e)
       
   911 {
       
   912 	return (1);
       
   913 }
       
   914 
       
   915 
       
   916 /* Passed to ENGINE_set_destroy_function(). */
       
   917 /* ARGSUSED */
       
   918 static int
       
   919 t4_destroy(ENGINE *e)
       
   920 {
       
   921 	ERR_unload_t4_strings();
       
   922 	return (1);
       
   923 }
       
   924 
       
   925 
       
   926 /*
       
   927  * Called by t4_bind_helper().
       
   928  * Note: too early to use T4err() functions on errors.
       
   929  */
       
   930 /* ARGSUSED */
       
   931 static int
       
   932 t4_bind(ENGINE *e)
       
   933 {
       
   934 	_Bool aes_engage, digest_engage, des_engage, montmul_engage;
       
   935 
       
   936 	t4_instructions_present(&aes_engage, &des_engage, &digest_engage,
       
   937 	    &montmul_engage);
       
   938 #ifdef	DEBUG_T4
       
   939 	(void) fprintf(stderr,
       
   940 	    "t4_bind: engage aes=%d, des=%d, digest=%d\n",
       
   941 	    aes_engage, des_engage, digest_engage);
       
   942 #endif
       
   943 #ifndef	OPENSSL_NO_DES
       
   944 	if (!des_engage) { /* Remove DES ciphers from list */
       
   945 		t4_cipher_count -= t4_des_cipher_count;
       
   946 	}
       
   947 #endif
       
   948 
       
   949 #ifdef	DEBUG_T4
       
   950 	(void) fprintf(stderr, "t4_cipher_count = %d; t4_cipher_nids[] =\n",
       
   951 	    t4_cipher_count);
       
   952 	for (int i = 0; i < t4_cipher_count; ++i) {
       
   953 		(void) fprintf(stderr, " %d", t4_cipher_nids[i]);
       
   954 	}
       
   955 	(void) fprintf(stderr, "\n");
       
   956 #endif	/* DEBUG_T4 */
       
   957 
       
   958 	/* Register T4 engine ID, name, and functions */
       
   959 	if (!ENGINE_set_id(e, ENGINE_T4_ID) ||
       
   960 	    !ENGINE_set_name(e,
       
   961 	    aes_engage ? ENGINE_T4_NAME: ENGINE_NO_T4_NAME) ||
       
   962 	    !ENGINE_set_init_function(e, t4_init) ||
       
   963 	    (aes_engage && !ENGINE_set_ciphers(e, t4_get_all_ciphers)) ||
       
   964 	    (digest_engage && !ENGINE_set_digests(e, t4_get_all_digests)) ||
       
   965 #ifndef OPENSSL_NO_RSA
       
   966 	    (montmul_engage && !ENGINE_set_RSA(e, t4_RSA())) ||
       
   967 #endif	/* OPENSSL_NO_RSA */
       
   968 #ifndef OPENSSL_NO_DH
       
   969 	    (montmul_engage && !ENGINE_set_DH(e, t4_DH())) ||
       
   970 #endif	/* OPENSSL_NO_DH */
       
   971 #ifndef OPENSSL_NO_DSA
       
   972 	    (montmul_engage && !ENGINE_set_DSA(e, t4_DSA())) ||
       
   973 #endif	/* OPENSSL_NO_DSA */
       
   974 	    !ENGINE_set_destroy_function(e, t4_destroy)) {
       
   975 		return (0);
       
   976 	}
       
   977 
       
   978 	return (1);
       
   979 }
       
   980 
       
   981 
       
   982 /*
       
   983  * Called by ENGINE_load_t4().
       
   984  * Note: too early to use T4err() functions on errors.
       
   985  */
       
   986 static int
       
   987 t4_bind_helper(ENGINE *e, const char *id)
       
   988 {
       
   989 	if (id != NULL && (strcmp(id, ENGINE_T4_ID) != 0)) {
       
   990 		(void) fprintf(stderr, "T4: bad t4 engine ID\n");
       
   991 		return (0);
       
   992 	}
       
   993 	if (!t4_bind(e)) {
       
   994 		(void) fprintf(stderr,
       
   995 		    "T4: failed to bind t4 engine\n");
       
   996 		return (0);
       
   997 	}
       
   998 
       
   999 	return (1);
       
  1000 }
       
  1001 
       
  1002 
       
  1003 #ifdef	DYNAMIC_ENGINE
       
  1004 IMPLEMENT_DYNAMIC_CHECK_FN()
       
  1005 IMPLEMENT_DYNAMIC_BIND_FN(t4_bind_helper)
       
  1006 #endif	/* DYNAMIC_ENGINE */
       
  1007 #endif	/* COMPILE_HW_T4 */
       
  1008 #endif	/* !OPENSSL_NO_HW && !OPENSSL_NO_HW_AES_T4 && !OPENSSL_NO_AES */