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 /*
28*0957b409SSimon J. Gerraty * Hash some data and XOR the result into the provided buffer. This is put
29*0957b409SSimon J. Gerraty * as a separate function so that stack allocation of the hash function
30*0957b409SSimon J. Gerraty * context is done only for the duration of the hash.
31*0957b409SSimon J. Gerraty */
32*0957b409SSimon J. Gerraty static void
xor_hash_data(const br_hash_class * dig,void * dst,const void * src,size_t len)33*0957b409SSimon J. Gerraty xor_hash_data(const br_hash_class *dig, void *dst, const void *src, size_t len)
34*0957b409SSimon J. Gerraty {
35*0957b409SSimon J. Gerraty br_hash_compat_context hc;
36*0957b409SSimon J. Gerraty unsigned char tmp[64];
37*0957b409SSimon J. Gerraty unsigned char *buf;
38*0957b409SSimon J. Gerraty size_t u, hlen;
39*0957b409SSimon J. Gerraty
40*0957b409SSimon J. Gerraty hc.vtable = dig;
41*0957b409SSimon J. Gerraty dig->init(&hc.vtable);
42*0957b409SSimon J. Gerraty dig->update(&hc.vtable, src, len);
43*0957b409SSimon J. Gerraty dig->out(&hc.vtable, tmp);
44*0957b409SSimon J. Gerraty buf = dst;
45*0957b409SSimon J. Gerraty hlen = br_digest_size(dig);
46*0957b409SSimon J. Gerraty for (u = 0; u < hlen; u ++) {
47*0957b409SSimon J. Gerraty buf[u] ^= tmp[u];
48*0957b409SSimon J. Gerraty }
49*0957b409SSimon J. Gerraty }
50*0957b409SSimon J. Gerraty
51*0957b409SSimon J. Gerraty /* see inner.h */
52*0957b409SSimon J. Gerraty uint32_t
br_rsa_oaep_unpad(const br_hash_class * dig,const void * label,size_t label_len,void * data,size_t * len)53*0957b409SSimon J. Gerraty br_rsa_oaep_unpad(const br_hash_class *dig,
54*0957b409SSimon J. Gerraty const void *label, size_t label_len,
55*0957b409SSimon J. Gerraty void *data, size_t *len)
56*0957b409SSimon J. Gerraty {
57*0957b409SSimon J. Gerraty size_t u, k, hlen;
58*0957b409SSimon J. Gerraty unsigned char *buf;
59*0957b409SSimon J. Gerraty uint32_t r, s, zlen;
60*0957b409SSimon J. Gerraty
61*0957b409SSimon J. Gerraty hlen = br_digest_size(dig);
62*0957b409SSimon J. Gerraty k = *len;
63*0957b409SSimon J. Gerraty buf = data;
64*0957b409SSimon J. Gerraty
65*0957b409SSimon J. Gerraty /*
66*0957b409SSimon J. Gerraty * There must be room for the padding.
67*0957b409SSimon J. Gerraty */
68*0957b409SSimon J. Gerraty if (k < ((hlen << 1) + 2)) {
69*0957b409SSimon J. Gerraty return 0;
70*0957b409SSimon J. Gerraty }
71*0957b409SSimon J. Gerraty
72*0957b409SSimon J. Gerraty /*
73*0957b409SSimon J. Gerraty * Unmask the seed, then the DB value.
74*0957b409SSimon J. Gerraty */
75*0957b409SSimon J. Gerraty br_mgf1_xor(buf + 1, hlen, dig, buf + 1 + hlen, k - hlen - 1);
76*0957b409SSimon J. Gerraty br_mgf1_xor(buf + 1 + hlen, k - hlen - 1, dig, buf + 1, hlen);
77*0957b409SSimon J. Gerraty
78*0957b409SSimon J. Gerraty /*
79*0957b409SSimon J. Gerraty * Hash the label and XOR it with the value in the array; if
80*0957b409SSimon J. Gerraty * they are equal then these should yield only zeros.
81*0957b409SSimon J. Gerraty */
82*0957b409SSimon J. Gerraty xor_hash_data(dig, buf + 1 + hlen, label, label_len);
83*0957b409SSimon J. Gerraty
84*0957b409SSimon J. Gerraty /*
85*0957b409SSimon J. Gerraty * At that point, if the padding was correct, when we should
86*0957b409SSimon J. Gerraty * have: 0x00 || seed || 0x00 ... 0x00 0x01 || M
87*0957b409SSimon J. Gerraty * Padding is valid as long as:
88*0957b409SSimon J. Gerraty * - There is at least hlen+1 leading bytes of value 0x00.
89*0957b409SSimon J. Gerraty * - There is at least one non-zero byte.
90*0957b409SSimon J. Gerraty * - The first (leftmost) non-zero byte has value 0x01.
91*0957b409SSimon J. Gerraty *
92*0957b409SSimon J. Gerraty * Ultimately, we may leak the resulting message length, i.e.
93*0957b409SSimon J. Gerraty * the position of the byte of value 0x01, but we must take care
94*0957b409SSimon J. Gerraty * to do so only if the number of zero bytes has been verified
95*0957b409SSimon J. Gerraty * to be at least hlen+1.
96*0957b409SSimon J. Gerraty *
97*0957b409SSimon J. Gerraty * The loop below counts the number of bytes of value 0x00, and
98*0957b409SSimon J. Gerraty * checks that the next byte has value 0x01, in constant-time.
99*0957b409SSimon J. Gerraty *
100*0957b409SSimon J. Gerraty * - If the initial byte (before the seed) is not 0x00, then
101*0957b409SSimon J. Gerraty * r and s are set to 0, and stay there.
102*0957b409SSimon J. Gerraty * - Value r is 1 until the first non-zero byte is reached
103*0957b409SSimon J. Gerraty * (after the seed); it switches to 0 at that point.
104*0957b409SSimon J. Gerraty * - Value s is set to 1 if and only if the data encountered
105*0957b409SSimon J. Gerraty * at the time of the transition of r from 1 to 0 has value
106*0957b409SSimon J. Gerraty * exactly 0x01.
107*0957b409SSimon J. Gerraty * - Value zlen counts the number of leading bytes of value zero
108*0957b409SSimon J. Gerraty * (after the seed).
109*0957b409SSimon J. Gerraty */
110*0957b409SSimon J. Gerraty r = 1 - ((buf[0] + 0xFF) >> 8);
111*0957b409SSimon J. Gerraty s = 0;
112*0957b409SSimon J. Gerraty zlen = 0;
113*0957b409SSimon J. Gerraty for (u = hlen + 1; u < k; u ++) {
114*0957b409SSimon J. Gerraty uint32_t w, nz;
115*0957b409SSimon J. Gerraty
116*0957b409SSimon J. Gerraty w = buf[u];
117*0957b409SSimon J. Gerraty
118*0957b409SSimon J. Gerraty /*
119*0957b409SSimon J. Gerraty * nz == 1 only for the first non-zero byte.
120*0957b409SSimon J. Gerraty */
121*0957b409SSimon J. Gerraty nz = r & ((w + 0xFF) >> 8);
122*0957b409SSimon J. Gerraty s |= nz & EQ(w, 0x01);
123*0957b409SSimon J. Gerraty r &= NOT(nz);
124*0957b409SSimon J. Gerraty zlen += r;
125*0957b409SSimon J. Gerraty }
126*0957b409SSimon J. Gerraty
127*0957b409SSimon J. Gerraty /*
128*0957b409SSimon J. Gerraty * Padding is correct only if s == 1, _and_ zlen >= hlen.
129*0957b409SSimon J. Gerraty */
130*0957b409SSimon J. Gerraty s &= GE(zlen, (uint32_t)hlen);
131*0957b409SSimon J. Gerraty
132*0957b409SSimon J. Gerraty /*
133*0957b409SSimon J. Gerraty * At that point, padding was verified, and we are now allowed
134*0957b409SSimon J. Gerraty * to make conditional jumps.
135*0957b409SSimon J. Gerraty */
136*0957b409SSimon J. Gerraty if (s) {
137*0957b409SSimon J. Gerraty size_t plen;
138*0957b409SSimon J. Gerraty
139*0957b409SSimon J. Gerraty plen = 2 + hlen + zlen;
140*0957b409SSimon J. Gerraty k -= plen;
141*0957b409SSimon J. Gerraty memmove(buf, buf + plen, k);
142*0957b409SSimon J. Gerraty *len = k;
143*0957b409SSimon J. Gerraty }
144*0957b409SSimon J. Gerraty return s;
145*0957b409SSimon J. Gerraty }
146