15
|
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 #include "mycrypt.h" |
|
12 |
|
13 /* PKCS #1 v1.5 Signature Padding -- Tom St Denis */ |
|
14 |
|
15 #ifdef PKCS_1 |
|
16 |
|
17 int pkcs_1_v15_sa_decode(const unsigned char *msghash, unsigned long msghashlen, |
|
18 const unsigned char *sig, unsigned long siglen, |
|
19 int hash_idx, unsigned long modulus_bitlen, |
|
20 int *res) |
|
21 { |
|
22 unsigned long x, y, modulus_bytelen, derlen; |
|
23 int err; |
|
24 |
|
25 _ARGCHK(msghash != NULL); |
|
26 _ARGCHK(sig != NULL); |
|
27 _ARGCHK(res != NULL); |
|
28 |
|
29 /* default to invalid */ |
|
30 *res = 0; |
|
31 |
|
32 /* valid hash ? */ |
|
33 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
|
34 return err; |
|
35 } |
|
36 |
|
37 /* get derlen */ |
|
38 derlen = hash_descriptor[hash_idx].DERlen; |
|
39 |
|
40 /* get modulus len */ |
|
41 modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0); |
|
42 |
|
43 /* valid sizes? */ |
|
44 if ((msghashlen + 3 + derlen > modulus_bytelen) || (siglen != modulus_bytelen)) { |
|
45 return CRYPT_PK_INVALID_SIZE; |
|
46 } |
|
47 |
|
48 /* packet is 0x00 0x01 PS 0x00 T, where PS == 0xFF repeated modulus_bytelen - 3 - derlen - msghashlen times, T == DER || hash */ |
|
49 x = 0; |
|
50 if (sig[x++] != 0x00 || sig[x++] != 0x01) { |
|
51 return CRYPT_OK; |
|
52 } |
|
53 |
|
54 /* now follows (modulus_bytelen - 3 - derlen - msghashlen) 0xFF bytes */ |
|
55 for (y = 0; y < (modulus_bytelen - 3 - derlen - msghashlen); y++) { |
|
56 if (sig[x++] != 0xFF) { |
|
57 return CRYPT_OK; |
|
58 } |
|
59 } |
|
60 |
|
61 if (sig[x++] != 0x00) { |
|
62 return CRYPT_OK; |
|
63 } |
|
64 |
|
65 for (y = 0; y < derlen; y++) { |
|
66 if (sig[x++] != hash_descriptor[hash_idx].DER[y]) { |
|
67 return CRYPT_OK; |
|
68 } |
|
69 } |
|
70 |
|
71 if (memcmp(msghash, sig+x, msghashlen) == 0) { |
|
72 *res = 1; |
|
73 } |
|
74 return CRYPT_OK; |
|
75 } |
|
76 |
|
77 #endif |