1
/* ---- SYMMETRIC KEY STUFF -----
3
* We put each of the ciphers scheduled keys in their own structs then we put all of
4
* the key formats in one union. This makes the function prototypes easier to use.
28
unsigned char K[33][16];
35
ulong32 eK[60], dK[60];
42
unsigned long A[32], B[32];
49
ulong32 S[4][256], K[40];
54
unsigned char S[32], start;
60
#define SAFER_K64_DEFAULT_NOF_ROUNDS 6
61
#define SAFER_K128_DEFAULT_NOF_ROUNDS 10
62
#define SAFER_SK64_DEFAULT_NOF_ROUNDS 8
63
#define SAFER_SK128_DEFAULT_NOF_ROUNDS 10
64
#define SAFER_MAX_NOF_ROUNDS 13
65
#define SAFER_BLOCK_LEN 8
66
#define SAFER_KEY_LEN (1 + SAFER_BLOCK_LEN * (1 + 2 * SAFER_MAX_NOF_ROUNDS))
67
typedef unsigned char safer_block_t[SAFER_BLOCK_LEN];
68
typedef unsigned char safer_key_t[SAFER_KEY_LEN];
69
struct safer_key { safer_key_t key; };
73
struct rc2_key { unsigned xkey[64]; };
78
ulong32 ek[32], dk[32];
82
ulong32 ek[3][32], dk[3][32];
88
ulong32 K[32], keylen;
100
unsigned char key[10];
106
ulong64 roundKeyEnc[8 + 1];
107
ulong64 roundKeyDec[8 + 1];
115
ulong32 roundKeyEnc[18 + 1][4];
116
ulong32 roundKeyDec[18 + 1][4];
120
typedef union Symmetric_key {
123
struct des3_key des3;
129
struct safer_key safer;
132
struct twofish_key twofish;
135
struct blowfish_key blowfish;
144
struct saferp_key saferp;
147
struct rijndael_key rijndael;
150
struct xtea_key xtea;
153
struct cast5_key cast5;
156
struct noekeon_key noekeon;
159
struct skipjack_key skipjack;
162
struct khazad_key khazad;
165
struct anubis_key anubis;
170
/* A block cipher ECB structure */
172
/** The index of the cipher chosen */
174
/** The block size of the given cipher */
176
/** The scheduled key */
180
/* A block cipher CFB structure */
182
/** The index of the cipher chosen */
184
/** The block size of the given cipher */
186
/** The padding offset */
188
/** The current IV */
189
unsigned char IV[MAXBLOCKSIZE],
190
/** The pad used to encrypt/decrypt */
192
/** The scheduled key */
196
/* A block cipher OFB structure */
198
/** The index of the cipher chosen */
200
/** The block size of the given cipher */
202
/** The padding offset */
204
/** The current IV */
205
unsigned char IV[MAXBLOCKSIZE];
206
/** The scheduled key */
210
/* A block cipher CBC structure */
212
/** The index of the cipher chosen */
214
/** The block size of the given cipher */
216
/** The current IV */
217
unsigned char IV[MAXBLOCKSIZE];
218
/** The scheduled key */
222
/* A block cipher CTR structure */
224
/** The index of the cipher chosen */
226
/** The block size of the given cipher */
228
/** The padding offset */
230
/** The mode (endianess) of the CTR, 0==little, 1==big */
233
unsigned char ctr[MAXBLOCKSIZE],
234
/** The pad used to encrypt/decrypt */
236
/** The scheduled key */
240
/* cipher descriptor table, last entry has "name == NULL" to mark the end of table */
241
extern struct ltc_cipher_descriptor {
242
/** name of cipher */
246
/** min keysize (octets) */
248
/** max keysize (octets) */
250
/** block size (octets) */
252
/** default number of rounds */
255
@param key The input symmetric key
256
@param keylen The length of the input key (octets)
257
@param num_rounds The requested number of rounds (0==default)
258
@param skey [out] The destination of the scheduled key
259
@return CRYPT_OK if successful
261
int (*setup)(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
263
@param pt The plaintext
264
@param ct [out] The ciphertext
265
@param skey The scheduled key
267
void (*ecb_encrypt)(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
269
@param ct The ciphertext
270
@param pt [out] The plaintext
271
@param skey The scheduled key
273
void (*ecb_decrypt)(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
274
/** Test the block cipher
275
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
279
/** Terminate the context
280
@param skey The scheduled key
282
void (*done)(symmetric_key *skey);
284
/** Determine a key size
285
@param keysize [in/out] The size of the key desired and the suggested size
286
@return CRYPT_OK if successful
288
int (*keysize)(int *keysize);
291
/** Accelerated ECB encryption
294
@param blocks The number of complete blocks to process
295
@param skey The scheduled key context
297
void (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, symmetric_key *skey);
299
/** Accelerated ECB decryption
302
@param blocks The number of complete blocks to process
303
@param skey The scheduled key context
305
void (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, symmetric_key *skey);
307
/** Accelerated CBC encryption
310
@param blocks The number of complete blocks to process
311
@param IV The initial value (input/output)
312
@param skey The scheduled key context
314
void (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
316
/** Accelerated CBC decryption
319
@param blocks The number of complete blocks to process
320
@param IV The initial value (input/output)
321
@param skey The scheduled key context
323
void (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
325
/** Accelerated CTR encryption
328
@param blocks The number of complete blocks to process
329
@param IV The initial value (input/output)
330
@param mode little or big endian counter (mode=0 or mode=1)
331
@param skey The scheduled key context
333
void (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, symmetric_key *skey);
335
/** Accelerated CCM packet (one-shot)
336
@param key The secret key to use
337
@param keylen The length of the secret key (octets)
338
@param nonce The session nonce [use once]
339
@param noncelen The length of the nonce
340
@param header The header for the session
341
@param headerlen The length of the header (octets)
342
@param pt [out] The plaintext
343
@param ptlen The length of the plaintext (octets)
344
@param ct [out] The ciphertext
345
@param tag [out] The destination tag
346
@param taglen [in/out] The max size and resulting size of the authentication tag
347
@param direction Encrypt or Decrypt direction (0 or 1)
348
@return CRYPT_OK if successful
350
void (*accel_ccm_memory)(
351
const unsigned char *key, unsigned long keylen,
352
const unsigned char *nonce, unsigned long noncelen,
353
const unsigned char *header, unsigned long headerlen,
354
unsigned char *pt, unsigned long ptlen,
356
unsigned char *tag, unsigned long *taglen,
359
/** Accelerated GCM packet (one shot)
360
@param key The secret key
361
@param keylen The length of the secret key
362
@param IV The initial vector
363
@param IVlen The length of the initial vector
364
@param adata The additional authentication data (header)
365
@param adatalen The length of the adata
366
@param pt The plaintext
367
@param ptlen The length of the plaintext (ciphertext length is the same)
368
@param ct The ciphertext
369
@param tag [out] The MAC tag
370
@param taglen [in/out] The MAC tag length
371
@param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)
373
void (*accel_gcm_memory)(
374
const unsigned char *key, unsigned long keylen,
375
const unsigned char *IV, unsigned long IVlen,
376
const unsigned char *adata, unsigned long adatalen,
377
unsigned char *pt, unsigned long ptlen,
379
unsigned char *tag, unsigned long *taglen,
381
} cipher_descriptor[];
384
int blowfish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
385
void blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
386
void blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
387
int blowfish_test(void);
388
void blowfish_done(symmetric_key *skey);
389
int blowfish_keysize(int *keysize);
390
extern const struct ltc_cipher_descriptor blowfish_desc;
394
int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
395
void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
396
void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
398
void rc5_done(symmetric_key *skey);
399
int rc5_keysize(int *keysize);
400
extern const struct ltc_cipher_descriptor rc5_desc;
404
int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
405
void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
406
void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
408
void rc6_done(symmetric_key *skey);
409
int rc6_keysize(int *keysize);
410
extern const struct ltc_cipher_descriptor rc6_desc;
414
int rc2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
415
void rc2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
416
void rc2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
418
void rc2_done(symmetric_key *skey);
419
int rc2_keysize(int *keysize);
420
extern const struct ltc_cipher_descriptor rc2_desc;
424
int saferp_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
425
void saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
426
void saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
427
int saferp_test(void);
428
void saferp_done(symmetric_key *skey);
429
int saferp_keysize(int *keysize);
430
extern const struct ltc_cipher_descriptor saferp_desc;
434
int safer_k64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
435
int safer_sk64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
436
int safer_k128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
437
int safer_sk128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
438
void safer_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key);
439
void safer_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key);
440
int safer_k64_test(void);
441
int safer_sk64_test(void);
442
int safer_sk128_test(void);
443
void safer_done(symmetric_key *skey);
444
int safer_64_keysize(int *keysize);
445
int safer_128_keysize(int *keysize);
446
extern const struct ltc_cipher_descriptor safer_k64_desc, safer_k128_desc, safer_sk64_desc, safer_sk128_desc;
451
/* make aes an alias */
452
#define aes_setup rijndael_setup
453
#define aes_ecb_encrypt rijndael_ecb_encrypt
454
#define aes_ecb_decrypt rijndael_ecb_decrypt
455
#define aes_test rijndael_test
456
#define aes_done rijndael_done
457
#define aes_keysize rijndael_keysize
459
#define aes_enc_setup rijndael_enc_setup
460
#define aes_enc_ecb_encrypt rijndael_enc_ecb_encrypt
461
#define aes_enc_keysize rijndael_enc_keysize
463
int rijndael_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
464
void rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
465
void rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
466
int rijndael_test(void);
467
void rijndael_done(symmetric_key *skey);
468
int rijndael_keysize(int *keysize);
469
int rijndael_enc_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
470
void rijndael_enc_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
471
void rijndael_enc_done(symmetric_key *skey);
472
int rijndael_enc_keysize(int *keysize);
473
extern const struct ltc_cipher_descriptor rijndael_desc, aes_desc;
474
extern const struct ltc_cipher_descriptor rijndael_enc_desc, aes_enc_desc;
478
int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
479
void xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
480
void xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
482
void xtea_done(symmetric_key *skey);
483
int xtea_keysize(int *keysize);
484
extern const struct ltc_cipher_descriptor xtea_desc;
488
int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
489
void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
490
void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
491
int twofish_test(void);
492
void twofish_done(symmetric_key *skey);
493
int twofish_keysize(int *keysize);
494
extern const struct ltc_cipher_descriptor twofish_desc;
498
int des_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
499
void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
500
void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
502
void des_done(symmetric_key *skey);
503
int des_keysize(int *keysize);
504
int des3_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
505
void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
506
void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
508
void des3_done(symmetric_key *skey);
509
int des3_keysize(int *keysize);
510
extern const struct ltc_cipher_descriptor des_desc, des3_desc;
514
int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
515
void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
516
void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
517
int cast5_test(void);
518
void cast5_done(symmetric_key *skey);
519
int cast5_keysize(int *keysize);
520
extern const struct ltc_cipher_descriptor cast5_desc;
524
int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
525
void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
526
void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
527
int noekeon_test(void);
528
void noekeon_done(symmetric_key *skey);
529
int noekeon_keysize(int *keysize);
530
extern const struct ltc_cipher_descriptor noekeon_desc;
534
int skipjack_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
535
void skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
536
void skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
537
int skipjack_test(void);
538
void skipjack_done(symmetric_key *skey);
539
int skipjack_keysize(int *keysize);
540
extern const struct ltc_cipher_descriptor skipjack_desc;
544
int khazad_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
545
void khazad_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
546
void khazad_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
547
int khazad_test(void);
548
void khazad_done(symmetric_key *skey);
549
int khazad_keysize(int *keysize);
550
extern const struct ltc_cipher_descriptor khazad_desc;
554
int anubis_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
555
void anubis_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
556
void anubis_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
557
int anubis_test(void);
558
void anubis_done(symmetric_key *skey);
559
int anubis_keysize(int *keysize);
560
extern const struct ltc_cipher_descriptor anubis_desc;
564
int ecb_start(int cipher, const unsigned char *key,
565
int keylen, int num_rounds, symmetric_ECB *ecb);
566
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb);
567
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb);
568
int ecb_done(symmetric_ECB *ecb);
572
int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key,
573
int keylen, int num_rounds, symmetric_CFB *cfb);
574
int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb);
575
int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb);
576
int cfb_getiv(unsigned char *IV, unsigned long *len, symmetric_CFB *cfb);
577
int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb);
578
int cfb_done(symmetric_CFB *cfb);
582
int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key,
583
int keylen, int num_rounds, symmetric_OFB *ofb);
584
int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb);
585
int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb);
586
int ofb_getiv(unsigned char *IV, unsigned long *len, symmetric_OFB *ofb);
587
int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb);
588
int ofb_done(symmetric_OFB *ofb);
592
int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
593
int keylen, int num_rounds, symmetric_CBC *cbc);
594
int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CBC *cbc);
595
int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CBC *cbc);
596
int cbc_getiv(unsigned char *IV, unsigned long *len, symmetric_CBC *cbc);
597
int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc);
598
int cbc_done(symmetric_CBC *cbc);
603
#define CTR_COUNTER_LITTLE_ENDIAN 0
604
#define CTR_COUNTER_BIG_ENDIAN 1
606
int ctr_start( int cipher,
607
const unsigned char *IV,
608
const unsigned char *key, int keylen,
609
int num_rounds, int ctr_mode,
611
int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr);
612
int ctr_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CTR *ctr);
613
int ctr_getiv(unsigned char *IV, unsigned long *len, symmetric_CTR *ctr);
614
int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr);
615
int ctr_done(symmetric_CTR *ctr);
618
int find_cipher(const char *name);
619
int find_cipher_any(const char *name, int blocklen, int keylen);
620
int find_cipher_id(unsigned char ID);
621
int register_cipher(const struct ltc_cipher_descriptor *cipher);
622
int unregister_cipher(const struct ltc_cipher_descriptor *cipher);
623
int cipher_is_valid(int idx);
625
LTC_MUTEX_PROTO(ltc_cipher_mutex);
627
/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_cipher.h,v $ */
628
/* $Revision: 1.16 $ */
629
/* $Date: 2005/06/19 18:00:28 $ */