diff options
author | djm <> | 2006-06-27 05:07:03 +0000 |
---|---|---|
committer | djm <> | 2006-06-27 05:07:03 +0000 |
commit | 7fe7e1ed6bcd0e342aed7c0f890962dda616aa0d (patch) | |
tree | 224c33f66b0b932c84dda315d9ba4236bf125b1c /src/lib/libcrypto/engine/hw_cswift.c | |
parent | 3f764f48d2626a43b6eeef7652c28303269d1204 (diff) | |
download | openbsd-7fe7e1ed6bcd0e342aed7c0f890962dda616aa0d.tar.gz openbsd-7fe7e1ed6bcd0e342aed7c0f890962dda616aa0d.tar.bz2 openbsd-7fe7e1ed6bcd0e342aed7c0f890962dda616aa0d.zip |
resolve conflicts
Diffstat (limited to 'src/lib/libcrypto/engine/hw_cswift.c')
-rw-r--r-- | src/lib/libcrypto/engine/hw_cswift.c | 204 |
1 files changed, 158 insertions, 46 deletions
diff --git a/src/lib/libcrypto/engine/hw_cswift.c b/src/lib/libcrypto/engine/hw_cswift.c index f128ee5a68..1411fd8333 100644 --- a/src/lib/libcrypto/engine/hw_cswift.c +++ b/src/lib/libcrypto/engine/hw_cswift.c | |||
@@ -90,6 +90,7 @@ static int cswift_destroy(ENGINE *e); | |||
90 | static int cswift_init(ENGINE *e); | 90 | static int cswift_init(ENGINE *e); |
91 | static int cswift_finish(ENGINE *e); | 91 | static int cswift_finish(ENGINE *e); |
92 | static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()); | 92 | static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)()); |
93 | static int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in); | ||
93 | 94 | ||
94 | /* BIGNUM stuff */ | 95 | /* BIGNUM stuff */ |
95 | static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 96 | static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
@@ -403,7 +404,10 @@ static int cswift_init(ENGINE *e) | |||
403 | return 1; | 404 | return 1; |
404 | err: | 405 | err: |
405 | if(cswift_dso) | 406 | if(cswift_dso) |
407 | { | ||
406 | DSO_free(cswift_dso); | 408 | DSO_free(cswift_dso); |
409 | cswift_dso = NULL; | ||
410 | } | ||
407 | p_CSwift_AcquireAccContext = NULL; | 411 | p_CSwift_AcquireAccContext = NULL; |
408 | p_CSwift_AttachKeyParam = NULL; | 412 | p_CSwift_AttachKeyParam = NULL; |
409 | p_CSwift_SimpleRequest = NULL; | 413 | p_CSwift_SimpleRequest = NULL; |
@@ -553,6 +557,29 @@ err: | |||
553 | return to_return; | 557 | return to_return; |
554 | } | 558 | } |
555 | 559 | ||
560 | |||
561 | int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in) | ||
562 | { | ||
563 | int mod; | ||
564 | int numbytes = BN_num_bytes(in); | ||
565 | |||
566 | mod = 0; | ||
567 | while( ((out->nbytes = (numbytes+mod)) % 32) ) | ||
568 | { | ||
569 | mod++; | ||
570 | } | ||
571 | out->value = (unsigned char*)OPENSSL_malloc(out->nbytes); | ||
572 | if(!out->value) | ||
573 | { | ||
574 | return 0; | ||
575 | } | ||
576 | BN_bn2bin(in, &out->value[mod]); | ||
577 | if(mod) | ||
578 | memset(out->value, 0, mod); | ||
579 | |||
580 | return 1; | ||
581 | } | ||
582 | |||
556 | /* Un petit mod_exp chinois */ | 583 | /* Un petit mod_exp chinois */ |
557 | static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 584 | static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
558 | const BIGNUM *q, const BIGNUM *dmp1, | 585 | const BIGNUM *q, const BIGNUM *dmp1, |
@@ -562,15 +589,16 @@ static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |||
562 | SW_LARGENUMBER arg, res; | 589 | SW_LARGENUMBER arg, res; |
563 | SW_PARAM sw_param; | 590 | SW_PARAM sw_param; |
564 | SW_CONTEXT_HANDLE hac; | 591 | SW_CONTEXT_HANDLE hac; |
565 | BIGNUM *rsa_p = NULL; | ||
566 | BIGNUM *rsa_q = NULL; | ||
567 | BIGNUM *rsa_dmp1 = NULL; | ||
568 | BIGNUM *rsa_dmq1 = NULL; | ||
569 | BIGNUM *rsa_iqmp = NULL; | ||
570 | BIGNUM *argument = NULL; | ||
571 | BIGNUM *result = NULL; | 592 | BIGNUM *result = NULL; |
593 | BIGNUM *argument = NULL; | ||
572 | int to_return = 0; /* expect failure */ | 594 | int to_return = 0; /* expect failure */ |
573 | int acquired = 0; | 595 | int acquired = 0; |
596 | |||
597 | sw_param.up.crt.p.value = NULL; | ||
598 | sw_param.up.crt.q.value = NULL; | ||
599 | sw_param.up.crt.dmp1.value = NULL; | ||
600 | sw_param.up.crt.dmq1.value = NULL; | ||
601 | sw_param.up.crt.iqmp.value = NULL; | ||
574 | 602 | ||
575 | if(!get_context(&hac)) | 603 | if(!get_context(&hac)) |
576 | { | 604 | { |
@@ -578,44 +606,55 @@ static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |||
578 | goto err; | 606 | goto err; |
579 | } | 607 | } |
580 | acquired = 1; | 608 | acquired = 1; |
609 | |||
581 | /* Prepare the params */ | 610 | /* Prepare the params */ |
582 | BN_CTX_start(ctx); | 611 | argument = BN_new(); |
583 | rsa_p = BN_CTX_get(ctx); | 612 | result = BN_new(); |
584 | rsa_q = BN_CTX_get(ctx); | 613 | if(!result || !argument) |
585 | rsa_dmp1 = BN_CTX_get(ctx); | ||
586 | rsa_dmq1 = BN_CTX_get(ctx); | ||
587 | rsa_iqmp = BN_CTX_get(ctx); | ||
588 | argument = BN_CTX_get(ctx); | ||
589 | result = BN_CTX_get(ctx); | ||
590 | if(!result) | ||
591 | { | 614 | { |
592 | CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL); | 615 | CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL); |
593 | goto err; | 616 | goto err; |
594 | } | 617 | } |
595 | if(!bn_wexpand(rsa_p, p->top) || !bn_wexpand(rsa_q, q->top) || | 618 | |
596 | !bn_wexpand(rsa_dmp1, dmp1->top) || | 619 | |
597 | !bn_wexpand(rsa_dmq1, dmq1->top) || | 620 | sw_param.type = SW_ALG_CRT; |
598 | !bn_wexpand(rsa_iqmp, iqmp->top) || | 621 | /************************************************************************/ |
599 | !bn_wexpand(argument, a->top) || | 622 | /* 04/02/2003 */ |
623 | /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */ | ||
624 | /* limitation of cswift with values not a multiple of 32 */ | ||
625 | /************************************************************************/ | ||
626 | if(!cswift_bn_32copy(&sw_param.up.crt.p, p)) | ||
627 | { | ||
628 | CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); | ||
629 | goto err; | ||
630 | } | ||
631 | if(!cswift_bn_32copy(&sw_param.up.crt.q, q)) | ||
632 | { | ||
633 | CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); | ||
634 | goto err; | ||
635 | } | ||
636 | if(!cswift_bn_32copy(&sw_param.up.crt.dmp1, dmp1)) | ||
637 | { | ||
638 | CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); | ||
639 | goto err; | ||
640 | } | ||
641 | if(!cswift_bn_32copy(&sw_param.up.crt.dmq1, dmq1)) | ||
642 | { | ||
643 | CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); | ||
644 | goto err; | ||
645 | } | ||
646 | if(!cswift_bn_32copy(&sw_param.up.crt.iqmp, iqmp)) | ||
647 | { | ||
648 | CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); | ||
649 | goto err; | ||
650 | } | ||
651 | if( !bn_wexpand(argument, a->top) || | ||
600 | !bn_wexpand(result, p->top + q->top)) | 652 | !bn_wexpand(result, p->top + q->top)) |
601 | { | 653 | { |
602 | CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); | 654 | CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL); |
603 | goto err; | 655 | goto err; |
604 | } | 656 | } |
605 | sw_param.type = SW_ALG_CRT; | 657 | |
606 | sw_param.up.crt.p.nbytes = BN_bn2bin(p, (unsigned char *)rsa_p->d); | ||
607 | sw_param.up.crt.p.value = (unsigned char *)rsa_p->d; | ||
608 | sw_param.up.crt.q.nbytes = BN_bn2bin(q, (unsigned char *)rsa_q->d); | ||
609 | sw_param.up.crt.q.value = (unsigned char *)rsa_q->d; | ||
610 | sw_param.up.crt.dmp1.nbytes = BN_bn2bin(dmp1, | ||
611 | (unsigned char *)rsa_dmp1->d); | ||
612 | sw_param.up.crt.dmp1.value = (unsigned char *)rsa_dmp1->d; | ||
613 | sw_param.up.crt.dmq1.nbytes = BN_bn2bin(dmq1, | ||
614 | (unsigned char *)rsa_dmq1->d); | ||
615 | sw_param.up.crt.dmq1.value = (unsigned char *)rsa_dmq1->d; | ||
616 | sw_param.up.crt.iqmp.nbytes = BN_bn2bin(iqmp, | ||
617 | (unsigned char *)rsa_iqmp->d); | ||
618 | sw_param.up.crt.iqmp.value = (unsigned char *)rsa_iqmp->d; | ||
619 | /* Attach the key params */ | 658 | /* Attach the key params */ |
620 | sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); | 659 | sw_status = p_CSwift_AttachKeyParam(hac, &sw_param); |
621 | switch(sw_status) | 660 | switch(sw_status) |
@@ -654,9 +693,22 @@ static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | |||
654 | BN_bin2bn((unsigned char *)result->d, res.nbytes, r); | 693 | BN_bin2bn((unsigned char *)result->d, res.nbytes, r); |
655 | to_return = 1; | 694 | to_return = 1; |
656 | err: | 695 | err: |
696 | if(sw_param.up.crt.p.value) | ||
697 | OPENSSL_free(sw_param.up.crt.p.value); | ||
698 | if(sw_param.up.crt.q.value) | ||
699 | OPENSSL_free(sw_param.up.crt.q.value); | ||
700 | if(sw_param.up.crt.dmp1.value) | ||
701 | OPENSSL_free(sw_param.up.crt.dmp1.value); | ||
702 | if(sw_param.up.crt.dmq1.value) | ||
703 | OPENSSL_free(sw_param.up.crt.dmq1.value); | ||
704 | if(sw_param.up.crt.iqmp.value) | ||
705 | OPENSSL_free(sw_param.up.crt.iqmp.value); | ||
706 | if(result) | ||
707 | BN_free(result); | ||
708 | if(argument) | ||
709 | BN_free(argument); | ||
657 | if(acquired) | 710 | if(acquired) |
658 | release_context(hac); | 711 | release_context(hac); |
659 | BN_CTX_end(ctx); | ||
660 | return to_return; | 712 | return to_return; |
661 | } | 713 | } |
662 | 714 | ||
@@ -665,6 +717,27 @@ static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa) | |||
665 | { | 717 | { |
666 | BN_CTX *ctx; | 718 | BN_CTX *ctx; |
667 | int to_return = 0; | 719 | int to_return = 0; |
720 | const RSA_METHOD * def_rsa_method; | ||
721 | |||
722 | /* Try the limits of RSA (2048 bits) */ | ||
723 | if(BN_num_bytes(rsa->p) > 128 || | ||
724 | BN_num_bytes(rsa->q) > 128 || | ||
725 | BN_num_bytes(rsa->dmp1) > 128 || | ||
726 | BN_num_bytes(rsa->dmq1) > 128 || | ||
727 | BN_num_bytes(rsa->iqmp) > 128) | ||
728 | { | ||
729 | #ifdef RSA_NULL | ||
730 | def_rsa_method=RSA_null_method(); | ||
731 | #else | ||
732 | #if 0 | ||
733 | def_rsa_method=RSA_PKCS1_RSAref(); | ||
734 | #else | ||
735 | def_rsa_method=RSA_PKCS1_SSLeay(); | ||
736 | #endif | ||
737 | #endif | ||
738 | if(def_rsa_method) | ||
739 | return def_rsa_method->rsa_mod_exp(r0, I, rsa); | ||
740 | } | ||
668 | 741 | ||
669 | if((ctx = BN_CTX_new()) == NULL) | 742 | if((ctx = BN_CTX_new()) == NULL) |
670 | goto err; | 743 | goto err; |
@@ -686,6 +759,26 @@ err: | |||
686 | static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | 759 | static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, |
687 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) | 760 | const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) |
688 | { | 761 | { |
762 | const RSA_METHOD * def_rsa_method; | ||
763 | |||
764 | /* Try the limits of RSA (2048 bits) */ | ||
765 | if(BN_num_bytes(r) > 256 || | ||
766 | BN_num_bytes(a) > 256 || | ||
767 | BN_num_bytes(m) > 256) | ||
768 | { | ||
769 | #ifdef RSA_NULL | ||
770 | def_rsa_method=RSA_null_method(); | ||
771 | #else | ||
772 | #if 0 | ||
773 | def_rsa_method=RSA_PKCS1_RSAref(); | ||
774 | #else | ||
775 | def_rsa_method=RSA_PKCS1_SSLeay(); | ||
776 | #endif | ||
777 | #endif | ||
778 | if(def_rsa_method) | ||
779 | return def_rsa_method->bn_mod_exp(r, a, p, m, ctx, m_ctx); | ||
780 | } | ||
781 | |||
689 | return cswift_mod_exp(r, a, p, m, ctx); | 782 | return cswift_mod_exp(r, a, p, m, ctx); |
690 | } | 783 | } |
691 | 784 | ||
@@ -930,9 +1023,10 @@ static int cswift_rand_bytes(unsigned char *buf, int num) | |||
930 | SW_CONTEXT_HANDLE hac; | 1023 | SW_CONTEXT_HANDLE hac; |
931 | SW_STATUS swrc; | 1024 | SW_STATUS swrc; |
932 | SW_LARGENUMBER largenum; | 1025 | SW_LARGENUMBER largenum; |
933 | size_t nbytes = 0; | ||
934 | int acquired = 0; | 1026 | int acquired = 0; |
935 | int to_return = 0; /* assume failure */ | 1027 | int to_return = 0; /* assume failure */ |
1028 | unsigned char buf32[1024]; | ||
1029 | |||
936 | 1030 | ||
937 | if (!get_context(&hac)) | 1031 | if (!get_context(&hac)) |
938 | { | 1032 | { |
@@ -941,17 +1035,19 @@ static int cswift_rand_bytes(unsigned char *buf, int num) | |||
941 | } | 1035 | } |
942 | acquired = 1; | 1036 | acquired = 1; |
943 | 1037 | ||
944 | while (nbytes < (size_t)num) | 1038 | /************************************************************************/ |
1039 | /* 04/02/2003 */ | ||
1040 | /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */ | ||
1041 | /* limitation of cswift with values not a multiple of 32 */ | ||
1042 | /************************************************************************/ | ||
1043 | |||
1044 | while(num >= sizeof(buf32)) | ||
945 | { | 1045 | { |
1046 | largenum.value = buf; | ||
1047 | largenum.nbytes = sizeof(buf32); | ||
946 | /* tell CryptoSwift how many bytes we want and where we want it. | 1048 | /* tell CryptoSwift how many bytes we want and where we want it. |
947 | * Note: - CryptoSwift cannot do more than 4096 bytes at a time. | 1049 | * Note: - CryptoSwift cannot do more than 4096 bytes at a time. |
948 | * - CryptoSwift can only do multiple of 32-bits. */ | 1050 | * - CryptoSwift can only do multiple of 32-bits. */ |
949 | largenum.value = (SW_BYTE *) buf + nbytes; | ||
950 | if (4096 > num - nbytes) | ||
951 | largenum.nbytes = num - nbytes; | ||
952 | else | ||
953 | largenum.nbytes = 4096; | ||
954 | |||
955 | swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1); | 1051 | swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1); |
956 | if (swrc != SW_OK) | 1052 | if (swrc != SW_OK) |
957 | { | 1053 | { |
@@ -961,14 +1057,30 @@ static int cswift_rand_bytes(unsigned char *buf, int num) | |||
961 | ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); | 1057 | ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); |
962 | goto err; | 1058 | goto err; |
963 | } | 1059 | } |
964 | 1060 | buf += sizeof(buf32); | |
965 | nbytes += largenum.nbytes; | 1061 | num -= sizeof(buf32); |
1062 | } | ||
1063 | if(num) | ||
1064 | { | ||
1065 | largenum.nbytes = sizeof(buf32); | ||
1066 | largenum.value = buf32; | ||
1067 | swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1); | ||
1068 | if (swrc != SW_OK) | ||
1069 | { | ||
1070 | char tmpbuf[20]; | ||
1071 | CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, CSWIFT_R_REQUEST_FAILED); | ||
1072 | sprintf(tmpbuf, "%ld", swrc); | ||
1073 | ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf); | ||
1074 | goto err; | ||
1075 | } | ||
1076 | memcpy(buf, largenum.value, num); | ||
966 | } | 1077 | } |
967 | to_return = 1; /* success */ | ||
968 | 1078 | ||
1079 | to_return = 1; /* success */ | ||
969 | err: | 1080 | err: |
970 | if (acquired) | 1081 | if (acquired) |
971 | release_context(hac); | 1082 | release_context(hac); |
1083 | |||
972 | return to_return; | 1084 | return to_return; |
973 | } | 1085 | } |
974 | 1086 | ||