Cleanup debug messages.
[shishi.git] / lib / crypto-3des.c
blob4a6a77e59d8d44e9703daacd3d1e2c4c29fa8e81
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.
24 static int
25 _des3_encrypt (Shishi * handle,
26 Shishi_key * key,
27 int keyusage,
28 const char *iv,
29 size_t ivlen,
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);
37 static int
38 _des3_decrypt (Shishi * handle,
39 Shishi_key * key,
40 int keyusage,
41 const char *iv,
42 size_t ivlen,
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);
50 static int
51 des3none_dencrypt (Shishi * handle,
52 Shishi_key * key,
53 int keyusage,
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)
59 int res;
61 if (keyusage != 0)
63 Shishi_key *derivedkey;
65 res = simplified_derivekey (handle, key, keyusage,
66 SHISHI_DERIVEKEYMODE_PRIVACY, &derivedkey);
67 if (res != SHISHI_OK)
68 return res;
70 res =
71 simplified_dencrypt (handle, derivedkey, iv, ivlen, ivout, ivoutlen,
72 in, inlen, out, outlen, direction);
74 shishi_key_done (derivedkey);
76 if (res != SHISHI_OK)
77 return res;
79 else
81 res = simplified_dencrypt (handle, key, iv, ivlen, ivout, ivoutlen,
82 in, inlen, out, outlen, direction);
83 if (res != SHISHI_OK)
84 return res;
87 return SHISHI_OK;
90 static int
91 des3none_encrypt (Shishi * handle,
92 Shishi_key * key,
93 int keyusage,
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);
102 static int
103 des3none_decrypt (Shishi * handle,
104 Shishi_key * key,
105 int keyusage,
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
117 * follows:
119 * 1 2 3 4 5 6 7 p
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.
133 static int
134 des3_random_to_key (Shishi * handle,
135 const char *random, size_t randomlen, Shishi_key * outkey)
137 unsigned char tmpkey[3 * 8];
138 int i;
140 if (randomlen < 168 / 8)
141 return !SHISHI_OK;
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);
148 binprint (random, 168 / 8);
151 memcpy (tmpkey, random, 7);
152 memcpy (tmpkey + 8, random + 7, 7);
153 memcpy (tmpkey + 16, random + 14, 7);
154 for (i = 0; i < 3; i++)
156 tmpkey[i * 8 + 7] =
157 ((tmpkey[i * 8 + 0] & 0x01) << 1) |
158 ((tmpkey[i * 8 + 1] & 0x01) << 2) |
159 ((tmpkey[i * 8 + 2] & 0x01) << 3) |
160 ((tmpkey[i * 8 + 3] & 0x01) << 4) |
161 ((tmpkey[i * 8 + 4] & 0x01) << 5) |
162 ((tmpkey[i * 8 + 5] & 0x01) << 6) | ((tmpkey[i * 8 + 6] & 0x01) << 7);
163 des_set_odd_key_parity (tmpkey + i * 8);
166 shishi_key_value_set (outkey, tmpkey);
168 if (VERBOSECRYPTO (handle))
170 printf ("key = des3_random_to_key (random)\n");
171 printf ("\t ;; key:\n");
172 hexprint (tmpkey, 3 * 8);
173 binprint (tmpkey, 3 * 8);
176 return SHISHI_OK;
179 static int
180 des3_string_to_key (Shishi * handle,
181 const char *string,
182 size_t stringlen,
183 const char *salt,
184 size_t saltlen,
185 const char *parameter, Shishi_key * outkey)
187 char *s;
188 int n_s;
189 Shishi_key *key;
190 char nfold[168 / 8];
191 int nfoldlen = 168 / 8;
192 int res;
194 if (VERBOSECRYPTO (handle))
196 printf ("des3_string_to_key (string, salt)\n");
197 printf ("\t ;; String:\n");
198 escapeprint (string, stringlen);
199 hexprint (string, stringlen);
200 printf ("\t ;; Salt:\n");
201 escapeprint (salt, saltlen);
202 hexprint (salt, saltlen);
205 /* s = passwordString + salt */
206 n_s = stringlen + saltlen;
207 s = (char *) xmalloc (n_s);
208 memcpy (s, string, stringlen);
209 memcpy (s + stringlen, salt, saltlen);
211 /* tmpKey = random-to-key(168-fold(s)) */
212 res = shishi_n_fold (handle, s, n_s, nfold, nfoldlen);
213 free (s);
214 if (res != SHISHI_OK)
215 return res;
217 res = shishi_key_from_value (handle, shishi_key_type (outkey), NULL, &key);
218 if (res != SHISHI_OK)
219 return res;
221 res = des3_random_to_key (handle, nfold, nfoldlen, key);
222 if (res != SHISHI_OK)
223 return res;
225 /* key = DK (tmpKey, KerberosConstant) */
226 res = shishi_dk (handle, key, "kerberos", strlen ("kerberos"), outkey);
227 if (res != SHISHI_OK)
228 return res;
230 shishi_key_done (key);
232 if (VERBOSECRYPTO (handle))
234 printf ("des3_string_to_key (string, salt)\n");
235 printf ("\t ;; Key:\n");
236 hexprint (shishi_key_value (outkey), shishi_key_length (outkey));
237 binprint (shishi_key_value (outkey), shishi_key_length (outkey));
240 return SHISHI_OK;
243 static int
244 des3_checksum (Shishi * handle,
245 Shishi_key * key,
246 int keyusage,
247 int cksumtype,
248 const char *in, size_t inlen, char **out, size_t * outlen)
250 return simplified_checksum (handle, key, keyusage, cksumtype,
251 in, inlen, out, outlen);