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_pad(const br_prng_class **rng,
30 const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
31 const unsigned char *hash, size_t salt_len,
32 uint32_t n_bitlen, unsigned char *x)
34 size_t xlen, hash_len;
35 br_hash_compat_context hc;
36 unsigned char *salt, *seed;
38 hash_len = br_digest_size(hf_data);
41 * The padded string is one bit smaller than the modulus;
42 * notably, if the modulus length is equal to 1 modulo 8, then
43 * the padded string will be one _byte_ smaller, and the first
44 * byte will be set to 0. We apply these transformations here.
47 if ((n_bitlen & 7) == 0) {
50 xlen = (n_bitlen + 7) >> 3;
53 * Check that the modulus is large enough for the hash value
54 * length combined with the intended salt length.
56 if (hash_len > xlen || salt_len > xlen
57 || (hash_len + salt_len + 2) > xlen)
63 * Produce a random salt.
65 salt = x + xlen - hash_len - salt_len - 1;
67 (*rng)->generate(rng, salt, salt_len);
71 * Compute the seed for MGF1.
73 seed = x + xlen - hash_len - 1;
74 hf_data->init(&hc.vtable);
76 hf_data->update(&hc.vtable, seed, 8);
77 hf_data->update(&hc.vtable, hash, hash_len);
78 hf_data->update(&hc.vtable, salt, salt_len);
79 hf_data->out(&hc.vtable, seed);
82 * Prepare string PS (padded salt). The salt is already at the
85 memset(x, 0, xlen - salt_len - hash_len - 2);
86 x[xlen - salt_len - hash_len - 2] = 0x01;
89 * Generate the mask and XOR it into PS.
91 br_mgf1_xor(x, xlen - hash_len - 1, hf_mgf1, seed, hash_len);
94 * Clear the top bits to ensure the value is lower than the
97 x[0] &= 0xFF >> (((uint32_t)xlen << 3) - n_bitlen);
100 * The seed (H) is already in the right place. We just set the