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