xref: /freebsd/contrib/bearssl/src/rsa/rsa_pkcs1_sig_unpad.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2017 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 bearssl_rsa.h */
28*0957b409SSimon J. Gerraty uint32_t
br_rsa_pkcs1_sig_unpad(const unsigned char * sig,size_t sig_len,const unsigned char * hash_oid,size_t hash_len,unsigned char * hash_out)29*0957b409SSimon J. Gerraty br_rsa_pkcs1_sig_unpad(const unsigned char *sig, size_t sig_len,
30*0957b409SSimon J. Gerraty 	const unsigned char *hash_oid, size_t hash_len,
31*0957b409SSimon J. Gerraty 	unsigned char *hash_out)
32*0957b409SSimon J. Gerraty {
33*0957b409SSimon J. Gerraty 	static const unsigned char pad1[] = {
34*0957b409SSimon J. Gerraty 		0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
35*0957b409SSimon J. Gerraty 	};
36*0957b409SSimon J. Gerraty 
37*0957b409SSimon J. Gerraty 	unsigned char pad2[43];
38*0957b409SSimon J. Gerraty 	size_t u, x2, x3, pad_len, zlen;
39*0957b409SSimon J. Gerraty 
40*0957b409SSimon J. Gerraty 	if (sig_len < 11) {
41*0957b409SSimon J. Gerraty 		return 0;
42*0957b409SSimon J. Gerraty 	}
43*0957b409SSimon J. Gerraty 
44*0957b409SSimon J. Gerraty 	/*
45*0957b409SSimon J. Gerraty 	 * Expected format:
46*0957b409SSimon J. Gerraty 	 *  00 01 FF ... FF 00 30 x1 30 x2 06 x3 OID [ 05 00 ] 04 x4 HASH
47*0957b409SSimon J. Gerraty 	 *
48*0957b409SSimon J. Gerraty 	 * with the following rules:
49*0957b409SSimon J. Gerraty 	 *
50*0957b409SSimon J. Gerraty 	 *  -- Total length is that of the modulus and the signature
51*0957b409SSimon J. Gerraty 	 *     (this was already verified by br_rsa_i31_public()).
52*0957b409SSimon J. Gerraty 	 *
53*0957b409SSimon J. Gerraty 	 *  -- There are at least eight bytes of value 0xFF.
54*0957b409SSimon J. Gerraty 	 *
55*0957b409SSimon J. Gerraty 	 *  -- x4 is equal to the hash length (hash_len).
56*0957b409SSimon J. Gerraty 	 *
57*0957b409SSimon J. Gerraty 	 *  -- x3 is equal to the encoded OID value length (so x3 is the
58*0957b409SSimon J. Gerraty 	 *     first byte of hash_oid[]).
59*0957b409SSimon J. Gerraty 	 *
60*0957b409SSimon J. Gerraty 	 *  -- If the "05 00" is present, then x2 == x3 + 4; otherwise,
61*0957b409SSimon J. Gerraty 	 *     x2 == x3 + 2.
62*0957b409SSimon J. Gerraty 	 *
63*0957b409SSimon J. Gerraty 	 *  -- x1 == x2 + x4 + 4.
64*0957b409SSimon J. Gerraty 	 *
65*0957b409SSimon J. Gerraty 	 * So the total length after the last "FF" is either x3 + x4 + 11
66*0957b409SSimon J. Gerraty 	 * (with the "05 00") or x3 + x4 + 9 (without the "05 00").
67*0957b409SSimon J. Gerraty 	 */
68*0957b409SSimon J. Gerraty 
69*0957b409SSimon J. Gerraty 	/*
70*0957b409SSimon J. Gerraty 	 * Check the "00 01 FF .. FF 00" with at least eight 0xFF bytes.
71*0957b409SSimon J. Gerraty 	 * The comparison is valid because we made sure that the signature
72*0957b409SSimon J. Gerraty 	 * is at least 11 bytes long.
73*0957b409SSimon J. Gerraty 	 */
74*0957b409SSimon J. Gerraty 	if (memcmp(sig, pad1, sizeof pad1) != 0) {
75*0957b409SSimon J. Gerraty 		return 0;
76*0957b409SSimon J. Gerraty 	}
77*0957b409SSimon J. Gerraty 	for (u = sizeof pad1; u < sig_len; u ++) {
78*0957b409SSimon J. Gerraty 		if (sig[u] != 0xFF) {
79*0957b409SSimon J. Gerraty 			break;
80*0957b409SSimon J. Gerraty 		}
81*0957b409SSimon J. Gerraty 	}
82*0957b409SSimon J. Gerraty 
83*0957b409SSimon J. Gerraty 	/*
84*0957b409SSimon J. Gerraty 	 * Remaining length is sig_len - u bytes (including the 00 just
85*0957b409SSimon J. Gerraty 	 * after the last FF). This must be equal to one of the two
86*0957b409SSimon J. Gerraty 	 * possible values (depending on whether the "05 00" sequence is
87*0957b409SSimon J. Gerraty 	 * present or not).
88*0957b409SSimon J. Gerraty 	 */
89*0957b409SSimon J. Gerraty 	if (hash_oid == NULL) {
90*0957b409SSimon J. Gerraty 		if (sig_len - u != hash_len + 1 || sig[u] != 0x00) {
91*0957b409SSimon J. Gerraty 			return 0;
92*0957b409SSimon J. Gerraty 		}
93*0957b409SSimon J. Gerraty 	} else {
94*0957b409SSimon J. Gerraty 		x3 = hash_oid[0];
95*0957b409SSimon J. Gerraty 		pad_len = x3 + 9;
96*0957b409SSimon J. Gerraty 		memset(pad2, 0, pad_len);
97*0957b409SSimon J. Gerraty 		zlen = sig_len - u - hash_len;
98*0957b409SSimon J. Gerraty 		if (zlen == pad_len) {
99*0957b409SSimon J. Gerraty 			x2 = x3 + 2;
100*0957b409SSimon J. Gerraty 		} else if (zlen == pad_len + 2) {
101*0957b409SSimon J. Gerraty 			x2 = x3 + 4;
102*0957b409SSimon J. Gerraty 			pad_len = zlen;
103*0957b409SSimon J. Gerraty 			pad2[pad_len - 4] = 0x05;
104*0957b409SSimon J. Gerraty 		} else {
105*0957b409SSimon J. Gerraty 			return 0;
106*0957b409SSimon J. Gerraty 		}
107*0957b409SSimon J. Gerraty 		pad2[1] = 0x30;
108*0957b409SSimon J. Gerraty 		pad2[2] = x2 + hash_len + 4;
109*0957b409SSimon J. Gerraty 		pad2[3] = 0x30;
110*0957b409SSimon J. Gerraty 		pad2[4] = x2;
111*0957b409SSimon J. Gerraty 		pad2[5] = 0x06;
112*0957b409SSimon J. Gerraty 		memcpy(pad2 + 6, hash_oid, x3 + 1);
113*0957b409SSimon J. Gerraty 		pad2[pad_len - 2] = 0x04;
114*0957b409SSimon J. Gerraty 		pad2[pad_len - 1] = hash_len;
115*0957b409SSimon J. Gerraty 		if (memcmp(pad2, sig + u, pad_len) != 0) {
116*0957b409SSimon J. Gerraty 			return 0;
117*0957b409SSimon J. Gerraty 		}
118*0957b409SSimon J. Gerraty 	}
119*0957b409SSimon J. Gerraty 	memcpy(hash_out, sig + sig_len - hash_len, hash_len);
120*0957b409SSimon J. Gerraty 	return 1;
121*0957b409SSimon J. Gerraty }
122