Skip to content

Commit bc3a752

Browse files
committed
re-factor modes to use internal ECB implementation
1 parent 40eea67 commit bc3a752

Some content is hidden

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

44 files changed

+229
-277
lines changed

src/headers/tomcrypt_cipher.h

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -257,94 +257,70 @@ typedef struct {
257257
#ifdef LTC_CFB_MODE
258258
/** A block cipher CFB structure */
259259
typedef struct {
260-
/** The index of the cipher chosen */
261-
int cipher,
262-
/** The block size of the given cipher */
263-
blocklen,
260+
/** The ECB context of the cipher */
261+
symmetric_ECB ecb;
264262
/** The padding offset */
265-
padlen;
263+
int padlen;
266264
/** The current IV */
267265
unsigned char IV[MAXBLOCKSIZE],
268266
/** The pad used to encrypt/decrypt */
269267
pad[MAXBLOCKSIZE];
270-
/** The scheduled key */
271-
symmetric_key key;
272268
} symmetric_CFB;
273269
#endif
274270

275271
#ifdef LTC_OFB_MODE
276272
/** A block cipher OFB structure */
277273
typedef struct {
278-
/** The index of the cipher chosen */
279-
int cipher,
280-
/** The block size of the given cipher */
281-
blocklen,
274+
/** The ECB context of the cipher */
275+
symmetric_ECB ecb;
282276
/** The padding offset */
283-
padlen;
277+
int padlen;
284278
/** The current IV */
285279
unsigned char IV[MAXBLOCKSIZE];
286-
/** The scheduled key */
287-
symmetric_key key;
288280
} symmetric_OFB;
289281
#endif
290282

291283
#ifdef LTC_CBC_MODE
292284
/** A block cipher CBC structure */
293285
typedef struct {
294-
/** The index of the cipher chosen */
295-
int cipher,
296-
/** The block size of the given cipher */
297-
blocklen;
286+
/** The ECB context of the cipher */
287+
symmetric_ECB ecb;
298288
/** The current IV */
299289
unsigned char IV[MAXBLOCKSIZE];
300-
/** The scheduled key */
301-
symmetric_key key;
302290
} symmetric_CBC;
303291
#endif
304292

305293

306294
#ifdef LTC_CTR_MODE
307295
/** A block cipher CTR structure */
308296
typedef struct {
309-
/** The index of the cipher chosen */
310-
int cipher,
311-
/** The block size of the given cipher */
312-
blocklen,
297+
/** The ECB context of the cipher */
298+
symmetric_ECB ecb;
313299
/** The padding offset */
314-
padlen,
300+
int padlen,
315301
/** The mode (endianess) of the CTR, 0==little, 1==big */
316302
mode,
317303
/** counter width */
318304
ctrlen;
319-
320305
/** The counter */
321306
unsigned char ctr[MAXBLOCKSIZE],
322307
/** The pad used to encrypt/decrypt */
323308
pad[MAXBLOCKSIZE];
324-
/** The scheduled key */
325-
symmetric_key key;
326309
} symmetric_CTR;
327310
#endif
328311

329312

330313
#ifdef LTC_LRW_MODE
331314
/** A LRW structure */
332315
typedef struct {
333-
/** The index of the cipher chosen (must be a 128-bit block cipher) */
334-
int cipher;
335-
316+
/** The ECB context of the cipher */
317+
symmetric_ECB ecb;
336318
/** The current IV */
337319
unsigned char IV[16],
338-
339320
/** the tweak key */
340321
tweak[16],
341-
342322
/** The current pad, it's the product of the first 15 bytes against the tweak key */
343323
pad[16];
344-
345-
/** The scheduled symmetric key */
346-
symmetric_key key;
347-
348324
#ifdef LTC_LRW_TABLES
349325
/** The pre-computed multiplication table */
350326
unsigned char PC[16][256][16];
@@ -355,19 +331,15 @@ typedef struct {
355331
#ifdef LTC_F8_MODE
356332
/** A block cipher F8 structure */
357333
typedef struct {
358-
/** The index of the cipher chosen */
359-
int cipher,
360-
/** The block size of the given cipher */
361-
blocklen,
334+
/** The ECB context of the cipher */
335+
symmetric_ECB ecb;
362336
/** The padding offset */
363-
padlen;
337+
int padlen;
364338
/** The current IV */
365339
unsigned char IV[MAXBLOCKSIZE],
366340
MIV[MAXBLOCKSIZE];
367341
/** Current block count */
368342
ulong32 blockcnt;
369-
/** The scheduled key */
370-
symmetric_key key;
371343
} symmetric_F8;
372344
#endif
373345

@@ -432,7 +404,7 @@ extern struct ltc_cipher_descriptor {
432404
@param skey The scheduled key context
433405
@return CRYPT_OK if successful
434406
*/
435-
int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, symmetric_key *skey);
407+
int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, const symmetric_key *skey);
436408

437409
/** Accelerated ECB decryption
438410
@param pt Plaintext
@@ -441,7 +413,7 @@ extern struct ltc_cipher_descriptor {
441413
@param skey The scheduled key context
442414
@return CRYPT_OK if successful
443415
*/
444-
int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, symmetric_key *skey);
416+
int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, const symmetric_key *skey);
445417

