3 * $Id: crypt.c,v 1.4 1998/01/12 16:45:55 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.4 1998/01/12 16:45:55 mdw
35 * Revision 1.3 1997/09/26 09:14:58 mdw
36 * Merged blowfish branch into trunk.
38 * Revision 1.2.2.1 1997/09/26 09:08:02 mdw
39 * Use the Blowfish encryption algorithm instead of IDEA. This is partly
40 * because I prefer Blowfish (without any particularly strong evidence) but
41 * mainly because IDEA is patented and Blowfish isn't.
43 * Revision 1.2 1997/08/04 10:24:21 mdw
44 * Sources placed under CVS control.
46 * Revision 1.1 1997/07/21 13:47:51 mdw
51 /*----- Header files ------------------------------------------------------*/
53 /* --- ANSI headers --- */
62 /* --- Unix headers --- */
64 #include <sys/types.h>
70 /* --- Local headers --- */
83 /*----- Magic numbers -----------------------------------------------------*/
85 #define crypt__timeError 60 /* Seconds error to permit */
87 /*----- Main code ---------------------------------------------------------*/
89 /* --- @crypt__sessionKey@ --- *
91 * Arguments: @const char *seedfile@ = pointer to name of seed file
92 * @unsigned char *k@ = our secret key
93 * @unsigned *sk@ = where to store the session key
94 * @unsigned char *iv@ = where to store the IV
98 * Use: Decides on a random session key and initialisation vector.
101 static void crypt__sessionKey(const char *seedfile
, unsigned char *k
,
102 unsigned char *sk
, unsigned char *iv
)
104 FILE *fp
; /* File handle for reading */
108 /* --- Open the random seed file --- *
110 * If I can't manage that, create a new one.
113 if ((fp
= fopen(seedfile
, "r+")) == 0) {
115 if ((fp
= fopen(seedfile
, "w+")) == 0)
116 die("can't create random number file: %s", strerror(errno
));
120 /* --- Lock the seed file against concurrency problems --- */
123 l
.l_whence
= SEEK_SET
;
126 if (fcntl(fileno(fp
), F_SETLKW
, &l
) < 0)
127 die("can't lock random number file: %s", strerror(errno
));
129 /* --- Now read the file, and launder the seed --- */
134 /* --- Encrypt the pool using the secret key --- */
138 icrypt_init(&j
, k
, BLOWFISH_KEYSIZE
, 0);
143 /* --- Generate the session key and IV --- */
146 rand_extract(sk
, BLOWFISH_KEYSIZE
);
147 rand_extract(iv
, BLOWFISH_BLKSIZE
);
149 IF_TRACING(TRACE_CRYPTO
,
150 traceblk(TRACE_CRYPTO
, "crypto: session key:", sk
, BLOWFISH_KEYSIZE
);
151 traceblk(TRACE_CRYPTO
, "crypto: initialisation vector:",
152 iv
, BLOWFISH_BLKSIZE
);
155 /* --- Write the seed back --- */
163 /* --- @crypt_packRequest@ --- *
165 * Arguments: @request *rq@ = pointer to request block
166 * @unsigned char *buff@ = pointer to a buffer
167 * @time_t t@ = the current time
168 * @pid_t pid@ = my process ID
169 * @unsigned char *k@ = pointer to 128-bit key
170 * @unsigned char *sk@ = where to put the session key
174 * Use: Packs a request block into a buffer. The buffer should have
175 * space for at least @crq_size@ bytes. The buffer comes back
176 * encrypted and ready to send.
179 void crypt_packRequest(request
*rq
, unsigned char *buff
,
181 unsigned char *k
, unsigned char *sk
)
183 /* --- First, build the easy stuff in the block --- */
185 buff
[crq_cryptType
] = cryptType_blowfish
;
186 store32(buff
+ crq_time
, t
);
187 store32(buff
+ crq_pid
, pid
);
188 store32(buff
+ crq_from
, rq
->from
);
189 store32(buff
+ crq_to
, rq
->to
);
191 /* --- Now generate session keys and things --- */
193 crypt__sessionKey(file_RANDSEED
, k
, sk
, buff
+ crq_iv
);
194 memcpy(buff
+ crq_session
, sk
, BLOWFISH_KEYSIZE
);
196 /* --- The string causes a few problems --- *
198 * There's a good chance that the string will be a good deal shorter than
199 * the space allowed for it. This will probably mean lots of zeroes, and a
200 * very easy known-plaintext job for a potential attacker. (An early
201 * version of this code used @strncpy@ which is even worse!)
203 * I'll fill the block with random (from @rand@(3) -- nothing too
204 * elaborate) and then encrypt it using Blowfish in CFB mode, using the
205 * first few bytes as the key. This should provide a sufficiently
206 * unpredictable background for the block.
214 unsigned char qk
[BLOWFISH_KEYSIZE
];
216 /* --- Initialise the buffer with junk --- */
218 srand((unsigned int)(t
^ pid
)); /* Seed the (bad) RNG */
219 for (p
= buff
+ crq_cmd
; p
< buff
+ crq_cmd
+ CMDLEN_MAX
; p
++) {
220 u
= rand(); *p
= u
^ (u
>> 8);
223 /* --- Now make the junk a whole lot harder to predict --- */
226 md5_init(&md
); md5_buffer(&md
, p
, CMDLEN_MAX
); md5_final(&md
, qk
);
227 icrypt_init(&j
, qk
, BLOWFISH_KEYSIZE
, 0);
228 icrypt_encrypt(&j
, p
, p
, CMDLEN_MAX
);
229 burn(j
); burn(qk
); burn(md
);
231 /* --- Copy the string into here --- */
233 strcpy((char *)buff
+ crq_cmd
, rq
->cmd
);
236 /* --- Checksum the finished data --- */
240 unsigned char mdbuf
[MD5_HASHSIZE
];
243 md5_buffer(&md
, buff
+ crq_cipher
, crq_check
- crq_cipher
);
244 md5_final(&md
, mdbuf
);
245 memcpy(buff
+ crq_check
, mdbuf
, 4);
246 burn(md
); burn(mdbuf
);
249 /* --- Encrypt the block --- *
251 * First, encrypt the session key using the master key. Since the session
252 * key is effectively random, this makes cracking the master key much
253 * harder. The rest of the block is then encrypted with the session key,
254 * using the IV left over from encrypting the session key.
260 T( traceblk(TRACE_CRYPTO
, "crypto: plaintext request:",
263 T( traceblk(TRACE_CRYPTO
, "crypto: master key:", k
, BLOWFISH_KEYSIZE
); )
264 T( traceblk(TRACE_CRYPTO
, "crypto: initial iv:",
265 buff
+ crq_iv
, BLOWFISH_BLKSIZE
); )
266 T( traceblk(TRACE_CRYPTO
, "crypto: session key:",
267 sk
, BLOWFISH_KEYSIZE
); )
269 icrypt_init(&j
, k
, BLOWFISH_KEYSIZE
, buff
+ crq_iv
);
271 icrypt_encrypt(&j
, buff
+ crq_session
,
272 buff
+ crq_session
, BLOWFISH_KEYSIZE
);
273 T( traceblk(TRACE_CRYPTO
, "crypto: encrypted session key:",
274 buff
+ crq_session
, BLOWFISH_KEYSIZE
); )
276 icrypt_reset(&j
, sk
, BLOWFISH_KEYSIZE
, 0);
278 T( traceblk(TRACE_CRYPTO
, "crypto: partial iv:",
279 j
.iv
, BLOWFISH_BLKSIZE
); )
281 icrypt_encrypt(&j
, buff
+ crq_cipher
,
282 buff
+ crq_cipher
, crq_size
- crq_cipher
);
285 T( traceblk(TRACE_CRYPTO
, "crypto: ciphertext request:",
290 /* --- @crypt_unpackRequest@ --- *
292 * Arguments: @reqest *rq@ = pointer to destination request block
293 * @unsigned char *buff@ = pointer to source buffer
294 * @unsigned char *k@ = pointer to encryption key
295 * @unsigned char *sk@ = pointer to where to store session key
296 * @unsigned char *rpl@ = where to start building reply
298 * Returns: Nonzero if it was decrypted OK
300 * Use: Decrypts and unpacks a request buffer.
303 int crypt_unpackRequest(request
*rq
, unsigned char *buff
,
304 unsigned char *k
, unsigned char *sk
,
308 /* --- Check the encryption format --- */
310 if (buff
[crq_cryptType
] != cryptType_blowfish
)
315 /* --- First things first: decrypt the block --- */
319 T( traceblk(TRACE_CRYPTO
, "crypto: ciphertext request:",
322 T( traceblk(TRACE_CRYPTO
, "crypto: master key:", k
, BLOWFISH_KEYSIZE
); )
323 T( traceblk(TRACE_CRYPTO
, "crypto: initial iv:",
324 buff
+ crq_iv
, BLOWFISH_BLKSIZE
); )
326 icrypt_init(&j
, k
, BLOWFISH_KEYSIZE
, buff
+ crq_iv
);
327 T( traceblk(TRACE_CRYPTO
, "crypto: job block:", &j
, sizeof(j
)); )
329 T( traceblk(TRACE_CRYPTO
, "crypto: encrypted session key:",
330 buff
+ crq_session
, BLOWFISH_KEYSIZE
); )
331 icrypt_decrypt(&j
, buff
+ crq_session
,
332 buff
+ crq_session
, BLOWFISH_KEYSIZE
);
333 memcpy(sk
, buff
+ crq_session
, BLOWFISH_KEYSIZE
);
334 T( traceblk(TRACE_CRYPTO
, "crypto: session key:",
335 sk
, BLOWFISH_KEYSIZE
); )
337 icrypt_reset(&j
, sk
, BLOWFISH_KEYSIZE
, 0);
339 T( traceblk(TRACE_CRYPTO
, "crypto: partial iv:",
340 j
.iv
, BLOWFISH_BLKSIZE
); )
342 icrypt_decrypt(&j
, buff
+ crq_cipher
,
343 buff
+ crq_cipher
, crq_size
- crq_cipher
);
344 icrypt_saveIV(&j
, rpl
+ crp_iv
);
346 T( traceblk(TRACE_CRYPTO
, "crypto: plaintext request:",
349 memset(buff
+ crq_session
, 0, BLOWFISH_KEYSIZE
); /* Burn, baby, burn */
354 /* --- Check the validity of the data therein --- */
357 unsigned char mdbuf
[MD5_HASHSIZE
];
360 md5_buffer(&md
, buff
+ crq_cipher
, crq_check
- crq_cipher
);
361 md5_final(&md
, mdbuf
);
362 if (memcmp(mdbuf
, buff
+ crq_check
, 4) != 0) {
363 syslog(LOG_INFO
, "packet rejected: bad checksum");
364 T( trace(TRACE_CRYPTO
, "crypto: bad checksum on incoming request"); )
367 burn(md
); burn(mdbuf
);
371 /* --- Extract fields from the block --- */
373 rq
->from
= load32(buff
+ crq_from
);
374 rq
->to
= load32(buff
+ crq_to
);
375 memcpy(rq
->cmd
, buff
+ crq_cmd
, CMDLEN_MAX
);
379 /* --- Fill in bits of the reply block --- */
381 long t
= (long)time(0);
382 long u
= (long)load32(buff
+ crq_time
);
384 if (t
- u
> crypt__timeError
|| u
- t
> crypt__timeError
) {
385 syslog(LOG_INFO
, "packet rejected: bad time");
386 T( trace(TRACE_CRYPTO
, "crypto: bad time on incoming request"); )
389 memcpy(rpl
+ crp_time
, buff
+ crq_time
, 8);
394 T( trace(TRACE_CRYPTO
, "crypto: valid request received"); )
398 /* --- @crypt_packReply@ --- *
400 * Arguments: @char *buff@ = pointer to reply block
401 * @unsigned char *sk@ = pointer to session key
402 * @int answer@ = yes or no
406 * Use: Packs and encrypts a reply block.
409 void crypt_packReply(unsigned char *buff
, unsigned char *sk
, int answer
)
412 /* --- Store the answer --- */
414 buff
[crp_answer
] = (answer
!= 0);
418 /* --- Build the checksum --- */
421 unsigned char mdbuf
[MD5_HASHSIZE
];
424 md5_buffer(&md
, buff
+ crp_cipher
, crp_check
- crp_cipher
);
425 md5_final(&md
, mdbuf
);
426 memcpy(buff
+ crp_check
, mdbuf
, 4);
427 burn(md
); burn(mdbuf
);
431 /* --- Encrypt the buffer --- */
435 T( traceblk(TRACE_CRYPTO
, "crypto: plaintext reply:", buff
, crp_size
); )
437 icrypt_init(&j
, sk
, BLOWFISH_KEYSIZE
, buff
+ crp_iv
);
438 icrypt_encrypt(&j
, buff
+ crp_cipher
,
439 buff
+ crp_cipher
, crp_size
- crp_cipher
);
442 T( traceblk(TRACE_CRYPTO
, "crypto: ciphertext reply:", buff
, crp_size
); )
446 /* --- @crypt_unpackReply@ --- *
448 * Arguments: @unsigned char *buff@ = pointer to reply buffer
449 * @unsigned char *sk@ = pointer to session key
450 * @time_t t@ = time at which request was sent
451 * @pid_t pid@ = my process ID
453 * Returns: >0 if request granted, zero if denied, <0 if reply rejected
455 * Use: Unpacks a reply block, and informs the caller of the outcome.
458 int crypt_unpackReply(unsigned char *buff
, unsigned char *sk
,
462 /* --- Decrypt my reply block --- */
466 T( traceblk(TRACE_CRYPTO
, "crypto: ciphertext reply:", buff
, crp_size
); )
468 icrypt_init(&j
, sk
, BLOWFISH_KEYSIZE
, buff
+ crp_iv
);
469 icrypt_decrypt(&j
, buff
+ crp_cipher
,
470 buff
+ crp_cipher
, crp_size
- crp_cipher
);
473 T( traceblk(TRACE_CRYPTO
, "crypto: plaintext reply:", buff
, crp_size
); )
477 /* --- Check validity --- */
480 unsigned char mdbuf
[MD5_HASHSIZE
];
483 /* --- Check the checksum --- */
486 md5_buffer(&md
, buff
+ crp_cipher
, crp_check
- crp_cipher
);
487 md5_final(&md
, mdbuf
);
488 if (memcmp(buff
+ crp_check
, mdbuf
, 4) != 0) {
489 syslog(LOG_INFO
, "reply rejected: bad checksum");
490 T( trace(TRACE_CRYPTO
, "crypto: bad checksum on reply"); )
494 /* --- Check the identifier --- */
496 store32(b
+ 0, t
); store32(b
+ 4, pid
);
497 if (memcmp(b
, buff
+ crp_time
, sizeof(b
)) != 0) {
498 syslog(LOG_INFO
, "reply rejected: bad identification marker");
499 T( trace(TRACE_CRYPTO
, "crypto: bad id on reply"); )
504 /* --- Return the value --- */
506 T( trace(TRACE_CRYPTO
, "crypto: valid reply received"); )
507 return (buff
[crp_answer
]);
510 /*----- Test rig ----------------------------------------------------------*/
514 int main(int argc
, char *argv
[])
516 unsigned char buff
[8];
517 unsigned char sk
[BLOWFISH_KEYSIZE
], k
[BLOWFISH_KEYSIZE
];
521 traceon(stdout
, TRACE_CRYPTO
);
524 fp
= fopen(argv
[1], "r");
526 die("fopen: %s", strerror(errno
));
527 tx_getBits(k
, 128, fp
);
529 crypt__sessionKey(argv
[2], k
, sk
, buff
);
535 /*----- That's all, folks -------------------------------------------------*/