72 #ifndef OPENSSL_NO_RSA
81 #if (defined(__x86_64) || defined(__x86_64__) || \
82 defined(_M_AMD64) || defined (_M_X64)) && !defined(OPENSSL_NO_ASM)
84 static ENGINE *ENGINE_rsax (
void);
91 ENGINE *toadd = ENGINE_rsax();
100 #define E_RSAX_LIB_NAME "rsax engine"
102 static int e_rsax_destroy(
ENGINE *
e);
103 static int e_rsax_init(
ENGINE *
e);
104 static int e_rsax_finish(
ENGINE *
e);
105 static int e_rsax_ctrl(
ENGINE *
e,
int cmd,
long i,
void *
p,
void (*
f)(
void));
107 #ifndef OPENSSL_NO_RSA
110 static int e_rsax_rsa_finish(
RSA *r);
117 #ifndef OPENSSL_NO_RSA
121 "Intel RSA-X method",
138 static const char *engine_e_rsax_id =
"rsax";
139 static const char *engine_e_rsax_name =
"RSAX engine support";
142 static int bind_helper(
ENGINE *e)
144 #ifndef OPENSSL_NO_RSA
149 #ifndef OPENSSL_NO_RSA
159 #ifndef OPENSSL_NO_RSA
170 static ENGINE *ENGINE_rsax(
void)
175 if(!bind_helper(ret))
183 #ifndef OPENSSL_NO_RSA
185 static int rsax_ex_data_idx = -1;
188 static int e_rsax_destroy(
ENGINE *e)
194 static int e_rsax_init(
ENGINE *e)
196 #ifndef OPENSSL_NO_RSA
197 if (rsax_ex_data_idx == -1)
202 if (rsax_ex_data_idx == -1)
207 static int e_rsax_finish(
ENGINE *e)
212 static int e_rsax_ctrl(
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f)(
void))
228 #ifndef OPENSSL_NO_RSA
231 typedef unsigned __int64 UINT64;
233 typedef unsigned long long UINT64;
235 typedef unsigned short UINT16;
245 static int interleaved_array_to_bn_512(
BIGNUM*
b, UINT64 *array);
250 static int bn_extract_to_array_512(
const BIGNUM*
b,
unsigned int n, UINT64 *array);
260 static int mod_exp_pre_compute_data_512(UINT64 *m,
struct mod_ctx_512 *
data);
262 void mod_exp_512(UINT64 *result,
265 struct mod_ctx_512 *
data);
267 typedef struct st_e_rsax_mod_ctx
271 struct mod_ctx_512 b512;
276 static E_RSAX_MOD_CTX *e_rsax_get_ctx(
RSA *rsa,
int idx,
BIGNUM* m)
278 E_RSAX_MOD_CTX *hptr;
280 if (idx < 0 || idx > 2)
286 if (!hptr)
return NULL;
287 hptr[2].type = hptr[1].type= hptr[0].type = 0;
296 bn_extract_to_array_512(m, 8, _m);
297 memset( &hptr[idx].ctx.b512, 0,
sizeof(
struct mod_ctx_512));
298 mod_exp_pre_compute_data_512(_m, &hptr[idx].ctx.b512);
305 static int e_rsax_rsa_finish(
RSA *rsa)
333 bn_extract_to_array_512(g, 8, _g);
334 bn_extract_to_array_512(e, 8, _e);
336 mod_exp_512(_r, _g, _e, &rsax_mod_ctx->ctx.b512);
338 interleaved_array_to_bn_512(r, _r);
356 static int bn_extract_to_array_512(
const BIGNUM*
b,
unsigned int n, UINT64 *array)
360 unsigned char bn_buff[64];
361 memset(bn_buff, 0, 64);
363 printf (
"Can't support this byte size\n");
367 printf (
"Error's in bn2bin\n");
372 for (i=7; i>=0; i--) {
373 tmp = bn_buff[63-(n*8+i)];
374 array[n] |= tmp << (8*i); } }
379 static int interleaved_array_to_bn_512(
BIGNUM* b, UINT64 *array)
381 unsigned char tmp[64];
385 for (i = 7; i>=0; i--) {
386 tmp[63-(n*8+i)] = (
unsigned char)(array[n]>>(8*i)); } }
393 static int mod_exp_pre_compute_data_512(UINT64 *m,
struct mod_ctx_512 *
data)
395 BIGNUM two_768, two_640, two_128, two_512, tmp, _m, tmp2;
405 interleaved_array_to_bn_512(&_m, m);
429 if (0 == (m[7] & 0x8000000000000000)) {
432 if (0 == (m[0] & 0x1)) {
437 BN_mod(&tmp, &two_768, &_m, ctx);
438 if (!bn_extract_to_array_512(&tmp, 8, &data->m1[0])) {
442 BN_mod(&tmp, &two_640, &_m, ctx);
443 if (!bn_extract_to_array_512(&tmp, 8, &data->m2[0])) {
453 if (!bn_extract_to_array_512(&tmp, 2, &data->k1[0])) {
457 for (i=0; i<8; i++) {
459 if (i & 1) {
BN_add(&tmp, &two_512, &tmp); }
460 if (i & 2) {
BN_add(&tmp, &two_512, &tmp); }
461 if (i & 4) {
BN_add(&tmp, &two_640, &tmp); }
464 if (!bn_extract_to_array_512(&tmp2, 8, _t)) {
466 for (j=0; j<8; j++) data->t[j][i] = _t[j]; }
469 for (i=0; i<8; i++) {
493 BIGNUM local_dmp1,local_dmq1,local_c,local_r1;
554 if (!
BN_mod(r1,c,rsa->
q,ctx))
goto err;
558 if (!
BN_mod(r1,I,rsa->
q,ctx))
goto err;
570 if (!e_rsax_bn_mod_exp(m1,r1,dmq1,rsa->
q,ctx,
578 if (!
BN_mod(r1,c,rsa->
p,ctx))
goto err;
582 if (!
BN_mod(r1,I,rsa->
p,ctx))
goto err;
594 if (!e_rsax_bn_mod_exp(r0,r1,dmp1,rsa->
p,ctx,
597 if (!
BN_sub(r0,r0,m1))
goto err;
601 if (!
BN_add(r0,r0,rsa->
p))
goto err;
613 if (!
BN_mod(r0,pr1,rsa->
p,ctx))
goto err;
623 if (!
BN_add(r0,r0,rsa->
p))
goto err;
624 if (!
BN_mul(r1,r0,rsa->
q,ctx))
goto err;
625 if (!
BN_add(r0,r1,m1))
goto err;
627 if (rsa->
e && rsa->
n)
629 if (!e_rsax_bn_mod_exp(vrfy,r0,rsa->
e,rsa->
n,ctx,rsa->
_method_mod_n, e_rsax_get_ctx(rsa, 2, rsa->
n) ))
636 if (!
BN_sub(vrfy, vrfy, I))
goto err;
637 if (!
BN_mod(vrfy, vrfy, rsa->
n, ctx))
goto err;
639 if (!
BN_add(vrfy, vrfy, rsa->
n))
goto err;
656 if (!e_rsax_bn_mod_exp(r0,I,d,rsa->
n,ctx,