comparison src/pk/pkcs1/pkcs_1_v15_sa_decode.c @ 192:9cc34777b479 libtomcrypt

propagate from branch 'au.asn.ucc.matt.ltc-orig' (head 9ba8f01f44320e9cb9f19881105ae84f84a43ea9) to branch 'au.asn.ucc.matt.dropbear.ltc' (head dbf51c569bc34956ad948e4cc87a0eeb2170b768)
author Matt Johnston <matt@ucc.asn.au>
date Sun, 08 May 2005 06:36:47 +0000
parents 1c15b283127b
children
comparison
equal deleted inserted replaced
164:cd1143579f00 192:9cc34777b479
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 "tomcrypt.h"
12
13 /**
14 @file pkcs_1_v15_sa_decode.c
15 PKCS #1 v1.5 Signature Padding, Tom St Denis
16 */
17
18 #ifdef PKCS_1
19
20 /**
21 Perform PKCS #1 v1.5 Signature Decoding
22 @param msghash The hash that was signed
23 @param msghashlen The length of the hash
24 @param sig The signature [padded data]
25 @param siglen The length of the signature
26 @param hash_idx The index of the hash used
27 @param modulus_bitlen The bit length of the RSA modulus
28 @param res [out] Result of comparison, 1==valid, 0==invalid
29 @return CRYPT_OK if successful
30 */
31 int pkcs_1_v15_sa_decode(const unsigned char *msghash, unsigned long msghashlen,
32 const unsigned char *sig, unsigned long siglen,
33 int hash_idx, unsigned long modulus_bitlen,
34 int *res)
35 {
36 unsigned long x, y, modulus_bytelen, derlen;
37 int err;
38
39 LTC_ARGCHK(msghash != NULL);
40 LTC_ARGCHK(sig != NULL);
41 LTC_ARGCHK(res != NULL);
42
43 /* default to invalid */
44 *res = 0;
45
46 /* valid hash ? */
47 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
48 return err;
49 }
50
51 /* get derlen */
52 derlen = hash_descriptor[hash_idx].DERlen;
53
54 /* get modulus len */
55 modulus_bytelen = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
56
57 /* valid sizes? */
58 if ((msghashlen + 3 + derlen > modulus_bytelen) || (siglen != modulus_bytelen)) {
59 return CRYPT_PK_INVALID_SIZE;
60 }
61
62 /* packet is 0x00 0x01 PS 0x00 T, where PS == 0xFF repeated modulus_bytelen - 3 - derlen - msghashlen times, T == DER || hash */
63 x = 0;
64 if (sig[x++] != 0x00 || sig[x++] != 0x01) {
65 return CRYPT_OK;
66 }
67
68 /* now follows (modulus_bytelen - 3 - derlen - msghashlen) 0xFF bytes */
69 for (y = 0; y < (modulus_bytelen - 3 - derlen - msghashlen); y++) {
70 if (sig[x++] != 0xFF) {
71 return CRYPT_OK;
72 }
73 }
74
75 if (sig[x++] != 0x00) {
76 return CRYPT_OK;
77 }
78
79 for (y = 0; y < derlen; y++) {
80 if (sig[x++] != hash_descriptor[hash_idx].DER[y]) {
81 return CRYPT_OK;
82 }
83 }
84
85 if (memcmp(msghash, sig+x, msghashlen) == 0) {
86 *res = 1;
87 }
88 return CRYPT_OK;
89 }
90
91 #endif