xref: /freebsd/sys/crypto/openssl/ossl_aes_gcm.h (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2023 Stormshield
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef _OSSL_AES_GCM_H_
28 #define	_OSSL_AES_GCM_H_
29 
30 #include <crypto/openssl/ossl_cipher.h>
31 #include <crypto/rijndael/rijndael.h>
32 
33 struct ossl_gcm_context;
34 
35 struct ossl_aes_gcm_ops {
36 	void (*init)(struct ossl_gcm_context *ctx, const void *key,
37 	    size_t keylen);
38 	void (*setiv)(struct ossl_gcm_context *ctx, const unsigned char *iv,
39 	    size_t ivlen);
40 	int (*aad)(struct ossl_gcm_context *ctx, const unsigned char *aad,
41 	    size_t len);
42 	int (*encrypt)(struct ossl_gcm_context *ctx, const unsigned char *in,
43 	    unsigned char *out, size_t len);
44 	int (*decrypt)(struct ossl_gcm_context *ctx, const unsigned char *in,
45 	    unsigned char *out, size_t len);
46 	int (*finish)(struct ossl_gcm_context *ctx, const unsigned char *tag,
47 	    size_t len);
48 	void (*tag)(struct ossl_gcm_context *ctx, unsigned char *tag,
49 	    size_t len);
50 };
51 
52 #ifndef __SIZEOF_INT128__
53 typedef	struct { uint64_t v[2]; } __uint128_t;
54 #endif
55 
56 struct ossl_gcm_context {
57 	struct {
58 		union {
59 			uint64_t u[2];
60 			uint32_t d[4];
61 			uint8_t c[16];
62 		} Yi, EKi, EK0, len, Xi, H;
63 		__uint128_t Htable[16];
64 		unsigned int mres, ares;
65 	} gcm;
66 
67 	struct {
68 		uint32_t ks[4 * (RIJNDAEL_MAXNR + 1)];
69 		int rounds;
70 	} aes_ks;
71 
72 	const struct ossl_aes_gcm_ops *ops;
73 };
74 
75 #endif /* !_OSSL_AES_GCM_H_ */
76