1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_DES_IMPL_H
27 #define	_DES_IMPL_H
28 
29 /*
30  * Common definitions used by DES
31  */
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #define	DES_BLOCK_LEN	8
38 
39 #define	DES_COPY_BLOCK(src, dst) \
40 	(dst)[0] = (src)[0]; \
41 	(dst)[1] = (src)[1]; \
42 	(dst)[2] = (src)[2]; \
43 	(dst)[3] = (src)[3]; \
44 	(dst)[4] = (src)[4]; \
45 	(dst)[5] = (src)[5]; \
46 	(dst)[6] = (src)[6]; \
47 	(dst)[7] = (src)[7];
48 
49 #define	DES_XOR_BLOCK(src, dst) \
50 	(dst)[0] ^= (src)[0]; \
51 	(dst)[1] ^= (src)[1]; \
52 	(dst)[2] ^= (src)[2]; \
53 	(dst)[3] ^= (src)[3]; \
54 	(dst)[4] ^= (src)[4]; \
55 	(dst)[5] ^= (src)[5]; \
56 	(dst)[6] ^= (src)[6]; \
57 	(dst)[7] ^= (src)[7]
58 
59 typedef enum des_strength {
60 	DES = 1,
61 	DES2,
62 	DES3
63 } des_strength_t;
64 
65 #define	DES3_STRENGTH	0x08000000
66 
67 #define	DES_KEYSIZE	8
68 #define	DES_MINBITS	64
69 #define	DES_MAXBITS	64
70 #define	DES_MINBYTES	(DES_MINBITS / 8)
71 #define	DES_MAXBYTES	(DES_MAXBITS / 8)
72 #define	DES_IV_LEN	8
73 
74 #define	DES2_KEYSIZE	(2 * DES_KEYSIZE)
75 #define	DES2_MINBITS	(2 * DES_MINBITS)
76 #define	DES2_MAXBITS	(2 * DES_MAXBITS)
77 #define	DES2_MINBYTES	(DES2_MINBITS / 8)
78 #define	DES2_MAXBYTES	(DES2_MAXBITS / 8)
79 
80 #define	DES3_KEYSIZE	(3 * DES_KEYSIZE)
81 #define	DES3_MINBITS	(2 * DES_MINBITS)	/* DES3 handles CKK_DES2 keys */
82 #define	DES3_MAXBITS	(3 * DES_MAXBITS)
83 #define	DES3_MINBYTES	(DES3_MINBITS / 8)
84 #define	DES3_MAXBYTES	(DES3_MAXBITS / 8)
85 
86 extern int des_encrypt_contiguous_blocks(void *, char *, size_t,
87     crypto_data_t *);
88 extern int des_decrypt_contiguous_blocks(void *, char *, size_t,
89     crypto_data_t *);
90 extern uint64_t des_crypt_impl(uint64_t *, uint64_t, int);
91 extern void des_ks(uint64_t *, uint64_t);
92 extern int des_crunch_block(const void *, const uint8_t *, uint8_t *,
93     boolean_t);
94 extern int des3_crunch_block(const void *, const uint8_t *, uint8_t *,
95     boolean_t);
96 extern void des_init_keysched(uint8_t *, des_strength_t, void *);
97 extern void *des_alloc_keysched(size_t *, des_strength_t, int);
98 extern boolean_t des_keycheck(uint8_t *, des_strength_t, uint8_t *);
99 extern void des_parity_fix(uint8_t *, des_strength_t, uint8_t *);
100 extern void des_copy_block(uint8_t *, uint8_t *);
101 extern void des_xor_block(uint8_t *, uint8_t *);
102 extern int des_encrypt_block(const void *, const uint8_t *, uint8_t *);
103 extern int des3_encrypt_block(const void *, const uint8_t *, uint8_t *);
104 extern int des_decrypt_block(const void *, const uint8_t *, uint8_t *);
105 extern int des3_decrypt_block(const void *, const uint8_t *, uint8_t *);
106 
107 /*
108  * The following definitions and declarations are only used by DES FIPS POST
109  */
110 #ifdef _DES_FIPS_POST
111 
112 #include <modes/modes.h>
113 #include <fips/fips_post.h>
114 
115 /* DES FIPS Declarations */
116 #define	FIPS_DES_ENCRYPT_LENGTH		8  /*  64-bits */
117 #define	FIPS_DES_DECRYPT_LENGTH		8  /*  64-bits */
118 #define	FIPS_DES3_ENCRYPT_LENGTH	8  /*  64-bits */
119 #define	FIPS_DES3_DECRYPT_LENGTH	8  /*  64-bits */
120 
121 #ifdef _KERNEL
122 typedef enum des_mech_type {
123 	DES_ECB_MECH_INFO_TYPE,		/* SUN_CKM_DES_ECB */
124 	DES_CBC_MECH_INFO_TYPE,		/* SUN_CKM_DES_CBC */
125 	DES_CFB_MECH_INFO_TYPE,		/* SUN_CKM_DES_CFB */
126 	DES3_ECB_MECH_INFO_TYPE,	/* SUN_CKM_DES3_ECB */
127 	DES3_CBC_MECH_INFO_TYPE,	/* SUN_CKM_DES3_CBC */
128 	DES3_CFB_MECH_INFO_TYPE		/* SUN_CKM_DES3_CFB */
129 } des_mech_type_t;
130 
131 
132 #undef	CKM_DES_ECB
133 #undef	CKM_DES3_ECB
134 #undef	CKM_DES_CBC
135 #undef	CKM_DES3_CBC
136 
137 #define	CKM_DES_ECB		DES_ECB_MECH_INFO_TYPE
138 #define	CKM_DES3_ECB		DES3_ECB_MECH_INFO_TYPE
139 #define	CKM_DES_CBC		DES_CBC_MECH_INFO_TYPE
140 #define	CKM_DES3_CBC		DES3_CBC_MECH_INFO_TYPE
141 #endif
142 
143 /* DES3 FIPS functions */
144 extern int fips_des3_post(void);
145 
146 #ifndef _KERNEL
147 #ifdef _DES_IMPL
148 struct soft_des_ctx;
149 extern struct soft_des_ctx *des_build_context(uint8_t *, uint8_t *,
150 	CK_KEY_TYPE, CK_MECHANISM_TYPE);
151 extern void fips_des_free_context(struct soft_des_ctx *);
152 extern CK_RV fips_des_encrypt(struct soft_des_ctx *, CK_BYTE_PTR,
153 	CK_ULONG, CK_BYTE_PTR, CK_ULONG_PTR, CK_MECHANISM_TYPE);
154 extern CK_RV fips_des_decrypt(struct soft_des_ctx *, CK_BYTE_PTR,
155 	CK_ULONG, CK_BYTE_PTR, CK_ULONG_PTR, CK_MECHANISM_TYPE);
156 #endif /* _DES_IMPL */
157 #else
158 extern des_ctx_t *des_build_context(uint8_t *, uint8_t *,
159 	des_mech_type_t);
160 extern void fips_des_free_context(des_ctx_t *);
161 extern int fips_des_encrypt(des_ctx_t *, uint8_t *,
162 	ulong_t, uint8_t *, ulong_t *, des_mech_type_t);
163 extern int fips_des_decrypt(des_ctx_t *, uint8_t *,
164 	ulong_t, uint8_t *, ulong_t *, des_mech_type_t);
165 #endif /* _KERNEL */
166 #endif /* _DES_FIPS_POST */
167 
168 #ifdef	__cplusplus
169 }
170 #endif
171 
172 #endif	/* _DES_IMPL_H */
173