3 * $Id: crypt.c,v 1.2 1997/08/04 10:24:21 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.2 1997/08/04 10:24:21 mdw
33 * Sources placed under CVS control.
35 * Revision 1.1 1997/07/21 13:47:51 mdw
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI headers --- */
51 /* --- Unix headers --- */
53 #include <sys/types.h>
59 /* --- Local headers --- */
72 /*----- Magic numbers -----------------------------------------------------*/
74 #define crypt__timeError 60 /* Seconds error to permit */
76 /*----- Main code ---------------------------------------------------------*/
78 /* --- @crypt__sessionKey@ --- *
80 * Arguments: @const char *seedfile@ = pointer to name of seed file
81 * @unsigned char *k@ = our secret key
82 * @unsigned *sk@ = where to store the session key
83 * @unsigned char *iv@ = where to store the IV
87 * Use: Decides on a random session key and initialisation vector.
90 static void crypt__sessionKey(const char *seedfile
, unsigned char *k
,
91 unsigned char *sk
, unsigned char *iv
)
93 FILE *fp
; /* File handle for reading */
97 /* --- Open the random seed file --- *
99 * If I can't manage that, create a new one.
102 if ((fp
= fopen(seedfile
, "r+")) == 0) {
104 if ((fp
= fopen(seedfile
, "w+")) == 0)
105 die("can't create random number file: %s", strerror(errno
));
109 /* --- Lock the seed file against concurrency problems --- */
112 l
.l_whence
= SEEK_SET
;
115 if (fcntl(fileno(fp
), F_SETLKW
, &l
) < 0)
116 die("can't lock random number file: %s", strerror(errno
));
118 /* --- Now read the file, and launder the seed --- */
123 /* --- Encrypt the pool using the secret key --- */
127 icrypt_init(&j
, k
, 0);
132 /* --- Generate the session key and IV --- */
135 rand_extract(sk
, IDEA_KEYSIZE
);
136 rand_extract(iv
, IDEA_BLKSIZE
);
138 IF_TRACING(TRACE_CRYPTO
,
139 traceblk(TRACE_CRYPTO
, "crypto: session key:", sk
, IDEA_KEYSIZE
);
140 traceblk(TRACE_CRYPTO
, "crypto: initialisation vector:",
144 /* --- Write the seed back --- */
152 /* --- @crypt_packRequest@ --- *
154 * Arguments: @request *rq@ = pointer to request block
155 * @unsigned char *buff@ = pointer to a buffer
156 * @time_t t@ = the current time
157 * @pid_t pid@ = my process ID
158 * @unsigned char *k@ = pointer to 128-bit key
159 * @unsigned char *sk@ = where to put the session key
163 * Use: Packs a request block into a buffer. The buffer should have
164 * space for at least @crq_size@ bytes. The buffer comes back
165 * encrypted and ready to send.
168 void crypt_packRequest(request
*rq
, unsigned char *buff
,
170 unsigned char *k
, unsigned char *sk
)
172 /* --- First, build the easy stuff in the block --- */
174 buff
[crq_cryptType
] = cryptType_idea
;
175 store32(buff
+ crq_time
, t
);
176 store32(buff
+ crq_pid
, pid
);
177 store32(buff
+ crq_from
, rq
->from
);
178 store32(buff
+ crq_to
, rq
->to
);
180 /* --- Now generate session keys and things --- */
182 crypt__sessionKey(file_RANDSEED
, k
, sk
, buff
+ crq_iv
);
183 memcpy(buff
+ crq_session
, sk
, IDEA_KEYSIZE
);
185 /* --- The string causes a few problems --- *
187 * There's a good chance that the string will be a good deal shorter than
188 * the space allowed for it. This will probably mean lots of zeroes, and a
189 * very easy known-plaintext job for a potential attacker. (An early
190 * version of this code used @strncpy@ which is even worse!)
192 * I'll fill the block with random (from @rand@(3) -- nothing too
193 * elaborate) and then encrypt it using IDEA in CFB mode, using the first
194 * few bytes as the key. This should provide a sufficiently unpredictable
195 * background for the block.
203 unsigned char qk
[IDEA_KEYSIZE
];
205 /* --- Initialise the buffer with junk --- */
207 srand((unsigned int)(t
^ pid
)); /* Seed the (bad) RNG */
208 for (p
= buff
+ crq_cmd
; p
< buff
+ crq_cmd
+ CMDLEN_MAX
; p
++) {
209 u
= rand(); *p
= u
^ (u
>> 8);
212 /* --- Now make the junk a whole lot harder to predict --- */
215 md5_init(&md
); md5_buffer(&md
, p
, CMDLEN_MAX
); md5_final(&md
, qk
);
216 icrypt_init(&j
, qk
, 0); icrypt_encrypt(&j
, p
, p
, CMDLEN_MAX
);
217 burn(j
); burn(qk
); burn(md
);
219 /* --- Copy the string into here --- */
221 strcpy((char *)buff
+ crq_cmd
, rq
->cmd
);
224 /* --- Checksum the finished data --- */
228 unsigned char mdbuf
[MD5_HASHSIZE
];
231 md5_buffer(&md
, buff
+ crq_cipher
, crq_check
- crq_cipher
);
232 md5_final(&md
, mdbuf
);
233 memcpy(buff
+ crq_check
, mdbuf
, 4);
234 burn(md
); burn(mdbuf
);
237 /* --- Encrypt the block --- *
239 * First, encrypt the session key using the master key. Since the session
240 * key is effectively random, this makes cracking the master key much
241 * harder. The rest of the block is then encrypted with the session key,
242 * using the IV left over from encrypting the session key.
248 T( traceblk(TRACE_CRYPTO
, "crypto: plaintext request:",
251 icrypt_init(&j
, k
, buff
+ crq_iv
);
252 icrypt_encrypt(&j
, buff
+ crq_session
, buff
+ crq_session
, IDEA_KEYSIZE
);
253 icrypt_reset(&j
, sk
, 0);
254 icrypt_encrypt(&j
, buff
+ crq_cipher
,
255 buff
+ crq_cipher
, crq_size
- crq_cipher
);
258 T( traceblk(TRACE_CRYPTO
, "crypto: ciphertext request:",
263 /* --- @crypt_unpackRequest@ --- *
265 * Arguments: @reqest *rq@ = pointer to destination request block
266 * @unsigned char *buff@ = pointer to source buffer
267 * @unsigned char *k@ = pointer to encryption key
268 * @unsigned char *sk@ = pointer to where to store session key
269 * @unsigned char *rpl@ = where to start building reply
271 * Returns: Nonzero if it was decrypted OK
273 * Use: Decrypts and unpacks a request buffer.
276 int crypt_unpackRequest(request
*rq
, unsigned char *buff
,
277 unsigned char *k
, unsigned char *sk
,
281 /* --- Check the encryption format --- */
283 if (buff
[crq_cryptType
] != cryptType_idea
)
288 /* --- First things first: decrypt the block --- */
292 T( traceblk(TRACE_CRYPTO
, "crypto: ciphertext request:",
295 icrypt_init(&j
, k
, buff
+ crq_iv
);
296 icrypt_decrypt(&j
, buff
+ crq_session
, buff
+ crq_session
, IDEA_KEYSIZE
);
297 memcpy(sk
, buff
+ crq_session
, IDEA_KEYSIZE
);
298 icrypt_reset(&j
, sk
, 0);
299 icrypt_decrypt(&j
, buff
+ crq_cipher
,
300 buff
+ crq_cipher
, crq_size
- crq_cipher
);
301 icrypt_saveIV(&j
, rpl
+ crp_iv
);
303 memset(buff
+ crq_session
, 0, IDEA_KEYSIZE
); /* Burn, baby, burn */
306 T( traceblk(TRACE_CRYPTO
, "crypto: plaintext request:",
311 /* --- Check the validity of the data therein --- */
314 unsigned char mdbuf
[MD5_HASHSIZE
];
317 md5_buffer(&md
, buff
+ crq_cipher
, crq_check
- crq_cipher
);
318 md5_final(&md
, mdbuf
);
319 if (memcmp(mdbuf
, buff
+ crq_check
, 4) != 0) {
320 syslog(LOG_INFO
, "packet rejected: bad checksum");
321 T( trace(TRACE_CRYPTO
, "crypto: bad checksum on incoming request"); )
324 burn(md
); burn(mdbuf
);
328 /* --- Extract fields from the block --- */
330 rq
->from
= load32(buff
+ crq_from
);
331 rq
->to
= load32(buff
+ crq_to
);
332 memcpy(rq
->cmd
, buff
+ crq_cmd
, CMDLEN_MAX
);
336 /* --- Fill in bits of the reply block --- */
338 long t
= (long)time(0);
339 long u
= (long)load32(buff
+ crq_time
);
341 if (t
- u
> crypt__timeError
|| u
- t
> crypt__timeError
) {
342 syslog(LOG_INFO
, "packet rejected: bad time");
343 T( trace(TRACE_CRYPTO
, "crypto: bad time on incoming request"); )
346 memcpy(rpl
+ crp_time
, buff
+ crq_time
, 8);
351 T( trace(TRACE_CRYPTO
, "crypto: valid request received"); )
355 /* --- @crypt_packReply@ --- *
357 * Arguments: @char *buff@ = pointer to reply block
358 * @unsigned char *sk@ = pointer to session key
359 * @int answer@ = yes or no
363 * Use: Packs and encrypts a reply block.
366 void crypt_packReply(unsigned char *buff
, unsigned char *sk
, int answer
)
369 /* --- Store the answer --- */
371 buff
[crp_answer
] = (answer
!= 0);
375 /* --- Build the checksum --- */
378 unsigned char mdbuf
[MD5_HASHSIZE
];
381 md5_buffer(&md
, buff
+ crp_cipher
, crp_check
- crp_cipher
);
382 md5_final(&md
, mdbuf
);
383 memcpy(buff
+ crp_check
, mdbuf
, 4);
384 burn(md
); burn(mdbuf
);
388 /* --- Encrypt the buffer --- */
392 T( traceblk(TRACE_CRYPTO
, "crypto: plaintext reply:", buff
, crp_size
); )
394 icrypt_init(&j
, sk
, buff
+ crp_iv
);
395 icrypt_encrypt(&j
, buff
+ crp_cipher
,
396 buff
+ crp_cipher
, crp_size
- crp_cipher
);
399 T( traceblk(TRACE_CRYPTO
, "crypto: ciphertext reply:", buff
, crp_size
); )
403 /* --- @crypt_unpackReply@ --- *
405 * Arguments: @unsigned char *buff@ = pointer to reply buffer
406 * @unsigned char *sk@ = pointer to session key
407 * @time_t t@ = time at which request was sent
408 * @pid_t pid@ = my process ID
410 * Returns: >0 if request granted, zero if denied, <0 if reply rejected
412 * Use: Unpacks a reply block, and informs the caller of the outcome.
415 int crypt_unpackReply(unsigned char *buff
, unsigned char *sk
,
419 /* --- Decrypt my reply block --- */
423 T( traceblk(TRACE_CRYPTO
, "crypto: ciphertext reply:", buff
, crp_size
); )
425 icrypt_init(&j
, sk
, buff
+ crp_iv
);
426 icrypt_decrypt(&j
, buff
+ crp_cipher
,
427 buff
+ crp_cipher
, crp_size
- crp_cipher
);
430 T( traceblk(TRACE_CRYPTO
, "crypto: plaintext reply:", buff
, crp_size
); )
434 /* --- Check validity --- */
437 unsigned char mdbuf
[MD5_HASHSIZE
];
440 /* --- Check the checksum --- */
443 md5_buffer(&md
, buff
+ crp_cipher
, crp_check
- crp_cipher
);
444 md5_final(&md
, mdbuf
);
445 if (memcmp(buff
+ crp_check
, mdbuf
, 4) != 0) {
446 syslog(LOG_INFO
, "reply rejected: bad checksum");
447 T( trace(TRACE_CRYPTO
, "crypto: bad checksum on reply"); )
451 /* --- Check the identifier --- */
453 store32(b
+ 0, t
); store32(b
+ 4, pid
);
454 if (memcmp(b
, buff
+ crp_time
, sizeof(b
)) != 0) {
455 syslog(LOG_INFO
, "reply rejected: bad identification marker");
456 T( trace(TRACE_CRYPTO
, "crypto: bad id on reply"); )
461 /* --- Return the value --- */
463 T( trace(TRACE_CRYPTO
, "crypto: valid reply received"); )
464 return (buff
[crp_answer
]);
467 /*----- Test rig ----------------------------------------------------------*/
471 int main(int argc
, char *argv
[])
473 unsigned char buff
[8];
474 unsigned char sk
[IDEA_KEYSIZE
], k
[IDEA_KEYSIZE
];
478 traceon(stdout
, TRACE_CRYPTO
);
481 fp
= fopen(argv
[1], "r");
483 die("fopen: %s", strerror(errno
));
484 tx_getBits(k
, 128, fp
);
486 crypt__sessionKey(argv
[2], k
, sk
, buff
);
492 /*----- That's all, folks -------------------------------------------------*/