]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/pkcs1/pkcs_1_pss_decode.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / pkcs1 / pkcs_1_pss_decode.c
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 #include "tomcrypt.h"
10
11 /**
12   @file pkcs_1_pss_decode.c
13   PKCS #1 PSS Signature Padding, Tom St Denis
14 */
15
16 #ifdef LTC_PKCS_1
17
18 /**
19    PKCS #1 v2.00 PSS decode
20    @param  msghash         The hash to verify
21    @param  msghashlen      The length of the hash (octets)
22    @param  sig             The signature data (encoded data)
23    @param  siglen          The length of the signature data (octets)
24    @param  saltlen         The length of the salt used (octets)
25    @param  hash_idx        The index of the hash desired
26    @param  modulus_bitlen  The bit length of the RSA modulus
27    @param  res             [out] The result of the comparison, 1==valid, 0==invalid
28    @return CRYPT_OK if successful (even if the comparison failed)
29 */
30 int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen,
31                       const unsigned char *sig,     unsigned long siglen,
32                             unsigned long saltlen,  int           hash_idx,
33                             unsigned long modulus_bitlen, int    *res)
34 {
35    unsigned char *DB, *mask, *salt, *hash;
36    unsigned long x, y, hLen, modulus_len;
37    int           err;
38    hash_state    md;
39
40    LTC_ARGCHK(msghash != NULL);
41    LTC_ARGCHK(res     != NULL);
42
43    /* default to invalid */
44    *res = 0;
45
46    /* ensure hash is valid */
47    if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
48       return err;
49    }
50
51    hLen        = hash_descriptor[hash_idx].hashsize;
52    modulus_bitlen--;
53    modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0);
54
55    /* check sizes */
56    if ((saltlen > modulus_len) ||
57        (modulus_len < hLen + saltlen + 2)) {
58       return CRYPT_PK_INVALID_SIZE;
59    }
60
61    /* allocate ram for DB/mask/salt/hash of size modulus_len */
62    DB   = XMALLOC(modulus_len);
63    mask = XMALLOC(modulus_len);
64    salt = XMALLOC(modulus_len);
65    hash = XMALLOC(modulus_len);
66    if (DB == NULL || mask == NULL || salt == NULL || hash == NULL) {
67       if (DB != NULL) {
68          XFREE(DB);
69       }
70       if (mask != NULL) {
71          XFREE(mask);
72       }
73       if (salt != NULL) {
74          XFREE(salt);
75       }
76       if (hash != NULL) {
77          XFREE(hash);
78       }
79       return CRYPT_MEM;
80    }
81
82    /* ensure the 0xBC byte */
83    if (sig[siglen-1] != 0xBC) {
84       err = CRYPT_INVALID_PACKET;
85       goto LBL_ERR;
86    }
87
88    /* copy out the DB */
89    x = 0;
90    XMEMCPY(DB, sig + x, modulus_len - hLen - 1);
91    x += modulus_len - hLen - 1;
92
93    /* copy out the hash */
94    XMEMCPY(hash, sig + x, hLen);
95    /* x += hLen; */
96
97    /* check the MSB */
98    if ((sig[0] & ~(0xFF >> ((modulus_len<<3) - (modulus_bitlen)))) != 0) {
99       err = CRYPT_INVALID_PACKET;
100       goto LBL_ERR;
101    }
102
103    /* generate mask of length modulus_len - hLen - 1 from hash */
104    if ((err = pkcs_1_mgf1(hash_idx, hash, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
105       goto LBL_ERR;
106    }
107
108    /* xor against DB */
109    for (y = 0; y < (modulus_len - hLen - 1); y++) {
110       DB[y] ^= mask[y];
111    }
112
113    /* now clear the first byte [make sure smaller than modulus] */
114    DB[0] &= 0xFF >> ((modulus_len<<3) - (modulus_bitlen));
115
116    /* DB = PS || 0x01 || salt, PS == modulus_len - saltlen - hLen - 2 zero bytes */
117
118    /* check for zeroes and 0x01 */
119    for (x = 0; x < modulus_len - saltlen - hLen - 2; x++) {
120        if (DB[x] != 0x00) {
121           err = CRYPT_INVALID_PACKET;
122           goto LBL_ERR;
123        }
124    }
125
126    /* check for the 0x01 */
127    if (DB[x++] != 0x01) {
128       err = CRYPT_INVALID_PACKET;
129       goto LBL_ERR;
130    }
131
132    /* M = (eight) 0x00 || msghash || salt, mask = H(M) */
133    if ((err = hash_descriptor[hash_idx].init(&md)) != CRYPT_OK) {
134       goto LBL_ERR;
135    }
136    zeromem(mask, 8);
137    if ((err = hash_descriptor[hash_idx].process(&md, mask, 8)) != CRYPT_OK) {
138       goto LBL_ERR;
139    }
140    if ((err = hash_descriptor[hash_idx].process(&md, msghash, msghashlen)) != CRYPT_OK) {
141       goto LBL_ERR;
142    }
143    if ((err = hash_descriptor[hash_idx].process(&md, DB+x, saltlen)) != CRYPT_OK) {
144       goto LBL_ERR;
145    }
146    if ((err = hash_descriptor[hash_idx].done(&md, mask)) != CRYPT_OK) {
147       goto LBL_ERR;
148    }
149
150    /* mask == hash means valid signature */
151    if (XMEM_NEQ(mask, hash, hLen) == 0) {
152       *res = 1;
153    }
154
155    err = CRYPT_OK;
156 LBL_ERR:
157 #ifdef LTC_CLEAN_STACK
158    zeromem(DB,   modulus_len);
159    zeromem(mask, modulus_len);
160    zeromem(salt, modulus_len);
161    zeromem(hash, modulus_len);
162 #endif
163
164    XFREE(hash);
165    XFREE(salt);
166    XFREE(mask);
167    XFREE(DB);
168
169    return err;
170 }
171
172 #endif /* LTC_PKCS_1 */
173
174 /* ref:         $Format:%D$ */
175 /* git commit:  $Format:%H$ */
176 /* commit time: $Format:%ai$ */