1*7188Smcpowers /*
2*7188Smcpowers  * CDDL HEADER START
3*7188Smcpowers  *
4*7188Smcpowers  * The contents of this file are subject to the terms of the
5*7188Smcpowers  * Common Development and Distribution License (the "License").
6*7188Smcpowers  * You may not use this file except in compliance with the License.
7*7188Smcpowers  *
8*7188Smcpowers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7188Smcpowers  * or http://www.opensolaris.org/os/licensing.
10*7188Smcpowers  * See the License for the specific language governing permissions
11*7188Smcpowers  * and limitations under the License.
12*7188Smcpowers  *
13*7188Smcpowers  * When distributing Covered Code, include this CDDL HEADER in each
14*7188Smcpowers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7188Smcpowers  * If applicable, add the following below this CDDL HEADER, with the
16*7188Smcpowers  * fields enclosed by brackets "[]" replaced with your own identifying
17*7188Smcpowers  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7188Smcpowers  *
19*7188Smcpowers  * CDDL HEADER END
20*7188Smcpowers  */
21*7188Smcpowers /*
22*7188Smcpowers  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7188Smcpowers  * Use is subject to license terms.
24*7188Smcpowers  */
25*7188Smcpowers 
26*7188Smcpowers #ifndef	_COMMON_CRYPTO_MODES_H
27*7188Smcpowers #define	_COMMON_CRYPTO_MODES_H
28*7188Smcpowers 
29*7188Smcpowers #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*7188Smcpowers 
31*7188Smcpowers #ifdef	__cplusplus
32*7188Smcpowers extern "C" {
33*7188Smcpowers #endif
34*7188Smcpowers 
35*7188Smcpowers #include <sys/strsun.h>
36*7188Smcpowers #include <sys/systm.h>
37*7188Smcpowers #include <sys/sysmacros.h>
38*7188Smcpowers #include <sys/types.h>
39*7188Smcpowers #include <sys/errno.h>
40*7188Smcpowers #include <sys/rwlock.h>
41*7188Smcpowers #include <sys/kmem.h>
42*7188Smcpowers #include <sys/crypto/common.h>
43*7188Smcpowers #include <sys/crypto/impl.h>
44*7188Smcpowers 
45*7188Smcpowers #define	ECB_MODE			0x00000002
46*7188Smcpowers #define	CBC_MODE			0x00000004
47*7188Smcpowers #define	CTR_MODE			0x00000008
48*7188Smcpowers #define	CCM_MODE			0x00000010
49*7188Smcpowers 
50*7188Smcpowers /*
51*7188Smcpowers  * cc_keysched:		Pointer to key schedule.
52*7188Smcpowers  *
53*7188Smcpowers  * cc_keysched_len:	Length of the key schedule.
54*7188Smcpowers  *
55*7188Smcpowers  * cc_remainder:	This is for residual data, i.e. data that can't
56*7188Smcpowers  *			be processed because there are too few bytes.
57*7188Smcpowers  *			Must wait until more data arrives.
58*7188Smcpowers  *
59*7188Smcpowers  * cc_remainder_len:	Number of bytes in cc_remainder.
60*7188Smcpowers  *
61*7188Smcpowers  * cc_iv:		Scratch buffer that sometimes contains the IV.
62*7188Smcpowers  *
63*7188Smcpowers  * cc_lastblock:	Scratch buffer.
64*7188Smcpowers  *
65*7188Smcpowers  * cc_lastp:		Pointer to previous block of ciphertext.
66*7188Smcpowers  *
67*7188Smcpowers  * cc_copy_to:		Pointer to where encrypted residual data needs
68*7188Smcpowers  *			to be copied.
69*7188Smcpowers  *
70*7188Smcpowers  * cc_flags:		PROVIDER_OWNS_KEY_SCHEDULE
71*7188Smcpowers  *			When a context is freed, it is necessary
72*7188Smcpowers  *			to know whether the key schedule was allocated
73*7188Smcpowers  *			by the caller, or internally, e.g. an init routine.
74*7188Smcpowers  *			If allocated by the latter, then it needs to be freed.
75*7188Smcpowers  *
76*7188Smcpowers  *			ECB_MODE, CBC_MODE, CTR_MODE, or CCM_MODE
77*7188Smcpowers  */
78*7188Smcpowers struct common_ctx {
79*7188Smcpowers 	void *cc_keysched;
80*7188Smcpowers 	size_t cc_keysched_len;
81*7188Smcpowers 	uint64_t cc_iv[2];
82*7188Smcpowers 	uint64_t cc_lastblock[2];
83*7188Smcpowers 	uint64_t cc_remainder[2];
84*7188Smcpowers 	size_t cc_remainder_len;
85*7188Smcpowers 	uint8_t *cc_lastp;
86*7188Smcpowers 	uint8_t *cc_copy_to;
87*7188Smcpowers 	uint32_t cc_flags;
88*7188Smcpowers };
89*7188Smcpowers 
90*7188Smcpowers typedef struct common_ctx ecb_ctx_t;
91*7188Smcpowers typedef struct common_ctx cbc_ctx_t;
92*7188Smcpowers typedef struct common_ctx common_ctx_t;
93*7188Smcpowers 
94*7188Smcpowers typedef struct ctr_ctx {
95*7188Smcpowers 	struct common_ctx ctr_common;
96*7188Smcpowers 	uint32_t ctr_tmp[4];
97*7188Smcpowers } ctr_ctx_t;
98*7188Smcpowers 
99*7188Smcpowers /*
100*7188Smcpowers  * ctr_cb                Counter block.
101*7188Smcpowers  *
102*7188Smcpowers  * ctr_counter_mask      Mask of counter bits in the last 8 bytes of the
103*7188Smcpowers  *                       counter block.
104*7188Smcpowers  */
105*7188Smcpowers 
106*7188Smcpowers #define	ctr_keysched		ctr_common.cc_keysched
107*7188Smcpowers #define	ctr_keysched_len	ctr_common.cc_keysched_len
108*7188Smcpowers #define	ctr_cb			ctr_common.cc_iv
109*7188Smcpowers #define	ctr_counter_mask	ctr_common.cc_lastblock[0]
110*7188Smcpowers #define	ctr_remainder		ctr_common.cc_remainder
111*7188Smcpowers #define	ctr_remainder_len	ctr_common.cc_remainder_len
112*7188Smcpowers #define	ctr_lastp		ctr_common.cc_lastp
113*7188Smcpowers #define	ctr_copy_to		ctr_common.cc_copy_to
114*7188Smcpowers #define	ctr_flags		ctr_common.cc_flags
115*7188Smcpowers 
116*7188Smcpowers /*
117*7188Smcpowers  *
118*7188Smcpowers  * ccm_mac_len:		Stores length of the MAC in CCM mode.
119*7188Smcpowers  * ccm_mac_buf:		Stores the intermediate value for MAC in CCM encrypt.
120*7188Smcpowers  *			In CCM decrypt, stores the input MAC value.
121*7188Smcpowers  * ccm_data_len:	Length of the plaintext for CCM mode encrypt, or
122*7188Smcpowers  *			length of the ciphertext for CCM mode decrypt.
123*7188Smcpowers  * ccm_processed_data_len:
124*7188Smcpowers  *			Length of processed plaintext in CCM mode encrypt,
125*7188Smcpowers  *			or length of processed ciphertext for CCM mode decrypt.
126*7188Smcpowers  * ccm_processed_mac_len:
127*7188Smcpowers  *			Length of MAC data accumulated in CCM mode decrypt.
128*7188Smcpowers  *
129*7188Smcpowers  * ccm_pt_buf:		Only used in CCM mode decrypt.  It stores the
130*7188Smcpowers  *			decrypted plaintext to be returned when
131*7188Smcpowers  *			MAC verification succeeds in decrypt_final.
132*7188Smcpowers  *			Memory for this should be allocated in the AES module.
133*7188Smcpowers  *
134*7188Smcpowers  */
135*7188Smcpowers typedef struct ccm_ctx {
136*7188Smcpowers 	struct common_ctx ccm_common;
137*7188Smcpowers 	uint32_t ccm_tmp[4];
138*7188Smcpowers 	size_t ccm_mac_len;
139*7188Smcpowers 	uint64_t ccm_mac_buf[2];
140*7188Smcpowers 	size_t ccm_data_len;
141*7188Smcpowers 	size_t ccm_processed_data_len;
142*7188Smcpowers 	size_t ccm_processed_mac_len;
143*7188Smcpowers 	uint8_t *ccm_pt_buf;
144*7188Smcpowers 	uint64_t ccm_mac_input_buf[2];
145*7188Smcpowers } ccm_ctx_t;
146*7188Smcpowers 
147*7188Smcpowers #define	ccm_keysched		ccm_common.cc_keysched
148*7188Smcpowers #define	ccm_keysched_len	ccm_common.cc_keysched_len
149*7188Smcpowers #define	ccm_cb			ccm_common.cc_iv
150*7188Smcpowers #define	ccm_counter_mask	ccm_common.cc_lastblock[0]
151*7188Smcpowers #define	ccm_remainder		ccm_common.cc_remainder
152*7188Smcpowers #define	ccm_remainder_len	ccm_common.cc_remainder_len
153*7188Smcpowers #define	ccm_lastp		ccm_common.cc_lastp
154*7188Smcpowers #define	ccm_copy_to		ccm_common.cc_copy_to
155*7188Smcpowers #define	ccm_flags		ccm_common.cc_flags
156*7188Smcpowers 
157*7188Smcpowers typedef struct aes_ctx {
158*7188Smcpowers 	union {
159*7188Smcpowers 		ecb_ctx_t acu_ecb;
160*7188Smcpowers 		cbc_ctx_t acu_cbc;
161*7188Smcpowers 		ctr_ctx_t acu_ctr;
162*7188Smcpowers #ifdef _KERNEL
163*7188Smcpowers 		ccm_ctx_t acu_ccm;
164*7188Smcpowers #endif
165*7188Smcpowers 	} acu;
166*7188Smcpowers } aes_ctx_t;
167*7188Smcpowers 
168*7188Smcpowers #define	ac_flags		acu.acu_ecb.cc_flags
169*7188Smcpowers #define	ac_remainder_len	acu.acu_ecb.cc_remainder_len
170*7188Smcpowers #define	ac_keysched		acu.acu_ecb.cc_keysched
171*7188Smcpowers #define	ac_keysched_len		acu.acu_ecb.cc_keysched_len
172*7188Smcpowers #define	ac_iv			acu.acu_ecb.cc_iv
173*7188Smcpowers #define	ac_lastp		acu.acu_ecb.cc_lastp
174*7188Smcpowers #define	ac_pt_buf		acu.acu_ccm.ccm_pt_buf
175*7188Smcpowers #define	ac_mac_len		acu.acu_ccm.ccm_mac_len
176*7188Smcpowers #define	ac_data_len		acu.acu_ccm.ccm_data_len
177*7188Smcpowers #define	ac_processed_mac_len	acu.acu_ccm.ccm_processed_mac_len
178*7188Smcpowers #define	ac_processed_data_len	acu.acu_ccm.ccm_processed_data_len
179*7188Smcpowers 
180*7188Smcpowers typedef struct blowfish_ctx {
181*7188Smcpowers 	union {
182*7188Smcpowers 		ecb_ctx_t bcu_ecb;
183*7188Smcpowers 		cbc_ctx_t bcu_cbc;
184*7188Smcpowers 	} bcu;
185*7188Smcpowers } blowfish_ctx_t;
186*7188Smcpowers 
187*7188Smcpowers #define	bc_flags		bcu.bcu_ecb.cc_flags
188*7188Smcpowers #define	bc_remainder_len	bcu.bcu_ecb.cc_remainder_len
189*7188Smcpowers #define	bc_keysched		bcu.bcu_ecb.cc_keysched
190*7188Smcpowers #define	bc_keysched_len		bcu.bcu_ecb.cc_keysched_len
191*7188Smcpowers #define	bc_iv			bcu.bcu_ecb.cc_iv
192*7188Smcpowers #define	bc_lastp		bcu.bcu_ecb.cc_lastp
193*7188Smcpowers 
194*7188Smcpowers typedef struct des_ctx {
195*7188Smcpowers 	union {
196*7188Smcpowers 		ecb_ctx_t dcu_ecb;
197*7188Smcpowers 		cbc_ctx_t dcu_cbc;
198*7188Smcpowers 	} dcu;
199*7188Smcpowers } des_ctx_t;
200*7188Smcpowers 
201*7188Smcpowers #define	dc_flags		dcu.dcu_ecb.cc_flags
202*7188Smcpowers #define	dc_remainder_len	dcu.dcu_ecb.cc_remainder_len
203*7188Smcpowers #define	dc_keysched		dcu.dcu_ecb.cc_keysched
204*7188Smcpowers #define	dc_keysched_len		dcu.dcu_ecb.cc_keysched_len
205*7188Smcpowers #define	dc_iv			dcu.dcu_ecb.cc_iv
206*7188Smcpowers #define	dc_lastp		dcu.dcu_ecb.cc_lastp
207*7188Smcpowers 
208*7188Smcpowers extern int ecb_cipher_contiguous_blocks(cbc_ctx_t *, char *, size_t,
209*7188Smcpowers     crypto_data_t *, size_t, int (*cipher)(const void *, const uint8_t *,
210*7188Smcpowers     uint8_t *));
211*7188Smcpowers 
212*7188Smcpowers extern int cbc_encrypt_contiguous_blocks(cbc_ctx_t *, char *, size_t,
213*7188Smcpowers     crypto_data_t *, size_t,
214*7188Smcpowers     int (*encrypt)(const void *, const uint8_t *, uint8_t *),
215*7188Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
216*7188Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
217*7188Smcpowers 
218*7188Smcpowers extern int cbc_decrypt_contiguous_blocks(cbc_ctx_t *, char *, size_t,
219*7188Smcpowers     crypto_data_t *, size_t,
220*7188Smcpowers     int (*decrypt)(const void *, const uint8_t *, uint8_t *),
221*7188Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
222*7188Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
223*7188Smcpowers 
224*7188Smcpowers extern int ctr_mode_contiguous_blocks(ctr_ctx_t *, char *, size_t,
225*7188Smcpowers     crypto_data_t *, size_t,
226*7188Smcpowers     int (*cipher)(const void *, const uint8_t *, uint8_t *),
227*7188Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
228*7188Smcpowers 
229*7188Smcpowers extern int ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,
230*7188Smcpowers     crypto_data_t *, size_t,
231*7188Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
232*7188Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
233*7188Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
234*7188Smcpowers 
235*7188Smcpowers extern int ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *, char *, size_t,
236*7188Smcpowers     crypto_data_t *, size_t,
237*7188Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
238*7188Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
239*7188Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
240*7188Smcpowers 
241*7188Smcpowers int ccm_encrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,
242*7188Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
243*7188Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
244*7188Smcpowers 
245*7188Smcpowers extern int ccm_decrypt_final(ccm_ctx_t *, crypto_data_t *, size_t,
246*7188Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
247*7188Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
248*7188Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
249*7188Smcpowers 
250*7188Smcpowers extern int ctr_mode_final(ctr_ctx_t *, crypto_data_t *,
251*7188Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *));
252*7188Smcpowers 
253*7188Smcpowers extern int cbc_init_ctx(cbc_ctx_t *, char *, size_t, size_t,
254*7188Smcpowers     void (*copy_block)(uint8_t *, uint64_t *));
255*7188Smcpowers 
256*7188Smcpowers extern int ctr_init_ctx(ctr_ctx_t *, ulong_t, uint8_t *,
257*7188Smcpowers     void (*copy_block)(uint8_t *, uint8_t *));
258*7188Smcpowers 
259*7188Smcpowers extern int ccm_init_ctx(ccm_ctx_t *, char *, int, boolean_t, size_t,
260*7188Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
261*7188Smcpowers     void (*xor_block)(uint8_t *, uint8_t *));
262*7188Smcpowers 
263*7188Smcpowers extern void calculate_ccm_mac(ccm_ctx_t *, uint8_t *,
264*7188Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *));
265*7188Smcpowers 
266*7188Smcpowers extern void crypto_init_ptrs(crypto_data_t *, void **, offset_t *);
267*7188Smcpowers extern void crypto_get_ptrs(crypto_data_t *, void **, offset_t *,
268*7188Smcpowers     uint8_t **, size_t *, uint8_t **, size_t);
269*7188Smcpowers 
270*7188Smcpowers extern void *ecb_alloc_ctx(int);
271*7188Smcpowers extern void *cbc_alloc_ctx(int);
272*7188Smcpowers extern void *ctr_alloc_ctx(int);
273*7188Smcpowers extern void *ccm_alloc_ctx(int);
274*7188Smcpowers extern void crypto_free_mode_ctx(void *);
275*7188Smcpowers 
276*7188Smcpowers #ifdef	__cplusplus
277*7188Smcpowers }
278*7188Smcpowers #endif
279*7188Smcpowers 
280*7188Smcpowers #endif	/* _COMMON_CRYPTO_MODES_H */
281