1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright 2019 Google LLC
4 * Copyright (c) Linux Test Project, 2019-2021
5 */
6
7 /*
8 * Regression test for commit af3ff8045bbf ("crypto: hmac - require that the
9 * underlying hash algorithm is unkeyed"), or CVE-2017-17806. This test
10 * verifies that the hmac template cannot be nested inside itself.
11 */
12
13 #include <errno.h>
14 #include <stdio.h>
15
16 #include "tst_test.h"
17 #include "tst_af_alg.h"
18 #include "lapi/socket.h"
19
test_with_hash_alg(const char * hash_algname)20 static void test_with_hash_alg(const char *hash_algname)
21 {
22 char hmac_algname[64];
23 char key[4096] = { 0 };
24 int ret;
25
26 if (!tst_have_alg("hash", hash_algname))
27 return;
28
29 sprintf(hmac_algname, "hmac(%s)", hash_algname);
30 if (!tst_have_alg("hash", hmac_algname))
31 return;
32
33 sprintf(hmac_algname, "hmac(hmac(%s))", hash_algname);
34
35 ret = tst_try_alg("hash", hmac_algname);
36 if (ret != ENOENT && ret != EINVAL) {
37 int algfd;
38
39 tst_res(TFAIL, "instantiated nested hmac algorithm ('%s')!",
40 hmac_algname);
41
42 /*
43 * Be extra annoying; with the bug, setting a key on
44 * "hmac(hmac(sha3-256-generic))" crashed the kernel.
45 */
46 algfd = tst_alg_setup("hash", hmac_algname, NULL, 0);
47 if (setsockopt(algfd, SOL_ALG, ALG_SET_KEY,
48 key, sizeof(key)) == 0) {
49 tst_res(TFAIL,
50 "set key on nested hmac algorithm ('%s')!",
51 hmac_algname);
52 }
53 } else {
54 tst_res(TPASS,
55 "couldn't instantiate nested hmac algorithm ('%s')",
56 hmac_algname);
57 }
58 }
59
60 /* try several different unkeyed hash algorithms */
61 static const char * const hash_algs[] = {
62 "md5", "md5-generic",
63 "sha1", "sha1-generic",
64 "sha224", "sha224-generic",
65 "sha256", "sha256-generic",
66 "sha3-256", "sha3-256-generic",
67 "sha3-512", "sha3-512-generic",
68 "sm3", "sm3-generic",
69 };
70
do_test(unsigned int i)71 static void do_test(unsigned int i)
72 {
73 test_with_hash_alg(hash_algs[i]);
74 }
75
76 static struct tst_test test = {
77 .test = do_test,
78 .tcnt = ARRAY_SIZE(hash_algs),
79 .tags = (const struct tst_tag[]) {
80 {"linux-git", "af3ff8045bbf"},
81 {"CVE", "2017-17806"},
82 {}
83 }
84 };
85