t_chacha.c revision 1.4
11.4Sriastrad/*	$NetBSD: t_chacha.c,v 1.4 2020/08/17 16:26:02 riastradh Exp $	*/
21.1Sriastrad
31.1Sriastrad/*-
41.1Sriastrad * Copyright (c) 2020 The NetBSD Foundation, Inc.
51.1Sriastrad * All rights reserved.
61.1Sriastrad *
71.1Sriastrad * Redistribution and use in source and binary forms, with or without
81.1Sriastrad * modification, are permitted provided that the following conditions
91.1Sriastrad * are met:
101.1Sriastrad * 1. Redistributions of source code must retain the above copyright
111.1Sriastrad *    notice, this list of conditions and the following disclaimer.
121.1Sriastrad * 2. Redistributions in binary form must reproduce the above copyright
131.1Sriastrad *    notice, this list of conditions and the following disclaimer in the
141.1Sriastrad *    documentation and/or other materials provided with the distribution.
151.1Sriastrad *
161.1Sriastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171.1Sriastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Sriastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Sriastrad * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Sriastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Sriastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Sriastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Sriastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Sriastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Sriastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Sriastrad * POSSIBILITY OF SUCH DAMAGE.
271.1Sriastrad */
281.1Sriastrad
291.1Sriastrad#include <sys/types.h>
301.1Sriastrad
311.1Sriastrad#include <crypto/chacha/chacha.h>
321.1Sriastrad#include <crypto/chacha/chacha_ref.h>
331.1Sriastrad
341.1Sriastrad#if defined(__i386__) || defined(__x86_64__)
351.1Sriastrad#include <crypto/chacha/arch/x86/chacha_sse2.h>
361.1Sriastrad#endif
371.1Sriastrad
381.4Sriastrad#if __ARM_ARCH >= 7
391.1Sriastrad#include <crypto/chacha/arch/arm/chacha_neon.h>
401.1Sriastrad#endif
411.1Sriastrad
421.1Sriastrad#include <atf-c.h>
431.1Sriastrad
441.1SriastradATF_TC(chacha_ref_selftest);
451.1SriastradATF_TC_HEAD(chacha_ref_selftest, tc)
461.1Sriastrad{
471.1Sriastrad
481.1Sriastrad	atf_tc_set_md_var(tc, "descr", "Portable C chacha_ref tests");
491.1Sriastrad}
501.1Sriastrad
511.1SriastradATF_TC_BODY(chacha_ref_selftest, tc)
521.1Sriastrad{
531.1Sriastrad
541.1Sriastrad	if (chacha_ref_impl.ci_probe()) {
551.1Sriastrad		/*
561.1Sriastrad		 * chacha_ref is the portable software fallback, so
571.1Sriastrad		 * probe should never fail.
581.1Sriastrad		 */
591.1Sriastrad		atf_tc_fail("Portable C chacha_ref probe failed");
601.1Sriastrad	}
611.1Sriastrad
621.1Sriastrad	if (chacha_selftest(&chacha_ref_impl))
631.1Sriastrad		atf_tc_fail("Portable C chacha_ref self-test failed");
641.1Sriastrad}
651.1Sriastrad
661.1Sriastrad#define	CHACHA_SELFTEST(name, impl, descr)				      \
671.1SriastradATF_TC(name);								      \
681.1SriastradATF_TC_HEAD(name, tc)							      \
691.1Sriastrad{									      \
701.1Sriastrad									      \
711.1Sriastrad	atf_tc_set_md_var(tc, "descr", descr);				      \
721.1Sriastrad}									      \
731.1Sriastrad									      \
741.1SriastradATF_TC_BODY(name, tc)							      \
751.1Sriastrad{									      \
761.1Sriastrad									      \
771.1Sriastrad	if ((impl)->ci_probe())						      \
781.1Sriastrad		atf_tc_skip("%s not supported on this hardware",	      \
791.1Sriastrad		    (impl)->ci_name);					      \
801.1Sriastrad	if (chacha_selftest(impl))					      \
811.1Sriastrad		atf_tc_fail("%s self-test failed", (impl)->ci_name);	      \
821.1Sriastrad}
831.1Sriastrad
841.4Sriastrad#if __ARM_ARCH >= 7
851.1SriastradCHACHA_SELFTEST(chacha_neon_selftest, &chacha_neon_impl,
861.1Sriastrad    "ARM NEON ChaCha self-test")
871.1Sriastrad#endif
881.1Sriastrad
891.1Sriastrad#if defined(__i386__) || defined(__x86_64__)
901.1SriastradCHACHA_SELFTEST(chacha_sse2_selftest, &chacha_sse2_impl,
911.1Sriastrad    "x86 SSE2 ChaCha self-test")
921.1Sriastrad#endif
931.1Sriastrad
941.1SriastradATF_TP_ADD_TCS(tp)
951.1Sriastrad{
961.1Sriastrad
971.1Sriastrad	ATF_TP_ADD_TC(tp, chacha_ref_selftest);
981.1Sriastrad
991.4Sriastrad#if __ARM_ARCH >= 7
1001.1Sriastrad	ATF_TP_ADD_TC(tp, chacha_neon_selftest);
1011.1Sriastrad#endif
1021.1Sriastrad
1031.1Sriastrad#if defined(__i386__) || defined(__x86_64__)
1041.1Sriastrad	ATF_TP_ADD_TC(tp, chacha_sse2_selftest);
1051.1Sriastrad#endif
1061.1Sriastrad
1071.1Sriastrad	return atf_no_error();
1081.1Sriastrad}
109