xref: /netbsd-src/tests/sys/crypto/chacha/t_chacha.c (revision 7f7b51ca345b9dfc244612b23b59d17cb36e9841)
1 /*	$NetBSD: t_chacha.c,v 1.4 2020/08/17 16:26:02 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 
31 #include <crypto/chacha/chacha.h>
32 #include <crypto/chacha/chacha_ref.h>
33 
34 #if defined(__i386__) || defined(__x86_64__)
35 #include <crypto/chacha/arch/x86/chacha_sse2.h>
36 #endif
37 
38 #if __ARM_ARCH >= 7
39 #include <crypto/chacha/arch/arm/chacha_neon.h>
40 #endif
41 
42 #include <atf-c.h>
43 
44 ATF_TC(chacha_ref_selftest);
ATF_TC_HEAD(chacha_ref_selftest,tc)45 ATF_TC_HEAD(chacha_ref_selftest, tc)
46 {
47 
48 	atf_tc_set_md_var(tc, "descr", "Portable C chacha_ref tests");
49 }
50 
ATF_TC_BODY(chacha_ref_selftest,tc)51 ATF_TC_BODY(chacha_ref_selftest, tc)
52 {
53 
54 	if (chacha_ref_impl.ci_probe()) {
55 		/*
56 		 * chacha_ref is the portable software fallback, so
57 		 * probe should never fail.
58 		 */
59 		atf_tc_fail("Portable C chacha_ref probe failed");
60 	}
61 
62 	if (chacha_selftest(&chacha_ref_impl))
63 		atf_tc_fail("Portable C chacha_ref self-test failed");
64 }
65 
66 #define	CHACHA_SELFTEST(name, impl, descr)				      \
67 ATF_TC(name);								      \
68 ATF_TC_HEAD(name, tc)							      \
69 {									      \
70 									      \
71 	atf_tc_set_md_var(tc, "descr", descr);				      \
72 }									      \
73 									      \
74 ATF_TC_BODY(name, tc)							      \
75 {									      \
76 									      \
77 	if ((impl)->ci_probe())						      \
78 		atf_tc_skip("%s not supported on this hardware",	      \
79 		    (impl)->ci_name);					      \
80 	if (chacha_selftest(impl))					      \
81 		atf_tc_fail("%s self-test failed", (impl)->ci_name);	      \
82 }
83 
84 #if __ARM_ARCH >= 7
85 CHACHA_SELFTEST(chacha_neon_selftest, &chacha_neon_impl,
86     "ARM NEON ChaCha self-test")
87 #endif
88 
89 #if defined(__i386__) || defined(__x86_64__)
90 CHACHA_SELFTEST(chacha_sse2_selftest, &chacha_sse2_impl,
91     "x86 SSE2 ChaCha self-test")
92 #endif
93 
ATF_TP_ADD_TCS(tp)94 ATF_TP_ADD_TCS(tp)
95 {
96 
97 	ATF_TP_ADD_TC(tp, chacha_ref_selftest);
98 
99 #if __ARM_ARCH >= 7
100 	ATF_TP_ADD_TC(tp, chacha_neon_selftest);
101 #endif
102 
103 #if defined(__i386__) || defined(__x86_64__)
104 	ATF_TP_ADD_TC(tp, chacha_sse2_selftest);
105 #endif
106 
107 	return atf_no_error();
108 }
109