Skip to content

Commit f7e6519

Browse files
authored
Merge pull request #636 from libtom/update-fortuna
Update fortuna
2 parents 86fe3b9 + 0e12dbd commit f7e6519

7 files changed

Lines changed: 70 additions & 38 deletions

File tree

.github/workflows/main.yml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,24 @@ jobs:
3434
cc: [ gcc, clang ]
3535
os: [ ubuntu-20.04, ubuntu-22.04 ]
3636
config:
37-
- { BUILDNAME: 'META_BUILDS', BUILDOPTIONS: '-DGMP_DESC', BUILDSCRIPT: '.ci/meta_builds.sh' }
38-
- { BUILDNAME: 'VALGRIND', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/valgrind.sh' }
39-
- { BUILDNAME: 'STOCK', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/run.sh' }
40-
- { BUILDNAME: 'STOCK-MPI', BUILDOPTIONS: '-ULTM_DESC -UTFM_DESC -UUSE_LTM -UUSE_TFM', BUILDSCRIPT: '.ci/run.sh' }
41-
- { BUILDNAME: 'STOCK+AESNI', BUILDOPTIONS: '-msse4.1 -maes', BUILDSCRIPT: '.ci/run.sh' }
42-
- { BUILDNAME: 'EASY', BUILDOPTIONS: '-DLTC_EASY', BUILDSCRIPT: '.ci/run.sh' }
43-
- { BUILDNAME: 'SMALL', BUILDOPTIONS: '-DLTC_SMALL_CODE', BUILDSCRIPT: '.ci/run.sh' }
44-
- { BUILDNAME: 'NOTABLES', BUILDOPTIONS: '-DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
45-
- { BUILDNAME: 'SMALL+NOTABLES', BUILDOPTIONS: '-DLTC_SMALL_CODE -DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
46-
- { BUILDNAME: 'NO_FAST', BUILDOPTIONS: '-DLTC_NO_FAST', BUILDSCRIPT: '.ci/run.sh' }
47-
- { BUILDNAME: 'NO_FAST+NOTABLES', BUILDOPTIONS: '-DLTC_NO_FAST -DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
48-
- { BUILDNAME: 'NO_ASM', BUILDOPTIONS: '-DLTC_NO_ASM', BUILDSCRIPT: '.ci/run.sh' }
49-
- { BUILDNAME: 'NO_TIMING_RESISTANCE', BUILDOPTIONS: '-DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING', BUILDSCRIPT: '.ci/run.sh' }
50-
- { BUILDNAME: 'PTHREAD', BUILDOPTIONS: '-DLTC_PTHREAD', BUILDSCRIPT: '.ci/run.sh' }
51-
- { BUILDNAME: 'STOCK+ARGTYPE=1', BUILDOPTIONS: '-DARGTYPE=1', BUILDSCRIPT: '.ci/run.sh' }
52-
- { BUILDNAME: 'STOCK+ARGTYPE=2', BUILDOPTIONS: '-DARGTYPE=2', BUILDSCRIPT: '.ci/run.sh' }
53-
- { BUILDNAME: 'STOCK+ARGTYPE=3', BUILDOPTIONS: '-DARGTYPE=3', BUILDSCRIPT: '.ci/run.sh' }
54-
- { BUILDNAME: 'STOCK+ARGTYPE=4', BUILDOPTIONS: '-DARGTYPE=4', BUILDSCRIPT: '.ci/run.sh' }
37+
- { BUILDNAME: 'META_BUILDS', BUILDOPTIONS: '-DGMP_DESC', BUILDSCRIPT: '.ci/meta_builds.sh' }
38+
- { BUILDNAME: 'VALGRIND', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/valgrind.sh' }
39+
- { BUILDNAME: 'STOCK', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/run.sh' }
40+
- { BUILDNAME: 'STOCK-MPI', BUILDOPTIONS: '-ULTM_DESC -UTFM_DESC -UUSE_LTM -UUSE_TFM', BUILDSCRIPT: '.ci/run.sh' }
41+
- { BUILDNAME: 'STOCK+AESNI', BUILDOPTIONS: '-msse4.1 -maes', BUILDSCRIPT: '.ci/run.sh' }
42+
- { BUILDNAME: 'EASY', BUILDOPTIONS: '-DLTC_EASY', BUILDSCRIPT: '.ci/run.sh' }
43+
- { BUILDNAME: 'SMALL', BUILDOPTIONS: '-DLTC_SMALL_CODE', BUILDSCRIPT: '.ci/run.sh' }
44+
- { BUILDNAME: 'NO_TABLES', BUILDOPTIONS: '-DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
45+
- { BUILDNAME: 'NO_FAST', BUILDOPTIONS: '-DLTC_NO_FAST', BUILDSCRIPT: '.ci/run.sh' }
46+
- { BUILDNAME: 'NO_FAST+SMALL+NO_TABLES', BUILDOPTIONS: '-DLTC_NO_FAST -DLTC_SMALL_CODE -DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }
47+
- { BUILDNAME: 'NO_ASM', BUILDOPTIONS: '-DLTC_NO_ASM', BUILDSCRIPT: '.ci/run.sh' }
48+
- { BUILDNAME: 'NO_TIMING_RESISTANCE', BUILDOPTIONS: '-DLTC_NO_ECC_TIMING_RESISTANT -DLTC_NO_RSA_BLINDING', BUILDSCRIPT: '.ci/run.sh' }
49+
- { BUILDNAME: 'FORTUNA_CUSTOM_OPTIONS', BUILDOPTIONS: '-DLTC_FORTUNA_USE_ENCRYPT_ONLY -DLTC_FORTUNA_RESEED_RATELIMIT_STATIC', BUILDSCRIPT: '.ci/run.sh' }
50+
- { BUILDNAME: 'PTHREAD', BUILDOPTIONS: '-DLTC_PTHREAD', BUILDSCRIPT: '.ci/run.sh' }
51+
- { BUILDNAME: 'STOCK+ARGTYPE=1', BUILDOPTIONS: '-DARGTYPE=1', BUILDSCRIPT: '.ci/run.sh' }
52+
- { BUILDNAME: 'STOCK+ARGTYPE=2', BUILDOPTIONS: '-DARGTYPE=2', BUILDSCRIPT: '.ci/run.sh' }
53+
- { BUILDNAME: 'STOCK+ARGTYPE=3', BUILDOPTIONS: '-DARGTYPE=3', BUILDSCRIPT: '.ci/run.sh' }
54+
- { BUILDNAME: 'STOCK+ARGTYPE=4', BUILDOPTIONS: '-DARGTYPE=4', BUILDSCRIPT: '.ci/run.sh' }
5555
steps:
5656
- uses: actions/checkout@v2
5757
- name: install dependencies

src/ciphers/aes/aes_desc.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const struct ltc_cipher_descriptor aes_desc =
3434
#define AES_SETUP aes_enc_setup
3535
#define AES_ENC aes_enc_ecb_encrypt
3636
#define AES_DONE aes_enc_done
37+
#define AES_TEST aes_enc_test
3738
#define AES_KS aes_enc_keysize
3839

3940
const struct ltc_cipher_descriptor aes_enc_desc =
@@ -119,6 +120,7 @@ int AES_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *ske
119120
}
120121

121122

123+
#ifndef ENCRYPT_ONLY
122124
/**
123125
Decrypts a block of text with AES
124126
@param ct The input ciphertext (16 bytes)
@@ -135,6 +137,7 @@ int AES_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *ske
135137
#endif
136138
return rijndael_ecb_decrypt(ct, pt, skey);
137139
}
140+
#endif /* ENCRYPT_ONLY */
138141

139142
/**
140143
Performs a self-test of the AES block cipher
@@ -181,26 +184,33 @@ int AES_TEST(void)
181184

182185
symmetric_key key;
183186
unsigned char tmp[2][16];
184-
int i, y;
187+
int i;
188+
#ifndef ENCRYPT_ONLY
189+
int y;
190+
#endif
185191

186192
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
187193
zeromem(&key, sizeof(key));
188-
if ((err = aes_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
194+
if ((err = AES_SETUP(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
189195
return err;
190196
}
191197

192-
aes_ecb_encrypt(tests[i].pt, tmp[0], &key);
193-
aes_ecb_decrypt(tmp[0], tmp[1], &key);
194-
if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "AES Encrypt", i) ||
195-
compare_testvector(tmp[1], 16, tests[i].pt, 16, "AES Decrypt", i)) {
198+
AES_ENC(tests[i].pt, tmp[0], &key);
199+
if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "AES Encrypt", i)) {
200+
return CRYPT_FAIL_TESTVECTOR;
201+
}
202+
#ifndef ENCRYPT_ONLY
203+
AES_DEC(tmp[0], tmp[1], &key);
204+
if (compare_testvector(tmp[1], 16, tests[i].pt, 16, "AES Decrypt", i)) {
196205
return CRYPT_FAIL_TESTVECTOR;
197206
}
198207

199208
/* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
200209
for (y = 0; y < 16; y++) tmp[0][y] = 0;
201-
for (y = 0; y < 1000; y++) aes_ecb_encrypt(tmp[0], tmp[0], &key);
202-
for (y = 0; y < 1000; y++) aes_ecb_decrypt(tmp[0], tmp[0], &key);
210+
for (y = 0; y < 1000; y++) AES_ENC(tmp[0], tmp[0], &key);
211+
for (y = 0; y < 1000; y++) AES_DEC(tmp[0], tmp[0], &key);
203212
for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
213+
#endif
204214
}
205215
return CRYPT_OK;
206216
#endif

src/headers/tomcrypt_cipher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ void aes_done(symmetric_key *skey);
699699
int aes_keysize(int *keysize);
700700
int aes_enc_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
701701
int aes_enc_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey);
702+
int aes_enc_test(void);
702703
void aes_enc_done(symmetric_key *skey);
703704
int aes_enc_keysize(int *keysize);
704705
extern const struct ltc_cipher_descriptor aes_desc;

src/headers/tomcrypt_custom.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@
375375

376376
/* with non-glibc or glibc 2.17+ prefer clock_gettime over gettimeofday */
377377
#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
378-
#if __GLIBC_PREREQ(2, 17)
379-
#define LTC_CLOCK_GETTIME
380-
#endif
378+
#if __GLIBC_PREREQ(2, 17)
379+
#define LTC_CLOCK_GETTIME
380+
#endif
381381
#elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
382382
#define LTC_CLOCK_GETTIME
383383
#endif
@@ -407,6 +407,11 @@
407407
#define LTC_FORTUNA_POOLS 32
408408
#endif
409409

410+
/* at compile time you can decide whether fortuna uses the regular AES APIs
411+
* or whether it will use the 'encrypt_only' variants.
412+
* This is useful for custom builds of libtomcrypt for size-constrained targets. */
413+
/* #define LTC_FORTUNA_USE_ENCRYPT_ONLY */
414+
410415
#endif /* LTC_FORTUNA */
411416

412417

src/misc/crypt/crypt.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,12 @@ const char *crypt_build_settings =
315315
#if defined(LTC_FORTUNA)
316316
" Fortuna (" NAME_VALUE(LTC_FORTUNA_POOLS) ", "
317317
#if defined(LTC_FORTUNA_RESEED_RATELIMIT_TIMED)
318-
"LTC_FORTUNA_RESEED_RATELIMIT_TIMED, "
318+
"LTC_FORTUNA_RESEED_RATELIMIT_TIMED"
319319
#else
320320
"LTC_FORTUNA_RESEED_RATELIMIT_STATIC, " NAME_VALUE(LTC_FORTUNA_WD)
321+
#endif
322+
#if defined(LTC_FORTUNA_USE_ENCRYPT_ONLY)
323+
", LTC_FORTUNA_USE_ENCRYPT_ONLY"
321324
#endif
322325
")\n"
323326
#endif

src/prngs/fortuna.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ we reseed automatically when len(pool0) >= 64 or every LTC_FORTUNA_WD calls to t
3939
#error LTC_FORTUNA_POOLS must be in [4..32]
4040
#endif
4141

42+
#ifdef LTC_FORTUNA_USE_ENCRYPT_ONLY
43+
#define AES_SETUP aes_enc_setup
44+
#define AES_ENC aes_enc_ecb_encrypt
45+
#define AES_DONE aes_enc_done
46+
#define AES_TEST aes_enc_test
47+
#else
48+
#define AES_SETUP aes_setup
49+
#define AES_ENC aes_ecb_encrypt
50+
#define AES_DONE aes_done
51+
#define AES_TEST aes_test
52+
#endif
53+
4254
const struct ltc_prng_descriptor fortuna_desc = {
4355
"fortuna",
4456
64,
@@ -146,7 +158,7 @@ static int s_fortuna_reseed(prng_state *prng)
146158
if ((err = sha256_done(&md, prng->u.fortuna.K)) != CRYPT_OK) {
147159
return err;
148160
}
149-
if ((err = rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey)) != CRYPT_OK) {
161+
if ((err = AES_SETUP(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey)) != CRYPT_OK) {
150162
return err;
151163
}
152164
s_fortuna_update_iv(prng);
@@ -236,7 +248,7 @@ int fortuna_start(prng_state *prng)
236248

237249
/* reset bufs */
238250
zeromem(prng->u.fortuna.K, 32);
239-
if ((err = rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey)) != CRYPT_OK) {
251+
if ((err = AES_SETUP(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey)) != CRYPT_OK) {
240252
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
241253
sha256_done(&prng->u.fortuna.pool[x], tmp);
242254
}
@@ -395,27 +407,27 @@ unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state
395407
/* handle whole blocks without the extra XMEMCPY */
396408
while (outlen >= 16) {
397409
/* encrypt the IV and store it */
398-
rijndael_ecb_encrypt(prng->u.fortuna.IV, out, &prng->u.fortuna.skey);
410+
AES_ENC(prng->u.fortuna.IV, out, &prng->u.fortuna.skey);
399411
out += 16;
400412
outlen -= 16;
401413
s_fortuna_update_iv(prng);
402414
}
403415

404416
/* left over bytes? */
405417
if (outlen > 0) {
406-
rijndael_ecb_encrypt(prng->u.fortuna.IV, tmp, &prng->u.fortuna.skey);
418+
AES_ENC(prng->u.fortuna.IV, tmp, &prng->u.fortuna.skey);
407419
XMEMCPY(out, tmp, outlen);
408420
s_fortuna_update_iv(prng);
409421
}
410422

411423
/* generate new key */
412-
rijndael_ecb_encrypt(prng->u.fortuna.IV, prng->u.fortuna.K , &prng->u.fortuna.skey);
424+
AES_ENC(prng->u.fortuna.IV, prng->u.fortuna.K , &prng->u.fortuna.skey);
413425
s_fortuna_update_iv(prng);
414426

415-
rijndael_ecb_encrypt(prng->u.fortuna.IV, prng->u.fortuna.K+16, &prng->u.fortuna.skey);
427+
AES_ENC(prng->u.fortuna.IV, prng->u.fortuna.K+16, &prng->u.fortuna.skey);
416428
s_fortuna_update_iv(prng);
417429

418-
if (rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey) != CRYPT_OK) {
430+
if (AES_SETUP(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey) != CRYPT_OK) {
419431
tlen = 0;
420432
}
421433

@@ -512,7 +524,7 @@ int fortuna_test(void)
512524
if ((err = sha256_test()) != CRYPT_OK) {
513525
return err;
514526
}
515-
return rijndael_test();
527+
return AES_TEST();
516528
#endif
517529
}
518530

tests/cipher_hash_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ int cipher_hash_test(void)
2020
}
2121
DO(rijndael_test());
2222
#endif
23+
DO(aes_enc_test());
2324

2425
/* test stream ciphers */
2526
#ifdef LTC_CHACHA

0 commit comments

Comments
 (0)