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. This is put as a separate function so that stack
29*0957b409SSimon J. Gerraty * allocation of the hash function context is done only for the duration
30*0957b409SSimon J. Gerraty * of the hash.
31*0957b409SSimon J. Gerraty */
32*0957b409SSimon J. Gerraty static void
hash_data(const br_hash_class * dig,void * dst,const void * src,size_t len)33*0957b409SSimon J. Gerraty 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
37*0957b409SSimon J. Gerraty hc.vtable = dig;
38*0957b409SSimon J. Gerraty dig->init(&hc.vtable);
39*0957b409SSimon J. Gerraty dig->update(&hc.vtable, src, len);
40*0957b409SSimon J. Gerraty dig->out(&hc.vtable, dst);
41*0957b409SSimon J. Gerraty }
42*0957b409SSimon J. Gerraty
43*0957b409SSimon J. Gerraty /* see inner.h */
44*0957b409SSimon J. Gerraty size_t
br_rsa_oaep_pad(const br_prng_class ** rnd,const br_hash_class * dig,const void * label,size_t label_len,const br_rsa_public_key * pk,void * dst,size_t dst_max_len,const void * src,size_t src_len)45*0957b409SSimon J. Gerraty br_rsa_oaep_pad(const br_prng_class **rnd, const br_hash_class *dig,
46*0957b409SSimon J. Gerraty const void *label, size_t label_len,
47*0957b409SSimon J. Gerraty const br_rsa_public_key *pk,
48*0957b409SSimon J. Gerraty void *dst, size_t dst_max_len,
49*0957b409SSimon J. Gerraty const void *src, size_t src_len)
50*0957b409SSimon J. Gerraty {
51*0957b409SSimon J. Gerraty size_t k, hlen;
52*0957b409SSimon J. Gerraty unsigned char *buf;
53*0957b409SSimon J. Gerraty
54*0957b409SSimon J. Gerraty hlen = br_digest_size(dig);
55*0957b409SSimon J. Gerraty
56*0957b409SSimon J. Gerraty /*
57*0957b409SSimon J. Gerraty * Compute actual modulus length (in bytes).
58*0957b409SSimon J. Gerraty */
59*0957b409SSimon J. Gerraty k = pk->nlen;
60*0957b409SSimon J. Gerraty while (k > 0 && pk->n[k - 1] == 0) {
61*0957b409SSimon J. Gerraty k --;
62*0957b409SSimon J. Gerraty }
63*0957b409SSimon J. Gerraty
64*0957b409SSimon J. Gerraty /*
65*0957b409SSimon J. Gerraty * An error is reported if:
66*0957b409SSimon J. Gerraty * - the modulus is too short;
67*0957b409SSimon J. Gerraty * - the source message length is too long;
68*0957b409SSimon J. Gerraty * - the destination buffer is too short.
69*0957b409SSimon J. Gerraty */
70*0957b409SSimon J. Gerraty if (k < ((hlen << 1) + 2)
71*0957b409SSimon J. Gerraty || src_len > (k - (hlen << 1) - 2)
72*0957b409SSimon J. Gerraty || dst_max_len < k)
73*0957b409SSimon J. Gerraty {
74*0957b409SSimon J. Gerraty return 0;
75*0957b409SSimon J. Gerraty }
76*0957b409SSimon J. Gerraty
77*0957b409SSimon J. Gerraty /*
78*0957b409SSimon J. Gerraty * Apply padding. At this point, things cannot fail.
79*0957b409SSimon J. Gerraty */
80*0957b409SSimon J. Gerraty buf = dst;
81*0957b409SSimon J. Gerraty
82*0957b409SSimon J. Gerraty /*
83*0957b409SSimon J. Gerraty * Assemble: DB = lHash || PS || 0x01 || M
84*0957b409SSimon J. Gerraty * We first place the source message M with memmove(), so that
85*0957b409SSimon J. Gerraty * overlaps between source and destination buffers are supported.
86*0957b409SSimon J. Gerraty */
87*0957b409SSimon J. Gerraty memmove(buf + k - src_len, src, src_len);
88*0957b409SSimon J. Gerraty hash_data(dig, buf + 1 + hlen, label, label_len);
89*0957b409SSimon J. Gerraty memset(buf + 1 + (hlen << 1), 0, k - src_len - (hlen << 1) - 2);
90*0957b409SSimon J. Gerraty buf[k - src_len - 1] = 0x01;
91*0957b409SSimon J. Gerraty
92*0957b409SSimon J. Gerraty /*
93*0957b409SSimon J. Gerraty * Make the random seed.
94*0957b409SSimon J. Gerraty */
95*0957b409SSimon J. Gerraty (*rnd)->generate(rnd, buf + 1, hlen);
96*0957b409SSimon J. Gerraty
97*0957b409SSimon J. Gerraty /*
98*0957b409SSimon J. Gerraty * Mask DB with the mask generated from the seed.
99*0957b409SSimon J. Gerraty */
100*0957b409SSimon J. Gerraty br_mgf1_xor(buf + 1 + hlen, k - hlen - 1, dig, buf + 1, hlen);
101*0957b409SSimon J. Gerraty
102*0957b409SSimon J. Gerraty /*
103*0957b409SSimon J. Gerraty * Mask the seed with the mask generated from the masked DB.
104*0957b409SSimon J. Gerraty */
105*0957b409SSimon J. Gerraty br_mgf1_xor(buf + 1, hlen, dig, buf + 1 + hlen, k - hlen - 1);
106*0957b409SSimon J. Gerraty
107*0957b409SSimon J. Gerraty /*
108*0957b409SSimon J. Gerraty * Padding result: EM = 0x00 || maskedSeed || maskedDB.
109*0957b409SSimon J. Gerraty */
110*0957b409SSimon J. Gerraty buf[0] = 0x00;
111*0957b409SSimon J. Gerraty return k;
112*0957b409SSimon J. Gerraty }
113