xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/crypto-des3.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: crypto-des3.c,v 1.3 2014/04/24 13:45:34 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "krb5_locl.h"
37ebfedea0SLionel Sambuc 
38ebfedea0SLionel Sambuc /*
39ebfedea0SLionel Sambuc  *
40ebfedea0SLionel Sambuc  */
41ebfedea0SLionel Sambuc 
42ebfedea0SLionel Sambuc static void
DES3_random_key(krb5_context context,krb5_keyblock * key)43ebfedea0SLionel Sambuc DES3_random_key(krb5_context context,
44ebfedea0SLionel Sambuc 		krb5_keyblock *key)
45ebfedea0SLionel Sambuc {
46ebfedea0SLionel Sambuc     DES_cblock *k = key->keyvalue.data;
47ebfedea0SLionel Sambuc     do {
48ebfedea0SLionel Sambuc 	krb5_generate_random_block(k, 3 * sizeof(DES_cblock));
49ebfedea0SLionel Sambuc 	DES_set_odd_parity(&k[0]);
50ebfedea0SLionel Sambuc 	DES_set_odd_parity(&k[1]);
51ebfedea0SLionel Sambuc 	DES_set_odd_parity(&k[2]);
52ebfedea0SLionel Sambuc     } while(DES_is_weak_key(&k[0]) ||
53ebfedea0SLionel Sambuc 	    DES_is_weak_key(&k[1]) ||
54ebfedea0SLionel Sambuc 	    DES_is_weak_key(&k[2]));
55ebfedea0SLionel Sambuc }
56ebfedea0SLionel Sambuc 
57ebfedea0SLionel Sambuc 
58ebfedea0SLionel Sambuc #ifdef DES3_OLD_ENCTYPE
59ebfedea0SLionel Sambuc static struct _krb5_key_type keytype_des3 = {
60*0a6a1f1dSLionel Sambuc     ETYPE_OLD_DES3_CBC_SHA1,
61ebfedea0SLionel Sambuc     "des3",
62ebfedea0SLionel Sambuc     168,
63ebfedea0SLionel Sambuc     24,
64ebfedea0SLionel Sambuc     sizeof(struct _krb5_evp_schedule),
65ebfedea0SLionel Sambuc     DES3_random_key,
66ebfedea0SLionel Sambuc     _krb5_evp_schedule,
67ebfedea0SLionel Sambuc     _krb5_des3_salt,
68ebfedea0SLionel Sambuc     _krb5_DES3_random_to_key,
69ebfedea0SLionel Sambuc     _krb5_evp_cleanup,
70ebfedea0SLionel Sambuc     EVP_des_ede3_cbc
71ebfedea0SLionel Sambuc };
72ebfedea0SLionel Sambuc #endif
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc static struct _krb5_key_type keytype_des3_derived = {
75*0a6a1f1dSLionel Sambuc     ETYPE_OLD_DES3_CBC_SHA1,
76ebfedea0SLionel Sambuc     "des3",
77ebfedea0SLionel Sambuc     168,
78ebfedea0SLionel Sambuc     24,
79ebfedea0SLionel Sambuc     sizeof(struct _krb5_evp_schedule),
80ebfedea0SLionel Sambuc     DES3_random_key,
81ebfedea0SLionel Sambuc     _krb5_evp_schedule,
82ebfedea0SLionel Sambuc     _krb5_des3_salt_derived,
83ebfedea0SLionel Sambuc     _krb5_DES3_random_to_key,
84ebfedea0SLionel Sambuc     _krb5_evp_cleanup,
85ebfedea0SLionel Sambuc     EVP_des_ede3_cbc
86ebfedea0SLionel Sambuc };
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc #ifdef DES3_OLD_ENCTYPE
89ebfedea0SLionel Sambuc static krb5_error_code
RSA_MD5_DES3_checksum(krb5_context context,struct _krb5_key_data * key,const void * data,size_t len,unsigned usage,Checksum * C)90ebfedea0SLionel Sambuc RSA_MD5_DES3_checksum(krb5_context context,
91ebfedea0SLionel Sambuc 		      struct _krb5_key_data *key,
92ebfedea0SLionel Sambuc 		      const void *data,
93ebfedea0SLionel Sambuc 		      size_t len,
94ebfedea0SLionel Sambuc 		      unsigned usage,
95ebfedea0SLionel Sambuc 		      Checksum *C)
96ebfedea0SLionel Sambuc {
97ebfedea0SLionel Sambuc     return _krb5_des_checksum(context, EVP_md5(), key, data, len, C);
98ebfedea0SLionel Sambuc }
99ebfedea0SLionel Sambuc 
100ebfedea0SLionel Sambuc static krb5_error_code
RSA_MD5_DES3_verify(krb5_context context,struct _krb5_key_data * key,const void * data,size_t len,unsigned usage,Checksum * C)101ebfedea0SLionel Sambuc RSA_MD5_DES3_verify(krb5_context context,
102ebfedea0SLionel Sambuc 		    struct _krb5_key_data *key,
103ebfedea0SLionel Sambuc 		    const void *data,
104ebfedea0SLionel Sambuc 		    size_t len,
105ebfedea0SLionel Sambuc 		    unsigned usage,
106ebfedea0SLionel Sambuc 		    Checksum *C)
107ebfedea0SLionel Sambuc {
108ebfedea0SLionel Sambuc     return _krb5_des_verify(context, EVP_md5(), key, data, len, C);
109ebfedea0SLionel Sambuc }
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc struct _krb5_checksum_type _krb5_checksum_rsa_md5_des3 = {
112ebfedea0SLionel Sambuc     CKSUMTYPE_RSA_MD5_DES3,
113ebfedea0SLionel Sambuc     "rsa-md5-des3",
114ebfedea0SLionel Sambuc     64,
115ebfedea0SLionel Sambuc     24,
116ebfedea0SLionel Sambuc     F_KEYED | F_CPROOF | F_VARIANT,
117ebfedea0SLionel Sambuc     RSA_MD5_DES3_checksum,
118ebfedea0SLionel Sambuc     RSA_MD5_DES3_verify
119ebfedea0SLionel Sambuc };
120ebfedea0SLionel Sambuc #endif
121ebfedea0SLionel Sambuc 
122ebfedea0SLionel Sambuc struct _krb5_checksum_type _krb5_checksum_hmac_sha1_des3 = {
123ebfedea0SLionel Sambuc     CKSUMTYPE_HMAC_SHA1_DES3,
124ebfedea0SLionel Sambuc     "hmac-sha1-des3",
125ebfedea0SLionel Sambuc     64,
126ebfedea0SLionel Sambuc     20,
127ebfedea0SLionel Sambuc     F_KEYED | F_CPROOF | F_DERIVED,
128ebfedea0SLionel Sambuc     _krb5_SP_HMAC_SHA1_checksum,
129ebfedea0SLionel Sambuc     NULL
130ebfedea0SLionel Sambuc };
131ebfedea0SLionel Sambuc 
132ebfedea0SLionel Sambuc #ifdef DES3_OLD_ENCTYPE
133ebfedea0SLionel Sambuc struct _krb5_encryption_type _krb5_enctype_des3_cbc_md5 = {
134ebfedea0SLionel Sambuc     ETYPE_DES3_CBC_MD5,
135ebfedea0SLionel Sambuc     "des3-cbc-md5",
136ebfedea0SLionel Sambuc     8,
137ebfedea0SLionel Sambuc     8,
138ebfedea0SLionel Sambuc     8,
139ebfedea0SLionel Sambuc     &keytype_des3,
140ebfedea0SLionel Sambuc     &_krb5_checksum_rsa_md5,
141ebfedea0SLionel Sambuc     &_krb5_checksum_rsa_md5_des3,
142ebfedea0SLionel Sambuc     0,
143ebfedea0SLionel Sambuc     _krb5_evp_encrypt,
144ebfedea0SLionel Sambuc     0,
145ebfedea0SLionel Sambuc     NULL
146ebfedea0SLionel Sambuc };
147ebfedea0SLionel Sambuc #endif
148ebfedea0SLionel Sambuc 
149ebfedea0SLionel Sambuc struct _krb5_encryption_type _krb5_enctype_des3_cbc_sha1 = {
150ebfedea0SLionel Sambuc     ETYPE_DES3_CBC_SHA1,
151ebfedea0SLionel Sambuc     "des3-cbc-sha1",
152ebfedea0SLionel Sambuc     8,
153ebfedea0SLionel Sambuc     8,
154ebfedea0SLionel Sambuc     8,
155ebfedea0SLionel Sambuc     &keytype_des3_derived,
156ebfedea0SLionel Sambuc     &_krb5_checksum_sha1,
157ebfedea0SLionel Sambuc     &_krb5_checksum_hmac_sha1_des3,
158ebfedea0SLionel Sambuc     F_DERIVED,
159ebfedea0SLionel Sambuc     _krb5_evp_encrypt,
160ebfedea0SLionel Sambuc     0,
161ebfedea0SLionel Sambuc     NULL
162ebfedea0SLionel Sambuc };
163ebfedea0SLionel Sambuc 
164ebfedea0SLionel Sambuc #ifdef DES3_OLD_ENCTYPE
165ebfedea0SLionel Sambuc struct _krb5_encryption_type _krb5_enctype_old_des3_cbc_sha1 = {
166ebfedea0SLionel Sambuc     ETYPE_OLD_DES3_CBC_SHA1,
167ebfedea0SLionel Sambuc     "old-des3-cbc-sha1",
168ebfedea0SLionel Sambuc     8,
169ebfedea0SLionel Sambuc     8,
170ebfedea0SLionel Sambuc     8,
171ebfedea0SLionel Sambuc     &keytype_des3,
172ebfedea0SLionel Sambuc     &_krb5_checksum_sha1,
173ebfedea0SLionel Sambuc     &_krb5_checksum_hmac_sha1_des3,
174ebfedea0SLionel Sambuc     0,
175ebfedea0SLionel Sambuc     _krb5_evp_encrypt,
176ebfedea0SLionel Sambuc     0,
177ebfedea0SLionel Sambuc     NULL
178ebfedea0SLionel Sambuc };
179ebfedea0SLionel Sambuc #endif
180ebfedea0SLionel Sambuc 
181ebfedea0SLionel Sambuc struct _krb5_encryption_type _krb5_enctype_des3_cbc_none = {
182ebfedea0SLionel Sambuc     ETYPE_DES3_CBC_NONE,
183ebfedea0SLionel Sambuc     "des3-cbc-none",
184ebfedea0SLionel Sambuc     8,
185ebfedea0SLionel Sambuc     8,
186ebfedea0SLionel Sambuc     0,
187ebfedea0SLionel Sambuc     &keytype_des3_derived,
188ebfedea0SLionel Sambuc     &_krb5_checksum_none,
189ebfedea0SLionel Sambuc     NULL,
190ebfedea0SLionel Sambuc     F_PSEUDO,
191ebfedea0SLionel Sambuc     _krb5_evp_encrypt,
192ebfedea0SLionel Sambuc     0,
193ebfedea0SLionel Sambuc     NULL
194ebfedea0SLionel Sambuc };
195ebfedea0SLionel Sambuc 
196ebfedea0SLionel Sambuc void
_krb5_DES3_random_to_key(krb5_context context,krb5_keyblock * key,const void * data,size_t size)197ebfedea0SLionel Sambuc _krb5_DES3_random_to_key(krb5_context context,
198ebfedea0SLionel Sambuc 			 krb5_keyblock *key,
199ebfedea0SLionel Sambuc 			 const void *data,
200ebfedea0SLionel Sambuc 			 size_t size)
201ebfedea0SLionel Sambuc {
202ebfedea0SLionel Sambuc     unsigned char *x = key->keyvalue.data;
203ebfedea0SLionel Sambuc     const u_char *q = data;
204ebfedea0SLionel Sambuc     DES_cblock *k;
205ebfedea0SLionel Sambuc     int i, j;
206ebfedea0SLionel Sambuc 
207*0a6a1f1dSLionel Sambuc     memset(key->keyvalue.data, 0, key->keyvalue.length);
208ebfedea0SLionel Sambuc     for (i = 0; i < 3; ++i) {
209ebfedea0SLionel Sambuc 	unsigned char foo;
210ebfedea0SLionel Sambuc 	for (j = 0; j < 7; ++j) {
211ebfedea0SLionel Sambuc 	    unsigned char b = q[7 * i + j];
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc 	    x[8 * i + j] = b;
214ebfedea0SLionel Sambuc 	}
215ebfedea0SLionel Sambuc 	foo = 0;
216ebfedea0SLionel Sambuc 	for (j = 6; j >= 0; --j) {
217ebfedea0SLionel Sambuc 	    foo |= q[7 * i + j] & 1;
218ebfedea0SLionel Sambuc 	    foo <<= 1;
219ebfedea0SLionel Sambuc 	}
220ebfedea0SLionel Sambuc 	x[8 * i + 7] = foo;
221ebfedea0SLionel Sambuc     }
222ebfedea0SLionel Sambuc     k = key->keyvalue.data;
223ebfedea0SLionel Sambuc     for (i = 0; i < 3; i++) {
224ebfedea0SLionel Sambuc 	DES_set_odd_parity(&k[i]);
225ebfedea0SLionel Sambuc 	if(DES_is_weak_key(&k[i]))
226ebfedea0SLionel Sambuc 	    _krb5_xor(&k[i], (const unsigned char*)"\0\0\0\0\0\0\0\xf0");
227ebfedea0SLionel Sambuc     }
228ebfedea0SLionel Sambuc }
229