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