/*-1* SPDX-License-Identifier: BSD-2-Clause-FreeBSD2*3* Copyright (c) 2023 Stormshield4* Copyright (c) 2023 Semihalf5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer,11* without modification.12* 2. Redistributions in binary form must reproduce at minimum a disclaimer13* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any14* redistribution must be conditioned upon including a substantially15* similar Disclaimer requirement for further binary redistribution.16*17* NO WARRANTY18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY21* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL22* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,23* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER26* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF28* THE POSSIBILITY OF SUCH DAMAGES.29*/3031#ifndef __OSSL_ARM__32#define __OSSL_ARM__3334#include <crypto/openssl/ossl.h>35#include <crypto/openssl/ossl_cipher.h>3637#include <opencrypto/cryptodev.h>3839struct bsaes_key {40struct ossl_aes_keysched ks;41int converted;42#define BSAES_KEY_SIZE (128 * (RIJNDAEL_MAXNR - 1) + 2 * AES_BLOCK_LEN)43uint8_t bitslice[BSAES_KEY_SIZE] __aligned(8);44} __aligned(8);4546ossl_cipher_encrypt_t ossl_bsaes_cbc_encrypt;4748void AES_encrypt(const void *, void *, const void *);4950static inline void51AES_CBC_ENCRYPT(const unsigned char *in, unsigned char *out,52size_t length, const void *key, unsigned char *iv, int encrypt)53{54struct bsaes_key bsks;55uint32_t iv32[4], scratch[4];5657/*58* bsaes_cbc_encrypt has some special requirements w.r.t input data.59* The key buffer, that normally holds round keys is used as a scratch60* space. 128 bytes per round of extra space is required.61* Another thing is that only decryption is supported.62* In the case of encryption block chaining has to be done in C.63*/64if (!encrypt) {65memcpy(&bsks.ks, key, sizeof(bsks.ks));66bsks.converted = 0;67ossl_bsaes_cbc_encrypt(in, out, length, &bsks, iv, false);68return;69}7071length /= AES_BLOCK_LEN;72memcpy(iv32, iv, AES_BLOCK_LEN);7374while (length-- > 0) {75memcpy(scratch, in, AES_BLOCK_LEN);7677/* XOR plaintext with IV. */78scratch[0] ^= iv32[0];79scratch[1] ^= iv32[1];80scratch[2] ^= iv32[2];81scratch[3] ^= iv32[3];8283AES_encrypt(scratch, out, key);8485memcpy(iv32, out, AES_BLOCK_LEN);86in += AES_BLOCK_LEN;87out += AES_BLOCK_LEN;88}8990memcpy(iv, iv32, AES_BLOCK_LEN);91}9293#endif /* __OSSL_ARM__ */949596