xref: /freebsd/contrib/bearssl/src/rsa/rsa_pss_sig_pad.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty  *
4*0957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty  * the following conditions:
11*0957b409SSimon J. Gerraty  *
12*0957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty  *
15*0957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty  * SOFTWARE.
23*0957b409SSimon J. Gerraty  */
24*0957b409SSimon J. Gerraty 
25*0957b409SSimon J. Gerraty #include "inner.h"
26*0957b409SSimon J. Gerraty 
27*0957b409SSimon J. Gerraty /* see inner.h */
28*0957b409SSimon J. Gerraty uint32_t
br_rsa_pss_sig_pad(const br_prng_class ** rng,const br_hash_class * hf_data,const br_hash_class * hf_mgf1,const unsigned char * hash,size_t salt_len,uint32_t n_bitlen,unsigned char * x)29*0957b409SSimon J. Gerraty br_rsa_pss_sig_pad(const br_prng_class **rng,
30*0957b409SSimon J. Gerraty 	const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
31*0957b409SSimon J. Gerraty 	const unsigned char *hash, size_t salt_len,
32*0957b409SSimon J. Gerraty 	uint32_t n_bitlen, unsigned char *x)
33*0957b409SSimon J. Gerraty {
34*0957b409SSimon J. Gerraty 	size_t xlen, hash_len;
35*0957b409SSimon J. Gerraty 	br_hash_compat_context hc;
36*0957b409SSimon J. Gerraty 	unsigned char *salt, *seed;
37*0957b409SSimon J. Gerraty 
38*0957b409SSimon J. Gerraty 	hash_len = br_digest_size(hf_data);
39*0957b409SSimon J. Gerraty 
40*0957b409SSimon J. Gerraty 	/*
41*0957b409SSimon J. Gerraty 	 * The padded string is one bit smaller than the modulus;
42*0957b409SSimon J. Gerraty 	 * notably, if the modulus length is equal to 1 modulo 8, then
43*0957b409SSimon J. Gerraty 	 * the padded string will be one _byte_ smaller, and the first
44*0957b409SSimon J. Gerraty 	 * byte will be set to 0. We apply these transformations here.
45*0957b409SSimon J. Gerraty 	 */
46*0957b409SSimon J. Gerraty 	n_bitlen --;
47*0957b409SSimon J. Gerraty 	if ((n_bitlen & 7) == 0) {
48*0957b409SSimon J. Gerraty 		*x ++ = 0;
49*0957b409SSimon J. Gerraty 	}
50*0957b409SSimon J. Gerraty 	xlen = (n_bitlen + 7) >> 3;
51*0957b409SSimon J. Gerraty 
52*0957b409SSimon J. Gerraty 	/*
53*0957b409SSimon J. Gerraty 	 * Check that the modulus is large enough for the hash value
54*0957b409SSimon J. Gerraty 	 * length combined with the intended salt length.
55*0957b409SSimon J. Gerraty 	 */
56*0957b409SSimon J. Gerraty 	if (hash_len > xlen || salt_len > xlen
57*0957b409SSimon J. Gerraty 		|| (hash_len + salt_len + 2) > xlen)
58*0957b409SSimon J. Gerraty 	{
59*0957b409SSimon J. Gerraty 		return 0;
60*0957b409SSimon J. Gerraty 	}
61*0957b409SSimon J. Gerraty 
62*0957b409SSimon J. Gerraty 	/*
63*0957b409SSimon J. Gerraty 	 * Produce a random salt.
64*0957b409SSimon J. Gerraty 	 */
65*0957b409SSimon J. Gerraty 	salt = x + xlen - hash_len - salt_len - 1;
66*0957b409SSimon J. Gerraty 	if (salt_len != 0) {
67*0957b409SSimon J. Gerraty 		(*rng)->generate(rng, salt, salt_len);
68*0957b409SSimon J. Gerraty 	}
69*0957b409SSimon J. Gerraty 
70*0957b409SSimon J. Gerraty 	/*
71*0957b409SSimon J. Gerraty 	 * Compute the seed for MGF1.
72*0957b409SSimon J. Gerraty 	 */
73*0957b409SSimon J. Gerraty 	seed = x + xlen - hash_len - 1;
74*0957b409SSimon J. Gerraty 	hf_data->init(&hc.vtable);
75*0957b409SSimon J. Gerraty 	memset(seed, 0, 8);
76*0957b409SSimon J. Gerraty 	hf_data->update(&hc.vtable, seed, 8);
77*0957b409SSimon J. Gerraty 	hf_data->update(&hc.vtable, hash, hash_len);
78*0957b409SSimon J. Gerraty 	hf_data->update(&hc.vtable, salt, salt_len);
79*0957b409SSimon J. Gerraty 	hf_data->out(&hc.vtable, seed);
80*0957b409SSimon J. Gerraty 
81*0957b409SSimon J. Gerraty 	/*
82*0957b409SSimon J. Gerraty 	 * Prepare string PS (padded salt). The salt is already at the
83*0957b409SSimon J. Gerraty 	 * right place.
84*0957b409SSimon J. Gerraty 	 */
85*0957b409SSimon J. Gerraty 	memset(x, 0, xlen - salt_len - hash_len - 2);
86*0957b409SSimon J. Gerraty 	x[xlen - salt_len - hash_len - 2] = 0x01;
87*0957b409SSimon J. Gerraty 
88*0957b409SSimon J. Gerraty 	/*
89*0957b409SSimon J. Gerraty 	 * Generate the mask and XOR it into PS.
90*0957b409SSimon J. Gerraty 	 */
91*0957b409SSimon J. Gerraty 	br_mgf1_xor(x, xlen - hash_len - 1, hf_mgf1, seed, hash_len);
92*0957b409SSimon J. Gerraty 
93*0957b409SSimon J. Gerraty 	/*
94*0957b409SSimon J. Gerraty 	 * Clear the top bits to ensure the value is lower than the
95*0957b409SSimon J. Gerraty 	 * modulus.
96*0957b409SSimon J. Gerraty 	 */
97*0957b409SSimon J. Gerraty 	x[0] &= 0xFF >> (((uint32_t)xlen << 3) - n_bitlen);
98*0957b409SSimon J. Gerraty 
99*0957b409SSimon J. Gerraty 	/*
100*0957b409SSimon J. Gerraty 	 * The seed (H) is already in the right place. We just set the
101*0957b409SSimon J. Gerraty 	 * last byte.
102*0957b409SSimon J. Gerraty 	 */
103*0957b409SSimon J. Gerraty 	x[xlen - 1] = 0xBC;
104*0957b409SSimon J. Gerraty 
105*0957b409SSimon J. Gerraty 	return 1;
106*0957b409SSimon J. Gerraty }
107