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
,
30 char **ivout
, size_t * ivoutlen
,
31 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
33 return simplified_encrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
34 ivoutlen
, in
, inlen
, out
, outlen
);
38 _des3_decrypt (Shishi
* handle
,
43 char **ivout
, size_t * ivoutlen
,
44 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
46 return simplified_decrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
,
47 ivoutlen
, in
, inlen
, out
, outlen
);
51 des3none_dencrypt (Shishi
* handle
,
54 const char *iv
, size_t ivlen
,
55 char **ivout
, size_t * ivoutlen
,
56 const char *in
, size_t inlen
,
57 char **out
, size_t * outlen
, int direction
)
63 Shishi_key
*derivedkey
;
65 res
= simplified_derivekey (handle
, key
, keyusage
,
66 SHISHI_DERIVEKEYMODE_PRIVACY
, &derivedkey
);
71 simplified_dencrypt (handle
, derivedkey
, iv
, ivlen
, ivout
, ivoutlen
,
72 in
, inlen
, out
, outlen
, direction
);
74 shishi_key_done (derivedkey
);
81 res
= simplified_dencrypt (handle
, key
, iv
, ivlen
, ivout
, ivoutlen
,
82 in
, inlen
, out
, outlen
, direction
);
91 des3none_encrypt (Shishi
* handle
,
94 const char *iv
, size_t ivlen
,
95 char **ivout
, size_t * ivoutlen
,
96 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
98 return des3none_dencrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
, ivoutlen
,
99 in
, inlen
, out
, outlen
, 0);
103 des3none_decrypt (Shishi
* handle
,
106 const char *iv
, size_t ivlen
,
107 char **ivout
, size_t * ivoutlen
,
108 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
110 return des3none_dencrypt (handle
, key
, keyusage
, iv
, ivlen
, ivout
, ivoutlen
,
111 in
, inlen
, out
, outlen
, 1);
114 /* The 168 bits of random key data are converted to a protocol key
115 * value as follows. First, the 168 bits are divided into three
116 * groups of 56 bits, which are expanded individually into 64 bits as
120 * 9 10 11 12 13 14 15 p
121 * 17 18 19 20 21 22 23 p
122 * 25 26 27 28 29 30 31 p
123 * 33 34 35 36 37 38 39 p
124 * 41 42 43 44 45 46 47 p
125 * 49 50 51 52 53 54 55 p
126 * 56 48 40 32 24 16 8 p
128 * The "p" bits are parity bits computed over the data bits. The
129 * output of the three expansions are concatenated to form the
130 * protocol key value.
134 des3_random_to_key (Shishi
* handle
,
135 const char *random
, size_t randomlen
, Shishi_key
* outkey
)
137 unsigned char tmpkey
[3 * 8];
140 if (randomlen
< 168 / 8)
143 if (VERBOSECRYPTO (handle
))
145 printf ("des3_random_to_key (random)\n");
146 printf ("\t ;; random (length %d):\n", 168 / 8);
147 hexprint (random
, 168 / 8);
149 binprint (random
, 168 / 8);
153 memcpy (tmpkey
, random
, 7);
154 memcpy (tmpkey
+ 8, random
+ 7, 7);
155 memcpy (tmpkey
+ 16, random
+ 14, 7);
156 for (i
= 0; i
< 3; i
++)
159 ((tmpkey
[i
* 8 + 0] & 0x01) << 1) |
160 ((tmpkey
[i
* 8 + 1] & 0x01) << 2) |
161 ((tmpkey
[i
* 8 + 2] & 0x01) << 3) |
162 ((tmpkey
[i
* 8 + 3] & 0x01) << 4) |
163 ((tmpkey
[i
* 8 + 4] & 0x01) << 5) |
164 ((tmpkey
[i
* 8 + 5] & 0x01) << 6) | ((tmpkey
[i
* 8 + 6] & 0x01) << 7);
165 des_set_odd_key_parity (tmpkey
+ i
* 8);
168 shishi_key_value_set (outkey
, tmpkey
);
170 if (VERBOSECRYPTO (handle
))
172 printf ("key = des3_random_to_key (random)\n");
173 printf ("\t ;; key:\n");
174 hexprint (tmpkey
, 3 * 8);
176 binprint (tmpkey
, 3 * 8);
184 des3_string_to_key (Shishi
* handle
,
189 const char *parameter
, Shishi_key
* outkey
)
195 int nfoldlen
= 168 / 8;
198 if (VERBOSECRYPTO (handle
))
200 printf ("des3_string_to_key (string, salt)\n");
201 printf ("\t ;; String:\n");
202 escapeprint (string
, stringlen
);
203 hexprint (string
, stringlen
);
205 printf ("\t ;; Salt:\n");
206 escapeprint (salt
, saltlen
);
207 hexprint (salt
, saltlen
);
211 /* s = passwordString + salt */
212 n_s
= stringlen
+ saltlen
;
213 s
= (char *) xmalloc (n_s
);
214 memcpy (s
, string
, stringlen
);
215 memcpy (s
+ stringlen
, salt
, saltlen
);
217 /* tmpKey = random-to-key(168-fold(s)) */
218 res
= shishi_n_fold (handle
, s
, n_s
, nfold
, nfoldlen
);
220 if (res
!= SHISHI_OK
)
223 res
= shishi_key_from_value (handle
, shishi_key_type (outkey
), NULL
, &key
);
224 if (res
!= SHISHI_OK
)
227 res
= des3_random_to_key (handle
, nfold
, nfoldlen
, key
);
228 if (res
!= SHISHI_OK
)
231 /* key = DK (tmpKey, KerberosConstant) */
232 res
= shishi_dk (handle
, key
, "kerberos", strlen ("kerberos"), outkey
);
233 if (res
!= SHISHI_OK
)
236 shishi_key_done (key
);
238 if (VERBOSECRYPTO (handle
))
240 printf ("des3_string_to_key (string, salt)\n");
241 printf ("\t ;; Key:\n");
242 hexprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
243 binprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
251 des3_checksum (Shishi
* handle
,
255 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
257 return simplified_checksum (handle
, key
, keyusage
, cksumtype
,
258 in
, inlen
, out
, outlen
);