xref: /titanic_53/usr/src/common/crypto/modes/modes.h (revision 16239bc82c111618343e0a5b1a70e0fc702d00e0)
123c57df7Smcpowers /*
223c57df7Smcpowers  * CDDL HEADER START
323c57df7Smcpowers  *
423c57df7Smcpowers  * The contents of this file are subject to the terms of the
523c57df7Smcpowers  * Common Development and Distribution License (the "License").
623c57df7Smcpowers  * You may not use this file except in compliance with the License.
723c57df7Smcpowers  *
823c57df7Smcpowers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
923c57df7Smcpowers  * or http://www.opensolaris.org/os/licensing.
1023c57df7Smcpowers  * See the License for the specific language governing permissions
1123c57df7Smcpowers  * and limitations under the License.
1223c57df7Smcpowers  *
1323c57df7Smcpowers  * When distributing Covered Code, include this CDDL HEADER in each
1423c57df7Smcpowers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1523c57df7Smcpowers  * If applicable, add the following below this CDDL HEADER, with the
1623c57df7Smcpowers  * fields enclosed by brackets "[]" replaced with your own identifying
1723c57df7Smcpowers  * information: Portions Copyright [yyyy] [name of copyright owner]
1823c57df7Smcpowers  *
1923c57df7Smcpowers  * CDDL HEADER END
2023c57df7Smcpowers  */
2123c57df7Smcpowers /*
2223c57df7Smcpowers  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2323c57df7Smcpowers  * Use is subject to license terms.
2423c57df7Smcpowers  */
2523c57df7Smcpowers 
2623c57df7Smcpowers #ifndef	_COMMON_CRYPTO_MODES_H
2723c57df7Smcpowers #define	_COMMON_CRYPTO_MODES_H
2823c57df7Smcpowers 
2923c57df7Smcpowers #ifdef	__cplusplus
3023c57df7Smcpowers extern "C" {
3123c57df7Smcpowers #endif
3223c57df7Smcpowers 
3323c57df7Smcpowers #include <sys/strsun.h>
3423c57df7Smcpowers #include <sys/systm.h>
3523c57df7Smcpowers #include <sys/sysmacros.h>
3623c57df7Smcpowers #include <sys/types.h>
3723c57df7Smcpowers #include <sys/errno.h>
3823c57df7Smcpowers #include <sys/rwlock.h>
3923c57df7Smcpowers #include <sys/kmem.h>
4023c57df7Smcpowers #include <sys/crypto/common.h>
4123c57df7Smcpowers #include <sys/crypto/impl.h>
4223c57df7Smcpowers 
4323c57df7Smcpowers #define	ECB_MODE			0x00000002
4423c57df7Smcpowers #define	CBC_MODE			0x00000004
4523c57df7Smcpowers #define	CTR_MODE			0x00000008
4623c57df7Smcpowers #define	CCM_MODE			0x00000010
4723c57df7Smcpowers 
4823c57df7Smcpowers /*
4923c57df7Smcpowers  * cc_keysched:		Pointer to key schedule.
5023c57df7Smcpowers  *
5123c57df7Smcpowers  * cc_keysched_len:	Length of the key schedule.
5223c57df7Smcpowers  *
5323c57df7Smcpowers  * cc_remainder:	This is for residual data, i.e. data that can't
5423c57df7Smcpowers  *			be processed because there are too few bytes.
5523c57df7Smcpowers  *			Must wait until more data arrives.
5623c57df7Smcpowers  *
5723c57df7Smcpowers  * cc_remainder_len:	Number of bytes in cc_remainder.
5823c57df7Smcpowers  *
5923c57df7Smcpowers  * cc_iv:		Scratch buffer that sometimes contains the IV.
6023c57df7Smcpowers  *
6123c57df7Smcpowers  * cc_lastp:		Pointer to previous block of ciphertext.
6223c57df7Smcpowers  *
6323c57df7Smcpowers  * cc_copy_to:		Pointer to where encrypted residual data needs
6423c57df7Smcpowers  *			to be copied.
6523c57df7Smcpowers  *
6623c57df7Smcpowers  * cc_flags:		PROVIDER_OWNS_KEY_SCHEDULE
6723c57df7Smcpowers  *			When a context is freed, it is necessary
6823c57df7Smcpowers  *			to know whether the key schedule was allocated
6923c57df7Smcpowers  *			by the caller, or internally, e.g. an init routine.
7023c57df7Smcpowers  *			If allocated by the latter, then it needs to be freed.
7123c57df7Smcpowers  *
7223c57df7Smcpowers  *			ECB_MODE, CBC_MODE, CTR_MODE, or CCM_MODE
7323c57df7Smcpowers  */
7423c57df7Smcpowers struct common_ctx {
7523c57df7Smcpowers 	void *cc_keysched;
7623c57df7Smcpowers 	size_t cc_keysched_len;
7723c57df7Smcpowers 	uint64_t cc_iv[2];
7823c57df7Smcpowers 	uint64_t cc_remainder[2];
7923c57df7Smcpowers 	size_t cc_remainder_len;
8023c57df7Smcpowers 	uint8_t *cc_lastp;
8123c57df7Smcpowers 	uint8_t *cc_copy_to;
8223c57df7Smcpowers 	uint32_t cc_flags;
8323c57df7Smcpowers };
8423c57df7Smcpowers 
8523c57df7Smcpowers typedef struct common_ctx common_ctx_t;
8623c57df7Smcpowers 
87*16239bc8SMark Powers typedef struct ecb_ctx {
88*16239bc8SMark Powers 	struct common_ctx ecb_common;
89*16239bc8SMark Powers 	uint64_t ecb_lastblock[2];
90*16239bc8SMark Powers } ecb_ctx_t;
91*16239bc8SMark Powers 
92*16239bc8SMark Powers #define	ecb_keysched		ecb_common.cc_keysched
93*16239bc8SMark Powers #define	ecb_keysched_len	ecb_common.cc_keysched_len
94*16239bc8SMark Powers #define	ecb_iv			ecb_common.cc_iv
95*16239bc8SMark Powers #define	ecb_remainder		ecb_common.cc_remainder
96*16239bc8SMark Powers #define	ecb_remainder_len	ecb_common.cc_remainder_len
97*16239bc8SMark Powers #define	ecb_lastp		ecb_common.cc_lastp
98*16239bc8SMark Powers #define	ecb_copy_to		ecb_common.cc_copy_to
99*16239bc8SMark Powers #define	ecb_flags		ecb_common.cc_flags
100*16239bc8SMark Powers 
101*16239bc8SMark Powers typedef struct cbc_ctx {
102*16239bc8SMark Powers 	struct common_ctx cbc_common;
103*16239bc8SMark Powers 	uint64_t cbc_lastblock[2];
104*16239bc8SMark Powers } cbc_ctx_t;
105*16239bc8SMark Powers 
106*16239bc8SMark Powers #define	cbc_keysched		cbc_common.cc_keysched
107*16239bc8SMark Powers #define	cbc_keysched_len	cbc_common.cc_keysched_len
108*16239bc8SMark Powers #define	cbc_iv			cbc_common.cc_iv
109*16239bc8SMark Powers #define	cbc_remainder		cbc_common.cc_remainder
110*16239bc8SMark Powers #define	cbc_remainder_len	cbc_common.cc_remainder_len
111*16239bc8SMark Powers #define	cbc_lastp		cbc_common.cc_lastp
112*16239bc8SMark Powers #define	cbc_copy_to		cbc_common.cc_copy_to
113*16239bc8SMark Powers #define	cbc_flags		cbc_common.cc_flags
114*16239bc8SMark Powers 
115*16239bc8SMark Powers /*
116*16239bc8SMark Powers  * ctr_lower_mask		Bit-mask for lower 8 bytes of counter block.
117*16239bc8SMark Powers  * ctr_upper_mask		Bit-mask for upper 8 bytes of counter block.
118*16239bc8SMark Powers  */
11923c57df7Smcpowers typedef struct ctr_ctx {
12023c57df7Smcpowers 	struct common_ctx ctr_common;
121*16239bc8SMark Powers 	uint64_t ctr_lower_mask;
122*16239bc8SMark Powers 	uint64_t ctr_upper_mask;
12323c57df7Smcpowers 	uint32_t ctr_tmp[4];
12423c57df7Smcpowers } ctr_ctx_t;
12523c57df7Smcpowers 
12623c57df7Smcpowers /*
12723c57df7Smcpowers  * ctr_cb			Counter block.
12823c57df7Smcpowers  */
12923c57df7Smcpowers #define	ctr_keysched		ctr_common.cc_keysched
13023c57df7Smcpowers #define	ctr_keysched_len	ctr_common.cc_keysched_len
13123c57df7Smcpowers #define	ctr_cb			ctr_common.cc_iv
13223c57df7Smcpowers #define	ctr_remainder		ctr_common.cc_remainder
13323c57df7Smcpowers #define	ctr_remainder_len	ctr_common.cc_remainder_len
13423c57df7Smcpowers #define	ctr_lastp		ctr_common.cc_lastp
13523c57df7Smcpowers #define	ctr_copy_to		ctr_common.cc_copy_to
13623c57df7Smcpowers #define	ctr_flags		ctr_common.cc_flags
13723c57df7Smcpowers 
13823c57df7Smcpowers /*
13923c57df7Smcpowers  *
14023c57df7Smcpowers  * ccm_mac_len:		Stores length of the MAC in CCM mode.
14123c57df7Smcpowers  * ccm_mac_buf:		Stores the intermediate value for MAC in CCM encrypt.
14223c57df7Smcpowers  *			In CCM decrypt, stores the input MAC value.
14323c57df7Smcpowers  * ccm_data_len:	Length of the plaintext for CCM mode encrypt, or
14423c57df7Smcpowers  *			length of the ciphertext for CCM mode decrypt.
14523c57df7Smcpowers  * ccm_processed_data_len:
14623c57df7Smcpowers  *			Length of processed plaintext in CCM mode encrypt,
14723c57df7Smcpowers  *			or length of processed ciphertext for CCM mode decrypt.
14823c57df7Smcpowers  * ccm_processed_mac_len:
14923c57df7Smcpowers  *			Length of MAC data accumulated in CCM mode decrypt.
15023c57df7Smcpowers  *
15123c57df7Smcpowers  * ccm_pt_buf:		Only used in CCM mode decrypt.  It stores the
15223c57df7Smcpowers  *			decrypted plaintext to be returned when
15323c57df7Smcpowers  *			MAC verification succeeds in decrypt_final.
15423c57df7Smcpowers  *			Memory for this should be allocated in the AES module.
15523c57df7Smcpowers  *
15623c57df7Smcpowers  */
15723c57df7Smcpowers typedef struct ccm_ctx {
15823c57df7Smcpowers 	struct common_ctx ccm_common;
15923c57df7Smcpowers 	uint32_t ccm_tmp[4];
16023c57df7Smcpowers 	size_t ccm_mac_len;
16123c57df7Smcpowers 	uint64_t ccm_mac_buf[2];
16223c57df7Smcpowers 	size_t ccm_data_len;
16323c57df7Smcpowers 	size_t ccm_processed_data_len;
16423c57df7Smcpowers 	size_t ccm_processed_mac_len;
16523c57df7Smcpowers 	uint8_t *ccm_pt_buf;
16623c57df7Smcpowers 	uint64_t ccm_mac_input_buf[2];
167*16239bc8SMark Powers 	uint64_t ccm_counter_mask;
16823c57df7Smcpowers } ccm_ctx_t;
16923c57df7Smcpowers 
17023c57df7Smcpowers #define	ccm_keysched		ccm_common.cc_keysched
17123c57df7Smcpowers #define	ccm_keysched_len	ccm_common.cc_keysched_len
17223c57df7Smcpowers #define	ccm_cb			ccm_common.cc_iv
17323c57df7Smcpowers #define	ccm_remainder		ccm_common.cc_remainder
17423c57df7Smcpowers #define	ccm_remainder_len	ccm_common.cc_remainder_len
17523c57df7Smcpowers #define	ccm_lastp		ccm_common.cc_lastp
17623c57df7Smcpowers #define	ccm_copy_to		ccm_common.cc_copy_to
17723c57df7Smcpowers #define	ccm_flags		ccm_common.cc_flags
17823c57df7Smcpowers 
17923c57df7Smcpowers typedef struct aes_ctx {
18023c57df7Smcpowers 	union {
18123c57df7Smcpowers 		ecb_ctx_t acu_ecb;
18223c57df7Smcpowers 		cbc_ctx_t acu_cbc;
18323c57df7Smcpowers 		ctr_ctx_t acu_ctr;
18423c57df7Smcpowers #ifdef _KERNEL
18523c57df7Smcpowers 		ccm_ctx_t acu_ccm;
18623c57df7Smcpowers #endif
18723c57df7Smcpowers 	} acu;
18823c57df7Smcpowers } aes_ctx_t;
18923c57df7Smcpowers 
190*16239bc8SMark Powers #define	ac_flags		acu.acu_ecb.ecb_common.cc_flags
191*16239bc8SMark Powers #define	ac_remainder_len	acu.acu_ecb.ecb_common.cc_remainder_len
192*16239bc8SMark Powers #define	ac_keysched		acu.acu_ecb.ecb_common.cc_keysched
193*16239bc8SMark Powers #define	ac_keysched_len		acu.acu_ecb.ecb_common.cc_keysched_len
194*16239bc8SMark Powers #define	ac_iv			acu.acu_ecb.ecb_common.cc_iv
195*16239bc8SMark Powers #define	ac_lastp		acu.acu_ecb.ecb_common.cc_lastp
19623c57df7Smcpowers #define	ac_pt_buf		acu.acu_ccm.ccm_pt_buf
19723c57df7Smcpowers #define	ac_mac_len		acu.acu_ccm.ccm_mac_len
19823c57df7Smcpowers #define	ac_data_len		acu.acu_ccm.ccm_data_len
19923c57df7Smcpowers #define	ac_processed_mac_len	acu.acu_ccm.ccm_processed_mac_len
20023c57df7Smcpowers #define	ac_processed_data_len	acu.acu_ccm.ccm_processed_data_len
20123c57df7Smcpowers 
20223c57df7Smcpowers typedef struct blowfish_ctx {
20323c57df7Smcpowers 	union {
20423c57df7Smcpowers 		ecb_ctx_t bcu_ecb;
20523c57df7Smcpowers 		cbc_ctx_t bcu_cbc;
20623c57df7Smcpowers 	} bcu;
20723c57df7Smcpowers } blowfish_ctx_t;
20823c57df7Smcpowers 
209*16239bc8SMark Powers #define	bc_flags		bcu.bcu_ecb.ecb_common.cc_flags
210*16239bc8SMark Powers #define	bc_remainder_len	bcu.bcu_ecb.ecb_common.cc_remainder_len
211*16239bc8SMark Powers #define	bc_keysched		bcu.bcu_ecb.ecb_common.cc_keysched
212*16239bc8SMark Powers #define	bc_keysched_len		bcu.bcu_ecb.ecb_common.cc_keysched_len
213*16239bc8SMark Powers #define	bc_iv			bcu.bcu_ecb.ecb_common.cc_iv
214*16239bc8SMark Powers #define	bc_lastp		bcu.bcu_ecb.ecb_common.cc_lastp
21523c57df7Smcpowers 
21623c57df7Smcpowers typedef struct des_ctx {
21723c57df7Smcpowers 	union {
21823c57df7Smcpowers 		ecb_ctx_t dcu_ecb;
21923c57df7Smcpowers 		cbc_ctx_t dcu_cbc;
22023c57df7Smcpowers 	} dcu;
22123c57df7Smcpowers } des_ctx_t;
22223c57df7Smcpowers 
223*16239bc8SMark Powers #define	dc_flags		dcu.dcu_ecb.ecb_common.cc_flags
224*16239bc8SMark Powers #define	dc_remainder_len	dcu.dcu_ecb.ecb_common.cc_remainder_len
225*16239bc8SMark Powers #define	dc_keysched		dcu.dcu_ecb.ecb_common.cc_keysched
226*16239bc8SMark Powers #define	dc_keysched_len		dcu.dcu_ecb.ecb_common.cc_keysched_len
227*16239bc8SMark Powers #define	dc_iv			dcu.dcu_ecb.ecb_common.cc_iv
228*16239bc8SMark Powers #define	dc_lastp		dcu.dcu_ecb.ecb_common.cc_lastp
22923c57df7Smcpowers 
230*16239bc8SMark Powers extern int ecb_cipher_contiguous_blocks(ecb_ctx_t *, char *, size_t,
23123c57df7Smcpowers     crypto_data_t *, size_t, int (*cipher)(const void *, const uint8_t *,
23223c57df7Smcpowers     uint8_t *));
23323c57df7Smcpowers 
23423c57df7Smcpowers extern int cbc_encrypt_contiguous_blocks(cbc_ctx_t *, char *, size_t,
23523c57df7Smcpowers     crypto_data_t *, size_t,
23623c57df7Smcpowers     int (*encrypt)(const void *, const uint8_t *, uint8_t *),
23723c57df7Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
23823c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
23923c57df7Smcpowers 
24023c57df7Smcpowers extern int cbc_decrypt_contiguous_blocks(cbc_ctx_t *, char *, size_t,
24123c57df7Smcpowers     crypto_data_t *, size_t,
24223c57df7Smcpowers     int (*decrypt)(const void *, const uint8_t *, uint8_t *),
24323c57df7Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
24423c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
24523c57df7Smcpowers 
24623c57df7Smcpowers extern int ctr_mode_contiguous_blocks(ctr_ctx_t *, char *, size_t,
24723c57df7Smcpowers     crypto_data_t *, size_t,
24823c57df7Smcpowers     int (*cipher)(const void *, const uint8_t *, uint8_t *),
24923c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
25023c57df7Smcpowers 
25123c57df7Smcpowers extern int ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,
25223c57df7Smcpowers     crypto_data_t *, size_t,
25323c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
25423c57df7Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
25523c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
25623c57df7Smcpowers 
25723c57df7Smcpowers extern int ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,
25823c57df7Smcpowers     crypto_data_t *, size_t,
25923c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
26023c57df7Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
26123c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
26223c57df7Smcpowers 
26323c57df7Smcpowers int ccm_encrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,
26423c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
26523c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
26623c57df7Smcpowers 
26723c57df7Smcpowers extern int ccm_decrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,
26823c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
26923c57df7Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
27023c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
27123c57df7Smcpowers 
27223c57df7Smcpowers extern int ctr_mode_final(ctr_ctx_t *, crypto_data_t *,
27323c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *));
27423c57df7Smcpowers 
27523c57df7Smcpowers extern int cbc_init_ctx(cbc_ctx_t *, char *, size_t, size_t,
27623c57df7Smcpowers     void (*copy_block)(uint8_t *, uint64_t *));
27723c57df7Smcpowers 
27823c57df7Smcpowers extern int ctr_init_ctx(ctr_ctx_t *, ulong_t, uint8_t *,
27923c57df7Smcpowers     void (*copy_block)(uint8_t *, uint8_t *));
28023c57df7Smcpowers 
28123c57df7Smcpowers extern int ccm_init_ctx(ccm_ctx_t *, char *, int, boolean_t, size_t,
28223c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
28323c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
28423c57df7Smcpowers 
28523c57df7Smcpowers extern void calculate_ccm_mac(ccm_ctx_t *, uint8_t *,
28623c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *));
28723c57df7Smcpowers 
28823c57df7Smcpowers extern void crypto_init_ptrs(crypto_data_t *, void **, offset_t *);
28923c57df7Smcpowers extern void crypto_get_ptrs(crypto_data_t *, void **, offset_t *,
29023c57df7Smcpowers     uint8_t **, size_t *, uint8_t **, size_t);
29123c57df7Smcpowers 
29223c57df7Smcpowers extern void *ecb_alloc_ctx(int);
29323c57df7Smcpowers extern void *cbc_alloc_ctx(int);
29423c57df7Smcpowers extern void *ctr_alloc_ctx(int);
29523c57df7Smcpowers extern void *ccm_alloc_ctx(int);
29623c57df7Smcpowers extern void crypto_free_mode_ctx(void *);
29723c57df7Smcpowers 
29823c57df7Smcpowers #ifdef	__cplusplus
29923c57df7Smcpowers }
30023c57df7Smcpowers #endif
30123c57df7Smcpowers 
30223c57df7Smcpowers #endif	/* _COMMON_CRYPTO_MODES_H */
303