122bd0c97SMitchell Horne /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
322bd0c97SMitchell Horne *
422bd0c97SMitchell Horne * Copyright (c) 2020 The FreeBSD Foundation
522bd0c97SMitchell Horne *
622bd0c97SMitchell Horne * This software was developed by Mitchell Horne <mhorne@FreeBSD.org>
722bd0c97SMitchell Horne * under sponsorship from the FreeBSD Foundation.
822bd0c97SMitchell Horne *
922bd0c97SMitchell Horne * Redistribution and use in source and binary forms, with or without
1022bd0c97SMitchell Horne * modification, are permitted provided that the following conditions
1122bd0c97SMitchell Horne * are met:
1222bd0c97SMitchell Horne * 1. Redistributions of source code must retain the above copyright
1322bd0c97SMitchell Horne * notice, this list of conditions and the following disclaimer.
1422bd0c97SMitchell Horne * 2. Redistributions in binary form must reproduce the above copyright
1522bd0c97SMitchell Horne * notice, this list of conditions and the following disclaimer in the
1622bd0c97SMitchell Horne * documentation and/or other materials provided with the distribution.
1722bd0c97SMitchell Horne *
1822bd0c97SMitchell Horne * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1922bd0c97SMitchell Horne * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2022bd0c97SMitchell Horne * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2122bd0c97SMitchell Horne * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2222bd0c97SMitchell Horne * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2322bd0c97SMitchell Horne * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2422bd0c97SMitchell Horne * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2522bd0c97SMitchell Horne * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2622bd0c97SMitchell Horne * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2722bd0c97SMitchell Horne * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2822bd0c97SMitchell Horne * SUCH DAMAGE.
2922bd0c97SMitchell Horne */
3022bd0c97SMitchell Horne
3122bd0c97SMitchell Horne #include <sys/types.h>
3222bd0c97SMitchell Horne
3322bd0c97SMitchell Horne #include <machine/elf.h>
3422bd0c97SMitchell Horne #include <machine/md_var.h>
3522bd0c97SMitchell Horne
3622bd0c97SMitchell Horne #include <crypto/openssl/ossl.h>
37197ff4c3SKornel Duleba #include <crypto/openssl/ossl_cipher.h>
38*e655cc70SMark Johnston #include <crypto/openssl/arm_arch.h>
3922bd0c97SMitchell Horne
4022bd0c97SMitchell Horne /*
4122bd0c97SMitchell Horne * Feature bits defined in arm_arch.h
4222bd0c97SMitchell Horne */
4322bd0c97SMitchell Horne unsigned int OPENSSL_armcap_P;
4422bd0c97SMitchell Horne
45197ff4c3SKornel Duleba ossl_cipher_setkey_t aes_v8_set_encrypt_key;
46197ff4c3SKornel Duleba ossl_cipher_setkey_t aes_v8_set_decrypt_key;
47197ff4c3SKornel Duleba
48197ff4c3SKornel Duleba ossl_cipher_setkey_t vpaes_set_encrypt_key;
49197ff4c3SKornel Duleba ossl_cipher_setkey_t vpaes_set_decrypt_key;
50197ff4c3SKornel Duleba
5122bd0c97SMitchell Horne void
ossl_cpuid(struct ossl_softc * sc)52197ff4c3SKornel Duleba ossl_cpuid(struct ossl_softc *sc)
5322bd0c97SMitchell Horne {
5422bd0c97SMitchell Horne /* SHA features */
5522bd0c97SMitchell Horne if ((elf_hwcap & HWCAP_SHA1) != 0)
5622bd0c97SMitchell Horne OPENSSL_armcap_P |= ARMV8_SHA1;
5722bd0c97SMitchell Horne if ((elf_hwcap & HWCAP_SHA2) != 0)
5822bd0c97SMitchell Horne OPENSSL_armcap_P |= ARMV8_SHA256;
5922bd0c97SMitchell Horne if ((elf_hwcap & HWCAP_SHA512) != 0)
6022bd0c97SMitchell Horne OPENSSL_armcap_P |= ARMV8_SHA512;
6122bd0c97SMitchell Horne
6222bd0c97SMitchell Horne /* AES features */
6322bd0c97SMitchell Horne if ((elf_hwcap & HWCAP_AES) != 0)
6422bd0c97SMitchell Horne OPENSSL_armcap_P |= ARMV8_AES;
6522bd0c97SMitchell Horne if ((elf_hwcap & HWCAP_PMULL) != 0)
6622bd0c97SMitchell Horne OPENSSL_armcap_P |= ARMV8_PMULL;
67197ff4c3SKornel Duleba
68197ff4c3SKornel Duleba if ((OPENSSL_armcap_P & ARMV8_AES) == 0 &&
69197ff4c3SKornel Duleba (OPENSSL_armcap_P & ARMV7_NEON) == 0) {
70197ff4c3SKornel Duleba sc->has_aes = false;
71197ff4c3SKornel Duleba return;
72197ff4c3SKornel Duleba }
73197ff4c3SKornel Duleba sc->has_aes = true;
74197ff4c3SKornel Duleba if (OPENSSL_armcap_P & ARMV8_AES) {
75197ff4c3SKornel Duleba ossl_cipher_aes_cbc.set_encrypt_key = aes_v8_set_encrypt_key;
76197ff4c3SKornel Duleba ossl_cipher_aes_cbc.set_decrypt_key = aes_v8_set_decrypt_key;
77197ff4c3SKornel Duleba } else {
78197ff4c3SKornel Duleba ossl_cipher_aes_cbc.set_encrypt_key = vpaes_set_encrypt_key;
79197ff4c3SKornel Duleba ossl_cipher_aes_cbc.set_decrypt_key = vpaes_set_decrypt_key;
80197ff4c3SKornel Duleba }
8122bd0c97SMitchell Horne }
82