Home | History | Annotate | Line # | Download | only in aes
      1  1.4  riastrad /*	$NetBSD: t_aes.c,v 1.4 2020/08/17 16:26:02 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad #include <sys/types.h>
     30  1.1  riastrad 
     31  1.1  riastrad #include <crypto/aes/aes.h>
     32  1.3  riastrad #include <crypto/aes/aes_bear.h>
     33  1.2    martin #include <crypto/aes/aes_impl.h>
     34  1.1  riastrad 
     35  1.1  riastrad #if defined(__i386__) || defined(__x86_64__)
     36  1.1  riastrad #include <crypto/aes/arch/x86/aes_ni.h>
     37  1.1  riastrad #include <crypto/aes/arch/x86/aes_sse2.h>
     38  1.1  riastrad #include <crypto/aes/arch/x86/aes_ssse3.h>
     39  1.1  riastrad #include <crypto/aes/arch/x86/aes_via.h>
     40  1.1  riastrad #endif
     41  1.1  riastrad 
     42  1.1  riastrad #ifdef __aarch64__
     43  1.1  riastrad #include <crypto/aes/arch/arm/aes_armv8.h>
     44  1.1  riastrad #endif
     45  1.1  riastrad 
     46  1.4  riastrad #if __ARM_ARCH >= 7
     47  1.1  riastrad #include <crypto/aes/arch/arm/aes_neon.h>
     48  1.1  riastrad #endif
     49  1.1  riastrad 
     50  1.1  riastrad #include <atf-c.h>
     51  1.1  riastrad 
     52  1.1  riastrad ATF_TC(aes_ct_selftest);
     53  1.1  riastrad ATF_TC_HEAD(aes_ct_selftest, tc)
     54  1.1  riastrad {
     55  1.1  riastrad 
     56  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", "BearSSL aes_ct tests");
     57  1.1  riastrad }
     58  1.1  riastrad 
     59  1.1  riastrad ATF_TC_BODY(aes_ct_selftest, tc)
     60  1.1  riastrad {
     61  1.1  riastrad 
     62  1.1  riastrad 	if (aes_bear_impl.ai_probe()) {
     63  1.1  riastrad 		/*
     64  1.1  riastrad 		 * aes_ct is the portable software fallback, so probe
     65  1.1  riastrad 		 * should never fail.
     66  1.1  riastrad 		 */
     67  1.1  riastrad 		atf_tc_fail("BearSSL aes_ct probe failed");
     68  1.1  riastrad 	}
     69  1.1  riastrad 
     70  1.1  riastrad 	if (aes_selftest(&aes_bear_impl))
     71  1.1  riastrad 		atf_tc_fail("BearSSL aes_ct self-test failed");
     72  1.1  riastrad }
     73  1.1  riastrad 
     74  1.1  riastrad #define	AES_SELFTEST(name, impl, descr)					      \
     75  1.1  riastrad ATF_TC(name);								      \
     76  1.1  riastrad ATF_TC_HEAD(name, tc)							      \
     77  1.1  riastrad {									      \
     78  1.1  riastrad 									      \
     79  1.1  riastrad 	atf_tc_set_md_var(tc, "descr", descr);				      \
     80  1.1  riastrad }									      \
     81  1.1  riastrad 									      \
     82  1.1  riastrad ATF_TC_BODY(name, tc)							      \
     83  1.1  riastrad {									      \
     84  1.1  riastrad 									      \
     85  1.1  riastrad 	if ((impl)->ai_probe())						      \
     86  1.1  riastrad 		atf_tc_skip("%s not supported on this hardware",	      \
     87  1.1  riastrad 		    (impl)->ai_name);					      \
     88  1.1  riastrad 	if (aes_selftest(impl))						      \
     89  1.1  riastrad 		atf_tc_fail("%s self-test failed", (impl)->ai_name);	      \
     90  1.1  riastrad }
     91  1.1  riastrad 
     92  1.1  riastrad #ifdef __aarch64__
     93  1.1  riastrad AES_SELFTEST(aes_armv8_selftest, &aes_armv8_impl, "ARMv8.0-AES self-test")
     94  1.1  riastrad #endif
     95  1.1  riastrad 
     96  1.4  riastrad #if __ARM_ARCH >= 7
     97  1.1  riastrad AES_SELFTEST(aes_neon_selftest, &aes_neon_impl, "ARM NEON vpaes self-test")
     98  1.1  riastrad #endif
     99  1.1  riastrad 
    100  1.1  riastrad #ifdef __x86_64__
    101  1.1  riastrad AES_SELFTEST(aes_ni_selftest, &aes_ni_impl, "Intel AES-NI self-test")
    102  1.1  riastrad #endif
    103  1.1  riastrad 
    104  1.1  riastrad #if defined(__i386__) || defined(__x86_64__)
    105  1.1  riastrad AES_SELFTEST(aes_sse2_selftest, &aes_sse2_impl,
    106  1.1  riastrad     "Intel SSE2 bitsliced self-test")
    107  1.1  riastrad AES_SELFTEST(aes_ssse3_selftest, &aes_ssse3_impl,
    108  1.1  riastrad     "Intel SSSE3 vpaes self-test")
    109  1.1  riastrad AES_SELFTEST(aes_via_selftest, &aes_via_impl, "VIA ACE AES self-test")
    110  1.1  riastrad #endif
    111  1.1  riastrad 
    112  1.1  riastrad ATF_TP_ADD_TCS(tp)
    113  1.1  riastrad {
    114  1.1  riastrad 
    115  1.1  riastrad 	ATF_TP_ADD_TC(tp, aes_ct_selftest);
    116  1.1  riastrad 
    117  1.1  riastrad #ifdef __aarch64__
    118  1.1  riastrad 	ATF_TP_ADD_TC(tp, aes_armv8_selftest);
    119  1.1  riastrad #endif
    120  1.1  riastrad 
    121  1.4  riastrad #if __ARM_ARCH >= 7
    122  1.1  riastrad 	ATF_TP_ADD_TC(tp, aes_neon_selftest);
    123  1.1  riastrad #endif
    124  1.1  riastrad 
    125  1.1  riastrad #ifdef __x86_64__
    126  1.1  riastrad 	ATF_TP_ADD_TC(tp, aes_ni_selftest);
    127  1.1  riastrad #endif
    128  1.1  riastrad 
    129  1.1  riastrad #if defined(__i386__) || defined(__x86_64__)
    130  1.1  riastrad 	ATF_TP_ADD_TC(tp, aes_sse2_selftest);
    131  1.1  riastrad 	ATF_TP_ADD_TC(tp, aes_ssse3_selftest);
    132  1.1  riastrad 	ATF_TP_ADD_TC(tp, aes_via_selftest);
    133  1.1  riastrad #endif
    134  1.1  riastrad 
    135  1.1  riastrad 	return atf_no_error();
    136  1.1  riastrad }
    137