xref: /freebsd/sys/opencrypto/xform_enc.h (revision a90b9d0159070121c221b966469c3e36d912bf82)
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/types.h>
34 
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 	const 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 	uint16_t macsize;		/* For AEAD ciphers. */
57 
58 	/* Initialize context and set key. */
59 	int (*setkey) (void *, const uint8_t *, int len);
60 
61 	/* Supply context with nonce/IV. */
62 	void (*reinit) (void *, const uint8_t *, size_t);
63 
64 	/*
65 	 * Encrypt/decrypt a single block.  For stream ciphers this
66 	 * encrypts/decrypts a single "native" block.
67 	 */
68 	void (*encrypt) (void *, const uint8_t *, uint8_t *);
69 	void (*decrypt) (void *, const uint8_t *, uint8_t *);
70 
71 	/*
72 	 * Encrypt/decrypt multiple blocks.  For stream ciphers this
73 	 * encrypts/decrypts multiple "native" blocks.  The fourth
74 	 * argument is a count of bytes.
75 	 */
76 	void (*encrypt_multi) (void *, const uint8_t *, uint8_t *, size_t);
77 	void (*decrypt_multi) (void *, const uint8_t *, uint8_t *, size_t);
78 
79 	/*
80 	 * For stream ciphers, encrypt/decrypt the final partial block
81 	 * of 'len' bytes.
82 	 */
83 	void (*encrypt_last) (void *, const uint8_t *, uint8_t *, size_t len);
84 	void (*decrypt_last) (void *, const uint8_t *, uint8_t *, size_t len);
85 
86 	/*
87 	 * For AEAD ciphers, update and generate MAC/tag.
88 	 */
89 	int  (*update) (void *, const void *, u_int);
90 	void (*final) (uint8_t *, void *);
91 };
92 
93 
94 extern const struct enc_xform enc_xform_null;
95 extern const struct enc_xform enc_xform_aes_cbc;
96 extern const struct enc_xform enc_xform_aes_icm;
97 extern const struct enc_xform enc_xform_aes_nist_gcm;
98 extern const struct enc_xform enc_xform_aes_nist_gmac;
99 extern const struct enc_xform enc_xform_aes_xts;
100 extern const struct enc_xform enc_xform_camellia;
101 extern const struct enc_xform enc_xform_chacha20;
102 extern const struct enc_xform enc_xform_chacha20_poly1305;
103 extern const struct enc_xform enc_xform_xchacha20_poly1305;
104 extern const struct enc_xform enc_xform_ccm;
105 
106 struct aes_icm_ctx {
107 	uint32_t	ac_ek[4*(RIJNDAEL_MAXNR + 1)];
108 	/* ac_block is initialized to IV */
109 	uint8_t		ac_block[AESICM_BLOCKSIZE];
110 	int		ac_nr;
111 };
112 
113 struct aes_xts_ctx {
114 	rijndael_ctx key1;
115 	rijndael_ctx key2;
116 	uint8_t tweak[AES_XTS_BLOCKSIZE];
117 };
118 
119 #endif /* _CRYPTO_XFORM_ENC_H_ */
120