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 inner.h */
28*0957b409SSimon J. Gerraty uint32_t
br_rsa_pkcs1_sig_pad(const unsigned char * hash_oid,const unsigned char * hash,size_t hash_len,uint32_t n_bitlen,unsigned char * x)29*0957b409SSimon J. Gerraty br_rsa_pkcs1_sig_pad(const unsigned char *hash_oid,
30*0957b409SSimon J. Gerraty const unsigned char *hash, size_t hash_len,
31*0957b409SSimon J. Gerraty uint32_t n_bitlen, unsigned char *x)
32*0957b409SSimon J. Gerraty {
33*0957b409SSimon J. Gerraty size_t u, x3, xlen;
34*0957b409SSimon J. Gerraty
35*0957b409SSimon J. Gerraty /*
36*0957b409SSimon J. Gerraty * Padded hash value has format:
37*0957b409SSimon J. Gerraty * 00 01 FF .. FF 00 30 x1 30 x2 06 x3 OID 05 00 04 x4 HASH
38*0957b409SSimon J. Gerraty *
39*0957b409SSimon J. Gerraty * with the following rules:
40*0957b409SSimon J. Gerraty *
41*0957b409SSimon J. Gerraty * -- Total length is equal to the modulus length (unsigned
42*0957b409SSimon J. Gerraty * encoding).
43*0957b409SSimon J. Gerraty *
44*0957b409SSimon J. Gerraty * -- There must be at least eight bytes of value 0xFF.
45*0957b409SSimon J. Gerraty *
46*0957b409SSimon J. Gerraty * -- x4 is equal to the hash length (hash_len).
47*0957b409SSimon J. Gerraty *
48*0957b409SSimon J. Gerraty * -- x3 is equal to the encoded OID value length (hash_oid[0]).
49*0957b409SSimon J. Gerraty *
50*0957b409SSimon J. Gerraty * -- x2 = x3 + 4.
51*0957b409SSimon J. Gerraty *
52*0957b409SSimon J. Gerraty * -- x1 = x2 + x4 + 4 = x3 + x4 + 8.
53*0957b409SSimon J. Gerraty *
54*0957b409SSimon J. Gerraty * Note: the "05 00" is optional (signatures with and without
55*0957b409SSimon J. Gerraty * that sequence exist in practice), but notes in PKCS#1 seem to
56*0957b409SSimon J. Gerraty * indicate that the presence of that sequence (specifically,
57*0957b409SSimon J. Gerraty * an ASN.1 NULL value for the hash parameters) may be slightly
58*0957b409SSimon J. Gerraty * more "standard" than the opposite.
59*0957b409SSimon J. Gerraty */
60*0957b409SSimon J. Gerraty xlen = (n_bitlen + 7) >> 3;
61*0957b409SSimon J. Gerraty
62*0957b409SSimon J. Gerraty if (hash_oid == NULL) {
63*0957b409SSimon J. Gerraty if (xlen < hash_len + 11) {
64*0957b409SSimon J. Gerraty return 0;
65*0957b409SSimon J. Gerraty }
66*0957b409SSimon J. Gerraty x[0] = 0x00;
67*0957b409SSimon J. Gerraty x[1] = 0x01;
68*0957b409SSimon J. Gerraty u = xlen - hash_len;
69*0957b409SSimon J. Gerraty memset(x + 2, 0xFF, u - 3);
70*0957b409SSimon J. Gerraty x[u - 1] = 0x00;
71*0957b409SSimon J. Gerraty } else {
72*0957b409SSimon J. Gerraty x3 = hash_oid[0];
73*0957b409SSimon J. Gerraty
74*0957b409SSimon J. Gerraty /*
75*0957b409SSimon J. Gerraty * Check that there is enough room for all the elements,
76*0957b409SSimon J. Gerraty * including at least eight bytes of value 0xFF.
77*0957b409SSimon J. Gerraty */
78*0957b409SSimon J. Gerraty if (xlen < (x3 + hash_len + 21)) {
79*0957b409SSimon J. Gerraty return 0;
80*0957b409SSimon J. Gerraty }
81*0957b409SSimon J. Gerraty x[0] = 0x00;
82*0957b409SSimon J. Gerraty x[1] = 0x01;
83*0957b409SSimon J. Gerraty u = xlen - x3 - hash_len - 11;
84*0957b409SSimon J. Gerraty memset(x + 2, 0xFF, u - 2);
85*0957b409SSimon J. Gerraty x[u] = 0x00;
86*0957b409SSimon J. Gerraty x[u + 1] = 0x30;
87*0957b409SSimon J. Gerraty x[u + 2] = x3 + hash_len + 8;
88*0957b409SSimon J. Gerraty x[u + 3] = 0x30;
89*0957b409SSimon J. Gerraty x[u + 4] = x3 + 4;
90*0957b409SSimon J. Gerraty x[u + 5] = 0x06;
91*0957b409SSimon J. Gerraty memcpy(x + u + 6, hash_oid, x3 + 1);
92*0957b409SSimon J. Gerraty u += x3 + 7;
93*0957b409SSimon J. Gerraty x[u ++] = 0x05;
94*0957b409SSimon J. Gerraty x[u ++] = 0x00;
95*0957b409SSimon J. Gerraty x[u ++] = 0x04;
96*0957b409SSimon J. Gerraty x[u ++] = hash_len;
97*0957b409SSimon J. Gerraty }
98*0957b409SSimon J. Gerraty memcpy(x + u, hash, hash_len);
99*0957b409SSimon J. Gerraty return 1;
100*0957b409SSimon J. Gerraty }
101