Skip to content

Commit 661109f

Browse files
committed
re-factor modes to use internal ECB implementation
1 parent 2380362 commit 661109f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+241
-284
lines changed

src/headers/tomcrypt_cipher.h

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -274,18 +274,14 @@ typedef struct {
274274
#ifdef LTC_CFB_MODE
275275
/** A block cipher CFB structure */
276276
typedef struct {
277+
/** The ECB context of the cipher */
278+
symmetric_ECB ecb;
277279
/** The current IV */
278280
unsigned char IV[MAXBLOCKSIZE],
279281
/** The pad used to encrypt/decrypt */
280282
pad[MAXBLOCKSIZE];
281-
/** The scheduled key */
282-
symmetric_key key;
283-
/** The index of the cipher chosen */
284-
int cipher,
285-
/** The block size of the given cipher */
286-
blocklen,
287283
/** The width of the mode: 1, 8, 64, or 128 */
288-
width,
284+
int width,
289285
/** The padding offset */
290286
padlen;
291287
} symmetric_CFB;
@@ -294,50 +290,39 @@ typedef struct {
294290
#ifdef LTC_OFB_MODE
295291
/** A block cipher OFB structure */
296292
typedef struct {
293+
/** The ECB context of the cipher */
294+
symmetric_ECB ecb;
297295
/** The current IV */
298296
unsigned char IV[MAXBLOCKSIZE];
299-
/** The scheduled key */
300-
symmetric_key key;
301-
/** The index of the cipher chosen */
302-
int cipher,
303-
/** The block size of the given cipher */
304-
blocklen,
305297
/** The padding offset */
306-
padlen;
298+
int padlen;
299+
307300
} symmetric_OFB;
308301
#endif
309302

310303
#ifdef LTC_CBC_MODE
311304
/** A block cipher CBC structure */
312305
typedef struct {
306+
/** The ECB context of the cipher */
307+
symmetric_ECB ecb;
313308
/** The current IV */
314309
unsigned char IV[MAXBLOCKSIZE];
315-
/** The scheduled key */
316-
symmetric_key key;
317-
/** The index of the cipher chosen */
318-
int cipher,
319-
/** The block size of the given cipher */
320-
blocklen;
321310
} symmetric_CBC;
322311
#endif
323312

324313

325314
#ifdef LTC_CTR_MODE
326315
/** A block cipher CTR structure */
327316
typedef struct {
317+
/** The ECB context of the cipher */
318+
symmetric_ECB ecb;
328319
/** The counter */
329320
unsigned char ctr[MAXBLOCKSIZE];
330321
/** The pad used to encrypt/decrypt */
331322
unsigned char pad[MAXBLOCKSIZE];
332-
/** The scheduled key */
333-
symmetric_key key;
334323

335-
/** The index of the cipher chosen */
336-
int cipher,
337-
/** The block size of the given cipher */
338-
blocklen,
339324
/** The padding offset */
340-
padlen,
325+
int padlen,
341326
/** The mode (endianess) of the CTR, 0==little, 1==big */
342327
mode,
343328
/** counter width */
@@ -349,18 +334,14 @@ typedef struct {
349334
#ifdef LTC_LRW_MODE
350335
/** A LRW structure */
351336
typedef struct {
337+
/** The ECB context of the cipher */
338+
symmetric_ECB ecb;
352339
/** The current IV */
353340
unsigned char IV[16],
354-
355341
/** the tweak key */
356342
tweak[16],
357-
358343
/** The current pad, it's the product of the first 15 bytes against the tweak key */
359344
pad[16];
360-
361-
/** The scheduled symmetric key */
362-
symmetric_key key;
363-
364345
#ifdef LTC_LRW_TABLES
365346
/** The pre-computed multiplication table */
366347
unsigned char PC[16][256][16];
@@ -374,17 +355,13 @@ typedef struct {
374355
#ifdef LTC_F8_MODE
375356
/** A block cipher F8 structure */
376357
typedef struct {
358+
/** The ECB context of the cipher */
359+
symmetric_ECB ecb;
377360
/** The current IV */
378361
unsigned char IV[MAXBLOCKSIZE],
379362
MIV[MAXBLOCKSIZE];
380-
/** The scheduled key */
381-
symmetric_key key;
382-
/** The index of the cipher chosen */
383-
int cipher,
384-
/** The block size of the given cipher */
385-
blocklen,
386363
/** The padding offset */
387-
padlen;
364+
int padlen;
388365
/** Current block count */
389366
ulong32 blockcnt;
390367
} symmetric_F8;
@@ -451,7 +428,7 @@ extern struct ltc_cipher_descriptor {
451428
@param skey The scheduled key context
452429
@return CRYPT_OK if successful
453430
*/
454-
int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, symmetric_key *skey);
431+
int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, const symmetric_key *skey);
455432

456433
/** Accelerated ECB decryption
457434
@param pt Plaintext
@@ -460,7 +437,7 @@ extern struct ltc_cipher_descriptor {
460437
@param skey The scheduled key context
461438
@return CRYPT_OK if successful
462439
*/
463-
int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, symmetric_key *skey);
440+
int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, const symmetric_key *skey);
464441

465442
/** Accelerated CBC encryption
466443
@param pt Plaintext
@@ -470,7 +447,7 @@ extern struct ltc_cipher_descriptor {
470447
@param skey The scheduled key context
471448
@return CRYPT_OK if successful
472449
*/
473-
int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
450+
int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const symmetric_key *skey);
474451

475452
/** Accelerated CBC decryption
476453
@param pt Plaintext
@@ -480,7 +457,7 @@ extern struct ltc_cipher_descriptor {
480457
@param skey The scheduled key context
481458
@return CRYPT_OK if successful
482459
*/
483-
int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
460+
int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const symmetric_key *skey);
484461

485462
/** Accelerated CTR encryption
486463
@param pt Plaintext
@@ -491,7 +468,7 @@ extern struct ltc_cipher_descriptor {
491468
@param skey The scheduled key context
492469
@return CRYPT_OK if successful
493470
*/
494-
int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, symmetric_key *skey);
471+
int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, const symmetric_key *skey);
495472