446418
/** Accelerated CBC encryption
447419
@param pt Plaintext
@@ -451,7 +423,7 @@ extern struct ltc_cipher_descriptor {
451423
@param skey The scheduled key context
452424
@return CRYPT_OK if successful
453425
*/
454-
int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
426+
int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const symmetric_key *skey);
455427

456428
/** Accelerated CBC decryption
457429
@param pt Plaintext
@@ -461,7 +433,7 @@ extern struct ltc_cipher_descriptor {
461433
@param skey The scheduled key context
462434
@return CRYPT_OK if successful
463435
*/
464-
int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
436+
int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const symmetric_key *skey);
465437

466438
/** Accelerated CTR encryption
467439
@param pt Plaintext
@@ -472,7 +444,7 @@ extern struct ltc_cipher_descriptor {
472444
@param skey The scheduled key context
473445
@return CRYPT_OK if successful
474446
*/
475-
int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, symmetric_key *skey);
447+
int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, const symmetric_key *skey);
476448

477449
/** Accelerated LRW
478450
@param pt Plaintext
@@ -483,7 +455,7 @@ extern struct ltc_cipher_descriptor {
483455
@param skey The scheduled key context
484456
@return CRYPT_OK if successful
485457
*/
486-
int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
458+
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);
487459

488460
/** Accelerated LRW
489461
@param ct Ciphertext
@@ -494,7 +466,7 @@ extern struct ltc_cipher_descriptor {
494466
@param skey The scheduled key context
495467
@return CRYPT_OK if successful
496468
*/
497-
int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
469+
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);
498470

499471
/** Accelerated CCM packet (one-shot)
500472
@param key The secret key to use
@@ -514,7 +486,7 @@ extern struct ltc_cipher_descriptor {
514486
*/
515487
int (*accel_ccm_memory)(
516488
const unsigned char *key, unsigned long keylen,
517-
symmetric_key *uskey,
489+
const symmetric_key *uskey,
518490
const unsigned char *nonce, unsigned long noncelen,
519491
const unsigned char *header, unsigned long headerlen,
520492
unsigned char *pt, unsigned long ptlen,
@@ -875,8 +847,8 @@ extern const struct ltc_cipher_descriptor tea_desc;
875847
#ifdef LTC_ECB_MODE
876848
int ecb_start(int cipher, const unsigned char *key,
877849
int keylen, int num_rounds, symmetric_ECB *ecb);
878-
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb);
879-
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb);
850+
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, const symmetric_ECB *ecb);
851+
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, const symmetric_ECB *ecb);
880852
int ecb_done(symmetric_ECB *ecb);
881853
#endif
882854

@@ -966,7 +938,7 @@ int f8_test_mode(void);
966938

967939
#ifdef LTC_XTS_MODE
968940
typedef struct {
969-
symmetric_key key1, key2;
941+
symmetric_ECB key1, key2;
970942
int cipher;
971943
} symmetric_xts;
972944

src/headers/tomcrypt_custom.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,14 @@
645645
#error LTC_NO_MATH defined, but also a math descriptor
646646
#endif
647647

648+
#if !defined(LTC_ECB_MODE)
649+
#if defined(LTC_CFB_MODE) || defined(LTC_OFB_MODE) || defined(LTC_CBC_MODE) || defined(LTC_CTR_MODE) || \
650+
defined(LTC_F8_MODE) || defined(LTC_LRW_MODE) || defined(LTC_XTS_MODE) )
651+
#error LTC_ECB_MODE not defined, but all other modes depend on it
652+
#endif
653+
#endif
654+
655+
648656
/* THREAD management */
649657
#ifdef LTC_PTHREAD
650658

src/headers/tomcrypt_private.h

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

7070
/* tomcrypt_cipher.h */
7171

72+
int ecb_encrypt_block(const unsigned char *pt, unsigned char *ct, const symmetric_ECB *ecb);
73+
int ecb_decrypt_block(const unsigned char *ct, unsigned char *pt, const symmetric_ECB *ecb);
74+
75+
7276
void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey);
7377
int blowfish_expand(const unsigned char *key, int keylen,
7478
const unsigned char *data, int datalen,

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)