Add DES encrypt/decrypt without checksum.
[shishi.git] / lib / crypto-des.c
blob47141da8b155c7c925e733f2aff3fa6bbe447960
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 hd;
30 char *p;
32 memcpy (md, out + 8, 4);
33 memset (out + 8, 0, 4);
35 hd = gcry_md_open (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 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 hd = gcry_md_open (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 hd;
121 char *p;
123 memcpy (md, out + 8, 16);
124 memset (out + 8, 0, 16);
126 hd = gcry_md_open (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 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 hd = gcry_md_open (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 hd;
212 char *p;
214 memcpy (md, out + 8, 16);
215 memset (out + 8, 0, 16);
217 hd = gcry_md_open (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 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 hd = gcry_md_open (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 ch;
575 /* fixparity(key); */
576 des_set_odd_key_parity (key);
578 ch = gcry_cipher_open (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 != GCRYERR_SUCCESS)
587 if (res == GCRYERR_WEAK_KEY)
589 if (VERBOSECRYPTO(handle))
590 printf ("\t ;; WEAK KEY (corrected)\n");
591 key[7] ^= 0xF0;
593 else
594 return !SHISHI_OK;
597 gcry_cipher_close (ch);
599 return SHISHI_OK;
602 static int
603 des_cbc_check (char key[8], char *data, int n_data)
605 GCRY_CIPHER_HD ch;
606 int res;
608 ch = gcry_cipher_open (GCRY_CIPHER_DES,
609 GCRY_CIPHER_MODE_CBC,
610 GCRY_CIPHER_CBC_MAC);
611 if (ch == NULL)
612 return SHISHI_GCRYPT_ERROR;
614 res = gcry_cipher_setkey (ch, key, 8);
615 if (res != GCRYERR_SUCCESS)
616 return SHISHI_GCRYPT_ERROR;
618 res = gcry_cipher_setiv (ch, key, 8);
619 if (res != 0)
620 return SHISHI_GCRYPT_ERROR;
622 res = gcry_cipher_encrypt (ch, key, 8, data, n_data);
623 if (res != 0)
624 return SHISHI_GCRYPT_ERROR;
626 gcry_cipher_close (ch);
628 return SHISHI_OK;
631 static int
632 des_random_to_key (Shishi * handle,
633 const char *random,
634 size_t randomlen,
635 Shishi_key *outkey)
637 char tmp[MAX_RANDOM_LEN];
638 int keylen = shishi_cipher_keylen (shishi_key_type(outkey));
640 if (randomlen != shishi_key_length(outkey))
641 return !SHISHI_OK;
643 memcpy(tmp, random, keylen);
644 des_set_odd_key_parity (tmp);
646 shishi_key_value_set(outkey, tmp);
648 return SHISHI_OK;
651 static int
652 des_string_to_key (Shishi * handle,
653 const char *string,
654 size_t stringlen,
655 const char *salt,
656 size_t saltlen,
657 const char *parameter,
658 Shishi_key *outkey)
660 char *s;
661 int n_s;
662 int odd;
663 char tempkey[8];
664 int i, j;
665 char temp, temp2;
666 int res;
668 if (VERBOSECRYPTO(handle))
670 printf ("des_string_to_key (string, salt)\n");
672 printf ("\t ;; String:\n");
673 escapeprint (string, stringlen);
674 hexprint (string, stringlen);
675 puts ("");
676 puts ("");
678 printf ("\t ;; Salt:\n");
679 escapeprint (salt, saltlen);
680 hexprint (salt, saltlen);
681 puts ("");
683 printf ("odd = 1;\n");
684 printf ("s = string | salt;\n");
685 printf ("tempstring = 0; /* 56-bit string */\n");
686 printf ("pad(s); /* with nulls to 8 byte boundary */\n");
690 if (saltlen < 0)
691 saltlen = 0;
693 odd = 1;
694 n_s = stringlen + saltlen;
695 if ((n_s % 8) != 0)
696 n_s += 8 - n_s % 8;
697 s = (char *) malloc (n_s);
698 memcpy (s, string, stringlen);
699 if (saltlen > 0)
700 memcpy (s + stringlen, salt, saltlen);
701 memset (s + stringlen + saltlen, 0, n_s - stringlen - saltlen);
702 memset (tempkey, 0, sizeof (tempkey)); /* tempkey = NULL; */
704 if (VERBOSECRYPTO(handle))
706 printf ("\t ;; s = pad(string|salt):\n");
707 escapeprint (s, n_s);
708 hexprint (s, n_s);
709 puts ("");
712 for (i = 0; i < n_s / 8; i++)
714 if (VERBOSECRYPTO(handle))
716 printf ("for (8byteblock in s) {\n");
717 printf ("\t ;; loop iteration %d\n", i);
718 printf ("\t ;; 8byteblock:\n");
719 escapeprint (&s[i * 8], 8);
720 hexprint (&s[i * 8], 8);
721 puts ("");
722 binprint (&s[i * 8], 8);
723 puts ("");
724 printf ("56bitstring = removeMSBits(8byteblock);\n");
727 for (j = 0; j < 8; j++)
728 s[i * 8 + j] = s[i * 8 + j] & ~0x80;
730 if (VERBOSECRYPTO(handle))
732 printf ("\t ;; 56bitstring:\n");
733 bin7print (&s[i * 8], 8);
734 puts ("");
735 printf ("if (odd == 0) reverse(56bitstring);\t ;; odd=%d\n", odd);
738 if (odd == 0)
740 for (j = 0; j < 4; j++)
742 temp = s[i * 8 + j];
743 temp =
744 ((temp >> 6) & 0x01) |
745 ((temp >> 4) & 0x02) |
746 ((temp >> 2) & 0x04) |
747 ((temp) & 0x08) |
748 ((temp << 2) & 0x10) |
749 ((temp << 4) & 0x20) | ((temp << 6) & 0x40);
750 temp2 = s[i * 8 + 7 - j];
751 temp2 =
752 ((temp2 >> 6) & 0x01) |
753 ((temp2 >> 4) & 0x02) |
754 ((temp2 >> 2) & 0x04) |
755 ((temp2) & 0x08) |
756 ((temp2 << 2) & 0x10) |
757 ((temp2 << 4) & 0x20) | ((temp2 << 6) & 0x40);
758 s[i * 8 + j] = temp2;
759 s[i * 8 + 7 - j] = temp;
761 if (VERBOSECRYPTO(handle))
763 printf ("reverse(56bitstring)\n");
764 printf ("\t ;; 56bitstring after reverse\n");
765 bin7print (&s[i * 8], 8);
766 puts ("");
770 odd = !odd;
772 if (VERBOSECRYPTO(handle))
774 printf ("odd = ! odd\n");
775 printf ("tempstring = tempstring XOR 56bitstring;\n");
778 /* tempkey = tempkey XOR 8byteblock; */
779 for (j = 0; j < 8; j++)
780 tempkey[j] ^= s[i * 8 + j];
782 if (VERBOSECRYPTO(handle))
784 printf ("\t ;; tempstring\n");
785 bin7print (tempkey, 8);
786 puts ("");
787 puts ("");
791 for (j = 0; j < 8; j++)
792 tempkey[j] = tempkey[j] << 1;
794 if (VERBOSECRYPTO(handle))
796 printf ("for (8byteblock in s) {\n");
797 printf ("}\n");
798 printf ("\t ;; for loop terminated\n");
799 printf ("\t ;; tempstring as 64bitblock\n");
800 hexprint (tempkey, 8);
801 puts ("");
802 binprint (tempkey, 8);
803 puts ("");
804 printf ("/* add parity as low bit of each byte */\n");
805 printf ("tempkey = key_correction(add_parity_bits(tempstring));\n");
808 res = des_key_correction (handle, tempkey);
809 if (res != SHISHI_OK)
810 return res;
812 if (VERBOSECRYPTO(handle))
814 printf ("\t ;; tempkey\n");
815 escapeprint (tempkey, 8);
816 hexprint (tempkey, 8);
817 puts ("");
818 binprint (tempkey, 8);
819 puts ("");
820 puts ("");
821 printf ("key = key_correction(DES-CBC-check(s,tempkey));\n");
824 memcpy (s, string, stringlen);
825 if (saltlen > 0)
826 memcpy (s + stringlen, salt, saltlen);
827 memset (s + stringlen + saltlen, 0, n_s - stringlen - saltlen);
829 res = des_cbc_check (tempkey, s, n_s);
830 if (res != SHISHI_OK)
831 return res;
833 res = des_key_correction (handle, tempkey);
834 if (res != SHISHI_OK)
835 return res;
837 if (VERBOSECRYPTO(handle))
839 printf ("\t ;; key\n");
840 escapeprint (tempkey, 8);
841 hexprint (tempkey, 8);
842 puts ("");
843 binprint (tempkey, 8);
844 puts ("");
845 puts ("");
848 shishi_key_value_set (outkey, tempkey);
850 return SHISHI_OK;
853 static int
854 checksum_md4 (Shishi * handle,
855 char *out, int *outlen, char *in, int inlen)
857 int res;
858 char buffer[BUFSIZ];
859 GCRY_MD_HD hd;
860 char *p;
862 if (inlen + 8 > BUFSIZ)
864 shishi_error_printf (handle, "checksum inbuffer too large");
865 return !SHISHI_OK;
868 memcpy (buffer + 8, in, inlen);
870 #if 0
871 printf ("cksum in len=%d:", inlen);
872 for (i = 0; i < inlen; i++)
873 printf ("%02x ", in[i] & 0xFF);
874 printf ("\n");
875 #endif
877 res = shishi_randomize (handle, buffer, 8);
878 if (res != SHISHI_OK)
879 return res;
881 #if 0
882 printf ("cksum random: ");
883 for (i = 0; i < 8; i++)
884 printf ("%02X ", buffer[i] & 0xFF);
885 printf ("\n");
886 #endif
888 hd = gcry_md_open (GCRY_MD_MD4, 0);
889 if (!hd)
890 return SHISHI_GCRYPT_ERROR;
892 gcry_md_write (hd, buffer, inlen + 8);
893 p = gcry_md_read (hd, GCRY_MD_MD4);
895 #if 0
896 printf ("cksum md4: ");
897 for (i = 0; i < 16; i++)
898 printf ("%02X ", p[i] & 0xFF);
899 printf ("\n");
900 #endif
902 memcpy (buffer + 8, p, 16);
903 gcry_md_close (hd);
905 memcpy (out, buffer, 8 + 16);
907 *outlen = 8 + 16;
909 #if 0
910 printf ("cksum out: ");
911 for (i = 0; i < *outlen; i++)
912 printf ("%02X ", out[i] & 0xFF);
913 printf ("\n");
914 #endif
916 return SHISHI_OK;
919 static int
920 checksum_md5 (Shishi * handle,
921 char *out, int *outlen, char *in, int inlen)
923 int res;
924 char buffer[BUFSIZ];
925 GCRY_MD_HD hd;
926 char *p;
928 if (inlen + 8 > BUFSIZ)
930 shishi_error_printf (handle, "checksum inbuffer too large");
931 return !SHISHI_OK;
934 memcpy (buffer + 8, in, inlen);
936 #if 0
937 printf ("cksum in len=%d:", inlen);
938 for (i = 0; i < inlen; i++)
939 printf ("%02x ", in[i] & 0xFF);
940 printf ("\n");
941 #endif
943 res = shishi_randomize (handle, buffer, 8);
944 if (res != SHISHI_OK)
945 return res;
947 #if 0
948 printf ("cksum random: ");
949 for (i = 0; i < 8; i++)
950 printf ("%02X ", buffer[i] & 0xFF);
951 printf ("\n");
952 #endif
954 hd = gcry_md_open (GCRY_MD_MD5, 0);
955 if (!hd)
956 return SHISHI_GCRYPT_ERROR;
958 gcry_md_write (hd, buffer, inlen + 8);
959 p = gcry_md_read (hd, GCRY_MD_MD5);
961 #if 0
962 printf ("cksum md5: ");
963 for (i = 0; i < 16; i++)
964 printf ("%02X ", p[i] & 0xFF);
965 printf ("\n");
966 #endif
968 memcpy (buffer + 8, p, 16);
969 gcry_md_close (hd);
971 memcpy (out, buffer, 8 + 16);
973 *outlen = 8 + 16;
975 #if 0
976 printf ("cksum out: ");
977 for (i = 0; i < *outlen; i++)
978 printf ("%02X ", out[i] & 0xFF);
979 printf ("\n");
980 #endif
982 return SHISHI_OK;