components/openssl/openssl-1.0.1/engines/t4/eng_t4.c
changeset 1382 3515c1afdfc8
parent 1381 15aec33b84fa
child 1383 41b331c5dc97
equal deleted inserted replaced
1381:15aec33b84fa 1382:3515c1afdfc8
     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 T4 MONTMUL
       
    62  * instructions, such as the T4 microprocessor.
       
    63  */
       
    64 
       
    65 #include <openssl/opensslconf.h>
       
    66 
       
    67 #if !defined(OPENSSL_NO_HW)
       
    68 #include <sys/types.h>
       
    69 #include <sys/auxv.h>	/* getisax() */
       
    70 #include <stdlib.h>
       
    71 #include <stdbool.h>
       
    72 #include <string.h>
       
    73 #include <errno.h>
       
    74 #include <openssl/engine.h>
       
    75 
       
    76 #define	T4_LIB_NAME "SPARC T4 engine"
       
    77 #include "eng_t4_err.c"
       
    78 
       
    79 /* Constants used when creating the ENGINE */
       
    80 static const char *ENGINE_T4_ID = "t4";
       
    81 static const char *ENGINE_T4_NAME = "SPARC T4 engine support";
       
    82 static const char *ENGINE_NO_T4_NAME = "SPARC T4 engine support (no T4)";
       
    83 
       
    84 
       
    85 #if (defined(sun4v) || defined(__sparcv9) || defined(__sparcv8plus) || \
       
    86 	defined(__sparcv8)) && !defined(OPENSSL_NO_ASM)
       
    87 #define	COMPILE_HW_T4
       
    88 static int t4_bind_helper(ENGINE *e, const char *id);
       
    89 #pragma inline(t4_bind_helper)
       
    90 #endif
       
    91 
       
    92 /*
       
    93  * This makes the engine "built-in" with OpenSSL.
       
    94  * On non-T4 CPUs this just returns.
       
    95  * Called by ENGINE_load_builtin_engines().
       
    96  */
       
    97 void
       
    98 ENGINE_load_t4(void)
       
    99 {
       
   100 #ifdef	COMPILE_HW_T4
       
   101 	ENGINE *toadd = ENGINE_new();
       
   102 	if (toadd != NULL) {
       
   103 		if (t4_bind_helper(toadd, ENGINE_T4_ID) != 0) {
       
   104 			(void) ENGINE_add(toadd);
       
   105 			(void) ENGINE_free(toadd);
       
   106 			ERR_clear_error();
       
   107 		} else {
       
   108 			(void) ENGINE_free(toadd);
       
   109 		}
       
   110 	}
       
   111 #endif
       
   112 }
       
   113 
       
   114 
       
   115 #ifdef	COMPILE_HW_T4
       
   116 static int t4_bind(ENGINE *e);
       
   117 #ifndef	DYNAMIC_ENGINE
       
   118 #pragma inline(t4_bind)
       
   119 #endif
       
   120 static void t4_instructions_present(_Bool *montmul_present);
       
   121 #pragma inline(t4_instructions_present)
       
   122 
       
   123 /* RSA_METHOD structure used by ENGINE_set_RSA() */
       
   124 extern RSA_METHOD *t4_RSA(void);
       
   125 
       
   126 /* DH_METHOD structure used by ENGINE_set_DH() */
       
   127 extern DH_METHOD *t4_DH(void);
       
   128 
       
   129 /* DSA_METHOD structure used by ENGINE_set_DSA() */
       
   130 extern DSA_METHOD *t4_DSA(void);
       
   131 
       
   132 /*
       
   133  * Utility Functions
       
   134  */
       
   135 
       
   136 /*
       
   137  * Set montmul_present to B_FALSE or B_TRUE depending on whether the
       
   138  * current SPARC processor supports MONTMUL.
       
   139  */
       
   140 static void
       
   141 t4_instructions_present(_Bool *montmul_present)
       
   142 {
       
   143 	uint_t		ui;
       
   144 
       
   145 	(void) getisax(&ui, 1);
       
   146 	*montmul_present = ((ui & AV_SPARC_MONT) != 0);
       
   147 }
       
   148 
       
   149 
       
   150 
       
   151 /*
       
   152  * Is the t4 engine available?
       
   153  * Passed to ENGINE_set_init_function().
       
   154  */
       
   155 /* ARGSUSED */
       
   156 static int
       
   157 t4_init(ENGINE *e)
       
   158 {
       
   159 	return (1);
       
   160 }
       
   161 
       
   162 /* Passed to ENGINE_set_destroy_function(). */
       
   163 /* ARGSUSED */
       
   164 static int
       
   165 t4_destroy(ENGINE *e)
       
   166 {
       
   167 	ERR_unload_t4_strings();
       
   168 	return (1);
       
   169 }
       
   170 
       
   171 
       
   172 /*
       
   173  * Called by t4_bind_helper().
       
   174  * Note: too early to use T4err() functions on errors.
       
   175  */
       
   176 /* ARGSUSED */
       
   177 static int
       
   178 t4_bind(ENGINE *e)
       
   179 {
       
   180 	_Bool montmul_engage;
       
   181 
       
   182 	/* Register T4 engine ID, name, and functions */
       
   183 	if (!ENGINE_set_id(e, ENGINE_T4_ID) ||
       
   184 	    !ENGINE_set_name(e,
       
   185 	    montmul_engage ? ENGINE_T4_NAME : ENGINE_NO_T4_NAME) ||
       
   186 	    !ENGINE_set_init_function(e, t4_init) ||
       
   187 #ifndef OPENSSL_NO_RSA
       
   188 	    (montmul_engage && !ENGINE_set_RSA(e, t4_RSA())) ||
       
   189 #endif	/* OPENSSL_NO_RSA */
       
   190 #ifndef OPENSSL_NO_DH
       
   191 	    (montmul_engage && !ENGINE_set_DH(e, t4_DH())) ||
       
   192 #endif	/* OPENSSL_NO_DH */
       
   193 #ifndef OPENSSL_NO_DSA
       
   194 	    (montmul_engage && !ENGINE_set_DSA(e, t4_DSA())) ||
       
   195 #endif	/* OPENSSL_NO_DSA */
       
   196 	    !ENGINE_set_destroy_function(e, t4_destroy)) {
       
   197 		return (0);
       
   198 	}
       
   199 
       
   200 	return (1);
       
   201 }
       
   202 
       
   203 
       
   204 /*
       
   205  * Called by ENGINE_load_t4().
       
   206  * Note: too early to use T4err() functions on errors.
       
   207  */
       
   208 static int
       
   209 t4_bind_helper(ENGINE *e, const char *id)
       
   210 {
       
   211 	if (id != NULL && (strcmp(id, ENGINE_T4_ID) != 0)) {
       
   212 		(void) fprintf(stderr, "T4: bad t4 engine ID\n");
       
   213 		return (0);
       
   214 	}
       
   215 	if (!t4_bind(e)) {
       
   216 		(void) fprintf(stderr,
       
   217 		    "T4: failed to bind t4 engine\n");
       
   218 		return (0);
       
   219 	}
       
   220 
       
   221 	return (1);
       
   222 }
       
   223 
       
   224 
       
   225 #ifdef	DYNAMIC_ENGINE
       
   226 IMPLEMENT_DYNAMIC_CHECK_FN()
       
   227 IMPLEMENT_DYNAMIC_BIND_FN(t4_bind_helper)
       
   228 #endif	/* DYNAMIC_ENGINE */
       
   229 #endif	/* COMPILE_HW_T4 */
       
   230 #endif	/* !OPENSSL_NO_HW */