2 * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 br_rsa_pss_sig_unpad(const br_hash_class
*hf_data
,
30 const br_hash_class
*hf_mgf1
,
31 const unsigned char *hash
, size_t salt_len
,
32 const br_rsa_public_key
*pk
, unsigned char *x
)
34 size_t u
, xlen
, hash_len
;
35 br_hash_compat_context hc
;
36 unsigned char *seed
, *salt
;
37 unsigned char tmp
[64];
40 hash_len
= br_digest_size(hf_data
);
43 * Value r will be set to a non-zero value is any test fails.
48 * The value bit length (as an integer) must be strictly less than
49 * that of the modulus.
51 for (u
= 0; u
< pk
->nlen
; u
++) {
59 n_bitlen
= BIT_LENGTH(pk
->n
[u
]) + ((uint32_t)(pk
->nlen
- u
- 1) << 3);
61 if ((n_bitlen
& 7) == 0) {
64 r
|= x
[0] & (0xFF << (n_bitlen
& 7));
66 xlen
= (n_bitlen
+ 7) >> 3;
69 * Check that the modulus is large enough for the hash value
70 * length combined with the intended salt length.
72 if (hash_len
> xlen
|| salt_len
> xlen
73 || (hash_len
+ salt_len
+ 2) > xlen
)
79 * Check value of rightmost byte.
81 r
|= x
[xlen
- 1] ^ 0xBC;
84 * Generate the mask and XOR it into the first bytes to reveal PS;
85 * we must also mask out the leading bits.
87 seed
= x
+ xlen
- hash_len
- 1;
88 br_mgf1_xor(x
, xlen
- hash_len
- 1, hf_mgf1
, seed
, hash_len
);
89 if ((n_bitlen
& 7) != 0) {
90 x
[0] &= 0xFF >> (8 - (n_bitlen
& 7));
94 * Check that all padding bytes have the expected value.
96 for (u
= 0; u
< (xlen
- hash_len
- salt_len
- 2); u
++) {
99 r
|= x
[xlen
- hash_len
- salt_len
- 2] ^ 0x01;
104 salt
= x
+ xlen
- hash_len
- salt_len
- 1;
105 hf_data
->init(&hc
.vtable
);
107 hf_data
->update(&hc
.vtable
, tmp
, 8);
108 hf_data
->update(&hc
.vtable
, hash
, hash_len
);
109 hf_data
->update(&hc
.vtable
, salt
, salt_len
);
110 hf_data
->out(&hc
.vtable
, tmp
);
113 * Check that the recomputed H value matches the one appearing
116 for (u
= 0; u
< hash_len
; u
++) {
117 r
|= tmp
[u
] ^ x
[(xlen
- hash_len
- 1) + u
];