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