3 * $Id: crypt.c,v 1.3 1997/09/26 09:14:58 mdw Exp $
5 * Cryptographic transfer of `become' requests
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.3 1997/09/26 09:14:58 mdw
33 * Merged blowfish branch into trunk.
35 * Revision 1.2.2.1 1997/09/26 09:08:02 mdw
36 * Use the Blowfish encryption algorithm instead of IDEA. This is partly
37 * because I prefer Blowfish (without any particularly strong evidence) but
38 * mainly because IDEA is patented and Blowfish isn't.
40 * Revision 1.2 1997/08/04 10:24:21 mdw
41 * Sources placed under CVS control.
43 * Revision 1.1 1997/07/21 13:47:51 mdw
48 /*----- Header files ------------------------------------------------------*/
50 /* --- ANSI headers --- */
59 /* --- Unix headers --- */
61 #include <sys/types.h>
67 /* --- Local headers --- */
80 /*----- Magic numbers -----------------------------------------------------*/
82 #define crypt__timeError 60 /* Seconds error to permit */
84 /*----- Main code ---------------------------------------------------------*/
86 /* --- @crypt__sessionKey@ --- *
88 * Arguments: @const char *seedfile@ = pointer to name of seed file
89 * @unsigned char *k@ = our secret key
90 * @unsigned *sk@ = where to store the session key
91 * @unsigned char *iv@ = where to store the IV
95 * Use: Decides on a random session key and initialisation vector.
98 static void crypt__sessionKey(const char *seedfile, unsigned char *k,
99 unsigned char *sk, unsigned char *iv)
101 FILE *fp; /* File handle for reading */
105 /* --- Open the random seed file --- *
107 * If I can't manage that, create a new one.
110 if ((fp = fopen(seedfile, "r+")) == 0) {
112 if ((fp = fopen(seedfile, "w+")) == 0)
113 die("can't create random number file: %s", strerror(errno));
117 /* --- Lock the seed file against concurrency problems --- */
120 l.l_whence = SEEK_SET;
123 if (fcntl(fileno(fp), F_SETLKW, &l) < 0)
124 die("can't lock random number file: %s", strerror(errno));
126 /* --- Now read the file, and launder the seed --- */
131 /* --- Encrypt the pool using the secret key --- */
135 icrypt_init(&j, k, BLOWFISH_KEYSIZE, 0);
140 /* --- Generate the session key and IV --- */
143 rand_extract(sk, BLOWFISH_KEYSIZE);
144 rand_extract(iv, BLOWFISH_BLKSIZE);
146 IF_TRACING(TRACE_CRYPTO,
147 traceblk(TRACE_CRYPTO, "crypto: session key:", sk, BLOWFISH_KEYSIZE);
148 traceblk(TRACE_CRYPTO, "crypto: initialisation vector:",
149 iv, BLOWFISH_BLKSIZE);
152 /* --- Write the seed back --- */
160 /* --- @crypt_packRequest@ --- *
162 * Arguments: @request *rq@ = pointer to request block
163 * @unsigned char *buff@ = pointer to a buffer
164 * @time_t t@ = the current time
165 * @pid_t pid@ = my process ID
166 * @unsigned char *k@ = pointer to 128-bit key
167 * @unsigned char *sk@ = where to put the session key
171 * Use: Packs a request block into a buffer. The buffer should have
172 * space for at least @crq_size@ bytes. The buffer comes back
173 * encrypted and ready to send.
176 void crypt_packRequest(request *rq, unsigned char *buff,
178 unsigned char *k, unsigned char *sk)
180 /* --- First, build the easy stuff in the block --- */
182 buff[crq_cryptType] = cryptType_blowfish;
183 store32(buff + crq_time, t);
184 store32(buff + crq_pid, pid);
185 store32(buff + crq_from, rq->from);
186 store32(buff + crq_to, rq->to);
188 /* --- Now generate session keys and things --- */
190 crypt__sessionKey(file_RANDSEED, k, sk, buff + crq_iv);
191 memcpy(buff + crq_session, sk, BLOWFISH_KEYSIZE);
193 /* --- The string causes a few problems --- *
195 * There's a good chance that the string will be a good deal shorter than
196 * the space allowed for it. This will probably mean lots of zeroes, and a
197 * very easy known-plaintext job for a potential attacker. (An early
198 * version of this code used @strncpy@ which is even worse!)
200 * I'll fill the block with random (from @rand@(3) -- nothing too
201 * elaborate) and then encrypt it using Blowfish in CFB mode, using the
202 * first few bytes as the key. This should provide a sufficiently
203 * unpredictable background for the block.
211 unsigned char qk[BLOWFISH_KEYSIZE];
213 /* --- Initialise the buffer with junk --- */
215 srand((unsigned int)(t ^ pid)); /* Seed the (bad) RNG */
216 for (p = buff + crq_cmd; p < buff + crq_cmd + CMDLEN_MAX; p++) {
217 u = rand(); *p = u ^ (u >> 8);
220 /* --- Now make the junk a whole lot harder to predict --- */
223 md5_init(&md); md5_buffer(&md, p, CMDLEN_MAX); md5_final(&md, qk);
224 icrypt_init(&j, qk, BLOWFISH_KEYSIZE, 0);
225 icrypt_encrypt(&j, p, p, CMDLEN_MAX);
226 burn(j); burn(qk); burn(md);
228 /* --- Copy the string into here --- */
230 strcpy((char *)buff + crq_cmd, rq->cmd);
233 /* --- Checksum the finished data --- */
237 unsigned char mdbuf[MD5_HASHSIZE];
240 md5_buffer(&md, buff + crq_cipher, crq_check - crq_cipher);
241 md5_final(&md, mdbuf);
242 memcpy(buff + crq_check, mdbuf, 4);
243 burn(md); burn(mdbuf);
246 /* --- Encrypt the block --- *
248 * First, encrypt the session key using the master key. Since the session
249 * key is effectively random, this makes cracking the master key much
250 * harder. The rest of the block is then encrypted with the session key,
251 * using the IV left over from encrypting the session key.
257 T( traceblk(TRACE_CRYPTO, "crypto: plaintext request:",
260 T( traceblk(TRACE_CRYPTO, "crypto: master key:", k, BLOWFISH_KEYSIZE); )
261 T( traceblk(TRACE_CRYPTO, "crypto: initial iv:",
262 buff + crq_iv, BLOWFISH_BLKSIZE); )
263 T( traceblk(TRACE_CRYPTO, "crypto: session key:",
264 sk, BLOWFISH_KEYSIZE); )
266 icrypt_init(&j, k, BLOWFISH_KEYSIZE, buff + crq_iv);
268 icrypt_encrypt(&j, buff + crq_session,
269 buff + crq_session, BLOWFISH_KEYSIZE);
270 T( traceblk(TRACE_CRYPTO, "crypto: encrypted session key:",
271 buff + crq_session, BLOWFISH_KEYSIZE); )
273 icrypt_reset(&j, sk, BLOWFISH_KEYSIZE, 0);
275 T( traceblk(TRACE_CRYPTO, "crypto: partial iv:",
276 j.iv, BLOWFISH_BLKSIZE); )
278 icrypt_encrypt(&j, buff + crq_cipher,
279 buff + crq_cipher, crq_size - crq_cipher);
282 T( traceblk(TRACE_CRYPTO, "crypto: ciphertext request:",
287 /* --- @crypt_unpackRequest@ --- *
289 * Arguments: @reqest *rq@ = pointer to destination request block
290 * @unsigned char *buff@ = pointer to source buffer
291 * @unsigned char *k@ = pointer to encryption key
292 * @unsigned char *sk@ = pointer to where to store session key
293 * @unsigned char *rpl@ = where to start building reply
295 * Returns: Nonzero if it was decrypted OK
297 * Use: Decrypts and unpacks a request buffer.
300 int crypt_unpackRequest(request *rq, unsigned char *buff,
301 unsigned char *k, unsigned char *sk,
305 /* --- Check the encryption format --- */
307 if (buff[crq_cryptType] != cryptType_blowfish)
312 /* --- First things first: decrypt the block --- */
316 T( traceblk(TRACE_CRYPTO, "crypto: ciphertext request:",
319 T( traceblk(TRACE_CRYPTO, "crypto: master key:", k, BLOWFISH_KEYSIZE); )
320 T( traceblk(TRACE_CRYPTO, "crypto: initial iv:",
321 buff + crq_iv, BLOWFISH_BLKSIZE); )
323 icrypt_init(&j, k, BLOWFISH_KEYSIZE, buff + crq_iv);
324 T( traceblk(TRACE_CRYPTO, "crypto: job block:", &j, sizeof(j)); )
326 T( traceblk(TRACE_CRYPTO, "crypto: encrypted session key:",
327 buff + crq_session, BLOWFISH_KEYSIZE); )
328 icrypt_decrypt(&j, buff + crq_session,
329 buff + crq_session, BLOWFISH_KEYSIZE);
330 memcpy(sk, buff + crq_session, BLOWFISH_KEYSIZE);
331 T( traceblk(TRACE_CRYPTO, "crypto: session key:",
332 sk, BLOWFISH_KEYSIZE); )
334 icrypt_reset(&j, sk, BLOWFISH_KEYSIZE, 0);
336 T( traceblk(TRACE_CRYPTO, "crypto: partial iv:",
337 j.iv, BLOWFISH_BLKSIZE); )
339 icrypt_decrypt(&j, buff + crq_cipher,
340 buff + crq_cipher, crq_size - crq_cipher);
341 icrypt_saveIV(&j, rpl + crp_iv);
343 T( traceblk(TRACE_CRYPTO, "crypto: plaintext request:",
346 memset(buff + crq_session, 0, BLOWFISH_KEYSIZE); /* Burn, baby, burn */
351 /* --- Check the validity of the data therein --- */
354 unsigned char mdbuf[MD5_HASHSIZE];
357 md5_buffer(&md, buff + crq_cipher, crq_check - crq_cipher);
358 md5_final(&md, mdbuf);
359 if (memcmp(mdbuf, buff + crq_check, 4) != 0) {
360 syslog(LOG_INFO, "packet rejected: bad checksum");
361 T( trace(TRACE_CRYPTO, "crypto: bad checksum on incoming request"); )
364 burn(md); burn(mdbuf);
368 /* --- Extract fields from the block --- */
370 rq->from = load32(buff + crq_from);
371 rq->to = load32(buff + crq_to);
372 memcpy(rq->cmd, buff + crq_cmd, CMDLEN_MAX);
376 /* --- Fill in bits of the reply block --- */
378 long t = (long)time(0);
379 long u = (long)load32(buff + crq_time);
381 if (t - u > crypt__timeError || u - t > crypt__timeError) {
382 syslog(LOG_INFO, "packet rejected: bad time");
383 T( trace(TRACE_CRYPTO, "crypto: bad time on incoming request"); )
386 memcpy(rpl + crp_time, buff + crq_time, 8);
391 T( trace(TRACE_CRYPTO, "crypto: valid request received"); )
395 /* --- @crypt_packReply@ --- *
397 * Arguments: @char *buff@ = pointer to reply block
398 * @unsigned char *sk@ = pointer to session key
399 * @int answer@ = yes or no
403 * Use: Packs and encrypts a reply block.
406 void crypt_packReply(unsigned char *buff, unsigned char *sk, int answer)
409 /* --- Store the answer --- */
411 buff[crp_answer] = (answer != 0);
415 /* --- Build the checksum --- */
418 unsigned char mdbuf[MD5_HASHSIZE];
421 md5_buffer(&md, buff + crp_cipher, crp_check - crp_cipher);
422 md5_final(&md, mdbuf);
423 memcpy(buff + crp_check, mdbuf, 4);
424 burn(md); burn(mdbuf);
428 /* --- Encrypt the buffer --- */
432 T( traceblk(TRACE_CRYPTO, "crypto: plaintext reply:", buff, crp_size); )
434 icrypt_init(&j, sk, BLOWFISH_KEYSIZE, buff + crp_iv);
435 icrypt_encrypt(&j, buff + crp_cipher,
436 buff + crp_cipher, crp_size - crp_cipher);
439 T( traceblk(TRACE_CRYPTO, "crypto: ciphertext reply:", buff, crp_size); )
443 /* --- @crypt_unpackReply@ --- *
445 * Arguments: @unsigned char *buff@ = pointer to reply buffer
446 * @unsigned char *sk@ = pointer to session key
447 * @time_t t@ = time at which request was sent
448 * @pid_t pid@ = my process ID
450 * Returns: >0 if request granted, zero if denied, <0 if reply rejected
452 * Use: Unpacks a reply block, and informs the caller of the outcome.
455 int crypt_unpackReply(unsigned char *buff, unsigned char *sk,
459 /* --- Decrypt my reply block --- */
463 T( traceblk(TRACE_CRYPTO, "crypto: ciphertext reply:", buff, crp_size); )
465 icrypt_init(&j, sk, BLOWFISH_KEYSIZE, buff + crp_iv);
466 icrypt_decrypt(&j, buff + crp_cipher,
467 buff + crp_cipher, crp_size - crp_cipher);
470 T( traceblk(TRACE_CRYPTO, "crypto: plaintext reply:", buff, crp_size); )
474 /* --- Check validity --- */
477 unsigned char mdbuf[MD5_HASHSIZE];
480 /* --- Check the checksum --- */
483 md5_buffer(&md, buff + crp_cipher, crp_check - crp_cipher);
484 md5_final(&md, mdbuf);
485 if (memcmp(buff + crp_check, mdbuf, 4) != 0) {
486 syslog(LOG_INFO, "reply rejected: bad checksum");
487 T( trace(TRACE_CRYPTO, "crypto: bad checksum on reply"); )
491 /* --- Check the identifier --- */
493 store32(b + 0, t); store32(b + 4, pid);
494 if (memcmp(b, buff + crp_time, sizeof(b)) != 0) {
495 syslog(LOG_INFO, "reply rejected: bad identification marker");
496 T( trace(TRACE_CRYPTO, "crypto: bad id on reply"); )
501 /* --- Return the value --- */
503 T( trace(TRACE_CRYPTO, "crypto: valid reply received"); )
504 return (buff[crp_answer]);
507 /*----- Test rig ----------------------------------------------------------*/
511 int main(int argc, char *argv[])
513 unsigned char buff[8];
514 unsigned char sk[BLOWFISH_KEYSIZE], k[BLOWFISH_KEYSIZE];
518 traceon(stdout, TRACE_CRYPTO);
521 fp = fopen(argv[1], "r");
523 die("fopen: %s", strerror(errno));
524 tx_getBits(k, 128, fp);
526 crypt__sessionKey(argv[2], k, sk, buff);
532 /*----- That's all, folks -------------------------------------------------*/