xref: /freebsd-src/sys/opencrypto/xform_enc.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
12155bb23SAllan Jude /*	$OpenBSD: xform.h,v 1.8 2001/08/28 12:20:43 ben Exp $	*/
22155bb23SAllan Jude 
32155bb23SAllan Jude /*-
42155bb23SAllan Jude  * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
52155bb23SAllan Jude  *
62155bb23SAllan Jude  * This code was written by Angelos D. Keromytis in Athens, Greece, in
72155bb23SAllan Jude  * February 2000. Network Security Technologies Inc. (NSTI) kindly
82155bb23SAllan Jude  * supported the development of this code.
92155bb23SAllan Jude  *
102155bb23SAllan Jude  * Copyright (c) 2000 Angelos D. Keromytis
112155bb23SAllan Jude  * Copyright (c) 2014 The FreeBSD Foundation
122155bb23SAllan Jude  * All rights reserved.
132155bb23SAllan Jude  *
142155bb23SAllan Jude  * Portions of this software were developed by John-Mark Gurney
152155bb23SAllan Jude  * under sponsorship of the FreeBSD Foundation and
162155bb23SAllan Jude  * Rubicon Communications, LLC (Netgate).
172155bb23SAllan Jude  *
182155bb23SAllan Jude  * Permission to use, copy, and modify this software without fee
192155bb23SAllan Jude  * is hereby granted, provided that this entire notice is included in
202155bb23SAllan Jude  * all source code copies of any software which is or includes a copy or
212155bb23SAllan Jude  * modification of this software.
222155bb23SAllan Jude  *
232155bb23SAllan Jude  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
242155bb23SAllan Jude  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
252155bb23SAllan Jude  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
262155bb23SAllan Jude  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
272155bb23SAllan Jude  * PURPOSE.
282155bb23SAllan Jude  */
292155bb23SAllan Jude 
302155bb23SAllan Jude #ifndef _CRYPTO_XFORM_ENC_H_
312155bb23SAllan Jude #define _CRYPTO_XFORM_ENC_H_
322155bb23SAllan Jude 
33*46f69ebaSJohn Baldwin #include <sys/types.h>
34*46f69ebaSJohn Baldwin 
352155bb23SAllan Jude #include <crypto/rijndael/rijndael.h>
362155bb23SAllan Jude #include <crypto/camellia/camellia.h>
372155bb23SAllan Jude #include <opencrypto/cryptodev.h>
384f98ffddSJohn Baldwin #ifdef _STANDALONE
394f98ffddSJohn Baldwin #include <stand.h>
404f98ffddSJohn Baldwin #endif
412155bb23SAllan Jude 
422155bb23SAllan Jude #define AESICM_BLOCKSIZE	AES_BLOCK_LEN
432155bb23SAllan Jude #define	AES_XTS_BLOCKSIZE	16
442155bb23SAllan Jude #define	AES_XTS_IVSIZE		8
452155bb23SAllan Jude #define	AES_XTS_ALPHA		0x87	/* GF(2^128) generator polynomial */
462155bb23SAllan Jude 
472155bb23SAllan Jude /* Declarations */
482155bb23SAllan Jude struct enc_xform {
492155bb23SAllan Jude 	int type;
50d8787d4fSMark Johnston 	const char *name;
513e947048SJohn Baldwin 	size_t ctxsize;
52d3d79e96SJohn Baldwin 	uint16_t blocksize;	/* Required input block size -- 1 for stream ciphers. */
53723d8764SJohn Baldwin 	uint16_t native_blocksize;	/* Used for stream ciphers. */
54d3d79e96SJohn Baldwin 	uint16_t ivsize;
55d3d79e96SJohn Baldwin 	uint16_t minkey, maxkey;
56ab91fb6cSJohn Baldwin 	uint16_t macsize;		/* For AEAD ciphers. */
57723d8764SJohn Baldwin 
58d7f0b3ceSJohn Baldwin 	/* Initialize context and set key. */
59d7f0b3ceSJohn Baldwin 	int (*setkey) (void *, const uint8_t *, int len);
60d7f0b3ceSJohn Baldwin 
61d7f0b3ceSJohn Baldwin 	/* Supply context with nonce/IV. */
62d7f0b3ceSJohn Baldwin 	void (*reinit) (void *, const uint8_t *, size_t);
63d7f0b3ceSJohn Baldwin 
64723d8764SJohn Baldwin 	/*
65723d8764SJohn Baldwin 	 * Encrypt/decrypt a single block.  For stream ciphers this
66723d8764SJohn Baldwin 	 * encrypts/decrypts a single "native" block.
67723d8764SJohn Baldwin 	 */
683e947048SJohn Baldwin 	void (*encrypt) (void *, const uint8_t *, uint8_t *);
693e947048SJohn Baldwin 	void (*decrypt) (void *, const uint8_t *, uint8_t *);
70d7f0b3ceSJohn Baldwin 
71d7f0b3ceSJohn Baldwin 	/*
72d7f0b3ceSJohn Baldwin 	 * Encrypt/decrypt multiple blocks.  For stream ciphers this
73d7f0b3ceSJohn Baldwin 	 * encrypts/decrypts multiple "native" blocks.  The fourth
74d7f0b3ceSJohn Baldwin 	 * argument is a count of bytes.
75d7f0b3ceSJohn Baldwin 	 */
76d7f0b3ceSJohn Baldwin 	void (*encrypt_multi) (void *, const uint8_t *, uint8_t *, size_t);
77d7f0b3ceSJohn Baldwin 	void (*decrypt_multi) (void *, const uint8_t *, uint8_t *, size_t);
78723d8764SJohn Baldwin 
792f1f9cceSConrad Meyer 	/*
80723d8764SJohn Baldwin 	 * For stream ciphers, encrypt/decrypt the final partial block
81723d8764SJohn Baldwin 	 * of 'len' bytes.
822f1f9cceSConrad Meyer 	 */
83723d8764SJohn Baldwin 	void (*encrypt_last) (void *, const uint8_t *, uint8_t *, size_t len);
84723d8764SJohn Baldwin 	void (*decrypt_last) (void *, const uint8_t *, uint8_t *, size_t len);
85ab91fb6cSJohn Baldwin 
86ab91fb6cSJohn Baldwin 	/*
87ab91fb6cSJohn Baldwin 	 * For AEAD ciphers, update and generate MAC/tag.
88ab91fb6cSJohn Baldwin 	 */
89ab91fb6cSJohn Baldwin 	int  (*update) (void *, const void *, u_int);
90ab91fb6cSJohn Baldwin 	void (*final) (uint8_t *, void *);
912155bb23SAllan Jude };
922155bb23SAllan Jude 
932155bb23SAllan Jude 
94d8787d4fSMark Johnston extern const struct enc_xform enc_xform_null;
95246982c1SJohn Baldwin extern const struct enc_xform enc_xform_aes_cbc;
96d8787d4fSMark Johnston extern const struct enc_xform enc_xform_aes_icm;
97d8787d4fSMark Johnston extern const struct enc_xform enc_xform_aes_nist_gcm;
98d8787d4fSMark Johnston extern const struct enc_xform enc_xform_aes_nist_gmac;
99d8787d4fSMark Johnston extern const struct enc_xform enc_xform_aes_xts;
100d8787d4fSMark Johnston extern const struct enc_xform enc_xform_camellia;
101d8787d4fSMark Johnston extern const struct enc_xform enc_xform_chacha20;
102d8787d4fSMark Johnston extern const struct enc_xform enc_xform_chacha20_poly1305;
1038f35841fSJohn Baldwin extern const struct enc_xform enc_xform_xchacha20_poly1305;
104d8787d4fSMark Johnston extern const struct enc_xform enc_xform_ccm;
1052155bb23SAllan Jude 
1062155bb23SAllan Jude struct aes_icm_ctx {
107d3d79e96SJohn Baldwin 	uint32_t	ac_ek[4*(RIJNDAEL_MAXNR + 1)];
1085aa0576bSEd Maste 	/* ac_block is initialized to IV */
109d3d79e96SJohn Baldwin 	uint8_t		ac_block[AESICM_BLOCKSIZE];
1102155bb23SAllan Jude 	int		ac_nr;
1112155bb23SAllan Jude };
1122155bb23SAllan Jude 
1132155bb23SAllan Jude struct aes_xts_ctx {
1142155bb23SAllan Jude 	rijndael_ctx key1;
1152155bb23SAllan Jude 	rijndael_ctx key2;
116d3d79e96SJohn Baldwin 	uint8_t tweak[AES_XTS_BLOCKSIZE];
1172155bb23SAllan Jude };
1182155bb23SAllan Jude 
1192155bb23SAllan Jude #endif /* _CRYPTO_XFORM_ENC_H_ */
120