496473
/** Accelerated LRW
497474
@param pt Plaintext
@@ -502,7 +479,7 @@ extern struct ltc_cipher_descriptor {
502479
@param skey The scheduled key context
503480
@return CRYPT_OK if successful
504481
*/
505-
int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
482+
int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, const symmetric_key *skey);
506483

507484
/** Accelerated LRW
508485
@param ct Ciphertext
@@ -513,7 +490,7 @@ extern struct ltc_cipher_descriptor {
513490
@param skey The scheduled key context
514491
@return CRYPT_OK if successful
515492
*/
516-
int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
493+
int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, const symmetric_key *skey);
517494

518495
/** Accelerated CCM packet (one-shot)
519496
@param key The secret key to use
@@ -533,7 +510,7 @@ extern struct ltc_cipher_descriptor {
533510
*/
534511
int (*accel_ccm_memory)(
535512
const unsigned char *key, unsigned long keylen,
536-
symmetric_key *uskey,
513+
const symmetric_key *uskey,
537514
const unsigned char *nonce, unsigned long noncelen,
538515
const unsigned char *header, unsigned long headerlen,
539516
unsigned char *pt, unsigned long ptlen,
@@ -923,8 +900,8 @@ extern const struct ltc_cipher_descriptor tea_desc;
923900
#ifdef LTC_ECB_MODE
924901
int ecb_start(int cipher, const unsigned char *key,
925902
int keylen, int num_rounds, symmetric_ECB *ecb);
926-
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb);
927-
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb);
903+
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, const symmetric_ECB *ecb);
904+
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, const symmetric_ECB *ecb);
928905
int ecb_done(symmetric_ECB *ecb);
929906
#endif
930907

@@ -1016,7 +993,7 @@ int f8_test_mode(void);
1016993

1017994
#ifdef LTC_XTS_MODE
1018995
typedef struct {
1019-
symmetric_key key1, key2;
996+
symmetric_ECB key1, key2;
1020997
int cipher;
1021998
} symmetric_xts;
1022999

src/headers/tomcrypt_custom.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,14 @@
710710
#error LTC_NO_MATH defined, but also a math descriptor
711711
#endif
712712

713+
#if !defined(LTC_ECB_MODE)
714+
#if defined(LTC_CFB_MODE) || defined(LTC_OFB_MODE) || defined(LTC_CBC_MODE) || defined(LTC_CTR_MODE) || \
715+
defined(LTC_F8_MODE) || defined(LTC_LRW_MODE) || defined(LTC_XTS_MODE) )
716+
#error LTC_ECB_MODE not defined, but all other modes depend on it
717+
#endif
718+
#endif
719+
720+
713721
/* THREAD management */
714722
#ifdef LTC_PTHREAD
715723

