1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright 2021 Google LLC
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Authors: Elena Petrova <lenaptr@google.com>,
6*4882a593Smuzhiyun * Eric Biggers <ebiggers@google.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Self-tests of fips140.ko cryptographic functionality. These are run at
9*4882a593Smuzhiyun * module load time to fulfill FIPS 140 and NIAP FPT_TST_EXT.1 requirements.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The actual requirements for these self-tests are somewhat vague, but
12*4882a593Smuzhiyun * section 9 ("Self-Tests") of the FIPS 140-2 Implementation Guidance document
13*4882a593Smuzhiyun * (https://csrc.nist.gov/csrc/media/projects/cryptographic-module-validation-program/documents/fips140-2/fips1402ig.pdf)
14*4882a593Smuzhiyun * is somewhat helpful. Basically, all implementations of all FIPS approved
15*4882a593Smuzhiyun * algorithms (including modes of operation) must be tested. However:
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * - There are provisions for skipping tests that are already sufficiently
18*4882a593Smuzhiyun * covered by other tests. E.g., HMAC-SHA256 may cover SHA-256.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * - Only one test vector is required per algorithm, and it can be generated
21*4882a593Smuzhiyun * by any known-good implementation or taken from any official document.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * - For ciphers, both encryption and decryption must be tested.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * - Only one key size per algorithm needs to be tested.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * There is some ambiguity about whether all implementations of each algorithm
28*4882a593Smuzhiyun * must be tested, or whether it is sufficient to test just the highest priority
29*4882a593Smuzhiyun * implementation. To be safe we test all implementations, except ones that can
30*4882a593Smuzhiyun * be excluded by one of the rules above.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * See fips140_selftests[] for the list of tests we've selected. Currently, all
33*4882a593Smuzhiyun * our test vectors except the AES-CBC-CTS and DRBG ones were generated by the
34*4882a593Smuzhiyun * script tools/crypto/gen_fips140_testvecs.py, using the known-good
35*4882a593Smuzhiyun * implementations in the Python packages hashlib, pycryptodome, and
36*4882a593Smuzhiyun * cryptography.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * Note that we don't reuse the upstream crypto API's self-tests
39*4882a593Smuzhiyun * (crypto/testmgr.{c,h}), for several reasons:
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * - To meet FIPS requirements, the self-tests must be located within the FIPS
42*4882a593Smuzhiyun * module boundary (fips140.ko). But testmgr is integrated into the crypto
43*4882a593Smuzhiyun * API framework and can't be extracted into the module.
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * - testmgr is much more heavyweight than required for FIPS and NIAP; it
46*4882a593Smuzhiyun * tests more algorithms and does more tests per algorithm, as it's meant to
47*4882a593Smuzhiyun * do proper testing and not just meet certification requirements. We need
48*4882a593Smuzhiyun * tests that can run with minimal overhead on every boot-up.
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * - Despite being more heavyweight in general, testmgr doesn't test the
51*4882a593Smuzhiyun * SHA-256 and AES library APIs, despite that being needed here.
52*4882a593Smuzhiyun */
53*4882a593Smuzhiyun #include <crypto/aead.h>
54*4882a593Smuzhiyun #include <crypto/aes.h>
55*4882a593Smuzhiyun #include <crypto/drbg.h>
56*4882a593Smuzhiyun #include <crypto/hash.h>
57*4882a593Smuzhiyun #include <crypto/rng.h>
58*4882a593Smuzhiyun #include <crypto/sha.h>
59*4882a593Smuzhiyun #include <crypto/skcipher.h>
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #include "fips140-module.h"
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* Test vector for an AEAD algorithm */
64*4882a593Smuzhiyun struct aead_testvec {
65*4882a593Smuzhiyun const u8 *key;
66*4882a593Smuzhiyun size_t key_size;
67*4882a593Smuzhiyun const u8 *iv;
68*4882a593Smuzhiyun size_t iv_size;
69*4882a593Smuzhiyun const u8 *assoc;
70*4882a593Smuzhiyun size_t assoc_size;
71*4882a593Smuzhiyun const u8 *plaintext;
72*4882a593Smuzhiyun size_t plaintext_size;
73*4882a593Smuzhiyun const u8 *ciphertext;
74*4882a593Smuzhiyun size_t ciphertext_size;
75*4882a593Smuzhiyun };
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* Test vector for a length-preserving encryption algorithm */
78*4882a593Smuzhiyun struct skcipher_testvec {
79*4882a593Smuzhiyun const u8 *key;
80*4882a593Smuzhiyun size_t key_size;
81*4882a593Smuzhiyun const u8 *iv;
82*4882a593Smuzhiyun size_t iv_size;
83*4882a593Smuzhiyun const u8 *plaintext;
84*4882a593Smuzhiyun const u8 *ciphertext;
85*4882a593Smuzhiyun size_t message_size;
86*4882a593Smuzhiyun };
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /* Test vector for a hash algorithm */
89*4882a593Smuzhiyun struct hash_testvec {
90*4882a593Smuzhiyun const u8 *key;
91*4882a593Smuzhiyun size_t key_size;
92*4882a593Smuzhiyun const u8 *message;
93*4882a593Smuzhiyun size_t message_size;
94*4882a593Smuzhiyun const u8 *digest;
95*4882a593Smuzhiyun size_t digest_size;
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /* Test vector for a DRBG algorithm */
99*4882a593Smuzhiyun struct drbg_testvec {
100*4882a593Smuzhiyun const u8 *entropy;
101*4882a593Smuzhiyun size_t entropy_size;
102*4882a593Smuzhiyun const u8 *pers;
103*4882a593Smuzhiyun size_t pers_size;
104*4882a593Smuzhiyun const u8 *entpr_a;
105*4882a593Smuzhiyun const u8 *entpr_b;
106*4882a593Smuzhiyun size_t entpr_size;
107*4882a593Smuzhiyun const u8 *add_a;
108*4882a593Smuzhiyun const u8 *add_b;
109*4882a593Smuzhiyun size_t add_size;
110*4882a593Smuzhiyun const u8 *output;
111*4882a593Smuzhiyun size_t out_size;
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun struct fips_test {
115*4882a593Smuzhiyun /* The name of the algorithm, in crypto API syntax */
116*4882a593Smuzhiyun const char *alg;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /*
119*4882a593Smuzhiyun * The optional list of implementations to test. @func will be called
120*4882a593Smuzhiyun * once per implementation, or once with @alg if this list is empty.
121*4882a593Smuzhiyun * The implementation names must be given in crypto API syntax, or in
122*4882a593Smuzhiyun * the case of a library implementation should have "-lib" appended.
123*4882a593Smuzhiyun */
124*4882a593Smuzhiyun const char *impls[8];
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /*
127*4882a593Smuzhiyun * The test function. It should execute a known-answer test on an
128*4882a593Smuzhiyun * algorithm implementation, using the below test vector.
129*4882a593Smuzhiyun */
130*4882a593Smuzhiyun int __must_check (*func)(const struct fips_test *test,
131*4882a593Smuzhiyun const char *impl);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* The test vector, with a format specific to the type of algorithm */
134*4882a593Smuzhiyun union {
135*4882a593Smuzhiyun struct aead_testvec aead;
136*4882a593Smuzhiyun struct skcipher_testvec skcipher;
137*4882a593Smuzhiyun struct hash_testvec hash;
138*4882a593Smuzhiyun struct drbg_testvec drbg;
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun };
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /* Maximum IV size (in bytes) among any algorithm tested here */
143*4882a593Smuzhiyun #define MAX_IV_SIZE 16
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun static int __init __must_check
fips_check_result(u8 * result,const u8 * expected_result,size_t result_size,const char * impl,const char * operation)146*4882a593Smuzhiyun fips_check_result(u8 *result, const u8 *expected_result, size_t result_size,
147*4882a593Smuzhiyun const char *impl, const char *operation)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun fips140_inject_selftest_failure(impl, result);
150*4882a593Smuzhiyun if (memcmp(result, expected_result, result_size) != 0) {
151*4882a593Smuzhiyun pr_err("wrong result from %s %s\n", impl, operation);
152*4882a593Smuzhiyun return -EBADMSG;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun return 0;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun /*
158*4882a593Smuzhiyun * None of the algorithms should be ASYNC, as the FIPS module doesn't register
159*4882a593Smuzhiyun * any ASYNC algorithms. (The ASYNC flag is only declared by hardware
160*4882a593Smuzhiyun * algorithms, which would need their own FIPS certification.)
161*4882a593Smuzhiyun *
162*4882a593Smuzhiyun * Ideally we would verify alg->cra_module == THIS_MODULE here as well, but that
163*4882a593Smuzhiyun * doesn't work because the files are compiled as built-in code.
164*4882a593Smuzhiyun */
165*4882a593Smuzhiyun static int __init __must_check
fips_validate_alg(const struct crypto_alg * alg)166*4882a593Smuzhiyun fips_validate_alg(const struct crypto_alg *alg)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun if (alg->cra_flags & CRYPTO_ALG_ASYNC) {
169*4882a593Smuzhiyun pr_err("unexpectedly got async implementation of %s (%s)\n",
170*4882a593Smuzhiyun alg->cra_name, alg->cra_driver_name);
171*4882a593Smuzhiyun return -EINVAL;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun return 0;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun static int __init __must_check
fips_handle_alloc_tfm_error(const char * impl,int err)177*4882a593Smuzhiyun fips_handle_alloc_tfm_error(const char *impl, int err)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun if (err == -ENOENT) {
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * The requested implementation of the algorithm wasn't found.
182*4882a593Smuzhiyun * This is expected if the CPU lacks a feature the
183*4882a593Smuzhiyun * implementation needs, such as the ARMv8 Crypto Extensions.
184*4882a593Smuzhiyun *
185*4882a593Smuzhiyun * When this happens, the implementation isn't available for
186*4882a593Smuzhiyun * use, so we can't test it, nor do we need to. So we just skip
187*4882a593Smuzhiyun * the test.
188*4882a593Smuzhiyun */
189*4882a593Smuzhiyun pr_info("%s is unavailable (no CPU support?), skipping testing it\n",
190*4882a593Smuzhiyun impl);
191*4882a593Smuzhiyun return 0;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun pr_err("failed to allocate %s tfm: %d\n", impl, err);
194*4882a593Smuzhiyun return err;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun static int __init __must_check
fips_test_aes_library(const struct fips_test * test,const char * impl)198*4882a593Smuzhiyun fips_test_aes_library(const struct fips_test *test, const char *impl)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun const struct skcipher_testvec *vec = &test->skcipher;
201*4882a593Smuzhiyun struct crypto_aes_ctx ctx;
202*4882a593Smuzhiyun u8 block[AES_BLOCK_SIZE];
203*4882a593Smuzhiyun int err;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun if (WARN_ON(vec->message_size != AES_BLOCK_SIZE))
206*4882a593Smuzhiyun return -EINVAL;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun err = aes_expandkey(&ctx, vec->key, vec->key_size);
209*4882a593Smuzhiyun if (err) {
210*4882a593Smuzhiyun pr_err("aes_expandkey() failed: %d\n", err);
211*4882a593Smuzhiyun return err;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun aes_encrypt(&ctx, block, vec->plaintext);
214*4882a593Smuzhiyun err = fips_check_result(block, vec->ciphertext, AES_BLOCK_SIZE,
215*4882a593Smuzhiyun impl, "encryption");
216*4882a593Smuzhiyun if (err)
217*4882a593Smuzhiyun return err;
218*4882a593Smuzhiyun aes_decrypt(&ctx, block, block);
219*4882a593Smuzhiyun return fips_check_result(block, vec->plaintext, AES_BLOCK_SIZE,
220*4882a593Smuzhiyun impl, "decryption");
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun /* Test a length-preserving symmetric cipher using the crypto_skcipher API. */
224*4882a593Smuzhiyun static int __init __must_check
fips_test_skcipher(const struct fips_test * test,const char * impl)225*4882a593Smuzhiyun fips_test_skcipher(const struct fips_test *test, const char *impl)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun const struct skcipher_testvec *vec = &test->skcipher;
228*4882a593Smuzhiyun struct crypto_skcipher *tfm;
229*4882a593Smuzhiyun struct skcipher_request *req = NULL;
230*4882a593Smuzhiyun u8 *message = NULL;
231*4882a593Smuzhiyun struct scatterlist sg;
232*4882a593Smuzhiyun u8 iv[MAX_IV_SIZE];
233*4882a593Smuzhiyun int err;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun if (WARN_ON(vec->iv_size > MAX_IV_SIZE))
236*4882a593Smuzhiyun return -EINVAL;
237*4882a593Smuzhiyun if (WARN_ON(vec->message_size <= 0))
238*4882a593Smuzhiyun return -EINVAL;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun tfm = crypto_alloc_skcipher(impl, 0, 0);
241*4882a593Smuzhiyun if (IS_ERR(tfm))
242*4882a593Smuzhiyun return fips_handle_alloc_tfm_error(impl, PTR_ERR(tfm));
243*4882a593Smuzhiyun err = fips_validate_alg(&crypto_skcipher_alg(tfm)->base);
244*4882a593Smuzhiyun if (err)
245*4882a593Smuzhiyun goto out;
246*4882a593Smuzhiyun if (crypto_skcipher_ivsize(tfm) != vec->iv_size) {
247*4882a593Smuzhiyun pr_err("%s has wrong IV size\n", impl);
248*4882a593Smuzhiyun err = -EINVAL;
249*4882a593Smuzhiyun goto out;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun req = skcipher_request_alloc(tfm, GFP_KERNEL);
253*4882a593Smuzhiyun message = kmemdup(vec->plaintext, vec->message_size, GFP_KERNEL);
254*4882a593Smuzhiyun if (!req || !message) {
255*4882a593Smuzhiyun err = -ENOMEM;
256*4882a593Smuzhiyun goto out;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun sg_init_one(&sg, message, vec->message_size);
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
261*4882a593Smuzhiyun NULL, NULL);
262*4882a593Smuzhiyun skcipher_request_set_crypt(req, &sg, &sg, vec->message_size, iv);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun err = crypto_skcipher_setkey(tfm, vec->key, vec->key_size);
265*4882a593Smuzhiyun if (err) {
266*4882a593Smuzhiyun pr_err("failed to set %s key: %d\n", impl, err);
267*4882a593Smuzhiyun goto out;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun /* Encrypt the plaintext, then verify the resulting ciphertext. */
271*4882a593Smuzhiyun memcpy(iv, vec->iv, vec->iv_size);
272*4882a593Smuzhiyun err = crypto_skcipher_encrypt(req);
273*4882a593Smuzhiyun if (err) {
274*4882a593Smuzhiyun pr_err("%s encryption failed: %d\n", impl, err);
275*4882a593Smuzhiyun goto out;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun err = fips_check_result(message, vec->ciphertext, vec->message_size,
278*4882a593Smuzhiyun impl, "encryption");
279*4882a593Smuzhiyun if (err)
280*4882a593Smuzhiyun goto out;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /* Decrypt the ciphertext, then verify the resulting plaintext. */
283*4882a593Smuzhiyun memcpy(iv, vec->iv, vec->iv_size);
284*4882a593Smuzhiyun err = crypto_skcipher_decrypt(req);
285*4882a593Smuzhiyun if (err) {
286*4882a593Smuzhiyun pr_err("%s decryption failed: %d\n", impl, err);
287*4882a593Smuzhiyun goto out;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun err = fips_check_result(message, vec->plaintext, vec->message_size,
290*4882a593Smuzhiyun impl, "decryption");
291*4882a593Smuzhiyun out:
292*4882a593Smuzhiyun kfree(message);
293*4882a593Smuzhiyun skcipher_request_free(req);
294*4882a593Smuzhiyun crypto_free_skcipher(tfm);
295*4882a593Smuzhiyun return err;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /* Test an AEAD using the crypto_aead API. */
299*4882a593Smuzhiyun static int __init __must_check
fips_test_aead(const struct fips_test * test,const char * impl)300*4882a593Smuzhiyun fips_test_aead(const struct fips_test *test, const char *impl)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun const struct aead_testvec *vec = &test->aead;
303*4882a593Smuzhiyun const int tag_size = vec->ciphertext_size - vec->plaintext_size;
304*4882a593Smuzhiyun struct crypto_aead *tfm;
305*4882a593Smuzhiyun struct aead_request *req = NULL;
306*4882a593Smuzhiyun u8 *assoc = NULL;
307*4882a593Smuzhiyun u8 *message = NULL;
308*4882a593Smuzhiyun struct scatterlist sg[2];
309*4882a593Smuzhiyun int sg_idx = 0;
310*4882a593Smuzhiyun u8 iv[MAX_IV_SIZE];
311*4882a593Smuzhiyun int err;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun if (WARN_ON(vec->iv_size > MAX_IV_SIZE))
314*4882a593Smuzhiyun return -EINVAL;
315*4882a593Smuzhiyun if (WARN_ON(vec->ciphertext_size <= vec->plaintext_size))
316*4882a593Smuzhiyun return -EINVAL;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun tfm = crypto_alloc_aead(impl, 0, 0);
319*4882a593Smuzhiyun if (IS_ERR(tfm))
320*4882a593Smuzhiyun return fips_handle_alloc_tfm_error(impl, PTR_ERR(tfm));
321*4882a593Smuzhiyun err = fips_validate_alg(&crypto_aead_alg(tfm)->base);
322*4882a593Smuzhiyun if (err)
323*4882a593Smuzhiyun goto out;
324*4882a593Smuzhiyun if (crypto_aead_ivsize(tfm) != vec->iv_size) {
325*4882a593Smuzhiyun pr_err("%s has wrong IV size\n", impl);
326*4882a593Smuzhiyun err = -EINVAL;
327*4882a593Smuzhiyun goto out;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun req = aead_request_alloc(tfm, GFP_KERNEL);
331*4882a593Smuzhiyun assoc = kmemdup(vec->assoc, vec->assoc_size, GFP_KERNEL);
332*4882a593Smuzhiyun message = kzalloc(vec->ciphertext_size, GFP_KERNEL);
333*4882a593Smuzhiyun if (!req || !assoc || !message) {
334*4882a593Smuzhiyun err = -ENOMEM;
335*4882a593Smuzhiyun goto out;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun memcpy(message, vec->plaintext, vec->plaintext_size);
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun sg_init_table(sg, ARRAY_SIZE(sg));
340*4882a593Smuzhiyun if (vec->assoc_size)
341*4882a593Smuzhiyun sg_set_buf(&sg[sg_idx++], assoc, vec->assoc_size);
342*4882a593Smuzhiyun sg_set_buf(&sg[sg_idx++], message, vec->ciphertext_size);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun aead_request_set_ad(req, vec->assoc_size);
345*4882a593Smuzhiyun aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun err = crypto_aead_setkey(tfm, vec->key, vec->key_size);
348*4882a593Smuzhiyun if (err) {
349*4882a593Smuzhiyun pr_err("failed to set %s key: %d\n", impl, err);
350*4882a593Smuzhiyun goto out;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun err = crypto_aead_setauthsize(tfm, tag_size);
354*4882a593Smuzhiyun if (err) {
355*4882a593Smuzhiyun pr_err("failed to set %s authentication tag size: %d\n",
356*4882a593Smuzhiyun impl, err);
357*4882a593Smuzhiyun goto out;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun /*
361*4882a593Smuzhiyun * Encrypt the plaintext, then verify the resulting ciphertext (which
362*4882a593Smuzhiyun * includes the authentication tag).
363*4882a593Smuzhiyun */
364*4882a593Smuzhiyun memcpy(iv, vec->iv, vec->iv_size);
365*4882a593Smuzhiyun aead_request_set_crypt(req, sg, sg, vec->plaintext_size, iv);
366*4882a593Smuzhiyun err = crypto_aead_encrypt(req);
367*4882a593Smuzhiyun if (err) {
368*4882a593Smuzhiyun pr_err("%s encryption failed: %d\n", impl, err);
369*4882a593Smuzhiyun goto out;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun err = fips_check_result(message, vec->ciphertext, vec->ciphertext_size,
372*4882a593Smuzhiyun impl, "encryption");
373*4882a593Smuzhiyun if (err)
374*4882a593Smuzhiyun goto out;
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /*
377*4882a593Smuzhiyun * Decrypt the ciphertext (which includes the authentication tag), then
378*4882a593Smuzhiyun * verify the resulting plaintext.
379*4882a593Smuzhiyun */
380*4882a593Smuzhiyun memcpy(iv, vec->iv, vec->iv_size);
381*4882a593Smuzhiyun aead_request_set_crypt(req, sg, sg, vec->ciphertext_size, iv);
382*4882a593Smuzhiyun err = crypto_aead_decrypt(req);
383*4882a593Smuzhiyun if (err) {
384*4882a593Smuzhiyun pr_err("%s decryption failed: %d\n", impl, err);
385*4882a593Smuzhiyun goto out;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun err = fips_check_result(message, vec->plaintext, vec->plaintext_size,
388*4882a593Smuzhiyun impl, "decryption");
389*4882a593Smuzhiyun out:
390*4882a593Smuzhiyun kfree(message);
391*4882a593Smuzhiyun kfree(assoc);
392*4882a593Smuzhiyun aead_request_free(req);
393*4882a593Smuzhiyun crypto_free_aead(tfm);
394*4882a593Smuzhiyun return err;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /*
398*4882a593Smuzhiyun * Test a hash algorithm using the crypto_shash API.
399*4882a593Smuzhiyun *
400*4882a593Smuzhiyun * Note that we don't need to test the crypto_ahash API too, since none of the
401*4882a593Smuzhiyun * hash algorithms in the FIPS module have the ASYNC flag, and thus there will
402*4882a593Smuzhiyun * be no hash algorithms that can be accessed only through crypto_ahash.
403*4882a593Smuzhiyun */
404*4882a593Smuzhiyun static int __init __must_check
fips_test_hash(const struct fips_test * test,const char * impl)405*4882a593Smuzhiyun fips_test_hash(const struct fips_test *test, const char *impl)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun const struct hash_testvec *vec = &test->hash;
408*4882a593Smuzhiyun struct crypto_shash *tfm;
409*4882a593Smuzhiyun u8 digest[HASH_MAX_DIGESTSIZE];
410*4882a593Smuzhiyun int err;
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun if (WARN_ON(vec->digest_size > HASH_MAX_DIGESTSIZE))
413*4882a593Smuzhiyun return -EINVAL;
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun tfm = crypto_alloc_shash(impl, 0, 0);
416*4882a593Smuzhiyun if (IS_ERR(tfm))
417*4882a593Smuzhiyun return fips_handle_alloc_tfm_error(impl, PTR_ERR(tfm));
418*4882a593Smuzhiyun err = fips_validate_alg(&crypto_shash_alg(tfm)->base);
419*4882a593Smuzhiyun if (err)
420*4882a593Smuzhiyun goto out;
421*4882a593Smuzhiyun if (crypto_shash_digestsize(tfm) != vec->digest_size) {
422*4882a593Smuzhiyun pr_err("%s has wrong digest size\n", impl);
423*4882a593Smuzhiyun err = -EINVAL;
424*4882a593Smuzhiyun goto out;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun if (vec->key) {
428*4882a593Smuzhiyun err = crypto_shash_setkey(tfm, vec->key, vec->key_size);
429*4882a593Smuzhiyun if (err) {
430*4882a593Smuzhiyun pr_err("failed to set %s key: %d\n", impl, err);
431*4882a593Smuzhiyun goto out;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun err = crypto_shash_tfm_digest(tfm, vec->message, vec->message_size,
436*4882a593Smuzhiyun digest);
437*4882a593Smuzhiyun if (err) {
438*4882a593Smuzhiyun pr_err("%s digest computation failed: %d\n", impl, err);
439*4882a593Smuzhiyun goto out;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun err = fips_check_result(digest, vec->digest, vec->digest_size,
442*4882a593Smuzhiyun impl, "digest");
443*4882a593Smuzhiyun out:
444*4882a593Smuzhiyun crypto_free_shash(tfm);
445*4882a593Smuzhiyun return err;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun static int __init __must_check
fips_test_sha256_library(const struct fips_test * test,const char * impl)449*4882a593Smuzhiyun fips_test_sha256_library(const struct fips_test *test, const char *impl)
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun const struct hash_testvec *vec = &test->hash;
452*4882a593Smuzhiyun u8 digest[SHA256_DIGEST_SIZE];
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun if (WARN_ON(vec->digest_size != SHA256_DIGEST_SIZE))
455*4882a593Smuzhiyun return -EINVAL;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun sha256(vec->message, vec->message_size, digest);
458*4882a593Smuzhiyun return fips_check_result(digest, vec->digest, vec->digest_size,
459*4882a593Smuzhiyun impl, "digest");
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun /* Test a DRBG using the crypto_rng API. */
463*4882a593Smuzhiyun static int __init __must_check
fips_test_drbg(const struct fips_test * test,const char * impl)464*4882a593Smuzhiyun fips_test_drbg(const struct fips_test *test, const char *impl)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun const struct drbg_testvec *vec = &test->drbg;
467*4882a593Smuzhiyun struct crypto_rng *rng;
468*4882a593Smuzhiyun u8 *output = NULL;
469*4882a593Smuzhiyun struct drbg_test_data test_data;
470*4882a593Smuzhiyun struct drbg_string addtl, pers, testentropy;
471*4882a593Smuzhiyun int err;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun rng = crypto_alloc_rng(impl, 0, 0);
474*4882a593Smuzhiyun if (IS_ERR(rng))
475*4882a593Smuzhiyun return fips_handle_alloc_tfm_error(impl, PTR_ERR(rng));
476*4882a593Smuzhiyun err = fips_validate_alg(&crypto_rng_alg(rng)->base);
477*4882a593Smuzhiyun if (err)
478*4882a593Smuzhiyun goto out;
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun output = kzalloc(vec->out_size, GFP_KERNEL);
481*4882a593Smuzhiyun if (!output) {
482*4882a593Smuzhiyun err = -ENOMEM;
483*4882a593Smuzhiyun goto out;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun /*
487*4882a593Smuzhiyun * Initialize the DRBG with the entropy and personalization string given
488*4882a593Smuzhiyun * in the test vector.
489*4882a593Smuzhiyun */
490*4882a593Smuzhiyun test_data.testentropy = &testentropy;
491*4882a593Smuzhiyun drbg_string_fill(&testentropy, vec->entropy, vec->entropy_size);
492*4882a593Smuzhiyun drbg_string_fill(&pers, vec->pers, vec->pers_size);
493*4882a593Smuzhiyun err = crypto_drbg_reset_test(rng, &pers, &test_data);
494*4882a593Smuzhiyun if (err) {
495*4882a593Smuzhiyun pr_err("failed to reset %s\n", impl);
496*4882a593Smuzhiyun goto out;
497*4882a593Smuzhiyun }
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /*
500*4882a593Smuzhiyun * Generate some random bytes using the additional data string provided
501*4882a593Smuzhiyun * in the test vector. Also use the additional entropy if provided
502*4882a593Smuzhiyun * (relevant for the prediction-resistant DRBG variants only).
503*4882a593Smuzhiyun */
504*4882a593Smuzhiyun drbg_string_fill(&addtl, vec->add_a, vec->add_size);
505*4882a593Smuzhiyun if (vec->entpr_size) {
506*4882a593Smuzhiyun drbg_string_fill(&testentropy, vec->entpr_a, vec->entpr_size);
507*4882a593Smuzhiyun err = crypto_drbg_get_bytes_addtl_test(rng, output,
508*4882a593Smuzhiyun vec->out_size, &addtl,
509*4882a593Smuzhiyun &test_data);
510*4882a593Smuzhiyun } else {
511*4882a593Smuzhiyun err = crypto_drbg_get_bytes_addtl(rng, output, vec->out_size,
512*4882a593Smuzhiyun &addtl);
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun if (err) {
515*4882a593Smuzhiyun pr_err("failed to get bytes from %s (try 1): %d\n",
516*4882a593Smuzhiyun impl, err);
517*4882a593Smuzhiyun goto out;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun /*
521*4882a593Smuzhiyun * Do the same again, using a second additional data string, and (when
522*4882a593Smuzhiyun * applicable) a second additional entropy string.
523*4882a593Smuzhiyun */
524*4882a593Smuzhiyun drbg_string_fill(&addtl, vec->add_b, vec->add_size);
525*4882a593Smuzhiyun if (test->drbg.entpr_size) {
526*4882a593Smuzhiyun drbg_string_fill(&testentropy, vec->entpr_b, vec->entpr_size);
527*4882a593Smuzhiyun err = crypto_drbg_get_bytes_addtl_test(rng, output,
528*4882a593Smuzhiyun vec->out_size, &addtl,
529*4882a593Smuzhiyun &test_data);
530*4882a593Smuzhiyun } else {
531*4882a593Smuzhiyun err = crypto_drbg_get_bytes_addtl(rng, output, vec->out_size,
532*4882a593Smuzhiyun &addtl);
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun if (err) {
535*4882a593Smuzhiyun pr_err("failed to get bytes from %s (try 2): %d\n",
536*4882a593Smuzhiyun impl, err);
537*4882a593Smuzhiyun goto out;
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun /* Check that the DRBG generated the expected output. */
541*4882a593Smuzhiyun err = fips_check_result(output, vec->output, vec->out_size,
542*4882a593Smuzhiyun impl, "get_bytes");
543*4882a593Smuzhiyun out:
544*4882a593Smuzhiyun kfree(output);
545*4882a593Smuzhiyun crypto_free_rng(rng);
546*4882a593Smuzhiyun return err;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun /* Include the test vectors generated by the Python script. */
550*4882a593Smuzhiyun #include "fips140-generated-testvecs.h"
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun /*
553*4882a593Smuzhiyun * List of all self-tests. Keep this in sync with fips140_algorithms[].
554*4882a593Smuzhiyun *
555*4882a593Smuzhiyun * When possible, we have followed the FIPS 140-2 Implementation Guidance (IG)
556*4882a593Smuzhiyun * document when creating this list of tests. The result is intended to be a
557*4882a593Smuzhiyun * list of tests that is near-minimal (and thus minimizes runtime overhead)
558*4882a593Smuzhiyun * while complying with all requirements. For additional details, see the
559*4882a593Smuzhiyun * comment at the beginning of this file.
560*4882a593Smuzhiyun */
561*4882a593Smuzhiyun static const struct fips_test fips140_selftests[] __initconst = {
562*4882a593Smuzhiyun /*
563*4882a593Smuzhiyun * Test for the AES library API.
564*4882a593Smuzhiyun *
565*4882a593Smuzhiyun * Since the AES library API may use its own AES implementation and the
566*4882a593Smuzhiyun * module provides no support for composing it with a mode of operation
567*4882a593Smuzhiyun * (it's just plain AES), we must test it directly.
568*4882a593Smuzhiyun *
569*4882a593Smuzhiyun * In contrast, we don't need to directly test the "aes" ciphers that
570*4882a593Smuzhiyun * are accessible through the crypto_cipher API (e.g. "aes-ce"), as they
571*4882a593Smuzhiyun * are covered indirectly by AES-CMAC and AES-ECB tests.
572*4882a593Smuzhiyun */
573*4882a593Smuzhiyun {
574*4882a593Smuzhiyun .alg = "aes",
575*4882a593Smuzhiyun .impls = {"aes-lib"},
576*4882a593Smuzhiyun .func = fips_test_aes_library,
577*4882a593Smuzhiyun .skcipher = {
578*4882a593Smuzhiyun .key = fips_aes_key,
579*4882a593Smuzhiyun .key_size = sizeof(fips_aes_key),
580*4882a593Smuzhiyun .plaintext = fips_message,
581*4882a593Smuzhiyun .ciphertext = fips_aes_ecb_ciphertext,
582*4882a593Smuzhiyun .message_size = 16,
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun },
585*4882a593Smuzhiyun /*
586*4882a593Smuzhiyun * Tests for AES-CMAC, a.k.a. "cmac(aes)" in crypto API syntax.
587*4882a593Smuzhiyun *
588*4882a593Smuzhiyun * The IG requires that each underlying AES implementation be tested in
589*4882a593Smuzhiyun * an authenticated mode, if implemented. Of such modes, this module
590*4882a593Smuzhiyun * implements AES-GCM and AES-CMAC. However, AES-GCM doesn't "count"
591*4882a593Smuzhiyun * because this module's implementations of AES-GCM won't actually be
592*4882a593Smuzhiyun * FIPS-approved, due to a quirk in the FIPS requirements.
593*4882a593Smuzhiyun *
594*4882a593Smuzhiyun * Therefore, for us this requirement applies to AES-CMAC, so we must
595*4882a593Smuzhiyun * test the "cmac" template composed with each "aes" implementation.
596*4882a593Smuzhiyun *
597*4882a593Smuzhiyun * Separately from the above, we also must test all standalone
598*4882a593Smuzhiyun * implementations of "cmac(aes)" such as "cmac-aes-ce", as they don't
599*4882a593Smuzhiyun * reuse another full AES implementation and thus can't be covered by
600*4882a593Smuzhiyun * another test.
601*4882a593Smuzhiyun */
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun .alg = "cmac(aes)",
604*4882a593Smuzhiyun .impls = {
605*4882a593Smuzhiyun /* "cmac" template with all "aes" implementations */
606*4882a593Smuzhiyun "cmac(aes-generic)",
607*4882a593Smuzhiyun "cmac(aes-arm64)",
608*4882a593Smuzhiyun "cmac(aes-ce)",
609*4882a593Smuzhiyun /* All standalone implementations of "cmac(aes)" */
610*4882a593Smuzhiyun "cmac-aes-neon",
611*4882a593Smuzhiyun "cmac-aes-ce",
612*4882a593Smuzhiyun },
613*4882a593Smuzhiyun .func = fips_test_hash,
614*4882a593Smuzhiyun .hash = {
615*4882a593Smuzhiyun .key = fips_aes_key,
616*4882a593Smuzhiyun .key_size = sizeof(fips_aes_key),
617*4882a593Smuzhiyun .message = fips_message,
618*4882a593Smuzhiyun .message_size = sizeof(fips_message),
619*4882a593Smuzhiyun .digest = fips_aes_cmac_digest,
620*4882a593Smuzhiyun .digest_size = sizeof(fips_aes_cmac_digest),
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun },
623*4882a593Smuzhiyun /*
624*4882a593Smuzhiyun * Tests for AES-ECB, a.k.a. "ecb(aes)" in crypto API syntax.
625*4882a593Smuzhiyun *
626*4882a593Smuzhiyun * The IG requires that each underlying AES implementation be tested in
627*4882a593Smuzhiyun * a mode that exercises the encryption direction of AES and in a mode
628*4882a593Smuzhiyun * that exercises the decryption direction of AES. CMAC only covers the
629*4882a593Smuzhiyun * encryption direction, so we choose ECB to test decryption. Thus, we
630*4882a593Smuzhiyun * test the "ecb" template composed with each "aes" implementation.
631*4882a593Smuzhiyun *
632*4882a593Smuzhiyun * Separately from the above, we also must test all standalone
633*4882a593Smuzhiyun * implementations of "ecb(aes)" such as "ecb-aes-ce", as they don't
634*4882a593Smuzhiyun * reuse another full AES implementation and thus can't be covered by
635*4882a593Smuzhiyun * another test.
636*4882a593Smuzhiyun */
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun .alg = "ecb(aes)",
639*4882a593Smuzhiyun .impls = {
640*4882a593Smuzhiyun /* "ecb" template with all "aes" implementations */
641*4882a593Smuzhiyun "ecb(aes-generic)",
642*4882a593Smuzhiyun "ecb(aes-arm64)",
643*4882a593Smuzhiyun "ecb(aes-ce)",
644*4882a593Smuzhiyun /* All standalone implementations of "ecb(aes)" */
645*4882a593Smuzhiyun "ecb-aes-neon",
646*4882a593Smuzhiyun "ecb-aes-neonbs",
647*4882a593Smuzhiyun "ecb-aes-ce",
648*4882a593Smuzhiyun },
649*4882a593Smuzhiyun .func = fips_test_skcipher,
650*4882a593Smuzhiyun .skcipher = {
651*4882a593Smuzhiyun .key = fips_aes_key,
652*4882a593Smuzhiyun .key_size = sizeof(fips_aes_key),
653*4882a593Smuzhiyun .plaintext = fips_message,
654*4882a593Smuzhiyun .ciphertext = fips_aes_ecb_ciphertext,
655*4882a593Smuzhiyun .message_size = sizeof(fips_message)
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun },
658*4882a593Smuzhiyun /*
659*4882a593Smuzhiyun * Tests for AES-CBC, AES-CBC-CTS, AES-CTR, AES-XTS, and AES-GCM.
660*4882a593Smuzhiyun *
661*4882a593Smuzhiyun * According to the IG, an AES mode of operation doesn't need to have
662*4882a593Smuzhiyun * its own test, provided that (a) both the encryption and decryption
663*4882a593Smuzhiyun * directions of the underlying AES implementation are already tested
664*4882a593Smuzhiyun * via other mode(s), and (b) in the case of an authenticated mode, at
665*4882a593Smuzhiyun * least one other authenticated mode is already tested. The tests of
666*4882a593Smuzhiyun * the "cmac" and "ecb" templates fulfill these conditions; therefore,
667*4882a593Smuzhiyun * we don't need to test any other AES mode templates.
668*4882a593Smuzhiyun *
669*4882a593Smuzhiyun * This does *not* apply to standalone implementations of these modes
670*4882a593Smuzhiyun * such as "cbc-aes-ce", as such implementations don't reuse another
671*4882a593Smuzhiyun * full AES implementation and thus can't be covered by another test.
672*4882a593Smuzhiyun * We must test all such standalone implementations.
673*4882a593Smuzhiyun *
674*4882a593Smuzhiyun * The AES-GCM test isn't actually required, as it's expected that this
675*4882a593Smuzhiyun * module's AES-GCM implementation won't actually be able to be
676*4882a593Smuzhiyun * FIPS-approved. This is unfortunate; it's caused by the FIPS
677*4882a593Smuzhiyun * requirements for GCM being incompatible with GCM implementations that
678*4882a593Smuzhiyun * don't generate their own IVs. We choose to still include the AES-GCM
679*4882a593Smuzhiyun * test to keep it on par with the other FIPS-approved algorithms, in
680*4882a593Smuzhiyun * case it turns out that AES-GCM can be approved after all.
681*4882a593Smuzhiyun */
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun .alg = "cbc(aes)",
684*4882a593Smuzhiyun .impls = {
685*4882a593Smuzhiyun /* All standalone implementations of "cbc(aes)" */
686*4882a593Smuzhiyun "cbc-aes-neon",
687*4882a593Smuzhiyun "cbc-aes-neonbs",
688*4882a593Smuzhiyun "cbc-aes-ce",
689*4882a593Smuzhiyun },
690*4882a593Smuzhiyun .func = fips_test_skcipher,
691*4882a593Smuzhiyun .skcipher = {
692*4882a593Smuzhiyun .key = fips_aes_key,
693*4882a593Smuzhiyun .key_size = sizeof(fips_aes_key),
694*4882a593Smuzhiyun .iv = fips_aes_iv,
695*4882a593Smuzhiyun .iv_size = sizeof(fips_aes_iv),
696*4882a593Smuzhiyun .plaintext = fips_message,
697*4882a593Smuzhiyun .ciphertext = fips_aes_cbc_ciphertext,
698*4882a593Smuzhiyun .message_size = sizeof(fips_message),
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun }, {
701*4882a593Smuzhiyun .alg = "cts(cbc(aes))",
702*4882a593Smuzhiyun .impls = {
703*4882a593Smuzhiyun /* All standalone implementations of "cts(cbc(aes))" */
704*4882a593Smuzhiyun "cts-cbc-aes-neon",
705*4882a593Smuzhiyun "cts-cbc-aes-ce",
706*4882a593Smuzhiyun },
707*4882a593Smuzhiyun .func = fips_test_skcipher,
708*4882a593Smuzhiyun /* Test vector taken from RFC 3962 */
709*4882a593Smuzhiyun .skcipher = {
710*4882a593Smuzhiyun .key = "\x63\x68\x69\x63\x6b\x65\x6e\x20"
711*4882a593Smuzhiyun "\x74\x65\x72\x69\x79\x61\x6b\x69",
712*4882a593Smuzhiyun .key_size = 16,
713*4882a593Smuzhiyun .iv = "\x00\x00\x00\x00\x00\x00\x00\x00"
714*4882a593Smuzhiyun "\x00\x00\x00\x00\x00\x00\x00\x00",
715*4882a593Smuzhiyun .iv_size = 16,
716*4882a593Smuzhiyun .plaintext = "\x49\x20\x77\x6f\x75\x6c\x64\x20"
717*4882a593Smuzhiyun "\x6c\x69\x6b\x65\x20\x74\x68\x65"
718*4882a593Smuzhiyun "\x20\x47\x65\x6e\x65\x72\x61\x6c"
719*4882a593Smuzhiyun "\x20\x47\x61\x75\x27\x73\x20",
720*4882a593Smuzhiyun .ciphertext = "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1"
721*4882a593Smuzhiyun "\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
722*4882a593Smuzhiyun "\x97\x68\x72\x68\xd6\xec\xcc\xc0"
723*4882a593Smuzhiyun "\xc0\x7b\x25\xe2\x5e\xcf\xe5",
724*4882a593Smuzhiyun .message_size = 31,
725*4882a593Smuzhiyun }
726*4882a593Smuzhiyun }, {
727*4882a593Smuzhiyun .alg = "ctr(aes)",
728*4882a593Smuzhiyun .impls = {
729*4882a593Smuzhiyun /* All standalone implementations of "ctr(aes)" */
730*4882a593Smuzhiyun "ctr-aes-neon",
731*4882a593Smuzhiyun "ctr-aes-neonbs",
732*4882a593Smuzhiyun "ctr-aes-ce",
733*4882a593Smuzhiyun },
734*4882a593Smuzhiyun .func = fips_test_skcipher,
735*4882a593Smuzhiyun .skcipher = {
736*4882a593Smuzhiyun .key = fips_aes_key,
737*4882a593Smuzhiyun .key_size = sizeof(fips_aes_key),
738*4882a593Smuzhiyun .iv = fips_aes_iv,
739*4882a593Smuzhiyun .iv_size = sizeof(fips_aes_iv),
740*4882a593Smuzhiyun .plaintext = fips_message,
741*4882a593Smuzhiyun .ciphertext = fips_aes_ctr_ciphertext,
742*4882a593Smuzhiyun .message_size = sizeof(fips_message),
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun }, {
745*4882a593Smuzhiyun .alg = "xts(aes)",
746*4882a593Smuzhiyun .impls = {
747*4882a593Smuzhiyun /* All standalone implementations of "xts(aes)" */
748*4882a593Smuzhiyun "xts-aes-neon",
749*4882a593Smuzhiyun "xts-aes-neonbs",
750*4882a593Smuzhiyun "xts-aes-ce",
751*4882a593Smuzhiyun },
752*4882a593Smuzhiyun .func = fips_test_skcipher,
753*4882a593Smuzhiyun .skcipher = {
754*4882a593Smuzhiyun .key = fips_aes_xts_key,
755*4882a593Smuzhiyun .key_size = sizeof(fips_aes_xts_key),
756*4882a593Smuzhiyun .iv = fips_aes_iv,
757*4882a593Smuzhiyun .iv_size = sizeof(fips_aes_iv),
758*4882a593Smuzhiyun .plaintext = fips_message,
759*4882a593Smuzhiyun .ciphertext = fips_aes_xts_ciphertext,
760*4882a593Smuzhiyun .message_size = sizeof(fips_message),
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun }, {
763*4882a593Smuzhiyun .alg = "gcm(aes)",
764*4882a593Smuzhiyun .impls = {
765*4882a593Smuzhiyun /* All standalone implementations of "gcm(aes)" */
766*4882a593Smuzhiyun "gcm-aes-ce",
767*4882a593Smuzhiyun },
768*4882a593Smuzhiyun .func = fips_test_aead,
769*4882a593Smuzhiyun .aead = {
770*4882a593Smuzhiyun .key = fips_aes_key,
771*4882a593Smuzhiyun .key_size = sizeof(fips_aes_key),
772*4882a593Smuzhiyun .iv = fips_aes_iv,
773*4882a593Smuzhiyun /* The GCM implementations assume an IV size of 12. */
774*4882a593Smuzhiyun .iv_size = 12,
775*4882a593Smuzhiyun .assoc = fips_aes_gcm_assoc,
776*4882a593Smuzhiyun .assoc_size = sizeof(fips_aes_gcm_assoc),
777*4882a593Smuzhiyun .plaintext = fips_message,
778*4882a593Smuzhiyun .plaintext_size = sizeof(fips_message),
779*4882a593Smuzhiyun .ciphertext = fips_aes_gcm_ciphertext,
780*4882a593Smuzhiyun .ciphertext_size = sizeof(fips_aes_gcm_ciphertext),
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun },
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun /* Tests for SHA-1 */
785*4882a593Smuzhiyun {
786*4882a593Smuzhiyun .alg = "sha1",
787*4882a593Smuzhiyun .impls = {
788*4882a593Smuzhiyun /* All implementations of "sha1" */
789*4882a593Smuzhiyun "sha1-generic",
790*4882a593Smuzhiyun "sha1-ce"
791*4882a593Smuzhiyun },
792*4882a593Smuzhiyun .func = fips_test_hash,
793*4882a593Smuzhiyun .hash = {
794*4882a593Smuzhiyun .message = fips_message,
795*4882a593Smuzhiyun .message_size = sizeof(fips_message),
796*4882a593Smuzhiyun .digest = fips_sha1_digest,
797*4882a593Smuzhiyun .digest_size = sizeof(fips_sha1_digest)
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun },
800*4882a593Smuzhiyun /*
801*4882a593Smuzhiyun * Tests for all SHA-256 implementations other than the sha256() library
802*4882a593Smuzhiyun * function. As per the IG, these tests also fulfill the tests for the
803*4882a593Smuzhiyun * corresponding SHA-224 implementations.
804*4882a593Smuzhiyun */
805*4882a593Smuzhiyun {
806*4882a593Smuzhiyun .alg = "sha256",
807*4882a593Smuzhiyun .impls = {
808*4882a593Smuzhiyun /* All implementations of "sha256" */
809*4882a593Smuzhiyun "sha256-generic",
810*4882a593Smuzhiyun "sha256-arm64",
811*4882a593Smuzhiyun "sha256-ce",
812*4882a593Smuzhiyun },
813*4882a593Smuzhiyun .func = fips_test_hash,
814*4882a593Smuzhiyun .hash = {
815*4882a593Smuzhiyun .message = fips_message,
816*4882a593Smuzhiyun .message_size = sizeof(fips_message),
817*4882a593Smuzhiyun .digest = fips_sha256_digest,
818*4882a593Smuzhiyun .digest_size = sizeof(fips_sha256_digest)
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun },
821*4882a593Smuzhiyun /*
822*4882a593Smuzhiyun * Test for the sha256() library function. This must be tested
823*4882a593Smuzhiyun * separately because it may use its own SHA-256 implementation.
824*4882a593Smuzhiyun */
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun .alg = "sha256",
827*4882a593Smuzhiyun .impls = {"sha256-lib"},
828*4882a593Smuzhiyun .func = fips_test_sha256_library,
829*4882a593Smuzhiyun .hash = {
830*4882a593Smuzhiyun .message = fips_message,
831*4882a593Smuzhiyun .message_size = sizeof(fips_message),
832*4882a593Smuzhiyun .digest = fips_sha256_digest,
833*4882a593Smuzhiyun .digest_size = sizeof(fips_sha256_digest)
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun },
836*4882a593Smuzhiyun /*
837*4882a593Smuzhiyun * Tests for all SHA-512 implementations. As per the IG, these tests
838*4882a593Smuzhiyun * also fulfill the tests for the corresponding SHA-384 implementations.
839*4882a593Smuzhiyun */
840*4882a593Smuzhiyun {
841*4882a593Smuzhiyun .alg = "sha512",
842*4882a593Smuzhiyun .impls = {
843*4882a593Smuzhiyun /* All implementations of "sha512" */
844*4882a593Smuzhiyun "sha512-generic",
845*4882a593Smuzhiyun "sha512-arm64",
846*4882a593Smuzhiyun "sha512-ce",
847*4882a593Smuzhiyun },
848*4882a593Smuzhiyun .func = fips_test_hash,
849*4882a593Smuzhiyun .hash = {
850*4882a593Smuzhiyun .message = fips_message,
851*4882a593Smuzhiyun .message_size = sizeof(fips_message),
852*4882a593Smuzhiyun .digest = fips_sha512_digest,
853*4882a593Smuzhiyun .digest_size = sizeof(fips_sha512_digest)
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun },
856*4882a593Smuzhiyun /*
857*4882a593Smuzhiyun * Test for HMAC. As per the IG, only one HMAC test is required,
858*4882a593Smuzhiyun * provided that the same HMAC code is shared by all HMAC-SHA*. This is
859*4882a593Smuzhiyun * true in our case. We choose HMAC-SHA256 for the test.
860*4882a593Smuzhiyun *
861*4882a593Smuzhiyun * Note that as per the IG, this can fulfill the test for the underlying
862*4882a593Smuzhiyun * SHA. However, we don't currently rely on this.
863*4882a593Smuzhiyun */
864*4882a593Smuzhiyun {
865*4882a593Smuzhiyun .alg = "hmac(sha256)",
866*4882a593Smuzhiyun .func = fips_test_hash,
867*4882a593Smuzhiyun .hash = {
868*4882a593Smuzhiyun .key = fips_hmac_key,
869*4882a593Smuzhiyun .key_size = sizeof(fips_hmac_key),
870*4882a593Smuzhiyun .message = fips_message,
871*4882a593Smuzhiyun .message_size = sizeof(fips_message),
872*4882a593Smuzhiyun .digest = fips_hmac_sha256_digest,
873*4882a593Smuzhiyun .digest_size = sizeof(fips_hmac_sha256_digest)
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun },
876*4882a593Smuzhiyun /*
877*4882a593Smuzhiyun * Known-answer tests for the SP800-90A DRBG algorithms.
878*4882a593Smuzhiyun *
879*4882a593Smuzhiyun * These test vectors were manually extracted from
880*4882a593Smuzhiyun * https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/drbg/drbgtestvectors.zip.
881*4882a593Smuzhiyun *
882*4882a593Smuzhiyun * The selection of these tests follows the FIPS 140-2 IG as well as
883*4882a593Smuzhiyun * Section 11 of SP800-90A:
884*4882a593Smuzhiyun *
885*4882a593Smuzhiyun * - We must test all DRBG types (HMAC, Hash, and CTR) that the module
886*4882a593Smuzhiyun * implements. However, currently the module only implements
887*4882a593Smuzhiyun * HMAC_DRBG (since CONFIG_CRYPTO_DRBG_CTR and CONFIG_CRYPTO_DRBG_HASH
888*4882a593Smuzhiyun * aren't enabled). Therefore, we only need to test HMAC_DRBG.
889*4882a593Smuzhiyun *
890*4882a593Smuzhiyun * - We only need to test one HMAC variant.
891*4882a593Smuzhiyun *
892*4882a593Smuzhiyun * - We must test all DRBG operations: Instantiate(), Reseed(), and
893*4882a593Smuzhiyun * Generate(). However, a single test sequence with a single output
894*4882a593Smuzhiyun * comparison may cover all three operations, and this is what we do.
895*4882a593Smuzhiyun * Note that Reseed() happens implicitly via the use of the additional
896*4882a593Smuzhiyun * input and also via the use of prediction resistance when enabled.
897*4882a593Smuzhiyun *
898*4882a593Smuzhiyun * - The personalization string, additional input, and prediction
899*4882a593Smuzhiyun * resistance support must be tested. Therefore we have chosen test
900*4882a593Smuzhiyun * vectors that have a nonempty personalization string and nonempty
901*4882a593Smuzhiyun * additional input, and we test the prediction-resistant variant.
902*4882a593Smuzhiyun * Testing the non-prediction-resistant variant is not required.
903*4882a593Smuzhiyun */
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun .alg = "drbg_pr_hmac_sha256",
906*4882a593Smuzhiyun .func = fips_test_drbg,
907*4882a593Smuzhiyun .drbg = {
908*4882a593Smuzhiyun .entropy =
909*4882a593Smuzhiyun "\xc7\xcc\xbc\x67\x7e\x21\x66\x1e\x27\x2b\x63\xdd"
910*4882a593Smuzhiyun "\x3a\x78\xdc\xdf\x66\x6d\x3f\x24\xae\xcf\x37\x01"
911*4882a593Smuzhiyun "\xa9\x0d\x89\x8a\xa7\xdc\x81\x58\xae\xb2\x10\x15"
912*4882a593Smuzhiyun "\x7e\x18\x44\x6d\x13\xea\xdf\x37\x85\xfe\x81\xfb",
913*4882a593Smuzhiyun .entropy_size = 48,
914*4882a593Smuzhiyun .entpr_a =
915*4882a593Smuzhiyun "\x7b\xa1\x91\x5b\x3c\x04\xc4\x1b\x1d\x19\x2f\x1a"
916*4882a593Smuzhiyun "\x18\x81\x60\x3c\x6c\x62\x91\xb7\xe9\xf5\xcb\x96"
917*4882a593Smuzhiyun "\xbb\x81\x6a\xcc\xb5\xae\x55\xb6",
918*4882a593Smuzhiyun .entpr_b =
919*4882a593Smuzhiyun "\x99\x2c\xc7\x78\x7e\x3b\x88\x12\xef\xbe\xd3\xd2"
920*4882a593Smuzhiyun "\x7d\x2a\xa5\x86\xda\x8d\x58\x73\x4a\x0a\xb2\x2e"
921*4882a593Smuzhiyun "\xbb\x4c\x7e\xe3\x9a\xb6\x81\xc1",
922*4882a593Smuzhiyun .entpr_size = 32,
923*4882a593Smuzhiyun .output =
924*4882a593Smuzhiyun "\x95\x6f\x95\xfc\x3b\xb7\xfe\x3e\xd0\x4e\x1a\x14"
925*4882a593Smuzhiyun "\x6c\x34\x7f\x7b\x1d\x0d\x63\x5e\x48\x9c\x69\xe6"
926*4882a593Smuzhiyun "\x46\x07\xd2\x87\xf3\x86\x52\x3d\x98\x27\x5e\xd7"
927*4882a593Smuzhiyun "\x54\xe7\x75\x50\x4f\xfb\x4d\xfd\xac\x2f\x4b\x77"
928*4882a593Smuzhiyun "\xcf\x9e\x8e\xcc\x16\xa2\x24\xcd\x53\xde\x3e\xc5"
929*4882a593Smuzhiyun "\x55\x5d\xd5\x26\x3f\x89\xdf\xca\x8b\x4e\x1e\xb6"
930*4882a593Smuzhiyun "\x88\x78\x63\x5c\xa2\x63\x98\x4e\x6f\x25\x59\xb1"
931*4882a593Smuzhiyun "\x5f\x2b\x23\xb0\x4b\xa5\x18\x5d\xc2\x15\x74\x40"
932*4882a593Smuzhiyun "\x59\x4c\xb4\x1e\xcf\x9a\x36\xfd\x43\xe2\x03\xb8"
933*4882a593Smuzhiyun "\x59\x91\x30\x89\x2a\xc8\x5a\x43\x23\x7c\x73\x72"
934*4882a593Smuzhiyun "\xda\x3f\xad\x2b\xba\x00\x6b\xd1",
935*4882a593Smuzhiyun .out_size = 128,
936*4882a593Smuzhiyun .add_a =
937*4882a593Smuzhiyun "\x18\xe8\x17\xff\xef\x39\xc7\x41\x5c\x73\x03\x03"
938*4882a593Smuzhiyun "\xf6\x3d\xe8\x5f\xc8\xab\xe4\xab\x0f\xad\xe8\xd6"
939*4882a593Smuzhiyun "\x86\x88\x55\x28\xc1\x69\xdd\x76",
940*4882a593Smuzhiyun .add_b =
941*4882a593Smuzhiyun "\xac\x07\xfc\xbe\x87\x0e\xd3\xea\x1f\x7e\xb8\xe7"
942*4882a593Smuzhiyun "\x9d\xec\xe8\xe7\xbc\xf3\x18\x25\x77\x35\x4a\xaa"
943*4882a593Smuzhiyun "\x00\x99\x2a\xdd\x0a\x00\x50\x82",
944*4882a593Smuzhiyun .add_size = 32,
945*4882a593Smuzhiyun .pers =
946*4882a593Smuzhiyun "\xbc\x55\xab\x3c\xf6\x52\xb0\x11\x3d\x7b\x90\xb8"
947*4882a593Smuzhiyun "\x24\xc9\x26\x4e\x5a\x1e\x77\x0d\x3d\x58\x4a\xda"
948*4882a593Smuzhiyun "\xd1\x81\xe9\xf8\xeb\x30\x8f\x6f",
949*4882a593Smuzhiyun .pers_size = 32,
950*4882a593Smuzhiyun }
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun };
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun static int __init __must_check
fips_run_test(const struct fips_test * test)955*4882a593Smuzhiyun fips_run_test(const struct fips_test *test)
956*4882a593Smuzhiyun {
957*4882a593Smuzhiyun int i;
958*4882a593Smuzhiyun int err;
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun /*
961*4882a593Smuzhiyun * If no implementations were specified, then just test the default one.
962*4882a593Smuzhiyun * Otherwise, test the specified list of implementations.
963*4882a593Smuzhiyun */
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun if (test->impls[0] == NULL) {
966*4882a593Smuzhiyun err = test->func(test, test->alg);
967*4882a593Smuzhiyun if (err)
968*4882a593Smuzhiyun pr_emerg("self-tests failed for algorithm %s: %d\n",
969*4882a593Smuzhiyun test->alg, err);
970*4882a593Smuzhiyun return err;
971*4882a593Smuzhiyun }
972*4882a593Smuzhiyun
973*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(test->impls) && test->impls[i] != NULL;
974*4882a593Smuzhiyun i++) {
975*4882a593Smuzhiyun err = test->func(test, test->impls[i]);
976*4882a593Smuzhiyun if (err) {
977*4882a593Smuzhiyun pr_emerg("self-tests failed for algorithm %s, implementation %s: %d\n",
978*4882a593Smuzhiyun test->alg, test->impls[i], err);
979*4882a593Smuzhiyun return err;
980*4882a593Smuzhiyun }
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun return 0;
983*4882a593Smuzhiyun }
984*4882a593Smuzhiyun
fips140_run_selftests(void)985*4882a593Smuzhiyun bool __init fips140_run_selftests(void)
986*4882a593Smuzhiyun {
987*4882a593Smuzhiyun int i;
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun pr_info("running self-tests\n");
990*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(fips140_selftests); i++) {
991*4882a593Smuzhiyun if (fips_run_test(&fips140_selftests[i]) != 0) {
992*4882a593Smuzhiyun /* The caller is responsible for calling panic(). */
993*4882a593Smuzhiyun return false;
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun pr_info("all self-tests passed\n");
997*4882a593Smuzhiyun return true;
998*4882a593Smuzhiyun }
999