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
= xmalloc (*outhashlen
);
261 memcpy (*outhash
, hash
, *outhashlen
);
265 struct hmac_sha1_ctx ctx
;
266 hmac_sha1_set_key (&ctx
, shishi_key_length (key
), shishi_key_value (key
));
267 hmac_sha1_update (&ctx
, inlen
, in
);
268 *outhashlen
= SHA1_DIGEST_SIZE
;
269 *outhash
= xmalloc (*outhashlen
);
270 hmac_sha1_digest (&ctx
, *outhashlen
, *outhash
);
276 simplified_hmac_verify (Shishi
* handle
,
278 const char *in
, size_t inlen
,
279 const char *hmac
, size_t hmaclen
)
286 res
= simplified_hmac (handle
, key
, in
, inlen
, &hash
, &hlen
);
287 if (res
!= SHISHI_OK
|| hash
== NULL
)
290 same
= (hlen
== hmaclen
) && memcmp (hash
, hmac
, hmaclen
) == 0;
296 shishi_error_printf (handle
, "HMAC verify failed");
297 return SHISHI_CRYPTO_ERROR
;
305 SHISHI_DERIVEKEYMODE_CHECKSUM
,
306 SHISHI_DERIVEKEYMODE_PRIVACY
,
307 SHISHI_DERIVEKEYMODE_INTEGRITY
309 Shishi_derivekeymode
;
312 simplified_derivekey (Shishi
* handle
,
314 int keyusage
, int derivekeymode
, Shishi_key
** outkey
)
318 Shishi_key
*derivedkey
;
320 if (VERBOSECRYPTO (handle
))
322 printf ("simplified_derivekey\n");
323 printf ("\t ;; mode %d (%s)\n", derivekeymode
,
324 derivekeymode
== SHISHI_DERIVEKEYMODE_CHECKSUM
? "checksum" :
325 derivekeymode
== SHISHI_DERIVEKEYMODE_INTEGRITY
? "integrity" :
326 derivekeymode
== SHISHI_DERIVEKEYMODE_PRIVACY
? "privacy" :
328 hexprint (shishi_key_value (key
), shishi_key_length (key
));
333 res
= shishi_key_from_value (handle
, shishi_key_type (key
),
335 if (res
!= SHISHI_OK
)
338 *outkey
= derivedkey
;
342 uint32_t tmp
= htonl (keyusage
);
343 memcpy (constant
, &tmp
, 4);
344 if (derivekeymode
== SHISHI_DERIVEKEYMODE_CHECKSUM
)
345 constant
[4] = '\x99';
346 else if (derivekeymode
== SHISHI_DERIVEKEYMODE_INTEGRITY
)
347 constant
[4] = '\x55';
348 else /* if (derivekeymode == SHISHI_DERIVEKEYMODE_PRIVACY) */
349 constant
[4] = '\xAA';
351 res
= shishi_dk (handle
, key
, constant
, 5, derivedkey
);
355 shishi_key_copy (derivedkey
, key
);
358 if (VERBOSECRYPTO (handle
))
360 printf ("\t ;; simplified_derivekey out (%d):\n",
361 shishi_key_length (derivedkey
));
362 hexprint (shishi_key_value (derivedkey
),
363 shishi_key_length (derivedkey
));
371 simplified_dencrypt (Shishi
* handle
,
373 const char *iv
, size_t ivlen
,
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 gcry_cipher_close (ch
);
448 struct CBC_CTX(struct des_ctx
, DES_BLOCK_SIZE
) des
;
449 struct CBC_CTX(struct des3_ctx
, DES3_BLOCK_SIZE
) des3
;
450 struct CBC_CTS_CTX(struct aes_ctx
, AES_BLOCK_SIZE
) aes
;
455 *out
= xmalloc (*outlen
);
457 switch (shishi_key_type (key
))
459 case SHISHI_DES_CBC_CRC
:
460 case SHISHI_DES_CBC_MD4
:
461 case SHISHI_DES_CBC_MD5
:
462 rc
= des_set_key (&des
.ctx
, shishi_key_value (key
));
465 shishi_error_printf (handle
, "Nettle setkey failed");
466 return SHISHI_CRYPTO_INTERNAL_ERROR
;
469 CBC_SET_IV (&des
, iv
);
471 memset(des
.iv
, 0, sizeof(des
.iv
));
473 CBC_DECRYPT (&des
, des_decrypt
, inlen
, *out
, in
);
475 CBC_ENCRYPT (&des
, des_encrypt
, inlen
, *out
, in
);
478 case SHISHI_DES3_CBC_HMAC_SHA1_KD
:
479 rc
= des3_set_key (&des3
.ctx
, shishi_key_value (key
));
482 shishi_error_printf (handle
, "Nettle setkey failed");
483 return SHISHI_CRYPTO_INTERNAL_ERROR
;
486 CBC_SET_IV (&des3
, iv
);
488 memset(des3
.iv
, 0, sizeof(des3
.iv
));
490 CBC_DECRYPT (&des3
, des3_decrypt
, inlen
, *out
, in
);
492 CBC_ENCRYPT (&des3
, des3_encrypt
, inlen
, *out
, in
);
495 case SHISHI_AES128_CTS_HMAC_SHA1_96
:
496 case SHISHI_AES256_CTS_HMAC_SHA1_96
:
498 CBC_CTS_SET_IV (&aes
, iv
);
500 memset(aes
.iv
, 0, sizeof(aes
.iv
));
503 aes_set_decrypt_key (&aes
.ctx
, shishi_key_length (key
),
504 shishi_key_value (key
));
505 CBC_CTS_DECRYPT (&aes
, aes_decrypt
, inlen
, *out
, in
);
509 aes_set_encrypt_key (&aes
.ctx
, shishi_key_length (key
),
510 shishi_key_value (key
));
511 CBC_CTS_ENCRYPT (&aes
, aes_encrypt
, inlen
, *out
, in
);
521 simplified_encrypt (Shishi
* handle
,
524 const char *iv
, size_t ivlen
,
525 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
530 if ((shishi_key_type (key
) == SHISHI_DES3_CBC_HMAC_SHA1_KD
||
531 shishi_key_type (key
) == SHISHI_DES_CBC_CRC
||
532 shishi_key_type (key
) == SHISHI_DES_CBC_MD4
||
533 shishi_key_type (key
) == SHISHI_DES_CBC_MD5
) && (inlen
% 8) != 0)
534 while (((inlen
+ padzerolen
) % 8) != 0)
539 char *pt
= NULL
, *ct
= NULL
, *hmac
= NULL
;
540 int blen
= shishi_cipher_blocksize (shishi_key_type (key
));
541 size_t ctlen
, ptlen
, hmaclen
;
542 Shishi_key
*privacykey
= NULL
, *integritykey
= NULL
;
544 ptlen
= inlen
+ blen
+ padzerolen
;
545 pt
= xmalloc (ptlen
);
547 res
= shishi_randomize (handle
, pt
, blen
);
548 if (res
!= SHISHI_OK
)
551 memcpy (pt
+ blen
, in
, inlen
);
552 memset (pt
+ blen
+ inlen
, 0, padzerolen
);
554 res
= simplified_derivekey (handle
, key
, keyusage
,
555 SHISHI_DERIVEKEYMODE_PRIVACY
, &privacykey
);
556 if (res
!= SHISHI_OK
)
559 res
= simplified_dencrypt (handle
, privacykey
, iv
, ivlen
,
560 pt
, ptlen
, &ct
, &ctlen
, 0);
561 if (res
!= SHISHI_OK
)
565 res
= simplified_derivekey (handle
, key
, keyusage
,
566 SHISHI_DERIVEKEYMODE_INTEGRITY
,
568 if (res
!= SHISHI_OK
)
572 simplified_hmac (handle
, integritykey
, pt
, ptlen
, &hmac
, &hmaclen
);
573 if (res
!= SHISHI_OK
)
576 *outlen
= ctlen
+ hmaclen
;
577 *out
= xmalloc (*outlen
);
578 memcpy (*out
, ct
, ctlen
);
579 memcpy (*out
+ ctlen
, hmac
, hmaclen
);
583 shishi_key_done (&privacykey
);
585 shishi_key_done (&integritykey
);
595 res
= simplified_dencrypt (handle
, key
, iv
, ivlen
,
596 in
, inlen
, out
, outlen
, 0);
603 simplified_decrypt (Shishi
* handle
,
606 const char *iv
, size_t ivlen
,
607 const char *in
, size_t inlen
, char **out
, size_t * outlen
)
613 Shishi_key
*privacykey
= NULL
, *integritykey
= NULL
;
614 int blen
= shishi_cipher_blocksize (shishi_key_type (key
));
615 size_t hlen
= 20; /* XXX only works for SHA-1 */
618 res
= simplified_derivekey (handle
, key
, keyusage
,
619 SHISHI_DERIVEKEYMODE_PRIVACY
, &privacykey
);
620 if (res
!= SHISHI_OK
)
623 res
= simplified_dencrypt (handle
, privacykey
, iv
, ivlen
,
624 in
, inlen
- hlen
, out
, outlen
, 1);
625 if (res
!= SHISHI_OK
)
628 res
= simplified_derivekey (handle
, key
, keyusage
,
629 SHISHI_DERIVEKEYMODE_INTEGRITY
,
631 if (res
!= SHISHI_OK
)
634 res
= simplified_hmac_verify (handle
, integritykey
, *out
, *outlen
,
635 in
+ inlen
- hlen
, hlen
);
637 if (res
!= SHISHI_OK
)
640 memmove (*out
, *out
+ blen
, *outlen
- blen
);
641 *outlen
= *outlen
- blen
;
642 *out
= xrealloc (*out
, *outlen
);
646 shishi_key_done (&privacykey
);
648 shishi_key_done (&integritykey
);
652 res
= simplified_dencrypt (handle
, key
, iv
, ivlen
,
653 in
, inlen
, out
, outlen
, 1);
660 simplified_checksum (Shishi
* handle
,
664 char *in
, size_t inlen
, char **out
, size_t * outlen
)
666 Shishi_key
*checksumkey
;
667 size_t hlen
= 20; /* XXX only works for SHA-1 */
668 int cksumlen
= shishi_checksum_cksumlen (cksumtype
);
671 res
= simplified_derivekey (handle
, key
, keyusage
,
672 SHISHI_DERIVEKEYMODE_CHECKSUM
, &checksumkey
);
673 if (res
!= SHISHI_OK
)
676 res
= simplified_hmac (handle
, checksumkey
, in
, inlen
, out
, outlen
);
678 shishi_key_done (&checksumkey
);
680 if (res
!= SHISHI_OK
)
689 _shishi_cipher_init (void)
692 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P
) == 0)
694 if (gcry_check_version (GCRYPT_VERSION
) == NULL
)
695 return SHISHI_CRYPTO_INTERNAL_ERROR
;
696 if (gcry_control (GCRYCTL_DISABLE_SECMEM
, NULL
, 0) != GPG_ERR_NO_ERROR
)
697 return SHISHI_CRYPTO_INTERNAL_ERROR
;
698 if (gcry_control (GCRYCTL_INITIALIZATION_FINISHED
,
699 NULL
, 0) != GPG_ERR_NO_ERROR
)
700 return SHISHI_CRYPTO_INTERNAL_ERROR
;
707 typedef int (*Shishi_random_to_key_function
) (Shishi
* handle
,
710 Shishi_key
* outkey
);
712 typedef int (*Shishi_string_to_key_function
) (Shishi
* handle
,
713 const char *password
,
717 const char *parameter
,
718 Shishi_key
* outkey
);
720 typedef int (*Shishi_encrypt_function
) (Shishi
* handle
,
723 const char *iv
, size_t ivlen
,
724 const char *in
, size_t inlen
,
725 char **out
, size_t * outlen
);
727 typedef int (*Shishi_decrypt_function
) (Shishi
* handle
,
730 const char *iv
, size_t ivlen
,
731 const char *in
, size_t inlen
,
732 char **out
, size_t * outlen
);
734 typedef int (*Shishi_checksum_function
) (Shishi
* handle
,
738 char *in
, size_t inlen
,
739 char **out
, size_t * outlen
);
741 #include "crypto-null.c"
742 #include "crypto-des.c"
743 #include "crypto-3des.c"
744 #include "crypto-aes.c"
755 int defaultcksumtype
;
756 Shishi_random_to_key_function random2key
;
757 Shishi_string_to_key_function string2key
;
758 Shishi_encrypt_function encrypt
;
759 Shishi_decrypt_function decrypt
;
761 typedef struct cipherinfo cipherinfo
;
763 static cipherinfo null_info
= {
778 static cipherinfo des_cbc_crc_info
= {
793 static cipherinfo des_cbc_md4_info
= {
808 static cipherinfo des_cbc_md5_info
= {
823 static cipherinfo des_cbc_none_info
= {
838 static cipherinfo des3_cbc_sha1_kd_info
= {
846 SHISHI_HMAC_SHA1_DES3_KD
,
853 static cipherinfo des3_cbc_none_info
= {
861 SHISHI_HMAC_SHA1_DES3_KD
,
868 static cipherinfo aes128_cts_hmac_sha1_96_info
= {
870 "aes128-cts-hmac-sha1-96",
876 SHISHI_HMAC_SHA1_96_AES128
,
877 aes128_random_to_key
,
878 aes128_string_to_key
,
883 static cipherinfo aes256_cts_hmac_sha1_96_info
= {
885 "aes256-cts-hmac-sha1-96",
891 SHISHI_HMAC_SHA1_96_AES256
,
892 aes256_random_to_key
,
893 aes256_string_to_key
,
898 static cipherinfo
*ciphers
[] = {
905 &des3_cbc_sha1_kd_info
,
906 &aes128_cts_hmac_sha1_96_info
,
907 &aes256_cts_hmac_sha1_96_info
911 * shishi_cipher_supported_p:
912 * @type: encryption type, see Shishi_etype.
914 * Return value: Return 0 iff cipher is unsupported.
917 shishi_cipher_supported_p (int32_t type
)
921 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
922 if (type
== ciphers
[i
]->type
)
929 * shishi_cipher_name:
930 * @type: encryption type, see Shishi_etype.
932 * Return value: Return name of encryption type,
933 * e.g. "des3-cbc-sha1-kd", as defined in the standards.
936 shishi_cipher_name (int32_t type
)
941 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
943 if (type
== ciphers
[i
]->type
)
944 return ciphers
[i
]->name
;
947 asprintf (&p
, "unknown cipher %d", type
);
952 * shishi_cipher_blocksize:
953 * @type: encryption type, see Shishi_etype.
955 * Return value: Return block size for encryption type, as defined in
959 shishi_cipher_blocksize (int32_t type
)
963 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
964 if (type
== ciphers
[i
]->type
)
965 return ciphers
[i
]->blocksize
;
971 * shishi_cipher_minpadsize:
972 * @type: encryption type, see Shishi_etype.
974 * Return value: Return the minimum pad size for encryption type, as
975 * defined in the standards.
978 shishi_cipher_minpadsize (int32_t type
)
982 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
983 if (type
== ciphers
[i
]->type
)
984 return ciphers
[i
]->minpadsize
;
990 * shishi_cipher_confoundersize:
991 * @type: encryption type, see Shishi_etype.
993 * Return value: Returns the size of the confounder (random data) for
994 * encryption type, as defined in the standards.
997 shishi_cipher_confoundersize (int32_t type
)
1001 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1002 if (type
== ciphers
[i
]->type
)
1003 return ciphers
[i
]->confoundersize
;
1009 * shishi_cipher_keylen:
1010 * @type: encryption type, see Shishi_etype.
1012 * Return value: Return length of key used for the encryption type, as
1013 * defined in the standards.
1016 shishi_cipher_keylen (int32_t type
)
1020 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1021 if (type
== ciphers
[i
]->type
)
1022 return ciphers
[i
]->keylen
;
1028 * shishi_cipher_randomlen:
1029 * @type: encryption type, see Shishi_etype.
1031 * Return value: Return length of random used for the encryption type,
1032 * as defined in the standards.
1035 shishi_cipher_randomlen (int32_t type
)
1039 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1040 if (type
== ciphers
[i
]->type
)
1041 return ciphers
[i
]->randomlen
;
1047 * shishi_cipher_defaultcksumtype:
1048 * @type: encryption type, see Shishi_etype.
1050 * Return value: Return associated checksum mechanism for the
1051 * encryption type, as defined in the standards.
1054 shishi_cipher_defaultcksumtype (int32_t type
)
1058 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1059 if (type
== ciphers
[i
]->type
)
1060 return ciphers
[i
]->defaultcksumtype
;
1066 * shishi_cipher_parse:
1067 * @cipher: name of encryption type, e.g. "des3-cbc-sha1-kd".
1069 * Return value: Return encryption type corresponding to a string.
1072 shishi_cipher_parse (const char *cipher
)
1077 i
= strtol (cipher
, &endptr
, 0);
1079 if (endptr
!= cipher
)
1082 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1083 if (strcasecmp (cipher
, ciphers
[i
]->name
) == 0)
1084 return ciphers
[i
]->type
;
1089 static Shishi_random_to_key_function
1090 _shishi_cipher_random_to_key (int32_t type
)
1094 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1095 if (type
== ciphers
[i
]->type
)
1096 return ciphers
[i
]->random2key
;
1101 static Shishi_string_to_key_function
1102 _shishi_cipher_string_to_key (int32_t type
)
1106 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1107 if (type
== ciphers
[i
]->type
)
1108 return ciphers
[i
]->string2key
;
1113 static Shishi_encrypt_function
1114 _shishi_cipher_encrypt (int32_t type
)
1118 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1119 if (type
== ciphers
[i
]->type
)
1120 return ciphers
[i
]->encrypt
;
1125 static Shishi_decrypt_function
1126 _shishi_cipher_decrypt (int32_t type
)
1130 for (i
= 0; i
< sizeof (ciphers
) / sizeof (ciphers
[0]); i
++)
1131 if (type
== ciphers
[i
]->type
)
1132 return ciphers
[i
]->decrypt
;
1142 Shishi_checksum_function checksum
;
1144 typedef struct checksuminfo checksuminfo
;
1146 static checksuminfo md4_info
= {
1153 static checksuminfo md5_info
= {
1160 static checksuminfo md5_gss_info
= {
1161 SHISHI_RSA_MD5_DES_GSS
,
1167 static checksuminfo hmac_sha1_des3_kd_info
= {
1168 SHISHI_HMAC_SHA1_DES3_KD
,
1169 "hmac-sha1-des3-kd",
1174 static checksuminfo hmac_sha1_96_aes128_info
= {
1175 SHISHI_HMAC_SHA1_96_AES128
,
1176 "hmac-sha1-96-aes128",
1181 static checksuminfo hmac_sha1_96_aes256_info
= {
1182 SHISHI_HMAC_SHA1_96_AES256
,
1183 "hmac-sha1-96-aes256",
1188 static checksuminfo
*checksums
[] = {
1192 &hmac_sha1_des3_kd_info
,
1193 &hmac_sha1_96_aes128_info
,
1194 &hmac_sha1_96_aes256_info
1198 * shishi_checksum_supported_p:
1199 * @type: encryption type, see Shishi_etype.
1201 * Return value: Return 0 iff checksum is unsupported.
1204 shishi_checksum_supported_p (int32_t type
)
1208 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1209 if (type
== checksums
[i
]->type
)
1216 * shishi_checksum_name:
1217 * @type: encryption type, see Shishi_etype.
1219 * Return value: Return name of checksum type,
1220 * e.g. "hmac-sha1-96-aes256", as defined in the standards.
1223 shishi_checksum_name (int32_t type
)
1228 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1230 if (type
== checksums
[i
]->type
)
1231 return checksums
[i
]->name
;
1234 asprintf (&p
, "unknown checksum %d", type
);
1239 * shishi_checksum_cksumlen:
1240 * @type: encryption type, see Shishi_etype.
1242 * Return value: Return length of checksum used for the encryption type,
1243 * as defined in the standards.
1246 shishi_checksum_cksumlen (int32_t type
)
1250 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1251 if (type
== checksums
[i
]->type
)
1252 return checksums
[i
]->cksumlen
;
1258 * shishi_checksum_parse:
1259 * @checksum: name of checksum type, e.g. "hmac-sha1-96-aes256".
1261 * Return value: Return checksum type corresponding to a string.
1264 shishi_checksum_parse (const char *checksum
)
1269 i
= strtol (checksum
, &endptr
, 0);
1271 if (endptr
!= checksum
)
1274 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1275 if (strcasecmp (checksum
, checksums
[i
]->name
) == 0)
1276 return checksums
[i
]->type
;
1281 static Shishi_checksum_function
1282 _shishi_checksum (int32_t type
)
1286 for (i
= 0; i
< sizeof (checksums
) / sizeof (checksums
[0]); i
++)
1287 if (type
== checksums
[i
]->type
)
1288 return checksums
[i
]->checksum
;
1294 * shishi_string_to_key:
1295 * @handle: shishi handle as allocated by shishi_init().
1296 * @keytype: cryptographic encryption type, see Shishi_etype.
1297 * @password: input array with password.
1298 * @passwordlen: length of input array with password.
1299 * @salt: input array with salt.
1300 * @saltlen: length of input array with salt.
1301 * @parameter: input array with opaque encryption type specific information.
1302 * @outkey: allocated key handle that will contain new key.
1304 * Derive key from a string (password) and salt (commonly
1305 * concatenation of realm and principal) for specified key type, and
1306 * set the type and value in the given key to the computed values.
1307 * The parameter value is specific for each keytype, and can be set if
1308 * the parameter information is not available.
1310 * Return value: Returns %SHISHI_OK iff successful.
1313 shishi_string_to_key (Shishi
* handle
,
1315 const char *password
, size_t passwordlen
,
1316 const char *salt
, size_t saltlen
,
1317 const char *parameter
, Shishi_key
* outkey
)
1319 Shishi_string_to_key_function string2key
;
1322 shishi_key_type_set (outkey
, keytype
);
1324 if (VERBOSECRYPTO (handle
))
1326 printf ("string_to_key (%s, password, salt)\n",
1327 shishi_key_name (outkey
));
1328 printf ("\t ;; password:\n");
1329 escapeprint (password
, passwordlen
);
1330 hexprint (password
, passwordlen
);
1332 printf ("\t ;; salt:\n");
1333 escapeprint (salt
, saltlen
);
1334 hexprint (salt
, saltlen
);
1338 string2key
= _shishi_cipher_string_to_key (shishi_key_type (outkey
));
1339 if (string2key
== NULL
)
1341 shishi_error_printf (handle
, "Unsupported keytype %d",
1342 shishi_key_type (outkey
));
1343 return SHISHI_CRYPTO_ERROR
;
1346 res
= (*string2key
) (handle
, password
, passwordlen
,
1347 salt
, saltlen
, parameter
, outkey
);
1349 if (VERBOSECRYPTO (handle
))
1351 printf ("\t ;; string_to_key key:\n");
1352 hexprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
1354 binprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
1362 * shishi_random_to_key:
1363 * @handle: shishi handle as allocated by shishi_init().
1364 * @keytype: cryptographic encryption type, see Shishi_etype.
1365 * @random: input array with random data.
1366 * @randomlen: length of input array with random data.
1367 * @outkey: allocated key handle that will contain new key.
1369 * Derive key from random data for specified key type, and set the
1370 * type and value in the given key to the computed values.
1372 * Return value: Returns %SHISHI_OK iff successful.
1375 shishi_random_to_key (Shishi
* handle
,
1377 char *random
, size_t randomlen
, Shishi_key
* outkey
)
1379 Shishi_random_to_key_function random2key
;
1382 shishi_key_type_set (outkey
, keytype
);
1384 if (VERBOSECRYPTO (handle
))
1386 printf ("random_to_key (%s, random)\n", shishi_key_name (outkey
));
1387 printf ("\t ;; random:\n");
1388 hexprint (random
, randomlen
);
1390 binprint (random
, randomlen
);
1394 random2key
= _shishi_cipher_random_to_key (keytype
);
1395 if (random2key
== NULL
)
1397 shishi_error_printf (handle
, "Unsupported random_to_key() ekeytype %d",
1399 return SHISHI_CRYPTO_ERROR
;
1402 res
= (*random2key
) (handle
, random
, randomlen
, outkey
);
1404 if (VERBOSECRYPTO (handle
))
1406 printf ("\t ;; random_to_key key:\n");
1407 hexprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
1409 binprint (shishi_key_value (outkey
), shishi_key_length (outkey
));
1418 * @handle: shishi handle as allocated by shishi_init().
1419 * @key: key to encrypt with.
1420 * @keyusage: integer specifying what this key is encrypting.
1421 * @cksumtype: the checksum algorithm to use.
1422 * @in: input array with data to integrity protect.
1423 * @inlen: size of input array with data to integrity protect.
1424 * @out: output array with integrity protected data.
1425 * @outlen: on input, holds maximum size of output array, on output,
1426 * holds actual size of output array.
1428 * Integrity protect data using key, possibly altered by supplied key
1429 * usage. If key usage is 0, no key derivation is used.
1431 * If OUT is NULL, this functions only set OUTLEN. This usage may be
1432 * used by the caller to allocate the proper buffer size.
1434 * Return value: Returns %SHISHI_OK iff successful.
1437 shishi_checksum (Shishi
* handle
,
1441 char *in
, size_t inlen
, char **out
, size_t * outlen
)
1443 Shishi_checksum_function checksum
;
1446 if (VERBOSECRYPTO (handle
))
1448 printf ("checksum (%s, %d, in, out)\n",
1449 shishi_key_name (key
), cksumtype
);
1450 printf ("\t ;; key (%d):\n", shishi_key_length (key
));
1451 hexprint (shishi_key_value (key
), shishi_key_length (key
));
1453 printf ("\t ;; in:\n");
1454 escapeprint (in
, inlen
);
1455 hexprint (in
, inlen
);
1460 cksumtype
= shishi_cipher_defaultcksumtype (shishi_key_type (key
));
1462 checksum
= _shishi_checksum (cksumtype
);
1463 if (checksum
== NULL
)
1465 shishi_error_printf (handle
, "Unsupported checksum type %d", cksumtype
);
1466 return SHISHI_CRYPTO_ERROR
;
1469 res
= (*checksum
) (handle
, key
, keyusage
, cksumtype
, in
, inlen
, out
, outlen
);
1471 if (VERBOSECRYPTO (handle
))
1473 printf ("\t ;; checksum out:\n");
1474 escapeprint (*out
, *outlen
);
1475 hexprint (*out
, *outlen
);
1483 * shishi_encrypt_iv_etype:
1484 * @handle: shishi handle as allocated by shishi_init().
1485 * @key: key to encrypt with.
1486 * @keyusage: integer specifying what this key is encrypting.
1487 * @etype: integer specifying what decryption method to use.
1488 * @iv: input array with initialization vector.
1489 * @ivlen: size of input array with initialization vector.
1490 * @in: input array with data to encrypt.
1491 * @inlen: size of input array with data to encrypt.
1492 * @out: output array with encrypted data.
1493 * @outlen: on input, holds maximum size of output array, on output,
1494 * holds actual size of output array.
1496 * Encrypts data using key, possibly altered by supplied key usage.
1497 * If key usage is 0, no key derivation is used.
1499 * If OUT is NULL, this functions only set OUTLEN. This usage may be
1500 * used by the caller to allocate the proper buffer size.
1502 * Return value: Returns %SHISHI_OK iff successful.
1505 shishi_encrypt_iv_etype (Shishi
* handle
,
1509 char *iv
, size_t ivlen
,
1510 char *in
, size_t inlen
, char **out
, size_t * outlen
)
1512 Shishi_encrypt_function encrypt
;
1515 if (VERBOSECRYPTO (handle
))
1517 printf ("encrypt (type=%s, usage=%d, key, in)\n",
1518 shishi_key_name (key
), keyusage
);
1519 printf ("\t ;; key (%d):\n", shishi_key_length (key
));
1520 hexprint (shishi_key_value (key
), shishi_key_length (key
));
1522 printf ("\t ;; in (%d):\n", inlen
);
1523 escapeprint (in
, inlen
);
1524 hexprint (in
, inlen
);
1528 encrypt
= _shishi_cipher_encrypt (etype
);
1529 if (encrypt
== NULL
)
1531 shishi_error_printf (handle
, "Unsupported keytype %d",
1532 shishi_key_type (key
));
1533 return SHISHI_CRYPTO_ERROR
;
1536 res
= (*encrypt
) (handle
, key
, keyusage
, iv
, ivlen
, in
, inlen
, out
, outlen
);
1538 if (VERBOSECRYPTO (handle
))
1540 printf ("\t ;; encrypt out:\n");
1541 escapeprint (*out
, *outlen
);
1542 hexprint (*out
, *outlen
);
1551 * @handle: shishi handle as allocated by shishi_init().
1552 * @key: key to encrypt with.
1553 * @keyusage: integer specifying what this key is encrypting.
1554 * @in: input array with data to encrypt.
1555 * @inlen: size of input array with data to encrypt.
1556 * @out: output array with encrypted data.
1557 * @outlen: on input, holds maximum size of output array, on output,
1558 * holds actual size of output array.
1560 * Encrypts data using key, possibly altered by supplied key usage.
1561 * If key usage is 0, no key derivation is used.
1563 * If OUT is NULL, this functions only set OUTLEN. This usage may be
1564 * used by the caller to allocate the proper buffer size.
1566 * Return value: Returns %SHISHI_OK iff successful.
1569 shishi_encrypt_iv (Shishi
* handle
,
1572 char *iv
, size_t ivlen
,
1573 char *in
, size_t inlen
, char **out
, size_t * outlen
)
1575 return shishi_encrypt_iv_etype (handle
, key
, keyusage
,
1576 shishi_key_type (key
), NULL
, 0, in
, inlen
,
1582 * @handle: shishi handle as allocated by shishi_init().
1583 * @key: key to encrypt with.
1584 * @keyusage: integer specifying what this key is encrypting.
1585 * @in: input array with data to encrypt.
1586 * @inlen: size of input array with data to encrypt.
1587 * @out: output array with encrypted data.
1588 * @outlen: on input, holds maximum size of output array, on output,
1589 * holds actual size of output array.
1591 * Encrypts data using key, possibly altered by supplied key usage.
1592 * If key usage is 0, no key derivation is used.
1594 * If OUT is NULL, this functions only set OUTLEN. This usage may be
1595 * used by the caller to allocate the proper buffer size.
1597 * Return value: Returns %SHISHI_OK iff successful.
1600 shishi_encrypt (Shishi
* handle
,
1603 char *in
, size_t inlen
, char **out
, size_t * outlen
)
1605 return shishi_encrypt_iv (handle
, key
, keyusage
, NULL
, 0,
1606 in
, inlen
, out
, outlen
);
1610 * shishi_decrypt_iv_etype:
1611 * @handle: shishi handle as allocated by shishi_init().
1612 * @key: key to decrypt with.
1613 * @keyusage: integer specifying what this key is decrypting.
1614 * @etype: integer specifying what decryption method to use.
1615 * @iv: input array with initialization vector.
1616 * @ivlen: size of input array with initialization vector.
1617 * @in: input array with data to decrypt.
1618 * @inlen: size of input array with data to decrypt.
1619 * @out: output array with decrypted data.
1620 * @outlen: on input, holds maximum size of output array, on output,
1621 * holds actual size of output array.
1623 * Decrypts data using key, possibly altered by supplied key usage.
1624 * If key usage is 0, no key derivation is used.
1626 * If OUT is NULL, this functions only set OUTLEN. This usage may be
1627 * used by the caller to allocate the proper buffer size.
1629 * Return value: Returns %SHISHI_OK iff successful.
1632 shishi_decrypt_iv_etype (Shishi
* handle
,
1636 char *iv
, size_t ivlen
,
1637 char *in
, size_t inlen
, char **out
, size_t * outlen
)
1639 Shishi_decrypt_function decrypt
;
1642 if (VERBOSECRYPTO (handle
))
1644 printf ("decrypt (type=%s, usage=%d, key, in, out)\n",
1645 shishi_key_name (key
), keyusage
);
1646 printf ("\t ;; key (%d):\n", shishi_key_length (key
));
1647 hexprint (shishi_key_value (key
), shishi_key_length (key
));
1649 printf ("\t ;; in (%d):\n", inlen
);
1650 escapeprint (in
, inlen
);
1651 hexprint (in
, inlen
);
1655 decrypt
= _shishi_cipher_decrypt (etype
);
1656 if (decrypt
== NULL
)
1658 shishi_error_printf (handle
, "Unsupported keytype %d",
1659 shishi_key_type (key
));
1660 return SHISHI_CRYPTO_ERROR
;
1663 res
= (*decrypt
) (handle
, key
, keyusage
, iv
, ivlen
, in
, inlen
, out
, outlen
);
1665 if (VERBOSECRYPTO (handle
))
1667 printf ("\t ;; decrypt out:\n");
1668 escapeprint (*out
, *outlen
);
1669 hexprint (*out
, *outlen
);
1677 * shishi_decrypt_iv:
1678 * @handle: shishi handle as allocated by shishi_init().
1679 * @key: key to decrypt with.
1680 * @keyusage: integer specifying what this key is decrypting.
1681 * @iv: input array with initialization vector.
1682 * @ivlen: size of input array with initialization vector.
1683 * @in: input array with data to decrypt.
1684 * @inlen: size of input array with data to decrypt.
1685 * @out: output array with decrypted data.
1686 * @outlen: on input, holds maximum size of output array, on output,
1687 * holds actual size of output array.
1689 * Decrypts data using key, possibly altered by supplied key usage.
1690 * If key usage is 0, no key derivation is used.
1692 * If OUT is NULL, this functions only set OUTLEN. This usage may be
1693 * used by the caller to allocate the proper buffer size.
1695 * Return value: Returns %SHISHI_OK iff successful.
1698 shishi_decrypt_iv (Shishi
* handle
,
1701 char *iv
, size_t ivlen
,
1702 char *in
, size_t inlen
, char **out
, size_t * outlen
)
1704 return shishi_decrypt_iv_etype (handle
, key
, keyusage
,
1705 shishi_key_type (key
),
1706 iv
, ivlen
, in
, inlen
, out
, outlen
);
1711 * @handle: shishi handle as allocated by shishi_init().
1712 * @key: key to decrypt with.
1713 * @keyusage: integer specifying what this key is decrypting.
1714 * @in: input array with data to decrypt.
1715 * @inlen: size of input array with data to decrypt.
1716 * @out: output array with decrypted data.
1717 * @outlen: on input, holds maximum size of output array, on output,
1718 * holds actual size of output array.
1720 * Decrypts data using key, possibly altered by supplied key usage.
1721 * If key usage is 0, no key derivation is used.
1723 * If OUT is NULL, this functions only set OUTLEN. This usage may be
1724 * used by the caller to allocate the proper buffer size.
1726 * Return value: Returns %SHISHI_OK iff successful.
1729 shishi_decrypt (Shishi
* handle
,
1732 char *in
, size_t inlen
, char **out
, size_t * outlen
)
1734 return shishi_decrypt_iv (handle
, key
, keyusage
, NULL
, 0,
1735 in
, inlen
, out
, outlen
);
1740 * @handle: shishi handle as allocated by shishi_init().
1741 * @data: output array to be filled with random data.
1742 * @datalen: size of output array.
1744 * Store cryptographically strong random data of given size in the
1747 * Return value: Returns %SHISHI_OK iff successful.
1750 shishi_randomize (Shishi
* handle
, char *data
, size_t datalen
)
1753 gcry_randomize (data
, datalen
, GCRY_STRONG_RANDOM
);
1758 device
= "/dev/urandom";
1760 fd
= open (device
, O_RDONLY
);
1763 shishi_error_printf(handle
, "Could not open random device: %s\n",
1765 return SHISHI_FOPEN_ERROR
;
1769 read (fd
, data
, datalen
);
1779 * @handle: shishi handle as allocated by shishi_init().
1780 * @in: input array with data to decrypt.
1781 * @inlen: size of input array with data to decrypt ("M").
1782 * @out: output array with decrypted data.
1783 * @outlen: size of output array ("N").
1785 * Fold data into a fixed length output array, with the intent to give
1786 * each input bit approximately equal weight in determining the value
1787 * of each output bit.
1789 * The algorithm is from "A Better Key Schedule For DES-like Ciphers"
1790 * by Uri Blumenthal and Steven M. Bellovin,
1791 * <URL:http://www.research.att.com/~smb/papers/ides.pdf>, although
1792 * the sample vectors provided by the paper are incorrect.
1794 * Return value: Returns %SHISHI_OK iff successful.
1797 shishi_n_fold (Shishi
* handle
,
1798 char *in
, size_t inlen
, char *out
, size_t outlen
)
1808 To n-fold a number X, replicate the input value to a length that is
1809 the least common multiple of n and the length of X. Before each
1810 repetition, the input is rotated to the right by 13 bit
1811 positions. The successive n-bit chunks are added together using
1812 1's-complement addition (that is, addition with end-around carry)
1813 to yield a n-bit result denoted <X>_n.
1816 a
= (char *) malloc (m
);
1818 return SHISHI_MALLOC_ERROR
;
1823 if (VERBOSECRYPTO (handle
))
1825 printf ("%d-fold (string)\n", n
* 8);
1826 printf ("\t ;; string length %d bytes %d bits\n", m
, m
* 8);
1830 printf ("\t ;; lcm(%d, %d) = lcm(%d, %d) = %d\n",
1831 8 * m
, 8 * n
, m
, n
, lcmmn
);
1835 buf
= (char *) malloc (lcmmn
);
1837 return SHISHI_MALLOC_ERROR
;
1839 /* Replicate the input th the LCMMN length */
1840 for (i
= 0; i
< (lcmmn
/ m
); i
++)
1842 if (VERBOSECRYPTO (handle
))
1844 printf ("\t ;; %d-th replication\n", i
+ 1);
1845 printf ("string = rot13(string)\n");
1848 memcpy ((char *) &buf
[i
* m
], a
, m
);
1849 rot13 (handle
, a
, a
, m
);
1851 if (VERBOSECRYPTO (handle
))
1855 memset (out
, 0, n
); /* just in case */
1857 if (VERBOSECRYPTO (handle
))
1859 printf ("\t ;; replicated string (length %d):\n", lcmmn
);
1860 hexprint (buf
, lcmmn
);
1862 binprint (buf
, lcmmn
);
1864 printf ("sum = 0\n");
1867 /* Now we view the buf as set of n-byte strings
1868 Add the n-byte long chunks together, using
1869 one's complement addition, storing the
1870 result in the output string. */
1872 for (i
= 0; i
< (lcmmn
/ n
); i
++)
1874 if (VERBOSECRYPTO (handle
))
1876 printf ("\t ;; %d-th one's complement addition sum\n", i
+ 1);
1877 printf ("\t ;; sum:\n");
1882 printf ("\t ;; A (offset %d):\n", i
* n
);
1883 hexprint (&buf
[i
* n
], n
);
1885 binprint (&buf
[i
* n
], n
);
1887 printf ("sum = ocadd(sum, A);\n");
1890 ocadd (out
, (char *) &buf
[i
* n
], out
, n
);
1892 if (VERBOSECRYPTO (handle
))
1894 printf ("\t ;; sum:\n");
1903 if (VERBOSECRYPTO (handle
))
1905 printf ("\t ;; nfold\n");
1919 #define MAX_DR_CONSTANT 1024
1923 * @handle: shishi handle as allocated by shishi_init().
1924 * @etype: cryptographic encryption type, see Shishi_etype.
1925 * @key: input array with cryptographic key to use.
1926 * @keylen: size of input array with cryptographic key.
1927 * @constant: input array with the constant string.
1928 * @constantlen: size of input array with the constant string.
1929 * @derivedrandom: output array with derived random data.
1930 * @derivedrandomlen: size of output array with derived random data.
1932 * Derive "random" data from a key and a constant thusly:
1933 * DR(KEY, CONSTANT) = TRUNCATE(DERIVEDRANDOMLEN,
1934 * SHISHI_ENCRYPT(KEY, CONSTANT)).
1936 * Return value: Returns %SHISHI_OK iff successful.
1939 shishi_dr (Shishi
* handle
,
1941 char *constant
, size_t constantlen
,
1942 char *derivedrandom
, size_t derivedrandomlen
)
1945 char plaintext
[MAX_DR_CONSTANT
];
1946 char nfoldconstant
[MAX_DR_CONSTANT
];
1947 int blocksize
= shishi_cipher_blocksize (shishi_key_type (key
));
1948 size_t totlen
, cipherlen
;
1951 if (VERBOSECRYPTO (handle
))
1953 printf ("dr (%s, key, constant, %d)\n",
1954 shishi_cipher_name (shishi_key_type (key
)), derivedrandomlen
);
1955 printf ("\t ;; key (length %d):\n", shishi_key_type (key
));
1956 hexprint (shishi_key_value (key
), shishi_key_type (key
));
1958 binprint (shishi_key_value (key
), shishi_key_type (key
));
1960 printf ("\t ;; constant %s':\n", constant
);
1961 escapeprint (constant
, constantlen
);
1962 hexprint (constant
, constantlen
);
1964 binprint (constant
, constantlen
);
1969 if (constantlen
> MAX_DR_CONSTANT
)
1970 return SHISHI_TOO_SMALL_BUFFER
;
1972 if (constantlen
== blocksize
)
1974 memcpy (nfoldconstant
, constant
, constantlen
);
1978 res
= shishi_n_fold (handle
, constant
, constantlen
, nfoldconstant
,
1980 if (res
!= SHISHI_OK
)
1984 if (VERBOSECRYPTO (handle
))
1986 printf ("\t ;; possibly nfolded constant (length %d):\n", blocksize
);
1987 escapeprint (nfoldconstant
, blocksize
);
1988 hexprint (nfoldconstant
, blocksize
);
1990 binprint (nfoldconstant
, blocksize
);
1994 memcpy (plaintext
, nfoldconstant
, blocksize
);
1999 res
= shishi_encrypt (handle
, key
, 0, plaintext
, blocksize
,
2000 &cipher
, &cipherlen
);
2001 if (res
!= SHISHI_OK
)
2003 if (cipherlen
!= blocksize
)
2004 return SHISHI_CRYPTO_ERROR
;
2005 memcpy (derivedrandom
+ totlen
, cipher
, cipherlen
);
2006 memcpy (plaintext
, cipher
, cipherlen
);
2008 totlen
+= cipherlen
;
2010 while (totlen
< derivedrandomlen
);
2012 if (VERBOSECRYPTO (handle
))
2014 printf ("\t ;; derived random (length %d):\n", derivedrandomlen
);
2015 hexprint (derivedrandom
, derivedrandomlen
);
2017 binprint (derivedrandom
, derivedrandomlen
);
2026 * @handle: shishi handle as allocated by shishi_init().
2027 * @etype: cryptographic encryption type, see Shishi_etype.
2028 * @key: input array with cryptographic key to use.
2029 * @keylen: size of input array with cryptographic key.
2030 * @constant: input array with the constant string.
2031 * @constantlen: size of input array with the constant string.
2032 * @derivedkey: output array with derived key.
2033 * @derivedkeylen: size of output array with derived key.
2035 * Derive a key from a key and a constant thusly:
2036 * DK(KEY, CONSTANT) = SHISHI_RANDOM-TO-KEY(SHISHI_DR(KEY, CONSTANT)).
2038 * Return value: Returns %SHISHI_OK iff successful.
2041 shishi_dk (Shishi
* handle
,
2043 char *constant
, int constantlen
, Shishi_key
* derivedkey
)
2045 char random
[MAX_RANDOM_LEN
];
2048 if (VERBOSECRYPTO (handle
))
2050 printf ("dk (%s, key, constant)\n", shishi_key_name (key
));
2051 printf ("\t ;; key (length %d):\n", shishi_key_length (key
));
2052 hexprint (shishi_key_value (key
), shishi_key_length (key
));
2054 binprint (shishi_key_value (key
), shishi_key_length (key
));
2056 printf ("\t ;; constant:\n");
2057 escapeprint (constant
, constantlen
);
2058 hexprint (constant
, constantlen
);
2060 binprint (constant
, constantlen
);
2065 shishi_key_type_set (derivedkey
, shishi_key_type (key
));
2067 res
= shishi_dr (handle
, key
, constant
, constantlen
, random
,
2068 shishi_key_length (derivedkey
));
2069 if (res
!= SHISHI_OK
)
2072 res
= shishi_random_to_key (handle
, shishi_key_type (derivedkey
),
2073 random
, shishi_key_length (derivedkey
),
2075 if (res
!= SHISHI_OK
)