components/openssl/openssl-1.0.1/engines/t4/eng_t4.c
changeset 1373 2fd83bee8884
parent 1341 af3d6ef22d65
equal deleted inserted replaced
1372:5f183c30981d 1373:2fd83bee8884
    56 /*
    56 /*
    57  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
    57  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
    58  */
    58  */
    59 
    59 
    60 /*
    60 /*
    61  * This engine supports SPARC microprocessors that provide T4 DES and MONTMUL
    61  * This engine supports SPARC microprocessors that provide T4 MONTMUL
    62  * instructions, such as the T4 microprocessor.
    62  * instructions, such as the T4 microprocessor.
    63  */
    63  */
    64 
    64 
    65 #include <openssl/opensslconf.h>
    65 #include <openssl/opensslconf.h>
    66 
    66 
   115 #ifdef	COMPILE_HW_T4
   115 #ifdef	COMPILE_HW_T4
   116 static int t4_bind(ENGINE *e);
   116 static int t4_bind(ENGINE *e);
   117 #ifndef	DYNAMIC_ENGINE
   117 #ifndef	DYNAMIC_ENGINE
   118 #pragma inline(t4_bind)
   118 #pragma inline(t4_bind)
   119 #endif
   119 #endif
   120 static void t4_instructions_present(_Bool *des_present, _Bool *montmul_present);
   120 static void t4_instructions_present(_Bool *montmul_present);
   121 #pragma inline(t4_instructions_present)
   121 #pragma inline(t4_instructions_present)
   122 
   122 
   123 /* RSA_METHOD structure used by ENGINE_set_RSA() */
   123 /* RSA_METHOD structure used by ENGINE_set_RSA() */
   124 extern RSA_METHOD *t4_RSA(void);
   124 extern RSA_METHOD *t4_RSA(void);
   125 
   125 
   127 extern DH_METHOD *t4_DH(void);
   127 extern DH_METHOD *t4_DH(void);
   128 
   128 
   129 /* DSA_METHOD structure used by ENGINE_set_DSA() */
   129 /* DSA_METHOD structure used by ENGINE_set_DSA() */
   130 extern DSA_METHOD *t4_DSA(void);
   130 extern DSA_METHOD *t4_DSA(void);
   131 
   131 
   132 /* Static variables */
       
   133 /* This can't be const as NID*ctr is inserted when the engine is initialized */
       
   134 static int t4_cipher_nids[] = {
       
   135 #ifndef	OPENSSL_NO_DES
       
   136 	/* Must be at end of list (see t4_des_cipher_count in t4_bind() */
       
   137 	NID_des_cbc, NID_des_ede3_cbc, NID_des_ecb, NID_des_ede3_ecb,
       
   138 #endif
       
   139 };
       
   140 static const int t4_des_cipher_count = 4;
       
   141 static int t4_cipher_count =
       
   142 	(sizeof (t4_cipher_nids) / sizeof (t4_cipher_nids[0]));
       
   143 
       
   144 
       
   145 
       
   146 /*
       
   147  * Cipher Algorithms
       
   148  *
       
   149  * OpenSSL's libcrypto EVP stuff. This is how this engine gets wired to EVP.
       
   150  * EVP_CIPHER is defined in evp.h.  To maintain binary compatibility the
       
   151  * definition cannot be modified.
       
   152  * Stuff specific to the t4 engine is kept in t4_cipher_ctx_t, which is
       
   153  * pointed to by cipher_data or md_data
       
   154  *
       
   155  * Fields: nid, block_size, key_len, iv_len, flags,
       
   156  *	init(), do_cipher(), cleanup(),
       
   157  *	ctx_size,
       
   158  *	set_asn1_parameters(), get_asn1_parameters(), ctrl(), app_data
       
   159  */
       
   160 
       
   161 
       
   162 #ifndef	OPENSSL_NO_DES
       
   163 extern const EVP_CIPHER t4_des_cbc;
       
   164 extern const EVP_CIPHER t4_des3_cbc;
       
   165 extern const EVP_CIPHER t4_des_ecb;
       
   166 extern const EVP_CIPHER t4_des3_ecb;
       
   167 #endif	/* OPENSSL_NO_DES */
       
   168 
       
   169 
       
   170 /*
   132 /*
   171  * Utility Functions
   133  * Utility Functions
   172  */
   134  */
   173 
   135 
   174 /*
   136 /*
   175  * Set des_present and montmul_present to B_FALSE or B_TRUE
   137  * Set montmul_present to B_FALSE or B_TRUE depending on whether the
   176  * depending on whether the current SPARC processor supports DES
   138  * current SPARC processor supports MONTMUL.
   177  * and MONTMUL, respectively.
       
   178  */
   139  */
   179 static void
   140 static void
   180 t4_instructions_present(_Bool *des_present, _Bool *montmul_present)
   141 t4_instructions_present(_Bool *montmul_present)
   181 {
   142 {
   182 #ifdef	OPENSSL_NO_DES
       
   183 #undef	AV_SPARC_DES
       
   184 #define	AV_SPARC_DES	0
       
   185 #endif
       
   186 	uint_t		ui;
   143 	uint_t		ui;
   187 
   144 
   188 	(void) getisax(&ui, 1);
   145 	(void) getisax(&ui, 1);
   189 	*des_present = ((ui & AV_SPARC_DES) != 0);
       
   190 	*montmul_present = ((ui & AV_SPARC_MONT) != 0);
   146 	*montmul_present = ((ui & AV_SPARC_MONT) != 0);
   191 }
   147 }
   192 
   148 
   193 
       
   194 /*
       
   195  * Cipher functions
       
   196  */
       
   197 
       
   198 
       
   199 /*
       
   200  * Registered by the ENGINE with ENGINE_set_ciphers().
       
   201  * Finds out how to deal with a particular cipher NID in the ENGINE.
       
   202  */
       
   203 /* ARGSUSED */
       
   204 static int
       
   205 t4_get_all_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
       
   206     const int **nids, int nid)
       
   207 {
       
   208 	if (cipher == NULL) { /* return a list of all supported ciphers */
       
   209 		*nids = (t4_cipher_count > 0) ? t4_cipher_nids : NULL;
       
   210 		return (t4_cipher_count);
       
   211 	}
       
   212 
       
   213 	switch (nid) {
       
   214 #ifndef	OPENSSL_NO_DES
       
   215 	case NID_des_cbc:
       
   216 		*cipher = &t4_des_cbc;
       
   217 		break;
       
   218 	case NID_des_ede3_cbc:
       
   219 		*cipher = &t4_des3_cbc;
       
   220 		break;
       
   221 	case NID_des_ecb:
       
   222 		*cipher = &t4_des_ecb;
       
   223 		break;
       
   224 	case NID_des_ede3_ecb:
       
   225 		*cipher = &t4_des3_ecb;
       
   226 		break;
       
   227 #endif	/* !OPENSSL_NO_DES */
       
   228 	default:
       
   229 		/* cipher not supported */
       
   230 		*cipher = NULL;
       
   231 		return (0);
       
   232 	}
       
   233 
       
   234 	return (1);
       
   235 }
       
   236 
   149 
   237 
   150 
   238 /*
   151 /*
   239  * Is the t4 engine available?
   152  * Is the t4 engine available?
   240  * Passed to ENGINE_set_init_function().
   153  * Passed to ENGINE_set_init_function().
   244 t4_init(ENGINE *e)
   157 t4_init(ENGINE *e)
   245 {
   158 {
   246 	return (1);
   159 	return (1);
   247 }
   160 }
   248 
   161 
   249 
       
   250 /* Passed to ENGINE_set_destroy_function(). */
   162 /* Passed to ENGINE_set_destroy_function(). */
   251 /* ARGSUSED */
   163 /* ARGSUSED */
   252 static int
   164 static int
   253 t4_destroy(ENGINE *e)
   165 t4_destroy(ENGINE *e)
   254 {
   166 {
   263  */
   175  */
   264 /* ARGSUSED */
   176 /* ARGSUSED */
   265 static int
   177 static int
   266 t4_bind(ENGINE *e)
   178 t4_bind(ENGINE *e)
   267 {
   179 {
   268 	_Bool des_engage, montmul_engage;
   180 	_Bool montmul_engage;
   269 
       
   270 #ifndef	OPENSSL_NO_DES
       
   271 	if (!des_engage) { /* Remove DES ciphers from list */
       
   272 		t4_cipher_count -= t4_des_cipher_count;
       
   273 	}
       
   274 #endif
       
   275 
       
   276 #ifdef	DEBUG_T4
       
   277 	(void) fprintf(stderr, "t4_cipher_count = %d; t4_cipher_nids[] =\n",
       
   278 	    t4_cipher_count);
       
   279 	for (int i = 0; i < t4_cipher_count; ++i) {
       
   280 		(void) fprintf(stderr, " %d", t4_cipher_nids[i]);
       
   281 	}
       
   282 	(void) fprintf(stderr, "\n");
       
   283 #endif	/* DEBUG_T4 */
       
   284 
   181 
   285 	/* Register T4 engine ID, name, and functions */
   182 	/* Register T4 engine ID, name, and functions */
   286 	if (!ENGINE_set_id(e, ENGINE_T4_ID) ||
   183 	if (!ENGINE_set_id(e, ENGINE_T4_ID) ||
   287 	    !ENGINE_set_name(e,
   184 	    !ENGINE_set_name(e,
   288 	    des_engage ? ENGINE_T4_NAME : ENGINE_NO_T4_NAME) ||
   185 	    montmul_engage ? ENGINE_T4_NAME : ENGINE_NO_T4_NAME) ||
   289 	    !ENGINE_set_init_function(e, t4_init) ||
   186 	    !ENGINE_set_init_function(e, t4_init) ||
   290 	    (des_engage && !ENGINE_set_ciphers(e, t4_get_all_ciphers)) ||
       
   291 #ifndef OPENSSL_NO_RSA
   187 #ifndef OPENSSL_NO_RSA
   292 	    (montmul_engage && !ENGINE_set_RSA(e, t4_RSA())) ||
   188 	    (montmul_engage && !ENGINE_set_RSA(e, t4_RSA())) ||
   293 #endif	/* OPENSSL_NO_RSA */
   189 #endif	/* OPENSSL_NO_RSA */
   294 #ifndef OPENSSL_NO_DH
   190 #ifndef OPENSSL_NO_DH
   295 	    (montmul_engage && !ENGINE_set_DH(e, t4_DH())) ||
   191 	    (montmul_engage && !ENGINE_set_DH(e, t4_DH())) ||