comparison rsa_v15_verify_hash.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents
children
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11
12 #include "mycrypt.h"
13
14 #ifdef MRSA
15
16 /* de-sign then PKCS v1.5 depad */
17 int rsa_v15_verify_hash(const unsigned char *sig, unsigned long siglen,
18 const unsigned char *msghash, unsigned long msghashlen,
19 prng_state *prng, int prng_idx,
20 int hash_idx, int *stat,
21 rsa_key *key)
22 {
23 unsigned long modulus_bitlen, modulus_bytelen, x;
24 int err;
25 unsigned char *tmpbuf;
26
27 _ARGCHK(msghash != NULL);
28 _ARGCHK(sig != NULL);
29 _ARGCHK(stat != NULL);
30 _ARGCHK(key != NULL);
31
32 /* default to invalid */
33 *stat = 0;
34
35 /* valid hash ? */
36 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
37 return err;
38 }
39
40 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
41 return err;
42 }
43
44 /* get modulus len in bits */
45 modulus_bitlen = mp_count_bits(&(key->N));
46
47 /* outlen must be at least the size of the modulus */
48 modulus_bytelen = mp_unsigned_bin_size(&(key->N));
49 if (modulus_bytelen != siglen) {
50 return CRYPT_INVALID_PACKET;
51 }
52
53 /* allocate temp buffer for decoded sig */
54 tmpbuf = XMALLOC(siglen);
55 if (tmpbuf == NULL) {
56 return CRYPT_MEM;
57 }
58
59 /* RSA decode it */
60 x = siglen;
61 if ((err = rsa_exptmod(sig, siglen, tmpbuf, &x, PK_PUBLIC, prng, prng_idx, key)) != CRYPT_OK) {
62 XFREE(tmpbuf);
63 return err;
64 }
65
66 /* PSS decode it */
67 err = pkcs_1_v15_sa_decode(msghash, msghashlen, tmpbuf, x, hash_idx, modulus_bitlen, stat);
68 XFREE(tmpbuf);
69 return err;
70 }
71
72 #endif