Mercurial > dropbear
comparison libtomcrypt/src/modes/f8/f8_start.c @ 1471:6dba84798cd5
Update to libtomcrypt 1.18.1, merged with Dropbear changes
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 09 Feb 2018 21:44:05 +0800 |
parents | f849a5ca2efc |
children |
comparison
equal
deleted
inserted
replaced
1470:8bba51a55704 | 1471:6dba84798cd5 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 #include "tomcrypt.h" | 9 #include "tomcrypt.h" |
12 | 10 |
13 /** | 11 /** |
14 @file f8_start.c | 12 @file f8_start.c |
19 #ifdef LTC_F8_MODE | 17 #ifdef LTC_F8_MODE |
20 | 18 |
21 /** | 19 /** |
22 Initialize an F8 context | 20 Initialize an F8 context |
23 @param cipher The index of the cipher desired | 21 @param cipher The index of the cipher desired |
24 @param IV The initial vector | 22 @param IV The initialization vector |
25 @param key The secret key | 23 @param key The secret key |
26 @param keylen The length of the secret key (octets) | 24 @param keylen The length of the secret key (octets) |
27 @param salt_key The salting key for the IV | 25 @param salt_key The salting key for the IV |
28 @param skeylen The length of the salting key (octets) | 26 @param skeylen The length of the salting key (octets) |
29 @param num_rounds Number of rounds in the cipher desired (0 for default) | 27 @param num_rounds Number of rounds in the cipher desired (0 for default) |
30 @param f8 The F8 state to initialize | 28 @param f8 The F8 state to initialize |
31 @return CRYPT_OK if successful | 29 @return CRYPT_OK if successful |
32 */ | 30 */ |
33 int f8_start( int cipher, const unsigned char *IV, | 31 int f8_start( int cipher, const unsigned char *IV, |
34 const unsigned char *key, int keylen, | 32 const unsigned char *key, int keylen, |
35 const unsigned char *salt_key, int skeylen, | 33 const unsigned char *salt_key, int skeylen, |
36 int num_rounds, symmetric_F8 *f8) | 34 int num_rounds, symmetric_F8 *f8) |
37 { | 35 { |
38 int x, err; | 36 int x, err; |
39 unsigned char tkey[MAXBLOCKSIZE]; | 37 unsigned char tkey[MAXBLOCKSIZE]; |
56 /* copy details */ | 54 /* copy details */ |
57 f8->blockcnt = 0; | 55 f8->blockcnt = 0; |
58 f8->cipher = cipher; | 56 f8->cipher = cipher; |
59 f8->blocklen = cipher_descriptor[cipher].block_length; | 57 f8->blocklen = cipher_descriptor[cipher].block_length; |
60 f8->padlen = f8->blocklen; | 58 f8->padlen = f8->blocklen; |
61 | 59 |
62 /* now get key ^ salt_key [extend salt_ket with 0x55 as required to match length] */ | 60 /* now get key ^ salt_key [extend salt_ket with 0x55 as required to match length] */ |
63 zeromem(tkey, sizeof(tkey)); | 61 zeromem(tkey, sizeof(tkey)); |
64 for (x = 0; x < keylen && x < (int)sizeof(tkey); x++) { | 62 for (x = 0; x < keylen && x < (int)sizeof(tkey); x++) { |
65 tkey[x] = key[x]; | 63 tkey[x] = key[x]; |
66 } | 64 } |
67 for (x = 0; x < skeylen && x < (int)sizeof(tkey); x++) { | 65 for (x = 0; x < skeylen && x < (int)sizeof(tkey); x++) { |
68 tkey[x] ^= salt_key[x]; | 66 tkey[x] ^= salt_key[x]; |
69 } | 67 } |
70 for (; x < keylen && x < (int)sizeof(tkey); x++) { | 68 for (; x < keylen && x < (int)sizeof(tkey); x++) { |
71 tkey[x] ^= 0x55; | 69 tkey[x] ^= 0x55; |
72 } | 70 } |
73 | 71 |
74 /* now encrypt with tkey[0..keylen-1] the IV and use that as the IV */ | 72 /* now encrypt with tkey[0..keylen-1] the IV and use that as the IV */ |
75 if ((err = cipher_descriptor[cipher].setup(tkey, keylen, num_rounds, &f8->key)) != CRYPT_OK) { | 73 if ((err = cipher_descriptor[cipher].setup(tkey, keylen, num_rounds, &f8->key)) != CRYPT_OK) { |
76 return err; | 74 return err; |
77 } | 75 } |
78 | 76 |
79 /* encrypt IV */ | 77 /* encrypt IV */ |
80 if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(IV, f8->MIV, &f8->key)) != CRYPT_OK) { | 78 if ((err = cipher_descriptor[f8->cipher].ecb_encrypt(IV, f8->MIV, &f8->key)) != CRYPT_OK) { |
81 cipher_descriptor[f8->cipher].done(&f8->key); | 79 cipher_descriptor[f8->cipher].done(&f8->key); |
82 return err; | 80 return err; |
83 } | 81 } |
84 zeromem(tkey, sizeof(tkey)); | 82 zeromem(tkey, sizeof(tkey)); |
85 zeromem(f8->IV, sizeof(f8->IV)); | 83 zeromem(f8->IV, sizeof(f8->IV)); |
86 | 84 |
87 /* terminate this cipher */ | 85 /* terminate this cipher */ |
88 cipher_descriptor[f8->cipher].done(&f8->key); | 86 cipher_descriptor[f8->cipher].done(&f8->key); |
89 | 87 |
90 /* init the cipher */ | 88 /* init the cipher */ |
91 return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &f8->key); | 89 return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &f8->key); |
92 } | 90 } |
93 | 91 |
94 #endif | 92 #endif |
95 | 93 |
96 /* $Source$ */ | 94 /* ref: $Format:%D$ */ |
97 /* $Revision$ */ | 95 /* git commit: $Format:%H$ */ |
98 /* $Date$ */ | 96 /* commit time: $Format:%ai$ */ |