1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * DRBG: Deterministic Random Bits Generator
3*4882a593Smuzhiyun * Based on NIST Recommended DRBG from NIST SP800-90A with the following
4*4882a593Smuzhiyun * properties:
5*4882a593Smuzhiyun * * CTR DRBG with DF with AES-128, AES-192, AES-256 cores
6*4882a593Smuzhiyun * * Hash DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
7*4882a593Smuzhiyun * * HMAC DRBG with DF with SHA-1, SHA-256, SHA-384, SHA-512 cores
8*4882a593Smuzhiyun * * with and without prediction resistance
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Copyright Stephan Mueller <smueller@chronox.de>, 2014
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
13*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
14*4882a593Smuzhiyun * are met:
15*4882a593Smuzhiyun * 1. Redistributions of source code must retain the above copyright
16*4882a593Smuzhiyun * notice, and the entire permission notice in its entirety,
17*4882a593Smuzhiyun * including the disclaimer of warranties.
18*4882a593Smuzhiyun * 2. Redistributions in binary form must reproduce the above copyright
19*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in the
20*4882a593Smuzhiyun * documentation and/or other materials provided with the distribution.
21*4882a593Smuzhiyun * 3. The name of the author may not be used to endorse or promote
22*4882a593Smuzhiyun * products derived from this software without specific prior
23*4882a593Smuzhiyun * written permission.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * ALTERNATIVELY, this product may be distributed under the terms of
26*4882a593Smuzhiyun * the GNU General Public License, in which case the provisions of the GPL are
27*4882a593Smuzhiyun * required INSTEAD OF the above restrictions. (This clause is
28*4882a593Smuzhiyun * necessary due to a potential bad interaction between the GPL and
29*4882a593Smuzhiyun * the restrictions contained in a BSD-style copyright.)
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
32*4882a593Smuzhiyun * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33*4882a593Smuzhiyun * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
34*4882a593Smuzhiyun * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
35*4882a593Smuzhiyun * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
37*4882a593Smuzhiyun * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
38*4882a593Smuzhiyun * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39*4882a593Smuzhiyun * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
41*4882a593Smuzhiyun * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
42*4882a593Smuzhiyun * DAMAGE.
43*4882a593Smuzhiyun *
44*4882a593Smuzhiyun * DRBG Usage
45*4882a593Smuzhiyun * ==========
46*4882a593Smuzhiyun * The SP 800-90A DRBG allows the user to specify a personalization string
47*4882a593Smuzhiyun * for initialization as well as an additional information string for each
48*4882a593Smuzhiyun * random number request. The following code fragments show how a caller
49*4882a593Smuzhiyun * uses the kernel crypto API to use the full functionality of the DRBG.
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * Usage without any additional data
52*4882a593Smuzhiyun * ---------------------------------
53*4882a593Smuzhiyun * struct crypto_rng *drng;
54*4882a593Smuzhiyun * int err;
55*4882a593Smuzhiyun * char data[DATALEN];
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * drng = crypto_alloc_rng(drng_name, 0, 0);
58*4882a593Smuzhiyun * err = crypto_rng_get_bytes(drng, &data, DATALEN);
59*4882a593Smuzhiyun * crypto_free_rng(drng);
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * Usage with personalization string during initialization
63*4882a593Smuzhiyun * -------------------------------------------------------
64*4882a593Smuzhiyun * struct crypto_rng *drng;
65*4882a593Smuzhiyun * int err;
66*4882a593Smuzhiyun * char data[DATALEN];
67*4882a593Smuzhiyun * struct drbg_string pers;
68*4882a593Smuzhiyun * char personalization[11] = "some-string";
69*4882a593Smuzhiyun *
70*4882a593Smuzhiyun * drbg_string_fill(&pers, personalization, strlen(personalization));
71*4882a593Smuzhiyun * drng = crypto_alloc_rng(drng_name, 0, 0);
72*4882a593Smuzhiyun * // The reset completely re-initializes the DRBG with the provided
73*4882a593Smuzhiyun * // personalization string
74*4882a593Smuzhiyun * err = crypto_rng_reset(drng, &personalization, strlen(personalization));
75*4882a593Smuzhiyun * err = crypto_rng_get_bytes(drng, &data, DATALEN);
76*4882a593Smuzhiyun * crypto_free_rng(drng);
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * Usage with additional information string during random number request
80*4882a593Smuzhiyun * ---------------------------------------------------------------------
81*4882a593Smuzhiyun * struct crypto_rng *drng;
82*4882a593Smuzhiyun * int err;
83*4882a593Smuzhiyun * char data[DATALEN];
84*4882a593Smuzhiyun * char addtl_string[11] = "some-string";
85*4882a593Smuzhiyun * string drbg_string addtl;
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * drbg_string_fill(&addtl, addtl_string, strlen(addtl_string));
88*4882a593Smuzhiyun * drng = crypto_alloc_rng(drng_name, 0, 0);
89*4882a593Smuzhiyun * // The following call is a wrapper to crypto_rng_get_bytes() and returns
90*4882a593Smuzhiyun * // the same error codes.
91*4882a593Smuzhiyun * err = crypto_drbg_get_bytes_addtl(drng, &data, DATALEN, &addtl);
92*4882a593Smuzhiyun * crypto_free_rng(drng);
93*4882a593Smuzhiyun *
94*4882a593Smuzhiyun *
95*4882a593Smuzhiyun * Usage with personalization and additional information strings
96*4882a593Smuzhiyun * -------------------------------------------------------------
97*4882a593Smuzhiyun * Just mix both scenarios above.
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun #include <crypto/drbg.h>
101*4882a593Smuzhiyun #include <crypto/internal/cipher.h>
102*4882a593Smuzhiyun #include <linux/kernel.h>
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /***************************************************************
105*4882a593Smuzhiyun * Backend cipher definitions available to DRBG
106*4882a593Smuzhiyun ***************************************************************/
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * The order of the DRBG definitions here matter: every DRBG is registered
110*4882a593Smuzhiyun * as stdrng. Each DRBG receives an increasing cra_priority values the later
111*4882a593Smuzhiyun * they are defined in this array (see drbg_fill_array).
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * HMAC DRBGs are favored over Hash DRBGs over CTR DRBGs, and
114*4882a593Smuzhiyun * the SHA256 / AES 256 over other ciphers. Thus, the favored
115*4882a593Smuzhiyun * DRBGs are the latest entries in this array.
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun static const struct drbg_core drbg_cores[] = {
118*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_CTR
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun .flags = DRBG_CTR | DRBG_STRENGTH128,
121*4882a593Smuzhiyun .statelen = 32, /* 256 bits as defined in 10.2.1 */
122*4882a593Smuzhiyun .blocklen_bytes = 16,
123*4882a593Smuzhiyun .cra_name = "ctr_aes128",
124*4882a593Smuzhiyun .backend_cra_name = "aes",
125*4882a593Smuzhiyun }, {
126*4882a593Smuzhiyun .flags = DRBG_CTR | DRBG_STRENGTH192,
127*4882a593Smuzhiyun .statelen = 40, /* 320 bits as defined in 10.2.1 */
128*4882a593Smuzhiyun .blocklen_bytes = 16,
129*4882a593Smuzhiyun .cra_name = "ctr_aes192",
130*4882a593Smuzhiyun .backend_cra_name = "aes",
131*4882a593Smuzhiyun }, {
132*4882a593Smuzhiyun .flags = DRBG_CTR | DRBG_STRENGTH256,
133*4882a593Smuzhiyun .statelen = 48, /* 384 bits as defined in 10.2.1 */
134*4882a593Smuzhiyun .blocklen_bytes = 16,
135*4882a593Smuzhiyun .cra_name = "ctr_aes256",
136*4882a593Smuzhiyun .backend_cra_name = "aes",
137*4882a593Smuzhiyun },
138*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_CTR */
139*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_HASH
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun .flags = DRBG_HASH | DRBG_STRENGTH128,
142*4882a593Smuzhiyun .statelen = 55, /* 440 bits */
143*4882a593Smuzhiyun .blocklen_bytes = 20,
144*4882a593Smuzhiyun .cra_name = "sha1",
145*4882a593Smuzhiyun .backend_cra_name = "sha1",
146*4882a593Smuzhiyun }, {
147*4882a593Smuzhiyun .flags = DRBG_HASH | DRBG_STRENGTH256,
148*4882a593Smuzhiyun .statelen = 111, /* 888 bits */
149*4882a593Smuzhiyun .blocklen_bytes = 48,
150*4882a593Smuzhiyun .cra_name = "sha384",
151*4882a593Smuzhiyun .backend_cra_name = "sha384",
152*4882a593Smuzhiyun }, {
153*4882a593Smuzhiyun .flags = DRBG_HASH | DRBG_STRENGTH256,
154*4882a593Smuzhiyun .statelen = 111, /* 888 bits */
155*4882a593Smuzhiyun .blocklen_bytes = 64,
156*4882a593Smuzhiyun .cra_name = "sha512",
157*4882a593Smuzhiyun .backend_cra_name = "sha512",
158*4882a593Smuzhiyun }, {
159*4882a593Smuzhiyun .flags = DRBG_HASH | DRBG_STRENGTH256,
160*4882a593Smuzhiyun .statelen = 55, /* 440 bits */
161*4882a593Smuzhiyun .blocklen_bytes = 32,
162*4882a593Smuzhiyun .cra_name = "sha256",
163*4882a593Smuzhiyun .backend_cra_name = "sha256",
164*4882a593Smuzhiyun },
165*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_HASH */
166*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_HMAC
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun .flags = DRBG_HMAC | DRBG_STRENGTH128,
169*4882a593Smuzhiyun .statelen = 20, /* block length of cipher */
170*4882a593Smuzhiyun .blocklen_bytes = 20,
171*4882a593Smuzhiyun .cra_name = "hmac_sha1",
172*4882a593Smuzhiyun .backend_cra_name = "hmac(sha1)",
173*4882a593Smuzhiyun }, {
174*4882a593Smuzhiyun .flags = DRBG_HMAC | DRBG_STRENGTH256,
175*4882a593Smuzhiyun .statelen = 48, /* block length of cipher */
176*4882a593Smuzhiyun .blocklen_bytes = 48,
177*4882a593Smuzhiyun .cra_name = "hmac_sha384",
178*4882a593Smuzhiyun .backend_cra_name = "hmac(sha384)",
179*4882a593Smuzhiyun }, {
180*4882a593Smuzhiyun .flags = DRBG_HMAC | DRBG_STRENGTH256,
181*4882a593Smuzhiyun .statelen = 64, /* block length of cipher */
182*4882a593Smuzhiyun .blocklen_bytes = 64,
183*4882a593Smuzhiyun .cra_name = "hmac_sha512",
184*4882a593Smuzhiyun .backend_cra_name = "hmac(sha512)",
185*4882a593Smuzhiyun }, {
186*4882a593Smuzhiyun .flags = DRBG_HMAC | DRBG_STRENGTH256,
187*4882a593Smuzhiyun .statelen = 32, /* block length of cipher */
188*4882a593Smuzhiyun .blocklen_bytes = 32,
189*4882a593Smuzhiyun .cra_name = "hmac_sha256",
190*4882a593Smuzhiyun .backend_cra_name = "hmac(sha256)",
191*4882a593Smuzhiyun },
192*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_HMAC */
193*4882a593Smuzhiyun };
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun static int drbg_uninstantiate(struct drbg_state *drbg);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun /******************************************************************
198*4882a593Smuzhiyun * Generic helper functions
199*4882a593Smuzhiyun ******************************************************************/
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /*
202*4882a593Smuzhiyun * Return strength of DRBG according to SP800-90A section 8.4
203*4882a593Smuzhiyun *
204*4882a593Smuzhiyun * @flags DRBG flags reference
205*4882a593Smuzhiyun *
206*4882a593Smuzhiyun * Return: normalized strength in *bytes* value or 32 as default
207*4882a593Smuzhiyun * to counter programming errors
208*4882a593Smuzhiyun */
drbg_sec_strength(drbg_flag_t flags)209*4882a593Smuzhiyun static inline unsigned short drbg_sec_strength(drbg_flag_t flags)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun switch (flags & DRBG_STRENGTH_MASK) {
212*4882a593Smuzhiyun case DRBG_STRENGTH128:
213*4882a593Smuzhiyun return 16;
214*4882a593Smuzhiyun case DRBG_STRENGTH192:
215*4882a593Smuzhiyun return 24;
216*4882a593Smuzhiyun case DRBG_STRENGTH256:
217*4882a593Smuzhiyun return 32;
218*4882a593Smuzhiyun default:
219*4882a593Smuzhiyun return 32;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun /*
224*4882a593Smuzhiyun * FIPS 140-2 continuous self test for the noise source
225*4882a593Smuzhiyun * The test is performed on the noise source input data. Thus, the function
226*4882a593Smuzhiyun * implicitly knows the size of the buffer to be equal to the security
227*4882a593Smuzhiyun * strength.
228*4882a593Smuzhiyun *
229*4882a593Smuzhiyun * Note, this function disregards the nonce trailing the entropy data during
230*4882a593Smuzhiyun * initial seeding.
231*4882a593Smuzhiyun *
232*4882a593Smuzhiyun * drbg->drbg_mutex must have been taken.
233*4882a593Smuzhiyun *
234*4882a593Smuzhiyun * @drbg DRBG handle
235*4882a593Smuzhiyun * @entropy buffer of seed data to be checked
236*4882a593Smuzhiyun *
237*4882a593Smuzhiyun * return:
238*4882a593Smuzhiyun * 0 on success
239*4882a593Smuzhiyun * -EAGAIN on when the CTRNG is not yet primed
240*4882a593Smuzhiyun * < 0 on error
241*4882a593Smuzhiyun */
drbg_fips_continuous_test(struct drbg_state * drbg,const unsigned char * entropy)242*4882a593Smuzhiyun static int drbg_fips_continuous_test(struct drbg_state *drbg,
243*4882a593Smuzhiyun const unsigned char *entropy)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun unsigned short entropylen = drbg_sec_strength(drbg->core->flags);
246*4882a593Smuzhiyun int ret = 0;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun if (!IS_ENABLED(CONFIG_CRYPTO_FIPS))
249*4882a593Smuzhiyun return 0;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* skip test if we test the overall system */
252*4882a593Smuzhiyun if (list_empty(&drbg->test_data.list))
253*4882a593Smuzhiyun return 0;
254*4882a593Smuzhiyun /* only perform test in FIPS mode */
255*4882a593Smuzhiyun if (!fips_enabled)
256*4882a593Smuzhiyun return 0;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun if (!drbg->fips_primed) {
259*4882a593Smuzhiyun /* Priming of FIPS test */
260*4882a593Smuzhiyun memcpy(drbg->prev, entropy, entropylen);
261*4882a593Smuzhiyun drbg->fips_primed = true;
262*4882a593Smuzhiyun /* priming: another round is needed */
263*4882a593Smuzhiyun return -EAGAIN;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun ret = memcmp(drbg->prev, entropy, entropylen);
266*4882a593Smuzhiyun if (!ret)
267*4882a593Smuzhiyun panic("DRBG continuous self test failed\n");
268*4882a593Smuzhiyun memcpy(drbg->prev, entropy, entropylen);
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /* the test shall pass when the two values are not equal */
271*4882a593Smuzhiyun return 0;
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun /*
275*4882a593Smuzhiyun * Convert an integer into a byte representation of this integer.
276*4882a593Smuzhiyun * The byte representation is big-endian
277*4882a593Smuzhiyun *
278*4882a593Smuzhiyun * @val value to be converted
279*4882a593Smuzhiyun * @buf buffer holding the converted integer -- caller must ensure that
280*4882a593Smuzhiyun * buffer size is at least 32 bit
281*4882a593Smuzhiyun */
282*4882a593Smuzhiyun #if (defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR))
drbg_cpu_to_be32(__u32 val,unsigned char * buf)283*4882a593Smuzhiyun static inline void drbg_cpu_to_be32(__u32 val, unsigned char *buf)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun struct s {
286*4882a593Smuzhiyun __be32 conv;
287*4882a593Smuzhiyun };
288*4882a593Smuzhiyun struct s *conversion = (struct s *) buf;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun conversion->conv = cpu_to_be32(val);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun #endif /* defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_CTR) */
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /******************************************************************
295*4882a593Smuzhiyun * CTR DRBG callback functions
296*4882a593Smuzhiyun ******************************************************************/
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_CTR
299*4882a593Smuzhiyun #define CRYPTO_DRBG_CTR_STRING "CTR "
300*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes256");
301*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes256");
302*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes192");
303*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes192");
304*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_ctr_aes128");
305*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_ctr_aes128");
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
308*4882a593Smuzhiyun const unsigned char *key);
309*4882a593Smuzhiyun static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
310*4882a593Smuzhiyun const struct drbg_string *in);
311*4882a593Smuzhiyun static int drbg_init_sym_kernel(struct drbg_state *drbg);
312*4882a593Smuzhiyun static int drbg_fini_sym_kernel(struct drbg_state *drbg);
313*4882a593Smuzhiyun static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
314*4882a593Smuzhiyun u8 *inbuf, u32 inbuflen,
315*4882a593Smuzhiyun u8 *outbuf, u32 outlen);
316*4882a593Smuzhiyun #define DRBG_OUTSCRATCHLEN 256
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun /* BCC function for CTR DRBG as defined in 10.4.3 */
drbg_ctr_bcc(struct drbg_state * drbg,unsigned char * out,const unsigned char * key,struct list_head * in)319*4882a593Smuzhiyun static int drbg_ctr_bcc(struct drbg_state *drbg,
320*4882a593Smuzhiyun unsigned char *out, const unsigned char *key,
321*4882a593Smuzhiyun struct list_head *in)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun int ret = 0;
324*4882a593Smuzhiyun struct drbg_string *curr = NULL;
325*4882a593Smuzhiyun struct drbg_string data;
326*4882a593Smuzhiyun short cnt = 0;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun drbg_string_fill(&data, out, drbg_blocklen(drbg));
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun /* 10.4.3 step 2 / 4 */
331*4882a593Smuzhiyun drbg_kcapi_symsetkey(drbg, key);
332*4882a593Smuzhiyun list_for_each_entry(curr, in, list) {
333*4882a593Smuzhiyun const unsigned char *pos = curr->buf;
334*4882a593Smuzhiyun size_t len = curr->len;
335*4882a593Smuzhiyun /* 10.4.3 step 4.1 */
336*4882a593Smuzhiyun while (len) {
337*4882a593Smuzhiyun /* 10.4.3 step 4.2 */
338*4882a593Smuzhiyun if (drbg_blocklen(drbg) == cnt) {
339*4882a593Smuzhiyun cnt = 0;
340*4882a593Smuzhiyun ret = drbg_kcapi_sym(drbg, out, &data);
341*4882a593Smuzhiyun if (ret)
342*4882a593Smuzhiyun return ret;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun out[cnt] ^= *pos;
345*4882a593Smuzhiyun pos++;
346*4882a593Smuzhiyun cnt++;
347*4882a593Smuzhiyun len--;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun /* 10.4.3 step 4.2 for last block */
351*4882a593Smuzhiyun if (cnt)
352*4882a593Smuzhiyun ret = drbg_kcapi_sym(drbg, out, &data);
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun return ret;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /*
358*4882a593Smuzhiyun * scratchpad usage: drbg_ctr_update is interlinked with drbg_ctr_df
359*4882a593Smuzhiyun * (and drbg_ctr_bcc, but this function does not need any temporary buffers),
360*4882a593Smuzhiyun * the scratchpad is used as follows:
361*4882a593Smuzhiyun * drbg_ctr_update:
362*4882a593Smuzhiyun * temp
363*4882a593Smuzhiyun * start: drbg->scratchpad
364*4882a593Smuzhiyun * length: drbg_statelen(drbg) + drbg_blocklen(drbg)
365*4882a593Smuzhiyun * note: the cipher writing into this variable works
366*4882a593Smuzhiyun * blocklen-wise. Now, when the statelen is not a multiple
367*4882a593Smuzhiyun * of blocklen, the generateion loop below "spills over"
368*4882a593Smuzhiyun * by at most blocklen. Thus, we need to give sufficient
369*4882a593Smuzhiyun * memory.
370*4882a593Smuzhiyun * df_data
371*4882a593Smuzhiyun * start: drbg->scratchpad +
372*4882a593Smuzhiyun * drbg_statelen(drbg) + drbg_blocklen(drbg)
373*4882a593Smuzhiyun * length: drbg_statelen(drbg)
374*4882a593Smuzhiyun *
375*4882a593Smuzhiyun * drbg_ctr_df:
376*4882a593Smuzhiyun * pad
377*4882a593Smuzhiyun * start: df_data + drbg_statelen(drbg)
378*4882a593Smuzhiyun * length: drbg_blocklen(drbg)
379*4882a593Smuzhiyun * iv
380*4882a593Smuzhiyun * start: pad + drbg_blocklen(drbg)
381*4882a593Smuzhiyun * length: drbg_blocklen(drbg)
382*4882a593Smuzhiyun * temp
383*4882a593Smuzhiyun * start: iv + drbg_blocklen(drbg)
384*4882a593Smuzhiyun * length: drbg_satelen(drbg) + drbg_blocklen(drbg)
385*4882a593Smuzhiyun * note: temp is the buffer that the BCC function operates
386*4882a593Smuzhiyun * on. BCC operates blockwise. drbg_statelen(drbg)
387*4882a593Smuzhiyun * is sufficient when the DRBG state length is a multiple
388*4882a593Smuzhiyun * of the block size. For AES192 (and maybe other ciphers)
389*4882a593Smuzhiyun * this is not correct and the length for temp is
390*4882a593Smuzhiyun * insufficient (yes, that also means for such ciphers,
391*4882a593Smuzhiyun * the final output of all BCC rounds are truncated).
392*4882a593Smuzhiyun * Therefore, add drbg_blocklen(drbg) to cover all
393*4882a593Smuzhiyun * possibilities.
394*4882a593Smuzhiyun */
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun /* Derivation Function for CTR DRBG as defined in 10.4.2 */
drbg_ctr_df(struct drbg_state * drbg,unsigned char * df_data,size_t bytes_to_return,struct list_head * seedlist)397*4882a593Smuzhiyun static int drbg_ctr_df(struct drbg_state *drbg,
398*4882a593Smuzhiyun unsigned char *df_data, size_t bytes_to_return,
399*4882a593Smuzhiyun struct list_head *seedlist)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun int ret = -EFAULT;
402*4882a593Smuzhiyun unsigned char L_N[8];
403*4882a593Smuzhiyun /* S3 is input */
404*4882a593Smuzhiyun struct drbg_string S1, S2, S4, cipherin;
405*4882a593Smuzhiyun LIST_HEAD(bcc_list);
406*4882a593Smuzhiyun unsigned char *pad = df_data + drbg_statelen(drbg);
407*4882a593Smuzhiyun unsigned char *iv = pad + drbg_blocklen(drbg);
408*4882a593Smuzhiyun unsigned char *temp = iv + drbg_blocklen(drbg);
409*4882a593Smuzhiyun size_t padlen = 0;
410*4882a593Smuzhiyun unsigned int templen = 0;
411*4882a593Smuzhiyun /* 10.4.2 step 7 */
412*4882a593Smuzhiyun unsigned int i = 0;
413*4882a593Smuzhiyun /* 10.4.2 step 8 */
414*4882a593Smuzhiyun const unsigned char *K = (unsigned char *)
415*4882a593Smuzhiyun "\x00\x01\x02\x03\x04\x05\x06\x07"
416*4882a593Smuzhiyun "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
417*4882a593Smuzhiyun "\x10\x11\x12\x13\x14\x15\x16\x17"
418*4882a593Smuzhiyun "\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
419*4882a593Smuzhiyun unsigned char *X;
420*4882a593Smuzhiyun size_t generated_len = 0;
421*4882a593Smuzhiyun size_t inputlen = 0;
422*4882a593Smuzhiyun struct drbg_string *seed = NULL;
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun memset(pad, 0, drbg_blocklen(drbg));
425*4882a593Smuzhiyun memset(iv, 0, drbg_blocklen(drbg));
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /* 10.4.2 step 1 is implicit as we work byte-wise */
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun /* 10.4.2 step 2 */
430*4882a593Smuzhiyun if ((512/8) < bytes_to_return)
431*4882a593Smuzhiyun return -EINVAL;
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /* 10.4.2 step 2 -- calculate the entire length of all input data */
434*4882a593Smuzhiyun list_for_each_entry(seed, seedlist, list)
435*4882a593Smuzhiyun inputlen += seed->len;
436*4882a593Smuzhiyun drbg_cpu_to_be32(inputlen, &L_N[0]);
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun /* 10.4.2 step 3 */
439*4882a593Smuzhiyun drbg_cpu_to_be32(bytes_to_return, &L_N[4]);
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /* 10.4.2 step 5: length is L_N, input_string, one byte, padding */
442*4882a593Smuzhiyun padlen = (inputlen + sizeof(L_N) + 1) % (drbg_blocklen(drbg));
443*4882a593Smuzhiyun /* wrap the padlen appropriately */
444*4882a593Smuzhiyun if (padlen)
445*4882a593Smuzhiyun padlen = drbg_blocklen(drbg) - padlen;
446*4882a593Smuzhiyun /*
447*4882a593Smuzhiyun * pad / padlen contains the 0x80 byte and the following zero bytes.
448*4882a593Smuzhiyun * As the calculated padlen value only covers the number of zero
449*4882a593Smuzhiyun * bytes, this value has to be incremented by one for the 0x80 byte.
450*4882a593Smuzhiyun */
451*4882a593Smuzhiyun padlen++;
452*4882a593Smuzhiyun pad[0] = 0x80;
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun /* 10.4.2 step 4 -- first fill the linked list and then order it */
455*4882a593Smuzhiyun drbg_string_fill(&S1, iv, drbg_blocklen(drbg));
456*4882a593Smuzhiyun list_add_tail(&S1.list, &bcc_list);
457*4882a593Smuzhiyun drbg_string_fill(&S2, L_N, sizeof(L_N));
458*4882a593Smuzhiyun list_add_tail(&S2.list, &bcc_list);
459*4882a593Smuzhiyun list_splice_tail(seedlist, &bcc_list);
460*4882a593Smuzhiyun drbg_string_fill(&S4, pad, padlen);
461*4882a593Smuzhiyun list_add_tail(&S4.list, &bcc_list);
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun /* 10.4.2 step 9 */
464*4882a593Smuzhiyun while (templen < (drbg_keylen(drbg) + (drbg_blocklen(drbg)))) {
465*4882a593Smuzhiyun /*
466*4882a593Smuzhiyun * 10.4.2 step 9.1 - the padding is implicit as the buffer
467*4882a593Smuzhiyun * holds zeros after allocation -- even the increment of i
468*4882a593Smuzhiyun * is irrelevant as the increment remains within length of i
469*4882a593Smuzhiyun */
470*4882a593Smuzhiyun drbg_cpu_to_be32(i, iv);
471*4882a593Smuzhiyun /* 10.4.2 step 9.2 -- BCC and concatenation with temp */
472*4882a593Smuzhiyun ret = drbg_ctr_bcc(drbg, temp + templen, K, &bcc_list);
473*4882a593Smuzhiyun if (ret)
474*4882a593Smuzhiyun goto out;
475*4882a593Smuzhiyun /* 10.4.2 step 9.3 */
476*4882a593Smuzhiyun i++;
477*4882a593Smuzhiyun templen += drbg_blocklen(drbg);
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /* 10.4.2 step 11 */
481*4882a593Smuzhiyun X = temp + (drbg_keylen(drbg));
482*4882a593Smuzhiyun drbg_string_fill(&cipherin, X, drbg_blocklen(drbg));
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun /* 10.4.2 step 12: overwriting of outval is implemented in next step */
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun /* 10.4.2 step 13 */
487*4882a593Smuzhiyun drbg_kcapi_symsetkey(drbg, temp);
488*4882a593Smuzhiyun while (generated_len < bytes_to_return) {
489*4882a593Smuzhiyun short blocklen = 0;
490*4882a593Smuzhiyun /*
491*4882a593Smuzhiyun * 10.4.2 step 13.1: the truncation of the key length is
492*4882a593Smuzhiyun * implicit as the key is only drbg_blocklen in size based on
493*4882a593Smuzhiyun * the implementation of the cipher function callback
494*4882a593Smuzhiyun */
495*4882a593Smuzhiyun ret = drbg_kcapi_sym(drbg, X, &cipherin);
496*4882a593Smuzhiyun if (ret)
497*4882a593Smuzhiyun goto out;
498*4882a593Smuzhiyun blocklen = (drbg_blocklen(drbg) <
499*4882a593Smuzhiyun (bytes_to_return - generated_len)) ?
500*4882a593Smuzhiyun drbg_blocklen(drbg) :
501*4882a593Smuzhiyun (bytes_to_return - generated_len);
502*4882a593Smuzhiyun /* 10.4.2 step 13.2 and 14 */
503*4882a593Smuzhiyun memcpy(df_data + generated_len, X, blocklen);
504*4882a593Smuzhiyun generated_len += blocklen;
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun ret = 0;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun out:
510*4882a593Smuzhiyun memset(iv, 0, drbg_blocklen(drbg));
511*4882a593Smuzhiyun memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
512*4882a593Smuzhiyun memset(pad, 0, drbg_blocklen(drbg));
513*4882a593Smuzhiyun return ret;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun /*
517*4882a593Smuzhiyun * update function of CTR DRBG as defined in 10.2.1.2
518*4882a593Smuzhiyun *
519*4882a593Smuzhiyun * The reseed variable has an enhanced meaning compared to the update
520*4882a593Smuzhiyun * functions of the other DRBGs as follows:
521*4882a593Smuzhiyun * 0 => initial seed from initialization
522*4882a593Smuzhiyun * 1 => reseed via drbg_seed
523*4882a593Smuzhiyun * 2 => first invocation from drbg_ctr_update when addtl is present. In
524*4882a593Smuzhiyun * this case, the df_data scratchpad is not deleted so that it is
525*4882a593Smuzhiyun * available for another calls to prevent calling the DF function
526*4882a593Smuzhiyun * again.
527*4882a593Smuzhiyun * 3 => second invocation from drbg_ctr_update. When the update function
528*4882a593Smuzhiyun * was called with addtl, the df_data memory already contains the
529*4882a593Smuzhiyun * DFed addtl information and we do not need to call DF again.
530*4882a593Smuzhiyun */
drbg_ctr_update(struct drbg_state * drbg,struct list_head * seed,int reseed)531*4882a593Smuzhiyun static int drbg_ctr_update(struct drbg_state *drbg, struct list_head *seed,
532*4882a593Smuzhiyun int reseed)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun int ret = -EFAULT;
535*4882a593Smuzhiyun /* 10.2.1.2 step 1 */
536*4882a593Smuzhiyun unsigned char *temp = drbg->scratchpad;
537*4882a593Smuzhiyun unsigned char *df_data = drbg->scratchpad + drbg_statelen(drbg) +
538*4882a593Smuzhiyun drbg_blocklen(drbg);
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun if (3 > reseed)
541*4882a593Smuzhiyun memset(df_data, 0, drbg_statelen(drbg));
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun if (!reseed) {
544*4882a593Smuzhiyun /*
545*4882a593Smuzhiyun * The DRBG uses the CTR mode of the underlying AES cipher. The
546*4882a593Smuzhiyun * CTR mode increments the counter value after the AES operation
547*4882a593Smuzhiyun * but SP800-90A requires that the counter is incremented before
548*4882a593Smuzhiyun * the AES operation. Hence, we increment it at the time we set
549*4882a593Smuzhiyun * it by one.
550*4882a593Smuzhiyun */
551*4882a593Smuzhiyun crypto_inc(drbg->V, drbg_blocklen(drbg));
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun ret = crypto_skcipher_setkey(drbg->ctr_handle, drbg->C,
554*4882a593Smuzhiyun drbg_keylen(drbg));
555*4882a593Smuzhiyun if (ret)
556*4882a593Smuzhiyun goto out;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun /* 10.2.1.3.2 step 2 and 10.2.1.4.2 step 2 */
560*4882a593Smuzhiyun if (seed) {
561*4882a593Smuzhiyun ret = drbg_ctr_df(drbg, df_data, drbg_statelen(drbg), seed);
562*4882a593Smuzhiyun if (ret)
563*4882a593Smuzhiyun goto out;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun ret = drbg_kcapi_sym_ctr(drbg, df_data, drbg_statelen(drbg),
567*4882a593Smuzhiyun temp, drbg_statelen(drbg));
568*4882a593Smuzhiyun if (ret)
569*4882a593Smuzhiyun return ret;
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun /* 10.2.1.2 step 5 */
572*4882a593Smuzhiyun ret = crypto_skcipher_setkey(drbg->ctr_handle, temp,
573*4882a593Smuzhiyun drbg_keylen(drbg));
574*4882a593Smuzhiyun if (ret)
575*4882a593Smuzhiyun goto out;
576*4882a593Smuzhiyun /* 10.2.1.2 step 6 */
577*4882a593Smuzhiyun memcpy(drbg->V, temp + drbg_keylen(drbg), drbg_blocklen(drbg));
578*4882a593Smuzhiyun /* See above: increment counter by one to compensate timing of CTR op */
579*4882a593Smuzhiyun crypto_inc(drbg->V, drbg_blocklen(drbg));
580*4882a593Smuzhiyun ret = 0;
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun out:
583*4882a593Smuzhiyun memset(temp, 0, drbg_statelen(drbg) + drbg_blocklen(drbg));
584*4882a593Smuzhiyun if (2 != reseed)
585*4882a593Smuzhiyun memset(df_data, 0, drbg_statelen(drbg));
586*4882a593Smuzhiyun return ret;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /*
590*4882a593Smuzhiyun * scratchpad use: drbg_ctr_update is called independently from
591*4882a593Smuzhiyun * drbg_ctr_extract_bytes. Therefore, the scratchpad is reused
592*4882a593Smuzhiyun */
593*4882a593Smuzhiyun /* Generate function of CTR DRBG as defined in 10.2.1.5.2 */
drbg_ctr_generate(struct drbg_state * drbg,unsigned char * buf,unsigned int buflen,struct list_head * addtl)594*4882a593Smuzhiyun static int drbg_ctr_generate(struct drbg_state *drbg,
595*4882a593Smuzhiyun unsigned char *buf, unsigned int buflen,
596*4882a593Smuzhiyun struct list_head *addtl)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun int ret;
599*4882a593Smuzhiyun int len = min_t(int, buflen, INT_MAX);
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun /* 10.2.1.5.2 step 2 */
602*4882a593Smuzhiyun if (addtl && !list_empty(addtl)) {
603*4882a593Smuzhiyun ret = drbg_ctr_update(drbg, addtl, 2);
604*4882a593Smuzhiyun if (ret)
605*4882a593Smuzhiyun return 0;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun /* 10.2.1.5.2 step 4.1 */
609*4882a593Smuzhiyun ret = drbg_kcapi_sym_ctr(drbg, NULL, 0, buf, len);
610*4882a593Smuzhiyun if (ret)
611*4882a593Smuzhiyun return ret;
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun /* 10.2.1.5.2 step 6 */
614*4882a593Smuzhiyun ret = drbg_ctr_update(drbg, NULL, 3);
615*4882a593Smuzhiyun if (ret)
616*4882a593Smuzhiyun len = ret;
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun return len;
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun static const struct drbg_state_ops drbg_ctr_ops = {
622*4882a593Smuzhiyun .update = drbg_ctr_update,
623*4882a593Smuzhiyun .generate = drbg_ctr_generate,
624*4882a593Smuzhiyun .crypto_init = drbg_init_sym_kernel,
625*4882a593Smuzhiyun .crypto_fini = drbg_fini_sym_kernel,
626*4882a593Smuzhiyun };
627*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_CTR */
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun /******************************************************************
630*4882a593Smuzhiyun * HMAC DRBG callback functions
631*4882a593Smuzhiyun ******************************************************************/
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
634*4882a593Smuzhiyun static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
635*4882a593Smuzhiyun const struct list_head *in);
636*4882a593Smuzhiyun static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
637*4882a593Smuzhiyun const unsigned char *key);
638*4882a593Smuzhiyun static int drbg_init_hash_kernel(struct drbg_state *drbg);
639*4882a593Smuzhiyun static int drbg_fini_hash_kernel(struct drbg_state *drbg);
640*4882a593Smuzhiyun #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_HMAC
643*4882a593Smuzhiyun #define CRYPTO_DRBG_HMAC_STRING "HMAC "
644*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha512");
645*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha512");
646*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha384");
647*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha384");
648*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha256");
649*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha256");
650*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_hmac_sha1");
651*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_hmac_sha1");
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun /* update function of HMAC DRBG as defined in 10.1.2.2 */
drbg_hmac_update(struct drbg_state * drbg,struct list_head * seed,int reseed)654*4882a593Smuzhiyun static int drbg_hmac_update(struct drbg_state *drbg, struct list_head *seed,
655*4882a593Smuzhiyun int reseed)
656*4882a593Smuzhiyun {
657*4882a593Smuzhiyun int ret = -EFAULT;
658*4882a593Smuzhiyun int i = 0;
659*4882a593Smuzhiyun struct drbg_string seed1, seed2, vdata;
660*4882a593Smuzhiyun LIST_HEAD(seedlist);
661*4882a593Smuzhiyun LIST_HEAD(vdatalist);
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun if (!reseed) {
664*4882a593Smuzhiyun /* 10.1.2.3 step 2 -- memset(0) of C is implicit with kzalloc */
665*4882a593Smuzhiyun memset(drbg->V, 1, drbg_statelen(drbg));
666*4882a593Smuzhiyun drbg_kcapi_hmacsetkey(drbg, drbg->C);
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun drbg_string_fill(&seed1, drbg->V, drbg_statelen(drbg));
670*4882a593Smuzhiyun list_add_tail(&seed1.list, &seedlist);
671*4882a593Smuzhiyun /* buffer of seed2 will be filled in for loop below with one byte */
672*4882a593Smuzhiyun drbg_string_fill(&seed2, NULL, 1);
673*4882a593Smuzhiyun list_add_tail(&seed2.list, &seedlist);
674*4882a593Smuzhiyun /* input data of seed is allowed to be NULL at this point */
675*4882a593Smuzhiyun if (seed)
676*4882a593Smuzhiyun list_splice_tail(seed, &seedlist);
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun drbg_string_fill(&vdata, drbg->V, drbg_statelen(drbg));
679*4882a593Smuzhiyun list_add_tail(&vdata.list, &vdatalist);
680*4882a593Smuzhiyun for (i = 2; 0 < i; i--) {
681*4882a593Smuzhiyun /* first round uses 0x0, second 0x1 */
682*4882a593Smuzhiyun unsigned char prefix = DRBG_PREFIX0;
683*4882a593Smuzhiyun if (1 == i)
684*4882a593Smuzhiyun prefix = DRBG_PREFIX1;
685*4882a593Smuzhiyun /* 10.1.2.2 step 1 and 4 -- concatenation and HMAC for key */
686*4882a593Smuzhiyun seed2.buf = &prefix;
687*4882a593Smuzhiyun ret = drbg_kcapi_hash(drbg, drbg->C, &seedlist);
688*4882a593Smuzhiyun if (ret)
689*4882a593Smuzhiyun return ret;
690*4882a593Smuzhiyun drbg_kcapi_hmacsetkey(drbg, drbg->C);
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun /* 10.1.2.2 step 2 and 5 -- HMAC for V */
693*4882a593Smuzhiyun ret = drbg_kcapi_hash(drbg, drbg->V, &vdatalist);
694*4882a593Smuzhiyun if (ret)
695*4882a593Smuzhiyun return ret;
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun /* 10.1.2.2 step 3 */
698*4882a593Smuzhiyun if (!seed)
699*4882a593Smuzhiyun return ret;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun return 0;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun /* generate function of HMAC DRBG as defined in 10.1.2.5 */
drbg_hmac_generate(struct drbg_state * drbg,unsigned char * buf,unsigned int buflen,struct list_head * addtl)706*4882a593Smuzhiyun static int drbg_hmac_generate(struct drbg_state *drbg,
707*4882a593Smuzhiyun unsigned char *buf,
708*4882a593Smuzhiyun unsigned int buflen,
709*4882a593Smuzhiyun struct list_head *addtl)
710*4882a593Smuzhiyun {
711*4882a593Smuzhiyun int len = 0;
712*4882a593Smuzhiyun int ret = 0;
713*4882a593Smuzhiyun struct drbg_string data;
714*4882a593Smuzhiyun LIST_HEAD(datalist);
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun /* 10.1.2.5 step 2 */
717*4882a593Smuzhiyun if (addtl && !list_empty(addtl)) {
718*4882a593Smuzhiyun ret = drbg_hmac_update(drbg, addtl, 1);
719*4882a593Smuzhiyun if (ret)
720*4882a593Smuzhiyun return ret;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun drbg_string_fill(&data, drbg->V, drbg_statelen(drbg));
724*4882a593Smuzhiyun list_add_tail(&data.list, &datalist);
725*4882a593Smuzhiyun while (len < buflen) {
726*4882a593Smuzhiyun unsigned int outlen = 0;
727*4882a593Smuzhiyun /* 10.1.2.5 step 4.1 */
728*4882a593Smuzhiyun ret = drbg_kcapi_hash(drbg, drbg->V, &datalist);
729*4882a593Smuzhiyun if (ret)
730*4882a593Smuzhiyun return ret;
731*4882a593Smuzhiyun outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
732*4882a593Smuzhiyun drbg_blocklen(drbg) : (buflen - len);
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun /* 10.1.2.5 step 4.2 */
735*4882a593Smuzhiyun memcpy(buf + len, drbg->V, outlen);
736*4882a593Smuzhiyun len += outlen;
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun /* 10.1.2.5 step 6 */
740*4882a593Smuzhiyun if (addtl && !list_empty(addtl))
741*4882a593Smuzhiyun ret = drbg_hmac_update(drbg, addtl, 1);
742*4882a593Smuzhiyun else
743*4882a593Smuzhiyun ret = drbg_hmac_update(drbg, NULL, 1);
744*4882a593Smuzhiyun if (ret)
745*4882a593Smuzhiyun return ret;
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun return len;
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun
750*4882a593Smuzhiyun static const struct drbg_state_ops drbg_hmac_ops = {
751*4882a593Smuzhiyun .update = drbg_hmac_update,
752*4882a593Smuzhiyun .generate = drbg_hmac_generate,
753*4882a593Smuzhiyun .crypto_init = drbg_init_hash_kernel,
754*4882a593Smuzhiyun .crypto_fini = drbg_fini_hash_kernel,
755*4882a593Smuzhiyun };
756*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_HMAC */
757*4882a593Smuzhiyun
758*4882a593Smuzhiyun /******************************************************************
759*4882a593Smuzhiyun * Hash DRBG callback functions
760*4882a593Smuzhiyun ******************************************************************/
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_HASH
763*4882a593Smuzhiyun #define CRYPTO_DRBG_HASH_STRING "HASH "
764*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_sha512");
765*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_sha512");
766*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_sha384");
767*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_sha384");
768*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_sha256");
769*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_sha256");
770*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_pr_sha1");
771*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("drbg_nopr_sha1");
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun /*
774*4882a593Smuzhiyun * Increment buffer
775*4882a593Smuzhiyun *
776*4882a593Smuzhiyun * @dst buffer to increment
777*4882a593Smuzhiyun * @add value to add
778*4882a593Smuzhiyun */
drbg_add_buf(unsigned char * dst,size_t dstlen,const unsigned char * add,size_t addlen)779*4882a593Smuzhiyun static inline void drbg_add_buf(unsigned char *dst, size_t dstlen,
780*4882a593Smuzhiyun const unsigned char *add, size_t addlen)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun /* implied: dstlen > addlen */
783*4882a593Smuzhiyun unsigned char *dstptr;
784*4882a593Smuzhiyun const unsigned char *addptr;
785*4882a593Smuzhiyun unsigned int remainder = 0;
786*4882a593Smuzhiyun size_t len = addlen;
787*4882a593Smuzhiyun
788*4882a593Smuzhiyun dstptr = dst + (dstlen-1);
789*4882a593Smuzhiyun addptr = add + (addlen-1);
790*4882a593Smuzhiyun while (len) {
791*4882a593Smuzhiyun remainder += *dstptr + *addptr;
792*4882a593Smuzhiyun *dstptr = remainder & 0xff;
793*4882a593Smuzhiyun remainder >>= 8;
794*4882a593Smuzhiyun len--; dstptr--; addptr--;
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun len = dstlen - addlen;
797*4882a593Smuzhiyun while (len && remainder > 0) {
798*4882a593Smuzhiyun remainder = *dstptr + 1;
799*4882a593Smuzhiyun *dstptr = remainder & 0xff;
800*4882a593Smuzhiyun remainder >>= 8;
801*4882a593Smuzhiyun len--; dstptr--;
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun
805*4882a593Smuzhiyun /*
806*4882a593Smuzhiyun * scratchpad usage: as drbg_hash_update and drbg_hash_df are used
807*4882a593Smuzhiyun * interlinked, the scratchpad is used as follows:
808*4882a593Smuzhiyun * drbg_hash_update
809*4882a593Smuzhiyun * start: drbg->scratchpad
810*4882a593Smuzhiyun * length: drbg_statelen(drbg)
811*4882a593Smuzhiyun * drbg_hash_df:
812*4882a593Smuzhiyun * start: drbg->scratchpad + drbg_statelen(drbg)
813*4882a593Smuzhiyun * length: drbg_blocklen(drbg)
814*4882a593Smuzhiyun *
815*4882a593Smuzhiyun * drbg_hash_process_addtl uses the scratchpad, but fully completes
816*4882a593Smuzhiyun * before either of the functions mentioned before are invoked. Therefore,
817*4882a593Smuzhiyun * drbg_hash_process_addtl does not need to be specifically considered.
818*4882a593Smuzhiyun */
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun /* Derivation Function for Hash DRBG as defined in 10.4.1 */
drbg_hash_df(struct drbg_state * drbg,unsigned char * outval,size_t outlen,struct list_head * entropylist)821*4882a593Smuzhiyun static int drbg_hash_df(struct drbg_state *drbg,
822*4882a593Smuzhiyun unsigned char *outval, size_t outlen,
823*4882a593Smuzhiyun struct list_head *entropylist)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun int ret = 0;
826*4882a593Smuzhiyun size_t len = 0;
827*4882a593Smuzhiyun unsigned char input[5];
828*4882a593Smuzhiyun unsigned char *tmp = drbg->scratchpad + drbg_statelen(drbg);
829*4882a593Smuzhiyun struct drbg_string data;
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun /* 10.4.1 step 3 */
832*4882a593Smuzhiyun input[0] = 1;
833*4882a593Smuzhiyun drbg_cpu_to_be32((outlen * 8), &input[1]);
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun /* 10.4.1 step 4.1 -- concatenation of data for input into hash */
836*4882a593Smuzhiyun drbg_string_fill(&data, input, 5);
837*4882a593Smuzhiyun list_add(&data.list, entropylist);
838*4882a593Smuzhiyun
839*4882a593Smuzhiyun /* 10.4.1 step 4 */
840*4882a593Smuzhiyun while (len < outlen) {
841*4882a593Smuzhiyun short blocklen = 0;
842*4882a593Smuzhiyun /* 10.4.1 step 4.1 */
843*4882a593Smuzhiyun ret = drbg_kcapi_hash(drbg, tmp, entropylist);
844*4882a593Smuzhiyun if (ret)
845*4882a593Smuzhiyun goto out;
846*4882a593Smuzhiyun /* 10.4.1 step 4.2 */
847*4882a593Smuzhiyun input[0]++;
848*4882a593Smuzhiyun blocklen = (drbg_blocklen(drbg) < (outlen - len)) ?
849*4882a593Smuzhiyun drbg_blocklen(drbg) : (outlen - len);
850*4882a593Smuzhiyun memcpy(outval + len, tmp, blocklen);
851*4882a593Smuzhiyun len += blocklen;
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun out:
855*4882a593Smuzhiyun memset(tmp, 0, drbg_blocklen(drbg));
856*4882a593Smuzhiyun return ret;
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun /* update function for Hash DRBG as defined in 10.1.1.2 / 10.1.1.3 */
drbg_hash_update(struct drbg_state * drbg,struct list_head * seed,int reseed)860*4882a593Smuzhiyun static int drbg_hash_update(struct drbg_state *drbg, struct list_head *seed,
861*4882a593Smuzhiyun int reseed)
862*4882a593Smuzhiyun {
863*4882a593Smuzhiyun int ret = 0;
864*4882a593Smuzhiyun struct drbg_string data1, data2;
865*4882a593Smuzhiyun LIST_HEAD(datalist);
866*4882a593Smuzhiyun LIST_HEAD(datalist2);
867*4882a593Smuzhiyun unsigned char *V = drbg->scratchpad;
868*4882a593Smuzhiyun unsigned char prefix = DRBG_PREFIX1;
869*4882a593Smuzhiyun
870*4882a593Smuzhiyun if (!seed)
871*4882a593Smuzhiyun return -EINVAL;
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun if (reseed) {
874*4882a593Smuzhiyun /* 10.1.1.3 step 1 */
875*4882a593Smuzhiyun memcpy(V, drbg->V, drbg_statelen(drbg));
876*4882a593Smuzhiyun drbg_string_fill(&data1, &prefix, 1);
877*4882a593Smuzhiyun list_add_tail(&data1.list, &datalist);
878*4882a593Smuzhiyun drbg_string_fill(&data2, V, drbg_statelen(drbg));
879*4882a593Smuzhiyun list_add_tail(&data2.list, &datalist);
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun list_splice_tail(seed, &datalist);
882*4882a593Smuzhiyun
883*4882a593Smuzhiyun /* 10.1.1.2 / 10.1.1.3 step 2 and 3 */
884*4882a593Smuzhiyun ret = drbg_hash_df(drbg, drbg->V, drbg_statelen(drbg), &datalist);
885*4882a593Smuzhiyun if (ret)
886*4882a593Smuzhiyun goto out;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun /* 10.1.1.2 / 10.1.1.3 step 4 */
889*4882a593Smuzhiyun prefix = DRBG_PREFIX0;
890*4882a593Smuzhiyun drbg_string_fill(&data1, &prefix, 1);
891*4882a593Smuzhiyun list_add_tail(&data1.list, &datalist2);
892*4882a593Smuzhiyun drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
893*4882a593Smuzhiyun list_add_tail(&data2.list, &datalist2);
894*4882a593Smuzhiyun /* 10.1.1.2 / 10.1.1.3 step 4 */
895*4882a593Smuzhiyun ret = drbg_hash_df(drbg, drbg->C, drbg_statelen(drbg), &datalist2);
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun out:
898*4882a593Smuzhiyun memset(drbg->scratchpad, 0, drbg_statelen(drbg));
899*4882a593Smuzhiyun return ret;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun /* processing of additional information string for Hash DRBG */
drbg_hash_process_addtl(struct drbg_state * drbg,struct list_head * addtl)903*4882a593Smuzhiyun static int drbg_hash_process_addtl(struct drbg_state *drbg,
904*4882a593Smuzhiyun struct list_head *addtl)
905*4882a593Smuzhiyun {
906*4882a593Smuzhiyun int ret = 0;
907*4882a593Smuzhiyun struct drbg_string data1, data2;
908*4882a593Smuzhiyun LIST_HEAD(datalist);
909*4882a593Smuzhiyun unsigned char prefix = DRBG_PREFIX2;
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun /* 10.1.1.4 step 2 */
912*4882a593Smuzhiyun if (!addtl || list_empty(addtl))
913*4882a593Smuzhiyun return 0;
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun /* 10.1.1.4 step 2a */
916*4882a593Smuzhiyun drbg_string_fill(&data1, &prefix, 1);
917*4882a593Smuzhiyun drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
918*4882a593Smuzhiyun list_add_tail(&data1.list, &datalist);
919*4882a593Smuzhiyun list_add_tail(&data2.list, &datalist);
920*4882a593Smuzhiyun list_splice_tail(addtl, &datalist);
921*4882a593Smuzhiyun ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
922*4882a593Smuzhiyun if (ret)
923*4882a593Smuzhiyun goto out;
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun /* 10.1.1.4 step 2b */
926*4882a593Smuzhiyun drbg_add_buf(drbg->V, drbg_statelen(drbg),
927*4882a593Smuzhiyun drbg->scratchpad, drbg_blocklen(drbg));
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun out:
930*4882a593Smuzhiyun memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
931*4882a593Smuzhiyun return ret;
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun /* Hashgen defined in 10.1.1.4 */
drbg_hash_hashgen(struct drbg_state * drbg,unsigned char * buf,unsigned int buflen)935*4882a593Smuzhiyun static int drbg_hash_hashgen(struct drbg_state *drbg,
936*4882a593Smuzhiyun unsigned char *buf,
937*4882a593Smuzhiyun unsigned int buflen)
938*4882a593Smuzhiyun {
939*4882a593Smuzhiyun int len = 0;
940*4882a593Smuzhiyun int ret = 0;
941*4882a593Smuzhiyun unsigned char *src = drbg->scratchpad;
942*4882a593Smuzhiyun unsigned char *dst = drbg->scratchpad + drbg_statelen(drbg);
943*4882a593Smuzhiyun struct drbg_string data;
944*4882a593Smuzhiyun LIST_HEAD(datalist);
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun /* 10.1.1.4 step hashgen 2 */
947*4882a593Smuzhiyun memcpy(src, drbg->V, drbg_statelen(drbg));
948*4882a593Smuzhiyun
949*4882a593Smuzhiyun drbg_string_fill(&data, src, drbg_statelen(drbg));
950*4882a593Smuzhiyun list_add_tail(&data.list, &datalist);
951*4882a593Smuzhiyun while (len < buflen) {
952*4882a593Smuzhiyun unsigned int outlen = 0;
953*4882a593Smuzhiyun /* 10.1.1.4 step hashgen 4.1 */
954*4882a593Smuzhiyun ret = drbg_kcapi_hash(drbg, dst, &datalist);
955*4882a593Smuzhiyun if (ret) {
956*4882a593Smuzhiyun len = ret;
957*4882a593Smuzhiyun goto out;
958*4882a593Smuzhiyun }
959*4882a593Smuzhiyun outlen = (drbg_blocklen(drbg) < (buflen - len)) ?
960*4882a593Smuzhiyun drbg_blocklen(drbg) : (buflen - len);
961*4882a593Smuzhiyun /* 10.1.1.4 step hashgen 4.2 */
962*4882a593Smuzhiyun memcpy(buf + len, dst, outlen);
963*4882a593Smuzhiyun len += outlen;
964*4882a593Smuzhiyun /* 10.1.1.4 hashgen step 4.3 */
965*4882a593Smuzhiyun if (len < buflen)
966*4882a593Smuzhiyun crypto_inc(src, drbg_statelen(drbg));
967*4882a593Smuzhiyun }
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun out:
970*4882a593Smuzhiyun memset(drbg->scratchpad, 0,
971*4882a593Smuzhiyun (drbg_statelen(drbg) + drbg_blocklen(drbg)));
972*4882a593Smuzhiyun return len;
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun /* generate function for Hash DRBG as defined in 10.1.1.4 */
drbg_hash_generate(struct drbg_state * drbg,unsigned char * buf,unsigned int buflen,struct list_head * addtl)976*4882a593Smuzhiyun static int drbg_hash_generate(struct drbg_state *drbg,
977*4882a593Smuzhiyun unsigned char *buf, unsigned int buflen,
978*4882a593Smuzhiyun struct list_head *addtl)
979*4882a593Smuzhiyun {
980*4882a593Smuzhiyun int len = 0;
981*4882a593Smuzhiyun int ret = 0;
982*4882a593Smuzhiyun union {
983*4882a593Smuzhiyun unsigned char req[8];
984*4882a593Smuzhiyun __be64 req_int;
985*4882a593Smuzhiyun } u;
986*4882a593Smuzhiyun unsigned char prefix = DRBG_PREFIX3;
987*4882a593Smuzhiyun struct drbg_string data1, data2;
988*4882a593Smuzhiyun LIST_HEAD(datalist);
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun /* 10.1.1.4 step 2 */
991*4882a593Smuzhiyun ret = drbg_hash_process_addtl(drbg, addtl);
992*4882a593Smuzhiyun if (ret)
993*4882a593Smuzhiyun return ret;
994*4882a593Smuzhiyun /* 10.1.1.4 step 3 */
995*4882a593Smuzhiyun len = drbg_hash_hashgen(drbg, buf, buflen);
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun /* this is the value H as documented in 10.1.1.4 */
998*4882a593Smuzhiyun /* 10.1.1.4 step 4 */
999*4882a593Smuzhiyun drbg_string_fill(&data1, &prefix, 1);
1000*4882a593Smuzhiyun list_add_tail(&data1.list, &datalist);
1001*4882a593Smuzhiyun drbg_string_fill(&data2, drbg->V, drbg_statelen(drbg));
1002*4882a593Smuzhiyun list_add_tail(&data2.list, &datalist);
1003*4882a593Smuzhiyun ret = drbg_kcapi_hash(drbg, drbg->scratchpad, &datalist);
1004*4882a593Smuzhiyun if (ret) {
1005*4882a593Smuzhiyun len = ret;
1006*4882a593Smuzhiyun goto out;
1007*4882a593Smuzhiyun }
1008*4882a593Smuzhiyun
1009*4882a593Smuzhiyun /* 10.1.1.4 step 5 */
1010*4882a593Smuzhiyun drbg_add_buf(drbg->V, drbg_statelen(drbg),
1011*4882a593Smuzhiyun drbg->scratchpad, drbg_blocklen(drbg));
1012*4882a593Smuzhiyun drbg_add_buf(drbg->V, drbg_statelen(drbg),
1013*4882a593Smuzhiyun drbg->C, drbg_statelen(drbg));
1014*4882a593Smuzhiyun u.req_int = cpu_to_be64(drbg->reseed_ctr);
1015*4882a593Smuzhiyun drbg_add_buf(drbg->V, drbg_statelen(drbg), u.req, 8);
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun out:
1018*4882a593Smuzhiyun memset(drbg->scratchpad, 0, drbg_blocklen(drbg));
1019*4882a593Smuzhiyun return len;
1020*4882a593Smuzhiyun }
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun /*
1023*4882a593Smuzhiyun * scratchpad usage: as update and generate are used isolated, both
1024*4882a593Smuzhiyun * can use the scratchpad
1025*4882a593Smuzhiyun */
1026*4882a593Smuzhiyun static const struct drbg_state_ops drbg_hash_ops = {
1027*4882a593Smuzhiyun .update = drbg_hash_update,
1028*4882a593Smuzhiyun .generate = drbg_hash_generate,
1029*4882a593Smuzhiyun .crypto_init = drbg_init_hash_kernel,
1030*4882a593Smuzhiyun .crypto_fini = drbg_fini_hash_kernel,
1031*4882a593Smuzhiyun };
1032*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_HASH */
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun /******************************************************************
1035*4882a593Smuzhiyun * Functions common for DRBG implementations
1036*4882a593Smuzhiyun ******************************************************************/
1037*4882a593Smuzhiyun
__drbg_seed(struct drbg_state * drbg,struct list_head * seed,int reseed,enum drbg_seed_state new_seed_state)1038*4882a593Smuzhiyun static inline int __drbg_seed(struct drbg_state *drbg, struct list_head *seed,
1039*4882a593Smuzhiyun int reseed, enum drbg_seed_state new_seed_state)
1040*4882a593Smuzhiyun {
1041*4882a593Smuzhiyun int ret = drbg->d_ops->update(drbg, seed, reseed);
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun if (ret)
1044*4882a593Smuzhiyun return ret;
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun drbg->seeded = new_seed_state;
1047*4882a593Smuzhiyun /* 10.1.1.2 / 10.1.1.3 step 5 */
1048*4882a593Smuzhiyun drbg->reseed_ctr = 1;
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun switch (drbg->seeded) {
1051*4882a593Smuzhiyun case DRBG_SEED_STATE_UNSEEDED:
1052*4882a593Smuzhiyun /* Impossible, but handle it to silence compiler warnings. */
1053*4882a593Smuzhiyun fallthrough;
1054*4882a593Smuzhiyun case DRBG_SEED_STATE_PARTIAL:
1055*4882a593Smuzhiyun /*
1056*4882a593Smuzhiyun * Require frequent reseeds until the seed source is
1057*4882a593Smuzhiyun * fully initialized.
1058*4882a593Smuzhiyun */
1059*4882a593Smuzhiyun drbg->reseed_threshold = 50;
1060*4882a593Smuzhiyun break;
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun case DRBG_SEED_STATE_FULL:
1063*4882a593Smuzhiyun /*
1064*4882a593Smuzhiyun * Seed source has become fully initialized, frequent
1065*4882a593Smuzhiyun * reseeds no longer required.
1066*4882a593Smuzhiyun */
1067*4882a593Smuzhiyun drbg->reseed_threshold = drbg_max_requests(drbg);
1068*4882a593Smuzhiyun break;
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun
1071*4882a593Smuzhiyun return ret;
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun
drbg_get_random_bytes(struct drbg_state * drbg,unsigned char * entropy,unsigned int entropylen)1074*4882a593Smuzhiyun static inline int drbg_get_random_bytes(struct drbg_state *drbg,
1075*4882a593Smuzhiyun unsigned char *entropy,
1076*4882a593Smuzhiyun unsigned int entropylen)
1077*4882a593Smuzhiyun {
1078*4882a593Smuzhiyun int ret;
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun do {
1081*4882a593Smuzhiyun get_random_bytes(entropy, entropylen);
1082*4882a593Smuzhiyun ret = drbg_fips_continuous_test(drbg, entropy);
1083*4882a593Smuzhiyun if (ret && ret != -EAGAIN)
1084*4882a593Smuzhiyun return ret;
1085*4882a593Smuzhiyun } while (ret);
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun return 0;
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun
drbg_seed_from_random(struct drbg_state * drbg)1090*4882a593Smuzhiyun static int drbg_seed_from_random(struct drbg_state *drbg)
1091*4882a593Smuzhiyun {
1092*4882a593Smuzhiyun struct drbg_string data;
1093*4882a593Smuzhiyun LIST_HEAD(seedlist);
1094*4882a593Smuzhiyun unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
1095*4882a593Smuzhiyun unsigned char entropy[32];
1096*4882a593Smuzhiyun int ret;
1097*4882a593Smuzhiyun
1098*4882a593Smuzhiyun BUG_ON(!entropylen);
1099*4882a593Smuzhiyun BUG_ON(entropylen > sizeof(entropy));
1100*4882a593Smuzhiyun
1101*4882a593Smuzhiyun drbg_string_fill(&data, entropy, entropylen);
1102*4882a593Smuzhiyun list_add_tail(&data.list, &seedlist);
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun ret = drbg_get_random_bytes(drbg, entropy, entropylen);
1105*4882a593Smuzhiyun if (ret)
1106*4882a593Smuzhiyun goto out;
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun ret = __drbg_seed(drbg, &seedlist, true, DRBG_SEED_STATE_FULL);
1109*4882a593Smuzhiyun
1110*4882a593Smuzhiyun out:
1111*4882a593Smuzhiyun memzero_explicit(entropy, entropylen);
1112*4882a593Smuzhiyun return ret;
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun /*
1116*4882a593Smuzhiyun * Seeding or reseeding of the DRBG
1117*4882a593Smuzhiyun *
1118*4882a593Smuzhiyun * @drbg: DRBG state struct
1119*4882a593Smuzhiyun * @pers: personalization / additional information buffer
1120*4882a593Smuzhiyun * @reseed: 0 for initial seed process, 1 for reseeding
1121*4882a593Smuzhiyun *
1122*4882a593Smuzhiyun * return:
1123*4882a593Smuzhiyun * 0 on success
1124*4882a593Smuzhiyun * error value otherwise
1125*4882a593Smuzhiyun */
drbg_seed(struct drbg_state * drbg,struct drbg_string * pers,bool reseed)1126*4882a593Smuzhiyun static int drbg_seed(struct drbg_state *drbg, struct drbg_string *pers,
1127*4882a593Smuzhiyun bool reseed)
1128*4882a593Smuzhiyun {
1129*4882a593Smuzhiyun int ret;
1130*4882a593Smuzhiyun unsigned char entropy[((32 + 16) * 2)];
1131*4882a593Smuzhiyun unsigned int entropylen = drbg_sec_strength(drbg->core->flags);
1132*4882a593Smuzhiyun struct drbg_string data1;
1133*4882a593Smuzhiyun LIST_HEAD(seedlist);
1134*4882a593Smuzhiyun enum drbg_seed_state new_seed_state = DRBG_SEED_STATE_FULL;
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun /* 9.1 / 9.2 / 9.3.1 step 3 */
1137*4882a593Smuzhiyun if (pers && pers->len > (drbg_max_addtl(drbg))) {
1138*4882a593Smuzhiyun pr_devel("DRBG: personalization string too long %zu\n",
1139*4882a593Smuzhiyun pers->len);
1140*4882a593Smuzhiyun return -EINVAL;
1141*4882a593Smuzhiyun }
1142*4882a593Smuzhiyun
1143*4882a593Smuzhiyun if (list_empty(&drbg->test_data.list)) {
1144*4882a593Smuzhiyun drbg_string_fill(&data1, drbg->test_data.buf,
1145*4882a593Smuzhiyun drbg->test_data.len);
1146*4882a593Smuzhiyun pr_devel("DRBG: using test entropy\n");
1147*4882a593Smuzhiyun } else {
1148*4882a593Smuzhiyun /*
1149*4882a593Smuzhiyun * Gather entropy equal to the security strength of the DRBG.
1150*4882a593Smuzhiyun * With a derivation function, a nonce is required in addition
1151*4882a593Smuzhiyun * to the entropy. A nonce must be at least 1/2 of the security
1152*4882a593Smuzhiyun * strength of the DRBG in size. Thus, entropy + nonce is 3/2
1153*4882a593Smuzhiyun * of the strength. The consideration of a nonce is only
1154*4882a593Smuzhiyun * applicable during initial seeding.
1155*4882a593Smuzhiyun */
1156*4882a593Smuzhiyun BUG_ON(!entropylen);
1157*4882a593Smuzhiyun if (!reseed)
1158*4882a593Smuzhiyun entropylen = ((entropylen + 1) / 2) * 3;
1159*4882a593Smuzhiyun BUG_ON((entropylen * 2) > sizeof(entropy));
1160*4882a593Smuzhiyun
1161*4882a593Smuzhiyun /* Get seed from in-kernel /dev/urandom */
1162*4882a593Smuzhiyun if (!rng_is_initialized())
1163*4882a593Smuzhiyun new_seed_state = DRBG_SEED_STATE_PARTIAL;
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun ret = drbg_get_random_bytes(drbg, entropy, entropylen);
1166*4882a593Smuzhiyun if (ret)
1167*4882a593Smuzhiyun goto out;
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun if (!drbg->jent) {
1170*4882a593Smuzhiyun drbg_string_fill(&data1, entropy, entropylen);
1171*4882a593Smuzhiyun pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
1172*4882a593Smuzhiyun entropylen);
1173*4882a593Smuzhiyun } else {
1174*4882a593Smuzhiyun /* Get seed from Jitter RNG */
1175*4882a593Smuzhiyun ret = crypto_rng_get_bytes(drbg->jent,
1176*4882a593Smuzhiyun entropy + entropylen,
1177*4882a593Smuzhiyun entropylen);
1178*4882a593Smuzhiyun if (ret) {
1179*4882a593Smuzhiyun pr_devel("DRBG: jent failed with %d\n", ret);
1180*4882a593Smuzhiyun
1181*4882a593Smuzhiyun /*
1182*4882a593Smuzhiyun * Do not treat the transient failure of the
1183*4882a593Smuzhiyun * Jitter RNG as an error that needs to be
1184*4882a593Smuzhiyun * reported. The combined number of the
1185*4882a593Smuzhiyun * maximum reseed threshold times the maximum
1186*4882a593Smuzhiyun * number of Jitter RNG transient errors is
1187*4882a593Smuzhiyun * less than the reseed threshold required by
1188*4882a593Smuzhiyun * SP800-90A allowing us to treat the
1189*4882a593Smuzhiyun * transient errors as such.
1190*4882a593Smuzhiyun *
1191*4882a593Smuzhiyun * However, we mandate that at least the first
1192*4882a593Smuzhiyun * seeding operation must succeed with the
1193*4882a593Smuzhiyun * Jitter RNG.
1194*4882a593Smuzhiyun */
1195*4882a593Smuzhiyun if (!reseed || ret != -EAGAIN)
1196*4882a593Smuzhiyun goto out;
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun drbg_string_fill(&data1, entropy, entropylen * 2);
1200*4882a593Smuzhiyun pr_devel("DRBG: (re)seeding with %u bytes of entropy\n",
1201*4882a593Smuzhiyun entropylen * 2);
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun list_add_tail(&data1.list, &seedlist);
1205*4882a593Smuzhiyun
1206*4882a593Smuzhiyun /*
1207*4882a593Smuzhiyun * concatenation of entropy with personalization str / addtl input)
1208*4882a593Smuzhiyun * the variable pers is directly handed in by the caller, so check its
1209*4882a593Smuzhiyun * contents whether it is appropriate
1210*4882a593Smuzhiyun */
1211*4882a593Smuzhiyun if (pers && pers->buf && 0 < pers->len) {
1212*4882a593Smuzhiyun list_add_tail(&pers->list, &seedlist);
1213*4882a593Smuzhiyun pr_devel("DRBG: using personalization string\n");
1214*4882a593Smuzhiyun }
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun if (!reseed) {
1217*4882a593Smuzhiyun memset(drbg->V, 0, drbg_statelen(drbg));
1218*4882a593Smuzhiyun memset(drbg->C, 0, drbg_statelen(drbg));
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun
1221*4882a593Smuzhiyun ret = __drbg_seed(drbg, &seedlist, reseed, new_seed_state);
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun out:
1224*4882a593Smuzhiyun memzero_explicit(entropy, entropylen * 2);
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun return ret;
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun /* Free all substructures in a DRBG state without the DRBG state structure */
drbg_dealloc_state(struct drbg_state * drbg)1230*4882a593Smuzhiyun static inline void drbg_dealloc_state(struct drbg_state *drbg)
1231*4882a593Smuzhiyun {
1232*4882a593Smuzhiyun if (!drbg)
1233*4882a593Smuzhiyun return;
1234*4882a593Smuzhiyun kfree_sensitive(drbg->Vbuf);
1235*4882a593Smuzhiyun drbg->Vbuf = NULL;
1236*4882a593Smuzhiyun drbg->V = NULL;
1237*4882a593Smuzhiyun kfree_sensitive(drbg->Cbuf);
1238*4882a593Smuzhiyun drbg->Cbuf = NULL;
1239*4882a593Smuzhiyun drbg->C = NULL;
1240*4882a593Smuzhiyun kfree_sensitive(drbg->scratchpadbuf);
1241*4882a593Smuzhiyun drbg->scratchpadbuf = NULL;
1242*4882a593Smuzhiyun drbg->reseed_ctr = 0;
1243*4882a593Smuzhiyun drbg->d_ops = NULL;
1244*4882a593Smuzhiyun drbg->core = NULL;
1245*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) {
1246*4882a593Smuzhiyun kfree_sensitive(drbg->prev);
1247*4882a593Smuzhiyun drbg->prev = NULL;
1248*4882a593Smuzhiyun drbg->fips_primed = false;
1249*4882a593Smuzhiyun }
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun /*
1253*4882a593Smuzhiyun * Allocate all sub-structures for a DRBG state.
1254*4882a593Smuzhiyun * The DRBG state structure must already be allocated.
1255*4882a593Smuzhiyun */
drbg_alloc_state(struct drbg_state * drbg)1256*4882a593Smuzhiyun static inline int drbg_alloc_state(struct drbg_state *drbg)
1257*4882a593Smuzhiyun {
1258*4882a593Smuzhiyun int ret = -ENOMEM;
1259*4882a593Smuzhiyun unsigned int sb_size = 0;
1260*4882a593Smuzhiyun
1261*4882a593Smuzhiyun switch (drbg->core->flags & DRBG_TYPE_MASK) {
1262*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_HMAC
1263*4882a593Smuzhiyun case DRBG_HMAC:
1264*4882a593Smuzhiyun drbg->d_ops = &drbg_hmac_ops;
1265*4882a593Smuzhiyun break;
1266*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_HMAC */
1267*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_HASH
1268*4882a593Smuzhiyun case DRBG_HASH:
1269*4882a593Smuzhiyun drbg->d_ops = &drbg_hash_ops;
1270*4882a593Smuzhiyun break;
1271*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_HASH */
1272*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_CTR
1273*4882a593Smuzhiyun case DRBG_CTR:
1274*4882a593Smuzhiyun drbg->d_ops = &drbg_ctr_ops;
1275*4882a593Smuzhiyun break;
1276*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_CTR */
1277*4882a593Smuzhiyun default:
1278*4882a593Smuzhiyun ret = -EOPNOTSUPP;
1279*4882a593Smuzhiyun goto err;
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun ret = drbg->d_ops->crypto_init(drbg);
1283*4882a593Smuzhiyun if (ret < 0)
1284*4882a593Smuzhiyun goto err;
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun drbg->Vbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL);
1287*4882a593Smuzhiyun if (!drbg->Vbuf) {
1288*4882a593Smuzhiyun ret = -ENOMEM;
1289*4882a593Smuzhiyun goto fini;
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun drbg->V = PTR_ALIGN(drbg->Vbuf, ret + 1);
1292*4882a593Smuzhiyun drbg->Cbuf = kmalloc(drbg_statelen(drbg) + ret, GFP_KERNEL);
1293*4882a593Smuzhiyun if (!drbg->Cbuf) {
1294*4882a593Smuzhiyun ret = -ENOMEM;
1295*4882a593Smuzhiyun goto fini;
1296*4882a593Smuzhiyun }
1297*4882a593Smuzhiyun drbg->C = PTR_ALIGN(drbg->Cbuf, ret + 1);
1298*4882a593Smuzhiyun /* scratchpad is only generated for CTR and Hash */
1299*4882a593Smuzhiyun if (drbg->core->flags & DRBG_HMAC)
1300*4882a593Smuzhiyun sb_size = 0;
1301*4882a593Smuzhiyun else if (drbg->core->flags & DRBG_CTR)
1302*4882a593Smuzhiyun sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg) + /* temp */
1303*4882a593Smuzhiyun drbg_statelen(drbg) + /* df_data */
1304*4882a593Smuzhiyun drbg_blocklen(drbg) + /* pad */
1305*4882a593Smuzhiyun drbg_blocklen(drbg) + /* iv */
1306*4882a593Smuzhiyun drbg_statelen(drbg) + drbg_blocklen(drbg); /* temp */
1307*4882a593Smuzhiyun else
1308*4882a593Smuzhiyun sb_size = drbg_statelen(drbg) + drbg_blocklen(drbg);
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun if (0 < sb_size) {
1311*4882a593Smuzhiyun drbg->scratchpadbuf = kzalloc(sb_size + ret, GFP_KERNEL);
1312*4882a593Smuzhiyun if (!drbg->scratchpadbuf) {
1313*4882a593Smuzhiyun ret = -ENOMEM;
1314*4882a593Smuzhiyun goto fini;
1315*4882a593Smuzhiyun }
1316*4882a593Smuzhiyun drbg->scratchpad = PTR_ALIGN(drbg->scratchpadbuf, ret + 1);
1317*4882a593Smuzhiyun }
1318*4882a593Smuzhiyun
1319*4882a593Smuzhiyun if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) {
1320*4882a593Smuzhiyun drbg->prev = kzalloc(drbg_sec_strength(drbg->core->flags),
1321*4882a593Smuzhiyun GFP_KERNEL);
1322*4882a593Smuzhiyun if (!drbg->prev) {
1323*4882a593Smuzhiyun ret = -ENOMEM;
1324*4882a593Smuzhiyun goto fini;
1325*4882a593Smuzhiyun }
1326*4882a593Smuzhiyun drbg->fips_primed = false;
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun return 0;
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun fini:
1332*4882a593Smuzhiyun drbg->d_ops->crypto_fini(drbg);
1333*4882a593Smuzhiyun err:
1334*4882a593Smuzhiyun drbg_dealloc_state(drbg);
1335*4882a593Smuzhiyun return ret;
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun /*************************************************************************
1339*4882a593Smuzhiyun * DRBG interface functions
1340*4882a593Smuzhiyun *************************************************************************/
1341*4882a593Smuzhiyun
1342*4882a593Smuzhiyun /*
1343*4882a593Smuzhiyun * DRBG generate function as required by SP800-90A - this function
1344*4882a593Smuzhiyun * generates random numbers
1345*4882a593Smuzhiyun *
1346*4882a593Smuzhiyun * @drbg DRBG state handle
1347*4882a593Smuzhiyun * @buf Buffer where to store the random numbers -- the buffer must already
1348*4882a593Smuzhiyun * be pre-allocated by caller
1349*4882a593Smuzhiyun * @buflen Length of output buffer - this value defines the number of random
1350*4882a593Smuzhiyun * bytes pulled from DRBG
1351*4882a593Smuzhiyun * @addtl Additional input that is mixed into state, may be NULL -- note
1352*4882a593Smuzhiyun * the entropy is pulled by the DRBG internally unconditionally
1353*4882a593Smuzhiyun * as defined in SP800-90A. The additional input is mixed into
1354*4882a593Smuzhiyun * the state in addition to the pulled entropy.
1355*4882a593Smuzhiyun *
1356*4882a593Smuzhiyun * return: 0 when all bytes are generated; < 0 in case of an error
1357*4882a593Smuzhiyun */
drbg_generate(struct drbg_state * drbg,unsigned char * buf,unsigned int buflen,struct drbg_string * addtl)1358*4882a593Smuzhiyun static int drbg_generate(struct drbg_state *drbg,
1359*4882a593Smuzhiyun unsigned char *buf, unsigned int buflen,
1360*4882a593Smuzhiyun struct drbg_string *addtl)
1361*4882a593Smuzhiyun {
1362*4882a593Smuzhiyun int len = 0;
1363*4882a593Smuzhiyun LIST_HEAD(addtllist);
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun if (!drbg->core) {
1366*4882a593Smuzhiyun pr_devel("DRBG: not yet seeded\n");
1367*4882a593Smuzhiyun return -EINVAL;
1368*4882a593Smuzhiyun }
1369*4882a593Smuzhiyun if (0 == buflen || !buf) {
1370*4882a593Smuzhiyun pr_devel("DRBG: no output buffer provided\n");
1371*4882a593Smuzhiyun return -EINVAL;
1372*4882a593Smuzhiyun }
1373*4882a593Smuzhiyun if (addtl && NULL == addtl->buf && 0 < addtl->len) {
1374*4882a593Smuzhiyun pr_devel("DRBG: wrong format of additional information\n");
1375*4882a593Smuzhiyun return -EINVAL;
1376*4882a593Smuzhiyun }
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun /* 9.3.1 step 2 */
1379*4882a593Smuzhiyun len = -EINVAL;
1380*4882a593Smuzhiyun if (buflen > (drbg_max_request_bytes(drbg))) {
1381*4882a593Smuzhiyun pr_devel("DRBG: requested random numbers too large %u\n",
1382*4882a593Smuzhiyun buflen);
1383*4882a593Smuzhiyun goto err;
1384*4882a593Smuzhiyun }
1385*4882a593Smuzhiyun
1386*4882a593Smuzhiyun /* 9.3.1 step 3 is implicit with the chosen DRBG */
1387*4882a593Smuzhiyun
1388*4882a593Smuzhiyun /* 9.3.1 step 4 */
1389*4882a593Smuzhiyun if (addtl && addtl->len > (drbg_max_addtl(drbg))) {
1390*4882a593Smuzhiyun pr_devel("DRBG: additional information string too long %zu\n",
1391*4882a593Smuzhiyun addtl->len);
1392*4882a593Smuzhiyun goto err;
1393*4882a593Smuzhiyun }
1394*4882a593Smuzhiyun /* 9.3.1 step 5 is implicit with the chosen DRBG */
1395*4882a593Smuzhiyun
1396*4882a593Smuzhiyun /*
1397*4882a593Smuzhiyun * 9.3.1 step 6 and 9 supplemented by 9.3.2 step c is implemented
1398*4882a593Smuzhiyun * here. The spec is a bit convoluted here, we make it simpler.
1399*4882a593Smuzhiyun */
1400*4882a593Smuzhiyun if (drbg->reseed_threshold < drbg->reseed_ctr)
1401*4882a593Smuzhiyun drbg->seeded = DRBG_SEED_STATE_UNSEEDED;
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun if (drbg->pr || drbg->seeded == DRBG_SEED_STATE_UNSEEDED) {
1404*4882a593Smuzhiyun pr_devel("DRBG: reseeding before generation (prediction "
1405*4882a593Smuzhiyun "resistance: %s, state %s)\n",
1406*4882a593Smuzhiyun drbg->pr ? "true" : "false",
1407*4882a593Smuzhiyun (drbg->seeded == DRBG_SEED_STATE_FULL ?
1408*4882a593Smuzhiyun "seeded" : "unseeded"));
1409*4882a593Smuzhiyun /* 9.3.1 steps 7.1 through 7.3 */
1410*4882a593Smuzhiyun len = drbg_seed(drbg, addtl, true);
1411*4882a593Smuzhiyun if (len)
1412*4882a593Smuzhiyun goto err;
1413*4882a593Smuzhiyun /* 9.3.1 step 7.4 */
1414*4882a593Smuzhiyun addtl = NULL;
1415*4882a593Smuzhiyun } else if (rng_is_initialized() &&
1416*4882a593Smuzhiyun drbg->seeded == DRBG_SEED_STATE_PARTIAL) {
1417*4882a593Smuzhiyun len = drbg_seed_from_random(drbg);
1418*4882a593Smuzhiyun if (len)
1419*4882a593Smuzhiyun goto err;
1420*4882a593Smuzhiyun }
1421*4882a593Smuzhiyun
1422*4882a593Smuzhiyun if (addtl && 0 < addtl->len)
1423*4882a593Smuzhiyun list_add_tail(&addtl->list, &addtllist);
1424*4882a593Smuzhiyun /* 9.3.1 step 8 and 10 */
1425*4882a593Smuzhiyun len = drbg->d_ops->generate(drbg, buf, buflen, &addtllist);
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun /* 10.1.1.4 step 6, 10.1.2.5 step 7, 10.2.1.5.2 step 7 */
1428*4882a593Smuzhiyun drbg->reseed_ctr++;
1429*4882a593Smuzhiyun if (0 >= len)
1430*4882a593Smuzhiyun goto err;
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun /*
1433*4882a593Smuzhiyun * Section 11.3.3 requires to re-perform self tests after some
1434*4882a593Smuzhiyun * generated random numbers. The chosen value after which self
1435*4882a593Smuzhiyun * test is performed is arbitrary, but it should be reasonable.
1436*4882a593Smuzhiyun * However, we do not perform the self tests because of the following
1437*4882a593Smuzhiyun * reasons: it is mathematically impossible that the initial self tests
1438*4882a593Smuzhiyun * were successfully and the following are not. If the initial would
1439*4882a593Smuzhiyun * pass and the following would not, the kernel integrity is violated.
1440*4882a593Smuzhiyun * In this case, the entire kernel operation is questionable and it
1441*4882a593Smuzhiyun * is unlikely that the integrity violation only affects the
1442*4882a593Smuzhiyun * correct operation of the DRBG.
1443*4882a593Smuzhiyun *
1444*4882a593Smuzhiyun * Albeit the following code is commented out, it is provided in
1445*4882a593Smuzhiyun * case somebody has a need to implement the test of 11.3.3.
1446*4882a593Smuzhiyun */
1447*4882a593Smuzhiyun #if 0
1448*4882a593Smuzhiyun if (drbg->reseed_ctr && !(drbg->reseed_ctr % 4096)) {
1449*4882a593Smuzhiyun int err = 0;
1450*4882a593Smuzhiyun pr_devel("DRBG: start to perform self test\n");
1451*4882a593Smuzhiyun if (drbg->core->flags & DRBG_HMAC)
1452*4882a593Smuzhiyun err = alg_test("drbg_pr_hmac_sha256",
1453*4882a593Smuzhiyun "drbg_pr_hmac_sha256", 0, 0);
1454*4882a593Smuzhiyun else if (drbg->core->flags & DRBG_CTR)
1455*4882a593Smuzhiyun err = alg_test("drbg_pr_ctr_aes128",
1456*4882a593Smuzhiyun "drbg_pr_ctr_aes128", 0, 0);
1457*4882a593Smuzhiyun else
1458*4882a593Smuzhiyun err = alg_test("drbg_pr_sha256",
1459*4882a593Smuzhiyun "drbg_pr_sha256", 0, 0);
1460*4882a593Smuzhiyun if (err) {
1461*4882a593Smuzhiyun pr_err("DRBG: periodical self test failed\n");
1462*4882a593Smuzhiyun /*
1463*4882a593Smuzhiyun * uninstantiate implies that from now on, only errors
1464*4882a593Smuzhiyun * are returned when reusing this DRBG cipher handle
1465*4882a593Smuzhiyun */
1466*4882a593Smuzhiyun drbg_uninstantiate(drbg);
1467*4882a593Smuzhiyun return 0;
1468*4882a593Smuzhiyun } else {
1469*4882a593Smuzhiyun pr_devel("DRBG: self test successful\n");
1470*4882a593Smuzhiyun }
1471*4882a593Smuzhiyun }
1472*4882a593Smuzhiyun #endif
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun /*
1475*4882a593Smuzhiyun * All operations were successful, return 0 as mandated by
1476*4882a593Smuzhiyun * the kernel crypto API interface.
1477*4882a593Smuzhiyun */
1478*4882a593Smuzhiyun len = 0;
1479*4882a593Smuzhiyun err:
1480*4882a593Smuzhiyun return len;
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun
1483*4882a593Smuzhiyun /*
1484*4882a593Smuzhiyun * Wrapper around drbg_generate which can pull arbitrary long strings
1485*4882a593Smuzhiyun * from the DRBG without hitting the maximum request limitation.
1486*4882a593Smuzhiyun *
1487*4882a593Smuzhiyun * Parameters: see drbg_generate
1488*4882a593Smuzhiyun * Return codes: see drbg_generate -- if one drbg_generate request fails,
1489*4882a593Smuzhiyun * the entire drbg_generate_long request fails
1490*4882a593Smuzhiyun */
drbg_generate_long(struct drbg_state * drbg,unsigned char * buf,unsigned int buflen,struct drbg_string * addtl)1491*4882a593Smuzhiyun static int drbg_generate_long(struct drbg_state *drbg,
1492*4882a593Smuzhiyun unsigned char *buf, unsigned int buflen,
1493*4882a593Smuzhiyun struct drbg_string *addtl)
1494*4882a593Smuzhiyun {
1495*4882a593Smuzhiyun unsigned int len = 0;
1496*4882a593Smuzhiyun unsigned int slice = 0;
1497*4882a593Smuzhiyun do {
1498*4882a593Smuzhiyun int err = 0;
1499*4882a593Smuzhiyun unsigned int chunk = 0;
1500*4882a593Smuzhiyun slice = ((buflen - len) / drbg_max_request_bytes(drbg));
1501*4882a593Smuzhiyun chunk = slice ? drbg_max_request_bytes(drbg) : (buflen - len);
1502*4882a593Smuzhiyun mutex_lock(&drbg->drbg_mutex);
1503*4882a593Smuzhiyun err = drbg_generate(drbg, buf + len, chunk, addtl);
1504*4882a593Smuzhiyun mutex_unlock(&drbg->drbg_mutex);
1505*4882a593Smuzhiyun if (0 > err)
1506*4882a593Smuzhiyun return err;
1507*4882a593Smuzhiyun len += chunk;
1508*4882a593Smuzhiyun } while (slice > 0 && (len < buflen));
1509*4882a593Smuzhiyun return 0;
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun
drbg_prepare_hrng(struct drbg_state * drbg)1512*4882a593Smuzhiyun static int drbg_prepare_hrng(struct drbg_state *drbg)
1513*4882a593Smuzhiyun {
1514*4882a593Smuzhiyun /* We do not need an HRNG in test mode. */
1515*4882a593Smuzhiyun if (list_empty(&drbg->test_data.list))
1516*4882a593Smuzhiyun return 0;
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun drbg->jent = crypto_alloc_rng("jitterentropy_rng", 0, 0);
1519*4882a593Smuzhiyun
1520*4882a593Smuzhiyun return 0;
1521*4882a593Smuzhiyun }
1522*4882a593Smuzhiyun
1523*4882a593Smuzhiyun /*
1524*4882a593Smuzhiyun * DRBG instantiation function as required by SP800-90A - this function
1525*4882a593Smuzhiyun * sets up the DRBG handle, performs the initial seeding and all sanity
1526*4882a593Smuzhiyun * checks required by SP800-90A
1527*4882a593Smuzhiyun *
1528*4882a593Smuzhiyun * @drbg memory of state -- if NULL, new memory is allocated
1529*4882a593Smuzhiyun * @pers Personalization string that is mixed into state, may be NULL -- note
1530*4882a593Smuzhiyun * the entropy is pulled by the DRBG internally unconditionally
1531*4882a593Smuzhiyun * as defined in SP800-90A. The additional input is mixed into
1532*4882a593Smuzhiyun * the state in addition to the pulled entropy.
1533*4882a593Smuzhiyun * @coreref reference to core
1534*4882a593Smuzhiyun * @pr prediction resistance enabled
1535*4882a593Smuzhiyun *
1536*4882a593Smuzhiyun * return
1537*4882a593Smuzhiyun * 0 on success
1538*4882a593Smuzhiyun * error value otherwise
1539*4882a593Smuzhiyun */
drbg_instantiate(struct drbg_state * drbg,struct drbg_string * pers,int coreref,bool pr)1540*4882a593Smuzhiyun static int drbg_instantiate(struct drbg_state *drbg, struct drbg_string *pers,
1541*4882a593Smuzhiyun int coreref, bool pr)
1542*4882a593Smuzhiyun {
1543*4882a593Smuzhiyun int ret;
1544*4882a593Smuzhiyun bool reseed = true;
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun pr_devel("DRBG: Initializing DRBG core %d with prediction resistance "
1547*4882a593Smuzhiyun "%s\n", coreref, pr ? "enabled" : "disabled");
1548*4882a593Smuzhiyun mutex_lock(&drbg->drbg_mutex);
1549*4882a593Smuzhiyun
1550*4882a593Smuzhiyun /* 9.1 step 1 is implicit with the selected DRBG type */
1551*4882a593Smuzhiyun
1552*4882a593Smuzhiyun /*
1553*4882a593Smuzhiyun * 9.1 step 2 is implicit as caller can select prediction resistance
1554*4882a593Smuzhiyun * and the flag is copied into drbg->flags --
1555*4882a593Smuzhiyun * all DRBG types support prediction resistance
1556*4882a593Smuzhiyun */
1557*4882a593Smuzhiyun
1558*4882a593Smuzhiyun /* 9.1 step 4 is implicit in drbg_sec_strength */
1559*4882a593Smuzhiyun
1560*4882a593Smuzhiyun if (!drbg->core) {
1561*4882a593Smuzhiyun drbg->core = &drbg_cores[coreref];
1562*4882a593Smuzhiyun drbg->pr = pr;
1563*4882a593Smuzhiyun drbg->seeded = DRBG_SEED_STATE_UNSEEDED;
1564*4882a593Smuzhiyun drbg->reseed_threshold = drbg_max_requests(drbg);
1565*4882a593Smuzhiyun
1566*4882a593Smuzhiyun ret = drbg_alloc_state(drbg);
1567*4882a593Smuzhiyun if (ret)
1568*4882a593Smuzhiyun goto unlock;
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun ret = drbg_prepare_hrng(drbg);
1571*4882a593Smuzhiyun if (ret)
1572*4882a593Smuzhiyun goto free_everything;
1573*4882a593Smuzhiyun
1574*4882a593Smuzhiyun if (IS_ERR(drbg->jent)) {
1575*4882a593Smuzhiyun ret = PTR_ERR(drbg->jent);
1576*4882a593Smuzhiyun drbg->jent = NULL;
1577*4882a593Smuzhiyun if (fips_enabled || ret != -ENOENT)
1578*4882a593Smuzhiyun goto free_everything;
1579*4882a593Smuzhiyun pr_info("DRBG: Continuing without Jitter RNG\n");
1580*4882a593Smuzhiyun }
1581*4882a593Smuzhiyun
1582*4882a593Smuzhiyun reseed = false;
1583*4882a593Smuzhiyun }
1584*4882a593Smuzhiyun
1585*4882a593Smuzhiyun ret = drbg_seed(drbg, pers, reseed);
1586*4882a593Smuzhiyun
1587*4882a593Smuzhiyun if (ret && !reseed)
1588*4882a593Smuzhiyun goto free_everything;
1589*4882a593Smuzhiyun
1590*4882a593Smuzhiyun mutex_unlock(&drbg->drbg_mutex);
1591*4882a593Smuzhiyun return ret;
1592*4882a593Smuzhiyun
1593*4882a593Smuzhiyun unlock:
1594*4882a593Smuzhiyun mutex_unlock(&drbg->drbg_mutex);
1595*4882a593Smuzhiyun return ret;
1596*4882a593Smuzhiyun
1597*4882a593Smuzhiyun free_everything:
1598*4882a593Smuzhiyun mutex_unlock(&drbg->drbg_mutex);
1599*4882a593Smuzhiyun drbg_uninstantiate(drbg);
1600*4882a593Smuzhiyun return ret;
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun
1603*4882a593Smuzhiyun /*
1604*4882a593Smuzhiyun * DRBG uninstantiate function as required by SP800-90A - this function
1605*4882a593Smuzhiyun * frees all buffers and the DRBG handle
1606*4882a593Smuzhiyun *
1607*4882a593Smuzhiyun * @drbg DRBG state handle
1608*4882a593Smuzhiyun *
1609*4882a593Smuzhiyun * return
1610*4882a593Smuzhiyun * 0 on success
1611*4882a593Smuzhiyun */
drbg_uninstantiate(struct drbg_state * drbg)1612*4882a593Smuzhiyun static int drbg_uninstantiate(struct drbg_state *drbg)
1613*4882a593Smuzhiyun {
1614*4882a593Smuzhiyun if (!IS_ERR_OR_NULL(drbg->jent))
1615*4882a593Smuzhiyun crypto_free_rng(drbg->jent);
1616*4882a593Smuzhiyun drbg->jent = NULL;
1617*4882a593Smuzhiyun
1618*4882a593Smuzhiyun if (drbg->d_ops)
1619*4882a593Smuzhiyun drbg->d_ops->crypto_fini(drbg);
1620*4882a593Smuzhiyun drbg_dealloc_state(drbg);
1621*4882a593Smuzhiyun /* no scrubbing of test_data -- this shall survive an uninstantiate */
1622*4882a593Smuzhiyun return 0;
1623*4882a593Smuzhiyun }
1624*4882a593Smuzhiyun
1625*4882a593Smuzhiyun /*
1626*4882a593Smuzhiyun * Helper function for setting the test data in the DRBG
1627*4882a593Smuzhiyun *
1628*4882a593Smuzhiyun * @drbg DRBG state handle
1629*4882a593Smuzhiyun * @data test data
1630*4882a593Smuzhiyun * @len test data length
1631*4882a593Smuzhiyun */
drbg_kcapi_set_entropy(struct crypto_rng * tfm,const u8 * data,unsigned int len)1632*4882a593Smuzhiyun static void drbg_kcapi_set_entropy(struct crypto_rng *tfm,
1633*4882a593Smuzhiyun const u8 *data, unsigned int len)
1634*4882a593Smuzhiyun {
1635*4882a593Smuzhiyun struct drbg_state *drbg = crypto_rng_ctx(tfm);
1636*4882a593Smuzhiyun
1637*4882a593Smuzhiyun mutex_lock(&drbg->drbg_mutex);
1638*4882a593Smuzhiyun drbg_string_fill(&drbg->test_data, data, len);
1639*4882a593Smuzhiyun mutex_unlock(&drbg->drbg_mutex);
1640*4882a593Smuzhiyun }
1641*4882a593Smuzhiyun
1642*4882a593Smuzhiyun /***************************************************************
1643*4882a593Smuzhiyun * Kernel crypto API cipher invocations requested by DRBG
1644*4882a593Smuzhiyun ***************************************************************/
1645*4882a593Smuzhiyun
1646*4882a593Smuzhiyun #if defined(CONFIG_CRYPTO_DRBG_HASH) || defined(CONFIG_CRYPTO_DRBG_HMAC)
1647*4882a593Smuzhiyun struct sdesc {
1648*4882a593Smuzhiyun struct shash_desc shash;
1649*4882a593Smuzhiyun char ctx[];
1650*4882a593Smuzhiyun };
1651*4882a593Smuzhiyun
drbg_init_hash_kernel(struct drbg_state * drbg)1652*4882a593Smuzhiyun static int drbg_init_hash_kernel(struct drbg_state *drbg)
1653*4882a593Smuzhiyun {
1654*4882a593Smuzhiyun struct sdesc *sdesc;
1655*4882a593Smuzhiyun struct crypto_shash *tfm;
1656*4882a593Smuzhiyun
1657*4882a593Smuzhiyun tfm = crypto_alloc_shash(drbg->core->backend_cra_name, 0, 0);
1658*4882a593Smuzhiyun if (IS_ERR(tfm)) {
1659*4882a593Smuzhiyun pr_info("DRBG: could not allocate digest TFM handle: %s\n",
1660*4882a593Smuzhiyun drbg->core->backend_cra_name);
1661*4882a593Smuzhiyun return PTR_ERR(tfm);
1662*4882a593Smuzhiyun }
1663*4882a593Smuzhiyun BUG_ON(drbg_blocklen(drbg) != crypto_shash_digestsize(tfm));
1664*4882a593Smuzhiyun sdesc = kzalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
1665*4882a593Smuzhiyun GFP_KERNEL);
1666*4882a593Smuzhiyun if (!sdesc) {
1667*4882a593Smuzhiyun crypto_free_shash(tfm);
1668*4882a593Smuzhiyun return -ENOMEM;
1669*4882a593Smuzhiyun }
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun sdesc->shash.tfm = tfm;
1672*4882a593Smuzhiyun drbg->priv_data = sdesc;
1673*4882a593Smuzhiyun
1674*4882a593Smuzhiyun return crypto_shash_alignmask(tfm);
1675*4882a593Smuzhiyun }
1676*4882a593Smuzhiyun
drbg_fini_hash_kernel(struct drbg_state * drbg)1677*4882a593Smuzhiyun static int drbg_fini_hash_kernel(struct drbg_state *drbg)
1678*4882a593Smuzhiyun {
1679*4882a593Smuzhiyun struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1680*4882a593Smuzhiyun if (sdesc) {
1681*4882a593Smuzhiyun crypto_free_shash(sdesc->shash.tfm);
1682*4882a593Smuzhiyun kfree_sensitive(sdesc);
1683*4882a593Smuzhiyun }
1684*4882a593Smuzhiyun drbg->priv_data = NULL;
1685*4882a593Smuzhiyun return 0;
1686*4882a593Smuzhiyun }
1687*4882a593Smuzhiyun
drbg_kcapi_hmacsetkey(struct drbg_state * drbg,const unsigned char * key)1688*4882a593Smuzhiyun static void drbg_kcapi_hmacsetkey(struct drbg_state *drbg,
1689*4882a593Smuzhiyun const unsigned char *key)
1690*4882a593Smuzhiyun {
1691*4882a593Smuzhiyun struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1692*4882a593Smuzhiyun
1693*4882a593Smuzhiyun crypto_shash_setkey(sdesc->shash.tfm, key, drbg_statelen(drbg));
1694*4882a593Smuzhiyun }
1695*4882a593Smuzhiyun
drbg_kcapi_hash(struct drbg_state * drbg,unsigned char * outval,const struct list_head * in)1696*4882a593Smuzhiyun static int drbg_kcapi_hash(struct drbg_state *drbg, unsigned char *outval,
1697*4882a593Smuzhiyun const struct list_head *in)
1698*4882a593Smuzhiyun {
1699*4882a593Smuzhiyun struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
1700*4882a593Smuzhiyun struct drbg_string *input = NULL;
1701*4882a593Smuzhiyun
1702*4882a593Smuzhiyun crypto_shash_init(&sdesc->shash);
1703*4882a593Smuzhiyun list_for_each_entry(input, in, list)
1704*4882a593Smuzhiyun crypto_shash_update(&sdesc->shash, input->buf, input->len);
1705*4882a593Smuzhiyun return crypto_shash_final(&sdesc->shash, outval);
1706*4882a593Smuzhiyun }
1707*4882a593Smuzhiyun #endif /* (CONFIG_CRYPTO_DRBG_HASH || CONFIG_CRYPTO_DRBG_HMAC) */
1708*4882a593Smuzhiyun
1709*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_CTR
drbg_fini_sym_kernel(struct drbg_state * drbg)1710*4882a593Smuzhiyun static int drbg_fini_sym_kernel(struct drbg_state *drbg)
1711*4882a593Smuzhiyun {
1712*4882a593Smuzhiyun struct crypto_cipher *tfm =
1713*4882a593Smuzhiyun (struct crypto_cipher *)drbg->priv_data;
1714*4882a593Smuzhiyun if (tfm)
1715*4882a593Smuzhiyun crypto_free_cipher(tfm);
1716*4882a593Smuzhiyun drbg->priv_data = NULL;
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun if (drbg->ctr_handle)
1719*4882a593Smuzhiyun crypto_free_skcipher(drbg->ctr_handle);
1720*4882a593Smuzhiyun drbg->ctr_handle = NULL;
1721*4882a593Smuzhiyun
1722*4882a593Smuzhiyun if (drbg->ctr_req)
1723*4882a593Smuzhiyun skcipher_request_free(drbg->ctr_req);
1724*4882a593Smuzhiyun drbg->ctr_req = NULL;
1725*4882a593Smuzhiyun
1726*4882a593Smuzhiyun kfree(drbg->outscratchpadbuf);
1727*4882a593Smuzhiyun drbg->outscratchpadbuf = NULL;
1728*4882a593Smuzhiyun
1729*4882a593Smuzhiyun return 0;
1730*4882a593Smuzhiyun }
1731*4882a593Smuzhiyun
drbg_init_sym_kernel(struct drbg_state * drbg)1732*4882a593Smuzhiyun static int drbg_init_sym_kernel(struct drbg_state *drbg)
1733*4882a593Smuzhiyun {
1734*4882a593Smuzhiyun struct crypto_cipher *tfm;
1735*4882a593Smuzhiyun struct crypto_skcipher *sk_tfm;
1736*4882a593Smuzhiyun struct skcipher_request *req;
1737*4882a593Smuzhiyun unsigned int alignmask;
1738*4882a593Smuzhiyun char ctr_name[CRYPTO_MAX_ALG_NAME];
1739*4882a593Smuzhiyun
1740*4882a593Smuzhiyun tfm = crypto_alloc_cipher(drbg->core->backend_cra_name, 0, 0);
1741*4882a593Smuzhiyun if (IS_ERR(tfm)) {
1742*4882a593Smuzhiyun pr_info("DRBG: could not allocate cipher TFM handle: %s\n",
1743*4882a593Smuzhiyun drbg->core->backend_cra_name);
1744*4882a593Smuzhiyun return PTR_ERR(tfm);
1745*4882a593Smuzhiyun }
1746*4882a593Smuzhiyun BUG_ON(drbg_blocklen(drbg) != crypto_cipher_blocksize(tfm));
1747*4882a593Smuzhiyun drbg->priv_data = tfm;
1748*4882a593Smuzhiyun
1749*4882a593Smuzhiyun if (snprintf(ctr_name, CRYPTO_MAX_ALG_NAME, "ctr(%s)",
1750*4882a593Smuzhiyun drbg->core->backend_cra_name) >= CRYPTO_MAX_ALG_NAME) {
1751*4882a593Smuzhiyun drbg_fini_sym_kernel(drbg);
1752*4882a593Smuzhiyun return -EINVAL;
1753*4882a593Smuzhiyun }
1754*4882a593Smuzhiyun sk_tfm = crypto_alloc_skcipher(ctr_name, 0, 0);
1755*4882a593Smuzhiyun if (IS_ERR(sk_tfm)) {
1756*4882a593Smuzhiyun pr_info("DRBG: could not allocate CTR cipher TFM handle: %s\n",
1757*4882a593Smuzhiyun ctr_name);
1758*4882a593Smuzhiyun drbg_fini_sym_kernel(drbg);
1759*4882a593Smuzhiyun return PTR_ERR(sk_tfm);
1760*4882a593Smuzhiyun }
1761*4882a593Smuzhiyun drbg->ctr_handle = sk_tfm;
1762*4882a593Smuzhiyun crypto_init_wait(&drbg->ctr_wait);
1763*4882a593Smuzhiyun
1764*4882a593Smuzhiyun req = skcipher_request_alloc(sk_tfm, GFP_KERNEL);
1765*4882a593Smuzhiyun if (!req) {
1766*4882a593Smuzhiyun pr_info("DRBG: could not allocate request queue\n");
1767*4882a593Smuzhiyun drbg_fini_sym_kernel(drbg);
1768*4882a593Smuzhiyun return -ENOMEM;
1769*4882a593Smuzhiyun }
1770*4882a593Smuzhiyun drbg->ctr_req = req;
1771*4882a593Smuzhiyun skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
1772*4882a593Smuzhiyun CRYPTO_TFM_REQ_MAY_SLEEP,
1773*4882a593Smuzhiyun crypto_req_done, &drbg->ctr_wait);
1774*4882a593Smuzhiyun
1775*4882a593Smuzhiyun alignmask = crypto_skcipher_alignmask(sk_tfm);
1776*4882a593Smuzhiyun drbg->outscratchpadbuf = kmalloc(DRBG_OUTSCRATCHLEN + alignmask,
1777*4882a593Smuzhiyun GFP_KERNEL);
1778*4882a593Smuzhiyun if (!drbg->outscratchpadbuf) {
1779*4882a593Smuzhiyun drbg_fini_sym_kernel(drbg);
1780*4882a593Smuzhiyun return -ENOMEM;
1781*4882a593Smuzhiyun }
1782*4882a593Smuzhiyun drbg->outscratchpad = (u8 *)PTR_ALIGN(drbg->outscratchpadbuf,
1783*4882a593Smuzhiyun alignmask + 1);
1784*4882a593Smuzhiyun
1785*4882a593Smuzhiyun sg_init_table(&drbg->sg_in, 1);
1786*4882a593Smuzhiyun sg_init_one(&drbg->sg_out, drbg->outscratchpad, DRBG_OUTSCRATCHLEN);
1787*4882a593Smuzhiyun
1788*4882a593Smuzhiyun return alignmask;
1789*4882a593Smuzhiyun }
1790*4882a593Smuzhiyun
drbg_kcapi_symsetkey(struct drbg_state * drbg,const unsigned char * key)1791*4882a593Smuzhiyun static void drbg_kcapi_symsetkey(struct drbg_state *drbg,
1792*4882a593Smuzhiyun const unsigned char *key)
1793*4882a593Smuzhiyun {
1794*4882a593Smuzhiyun struct crypto_cipher *tfm =
1795*4882a593Smuzhiyun (struct crypto_cipher *)drbg->priv_data;
1796*4882a593Smuzhiyun
1797*4882a593Smuzhiyun crypto_cipher_setkey(tfm, key, (drbg_keylen(drbg)));
1798*4882a593Smuzhiyun }
1799*4882a593Smuzhiyun
drbg_kcapi_sym(struct drbg_state * drbg,unsigned char * outval,const struct drbg_string * in)1800*4882a593Smuzhiyun static int drbg_kcapi_sym(struct drbg_state *drbg, unsigned char *outval,
1801*4882a593Smuzhiyun const struct drbg_string *in)
1802*4882a593Smuzhiyun {
1803*4882a593Smuzhiyun struct crypto_cipher *tfm =
1804*4882a593Smuzhiyun (struct crypto_cipher *)drbg->priv_data;
1805*4882a593Smuzhiyun
1806*4882a593Smuzhiyun /* there is only component in *in */
1807*4882a593Smuzhiyun BUG_ON(in->len < drbg_blocklen(drbg));
1808*4882a593Smuzhiyun crypto_cipher_encrypt_one(tfm, outval, in->buf);
1809*4882a593Smuzhiyun return 0;
1810*4882a593Smuzhiyun }
1811*4882a593Smuzhiyun
drbg_kcapi_sym_ctr(struct drbg_state * drbg,u8 * inbuf,u32 inlen,u8 * outbuf,u32 outlen)1812*4882a593Smuzhiyun static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
1813*4882a593Smuzhiyun u8 *inbuf, u32 inlen,
1814*4882a593Smuzhiyun u8 *outbuf, u32 outlen)
1815*4882a593Smuzhiyun {
1816*4882a593Smuzhiyun struct scatterlist *sg_in = &drbg->sg_in, *sg_out = &drbg->sg_out;
1817*4882a593Smuzhiyun u32 scratchpad_use = min_t(u32, outlen, DRBG_OUTSCRATCHLEN);
1818*4882a593Smuzhiyun int ret;
1819*4882a593Smuzhiyun
1820*4882a593Smuzhiyun if (inbuf) {
1821*4882a593Smuzhiyun /* Use caller-provided input buffer */
1822*4882a593Smuzhiyun sg_set_buf(sg_in, inbuf, inlen);
1823*4882a593Smuzhiyun } else {
1824*4882a593Smuzhiyun /* Use scratchpad for in-place operation */
1825*4882a593Smuzhiyun inlen = scratchpad_use;
1826*4882a593Smuzhiyun memset(drbg->outscratchpad, 0, scratchpad_use);
1827*4882a593Smuzhiyun sg_set_buf(sg_in, drbg->outscratchpad, scratchpad_use);
1828*4882a593Smuzhiyun }
1829*4882a593Smuzhiyun
1830*4882a593Smuzhiyun while (outlen) {
1831*4882a593Smuzhiyun u32 cryptlen = min3(inlen, outlen, (u32)DRBG_OUTSCRATCHLEN);
1832*4882a593Smuzhiyun
1833*4882a593Smuzhiyun /* Output buffer may not be valid for SGL, use scratchpad */
1834*4882a593Smuzhiyun skcipher_request_set_crypt(drbg->ctr_req, sg_in, sg_out,
1835*4882a593Smuzhiyun cryptlen, drbg->V);
1836*4882a593Smuzhiyun ret = crypto_wait_req(crypto_skcipher_encrypt(drbg->ctr_req),
1837*4882a593Smuzhiyun &drbg->ctr_wait);
1838*4882a593Smuzhiyun if (ret)
1839*4882a593Smuzhiyun goto out;
1840*4882a593Smuzhiyun
1841*4882a593Smuzhiyun crypto_init_wait(&drbg->ctr_wait);
1842*4882a593Smuzhiyun
1843*4882a593Smuzhiyun memcpy(outbuf, drbg->outscratchpad, cryptlen);
1844*4882a593Smuzhiyun memzero_explicit(drbg->outscratchpad, cryptlen);
1845*4882a593Smuzhiyun
1846*4882a593Smuzhiyun outlen -= cryptlen;
1847*4882a593Smuzhiyun outbuf += cryptlen;
1848*4882a593Smuzhiyun }
1849*4882a593Smuzhiyun ret = 0;
1850*4882a593Smuzhiyun
1851*4882a593Smuzhiyun out:
1852*4882a593Smuzhiyun return ret;
1853*4882a593Smuzhiyun }
1854*4882a593Smuzhiyun #endif /* CONFIG_CRYPTO_DRBG_CTR */
1855*4882a593Smuzhiyun
1856*4882a593Smuzhiyun /***************************************************************
1857*4882a593Smuzhiyun * Kernel crypto API interface to register DRBG
1858*4882a593Smuzhiyun ***************************************************************/
1859*4882a593Smuzhiyun
1860*4882a593Smuzhiyun /*
1861*4882a593Smuzhiyun * Look up the DRBG flags by given kernel crypto API cra_name
1862*4882a593Smuzhiyun * The code uses the drbg_cores definition to do this
1863*4882a593Smuzhiyun *
1864*4882a593Smuzhiyun * @cra_name kernel crypto API cra_name
1865*4882a593Smuzhiyun * @coreref reference to integer which is filled with the pointer to
1866*4882a593Smuzhiyun * the applicable core
1867*4882a593Smuzhiyun * @pr reference for setting prediction resistance
1868*4882a593Smuzhiyun *
1869*4882a593Smuzhiyun * return: flags
1870*4882a593Smuzhiyun */
drbg_convert_tfm_core(const char * cra_driver_name,int * coreref,bool * pr)1871*4882a593Smuzhiyun static inline void drbg_convert_tfm_core(const char *cra_driver_name,
1872*4882a593Smuzhiyun int *coreref, bool *pr)
1873*4882a593Smuzhiyun {
1874*4882a593Smuzhiyun int i = 0;
1875*4882a593Smuzhiyun size_t start = 0;
1876*4882a593Smuzhiyun int len = 0;
1877*4882a593Smuzhiyun
1878*4882a593Smuzhiyun *pr = true;
1879*4882a593Smuzhiyun /* disassemble the names */
1880*4882a593Smuzhiyun if (!memcmp(cra_driver_name, "drbg_nopr_", 10)) {
1881*4882a593Smuzhiyun start = 10;
1882*4882a593Smuzhiyun *pr = false;
1883*4882a593Smuzhiyun } else if (!memcmp(cra_driver_name, "drbg_pr_", 8)) {
1884*4882a593Smuzhiyun start = 8;
1885*4882a593Smuzhiyun } else {
1886*4882a593Smuzhiyun return;
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun /* remove the first part */
1890*4882a593Smuzhiyun len = strlen(cra_driver_name) - start;
1891*4882a593Smuzhiyun for (i = 0; ARRAY_SIZE(drbg_cores) > i; i++) {
1892*4882a593Smuzhiyun if (!memcmp(cra_driver_name + start, drbg_cores[i].cra_name,
1893*4882a593Smuzhiyun len)) {
1894*4882a593Smuzhiyun *coreref = i;
1895*4882a593Smuzhiyun return;
1896*4882a593Smuzhiyun }
1897*4882a593Smuzhiyun }
1898*4882a593Smuzhiyun }
1899*4882a593Smuzhiyun
drbg_kcapi_init(struct crypto_tfm * tfm)1900*4882a593Smuzhiyun static int drbg_kcapi_init(struct crypto_tfm *tfm)
1901*4882a593Smuzhiyun {
1902*4882a593Smuzhiyun struct drbg_state *drbg = crypto_tfm_ctx(tfm);
1903*4882a593Smuzhiyun
1904*4882a593Smuzhiyun mutex_init(&drbg->drbg_mutex);
1905*4882a593Smuzhiyun
1906*4882a593Smuzhiyun return 0;
1907*4882a593Smuzhiyun }
1908*4882a593Smuzhiyun
drbg_kcapi_cleanup(struct crypto_tfm * tfm)1909*4882a593Smuzhiyun static void drbg_kcapi_cleanup(struct crypto_tfm *tfm)
1910*4882a593Smuzhiyun {
1911*4882a593Smuzhiyun drbg_uninstantiate(crypto_tfm_ctx(tfm));
1912*4882a593Smuzhiyun }
1913*4882a593Smuzhiyun
1914*4882a593Smuzhiyun /*
1915*4882a593Smuzhiyun * Generate random numbers invoked by the kernel crypto API:
1916*4882a593Smuzhiyun * The API of the kernel crypto API is extended as follows:
1917*4882a593Smuzhiyun *
1918*4882a593Smuzhiyun * src is additional input supplied to the RNG.
1919*4882a593Smuzhiyun * slen is the length of src.
1920*4882a593Smuzhiyun * dst is the output buffer where random data is to be stored.
1921*4882a593Smuzhiyun * dlen is the length of dst.
1922*4882a593Smuzhiyun */
drbg_kcapi_random(struct crypto_rng * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int dlen)1923*4882a593Smuzhiyun static int drbg_kcapi_random(struct crypto_rng *tfm,
1924*4882a593Smuzhiyun const u8 *src, unsigned int slen,
1925*4882a593Smuzhiyun u8 *dst, unsigned int dlen)
1926*4882a593Smuzhiyun {
1927*4882a593Smuzhiyun struct drbg_state *drbg = crypto_rng_ctx(tfm);
1928*4882a593Smuzhiyun struct drbg_string *addtl = NULL;
1929*4882a593Smuzhiyun struct drbg_string string;
1930*4882a593Smuzhiyun
1931*4882a593Smuzhiyun if (slen) {
1932*4882a593Smuzhiyun /* linked list variable is now local to allow modification */
1933*4882a593Smuzhiyun drbg_string_fill(&string, src, slen);
1934*4882a593Smuzhiyun addtl = &string;
1935*4882a593Smuzhiyun }
1936*4882a593Smuzhiyun
1937*4882a593Smuzhiyun return drbg_generate_long(drbg, dst, dlen, addtl);
1938*4882a593Smuzhiyun }
1939*4882a593Smuzhiyun
1940*4882a593Smuzhiyun /*
1941*4882a593Smuzhiyun * Seed the DRBG invoked by the kernel crypto API
1942*4882a593Smuzhiyun */
drbg_kcapi_seed(struct crypto_rng * tfm,const u8 * seed,unsigned int slen)1943*4882a593Smuzhiyun static int drbg_kcapi_seed(struct crypto_rng *tfm,
1944*4882a593Smuzhiyun const u8 *seed, unsigned int slen)
1945*4882a593Smuzhiyun {
1946*4882a593Smuzhiyun struct drbg_state *drbg = crypto_rng_ctx(tfm);
1947*4882a593Smuzhiyun struct crypto_tfm *tfm_base = crypto_rng_tfm(tfm);
1948*4882a593Smuzhiyun bool pr = false;
1949*4882a593Smuzhiyun struct drbg_string string;
1950*4882a593Smuzhiyun struct drbg_string *seed_string = NULL;
1951*4882a593Smuzhiyun int coreref = 0;
1952*4882a593Smuzhiyun
1953*4882a593Smuzhiyun drbg_convert_tfm_core(crypto_tfm_alg_driver_name(tfm_base), &coreref,
1954*4882a593Smuzhiyun &pr);
1955*4882a593Smuzhiyun if (0 < slen) {
1956*4882a593Smuzhiyun drbg_string_fill(&string, seed, slen);
1957*4882a593Smuzhiyun seed_string = &string;
1958*4882a593Smuzhiyun }
1959*4882a593Smuzhiyun
1960*4882a593Smuzhiyun return drbg_instantiate(drbg, seed_string, coreref, pr);
1961*4882a593Smuzhiyun }
1962*4882a593Smuzhiyun
1963*4882a593Smuzhiyun /***************************************************************
1964*4882a593Smuzhiyun * Kernel module: code to load the module
1965*4882a593Smuzhiyun ***************************************************************/
1966*4882a593Smuzhiyun
1967*4882a593Smuzhiyun /*
1968*4882a593Smuzhiyun * Tests as defined in 11.3.2 in addition to the cipher tests: testing
1969*4882a593Smuzhiyun * of the error handling.
1970*4882a593Smuzhiyun *
1971*4882a593Smuzhiyun * Note: testing of failing seed source as defined in 11.3.2 is not applicable
1972*4882a593Smuzhiyun * as seed source of get_random_bytes does not fail.
1973*4882a593Smuzhiyun *
1974*4882a593Smuzhiyun * Note 2: There is no sensible way of testing the reseed counter
1975*4882a593Smuzhiyun * enforcement, so skip it.
1976*4882a593Smuzhiyun */
drbg_healthcheck_sanity(void)1977*4882a593Smuzhiyun static inline int __init drbg_healthcheck_sanity(void)
1978*4882a593Smuzhiyun {
1979*4882a593Smuzhiyun int len = 0;
1980*4882a593Smuzhiyun #define OUTBUFLEN 16
1981*4882a593Smuzhiyun unsigned char buf[OUTBUFLEN];
1982*4882a593Smuzhiyun struct drbg_state *drbg = NULL;
1983*4882a593Smuzhiyun int ret = -EFAULT;
1984*4882a593Smuzhiyun int rc = -EFAULT;
1985*4882a593Smuzhiyun bool pr = false;
1986*4882a593Smuzhiyun int coreref = 0;
1987*4882a593Smuzhiyun struct drbg_string addtl;
1988*4882a593Smuzhiyun size_t max_addtllen, max_request_bytes;
1989*4882a593Smuzhiyun
1990*4882a593Smuzhiyun /* only perform test in FIPS mode */
1991*4882a593Smuzhiyun if (!fips_enabled)
1992*4882a593Smuzhiyun return 0;
1993*4882a593Smuzhiyun
1994*4882a593Smuzhiyun #ifdef CONFIG_CRYPTO_DRBG_CTR
1995*4882a593Smuzhiyun drbg_convert_tfm_core("drbg_nopr_ctr_aes128", &coreref, &pr);
1996*4882a593Smuzhiyun #elif defined CONFIG_CRYPTO_DRBG_HASH
1997*4882a593Smuzhiyun drbg_convert_tfm_core("drbg_nopr_sha256", &coreref, &pr);
1998*4882a593Smuzhiyun #else
1999*4882a593Smuzhiyun drbg_convert_tfm_core("drbg_nopr_hmac_sha256", &coreref, &pr);
2000*4882a593Smuzhiyun #endif
2001*4882a593Smuzhiyun
2002*4882a593Smuzhiyun drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
2003*4882a593Smuzhiyun if (!drbg)
2004*4882a593Smuzhiyun return -ENOMEM;
2005*4882a593Smuzhiyun
2006*4882a593Smuzhiyun mutex_init(&drbg->drbg_mutex);
2007*4882a593Smuzhiyun drbg->core = &drbg_cores[coreref];
2008*4882a593Smuzhiyun drbg->reseed_threshold = drbg_max_requests(drbg);
2009*4882a593Smuzhiyun
2010*4882a593Smuzhiyun /*
2011*4882a593Smuzhiyun * if the following tests fail, it is likely that there is a buffer
2012*4882a593Smuzhiyun * overflow as buf is much smaller than the requested or provided
2013*4882a593Smuzhiyun * string lengths -- in case the error handling does not succeed
2014*4882a593Smuzhiyun * we may get an OOPS. And we want to get an OOPS as this is a
2015*4882a593Smuzhiyun * grave bug.
2016*4882a593Smuzhiyun */
2017*4882a593Smuzhiyun
2018*4882a593Smuzhiyun max_addtllen = drbg_max_addtl(drbg);
2019*4882a593Smuzhiyun max_request_bytes = drbg_max_request_bytes(drbg);
2020*4882a593Smuzhiyun drbg_string_fill(&addtl, buf, max_addtllen + 1);
2021*4882a593Smuzhiyun /* overflow addtllen with additonal info string */
2022*4882a593Smuzhiyun len = drbg_generate(drbg, buf, OUTBUFLEN, &addtl);
2023*4882a593Smuzhiyun BUG_ON(0 < len);
2024*4882a593Smuzhiyun /* overflow max_bits */
2025*4882a593Smuzhiyun len = drbg_generate(drbg, buf, (max_request_bytes + 1), NULL);
2026*4882a593Smuzhiyun BUG_ON(0 < len);
2027*4882a593Smuzhiyun
2028*4882a593Smuzhiyun /* overflow max addtllen with personalization string */
2029*4882a593Smuzhiyun ret = drbg_seed(drbg, &addtl, false);
2030*4882a593Smuzhiyun BUG_ON(0 == ret);
2031*4882a593Smuzhiyun /* all tests passed */
2032*4882a593Smuzhiyun rc = 0;
2033*4882a593Smuzhiyun
2034*4882a593Smuzhiyun pr_devel("DRBG: Sanity tests for failure code paths successfully "
2035*4882a593Smuzhiyun "completed\n");
2036*4882a593Smuzhiyun
2037*4882a593Smuzhiyun kfree(drbg);
2038*4882a593Smuzhiyun return rc;
2039*4882a593Smuzhiyun }
2040*4882a593Smuzhiyun
2041*4882a593Smuzhiyun static struct rng_alg drbg_algs[22];
2042*4882a593Smuzhiyun
2043*4882a593Smuzhiyun /*
2044*4882a593Smuzhiyun * Fill the array drbg_algs used to register the different DRBGs
2045*4882a593Smuzhiyun * with the kernel crypto API. To fill the array, the information
2046*4882a593Smuzhiyun * from drbg_cores[] is used.
2047*4882a593Smuzhiyun */
drbg_fill_array(struct rng_alg * alg,const struct drbg_core * core,int pr)2048*4882a593Smuzhiyun static inline void __init drbg_fill_array(struct rng_alg *alg,
2049*4882a593Smuzhiyun const struct drbg_core *core, int pr)
2050*4882a593Smuzhiyun {
2051*4882a593Smuzhiyun int pos = 0;
2052*4882a593Smuzhiyun static int priority = 200;
2053*4882a593Smuzhiyun
2054*4882a593Smuzhiyun memcpy(alg->base.cra_name, "stdrng", 6);
2055*4882a593Smuzhiyun if (pr) {
2056*4882a593Smuzhiyun memcpy(alg->base.cra_driver_name, "drbg_pr_", 8);
2057*4882a593Smuzhiyun pos = 8;
2058*4882a593Smuzhiyun } else {
2059*4882a593Smuzhiyun memcpy(alg->base.cra_driver_name, "drbg_nopr_", 10);
2060*4882a593Smuzhiyun pos = 10;
2061*4882a593Smuzhiyun }
2062*4882a593Smuzhiyun memcpy(alg->base.cra_driver_name + pos, core->cra_name,
2063*4882a593Smuzhiyun strlen(core->cra_name));
2064*4882a593Smuzhiyun
2065*4882a593Smuzhiyun alg->base.cra_priority = priority;
2066*4882a593Smuzhiyun priority++;
2067*4882a593Smuzhiyun /*
2068*4882a593Smuzhiyun * If FIPS mode enabled, the selected DRBG shall have the
2069*4882a593Smuzhiyun * highest cra_priority over other stdrng instances to ensure
2070*4882a593Smuzhiyun * it is selected.
2071*4882a593Smuzhiyun */
2072*4882a593Smuzhiyun if (fips_enabled)
2073*4882a593Smuzhiyun alg->base.cra_priority += 200;
2074*4882a593Smuzhiyun
2075*4882a593Smuzhiyun alg->base.cra_ctxsize = sizeof(struct drbg_state);
2076*4882a593Smuzhiyun alg->base.cra_module = THIS_MODULE;
2077*4882a593Smuzhiyun alg->base.cra_init = drbg_kcapi_init;
2078*4882a593Smuzhiyun alg->base.cra_exit = drbg_kcapi_cleanup;
2079*4882a593Smuzhiyun alg->generate = drbg_kcapi_random;
2080*4882a593Smuzhiyun alg->seed = drbg_kcapi_seed;
2081*4882a593Smuzhiyun alg->set_ent = drbg_kcapi_set_entropy;
2082*4882a593Smuzhiyun alg->seedsize = 0;
2083*4882a593Smuzhiyun }
2084*4882a593Smuzhiyun
drbg_init(void)2085*4882a593Smuzhiyun static int __init drbg_init(void)
2086*4882a593Smuzhiyun {
2087*4882a593Smuzhiyun unsigned int i = 0; /* pointer to drbg_algs */
2088*4882a593Smuzhiyun unsigned int j = 0; /* pointer to drbg_cores */
2089*4882a593Smuzhiyun int ret;
2090*4882a593Smuzhiyun
2091*4882a593Smuzhiyun ret = drbg_healthcheck_sanity();
2092*4882a593Smuzhiyun if (ret)
2093*4882a593Smuzhiyun return ret;
2094*4882a593Smuzhiyun
2095*4882a593Smuzhiyun if (ARRAY_SIZE(drbg_cores) * 2 > ARRAY_SIZE(drbg_algs)) {
2096*4882a593Smuzhiyun pr_info("DRBG: Cannot register all DRBG types"
2097*4882a593Smuzhiyun "(slots needed: %zu, slots available: %zu)\n",
2098*4882a593Smuzhiyun ARRAY_SIZE(drbg_cores) * 2, ARRAY_SIZE(drbg_algs));
2099*4882a593Smuzhiyun return -EFAULT;
2100*4882a593Smuzhiyun }
2101*4882a593Smuzhiyun
2102*4882a593Smuzhiyun /*
2103*4882a593Smuzhiyun * each DRBG definition can be used with PR and without PR, thus
2104*4882a593Smuzhiyun * we instantiate each DRBG in drbg_cores[] twice.
2105*4882a593Smuzhiyun *
2106*4882a593Smuzhiyun * As the order of placing them into the drbg_algs array matters
2107*4882a593Smuzhiyun * (the later DRBGs receive a higher cra_priority) we register the
2108*4882a593Smuzhiyun * prediction resistance DRBGs first as the should not be too
2109*4882a593Smuzhiyun * interesting.
2110*4882a593Smuzhiyun */
2111*4882a593Smuzhiyun for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2112*4882a593Smuzhiyun drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 1);
2113*4882a593Smuzhiyun for (j = 0; ARRAY_SIZE(drbg_cores) > j; j++, i++)
2114*4882a593Smuzhiyun drbg_fill_array(&drbg_algs[i], &drbg_cores[j], 0);
2115*4882a593Smuzhiyun return crypto_register_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2116*4882a593Smuzhiyun }
2117*4882a593Smuzhiyun
drbg_exit(void)2118*4882a593Smuzhiyun static void __exit drbg_exit(void)
2119*4882a593Smuzhiyun {
2120*4882a593Smuzhiyun crypto_unregister_rngs(drbg_algs, (ARRAY_SIZE(drbg_cores) * 2));
2121*4882a593Smuzhiyun }
2122*4882a593Smuzhiyun
2123*4882a593Smuzhiyun subsys_initcall(drbg_init);
2124*4882a593Smuzhiyun module_exit(drbg_exit);
2125*4882a593Smuzhiyun #ifndef CRYPTO_DRBG_HASH_STRING
2126*4882a593Smuzhiyun #define CRYPTO_DRBG_HASH_STRING ""
2127*4882a593Smuzhiyun #endif
2128*4882a593Smuzhiyun #ifndef CRYPTO_DRBG_HMAC_STRING
2129*4882a593Smuzhiyun #define CRYPTO_DRBG_HMAC_STRING ""
2130*4882a593Smuzhiyun #endif
2131*4882a593Smuzhiyun #ifndef CRYPTO_DRBG_CTR_STRING
2132*4882a593Smuzhiyun #define CRYPTO_DRBG_CTR_STRING ""
2133*4882a593Smuzhiyun #endif
2134*4882a593Smuzhiyun MODULE_LICENSE("GPL");
2135*4882a593Smuzhiyun MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
2136*4882a593Smuzhiyun MODULE_DESCRIPTION("NIST SP800-90A Deterministic Random Bit Generator (DRBG) "
2137*4882a593Smuzhiyun "using following cores: "
2138*4882a593Smuzhiyun CRYPTO_DRBG_HASH_STRING
2139*4882a593Smuzhiyun CRYPTO_DRBG_HMAC_STRING
2140*4882a593Smuzhiyun CRYPTO_DRBG_CTR_STRING);
2141*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("stdrng");
2142*4882a593Smuzhiyun MODULE_IMPORT_NS(CRYPTO_INTERNAL);
2143