1 /* crypto-3des.c --- 3DES crypto functions.
2 * Copyright (C) 2002, 2003, 2004, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 /* Get _shishi_escapeprint, etc. */
31 _des3_encrypt (Shishi
* handle
,
36 char **ivout
, size_t * ivoutlen
,
37 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
39 return _shishi_simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
40 ivoutlen
, in
, inlen
, out
, outlen
);
44 _des3_decrypt (Shishi
* handle
,
49 char **ivout
, size_t * ivoutlen
,
50 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
52 return _shishi_simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
53 ivoutlen
, in
, inlen
, out
, outlen
);
57 des3none_dencrypt (Shishi
* handle
,
60 const char *iv
, size_t ivlen
,
61 char **ivout
, size_t * ivoutlen
,
62 const char *in
, size_t inlen
,
63 char **out
, size_t * outlen
, int direction
)
69 Shishi_key
*derivedkey
;
71 res
= _shishi_simplified_derivekey (handle
, key
, keyusage
,
72 SHISHI_DERIVEKEYMODE_PRIVACY
,
78 _shishi_simplified_dencrypt (handle
, derivedkey
, iv
, ivlen
, ivout
,
79 ivoutlen
, in
, inlen
, out
, outlen
,
82 shishi_key_done (derivedkey
);
90 _shishi_simplified_dencrypt (handle
, key
, iv
, ivlen
, ivout
, ivoutlen
,
91 in
, inlen
, out
, outlen
, direction
);
100 des3none_encrypt (Shishi
* handle
,
103 const char *iv
, size_t ivlen
,
104 char **ivout
, size_t * ivoutlen
,
105 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
107 return des3none_dencrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
, ivoutlen
,
108 in
, inlen
, out
, outlen
, 0);
112 des3none_decrypt (Shishi
* handle
,
115 const char *iv
, size_t ivlen
,
116 char **ivout
, size_t * ivoutlen
,
117 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
119 return des3none_dencrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
, ivoutlen
,
120 in
, inlen
, out
, outlen
, 1);
124 des_set_odd_key_parity (char key
[8])
128 for (i
= 0; i
< 8; i
++)
132 for (j
= 1; j
< 8; j
++)
133 if (key
[i
] & (1 << j
))
137 if ((n_set_bits
% 2) == 0)
142 /* The 168 bits of random key data are converted to a protocol key
143 * value as follows. First, the 168 bits are divided into three
144 * groups of 56 bits, which are expanded individually into 64 bits as
148 * 9 10 11 12 13 14 15 p
149 * 17 18 19 20 21 22 23 p
150 * 25 26 27 28 29 30 31 p
151 * 33 34 35 36 37 38 39 p
152 * 41 42 43 44 45 46 47 p
153 * 49 50 51 52 53 54 55 p
154 * 56 48 40 32 24 16 8 p
156 * The "p" bits are parity bits computed over the data bits. The
157 * output of the three expansions are concatenated to form the
158 * protocol key value.
162 des3_random_to_key (Shishi
* handle
,
163 const char *rnd
, size_t rndlen
, Shishi_key
* outkey
)
168 if (rndlen
< 168 / 8)
171 if (VERBOSECRYPTO (handle
))
173 printf ("des3_random_to_key (random)\n");
174 printf ("\t ;; random (length %d):\n", 168 / 8);
175 _shishi_hexprint (rnd
, 168 / 8);
176 _shishi_binprint (rnd
, 168 / 8);
179 memcpy (tmpkey
, rnd
, 7);
180 memcpy (tmpkey
+ 8, rnd
+ 7, 7);
181 memcpy (tmpkey
+ 16, rnd
+ 14, 7);
182 for (i
= 0; i
< 3; i
++)
185 ((tmpkey
[i
* 8 + 0] & 0x01) << 1) |
186 ((tmpkey
[i
* 8 + 1] & 0x01) << 2) |
187 ((tmpkey
[i
* 8 + 2] & 0x01) << 3) |
188 ((tmpkey
[i
* 8 + 3] & 0x01) << 4) |
189 ((tmpkey
[i
* 8 + 4] & 0x01) << 5) |
190 ((tmpkey
[i
* 8 + 5] & 0x01) << 6) | ((tmpkey
[i
* 8 + 6] & 0x01) << 7);
191 des_set_odd_key_parity (tmpkey
+ i
* 8);
194 shishi_key_value_set (outkey
, tmpkey
);
196 if (VERBOSECRYPTO (handle
))
198 printf ("key = des3_random_to_key (random)\n");
199 printf ("\t ;; key:\n");
200 _shishi_hexprint (tmpkey
, 3 * 8);
201 _shishi_binprint (tmpkey
, 3 * 8);
208 des3_string_to_key (Shishi
* handle
,
213 const char *parameter
, Shishi_key
* outkey
)
219 int nfoldlen
= 168 / 8;
222 if (VERBOSECRYPTO (handle
))
224 printf ("des3_string_to_key (string, salt)\n");
225 printf ("\t ;; String:\n");
226 _shishi_escapeprint (string
, stringlen
);
227 _shishi_hexprint (string
, stringlen
);
228 printf ("\t ;; Salt:\n");
229 _shishi_escapeprint (salt
, saltlen
);
230 _shishi_hexprint (salt
, saltlen
);
233 /* s = passwordString + salt */
234 n_s
= stringlen
+ saltlen
;
235 s
= (char *) xmalloc (n_s
);
236 memcpy (s
, string
, stringlen
);
237 memcpy (s
+ stringlen
, salt
, saltlen
);
239 /* tmpKey = random-to-key(168-fold(s)) */
240 res
= shishi_n_fold (handle
, s
, n_s
, nfold
, nfoldlen
);
242 if (res
!= SHISHI_OK
)
245 res
= shishi_key_from_value (handle
, shishi_key_type (outkey
), NULL
, &key
);
246 if (res
!= SHISHI_OK
)
249 res
= des3_random_to_key (handle
, nfold
, nfoldlen
, key
);
250 if (res
== SHISHI_OK
)
251 /* key = DK (tmpKey, Constant) */
252 res
= shishi_dk (handle
, key
, SHISHI_DK_CONSTANT
,
253 strlen (SHISHI_DK_CONSTANT
), outkey
);
255 shishi_key_done (key
);
257 if (res
!= SHISHI_OK
)
260 if (VERBOSECRYPTO (handle
))
262 printf ("des3_string_to_key (string, salt)\n");
263 printf ("\t ;; Key:\n");
264 _shishi_hexprint (shishi_key_value (outkey
),
265 shishi_key_length (outkey
));
266 _shishi_binprint (shishi_key_value (outkey
),
267 shishi_key_length (outkey
));
274 des3_checksum (Shishi
* handle
,
278 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
280 return _shishi_simplified_checksum (handle
, key
, keyusage
, cksumtype
,
281 in
, inlen
, out
, outlen
);
284 cipherinfo des3_cbc_none_info
= {
285 SHISHI_DES3_CBC_NONE
,
291 SHISHI_HMAC_SHA1_DES3_KD
,
298 cipherinfo des3_cbc_sha1_kd_info
= {
299 SHISHI_DES3_CBC_HMAC_SHA1_KD
,
305 SHISHI_HMAC_SHA1_DES3_KD
,
312 checksuminfo hmac_sha1_des3_kd_info
= {
313 SHISHI_HMAC_SHA1_DES3_KD
,