xref: /freebsd-13-stable/sys/opencrypto/xform_enc.h (revision 4fbf14e22d7b83de7080a8e491ba14a5785a0ff4)
1 /*	$OpenBSD: xform.h,v 1.8 2001/08/28 12:20:43 ben Exp $	*/
2 
3 /*-
4  * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
5  *
6  * This code was written by Angelos D. Keromytis in Athens, Greece, in
7  * February 2000. Network Security Technologies Inc. (NSTI) kindly
8  * supported the development of this code.
9  *
10  * Copyright (c) 2000 Angelos D. Keromytis
11  * Copyright (c) 2014 The FreeBSD Foundation
12  * All rights reserved.
13  *
14  * Portions of this software were developed by John-Mark Gurney
15  * under sponsorship of the FreeBSD Foundation and
16  * Rubicon Communications, LLC (Netgate).
17  *
18  * Permission to use, copy, and modify this software without fee
19  * is hereby granted, provided that this entire notice is included in
20  * all source code copies of any software which is or includes a copy or
21  * modification of this software.
22  *
23  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
24  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
25  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
26  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
27  * PURPOSE.
28  */
29 
30 #ifndef _CRYPTO_XFORM_ENC_H_
31 #define _CRYPTO_XFORM_ENC_H_
32 
33 #include <sys/malloc.h>
34 #include <sys/errno.h>
35 #include <crypto/rijndael/rijndael.h>
36 #include <crypto/camellia/camellia.h>
37 #include <opencrypto/cryptodev.h>
38 #ifdef _STANDALONE
39 #include <stand.h>
40 #endif
41 
42 #define AESICM_BLOCKSIZE	AES_BLOCK_LEN
43 #define	AES_XTS_BLOCKSIZE	16
44 #define	AES_XTS_IVSIZE		8
45 #define	AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
46 
47 /* Declarations */
48 struct enc_xform {
49 	int type;
50 	char *name;
51 	size_t ctxsize;
52 	uint16_t blocksize;	/* Required input block size -- 1 for stream ciphers. */
53 	uint16_t native_blocksize;	/* Used for stream ciphers. */
54 	uint16_t ivsize;
55 	uint16_t minkey, maxkey;
56 
57 	/*
58 	 * Encrypt/decrypt a single block.  For stream ciphers this
59 	 * encrypts/decrypts a single "native" block.
60 	 */
61 	void (*encrypt) (void *, const uint8_t *, uint8_t *);
62 	void (*decrypt) (void *, const uint8_t *, uint8_t *);
63 	int (*setkey) (void *, const uint8_t *, int len);
64 	void (*reinit) (void *, const uint8_t *, size_t);
65 
66 	/*
67 	 * For stream ciphers, encrypt/decrypt the final partial block
68 	 * of 'len' bytes.
69 	 */
70 	void (*encrypt_last) (void *, const uint8_t *, uint8_t *, size_t len);
71 	void (*decrypt_last) (void *, const uint8_t *, uint8_t *, size_t len);
72 };
73 
74 
75 extern struct enc_xform enc_xform_null;
76 extern struct enc_xform enc_xform_rijndael128;
77 extern struct enc_xform enc_xform_aes_icm;
78 extern struct enc_xform enc_xform_aes_nist_gcm;
79 extern struct enc_xform enc_xform_aes_nist_gmac;
80 extern struct enc_xform enc_xform_aes_xts;
81 extern struct enc_xform enc_xform_camellia;
82 extern struct enc_xform enc_xform_chacha20;
83 extern struct enc_xform enc_xform_chacha20_poly1305;
84 extern struct enc_xform enc_xform_ccm;
85 
86 struct aes_icm_ctx {
87 	uint32_t	ac_ek[4*(RIJNDAEL_MAXNR + 1)];
88 	/* ac_block is initialized to IV */
89 	uint8_t		ac_block[AESICM_BLOCKSIZE];
90 	int		ac_nr;
91 };
92 
93 struct aes_xts_ctx {
94 	rijndael_ctx key1;
95 	rijndael_ctx key2;
96 	uint8_t tweak[AES_XTS_BLOCKSIZE];
97 };
98 
99 #endif /* _CRYPTO_XFORM_ENC_H_ */
100