73 #ifndef OPENSSL_NO_HW_ZENCOD
78 # include "vendor_defns/hw_zencod.h"
81 #define ZENCOD_LIB_NAME "zencod engine"
84 #define FAIL_TO_SOFTWARE -15
86 #define ZEN_LIBRARY "zenbridge"
89 # define PERROR(s) perror(s)
90 # define CHEESE() fputs("## [ZenEngine] ## " __FUNCTION__ "\n", stderr)
99 static inline void esrever (
unsigned char *d,
int l )
101 for(;--l>0;--l,d++){*d^=*(d+l);*(d+l)^=*d;*d^=*(d+l);}
104 static inline void ypcmem (
unsigned char *d,
const unsigned char *s,
int l )
106 for(d+=l;l--;)*--d=*s++;
109 static __inline
void esrever (
unsigned char *d,
int l )
111 for(;--l>0;--l,d++){*d^=*(d+l);*(d+l)^=*d;*d^=*(d+l);}
114 static __inline
void ypcmem (
unsigned char *d,
const unsigned char *s,
int l )
116 for(d+=l;l--;)*--d=*s++;
121 #define BIGNUM2ZEN(n, bn) (ptr_zencod_init_number((n), \
122 (unsigned long) ((bn)->top * BN_BITS2), \
123 (unsigned char *) ((bn)->d)))
125 #define ZEN_BITS(n, bytes) (ptr_zencod_bytes2bits((unsigned char *) (n), (unsigned long) (bytes)))
126 #define ZEN_BYTES(bits) (ptr_zencod_bits2bytes((unsigned long) (bits)))
130 static int zencod_destroy (
ENGINE *
e ) ;
131 static int zencod_init (
ENGINE *
e ) ;
132 static int zencod_finish (
ENGINE *
e ) ;
133 static int zencod_ctrl (
ENGINE *
e,
int cmd,
long i,
void *
p,
void (*
f) () ) ;
139 #ifndef OPENSSL_NO_RSA
140 static int RSA_zencod_rsa_mod_exp (
BIGNUM *r0,
const BIGNUM *
I,
RSA *rsa ) ;
146 #ifndef OPENSSL_NO_DSA
150 static DSA_SIG *DSA_zencod_do_sign (
const unsigned char *dgst,
int dlen,
DSA *dsa ) ;
151 static int DSA_zencod_do_verify (
const unsigned char *dgst,
int dgst_len,
DSA_SIG *sig,
156 #ifndef OPENSSL_NO_DH
159 static int DH_zencod_generate_key (
DH *dh ) ;
160 static int DH_zencod_compute_key (
unsigned char *
key,
const BIGNUM *pub_key,
DH *dh ) ;
164 static void RAND_zencod_seed (
const void *buf,
int num ) ;
165 static int RAND_zencod_rand_bytes (
unsigned char *buf,
int num ) ;
166 static int RAND_zencod_rand_status (
void ) ;
169 static int engine_digests (
ENGINE *e,
const EVP_MD **digest,
const int **nids,
int nid ) ;
172 static int engine_ciphers (
ENGINE *e,
const EVP_CIPHER **cipher,
const int **nids,
int nid ) ;
175 #define ZENCOD_CMD_SO_PATH ENGINE_CMD_BASE
180 "Specifies the path to the 'zenbridge' shared library",
186 #ifndef OPENSSL_NO_RSA
195 RSA_zencod_rsa_mod_exp,
196 RSA_zencod_bn_mod_exp,
206 #ifndef OPENSSL_NO_DSA
213 DSA_zencod_do_verify,
215 DSA_zencod_bn_mod_exp,
223 #ifndef OPENSSL_NO_DH
228 DH_zencod_generate_key,
229 DH_zencod_compute_key,
230 DH_zencod_bn_mod_exp,
242 RAND_zencod_rand_bytes,
245 RAND_zencod_rand_bytes,
246 RAND_zencod_rand_status
251 static const char *engine_zencod_id =
"zencod";
252 static const char *engine_zencod_name =
"ZENCOD hardware engine support";
258 static int bind_helper (
ENGINE *e )
261 #ifndef OPENSSL_NO_RSA
264 #ifndef OPENSSL_NO_DSA
267 #ifndef OPENSSL_NO_DH
276 #ifndef OPENSSL_NO_RSA
279 #ifndef OPENSSL_NO_DSA
282 #ifndef OPENSSL_NO_DH
297 #ifndef OPENSSL_NO_RSA
318 #ifndef OPENSSL_NO_DSA
332 #ifndef OPENSSL_NO_DH
354 zencod_rand.
add = meth_rand->
add ;
361 ERR_load_ZENCOD_strings () ;
369 static ENGINE *ENGINE_zencod (
void )
377 if ( !bind_helper ( eng ) ) {
386 #ifdef ENGINE_DYNAMIC_SUPPORT
392 ENGINE *toadd = ENGINE_zencod ( ) ;
393 if ( !toadd ) return ;
406 static DSO *zencod_dso = NULL ;
438 static const char *ZENCOD_Fct_0 =
"test_device" ;
439 static const char *ZENCOD_Fct_1 =
"zenbridge_bytes2bits" ;
440 static const char *ZENCOD_Fct_2 =
"zenbridge_bits2bytes" ;
441 static const char *ZENCOD_Fct_3 =
"zenbridge_new_number" ;
442 static const char *ZENCOD_Fct_4 =
"zenbridge_init_number" ;
444 static const char *ZENCOD_Fct_exp_1 =
"zenbridge_rsa_mod_exp" ;
445 static const char *ZENCOD_Fct_exp_2 =
"zenbridge_rsa_mod_exp_crt" ;
446 static const char *ZENCOD_Fct_dsa_1 =
"zenbridge_dsa_do_sign" ;
447 static const char *ZENCOD_Fct_dsa_2 =
"zenbridge_dsa_do_verify" ;
448 static const char *ZENCOD_Fct_dh_1 =
"zenbridge_dh_generate_key" ;
449 static const char *ZENCOD_Fct_dh_2 =
"zenbridge_dh_compute_key" ;
450 static const char *ZENCOD_Fct_rand_1 =
"zenbridge_rand_bytes" ;
451 static const char *ZENCOD_Fct_math_1 =
"zenbridge_math_mod_exp" ;
453 static const char *ZENCOD_Fct_md5_1 =
"zenbridge_md5_init" ;
454 static const char *ZENCOD_Fct_md5_2 =
"zenbridge_md5_update" ;
455 static const char *ZENCOD_Fct_md5_3 =
"zenbridge_md5_do_final" ;
456 static const char *ZENCOD_Fct_sha1_1 =
"zenbridge_sha1_init" ;
457 static const char *ZENCOD_Fct_sha1_2 =
"zenbridge_sha1_update" ;
458 static const char *ZENCOD_Fct_sha1_3 =
"zenbridge_sha1_do_final" ;
460 static const char *ZENCOD_Fct_xdes_1 =
"zenbridge_xdes_cipher" ;
461 static const char *ZENCOD_Fct_rc4_1 =
"zenbridge_rc4_cipher" ;
465 static int zencod_destroy (
ENGINE *e )
468 ERR_unload_ZENCOD_strings () ;
476 static int zencod_init (
ENGINE *e )
508 if ( zencod_dso != NULL ) {
514 zencod_dso =
DSO_load ( NULL, ZENCOD_LIBNAME, NULL, 0 ) ;
515 if ( zencod_dso == NULL ) {
550 ptr_zencod_test = ptr_0 ;
551 ptr_zencod_bytes2bits = ptr_1 ;
552 ptr_zencod_bits2bytes = ptr_2 ;
553 ptr_zencod_new_number = ptr_3 ;
554 ptr_zencod_init_number = ptr_4 ;
555 ptr_zencod_rsa_mod_exp = ptr_exp_1 ;
556 ptr_zencod_rsa_mod_exp_crt = ptr_exp_2 ;
557 ptr_zencod_dsa_do_sign = ptr_dsa_1 ;
558 ptr_zencod_dsa_do_verify = ptr_dsa_2 ;
559 ptr_zencod_dh_generate_key = ptr_dh_1 ;
560 ptr_zencod_dh_compute_key = ptr_dh_2 ;
561 ptr_zencod_rand_bytes = ptr_rand_1 ;
562 ptr_zencod_math_mod_exp = ptr_math_1 ;
563 ptr_zencod_test = ptr_0 ;
564 ptr_zencod_md5_init = ptr_md5_1 ;
565 ptr_zencod_md5_update = ptr_md5_2 ;
566 ptr_zencod_md5_do_final = ptr_md5_3 ;
567 ptr_zencod_sha1_init = ptr_sha1_1 ;
568 ptr_zencod_sha1_update = ptr_sha1_2 ;
569 ptr_zencod_sha1_do_final = ptr_sha1_3 ;
570 ptr_zencod_xdes_cipher = ptr_xdes_1 ;
571 ptr_zencod_rc4_cipher = ptr_rc4_1 ;
578 if ( ptr_zencod_test () != 0 ) {
589 ptr_zencod_bytes2bits = NULL ;
590 ptr_zencod_bits2bytes = NULL ;
591 ptr_zencod_new_number = NULL ;
592 ptr_zencod_init_number = NULL ;
593 ptr_zencod_rsa_mod_exp = NULL ;
594 ptr_zencod_rsa_mod_exp_crt = NULL ;
595 ptr_zencod_dsa_do_sign = NULL ;
596 ptr_zencod_dsa_do_verify = NULL ;
597 ptr_zencod_dh_generate_key = NULL ;
598 ptr_zencod_dh_compute_key = NULL ;
599 ptr_zencod_rand_bytes = NULL ;
600 ptr_zencod_math_mod_exp = NULL ;
601 ptr_zencod_test = NULL ;
602 ptr_zencod_md5_init = NULL ;
603 ptr_zencod_md5_update = NULL ;
604 ptr_zencod_md5_do_final = NULL ;
605 ptr_zencod_sha1_init = NULL ;
606 ptr_zencod_sha1_update = NULL ;
607 ptr_zencod_sha1_do_final = NULL ;
608 ptr_zencod_xdes_cipher = NULL ;
609 ptr_zencod_rc4_cipher = NULL ;
615 static int zencod_finish (
ENGINE *e )
624 if ( zencod_dso == NULL ) {
635 ptr_zencod_bytes2bits = NULL ;
636 ptr_zencod_bits2bytes = NULL ;
637 ptr_zencod_new_number = NULL ;
638 ptr_zencod_init_number = NULL ;
639 ptr_zencod_rsa_mod_exp = NULL ;
640 ptr_zencod_rsa_mod_exp_crt = NULL ;
641 ptr_zencod_dsa_do_sign = NULL ;
642 ptr_zencod_dsa_do_verify = NULL ;
643 ptr_zencod_dh_generate_key = NULL ;
644 ptr_zencod_dh_compute_key = NULL ;
645 ptr_zencod_rand_bytes = NULL ;
646 ptr_zencod_math_mod_exp = NULL ;
647 ptr_zencod_test = NULL ;
648 ptr_zencod_md5_init = NULL ;
649 ptr_zencod_md5_update = NULL ;
650 ptr_zencod_md5_do_final = NULL ;
651 ptr_zencod_sha1_init = NULL ;
652 ptr_zencod_sha1_update = NULL ;
653 ptr_zencod_sha1_do_final = NULL ;
654 ptr_zencod_xdes_cipher = NULL ;
655 ptr_zencod_rc4_cipher = NULL ;
661 static int zencod_ctrl (
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f) () )
664 int initialised = ( ( zencod_dso == NULL ) ? 0 : 1 ) ;
682 ZENCOD_LIBNAME = (
const char *) p ;
715 ptr_zencod_init_number ( &y, (r->
dmax - 1) * sizeof (BN_ULONG) * 8, (
unsigned char *) r->
d ) ;
721 ret = ptr_zencod_math_mod_exp ( &y, &e, &x, &n ) ;
724 PERROR(
"zenbridge_math_mod_exp");
737 #ifndef OPENSSL_NO_RSA
738 static int RSA_zencod_rsa_mod_exp (
BIGNUM *r0,
const BIGNUM *i,
RSA *rsa )
748 if ( !rsa->
p || !rsa->
q || !rsa->
dmp1 || !rsa->
dmq1 || !rsa->
iqmp ) {
766 r0->
top = (
RSA_size(rsa) * 8 + BN_BITS2 - 1) / BN_BITS2;
776 if ( ptr_zencod_rsa_mod_exp_crt ( &y, &x, &p, &q, &dmp1, &dmq1, &iqmp ) < 0 ) {
777 PERROR(
"zenbridge_rsa_mod_exp_crt");
805 return meth->
bn_mod_exp(r, a, p, m, ctx, m_ctx);
820 if ( ptr_zencod_rsa_mod_exp ( &y, &x, &n, &e ) < 0 ) {
821 PERROR(
"zenbridge_rsa_mod_exp");
832 #ifndef OPENSSL_NO_DSA
835 static DSA_SIG *DSA_zencod_do_sign (
const unsigned char *dgst,
int dlen,
DSA *dsa )
874 bn_r->
top = bn_s->
top = (160 + BN_BITS2 - 1) / BN_BITS2;
884 ypcmem(msg, dgst, 20);
885 ptr_zencod_init_number ( &data, 160, msg ) ;
887 if ( ptr_zencod_dsa_do_sign ( 0, &data, &y, &p, &q, &g, &x, &r, &s ) < 0 ) {
888 PERROR(
"zenbridge_dsa_do_sign");
910 static int DSA_zencod_do_verify (
const unsigned char *dgst,
int dlen,
DSA_SIG *sig,
DSA *dsa )
944 ptr_zencod_init_number ( &v, 160, v_data ) ;
945 ypcmem(msg, dgst, 20);
946 ptr_zencod_init_number ( &data, 160, msg ) ;
948 if ( ( ret = ptr_zencod_dsa_do_verify ( 0, &data, &p, &q, &g, &y, &r, &s, &v ) ) < 0 ) {
949 PERROR(
"zenbridge_dsa_do_verify");
954 return ( ( ret == 0 ) ? 1 : ret ) ;
963 return zencod_bn_mod_exp ( r, a, p, m, ctx ) ;
968 #ifndef OPENSSl_NO_DH
971 static int DH_zencod_generate_key (
DH *dh )
990 if (!(bn_prv =
BN_new())) {
1001 if ( !( bn_pub =
BN_new () ) ) {
1027 if ( ptr_zencod_dh_generate_key ( &y, &x, &g, &p, generate_x ) < 0 ) {
1028 perror(
"zenbridge_dh_generate_key");
1048 static int DH_zencod_compute_key (
unsigned char *
key,
const BIGNUM *pub_key,
DH *dh )
1054 if ( !zencod_dso ) {
1068 ptr_zencod_init_number ( &k, p.
len, key ) ;
1076 if ( ptr_zencod_dh_compute_key ( &k, &y, &x, &p ) < 0 ) {
1094 return zencod_bn_mod_exp ( r, a, p, m, ctx ) ;
1101 static void RAND_zencod_seed (
const void *buf,
int num )
1107 static int RAND_zencod_rand_bytes (
unsigned char *buf,
int num )
1113 if ( !zencod_dso ) {
1118 ptr_zencod_init_number ( &r, num * 8, buf ) ;
1121 PERROR(
"zenbridge_rand_bytes");
1130 static int RAND_zencod_rand_status (
void )
1141 #ifdef ENGINE_DYNAMIC_SUPPORT
1142 static int bind_fn (
ENGINE *e,
const char *
id )
1145 if (
id && ( strcmp (
id, engine_zencod_id ) != 0 ) ) {
1148 if ( !bind_helper ( e ) ) {
1181 static int engine_digest_nids [ ] = { } ;
1182 static int engine_digest_nids_num = 0 ;
1185 static int engine_cipher_nids_num = 2 ;
1190 static int engine_sha1_init (
EVP_MD_CTX *ctx ) ;
1191 static int engine_sha1_update (
EVP_MD_CTX *ctx,
const void *data,
unsigned long count ) ;
1192 static int engine_sha1_final (
EVP_MD_CTX *ctx,
unsigned char *md ) ;
1195 static int engine_md5_init (
EVP_MD_CTX *ctx ) ;
1196 static int engine_md5_update (
EVP_MD_CTX *ctx,
const void *data,
unsigned long count ) ;
1197 static int engine_md5_final (
EVP_MD_CTX *ctx,
unsigned char *md ) ;
1199 static int engine_md_cleanup (
EVP_MD_CTX *ctx ) ;
1204 static int engine_rc4_init_key (
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
const unsigned char *iv,
int enc ) ;
1205 static int engine_rc4_cipher (
EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int inl ) ;
1208 static int engine_des_init_key (
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
const unsigned char *iv,
int enc ) ;
1209 static int engine_des_cbc_cipher (
EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int inl ) ;
1212 static int engine_des_ede3_init_key (
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
const unsigned char *iv,
int enc ) ;
1213 static int engine_des_ede3_cbc_cipher (
EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int inl ) ;
1219 static const EVP_MD engine_sha1_md =
1240 static const EVP_MD engine_md5_md =
1262 #define EVP_RC4_KEY_SIZE 16
1269 unsigned char rc4_state [ 260 ] ;
1272 #define rc4_data(ctx) ( (EVP_RC4_KEY *) ( ctx )->cipher_data )
1281 engine_rc4_init_key,
1283 engine_cipher_cleanup,
1298 engine_rc4_init_key,
1300 engine_cipher_cleanup,
1312 unsigned char des_key [ 24 ] ;
1313 unsigned char des_iv [ 8 ] ;
1321 engine_des_init_key,
1322 engine_des_cbc_cipher,
1323 engine_cipher_cleanup,
1336 unsigned char des3_key [ 24 ] ;
1337 unsigned char des3_iv [ 8 ] ;
1340 #define des_data(ctx) ( (DES_EDE_KEY *) ( ctx )->cipher_data )
1342 static const EVP_CIPHER engine_des_ede3_cbc =
1347 engine_des_ede3_init_key,
1348 engine_des_ede3_cbc_cipher,
1349 engine_cipher_cleanup,
1359 static int engine_digests (
ENGINE *e,
const EVP_MD **digest,
const int **nids,
int nid )
1362 #ifdef DEBUG_ZENCOD_MD
1363 fprintf ( stderr,
"\t=>Function : static int engine_digests () called !\n" ) ;
1368 *nids = engine_digest_nids ;
1369 return engine_digest_nids_num ;
1373 *digest = &engine_md5_md ;
1376 *digest = &engine_sha1_md ;
1388 static int engine_sha1_init (
EVP_MD_CTX *ctx )
1395 to_return = !to_return ;
1401 static int engine_sha1_update (
EVP_MD_CTX *ctx,
const void *data,
unsigned long count )
1409 input.
data = (
unsigned char *) data ;
1413 to_return = !to_return ;
1419 static int engine_sha1_final (
EVP_MD_CTX *ctx,
unsigned char *md )
1431 to_return = !to_return ;
1440 static int engine_md5_init (
EVP_MD_CTX *ctx )
1447 to_return = !to_return ;
1453 static int engine_md5_update (
EVP_MD_CTX *ctx,
const void *data,
unsigned long count )
1461 input.
data = (
unsigned char *) data ;
1465 to_return = !to_return ;
1471 static int engine_md5_final (
EVP_MD_CTX *ctx,
unsigned char *md )
1483 to_return = !to_return ;
1489 static int engine_md_cleanup (
EVP_MD_CTX *ctx )
1517 static int engine_ciphers (
ENGINE *e,
const EVP_CIPHER **cipher,
const int **nids,
int nid )
1522 *nids = engine_cipher_nids ;
1523 return engine_cipher_nids_num ;
1527 *cipher = &engine_rc4 ;
1530 *cipher = &engine_rc4_40 ;
1533 *cipher = &engine_des_cbc ;
1536 *cipher = &engine_des_ede3_cbc ;
1547 static int engine_rc4_init_key (
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
const unsigned char *iv,
int enc )
1555 tmp_rc4_key->
first = 0 ;
1560 for ( i = 0; i < nb ; i++ ) {
1570 static int engine_rc4_cipher (
EVP_CIPHER_CTX *ctx,
unsigned char *
out,
const unsigned char *
in,
unsigned int in_len )
1579 input.
len = in_len ;
1580 input.
data = (
unsigned char *) in ;
1581 output.
len = in_len ;
1582 output.
data = (
unsigned char *) out ;
1589 to_return = ptr_zencod_rc4_cipher ( &output, &input, (
const zen_nb_t *) &rc4key, &( tmp_rc4_key->
rc4_state [0] ), &( tmp_rc4_key->
rc4_state [3] ), !tmp_rc4_key->
first ) ;
1590 to_return = !to_return ;
1593 tmp_rc4_key->
first = 1 ;
1594 tmp_rc4_key = NULL ;
1600 static int engine_des_init_key (
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
const unsigned char *iv,
int enc )
1607 memcpy ( &( tmp_des_key->
des_key [ 0 ] ), key, 8 ) ;
1608 memcpy ( &( tmp_des_key->
des_key [ 8 ] ), key, 8 ) ;
1609 memcpy ( &( tmp_des_key->
des_key [ 16 ] ), key, 8 ) ;
1610 memcpy ( &( tmp_des_key->
des_iv [ 0 ] ), iv, 8 ) ;
1618 static int engine_des_cbc_cipher (
EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
unsigned int inl )
1622 zen_nb_t deskey_1, deskey_2, deskey_3, iv ;
1627 input.
data = (
unsigned char *) in ;
1645 memcpy ( ctx->
iv, &( input.
data [ input.
len - 8 ] ), 8 ) ;
1649 to_return = ptr_zencod_xdes_cipher ( &output, &input,
1651 to_return = !to_return ;
1656 memcpy ( ctx->
iv, &( output.
data [ output.
len - 8 ] ), 8 ) ;
1663 static int engine_des_ede3_init_key (
EVP_CIPHER_CTX *ctx,
const unsigned char *key,
const unsigned char *iv,
int enc )
1670 memcpy ( &( tmp_3des_key->
des3_key [ 0 ] ), key, 24 ) ;
1671 memcpy ( &( tmp_3des_key->
des3_iv [ 0 ] ), iv, 8 ) ;
1679 static int engine_des_ede3_cbc_cipher (
EVP_CIPHER_CTX *ctx,
unsigned char *out,
const unsigned char *in,
1680 unsigned int in_len )
1684 zen_nb_t deskey_1, deskey_2, deskey_3, iv ;
1688 input.
len = in_len ;
1689 input.
data = (
unsigned char *) in ;
1690 output.
len = in_len ;
1707 memcpy ( ctx->
iv, &( input.
data [ input.
len - 8 ] ), 8 ) ;
1711 to_return = ptr_zencod_xdes_cipher ( &output, &input,
1713 to_return = !to_return ;
1716 memcpy ( ctx->
iv, &( output.
data [ output.
len - 8 ] ), 8 ) ;