1 /* crypto-3des.c 3DES crypto functions
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Note: This file is #include'd by crypto.c.
25 des3_encrypt (Shishi
* handle
,
35 return simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
,
36 in
, inlen
, out
, outlen
);
40 des3_decrypt (Shishi
* handle
,
50 return simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
,
51 in
, inlen
, out
, outlen
);
55 des3none_dencrypt (Shishi
* handle
,
71 Shishi_key
*derivedkey
;
73 res
= shishi_key_from_value (handle
, shishi_key_type (key
),
78 res
= simplified_derivekey (handle
, key
, keyusage
,
79 SHISHI_DERIVEKEYMODE_PRIVACY
, derivedkey
);
83 res
= simplified_dencrypt (handle
, derivedkey
, iv
, ivlen
,
84 in
, inlen
, out
, outlen
, direction
);
88 shishi_key_done (&derivedkey
);
92 res
= simplified_dencrypt (handle
, key
, iv
, ivlen
,
93 in
, inlen
, out
, outlen
, direction
);
100 des3none_encrypt (Shishi
* handle
,
110 return des3none_dencrypt (handle
, key
, keyusage
,
111 iv
, ivlen
, in
, inlen
, out
, outlen
, 0);
115 des3none_decrypt (Shishi
* handle
,
125 return des3none_dencrypt (handle
, key
, keyusage
,
126 iv
, ivlen
, in
, inlen
, out
, outlen
, 1);
129 /* The 168 bits of random key data are converted to a protocol key
130 * value as follows. First, the 168 bits are divided into three
131 * groups of 56 bits, which are expanded individually into 64 bits as
135 * 9 10 11 12 13 14 15 p
136 * 17 18 19 20 21 22 23 p
137 * 25 26 27 28 29 30 31 p
138 * 33 34 35 36 37 38 39 p
139 * 41 42 43 44 45 46 47 p
140 * 49 50 51 52 53 54 55 p
141 * 56 48 40 32 24 16 8 p
143 * The "p" bits are parity bits computed over the data bits. The
144 * output of the three expansions are concatenated to form the
145 * protocol key value.
149 des3_random_to_key (Shishi
* handle
,
154 unsigned char tmpkey
[3*8];
157 if (randomlen
< 168 / 8)
160 if (VERBOSECRYPTO(handle
))
162 printf ("des3_random_to_key (random)\n");
163 printf ("\t ;; random (length %d):\n", 168 / 8);
164 hexprint (random
, 168 / 8);
166 binprint (random
, 168 / 8);
170 memcpy (tmpkey
, random
, 7);
171 memcpy (tmpkey
+ 8, random
+ 7, 7);
172 memcpy (tmpkey
+ 16, random
+ 14, 7);
173 for (i
= 0; i
< 3; i
++)
176 ((tmpkey
[i
* 8 + 0] & 0x01) << 1) |
177 ((tmpkey
[i
* 8 + 1] & 0x01) << 2) |
178 ((tmpkey
[i
* 8 + 2] & 0x01) << 3) |
179 ((tmpkey
[i
* 8 + 3] & 0x01) << 4) |
180 ((tmpkey
[i
* 8 + 4] & 0x01) << 5) |
181 ((tmpkey
[i
* 8 + 5] & 0x01) << 6) |
182 ((tmpkey
[i
* 8 + 6] & 0x01) << 7);
183 des_set_odd_key_parity (tmpkey
+ i
* 8);
186 shishi_key_value_set(outkey
, tmpkey
);
188 if (VERBOSECRYPTO(handle
))
190 printf ("key = des3_random_to_key (random)\n");
191 printf ("\t ;; key:\n");
192 hexprint (tmpkey
, 3 * 8);
194 binprint (tmpkey
, 3 * 8);
202 des3_string_to_key (Shishi
* handle
,
207 const char *parameter
,
214 int nfoldlen
= 168 / 8;
217 if (VERBOSECRYPTO(handle
))
219 printf ("des3_string_to_key (string, salt)\n");
220 printf ("\t ;; String:\n");
221 escapeprint (string
, stringlen
);
222 hexprint (string
, stringlen
);
224 printf ("\t ;; Salt:\n");
225 escapeprint (salt
, saltlen
);
226 hexprint (salt
, saltlen
);
230 /* s = passwordString + salt */
231 n_s
= stringlen
+ saltlen
;
232 s
= (char *) malloc (n_s
);
233 memcpy (s
, string
, stringlen
);
234 memcpy (s
+ stringlen
, salt
, saltlen
);
236 /* tmpKey = random-to-key(168-fold(s)) */
237 res
= shishi_n_fold (handle
, s
, n_s
, nfold
, nfoldlen
);
238 if (res
!= SHISHI_OK
)
243 res
= shishi_key_from_value(handle
, shishi_key_type(outkey
), NULL
, &key
);
244 if (res
!= SHISHI_OK
)
247 res
= des3_random_to_key (handle
, nfold
, nfoldlen
, key
);
248 if (res
!= SHISHI_OK
)
251 /* key = DK (tmpKey, KerberosConstant) */
252 res
= shishi_dk (handle
, key
, "kerberos", strlen ("kerberos"), outkey
);
253 if (res
!= SHISHI_OK
)
256 shishi_key_done(&key
);
258 if (VERBOSECRYPTO(handle
))
260 printf ("des3_string_to_key (string, salt)\n");
261 printf ("\t ;; Key:\n");
262 hexprint (shishi_key_value(outkey
), shishi_key_length(outkey
));
263 binprint (shishi_key_value(outkey
), shishi_key_length(outkey
));