src/headers/tomcrypt_private.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ typedef struct {
122122

123123
/* tomcrypt_cipher.h */
124124

125+
int ecb_encrypt_block(const unsigned char *pt, unsigned char *ct, const symmetric_ECB *ecb);
126+
int ecb_decrypt_block(const unsigned char *ct, unsigned char *pt, const symmetric_ECB *ecb);
127+
128+
125129
void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey);
126130
int blowfish_expand(const unsigned char *key, int keylen,
127131
const unsigned char *data, int datalen,

src/misc/pem/pem.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ int pem_decrypt(unsigned char *data, unsigned long *datalen,
197197
goto error_out;
198198
}
199199

200-
if ((err = padding_depad(data, datalen, padding | s.ctx.cbc.blocklen)) != CRYPT_OK) {
200+
if ((err = padding_depad(data, datalen, padding | s.ctx.cbc.ecb.blocklen)) != CRYPT_OK) {
201201
goto error_out;
202202
}
203203
#else

src/modes/cbc/cbc_decrypt.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,51 +32,51 @@ int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s
3232
LTC_ARGCHK(ct != NULL);
3333
LTC_ARGCHK(cbc != NULL);
3434

35-
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
35+
if ((err = cipher_is_valid(cbc->ecb.cipher)) != CRYPT_OK) {
3636
return err;
3737
}
3838

3939
/* is blocklen valid? */
40-
if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV) || cbc->blocklen > (int)sizeof(tmp)) {
40+
if (cbc->ecb.blocklen < 1 || cbc->ecb.blocklen > (int)sizeof(cbc->IV) || cbc->ecb.blocklen > (int)sizeof(tmp)) {
4141
return CRYPT_INVALID_ARG;
4242
}
4343

44-
if (len % cbc->blocklen) {
44+
if (len % cbc->ecb.blocklen) {
4545
return CRYPT_INVALID_ARG;
4646
}
4747
#ifdef LTC_FAST
48-
if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
48+
if (cbc->ecb.blocklen % sizeof(LTC_FAST_TYPE)) {
4949
return CRYPT_INVALID_ARG;
5050
}
5151
#endif
5252

53-
if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
54-
return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
53+
if (cipher_descriptor[cbc->ecb.cipher].accel_cbc_decrypt != NULL) {
54+
return cipher_descriptor[cbc->ecb.cipher].accel_cbc_decrypt(ct, pt, len / cbc->ecb.blocklen, cbc->IV, &cbc->ecb.key);
5555
}
5656
while (len) {
5757
/* decrypt */
58-
if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
58+
if ((err = ecb_decrypt_block(ct, tmp, &cbc->ecb)) != CRYPT_OK) {
5959
return err;
6060
}
6161

6262
/* xor IV against plaintext */
6363
#if defined(LTC_FAST)
64-
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
64+
for (x = 0; x < cbc->ecb.blocklen; x += sizeof(LTC_FAST_TYPE)) {
6565
tmpy = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)tmp + x));
6666
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
6767
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) = tmpy;
6868
}
6969
#else
70-
for (x = 0; x < cbc->blocklen; x++) {
70+
for (x = 0; x < cbc->ecb.blocklen; x++) {
7171
tmpy = tmp[x] ^ cbc->IV[x];
7272
cbc->IV[x] = ct[x];
7373
pt[x] = tmpy;
7474
}
7575
#endif
7676

77-
ct += cbc->blocklen;
78-
pt += cbc->blocklen;
79-
len -= cbc->blocklen;
77+
ct += cbc->ecb.blocklen;
78+
pt += cbc->ecb.blocklen;
79+
len -= cbc->ecb.blocklen;
8080
}
8181
return CRYPT_OK;
8282
}

src/modes/cbc/cbc_done.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515
*/
1616
int cbc_done(symmetric_CBC *cbc)
1717
{
18-
int err;
1918
LTC_ARGCHK(cbc != NULL);
2019

21-
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
22-
return err;
23-
}
24-
cipher_descriptor[cbc->cipher].done(&cbc->key);
25-
return CRYPT_OK;
20+
return ecb_done(&cbc->ecb);
2621
}
2722

2823

0 commit comments

Comments
 (0)