Compile with libgcrypt 1.1.13.
[shishi.git] / lib / crypto-des.c
blob1eacf07f2982936cc55b092810afac9eb891f3e0
1 /* crypto-des.c DES 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 des_crc_verify (Shishi * handle, char *out, int *outlen)
27 int res;
28 char md[16];
29 gcry_md_hd_t hd;
30 char *p;
32 memcpy (md, out + 8, 4);
33 memset (out + 8, 0, 4);
35 gcry_md_open (&hd, GCRY_MD_CRC32_RFC1510, 0);
37 if (!hd)
39 puts("CRC not available");
40 return !SHISHI_OK;
43 gcry_md_write (hd, out, *outlen);
44 p = gcry_md_read (hd, GCRY_MD_CRC32_RFC1510);
45 if (VERBOSECRYPTO(handle))
47 int i;
49 for (i = 0; i < 4; i++)
50 printf ("%02X ", md[i] & 0xFF);
51 printf ("\n");
52 for (i = 0; i < 4; i++)
53 printf ("%02X ", p[i] & 0xFF);
54 printf ("\n");
57 if (memcmp (p, md, 4) == 0)
59 memmove (out, out + 8 + 4, *outlen - 8 - 4);
60 *outlen -= 8 + 4;
61 res = SHISHI_OK;
63 else
65 if (VERBOSE(handle))
66 printf ("des-cbc-crc verify fail\n");
67 res = !SHISHI_OK;
70 gcry_md_close (hd);
72 return res;
75 static int
76 des_crc_checksum (Shishi * handle,
77 char *out, size_t *outlen,
78 const char *in, size_t inlen)
80 int res;
81 char buffer[BUFSIZ];
82 char *p;
83 gcry_md_hd_t hd;
85 if (inlen + 8 + 4 > BUFSIZ)
87 shishi_error_printf (handle, "checksum inbuffer too large");
88 return !SHISHI_OK;
91 memcpy (buffer + 8 + 4, in, inlen);
92 memset (buffer + 8, 0, 4);
94 res = shishi_randomize (handle, buffer, 8);
95 if (res != SHISHI_OK)
96 return res;
98 gcry_md_open (&hd, GCRY_MD_CRC32_RFC1510, 0);
99 if (hd == NULL)
100 return SHISHI_GCRYPT_ERROR;
102 gcry_md_write (hd, buffer, inlen + 8 + 4);
103 p = gcry_md_read (hd, GCRY_MD_CRC32_RFC1510);
105 memcpy (buffer + 8, p, 4);
106 gcry_md_close (hd);
108 memcpy (out, buffer, 8 + 4);
110 *outlen = 8 + 4;
112 return SHISHI_OK;
115 static int
116 des_md4_verify (Shishi * handle, char *out, int *outlen)
118 int res;
119 char md[16];
120 gcry_md_hd_t hd;
121 char *p;
123 memcpy (md, out + 8, 16);
124 memset (out + 8, 0, 16);
126 gcry_md_open (&hd, GCRY_MD_MD4, 0);
128 if (!hd)
130 puts("MD4 not available");
131 return !SHISHI_OK;
134 gcry_md_write (hd, out, *outlen);
135 p = gcry_md_read (hd, GCRY_MD_MD4);
136 if (VERBOSECRYPTO(handle))
138 int i;
140 for (i = 0; i < 16; i++)
141 printf ("%02X ", md[i] & 0xFF);
142 printf ("\n");
143 for (i = 0; i < 16; i++)
144 printf ("%02X ", p[i] & 0xFF);
145 printf ("\n");
148 if (memcmp (p, md, 16) == 0)
150 memmove (out, out + 8 + 16, *outlen - 8 - 16);
151 *outlen -= 8 + 16;
152 res = SHISHI_OK;
154 else
156 if (VERBOSE(handle))
157 printf ("des-cbc-md4 verify fail\n");
158 res = !SHISHI_OK;
161 gcry_md_close (hd);
163 return res;
166 static int
167 des_md4_checksum (Shishi * handle,
168 char *out, size_t *outlen,
169 const char *in, size_t inlen)
171 int res;
172 char buffer[BUFSIZ];
173 char *p;
174 gcry_md_hd_t hd;
176 if (inlen + 8 + 16 > BUFSIZ)
178 shishi_error_printf (handle, "checksum inbuffer too large");
179 return !SHISHI_OK;
182 memcpy (buffer + 8 + 16, in, inlen);
183 memset (buffer + 8, 0, 16);
185 res = shishi_randomize (handle, buffer, 8);
186 if (res != SHISHI_OK)
187 return res;
189 gcry_md_open (&hd, GCRY_MD_MD4, 0);
190 if (hd == NULL)
191 return SHISHI_GCRYPT_ERROR;
193 gcry_md_write (hd, buffer, inlen + 8 + 16);
194 p = gcry_md_read (hd, GCRY_MD_MD4);
196 memcpy (buffer + 8, p, 16);
197 gcry_md_close (hd);
199 memcpy (out, buffer, 8 + 16);
201 *outlen = 8 + 16;
203 return SHISHI_OK;
206 static int
207 des_md5_verify (Shishi * handle, char *out, int *outlen)
209 int res;
210 char md[16];
211 gcry_md_hd_t hd;
212 char *p;
214 memcpy (md, out + 8, 16);
215 memset (out + 8, 0, 16);
217 gcry_md_open (&hd, GCRY_MD_MD5, 0);
219 if (!hd)
221 shishi_error_set (handle, "MD5 not available");
222 return !SHISHI_OK;
225 gcry_md_write (hd, out, *outlen);
226 p = gcry_md_read (hd, GCRY_MD_MD5);
227 if (VERBOSECRYPTO(handle))
229 int i;
231 for (i = 0; i < 16; i++)
232 printf ("%02X ", md[i] & 0xFF);
233 printf ("\n");
234 for (i = 0; i < 16; i++)
235 printf ("%02X ", p[i] & 0xFF);
236 printf ("\n");
239 if (memcmp (p, md, 16) == 0)
241 memmove (out, out + 8 + 16, *outlen - 8 - 16);
242 *outlen -= 8 + 16;
243 res = SHISHI_OK;
245 else
247 if (VERBOSE(handle))
248 printf ("des-cbc-md5 verify fail\n");
249 res = !SHISHI_OK;
252 gcry_md_close (hd);
254 return res;
257 static int
258 des_md5_checksum (Shishi * handle,
259 char *out, size_t *outlen,
260 const char *in, size_t inlen)
262 int res;
263 char buffer[BUFSIZ];
264 char *p;
265 gcry_md_hd_t hd;
267 if (inlen + 8 + 16 > BUFSIZ)
269 shishi_error_printf (handle, "checksum inbuffer too large");
270 return !SHISHI_OK;
273 memcpy (buffer + 8 + 16, in, inlen);
274 memset (buffer + 8, 0, 16);
276 res = shishi_randomize (handle, buffer, 8);
277 if (res != SHISHI_OK)
278 return res;
280 gcry_md_open (&hd, GCRY_MD_MD5, 0);
281 if (hd == NULL)
282 return SHISHI_GCRYPT_ERROR;
284 gcry_md_write (hd, buffer, inlen + 8 + 16);
285 p = gcry_md_read (hd, GCRY_MD_MD5);
287 memcpy (buffer + 8, p, 16);
288 gcry_md_close (hd);
290 memcpy (out, buffer, 8 + 16);
292 *outlen = 8 + 16;
294 return SHISHI_OK;
297 static int
298 des_crc_encrypt (Shishi * handle,
299 Shishi_key *key,
300 int keyusage,
301 const char *iv,
302 size_t ivlen,
303 const char *in,
304 size_t inlen,
305 char *out,
306 size_t *outlen)
308 char buffer[BUFSIZ];
309 char buffer2[BUFSIZ];
310 int buflen;
311 int buf2len;
312 int res;
314 memcpy(buffer2, in, inlen);
315 buf2len = inlen;
317 while ((buf2len % 8) != 0)
319 buffer2[buf2len] = '\0'; /* XXX */
320 buf2len++;
323 buflen = sizeof (buffer);
324 res = des_crc_checksum (handle, buffer, &buflen, buffer2, buf2len);
325 memcpy (buffer + buflen, buffer2, buf2len);
326 buflen += buf2len;
327 res = simplified_encrypt (handle, key, 0, iv, ivlen,
328 buffer, buflen, out, outlen);
330 return res;
333 static int
334 des_crc_decrypt (Shishi * handle,
335 Shishi_key *key,
336 int keyusage,
337 const char *iv,
338 size_t ivlen,
339 const char *in,
340 size_t inlen,
341 char *out,
342 size_t *outlen)
344 int res;
346 printf("in %d\n", inlen);
347 res = simplified_decrypt (handle, key, 0, iv, ivlen,
348 in, inlen, out, outlen);
349 if (res != SHISHI_OK)
351 shishi_error_set (handle, "decrypt failed");
352 return res;
354 #if 0
355 memcpy(out, "\x56\xcc\xa9\xd6\x67\x0a\xca\x0e\xbc\x58\xdc\x9b\x79\x81\xd3\x30\x81\xd0\xa0\x13\x30\x11\xa0\x03\x02\x01\x01\xa1\x0a\x04\x08\x8f\x75\x58\x45\x9d\x31\x6b\x1f\xa1\x1c\x30\x1a\x30\x18\xa0\x03\x02\x01\x00\xa1\x11\x18\x0f\x31\x39\x37\x30\x30\x31\x30\x31\x30\x30\x30\x30\x30\x30\x5a\xa2\x06\x02\x04\x3d\xdd\x3a\x46\xa4\x07\x03\x05\x00\x50\x40\x00\x00\xa5\x11\x18\x0f\x32\x30\x30\x32\x31\x31\x32\x31\x31\x39\x35\x35\x35\x30\x5a\xa7\x11\x18\x0f\x32\x30\x30\x32\x31\x31\x32\x32\x30\x35\x35\x35\x35\x30\x5a\xa9\x0f\x1b\x0d\x4a\x4f\x53\x45\x46\x53\x53\x4f\x4e\x2e\x4f\x52\x47\xaa\x22\x30\x20\xa0\x03\x02\x01\x00\xa1\x19\x30\x17\x1b\x06\x6b\x72\x62\x74\x67\x74\x1b\x0d\x4a\x4f\x53\x45\x46\x53\x53\x4f\x4e\x2e\x4f\x52\x47\xab\x2f\x30\x2d\x30\x0d\xa0\x03\x02\x01\x02\xa1\x06\x04\x04\xc0\xa8\x01\x01\x30\x0d\xa0\x03\x02\x01\x02\xa1\x06\x04\x04\xc0\xa8\x02\x01\x30\x0d\xa0\x03\x02\x01\x02\xa1\x06\x04\x04\xd9\xd0\xac\x49\x00\x00\x00\x00\x00\x00", 232);
356 *outlen = 232;
357 #endif
359 size_t i;
360 printf("decrypt %d\n", *outlen);
361 for(i=0; i < *outlen; i++)
362 printf("%02x ", ((char*)out)[i] & 0xFF);
363 printf("\n");
365 res = des_crc_verify (handle, out, outlen);
366 if (res != SHISHI_OK)
368 shishi_error_set (handle, "verify failed");
369 return res;
372 return res;
375 static int
376 des_md4_encrypt (Shishi * handle,
377 Shishi_key *key,
378 int keyusage,
379 const char *iv,
380 size_t ivlen,
381 const char *in,
382 size_t inlen,
383 char *out,
384 size_t *outlen)
386 char buffer[BUFSIZ];
387 char buffer2[BUFSIZ];
388 size_t buflen;
389 size_t buf2len;
390 int res;
392 memcpy(buffer2, in, inlen);
393 buf2len = inlen;
394 while ((buf2len % 8) != 0)
396 buffer2[buf2len] = '\0'; /* XXX */
397 buf2len++;
400 buflen = sizeof (buffer);
401 res = des_md4_checksum (handle, buffer, &buflen, buffer2, buf2len);
402 memcpy (buffer + buflen, buffer, buf2len);
403 buflen += buf2len;
404 res = simplified_encrypt (handle, key, 0, iv, ivlen,
405 buffer, buflen, out, outlen);
407 return res;
410 static int
411 des_md4_decrypt (Shishi * handle,
412 Shishi_key *key,
413 int keyusage,
414 const char *iv,
415 size_t ivlen,
416 const char *in,
417 size_t inlen,
418 char *out,
419 size_t *outlen)
421 int res;
423 res = simplified_decrypt (handle, key, 0, iv, ivlen,
424 in, inlen, out, outlen);
425 if (res != SHISHI_OK)
427 shishi_error_set (handle, "decrypt failed");
428 return res;
430 res = des_md4_verify (handle, out, outlen);
431 if (res != SHISHI_OK)
433 shishi_error_set (handle, "verify failed");
434 return res;
437 return res;
440 static int
441 des_md5_encrypt (Shishi * handle,
442 Shishi_key *key,
443 int keyusage,
444 const char *iv,
445 size_t ivlen,
446 const char *in,
447 size_t inlen,
448 char *out,
449 size_t *outlen)
451 char buffer[BUFSIZ];
452 char buffer2[BUFSIZ];
453 size_t buflen;
454 size_t buf2len;
455 int res;
457 memcpy(buffer2, in, inlen);
458 buf2len = inlen;
460 while ((buf2len % 8) != 0)
462 buffer2[buf2len] = '\0'; /* XXX */
463 buf2len++;
466 buflen = sizeof (buffer);
467 res = des_md5_checksum (handle, buffer, &buflen, buffer2, buf2len);
468 memcpy (buffer + buflen, buffer2, buf2len);
469 buflen += buf2len;
470 res = simplified_encrypt (handle, key, 0, iv, ivlen,
471 buffer, buflen, out, outlen);
473 return res;
476 static int
477 des_md5_decrypt (Shishi * handle,
478 Shishi_key *key,
479 int keyusage,
480 const char *iv,
481 size_t ivlen,
482 const char *in,
483 size_t inlen,
484 char *out,
485 size_t *outlen)
487 int res;
489 res = simplified_decrypt (handle, key, 0, iv, ivlen,
490 in, inlen, out, outlen);
491 if (res != SHISHI_OK)
493 shishi_error_set (handle, "decrypt failed");
494 return res;
496 res = des_md5_verify (handle, out, outlen);
497 if (res != SHISHI_OK)
499 shishi_error_set (handle, "verify failed");
500 return res;
503 return res;
506 static int
507 des_none_encrypt (Shishi * handle,
508 Shishi_key *key,
509 int keyusage,
510 const char *iv,
511 size_t ivlen,
512 const char *in,
513 size_t inlen,
514 char *out,
515 size_t *outlen)
517 int res;
519 res = simplified_encrypt (handle, key, 0, iv, ivlen,
520 in, inlen, out, outlen);
521 if (res != SHISHI_OK)
522 return res;
524 return SHISHI_OK;
527 static int
528 des_none_decrypt (Shishi * handle,
529 Shishi_key *key,
530 int keyusage,
531 const char *iv,
532 size_t ivlen,
533 const char *in,
534 size_t inlen,
535 char *out,
536 size_t *outlen)
538 int res;
540 res = simplified_decrypt (handle, key, 0, iv, ivlen,
541 in, inlen, out, outlen);
542 if (res != SHISHI_OK)
543 return res;
545 return SHISHI_OK;
548 static int
549 des_set_odd_key_parity (char key[8])
551 int i, j;
553 for (i = 0; i < 8; i++)
555 int n_set_bits = 0;
557 for (j = 1; j < 8; j++)
558 if (key[i] & (1 << j))
559 n_set_bits++;
561 key[i] &= ~1;
562 if ((n_set_bits % 2) == 0)
563 key[i] |= 1;
566 return SHISHI_OK;
569 static int
570 des_key_correction (Shishi * handle, char *key)
572 int res;
573 gcry_cipher_hd_t ch;
575 /* fixparity(key); */
576 des_set_odd_key_parity (key);
578 gcry_cipher_open (&ch, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_CBC, 0);
579 if (ch == NULL)
580 return !SHISHI_OK;
582 /* XXX libgcrypt tests for pseudo-weak keys, rfc 1510 doesn't */
584 res = gcry_cipher_setkey (ch, key, 8);
585 if (res != GPG_ERR_NO_ERROR)
587 printf("hepp %d\n", gpg_err_code(res));
588 if (gpg_err_code(res) == GPG_ERR_WEAK_KEY)
590 if (VERBOSECRYPTO(handle))
591 printf ("\t ;; WEAK KEY (corrected)\n");
592 key[7] ^= 0xF0;
594 else
595 return !SHISHI_OK;
598 gcry_cipher_close (ch);
600 return SHISHI_OK;
603 static int
604 des_cbc_check (char key[8], char *data, int n_data)
606 gcry_cipher_hd_t ch;
607 int res;
609 gcry_cipher_open (&ch, GCRY_CIPHER_DES,
610 GCRY_CIPHER_MODE_CBC,
611 GCRY_CIPHER_CBC_MAC);
612 if (ch == NULL)
613 return SHISHI_GCRYPT_ERROR;
615 res = gcry_cipher_setkey (ch, key, 8);
616 if (res != GPG_ERR_NO_ERROR)
617 return SHISHI_GCRYPT_ERROR;
619 res = gcry_cipher_setiv (ch, key, 8);
620 if (res != 0)
621 return SHISHI_GCRYPT_ERROR;
623 res = gcry_cipher_encrypt (ch, key, 8, data, n_data);
624 if (res != 0)
625 return SHISHI_GCRYPT_ERROR;
627 gcry_cipher_close (ch);
629 return SHISHI_OK;
632 static int
633 des_random_to_key (Shishi * handle,
634 const char *random,
635 size_t randomlen,
636 Shishi_key *outkey)
638 char tmp[MAX_RANDOM_LEN];
639 int keylen = shishi_cipher_keylen (shishi_key_type(outkey));
641 if (randomlen != shishi_key_length(outkey))
642 return !SHISHI_OK;
644 memcpy(tmp, random, keylen);
645 des_set_odd_key_parity (tmp);
647 shishi_key_value_set(outkey, tmp);
649 return SHISHI_OK;
652 static int
653 des_string_to_key (Shishi * handle,
654 const char *string,
655 size_t stringlen,
656 const char *salt,
657 size_t saltlen,
658 const char *parameter,
659 Shishi_key *outkey)
661 char *s;
662 int n_s;
663 int odd;
664 char tempkey[8];
665 int i, j;
666 char temp, temp2;
667 int res;
669 if (VERBOSECRYPTO(handle))
671 printf ("des_string_to_key (string, salt)\n");
673 printf ("\t ;; String:\n");
674 escapeprint (string, stringlen);
675 hexprint (string, stringlen);
676 puts ("");
677 puts ("");
679 printf ("\t ;; Salt:\n");
680 escapeprint (salt, saltlen);
681 hexprint (salt, saltlen);
682 puts ("");
684 printf ("odd = 1;\n");
685 printf ("s = string | salt;\n");
686 printf ("tempstring = 0; /* 56-bit string */\n");
687 printf ("pad(s); /* with nulls to 8 byte boundary */\n");
691 if (saltlen < 0)
692 saltlen = 0;
694 odd = 1;
695 n_s = stringlen + saltlen;
696 if ((n_s % 8) != 0)
697 n_s += 8 - n_s % 8;
698 s = (char *) malloc (n_s);
699 memcpy (s, string, stringlen);
700 if (saltlen > 0)
701 memcpy (s + stringlen, salt, saltlen);
702 memset (s + stringlen + saltlen, 0, n_s - stringlen - saltlen);
703 memset (tempkey, 0, sizeof (tempkey)); /* tempkey = NULL; */
705 if (VERBOSECRYPTO(handle))
707 printf ("\t ;; s = pad(string|salt):\n");
708 escapeprint (s, n_s);
709 hexprint (s, n_s);
710 puts ("");
713 for (i = 0; i < n_s / 8; i++)
715 if (VERBOSECRYPTO(handle))
717 printf ("for (8byteblock in s) {\n");
718 printf ("\t ;; loop iteration %d\n", i);
719 printf ("\t ;; 8byteblock:\n");
720 escapeprint (&s[i * 8], 8);
721 hexprint (&s[i * 8], 8);
722 puts ("");
723 binprint (&s[i * 8], 8);
724 puts ("");
725 printf ("56bitstring = removeMSBits(8byteblock);\n");
728 for (j = 0; j < 8; j++)
729 s[i * 8 + j] = s[i * 8 + j] & ~0x80;
731 if (VERBOSECRYPTO(handle))
733 printf ("\t ;; 56bitstring:\n");
734 bin7print (&s[i * 8], 8);
735 puts ("");
736 printf ("if (odd == 0) reverse(56bitstring);\t ;; odd=%d\n", odd);
739 if (odd == 0)
741 for (j = 0; j < 4; j++)
743 temp = s[i * 8 + j];
744 temp =
745 ((temp >> 6) & 0x01) |
746 ((temp >> 4) & 0x02) |
747 ((temp >> 2) & 0x04) |
748 ((temp) & 0x08) |
749 ((temp << 2) & 0x10) |
750 ((temp << 4) & 0x20) | ((temp << 6) & 0x40);
751 temp2 = s[i * 8 + 7 - j];
752 temp2 =
753 ((temp2 >> 6) & 0x01) |
754 ((temp2 >> 4) & 0x02) |
755 ((temp2 >> 2) & 0x04) |
756 ((temp2) & 0x08) |
757 ((temp2 << 2) & 0x10) |
758 ((temp2 << 4) & 0x20) | ((temp2 << 6) & 0x40);
759 s[i * 8 + j] = temp2;
760 s[i * 8 + 7 - j] = temp;
762 if (VERBOSECRYPTO(handle))
764 printf ("reverse(56bitstring)\n");
765 printf ("\t ;; 56bitstring after reverse\n");
766 bin7print (&s[i * 8], 8);
767 puts ("");
771 odd = !odd;
773 if (VERBOSECRYPTO(handle))
775 printf ("odd = ! odd\n");
776 printf ("tempstring = tempstring XOR 56bitstring;\n");
779 /* tempkey = tempkey XOR 8byteblock; */
780 for (j = 0; j < 8; j++)
781 tempkey[j] ^= s[i * 8 + j];
783 if (VERBOSECRYPTO(handle))
785 printf ("\t ;; tempstring\n");
786 bin7print (tempkey, 8);
787 puts ("");
788 puts ("");
792 for (j = 0; j < 8; j++)
793 tempkey[j] = tempkey[j] << 1;
795 if (VERBOSECRYPTO(handle))
797 printf ("for (8byteblock in s) {\n");
798 printf ("}\n");
799 printf ("\t ;; for loop terminated\n");
800 printf ("\t ;; tempstring as 64bitblock\n");
801 hexprint (tempkey, 8);
802 puts ("");
803 binprint (tempkey, 8);
804 puts ("");
805 printf ("/* add parity as low bit of each byte */\n");
806 printf ("tempkey = key_correction(add_parity_bits(tempstring));\n");
809 res = des_key_correction (handle, tempkey);
810 if (res != SHISHI_OK)
811 return res;
813 if (VERBOSECRYPTO(handle))
815 printf ("\t ;; tempkey\n");
816 escapeprint (tempkey, 8);
817 hexprint (tempkey, 8);
818 puts ("");
819 binprint (tempkey, 8);
820 puts ("");
821 puts ("");
822 printf ("key = key_correction(DES-CBC-check(s,tempkey));\n");
825 memcpy (s, string, stringlen);
826 if (saltlen > 0)
827 memcpy (s + stringlen, salt, saltlen);
828 memset (s + stringlen + saltlen, 0, n_s - stringlen - saltlen);
830 res = des_cbc_check (tempkey, s, n_s);
831 if (res != SHISHI_OK)
832 return res;
834 res = des_key_correction (handle, tempkey);
835 if (res != SHISHI_OK)
836 return res;
838 if (VERBOSECRYPTO(handle))
840 printf ("\t ;; key\n");
841 escapeprint (tempkey, 8);
842 hexprint (tempkey, 8);
843 puts ("");
844 binprint (tempkey, 8);
845 puts ("");
846 puts ("");
849 shishi_key_value_set (outkey, tempkey);
851 return SHISHI_OK;
854 static int
855 checksum_md4 (Shishi * handle,
856 char *out, int *outlen, char *in, int inlen)
858 int res;
859 char buffer[BUFSIZ];
860 gcry_md_hd_t hd;
861 char *p;
863 if (inlen + 8 > BUFSIZ)
865 shishi_error_printf (handle, "checksum inbuffer too large");
866 return !SHISHI_OK;
869 memcpy (buffer + 8, in, inlen);
871 #if 0
872 printf ("cksum in len=%d:", inlen);
873 for (i = 0; i < inlen; i++)
874 printf ("%02x ", in[i] & 0xFF);
875 printf ("\n");
876 #endif
878 res = shishi_randomize (handle, buffer, 8);
879 if (res != SHISHI_OK)
880 return res;
882 #if 0
883 printf ("cksum random: ");
884 for (i = 0; i < 8; i++)
885 printf ("%02X ", buffer[i] & 0xFF);
886 printf ("\n");
887 #endif
889 gcry_md_open (&hd, GCRY_MD_MD4, 0);
890 if (!hd)
891 return SHISHI_GCRYPT_ERROR;
893 gcry_md_write (hd, buffer, inlen + 8);
894 p = gcry_md_read (hd, GCRY_MD_MD4);
896 #if 0
897 printf ("cksum md4: ");
898 for (i = 0; i < 16; i++)
899 printf ("%02X ", p[i] & 0xFF);
900 printf ("\n");
901 #endif
903 memcpy (buffer + 8, p, 16);
904 gcry_md_close (hd);
906 memcpy (out, buffer, 8 + 16);
908 *outlen = 8 + 16;
910 #if 0
911 printf ("cksum out: ");
912 for (i = 0; i < *outlen; i++)
913 printf ("%02X ", out[i] & 0xFF);
914 printf ("\n");
915 #endif
917 return SHISHI_OK;
920 static int
921 checksum_md5 (Shishi * handle,
922 char *out, int *outlen, char *in, int inlen)
924 int res;
925 char buffer[BUFSIZ];
926 gcry_md_hd_t hd;
927 char *p;
929 if (inlen + 8 > BUFSIZ)
931 shishi_error_printf (handle, "checksum inbuffer too large");
932 return !SHISHI_OK;
935 memcpy (buffer + 8, in, inlen);
937 #if 0
938 printf ("cksum in len=%d:", inlen);
939 for (i = 0; i < inlen; i++)
940 printf ("%02x ", in[i] & 0xFF);
941 printf ("\n");
942 #endif
944 res = shishi_randomize (handle, buffer, 8);
945 if (res != SHISHI_OK)
946 return res;
948 #if 0
949 printf ("cksum random: ");
950 for (i = 0; i < 8; i++)
951 printf ("%02X ", buffer[i] & 0xFF);
952 printf ("\n");
953 #endif
955 gcry_md_open (&hd, GCRY_MD_MD5, 0);
956 if (!hd)
957 return SHISHI_GCRYPT_ERROR;
959 gcry_md_write (hd, buffer, inlen + 8);
960 p = gcry_md_read (hd, GCRY_MD_MD5);
962 #if 0
963 printf ("cksum md5: ");
964 for (i = 0; i < 16; i++)
965 printf ("%02X ", p[i] & 0xFF);
966 printf ("\n");
967 #endif
969 memcpy (buffer + 8, p, 16);
970 gcry_md_close (hd);
972 memcpy (out, buffer, 8 + 16);
974 *outlen = 8 + 16;
976 #if 0
977 printf ("cksum out: ");
978 for (i = 0; i < *outlen; i++)
979 printf ("%02X ", out[i] & 0xFF);
980 printf ("\n");
981 #endif
983 return SHISHI_OK;