diff options
Diffstat (limited to 'src/lib/libcrypto/dsa/dsa_vrf.c')
| -rw-r--r-- | src/lib/libcrypto/dsa/dsa_vrf.c | 109 |
1 files changed, 26 insertions, 83 deletions
diff --git a/src/lib/libcrypto/dsa/dsa_vrf.c b/src/lib/libcrypto/dsa/dsa_vrf.c index 0f860984ed..066c6b5b28 100644 --- a/src/lib/libcrypto/dsa/dsa_vrf.c +++ b/src/lib/libcrypto/dsa/dsa_vrf.c | |||
| @@ -56,97 +56,40 @@ | |||
| 56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
| 57 | */ | 57 | */ |
| 58 | 58 | ||
| 59 | /* Origional version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ | 59 | /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ |
| 60 | 60 | ||
| 61 | #include <stdio.h> | 61 | #include <stdio.h> |
| 62 | #include "cryptlib.h" | 62 | #include "cryptlib.h" |
| 63 | #include "bn.h" | 63 | #include <openssl/bn.h> |
| 64 | #include "dsa.h" | 64 | #include <openssl/dsa.h> |
| 65 | #include "rand.h" | 65 | #include <openssl/rand.h> |
| 66 | #include "asn1.h" | 66 | #include <openssl/asn1.h> |
| 67 | #include "asn1_mac.h" | 67 | #include <openssl/asn1_mac.h> |
| 68 | #include <openssl/engine.h> | ||
| 69 | |||
| 70 | int DSA_do_verify(const unsigned char *dgst, int dgst_len, DSA_SIG *sig, | ||
| 71 | DSA *dsa) | ||
| 72 | { | ||
| 73 | return dsa->meth->dsa_do_verify(dgst, dgst_len, sig, dsa); | ||
| 74 | } | ||
| 68 | 75 | ||
| 69 | /* data has already been hashed (probably with SHA or SHA-1). */ | 76 | /* data has already been hashed (probably with SHA or SHA-1). */ |
| 70 | /* returns | 77 | /* returns |
| 71 | * 1: correct signature | 78 | * 1: correct signature |
| 72 | * 0: incorrect signature | 79 | * 0: incorrect signature |
| 73 | * -1: error | 80 | * -1: error |
| 74 | */ | 81 | */ |
| 75 | int DSA_verify(type,dgst,dgst_len,sigbuf,siglen, dsa) | 82 | int DSA_verify(int type, const unsigned char *dgst, int dgst_len, |
| 76 | int type; | 83 | const unsigned char *sigbuf, int siglen, DSA *dsa) |
| 77 | unsigned char *dgst; | ||
| 78 | int dgst_len; | ||
| 79 | unsigned char *sigbuf; | ||
| 80 | int siglen; | ||
| 81 | DSA *dsa; | ||
| 82 | { | 84 | { |
| 83 | /* The next 3 are used by the M_ASN1 macros */ | 85 | DSA_SIG *s; |
| 84 | long length=siglen; | 86 | int ret=-1; |
| 85 | ASN1_CTX c; | ||
| 86 | unsigned char **pp= &sigbuf; | ||
| 87 | BN_CTX *ctx; | ||
| 88 | BIGNUM *r=NULL; | ||
| 89 | BIGNUM *t1=NULL,*t2=NULL; | ||
| 90 | BIGNUM *u1=NULL,*u2=NULL; | ||
| 91 | ASN1_INTEGER *bs=NULL; | ||
| 92 | int ret = -1; | ||
| 93 | |||
| 94 | ctx=BN_CTX_new(); | ||
| 95 | if (ctx == NULL) goto err; | ||
| 96 | |||
| 97 | t1=BN_new(); | ||
| 98 | t2=BN_new(); | ||
| 99 | if (t1 == NULL || t2 == NULL) goto err; | ||
| 100 | |||
| 101 | M_ASN1_D2I_Init(); | ||
| 102 | M_ASN1_D2I_start_sequence(); | ||
| 103 | M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); | ||
| 104 | if ((r=BN_bin2bn(bs->data,bs->length,NULL)) == NULL) goto err_bn; | ||
| 105 | M_ASN1_D2I_get(bs,d2i_ASN1_INTEGER); | ||
| 106 | if ((u1=BN_bin2bn(bs->data,bs->length,NULL)) == NULL) goto err_bn; | ||
| 107 | if (!asn1_Finish(&c)) goto err; | ||
| 108 | |||
| 109 | /* Calculate W = inv(S) mod Q | ||
| 110 | * save W in u2 */ | ||
| 111 | if ((u2=BN_mod_inverse(u1,dsa->q,ctx)) == NULL) goto err_bn; | ||
| 112 | |||
| 113 | /* save M in u1 */ | ||
| 114 | if (BN_bin2bn(dgst,dgst_len,u1) == NULL) goto err_bn; | ||
| 115 | |||
| 116 | /* u1 = M * w mod q */ | ||
| 117 | if (!BN_mod_mul(u1,u1,u2,dsa->q,ctx)) goto err_bn; | ||
| 118 | |||
| 119 | /* u2 = r * w mod q */ | ||
| 120 | if (!BN_mod_mul(u2,r,u2,dsa->q,ctx)) goto err_bn; | ||
| 121 | 87 | ||
| 122 | /* v = ( g^u1 * y^u2 mod p ) mod q */ | 88 | s = DSA_SIG_new(); |
| 123 | /* let t1 = g ^ u1 mod p */ | 89 | if (s == NULL) return(ret); |
| 124 | if (!BN_mod_exp(t1,dsa->g,u1,dsa->p,ctx)) goto err_bn; | 90 | if (d2i_DSA_SIG(&s,&sigbuf,siglen) == NULL) goto err; |
| 125 | /* let t2 = y ^ u2 mod p */ | 91 | ret=DSA_do_verify(dgst,dgst_len,s,dsa); |
| 126 | if (!BN_mod_exp(t2,dsa->pub_key,u2,dsa->p,ctx)) goto err_bn; | 92 | err: |
| 127 | /* let u1 = t1 * t2 mod p */ | 93 | DSA_SIG_free(s); |
| 128 | if (!BN_mod_mul(u1,t1,t2,dsa->p,ctx)) goto err_bn; | ||
| 129 | /* let u1 = u1 mod q */ | ||
| 130 | if (!BN_mod(u1,u1,dsa->q,ctx)) goto err_bn; | ||
| 131 | /* V is now in u1. If the signature is correct, it will be | ||
| 132 | * equal to R. */ | ||
| 133 | ret=(BN_ucmp(u1, r) == 0); | ||
| 134 | if (0) | ||
| 135 | { | ||
| 136 | err: /* ASN1 error */ | ||
| 137 | DSAerr(DSA_F_DSA_VERIFY,c.error); | ||
| 138 | } | ||
| 139 | if (0) | ||
| 140 | { | ||
| 141 | err_bn: /* BN error */ | ||
| 142 | DSAerr(DSA_F_DSA_VERIFY,ERR_R_BN_LIB); | ||
| 143 | } | ||
| 144 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 145 | if (r != NULL) BN_free(r); | ||
| 146 | if (t1 != NULL) BN_free(t1); | ||
| 147 | if (t2 != NULL) BN_free(t2); | ||
| 148 | if (u1 != NULL) BN_free(u1); | ||
| 149 | if (u2 != NULL) BN_free(u2); | ||
| 150 | if (bs != NULL) ASN1_BIT_STRING_free(bs); | ||
| 151 | return(ret); | 94 | return(ret); |
| 152 | } | 95 | } |
