1 /* crypto.c 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
22 /* XXX several functions with out/outlen writes longer than the outlen */
37 escapeprint (const char *str
, int len
)
42 for (i
= 0; i
< len
; i
++)
43 if ((str
[i
] >= 'A' && str
[i
] <= 'Z') ||
44 (str
[i
] >= 'a' && str
[i
] <= 'z') ||
45 (str
[i
] >= '0' && str
[i
] <= '9') || str
[i
] == '.')
46 printf ("%c", str
[i
] & 0xFF);
48 printf ("\\x%02x", str
[i
] & 0xFF);
49 printf ("' (length %d bytes)\n", len
);
53 hexprint (const char *str
, int len
)
58 for (i
= 0; i
< len
; i
++)
60 printf ("%02x ", str
[i
] & 0xFF);
63 if ((i
+ 1) % 16 == 0 && i
+ 1 < len
)
69 binprint (const char *str
, int len
)
74 for (i
= 0; i
< len
; i
++)
76 printf ("%d%d%d%d%d%d%d%d ",
77 str
[i
] & 0x80 ? 1 : 0,
78 str
[i
] & 0x40 ? 1 : 0,
79 str
[i
] & 0x20 ? 1 : 0,
80 str
[i
] & 0x10 ? 1 : 0,
81 str
[i
] & 0x08 ? 1 : 0,
82 str
[i
] & 0x04 ? 1 : 0,
83 str
[i
] & 0x02 ? 1 : 0, str
[i
] & 0x01 ? 1 : 0);
86 if ((i
+ 1) % 6 == 0 && i
+ 1 < len
)
92 bin7print (const char *str
, int len
)
97 for (i
= 0; i
< len
; i
++)
99 printf ("%d%d%d%d%d%d%d ",
100 str
[i
] & 0x40 ? 1 : 0,
101 str
[i
] & 0x20 ? 1 : 0,
102 str
[i
] & 0x10 ? 1 : 0,
103 str
[i
] & 0x08 ? 1 : 0,
104 str
[i
] & 0x04 ? 1 : 0,
105 str
[i
] & 0x02 ? 1 : 0, str
[i
] & 0x01 ? 1 : 0);
106 if ((i
+ 1) % 3 == 0)
108 if ((i
+ 1) % 6 == 0 && i
+ 1 < len
)
119 return gcd (b
, a
% b
);
125 return a
* b
/ gcd (a
, b
);
129 rot13 (Shishi
* handle
, char *in
, char *out
, int len
)
131 if (VERBOSECRYPTO (handle
))
133 printf ("\t ;; rot 13 in:\n");
134 escapeprint (in
, len
);
144 ((in
[0] >> 5) & 0x01) |
145 ((in
[0] >> 5) & 0x02) |
146 ((in
[0] >> 5) & 0x04) |
147 ((in
[0] << 3) & 0x08) |
148 ((in
[0] << 3) & 0x10) |
149 ((in
[0] << 3) & 0x20) | ((in
[0] << 3) & 0x40) | ((in
[0] << 3) & 0x80);
153 char nexttolast
, last
;
156 nexttolast
= in
[len
- 2];
159 for (i
= len
* 8 - 1; i
>= 13; i
--)
162 char mask
= ~(1 << (7 - i
% 8));
163 int pos2
= (i
- 13) / 8;
164 char mask2
= (1 << (7 - (i
- 13) % 8));
166 out
[pos
] = (out
[pos
] & mask
) |
167 (((in
[pos2
] & mask2
) ? 0xFF : 0x00) & ~mask
);
169 out
[0] = ((nexttolast
& 0xFF) << 3) | ((last
& 0xFF) >> 5);
170 out
[1] = (in
[1] & ~(0xFF & (0xFF << 3))) | (0xFF & (last
<< 3));
173 if (VERBOSECRYPTO (handle
))
175 printf ("\t ;; rot13 out:\n");
176 escapeprint (out
, len
);
187 ocadd (char *add1
, char *add2
, char *sum
, int len
)
192 for (i
= len
- 1; i
>= 0; i
--)
194 int tmpsum
= (unsigned char) add1
[i
] + (unsigned char) add2
[i
];
196 sum
[i
] = (tmpsum
+ carry
) & 0xFF;
197 if (tmpsum
+ carry
> 0xFF)
206 for (i
= len
- 1; i
>= 0; i
--)
207 if ((unsigned char) sum
[i
] != 0xFF)
215 memset (sum
, 0, len
);
222 simplified_hmac (Shishi
* handle
,
224 const char *in
, size_t inlen
,
225 char **outhash
, size_t * outhashlen
)
229 int halg
= GCRY_MD_SHA1
;
230 size_t hlen
= gcry_md_get_algo_dlen (halg
);
234 err
= gcry_md_open (&mdh
, halg
, GCRY_MD_FLAG_HMAC
);
235 if (err
!= GPG_ERR_NO_ERROR
)
237 shishi_error_printf (handle
, "Libgcrypt md open failed");
238 shishi_error_set (handle
, gpg_strerror (err
));
239 return SHISHI_CRYPTO_INTERNAL_ERROR
;
242 err
= gcry_md_setkey (mdh
, shishi_key_value (key
), shishi_key_length (key
));
243 if (err
!= GPG_ERR_NO_ERROR
)
245 shishi_error_printf (handle
, "Libgcrypt md setkey failed");
246 shishi_error_set (handle
, gpg_strerror (err
));
247 return SHISHI_CRYPTO_INTERNAL_ERROR
;
250 gcry_md_write (mdh
, in
, inlen
);
252 hash
= gcry_md_read (mdh
, halg
);
255 shishi_error_printf (handle
, "Libgcrypt failed to compute hash");
256 return SHISHI_CRYPTO_INTERNAL_ERROR
;
260 *outhash
= xmemdup (*outhash
, hash
, *outhashlen
);
264 struct hmac_sha1_ctx ctx
;
265 hmac_sha1_set_key (&ctx
, shishi_key_length (key
), shishi_key_value (key
));
266 hmac_sha1_update (&ctx
, inlen
, in
);
267 *outhashlen
= SHA1_DIGEST_SIZE
;
268 *outhash
= xmalloc (*outhashlen
);
269 hmac_sha1_digest (&ctx
, *outhashlen
, *outhash
);
275 simplified_hmac_verify (Shishi
* handle
,
277 const char *in
, size_t inlen
,
278 const char *hmac
, size_t hmaclen
)
285 res
= simplified_hmac (handle
, key
, in
, inlen
, &hash
, &hlen
);
286 if (res
!= SHISHI_OK
|| hash
== NULL
)
289 same
= (hlen
== hmaclen
) && memcmp (hash
, hmac
, hmaclen
) == 0;
295 shishi_error_printf (handle
, "HMAC verify failed");
296 return SHISHI_CRYPTO_ERROR
;
304 SHISHI_DERIVEKEYMODE_CHECKSUM
,
305 SHISHI_DERIVEKEYMODE_PRIVACY
,
306 SHISHI_DERIVEKEYMODE_INTEGRITY
308 Shishi_derivekeymode
;
311 simplified_derivekey (Shishi
* handle
,
313 int keyusage
, int derivekeymode
, Shishi_key
** outkey
)
317 Shishi_key
*derivedkey
;
319 if (VERBOSECRYPTO (handle
))
321 printf ("simplified_derivekey\n");
322 printf ("\t ;; mode %d (%s)\n", derivekeymode
,
323 derivekeymode
== SHISHI_DERIVEKEYMODE_CHECKSUM
? "checksum" :
324 derivekeymode
== SHISHI_DERIVEKEYMODE_INTEGRITY
? "integrity" :
325 derivekeymode
== SHISHI_DERIVEKEYMODE_PRIVACY
? "privacy" :
327 hexprint (shishi_key_value (key
), shishi_key_length (key
));
332 res
= shishi_key_from_value (handle
, shishi_key_type (key
),
334 if (res
!= SHISHI_OK
)
337 *outkey
= derivedkey
;
341 uint32_t tmp
= htonl (keyusage
);
342 memcpy (constant
, &tmp
, 4);
343 if (derivekeymode
== SHISHI_DERIVEKEYMODE_CHECKSUM
)
344 constant
[4] = '\x99';
345 else if (derivekeymode
== SHISHI_DERIVEKEYMODE_INTEGRITY
)
346 constant
[4] = '\x55';
347 else /* if (derivekeymode == SHISHI_DERIVEKEYMODE_PRIVACY) */
348 constant
[4] = '\xAA';
350 res
= shishi_dk (handle
, key
, constant
, 5, derivedkey
);
354 shishi_key_copy (derivedkey
, key
);
357 if (VERBOSECRYPTO (handle
))
359 printf ("\t ;; simplified_derivekey out (%d):\n",
360 shishi_key_length (derivedkey
));
361 hexprint (shishi_key_value (derivedkey
),
362 shishi_key_length (derivedkey
));
370 simplified_dencrypt (Shishi
* handle
,
372 const char *iv
, size_t ivlen
,
373 char **ivout
, size_t * ivoutlen
,
374 const char *in
, size_t inlen
,
375 char **out
, size_t * outlen
, int decryptp
)
381 int mode
= GCRY_CIPHER_MODE_CBC
;
384 switch (shishi_key_type (key
))
386 case SHISHI_DES_CBC_CRC
:
387 case SHISHI_DES_CBC_MD4
:
388 case SHISHI_DES_CBC_MD5
:
389 alg
= GCRY_CIPHER_DES
;
393 case SHISHI_DES3_CBC_HMAC_SHA1_KD
:
394 alg
= GCRY_CIPHER_3DES
;
398 case SHISHI_AES128_CTS_HMAC_SHA1_96
:
399 case SHISHI_AES256_CTS_HMAC_SHA1_96
:
400 alg
= GCRY_CIPHER_AES
;
401 flags
|= GCRY_CIPHER_CBC_CTS
;
406 err
= gcry_cipher_open (&ch
, alg
, mode
, flags
);
407 if (err
!= GPG_ERR_NO_ERROR
)
409 shishi_error_printf (handle
, "Libgcrypt cipher open failed");
410 shishi_error_set (handle
, gpg_strerror (err
));
411 return SHISHI_CRYPTO_INTERNAL_ERROR
;
414 err
= gcry_cipher_setkey (ch
, shishi_key_value (key
),
415 shishi_key_length (key
));
416 if (err
!= GPG_ERR_NO_ERROR
)
418 shishi_error_printf (handle
, "Libgcrypt setkey failed");
419 shishi_error_set (handle
, gpg_strerror (err
));
420 return SHISHI_CRYPTO_INTERNAL_ERROR
;
423 err
= gcry_cipher_setiv (ch
, iv
, ivlen
);
424 if (err
!= GPG_ERR_NO_ERROR
)
426 shishi_error_printf (handle
, "Libgcrypt setiv failed");
427 shishi_error_set (handle
, gpg_strerror (err
));
428 return SHISHI_CRYPTO_INTERNAL_ERROR
;
431 *out
= xmalloc (*outlen
);
434 err
= gcry_cipher_decrypt (ch
, (unsigned char *) *out
, *outlen
,
435 (const unsigned char *) in
, inlen
);
437 err
= gcry_cipher_encrypt (ch
, (unsigned char *) *out
, *outlen
,
438 (const unsigned char *) in
, inlen
);
439 if (err
!= GPG_ERR_NO_ERROR
)
441 shishi_error_set (handle
, gpg_strerror (err
));
442 return SHISHI_CRYPTO_INTERNAL_ERROR
;
445 if (ivout
&& ivoutlen
)
447 *ivoutlen
= gcry_cipher_get_algo_blklen (alg
);
448 *ivout
= xmalloc (*ivoutlen
);
450 memcpy (*ivout
, in
+ inlen
- *ivoutlen
, *ivoutlen
);
452 /* XXX what is the output iv for CBC-CTS mode?
453 but is this value useful at all for that mode anyway?
454 Mostly it is DES apps that want the updated iv, so this is ok. */
455 memcpy (*ivout
, *out
+ *outlen
- *ivoutlen
, *ivoutlen
);
458 gcry_cipher_close (ch
);
460 struct CBC_CTX (struct des_ctx
, DES_BLOCK_SIZE
) des
;
461 struct CBC_CTX (struct des3_ctx
, DES3_BLOCK_SIZE
) des3
;
462 struct CBC_CTS_CTX (struct aes_ctx
, AES_BLOCK_SIZE
) aes
;
466 *out
= xmalloc (*outlen
);
468 switch (shishi_key_type (key
))
470 case SHISHI_DES_CBC_CRC
:
471 case SHISHI_DES_CBC_MD4
:
472 case SHISHI_DES_CBC_MD5
:
473 rc
= des_set_key (&des
.ctx
, shishi_key_value (key
));
476 shishi_error_printf (handle
, "Nettle setkey failed");
477 return SHISHI_CRYPTO_INTERNAL_ERROR
;
479 memset (des
.iv
, 0, sizeof (des
.iv
));
480 /* XXX Use CBC_SET_IV (&des, iv), but how with ivlen? */
481 memcpy (des
.iv
, iv
, ivlen
< sizeof (des
.iv
) ? ivlen
: sizeof (des
.iv
));
483 CBC_DECRYPT (&des
, des_decrypt
, inlen
, *out
, in
);
485 CBC_ENCRYPT (&des
, des_encrypt
, inlen
, *out
, in
);
486 if (ivout
&& ivoutlen
)
488 *ivoutlen
= sizeof (des
.iv
);
490 *ivout
= xmemdup (*ivout
, des
.iv
, *ivoutlen
);
494 case SHISHI_DES3_CBC_HMAC_SHA1_KD
:
495 rc
= des3_set_key (&des3
.ctx
, shishi_key_value (key
));
498 shishi_error_printf (handle
, "Nettle setkey failed");
499 return SHISHI_CRYPTO_INTERNAL_ERROR
;
501 memset (des3
.iv
, 0, sizeof (des3
.iv
));
502 /* XXX Use CBC_SET_IV (&des, iv), but how with ivlen? */
504 ivlen
< sizeof (des3
.iv
) ? ivlen
: sizeof (des3
.iv
));
506 CBC_DECRYPT (&des3
, des3_decrypt
, inlen
, *out
, in
);
508 CBC_ENCRYPT (&des3
, des3_encrypt
, inlen
, *out
, in
);
509 if (ivout
&& ivoutlen
)
511 *ivoutlen
= sizeof (des3
.iv
);
513 *ivout
= xmemdup (*ivout
, des3
.iv
, *ivoutlen
);
517 case SHISHI_AES128_CTS_HMAC_SHA1_96
:
518 case SHISHI_AES256_CTS_HMAC_SHA1_96
:
519 memset (aes
.iv
, 0, sizeof (aes
.iv
));
520 /* XXX Use CBC_SET_IV (&des, iv), but how with ivlen? */
521 memcpy (aes
.iv
, iv
, ivlen
< sizeof (aes
.iv
) ? ivlen
: sizeof (aes
.iv
));
524 aes_set_decrypt_key (&aes
.ctx
, shishi_key_length (key
),
525 shishi_key_value (key
));
526 CBC_CTS_DECRYPT (&aes
, aes_decrypt
, inlen
, *out
, in
);
530 aes_set_encrypt_key (&aes
.ctx
, shishi_key_length (key
),
531 shishi_key_value (key
));
532 CBC_CTS_ENCRYPT (&aes
, aes_encrypt
, inlen
, *out
, in
);
534 if (ivout
&& ivoutlen
)
536 *ivoutlen
= sizeof (aes
.iv
);
538 *ivout
= xmemdup (*ivout
, aes
.iv
, *ivoutlen
);
548 simplified_encrypt (Shishi
* handle
,
551 const char *iv
, size_t ivlen
,
552 char **ivout
, size_t * ivoutlen
,
553 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
558 if ((shishi_key_type (key
) == SHISHI_DES3_CBC_HMAC_SHA1_KD
||
559 shishi_key_type (key
) == SHISHI_DES_CBC_CRC
||
560 shishi_key_type (key
) == SHISHI_DES_CBC_MD4
||
561 shishi_key_type (key
) == SHISHI_DES_CBC_MD5
) && (inlen
% 8) != 0)
562 while (((inlen
+ padzerolen
) % 8) != 0)
567 char *pt
= NULL
, *ct
= NULL
, *hmac
= NULL
;
568 int blen
= shishi_cipher_blocksize (shishi_key_type (key
));
569 size_t ctlen
, ptlen
, hmaclen
;
570 Shishi_key
*privacykey
= NULL
, *integritykey
= NULL
;
572 ptlen
= inlen
+ blen
+ padzerolen
;
573 pt
= xmalloc (ptlen
);
575 res
= shishi_randomize (handle
, pt
, blen
);
576 if (res
!= SHISHI_OK
)
579 memcpy (pt
+ blen
, in
, inlen
);
580 memset (pt
+ blen
+ inlen
, 0, padzerolen
);
582 res
= simplified_derivekey (handle
, key
, keyusage
,
583 SHISHI_DERIVEKEYMODE_PRIVACY
, &privacykey
);
584 if (res
!= SHISHI_OK
)
588 simplified_dencrypt (handle
, privacykey
, iv
, ivlen
, ivout
, ivoutlen
,
589 pt
, ptlen
, &ct
, &ctlen
, 0);
590 if (res
!= SHISHI_OK
)
594 res
= simplified_derivekey (handle
, key
, keyusage
,
595 SHISHI_DERIVEKEYMODE_INTEGRITY
,
597 if (res
!= SHISHI_OK
)
601 simplified_hmac (handle
, integritykey
, pt
, ptlen
, &hmac
, &hmaclen
);
602 if (res
!= SHISHI_OK
)
605 *outlen
= ctlen
+ hmaclen
;
606 *out
= xmalloc (*outlen
);
607 memcpy (*out
, ct
, ctlen
);
608 memcpy (*out
+ ctlen
, hmac
, hmaclen
);
612 shishi_key_done (privacykey
);
614 shishi_key_done (integritykey
);
624 res
= simplified_dencrypt (handle
, key
, iv
, ivlen
, ivout
, ivoutlen
,
625 in
, inlen
, out
, outlen
, 0);
632 simplified_decrypt (Shishi
* handle
,
635 const char *iv
, size_t ivlen
,
636 char **ivout
, size_t * ivoutlen
,
637 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
643 Shishi_key
*privacykey
= NULL
, *integritykey
= NULL
;
644 int blen
= shishi_cipher_blocksize (shishi_key_type (key
));
645 size_t hlen
= 20; /* XXX only works for SHA-1 */
647 res
= simplified_derivekey (handle
, key
, keyusage
,
648 SHISHI_DERIVEKEYMODE_PRIVACY
, &privacykey
);
649 if (res
!= SHISHI_OK
)
653 simplified_dencrypt (handle
, privacykey
, iv
, ivlen
, ivout
, ivoutlen
,
654 in
, inlen
- hlen
, out
, outlen
, 1);
655 if (res
!= SHISHI_OK
)
658 res
= simplified_derivekey (handle
, key
, keyusage
,
659 SHISHI_DERIVEKEYMODE_INTEGRITY
,
661 if (res
!= SHISHI_OK
)
664 res
= simplified_hmac_verify (handle
, integritykey
, *out
, *outlen
,
665 in
+ inlen
- hlen
, hlen
);
667 if (res
!= SHISHI_OK
)
670 memmove (*out
, *out
+ blen
, *outlen
- blen
);
671 *outlen
= *outlen
- blen
;
672 *out
= xrealloc (*out
, *outlen
);
676 shishi_key_done (privacykey
);
678 shishi_key_done (integritykey
);
682 res
= simplified_dencrypt (handle
, key
, iv
, ivlen
, ivout
, ivoutlen
,
683 in
, inlen
, out
, outlen
, 1);
690 simplified_checksum (Shishi
* handle
,
694 const char *in
, size_t inlen
,
695 char **out
, size_t * outlen
)
697 Shishi_key
*checksumkey
;
698 int cksumlen
= shishi_checksum_cksumlen (cksumtype
);
701 res
= simplified_derivekey (handle
, key
, keyusage
,
702 SHISHI_DERIVEKEYMODE_CHECKSUM
, &checksumkey
);
703 if (res
!= SHISHI_OK
)
706 res
= simplified_hmac (handle
, checksumkey
, in
, inlen
, out
, outlen
);
708 shishi_key_done (checksumkey
);
710 if (res
!= SHISHI_OK
)
719 _shishi_cipher_init (void)
722 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P
) == 0)
724 if (gcry_check_version (GCRYPT_VERSION
) == NULL
)
725 return SHISHI_CRYPTO_INTERNAL_ERROR
;
726 if (gcry_control (GCRYCTL_DISABLE_SECMEM
, NULL
, 0) != GPG_ERR_NO_ERROR
)
727 return SHISHI_CRYPTO_INTERNAL_ERROR
;
728 if (gcry_control (GCRYCTL_INITIALIZATION_FINISHED
,
729 NULL
, 0) != GPG_ERR_NO_ERROR
)
730 return SHISHI_CRYPTO_INTERNAL_ERROR
;
737 typedef int (*Shishi_random_to_key_function
) (Shishi
* handle
,
740 Shishi_key
* outkey
);
742 typedef int (*Shishi_string_to_key_function
) (Shishi
* handle
,
743 const char *password
,
747 const char *parameter
,
748 Shishi_key
* outkey
);
750 typedef int (*Shishi_encrypt_function
) (Shishi
* handle
,
753 const char *iv
, size_t ivlen
,
754 char **ivout
, size_t * ivoutlen
,
755 const char *in
, size_t inlen
,
756 char **out
, size_t * outlen
);
758 typedef int (*Shishi_decrypt_function
) (Shishi
* handle
,
761 const char *iv
, size_t ivlen
,
762 char **ivout
, size_t * ivoutlen
,
763 const char *in
, size_t inlen
,
764 char **out
, size_t * outlen
);
766 typedef int (*Shishi_checksum_function
) (Shishi
* handle
,
770 const char *in
, size_t inlen
,
771 char **out
, size_t * outlen
);
773 typedef int (*Shishi_verify_function
) (Shishi
* handle
,
777 const char *in
, size_t inlen
,
778 const char *cksum
, size_t cksumlen
);
780 #include "crypto-null.c"
781 #include "crypto-md.c"
782 #include "crypto-des.c"
783 #include "crypto-3des.c"
784 #include "crypto-aes.c"
785 #include "crypto-rc4.c"
796 int defaultcksumtype
;
797 Shishi_random_to_key_function random2key
;
798 Shishi_string_to_key_function string2key
;
799 Shishi_encrypt_function encrypt
;
800 Shishi_decrypt_function decrypt
;
802 typedef struct cipherinfo cipherinfo
;
804 static cipherinfo null_info
= {
819 static cipherinfo des_cbc_crc_info
= {
834 static cipherinfo des_cbc_md4_info
= {
849 static cipherinfo des_cbc_md5_info
= {
864 static cipherinfo des_cbc_none_info
= {
879 static cipherinfo des3_cbc_none_info
= {
880 SHISHI_DES3_CBC_NONE
,
887 SHISHI_HMAC_SHA1_DES3_KD
,
894 static cipherinfo des3_cbc_sha1_kd_info
= {
895 SHISHI_DES3_CBC_HMAC_SHA1_KD
,
902 SHISHI_HMAC_SHA1_DES3_KD
,
909 static cipherinfo aes128_cts_hmac_sha1_96_info
= {
910 SHISHI_AES128_CTS_HMAC_SHA1_96
,
911 "aes128-cts-hmac-sha1-96",
917 SHISHI_HMAC_SHA1_96_AES128
,
918 aes128_random_to_key
,
919 aes128_string_to_key
,
924 static cipherinfo aes256_cts_hmac_sha1_96_info
= {
925 SHISHI_AES256_CTS_HMAC_SHA1_96
,
926 "aes256-cts-hmac-sha1-96",
932 SHISHI_HMAC_SHA1_96_AES256
,
933 aes256_random_to_key
,
934 aes256_string_to_key
,
939 static cipherinfo rc4_hmac_info
= {
948 rc4_hmac_random_to_key
,
949 rc4_hmac_string_to_key
,
954 static cipherinfo rc4_hmac_exp_info
= {
963 rc4_hmac_random_to_key
,
964 rc4_hmac_string_to_key
,
965 rc4_hmac_exp_encrypt
,
969 static cipherinfo
*ciphers
[] = {
976 &des3_cbc_sha1_kd_info
,
977 &aes128_cts_hmac_sha1_96_info
,
978 &aes256_cts_hmac_sha1_96_info
,
984 * shishi_cipher_supported_p:
985 * @type: encryption type, see Shishi_etype.
987 * Return value: Return 0 iff cipher is unsupported.
990 shishi_cipher_supported_p (int32_t type
)
994 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
995 if (type
== ciphers
[i
]->type
)
1002 * shishi_cipher_name:
1003 * @type: encryption type, see Shishi_etype.
1005 * Return value: Return name of encryption type,
1006 * e.g. "des3-cbc-sha1-kd", as defined in the standards.
1009 shishi_cipher_name (int32_t type
)
1014 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1016 if (type
== ciphers
[i
]->type
)
1017 return ciphers
[i
]->name
;
1020 asprintf (&p
, "unknown cipher %d", type
);
1025 * shishi_cipher_blocksize:
1026 * @type: encryption type, see Shishi_etype.
1028 * Return value: Return block size for encryption type, as defined in
1032 shishi_cipher_blocksize (int32_t type
)
1036 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1037 if (type
== ciphers
[i
]->type
)
1038 return ciphers
[i
]->blocksize
;
1044 * shishi_cipher_minpadsize:
1045 * @type: encryption type, see Shishi_etype.
1047 * Return value: Return the minimum pad size for encryption type, as
1048 * defined in the standards.
1051 shishi_cipher_minpadsize (int32_t type
)
1055 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1056 if (type
== ciphers
[i
]->type
)
1057 return ciphers
[i
]->minpadsize
;
1063 * shishi_cipher_confoundersize:
1064 * @type: encryption type, see Shishi_etype.
1066 * Return value: Returns the size of the confounder (random data) for
1067 * encryption type, as defined in the standards.
1070 shishi_cipher_confoundersize (int32_t type
)
1074 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1075 if (type
== ciphers
[i
]->type
)
1076 return ciphers
[i
]->confoundersize
;
1082 * shishi_cipher_keylen:
1083 * @type: encryption type, see Shishi_etype.
1085 * Return value: Return length of key used for the encryption type, as
1086 * defined in the standards.
1089 shishi_cipher_keylen (int32_t type
)
1093 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1094 if (type
== ciphers
[i
]->type
)
1095 return ciphers
[i
]->keylen
;
1101 * shishi_cipher_randomlen:
1102 * @type: encryption type, see Shishi_etype.
1104 * Return value: Return length of random used for the encryption type,
1105 * as defined in the standards.
1108 shishi_cipher_randomlen (int32_t type
)
1112 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1113 if (type
== ciphers
[i
]->type
)
1114 return ciphers
[i
]->randomlen
;
1120 * shishi_cipher_defaultcksumtype:
1121 * @type: encryption type, see Shishi_etype.
1123 * Return value: Return associated checksum mechanism for the
1124 * encryption type, as defined in the standards.
1127 shishi_cipher_defaultcksumtype (int32_t type
)
1131 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1132 if (type
== ciphers
[i
]->type
)
1133 return ciphers
[i
]->defaultcksumtype
;
1139 * shishi_cipher_parse:
1140 * @cipher: name of encryption type, e.g. "des3-cbc-sha1-kd".
1142 * Return value: Return encryption type corresponding to a string.
1145 shishi_cipher_parse (const char *cipher
)
1150 i
= strtol (cipher
, &endptr
, 0);
1152 if (endptr
!= cipher
)
1155 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1156 if (strcasecmp (cipher
, ciphers
[i
]->name
) == 0)
1157 return ciphers
[i
]->type
;
1162 static Shishi_random_to_key_function
1163 _shishi_cipher_random_to_key (int32_t type
)
1167 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1168 if (type
== ciphers
[i
]->type
)
1169 return ciphers
[i
]->random2key
;
1174 static Shishi_string_to_key_function
1175 _shishi_cipher_string_to_key (int32_t type
)
1179 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1180 if (type
== ciphers
[i
]->type
)
1181 return ciphers
[i
]->string2key
;
1186 static Shishi_encrypt_function
1187 _shishi_cipher_encrypt (int32_t type
)
1191 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1192 if (type
== ciphers
[i
]->type
)
1193 return ciphers
[i
]->encrypt
;
1198 static Shishi_decrypt_function
1199 _shishi_cipher_decrypt (int32_t type
)
1203 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1204 if (type
== ciphers
[i
]->type
)
1205 return ciphers
[i
]->decrypt
;
1215 Shishi_checksum_function checksum
;
1216 Shishi_verify_function verify
;
1218 typedef struct checksuminfo checksuminfo
;
1220 static checksuminfo md4_info
= {
1227 static checksuminfo md4_des_info
= {
1234 static checksuminfo md5_info
= {
1241 static checksuminfo md5_des_info
= {
1249 static checksuminfo md5_gss_info
= {
1250 SHISHI_RSA_MD5_DES_GSS
,
1256 static checksuminfo hmac_sha1_des3_kd_info
= {
1257 SHISHI_HMAC_SHA1_DES3_KD
,
1258 "hmac-sha1-des3-kd",
1263 static checksuminfo hmac_sha1_96_aes128_info
= {
1264 SHISHI_HMAC_SHA1_96_AES128
,
1265 "hmac-sha1-96-aes128",
1270 static checksuminfo hmac_sha1_96_aes256_info
= {
1271 SHISHI_HMAC_SHA1_96_AES256
,
1272 "hmac-sha1-96-aes256",
1277 static checksuminfo rc4_hmac_md5_info
= {
1278 SHISHI_RC4_HMAC_MD5
,
1281 rc4_hmac_md5_checksum
1284 static checksuminfo
*checksums
[] = {
1290 &hmac_sha1_des3_kd_info
,
1291 &hmac_sha1_96_aes128_info
,
1292 &hmac_sha1_96_aes256_info
,
1297 * shishi_checksum_supported_p:
1298 * @type: checksum type, see Shishi_cksumtype.
1300 * Return value: Return 0 iff checksum is unsupported.
1303 shishi_checksum_supported_p (int32_t type
)
1307 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1308 if (type
== checksums
[i
]->type
)
1315 * shishi_checksum_name:
1316 * @type: checksum type, see Shishi_cksumtype.
1318 * Return value: Return name of checksum type,
1319 * e.g. "hmac-sha1-96-aes256", as defined in the standards.
1322 shishi_checksum_name (int32_t type
)
1327 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1329 if (type
== checksums
[i
]->type
)
1330 return checksums
[i
]->name
;
1333 asprintf (&p
, "unknown checksum %d", type
);
1338 * shishi_checksum_cksumlen:
1339 * @type: checksum type, see Shishi_cksumtype.
1341 * Return value: Return length of checksum used for the checksum type,
1342 * as defined in the standards.
1345 shishi_checksum_cksumlen (int32_t type
)
1349 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1350 if (type
== checksums
[i
]->type
)
1351 return checksums
[i
]->cksumlen
;
1357 * shishi_checksum_parse:
1358 * @checksum: name of checksum type, e.g. "hmac-sha1-96-aes256".
1360 * Return value: Return checksum type, see Shishi_cksumtype,
1361 * corresponding to a string.
1364 shishi_checksum_parse (const char *checksum
)
1369 i
= strtol (checksum
, &endptr
, 0);
1371 if (endptr
!= checksum
)
1374 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1375 if (strcasecmp (checksum
, checksums
[i
]->name
) == 0)
1376 return checksums
[i
]->type
;
1381 static Shishi_checksum_function
1382 _shishi_checksum (int32_t type
)
1386 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1387 if (type
== checksums
[i
]->type
)
1388 return checksums
[i
]->checksum
;
1393 static Shishi_verify_function
1394 _shishi_verify (int32_t type
)
1398 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1399 if (type
== checksums
[i
]->type
)
1400 return checksums
[i
]->verify
;
1406 * shishi_string_to_key:
1407 * @handle: shishi handle as allocated by shishi_init().
1408 * @keytype: cryptographic encryption type, see Shishi_etype.
1409 * @password: input array with password.
1410 * @passwordlen: length of input array with password.
1411 * @salt: input array with salt.
1412 * @saltlen: length of input array with salt.
1413 * @parameter: input array with opaque encryption type specific information.
1414 * @outkey: allocated key handle that will contain new key.
1416 * Derive key from a string (password) and salt (commonly
1417 * concatenation of realm and principal) for specified key type, and
1418 * set the type and value in the given key to the computed values.
1419 * The parameter value is specific for each keytype, and can be set if
1420 * the parameter information is not available.
1422 * Return value: Returns %SHISHI_OK iff successful.
1425 shishi_string_to_key (Shishi
* handle
,
1427 const char *password
, size_t passwordlen
,
1428 const char *salt
, size_t saltlen
,
1429 const char *parameter
, Shishi_key
* outkey
)
1431 Shishi_string_to_key_function string2key
;
1434 shishi_key_type_set (outkey
, keytype
);
1436 if (VERBOSECRYPTO (handle
))
1438 printf ("string_to_key (%s, password, salt)\n",
1439 shishi_key_name (outkey
));
1440 printf ("\t ;; password:\n");
1441 escapeprint (password
, passwordlen
);
1442 hexprint (password
, passwordlen
);
1444 printf ("\t ;; salt:\n");
1445 escapeprint (salt
, saltlen
);
1446 hexprint (salt
, saltlen
);
1450 string2key
= _shishi_cipher_string_to_key (shishi_key_type (outkey
));
1451 if (string2key
== NULL
)
1453 shishi_error_printf (handle
, "Unsupported keytype %d",
1454 shishi_key_type (outkey
));
1455 return SHISHI_CRYPTO_ERROR
;
1458 res
= (*string2key
) (handle
, password
, passwordlen
,
1459 salt
, saltlen
, parameter
, outkey
);
1461 if (VERBOSECRYPTO (handle
))
1463 printf ("\t ;; string_to_key key:\n");
1464 hexprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
1466 binprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
1474 * shishi_random_to_key:
1475 * @handle: shishi handle as allocated by shishi_init().
1476 * @keytype: cryptographic encryption type, see Shishi_etype.
1477 * @random: input array with random data.
1478 * @randomlen: length of input array with random data.
1479 * @outkey: allocated key handle that will contain new key.
1481 * Derive key from random data for specified key type, and set the
1482 * type and value in the given key to the computed values.
1484 * Return value: Returns %SHISHI_OK iff successful.
1487 shishi_random_to_key (Shishi
* handle
,
1489 char *random
, size_t randomlen
, Shishi_key
* outkey
)
1491 Shishi_random_to_key_function random2key
;
1494 shishi_key_type_set (outkey
, keytype
);
1496 if (VERBOSECRYPTO (handle
))
1498 printf ("random_to_key (%s, random)\n", shishi_key_name (outkey
));
1499 printf ("\t ;; random:\n");
1500 hexprint (random
, randomlen
);
1502 binprint (random
, randomlen
);
1506 random2key
= _shishi_cipher_random_to_key (keytype
);
1507 if (random2key
== NULL
)
1509 shishi_error_printf (handle
, "Unsupported random_to_key() ekeytype %d",
1511 return SHISHI_CRYPTO_ERROR
;
1514 res
= (*random2key
) (handle
, random
, randomlen
, outkey
);
1516 if (VERBOSECRYPTO (handle
))
1518 printf ("\t ;; random_to_key key:\n");
1519 hexprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
1521 binprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
1530 * @handle: shishi handle as allocated by shishi_init().
1531 * @key: key to compute checksum with.
1532 * @keyusage: integer specifying what this key is used for.
1533 * @cksumtype: the checksum algorithm to use.
1534 * @in: input array with data to integrity protect.
1535 * @inlen: size of input array with data to integrity protect.
1536 * @out: output array with newly allocated integrity protected data.
1537 * @outlen: output variable with length of output array with checksum.
1539 * Integrity protect data using key, possibly altered by supplied key
1540 * usage. If key usage is 0, no key derivation is used. The OUT
1541 * buffer must be deallocated by the caller.
1543 * Return value: Returns %SHISHI_OK iff successful.
1546 shishi_checksum (Shishi
* handle
,
1550 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
1552 Shishi_checksum_function checksum
;
1555 if (VERBOSECRYPTO (handle
))
1557 printf ("checksum (%s, %d, in, out)\n",
1558 shishi_key_name (key
), cksumtype
);
1559 printf ("\t ;; key (%d):\n", shishi_key_length (key
));
1560 hexprint (shishi_key_value (key
), shishi_key_length (key
));
1562 printf ("\t ;; in:\n");
1563 escapeprint (in
, inlen
);
1564 hexprint (in
, inlen
);
1569 cksumtype
= shishi_cipher_defaultcksumtype (shishi_key_type (key
));
1571 checksum
= _shishi_checksum (cksumtype
);
1572 if (checksum
== NULL
)
1574 shishi_error_printf (handle
, "Unsupported checksum type %d", cksumtype
);
1575 return SHISHI_CRYPTO_ERROR
;
1578 /* XXX? check if etype and cksumtype are compatible? */
1580 res
= (*checksum
) (handle
, key
, keyusage
, cksumtype
,
1581 in
, inlen
, out
, outlen
);
1583 if (VERBOSECRYPTO (handle
))
1585 printf ("\t ;; checksum out:\n");
1586 escapeprint (*out
, *outlen
);
1587 hexprint (*out
, *outlen
);
1596 * @handle: shishi handle as allocated by shishi_init().
1597 * @key: key to verify checksum with.
1598 * @keyusage: integer specifying what this key is used for.
1599 * @cksumtype: the checksum algorithm to use.
1600 * @in: input array with data that was integrity protected.
1601 * @inlen: size of input array with data that was integrity protected.
1602 * @cksum: input array with alleged checksum of data.
1603 * @cksumlen: size of input array with alleged checksum of data.
1605 * Verify checksum of data using key, possibly altered by supplied key
1606 * usage. If key usage is 0, no key derivation is used.
1608 * Return value: Returns %SHISHI_OK iff successful.
1611 shishi_verify (Shishi
* handle
,
1615 const char *in
, size_t inlen
,
1616 const char *cksum
, size_t cksumlen
)
1618 Shishi_verify_function verify
;
1621 if (VERBOSECRYPTO (handle
))
1623 printf ("verify (%s, %d, in, out)\n", shishi_key_name (key
), cksumtype
);
1624 printf ("\t ;; key (%d):\n", shishi_key_length (key
));
1625 hexprint (shishi_key_value (key
), shishi_key_length (key
));
1627 printf ("\t ;; data:\n");
1628 escapeprint (in
, inlen
);
1629 hexprint (in
, inlen
);
1631 printf ("\t ;; mic:\n");
1632 escapeprint (cksum
, cksumlen
);
1633 hexprint (cksum
, cksumlen
);
1638 cksumtype
= shishi_cipher_defaultcksumtype (shishi_key_type (key
));
1640 verify
= _shishi_verify (cksumtype
);
1643 shishi_error_printf (handle
, "Unsupported checksum type %d", cksumtype
);
1644 return SHISHI_CRYPTO_ERROR
;
1647 /* XXX? check if etype and cksumtype are compatible? */
1649 res
= (*verify
) (handle
, key
, keyusage
, cksumtype
,
1650 in
, inlen
, cksum
, cksumlen
);
1652 if (VERBOSECRYPTO (handle
))
1653 printf ("\t ;; verify return: %d\n", res
);
1659 * shishi_encrypt_ivupdate_etype:
1660 * @handle: shishi handle as allocated by shishi_init().
1661 * @key: key to encrypt with.
1662 * @keyusage: integer specifying what this key is encrypting.
1663 * @etype: integer specifying what cipher to use.
1664 * @iv: input array with initialization vector
1665 * @ivlen: size of input array with initialization vector.
1666 * @ivout: output array with newly allocated updated initialization vector.
1667 * @ivoutlen: size of output array with updated initialization vector.
1668 * @in: input array with data to encrypt.
1669 * @inlen: size of input array with data to encrypt.
1670 * @out: output array with newly allocated encrypted data.
1671 * @outlen: output variable with size of newly allocated output array.
1673 * Encrypts data as per encryption method using specified
1674 * initialization vector and key. The key actually used is derived
1675 * using the key usage. If key usage is 0, no key derivation is used.
1676 * The OUT buffer must be deallocated by the caller. If IVOUT or
1677 * IVOUTLEN is NULL, the updated IV is not saved anywhere.
1679 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
1680 * exactly, some Kerberos encryption types add pad to make the data
1681 * fit into the block size of the encryption algorithm. Furthermore,
1682 * the pad is not guaranteed to look in any special way, although
1683 * existing implementations often pad with the zero byte. This means
1684 * that you may have to "frame" data, so it is possible to infer the
1685 * original length after decryption. Compare ASN.1 DER which contains
1688 * Return value: Returns %SHISHI_OK iff successful.
1691 shishi_encrypt_ivupdate_etype (Shishi
* handle
,
1695 const char *iv
, size_t ivlen
,
1696 char **ivout
, size_t * ivoutlen
,
1697 const char *in
, size_t inlen
,
1698 char **out
, size_t * outlen
)
1700 Shishi_encrypt_function encrypt
;
1703 if (VERBOSECRYPTO (handle
))
1705 printf ("encrypt (type=%s, usage=%d, key, in)\n",
1706 shishi_key_name (key
), keyusage
);
1707 printf ("\t ;; key (%d):\n", shishi_key_length (key
));
1708 hexprint (shishi_key_value (key
), shishi_key_length (key
));
1710 printf ("\t ;; in (%d):\n", inlen
);
1711 escapeprint (in
, inlen
);
1712 hexprint (in
, inlen
);
1716 printf ("\t ;; iv (%d):\n", ivlen
);
1717 escapeprint (iv
, ivlen
);
1718 hexprint (iv
, ivlen
);
1723 encrypt
= _shishi_cipher_encrypt (etype
);
1724 if (encrypt
== NULL
)
1726 shishi_error_printf (handle
, "Unsupported keytype %d",
1727 shishi_key_type (key
));
1728 return SHISHI_CRYPTO_ERROR
;
1731 res
= (*encrypt
) (handle
, key
, keyusage
,
1732 iv
, ivlen
, ivout
, ivoutlen
, in
, inlen
, out
, outlen
);
1734 if (VERBOSECRYPTO (handle
))
1736 printf ("\t ;; encrypt out:\n");
1737 escapeprint (*out
, *outlen
);
1738 hexprint (*out
, *outlen
);
1740 if (ivout
&& ivoutlen
)
1742 printf ("\t ;; iv out:\n");
1743 escapeprint (*ivout
, *ivoutlen
);
1744 hexprint (*ivout
, *ivoutlen
);
1753 * shishi_encrypt_iv_etype:
1754 * @handle: shishi handle as allocated by shishi_init().
1755 * @key: key to encrypt with.
1756 * @keyusage: integer specifying what this key is encrypting.
1757 * @etype: integer specifying what cipher to use.
1758 * @iv: input array with initialization vector
1759 * @ivlen: size of input array with initialization vector.
1760 * @in: input array with data to encrypt.
1761 * @inlen: size of input array with data to encrypt.
1762 * @out: output array with newly allocated encrypted data.
1763 * @outlen: output variable with size of newly allocated output array.
1765 * Encrypts data as per encryption method using specified
1766 * initialization vector and key. The key actually used is derived
1767 * using the key usage. If key usage is 0, no key derivation is used.
1768 * The OUT buffer must be deallocated by the caller. The next IV is
1769 * lost, see shishi_encrypt_ivupdate_etype if you need it.
1771 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
1772 * exactly, some Kerberos encryption types add pad to make the data
1773 * fit into the block size of the encryption algorithm. Furthermore,
1774 * the pad is not guaranteed to look in any special way, although
1775 * existing implementations often pad with the zero byte. This means
1776 * that you may have to "frame" data, so it is possible to infer the
1777 * original length after decryption. Compare ASN.1 DER which contains
1780 * Return value: Returns %SHISHI_OK iff successful.
1783 shishi_encrypt_iv_etype (Shishi
* handle
,
1787 const char *iv
, size_t ivlen
,
1788 const char *in
, size_t inlen
,
1789 char **out
, size_t * outlen
)
1791 return shishi_encrypt_ivupdate_etype (handle
, key
, keyusage
, etype
,
1792 iv
, ivlen
, NULL
, NULL
,
1793 in
, inlen
, out
, outlen
);
1797 * shishi_encrypt_etype:
1798 * @handle: shishi handle as allocated by shishi_init().
1799 * @key: key to encrypt with.
1800 * @keyusage: integer specifying what this key is encrypting.
1801 * @etype: integer specifying what cipher to use.
1802 * @in: input array with data to encrypt.
1803 * @inlen: size of input array with data to encrypt.
1804 * @out: output array with newly allocated encrypted data.
1805 * @outlen: output variable with size of newly allocated output array.
1807 * Encrypts data as per encryption method using specified
1808 * initialization vector and key. The key actually used is derived
1809 * using the key usage. If key usage is 0, no key derivation is used.
1810 * The OUT buffer must be deallocated by the caller. The default IV
1811 * is used, see shishi_encrypt_iv_etype if you need to alter it. The
1812 * next IV is lost, see shishi_encrypt_ivupdate_etype if you need it.
1814 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
1815 * exactly, some Kerberos encryption types add pad to make the data
1816 * fit into the block size of the encryption algorithm. Furthermore,
1817 * the pad is not guaranteed to look in any special way, although
1818 * existing implementations often pad with the zero byte. This means
1819 * that you may have to "frame" data, so it is possible to infer the
1820 * original length after decryption. Compare ASN.1 DER which contains
1823 * Return value: Returns %SHISHI_OK iff successful.
1826 shishi_encrypt_etype (Shishi
* handle
,
1830 const char *in
, size_t inlen
,
1831 char **out
, size_t * outlen
)
1833 return shishi_encrypt_ivupdate_etype (handle
, key
, keyusage
,
1834 shishi_key_type (key
),
1835 NULL
, 0, NULL
, NULL
,
1836 in
, inlen
, out
, outlen
);
1840 * shishi_encrypt_ivupdate:
1841 * @handle: shishi handle as allocated by shishi_init().
1842 * @key: key to encrypt with.
1843 * @keyusage: integer specifying what this key is encrypting.
1844 * @iv: input array with initialization vector
1845 * @ivlen: size of input array with initialization vector.
1846 * @ivout: output array with newly allocated updated initialization vector.
1847 * @ivoutlen: size of output array with updated initialization vector.
1848 * @in: input array with data to encrypt.
1849 * @inlen: size of input array with data to encrypt.
1850 * @out: output array with newly allocated encrypted data.
1851 * @outlen: output variable with size of newly allocated output array.
1853 * Encrypts data using specified initialization vector and key. The
1854 * key actually used is derived using the key usage. If key usage is
1855 * 0, no key derivation is used. The OUT buffer must be deallocated
1856 * by the caller. If IVOUT or IVOUTLEN is NULL, the updated IV is not
1859 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
1860 * exactly, some Kerberos encryption types add pad to make the data
1861 * fit into the block size of the encryption algorithm. Furthermore,
1862 * the pad is not guaranteed to look in any special way, although
1863 * existing implementations often pad with the zero byte. This means
1864 * that you may have to "frame" data, so it is possible to infer the
1865 * original length after decryption. Compare ASN.1 DER which contains
1868 * Return value: Returns %SHISHI_OK iff successful.
1871 shishi_encrypt_ivupdate (Shishi
* handle
,
1874 const char *iv
, size_t ivlen
,
1875 char **ivout
, size_t * ivoutlen
,
1876 const char *in
, size_t inlen
,
1877 char **out
, size_t * outlen
)
1879 return shishi_encrypt_ivupdate_etype (handle
, key
, keyusage
,
1880 shishi_key_type (key
),
1881 iv
, ivlen
, ivout
, ivoutlen
,
1882 in
, inlen
, out
, outlen
);
1886 * shishi_encrypt_iv:
1887 * @handle: shishi handle as allocated by shishi_init().
1888 * @key: key to encrypt with.
1889 * @keyusage: integer specifying what this key is encrypting.
1890 * @iv: input array with initialization vector
1891 * @ivlen: size of input array with initialization vector.
1892 * @in: input array with data to encrypt.
1893 * @inlen: size of input array with data to encrypt.
1894 * @out: output array with newly allocated encrypted data.
1895 * @outlen: output variable with size of newly allocated output array.
1897 * Encrypts data using specified initialization vector and key. The
1898 * key actually used is derived using the key usage. If key usage is
1899 * 0, no key derivation is used. The OUT buffer must be deallocated
1900 * by the caller. The next IV is lost, see shishi_encrypt_ivupdate if
1903 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
1904 * exactly, some Kerberos encryption types add pad to make the data
1905 * fit into the block size of the encryption algorithm. Furthermore,
1906 * the pad is not guaranteed to look in any special way, although
1907 * existing implementations often pad with the zero byte. This means
1908 * that you may have to "frame" data, so it is possible to infer the
1909 * original length after decryption. Compare ASN.1 DER which contains
1912 * Return value: Returns %SHISHI_OK iff successful.
1915 shishi_encrypt_iv (Shishi
* handle
,
1918 const char *iv
, size_t ivlen
,
1919 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
1921 return shishi_encrypt_ivupdate_etype (handle
, key
, keyusage
,
1922 shishi_key_type (key
),
1923 iv
, ivlen
, NULL
, NULL
,
1924 in
, inlen
, out
, outlen
);
1929 * @handle: shishi handle as allocated by shishi_init().
1930 * @key: key to encrypt with.
1931 * @keyusage: integer specifying what this key is encrypting.
1932 * @in: input array with data to encrypt.
1933 * @inlen: size of input array with data to encrypt.
1934 * @out: output array with newly allocated encrypted data.
1935 * @outlen: output variable with size of newly allocated output array.
1937 * Encrypts data using specified key. The key actually used is
1938 * derived using the key usage. If key usage is 0, no key derivation
1939 * is used. The OUT buffer must be deallocated by the caller. The
1940 * default IV is used, see shishi_encrypt_iv if you need to alter it.
1941 * The next IV is lost, see shishi_encrypt_ivupdate if you need it.
1943 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
1944 * exactly, some Kerberos encryption types add pad to make the data
1945 * fit into the block size of the encryption algorithm. Furthermore,
1946 * the pad is not guaranteed to look in any special way, although
1947 * existing implementations often pad with the zero byte. This means
1948 * that you may have to "frame" data, so it is possible to infer the
1949 * original length after decryption. Compare ASN.1 DER which contains
1952 * Return value: Returns %SHISHI_OK iff successful.
1955 shishi_encrypt (Shishi
* handle
,
1958 char *in
, size_t inlen
, char **out
, size_t * outlen
)
1960 return shishi_encrypt_ivupdate_etype (handle
, key
, keyusage
,
1961 shishi_key_type (key
),
1962 NULL
, 0, NULL
, NULL
,
1963 in
, inlen
, out
, outlen
);
1967 * shishi_decrypt_ivupdate_etype:
1968 * @handle: shishi handle as allocated by shishi_init().
1969 * @key: key to decrypt with.
1970 * @keyusage: integer specifying what this key is decrypting.
1971 * @etype: integer specifying what cipher to use.
1972 * @iv: input array with initialization vector
1973 * @ivlen: size of input array with initialization vector.
1974 * @ivout: output array with newly allocated updated initialization vector.
1975 * @ivoutlen: size of output array with updated initialization vector.
1976 * @in: input array with data to decrypt.
1977 * @inlen: size of input array with data to decrypt.
1978 * @out: output array with newly allocated decrypted data.
1979 * @outlen: output variable with size of newly allocated output array.
1981 * Decrypts data as per encryption method using specified
1982 * initialization vector and key. The key actually used is derived
1983 * using the key usage. If key usage is 0, no key derivation is used.
1984 * The OUT buffer must be deallocated by the caller. If IVOUT or
1985 * IVOUTLEN is NULL, the updated IV is not saved anywhere.
1987 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
1988 * exactly, some Kerberos encryption types add pad to make the data
1989 * fit into the block size of the encryption algorithm. Furthermore,
1990 * the pad is not guaranteed to look in any special way, although
1991 * existing implementations often pad with the zero byte. This means
1992 * that you may have to "frame" data, so it is possible to infer the
1993 * original length after decryption. Compare ASN.1 DER which contains
1996 * Return value: Returns %SHISHI_OK iff successful.
1999 shishi_decrypt_ivupdate_etype (Shishi
* handle
,
2003 const char *iv
, size_t ivlen
,
2004 char **ivout
, size_t * ivoutlen
,
2005 const char *in
, size_t inlen
,
2006 char **out
, size_t * outlen
)
2008 Shishi_decrypt_function decrypt
;
2011 if (VERBOSECRYPTO (handle
))
2013 printf ("decrypt (type=%s, usage=%d, key, in, out)\n",
2014 shishi_key_name (key
), keyusage
);
2015 printf ("\t ;; key (%d):\n", shishi_key_length (key
));
2016 hexprint (shishi_key_value (key
), shishi_key_length (key
));
2018 printf ("\t ;; in (%d):\n", inlen
);
2019 escapeprint (in
, inlen
);
2020 hexprint (in
, inlen
);
2024 decrypt
= _shishi_cipher_decrypt (etype
);
2025 if (decrypt
== NULL
)
2027 shishi_error_printf (handle
, "Unsupported keytype %d",
2028 shishi_key_type (key
));
2029 return SHISHI_CRYPTO_ERROR
;
2032 res
= (*decrypt
) (handle
, key
, keyusage
,
2033 iv
, ivlen
, ivout
, ivoutlen
, in
, inlen
, out
, outlen
);
2035 if (VERBOSECRYPTO (handle
))
2037 printf ("\t ;; decrypt out:\n");
2038 escapeprint (*out
, *outlen
);
2039 hexprint (*out
, *outlen
);
2047 * shishi_decrypt_iv_etype:
2048 * @handle: shishi handle as allocated by shishi_init().
2049 * @key: key to decrypt with.
2050 * @keyusage: integer specifying what this key is decrypting.
2051 * @etype: integer specifying what cipher to use.
2052 * @iv: input array with initialization vector
2053 * @ivlen: size of input array with initialization vector.
2054 * @in: input array with data to decrypt.
2055 * @inlen: size of input array with data to decrypt.
2056 * @out: output array with newly allocated decrypted data.
2057 * @outlen: output variable with size of newly allocated output array.
2059 * Decrypts data as per encryption method using specified
2060 * initialization vector and key. The key actually used is derived
2061 * using the key usage. If key usage is 0, no key derivation is used.
2062 * The OUT buffer must be deallocated by the caller. The next IV is
2063 * lost, see shishi_decrypt_ivupdate_etype if you need it.
2065 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
2066 * exactly, some Kerberos encryption types add pad to make the data
2067 * fit into the block size of the encryption algorithm. Furthermore,
2068 * the pad is not guaranteed to look in any special way, although
2069 * existing implementations often pad with the zero byte. This means
2070 * that you may have to "frame" data, so it is possible to infer the
2071 * original length after decryption. Compare ASN.1 DER which contains
2074 * Return value: Returns %SHISHI_OK iff successful.
2077 shishi_decrypt_iv_etype (Shishi
* handle
,
2081 const char *iv
, size_t ivlen
,
2082 const char *in
, size_t inlen
,
2083 char **out
, size_t * outlen
)
2085 return shishi_decrypt_ivupdate_etype (handle
, key
, keyusage
, etype
,
2086 iv
, ivlen
, NULL
, NULL
,
2087 in
, inlen
, out
, outlen
);
2091 * shishi_decrypt_etype:
2092 * @handle: shishi handle as allocated by shishi_init().
2093 * @key: key to decrypt with.
2094 * @keyusage: integer specifying what this key is decrypting.
2095 * @etype: integer specifying what cipher to use.
2096 * @in: input array with data to decrypt.
2097 * @inlen: size of input array with data to decrypt.
2098 * @out: output array with newly allocated decrypted data.
2099 * @outlen: output variable with size of newly allocated output array.
2101 * Decrypts data as per encryption method using specified key. The
2102 * key actually used is derived using the key usage. If key usage is
2103 * 0, no key derivation is used. The OUT buffer must be deallocated
2104 * by the caller. The default IV is used, see shishi_decrypt_iv_etype
2105 * if you need to alter it. The next IV is lost, see
2106 * shishi_decrypt_ivupdate_etype if you need it.
2108 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
2109 * exactly, some Kerberos encryption types add pad to make the data
2110 * fit into the block size of the encryption algorithm. Furthermore,
2111 * the pad is not guaranteed to look in any special way, although
2112 * existing implementations often pad with the zero byte. This means
2113 * that you may have to "frame" data, so it is possible to infer the
2114 * original length after decryption. Compare ASN.1 DER which contains
2117 * Return value: Returns %SHISHI_OK iff successful.
2120 shishi_decrypt_etype (Shishi
* handle
,
2124 const char *in
, size_t inlen
,
2125 char **out
, size_t * outlen
)
2127 return shishi_decrypt_ivupdate_etype (handle
, key
, keyusage
, etype
,
2128 NULL
, 0, NULL
, NULL
,
2129 in
, inlen
, out
, outlen
);
2133 * shishi_decrypt_ivupdate:
2134 * @handle: shishi handle as allocated by shishi_init().
2135 * @key: key to decrypt with.
2136 * @keyusage: integer specifying what this key is decrypting.
2137 * @iv: input array with initialization vector
2138 * @ivlen: size of input array with initialization vector.
2139 * @ivout: output array with newly allocated updated initialization vector.
2140 * @ivoutlen: size of output array with updated initialization vector.
2141 * @in: input array with data to decrypt.
2142 * @inlen: size of input array with data to decrypt.
2143 * @out: output array with newly allocated decrypted data.
2144 * @outlen: output variable with size of newly allocated output array.
2146 * Decrypts data using specified initialization vector and key. The
2147 * key actually used is derived using the key usage. If key usage is
2148 * 0, no key derivation is used. The OUT buffer must be deallocated
2149 * by the caller. If IVOUT or IVOUTLEN is NULL, the updated IV is not
2152 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
2153 * exactly, some Kerberos encryption types add pad to make the data
2154 * fit into the block size of the encryption algorithm. Furthermore,
2155 * the pad is not guaranteed to look in any special way, although
2156 * existing implementations often pad with the zero byte. This means
2157 * that you may have to "frame" data, so it is possible to infer the
2158 * original length after decryption. Compare ASN.1 DER which contains
2161 * Return value: Returns %SHISHI_OK iff successful.
2164 shishi_decrypt_ivupdate (Shishi
* handle
,
2167 const char *iv
, size_t ivlen
,
2168 char **ivout
, size_t * ivoutlen
,
2169 const char *in
, size_t inlen
,
2170 char **out
, size_t * outlen
)
2172 return shishi_decrypt_ivupdate_etype (handle
, key
, keyusage
,
2173 shishi_key_type (key
),
2174 iv
, ivlen
, ivout
, ivoutlen
,
2175 in
, inlen
, out
, outlen
);
2179 * shishi_decrypt_iv:
2180 * @handle: shishi handle as allocated by shishi_init().
2181 * @key: key to decrypt with.
2182 * @keyusage: integer specifying what this key is decrypting.
2183 * @iv: input array with initialization vector
2184 * @ivlen: size of input array with initialization vector.
2185 * @in: input array with data to decrypt.
2186 * @inlen: size of input array with data to decrypt.
2187 * @out: output array with newly allocated decrypted data.
2188 * @outlen: output variable with size of newly allocated output array.
2190 * Decrypts data using specified initialization vector and key. The
2191 * key actually used is derived using the key usage. If key usage is
2192 * 0, no key derivation is used. The OUT buffer must be deallocated
2193 * by the caller. The next IV is lost, see
2194 * shishi_decrypt_ivupdate_etype if you need it.
2196 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
2197 * exactly, some Kerberos encryption types add pad to make the data
2198 * fit into the block size of the encryption algorithm. Furthermore,
2199 * the pad is not guaranteed to look in any special way, although
2200 * existing implementations often pad with the zero byte. This means
2201 * that you may have to "frame" data, so it is possible to infer the
2202 * original length after decryption. Compare ASN.1 DER which contains
2205 * Return value: Returns %SHISHI_OK iff successful.
2208 shishi_decrypt_iv (Shishi
* handle
,
2211 const char *iv
, size_t ivlen
,
2212 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
2214 return shishi_decrypt_ivupdate_etype (handle
, key
, keyusage
,
2215 shishi_key_type (key
),
2216 iv
, ivlen
, NULL
, NULL
,
2217 in
, inlen
, out
, outlen
);
2222 * @handle: shishi handle as allocated by shishi_init().
2223 * @key: key to decrypt with.
2224 * @keyusage: integer specifying what this key is decrypting.
2225 * @in: input array with data to decrypt.
2226 * @inlen: size of input array with data to decrypt.
2227 * @out: output array with newly allocated decrypted data.
2228 * @outlen: output variable with size of newly allocated output array.
2230 * Decrypts data specified key. The key actually used is derived
2231 * using the key usage. If key usage is 0, no key derivation is used.
2232 * The OUT buffer must be deallocated by the caller. The default IV
2233 * is used, see shishi_decrypt_iv if you need to alter it. The next
2234 * IV is lost, see shishi_decrypt_ivupdate if you need it.
2236 * Note that DECRYPT(ENCRYPT(data)) does not necessarily yield data
2237 * exactly, some Kerberos encryption types add pad to make the data
2238 * fit into the block size of the encryption algorithm. Furthermore,
2239 * the pad is not guaranteed to look in any special way, although
2240 * existing implementations often pad with the zero byte. This means
2241 * that you may have to "frame" data, so it is possible to infer the
2242 * original length after decryption. Compare ASN.1 DER which contains
2245 * Return value: Returns %SHISHI_OK iff successful.
2248 shishi_decrypt (Shishi
* handle
,
2251 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
2253 return shishi_decrypt_ivupdate_etype (handle
, key
, keyusage
,
2254 shishi_key_type (key
),
2255 NULL
, 0, NULL
, NULL
,
2256 in
, inlen
, out
, outlen
);
2261 * @handle: shishi handle as allocated by shishi_init().
2262 * @data: output array to be filled with random data.
2263 * @datalen: size of output array.
2265 * Store cryptographically strong random data of given size in the
2268 * Return value: Returns %SHISHI_OK iff successful.
2271 shishi_randomize (Shishi
* handle
, char *data
, size_t datalen
)
2274 gcry_randomize (data
, datalen
, GCRY_STRONG_RANDOM
);
2279 device
= "/dev/urandom";
2281 fd
= open (device
, O_RDONLY
);
2284 shishi_error_printf (handle
, "Could not open random device: %s\n",
2286 return SHISHI_FOPEN_ERROR
;
2290 read (fd
, data
, datalen
);
2300 * @handle: shishi handle as allocated by shishi_init().
2301 * @in: input array with data to decrypt.
2302 * @inlen: size of input array with data to decrypt ("M").
2303 * @out: output array with decrypted data.
2304 * @outlen: size of output array ("N").
2306 * Fold data into a fixed length output array, with the intent to give
2307 * each input bit approximately equal weight in determining the value
2308 * of each output bit.
2310 * The algorithm is from "A Better Key Schedule For DES-like Ciphers"
2311 * by Uri Blumenthal and Steven M. Bellovin,
2312 * <URL:http://www.research.att.com/~smb/papers/ides.pdf>, although
2313 * the sample vectors provided by the paper are incorrect.
2315 * Return value: Returns %SHISHI_OK iff successful.
2318 shishi_n_fold (Shishi
* handle
,
2319 char *in
, size_t inlen
, char *out
, size_t outlen
)
2329 To n-fold a number X, replicate the input value to a length that is
2330 the least common multiple of n and the length of X. Before each
2331 repetition, the input is rotated to the right by 13 bit
2332 positions. The successive n-bit chunks are added together using
2333 1's-complement addition (that is, addition with end-around carry)
2334 to yield a n-bit result denoted <X>_n.
2337 a
= (char *) xmemdup (a
, in
, m
);
2341 if (VERBOSECRYPTO (handle
))
2343 printf ("%d-fold (string)\n", n
* 8);
2344 printf ("\t ;; string length %d bytes %d bits\n", m
, m
* 8);
2348 printf ("\t ;; lcm(%d, %d) = lcm(%d, %d) = %d\n",
2349 8 * m
, 8 * n
, m
, n
, lcmmn
);
2353 buf
= (char *) xmalloc (lcmmn
);
2355 /* Replicate the input th the LCMMN length */
2356 for (i
= 0; i
< (lcmmn
/ m
); i
++)
2358 if (VERBOSECRYPTO (handle
))
2360 printf ("\t ;; %d-th replication\n", i
+ 1);
2361 printf ("string = rot13(string)\n");
2364 memcpy ((char *) &buf
[i
* m
], a
, m
);
2365 rot13 (handle
, a
, a
, m
);
2367 if (VERBOSECRYPTO (handle
))
2371 memset (out
, 0, n
); /* just in case */
2373 if (VERBOSECRYPTO (handle
))
2375 printf ("\t ;; replicated string (length %d):\n", lcmmn
);
2376 hexprint (buf
, lcmmn
);
2378 binprint (buf
, lcmmn
);
2380 printf ("sum = 0\n");
2383 /* Now we view the buf as set of n-byte strings
2384 Add the n-byte long chunks together, using
2385 one's complement addition, storing the
2386 result in the output string. */
2388 for (i
= 0; i
< (lcmmn
/ n
); i
++)
2390 if (VERBOSECRYPTO (handle
))
2392 printf ("\t ;; %d-th one's complement addition sum\n", i
+ 1);
2393 printf ("\t ;; sum:\n");
2398 printf ("\t ;; A (offset %d):\n", i
* n
);
2399 hexprint (&buf
[i
* n
], n
);
2401 binprint (&buf
[i
* n
], n
);
2403 printf ("sum = ocadd(sum, A);\n");
2406 ocadd (out
, (char *) &buf
[i
* n
], out
, n
);
2408 if (VERBOSECRYPTO (handle
))
2410 printf ("\t ;; sum:\n");
2419 if (VERBOSECRYPTO (handle
))
2421 printf ("\t ;; nfold\n");
2435 #define MAX_DR_CONSTANT 1024
2439 * @handle: shishi handle as allocated by shishi_init().
2440 * @key: input array with cryptographic key to use.
2441 * @constant: input array with the constant string.
2442 * @constantlen: size of input array with the constant string.
2443 * @derivedrandom: output array with derived random data.
2444 * @derivedrandomlen: size of output array with derived random data.
2446 * Derive "random" data from a key and a constant thusly:
2447 * DR(KEY, CONSTANT) = TRUNCATE(DERIVEDRANDOMLEN,
2448 * SHISHI_ENCRYPT(KEY, CONSTANT)).
2450 * Return value: Returns %SHISHI_OK iff successful.
2453 shishi_dr (Shishi
* handle
,
2455 char *constant
, size_t constantlen
,
2456 char *derivedrandom
, size_t derivedrandomlen
)
2459 char plaintext
[MAX_DR_CONSTANT
];
2460 char nfoldconstant
[MAX_DR_CONSTANT
];
2461 size_t blocksize
= shishi_cipher_blocksize (shishi_key_type (key
));
2462 size_t totlen
, cipherlen
;
2465 if (VERBOSECRYPTO (handle
))
2467 printf ("dr (%s, key, constant, %d)\n",
2468 shishi_cipher_name (shishi_key_type (key
)), derivedrandomlen
);
2469 printf ("\t ;; key (length %d):\n", shishi_key_length (key
));
2470 hexprint (shishi_key_value (key
), shishi_key_length (key
));
2472 binprint (shishi_key_value (key
), shishi_key_length (key
));
2474 printf ("\t ;; constant %s':\n", constant
);
2475 escapeprint (constant
, constantlen
);
2476 hexprint (constant
, constantlen
);
2478 binprint (constant
, constantlen
);
2483 if (constantlen
> MAX_DR_CONSTANT
)
2484 return SHISHI_TOO_SMALL_BUFFER
;
2486 if (constantlen
== blocksize
)
2488 memcpy (nfoldconstant
, constant
, constantlen
);
2492 res
= shishi_n_fold (handle
, constant
, constantlen
, nfoldconstant
,
2494 if (res
!= SHISHI_OK
)
2498 if (VERBOSECRYPTO (handle
))
2500 printf ("\t ;; possibly nfolded constant (length %d):\n", blocksize
);
2501 escapeprint (nfoldconstant
, blocksize
);
2502 hexprint (nfoldconstant
, blocksize
);
2504 binprint (nfoldconstant
, blocksize
);
2508 memcpy (plaintext
, nfoldconstant
, blocksize
);
2513 res
= shishi_encrypt (handle
, key
, 0, plaintext
, blocksize
,
2514 &cipher
, &cipherlen
);
2515 if (res
!= SHISHI_OK
)
2517 if (cipherlen
!= blocksize
)
2518 return SHISHI_CRYPTO_ERROR
;
2519 memcpy (derivedrandom
+ totlen
, cipher
, cipherlen
);
2520 memcpy (plaintext
, cipher
, cipherlen
);
2522 totlen
+= cipherlen
;
2524 while (totlen
< derivedrandomlen
);
2526 if (VERBOSECRYPTO (handle
))
2528 printf ("\t ;; derived random (length %d):\n", derivedrandomlen
);
2529 hexprint (derivedrandom
, derivedrandomlen
);
2531 binprint (derivedrandom
, derivedrandomlen
);
2540 * @handle: shishi handle as allocated by shishi_init().
2541 * @key: input cryptographic key to use.
2542 * @constant: input array with the constant string.
2543 * @constantlen: size of input array with the constant string.
2544 * @derivedkey: pointer to derived key (allocated by caller).
2546 * Derive a key from a key and a constant thusly:
2547 * DK(KEY, CONSTANT) = SHISHI_RANDOM-TO-KEY(SHISHI_DR(KEY, CONSTANT)).
2549 * Return value: Returns %SHISHI_OK iff successful.
2552 shishi_dk (Shishi
* handle
,
2554 char *constant
, int constantlen
, Shishi_key
* derivedkey
)
2556 char random
[MAX_RANDOM_LEN
];
2559 if (VERBOSECRYPTO (handle
))
2561 printf ("dk (%s, key, constant)\n", shishi_key_name (key
));
2562 printf ("\t ;; key (length %d):\n", shishi_key_length (key
));
2563 hexprint (shishi_key_value (key
), shishi_key_length (key
));
2565 binprint (shishi_key_value (key
), shishi_key_length (key
));
2567 printf ("\t ;; constant:\n");
2568 escapeprint (constant
, constantlen
);
2569 hexprint (constant
, constantlen
);
2571 binprint (constant
, constantlen
);
2576 shishi_key_type_set (derivedkey
, shishi_key_type (key
));
2578 res
= shishi_dr (handle
, key
, constant
, constantlen
, random
,
2579 shishi_key_length (derivedkey
));
2580 if (res
!= SHISHI_OK
)
2583 res
= shishi_random_to_key (handle
, shishi_key_type (derivedkey
),
2584 random
, shishi_key_length (derivedkey
),
2586 if (res
!= SHISHI_OK
)