xref: /freebsd/sys/crypto/openssl/ossl_arm.h (revision 06c3fb27)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2023 Stormshield
5  * Copyright (c) 2023 Semihalf
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  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15  *    redistribution must be conditioned upon including a substantially
16  *    similar Disclaimer requirement for further binary redistribution.
17  *
18  * NO WARRANTY
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGES.
30  */
31 
32 #ifndef __OSSL_ARM__
33 #define __OSSL_ARM__
34 
35 #include <crypto/openssl/ossl.h>
36 #include <crypto/openssl/ossl_cipher.h>
37 
38 #include <opencrypto/cryptodev.h>
39 
40 struct bsaes_key {
41 	struct ossl_aes_keysched ks;
42 	int converted;
43 #define	BSAES_KEY_SIZE	(128 * (RIJNDAEL_MAXNR - 1) + 2 * AES_BLOCK_LEN)
44 	uint8_t bitslice[BSAES_KEY_SIZE] __aligned(8);
45 } __aligned(8);
46 
47 ossl_cipher_encrypt_t ossl_bsaes_cbc_encrypt;
48 
49 void AES_encrypt(const void *, void *, const void *);
50 
51 static inline void
52 AES_CBC_ENCRYPT(const unsigned char *in, unsigned char *out,
53     size_t length, const void *key, unsigned char *iv, int encrypt)
54 {
55 	struct bsaes_key bsks;
56 	uint32_t iv32[4], scratch[4];
57 
58 	/*
59 	 * bsaes_cbc_encrypt has some special requirements w.r.t input data.
60 	 * The key buffer, that normally holds round keys is used as a scratch
61 	 * space. 128 bytes per round of extra space is required.
62 	 * Another thing is that only decryption is supported.
63 	 * In the case of encryption block chaining has to be done in C.
64 	 */
65 	if (!encrypt) {
66 		memcpy(&bsks.ks, key, sizeof(bsks.ks));
67 		bsks.converted = 0;
68 		ossl_bsaes_cbc_encrypt(in, out, length, &bsks, iv, false);
69 		return;
70 	}
71 
72 	length /= AES_BLOCK_LEN;
73 	memcpy(iv32, iv, AES_BLOCK_LEN);
74 
75 	while (length-- > 0) {
76 		memcpy(scratch, in, AES_BLOCK_LEN);
77 
78 		/* XOR plaintext with IV. */
79 		scratch[0] ^= iv32[0];
80 		scratch[1] ^= iv32[1];
81 		scratch[2] ^= iv32[2];
82 		scratch[3] ^= iv32[3];
83 
84 		AES_encrypt(scratch, out, key);
85 
86 		memcpy(iv32, out, AES_BLOCK_LEN);
87 		in += AES_BLOCK_LEN;
88 		out += AES_BLOCK_LEN;
89 	}
90 
91 	memcpy(iv, iv32, AES_BLOCK_LEN);
92 }
93 
94 #endif /* __OSSL_ARM__ */
95