1 /* crypto-3des.c --- 3DES crypto functions.
2 * Copyright (C) 2002, 2003, 2004, 2006, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful, but
12 * 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, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
28 /* Get _shishi_escapeprint, etc. */
32 _des3_encrypt (Shishi
* handle
,
37 char **ivout
, size_t * ivoutlen
,
38 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
40 return _shishi_simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
41 ivoutlen
, in
, inlen
, out
, outlen
);
45 _des3_decrypt (Shishi
* handle
,
50 char **ivout
, size_t * ivoutlen
,
51 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
53 return _shishi_simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
54 ivoutlen
, in
, inlen
, out
, outlen
);
58 des3none_dencrypt (Shishi
* handle
,
61 const char *iv
, size_t ivlen
,
62 char **ivout
, size_t * ivoutlen
,
63 const char *in
, size_t inlen
,
64 char **out
, size_t * outlen
, int direction
)
70 Shishi_key
*derivedkey
;
72 res
= _shishi_simplified_derivekey (handle
, key
, keyusage
,
73 SHISHI_DERIVEKEYMODE_PRIVACY
,
79 _shishi_simplified_dencrypt (handle
, derivedkey
, iv
, ivlen
, ivout
,
80 ivoutlen
, in
, inlen
, out
, outlen
,
83 shishi_key_done (derivedkey
);
91 _shishi_simplified_dencrypt (handle
, key
, iv
, ivlen
, ivout
, ivoutlen
,
92 in
, inlen
, out
, outlen
, direction
);
101 des3none_encrypt (Shishi
* handle
,
104 const char *iv
, size_t ivlen
,
105 char **ivout
, size_t * ivoutlen
,
106 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
108 return des3none_dencrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
, ivoutlen
,
109 in
, inlen
, out
, outlen
, 0);
113 des3none_decrypt (Shishi
* handle
,
116 const char *iv
, size_t ivlen
,
117 char **ivout
, size_t * ivoutlen
,
118 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
120 return des3none_dencrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
, ivoutlen
,
121 in
, inlen
, out
, outlen
, 1);
125 des_set_odd_key_parity (char key
[8])
129 for (i
= 0; i
< 8; i
++)
133 for (j
= 1; j
< 8; j
++)
134 if (key
[i
] & (1 << j
))
138 if ((n_set_bits
% 2) == 0)
143 /* The 168 bits of random key data are converted to a protocol key
144 * value as follows. First, the 168 bits are divided into three
145 * groups of 56 bits, which are expanded individually into 64 bits as
149 * 9 10 11 12 13 14 15 p
150 * 17 18 19 20 21 22 23 p
151 * 25 26 27 28 29 30 31 p
152 * 33 34 35 36 37 38 39 p
153 * 41 42 43 44 45 46 47 p
154 * 49 50 51 52 53 54 55 p
155 * 56 48 40 32 24 16 8 p
157 * The "p" bits are parity bits computed over the data bits. The
158 * output of the three expansions are concatenated to form the
159 * protocol key value.
163 des3_random_to_key (Shishi
* handle
,
164 const char *rnd
, size_t rndlen
, Shishi_key
* outkey
)
169 if (rndlen
< 168 / 8)
172 if (VERBOSECRYPTO (handle
))
174 printf ("des3_random_to_key (random)\n");
175 printf ("\t ;; random (length %d):\n", 168 / 8);
176 _shishi_hexprint (rnd
, 168 / 8);
177 _shishi_binprint (rnd
, 168 / 8);
180 memcpy (tmpkey
, rnd
, 7);
181 memcpy (tmpkey
+ 8, rnd
+ 7, 7);
182 memcpy (tmpkey
+ 16, rnd
+ 14, 7);
183 for (i
= 0; i
< 3; i
++)
186 ((tmpkey
[i
* 8 + 0] & 0x01) << 1) |
187 ((tmpkey
[i
* 8 + 1] & 0x01) << 2) |
188 ((tmpkey
[i
* 8 + 2] & 0x01) << 3) |
189 ((tmpkey
[i
* 8 + 3] & 0x01) << 4) |
190 ((tmpkey
[i
* 8 + 4] & 0x01) << 5) |
191 ((tmpkey
[i
* 8 + 5] & 0x01) << 6) | ((tmpkey
[i
* 8 + 6] & 0x01) << 7);
192 des_set_odd_key_parity (tmpkey
+ i
* 8);
195 shishi_key_value_set (outkey
, tmpkey
);
197 if (VERBOSECRYPTO (handle
))
199 printf ("key = des3_random_to_key (random)\n");
200 printf ("\t ;; key:\n");
201 _shishi_hexprint (tmpkey
, 3 * 8);
202 _shishi_binprint (tmpkey
, 3 * 8);
209 des3_string_to_key (Shishi
* handle
,
214 const char *parameter
, Shishi_key
* outkey
)
220 int nfoldlen
= 168 / 8;
223 if (VERBOSECRYPTO (handle
))
225 printf ("des3_string_to_key (string, salt)\n");
226 printf ("\t ;; String:\n");
227 _shishi_escapeprint (string
, stringlen
);
228 _shishi_hexprint (string
, stringlen
);
229 printf ("\t ;; Salt:\n");
230 _shishi_escapeprint (salt
, saltlen
);
231 _shishi_hexprint (salt
, saltlen
);
234 /* s = passwordString + salt */
235 n_s
= stringlen
+ saltlen
;
236 s
= (char *) xmalloc (n_s
);
237 memcpy (s
, string
, stringlen
);
238 memcpy (s
+ stringlen
, salt
, saltlen
);
240 /* tmpKey = random-to-key(168-fold(s)) */
241 res
= shishi_n_fold (handle
, s
, n_s
, nfold
, nfoldlen
);
243 if (res
!= SHISHI_OK
)
246 res
= shishi_key_from_value (handle
, shishi_key_type (outkey
), NULL
, &key
);
247 if (res
!= SHISHI_OK
)
250 res
= des3_random_to_key (handle
, nfold
, nfoldlen
, key
);
251 if (res
== SHISHI_OK
)
252 /* key = DK (tmpKey, Constant) */
253 res
= shishi_dk (handle
, key
, SHISHI_DK_CONSTANT
,
254 strlen (SHISHI_DK_CONSTANT
), outkey
);
256 shishi_key_done (key
);
258 if (res
!= SHISHI_OK
)
261 if (VERBOSECRYPTO (handle
))
263 printf ("des3_string_to_key (string, salt)\n");
264 printf ("\t ;; Key:\n");
265 _shishi_hexprint (shishi_key_value (outkey
),
266 shishi_key_length (outkey
));
267 _shishi_binprint (shishi_key_value (outkey
),
268 shishi_key_length (outkey
));
275 des3_checksum (Shishi
* handle
,
279 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
281 return _shishi_simplified_checksum (handle
, key
, keyusage
, cksumtype
,
282 in
, inlen
, out
, outlen
);
285 cipherinfo des3_cbc_none_info
= {
286 SHISHI_DES3_CBC_NONE
,
292 SHISHI_HMAC_SHA1_DES3_KD
,
299 cipherinfo des3_cbc_sha1_kd_info
= {
300 SHISHI_DES3_CBC_HMAC_SHA1_KD
,
306 SHISHI_HMAC_SHA1_DES3_KD
,
313 checksuminfo hmac_sha1_des3_kd_info
= {
314 SHISHI_HMAC_SHA1_DES3_KD
,