Skip to content

Commit 59a17dd

Browse files
committed
Unit testing: Add Monte Carlo testing to ciphers
Monte Carlo testing is randomized test data. These new tests have random keys, IVs, nonce, etc and random data to encrypt. 100 sets of random test data are encrypted and decrypted with a check to ensure the input to encrypt is the same as the output of decrypt. Tags are generated and checked in the calls to encrypt and decrypt.
1 parent b17755b commit 59a17dd

16 files changed

Lines changed: 1126 additions & 21 deletions

tests/api/test_aes.c

Lines changed: 454 additions & 0 deletions
Large diffs are not rendered by default.

tests/api/test_aes.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ int test_wc_AesEaxStream(void);
5555
int test_wc_AesSivEncryptDecrypt(void);
5656
#endif
5757

58+
int test_wc_AesCbc_MonteCarlo(void);
59+
int test_wc_AesCtr_MonteCarlo(void);
60+
int test_wc_AesGcm_MonteCarlo(void);
61+
int test_wc_AesCcm_MonteCarlo(void);
62+
int test_wc_AesCfb_MonteCarlo(void);
63+
int test_wc_AesOfb_MonteCarlo(void);
64+
5865
int test_wc_GmacSetKey(void);
5966
int test_wc_GmacUpdate(void);
6067
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_AES_SETKEY) && \
@@ -91,7 +98,13 @@ int test_wc_CryptoCb_AesGcm_EncryptDecrypt(void);
9198
TEST_DECL_GROUP("aes", test_wc_AesCcmEncryptDecrypt), \
9299
TEST_DECL_GROUP("aes", test_wc_AesXtsSetKey), \
93100
TEST_DECL_GROUP("aes", test_wc_AesXtsEncryptDecrypt_Sizes), \
94-
TEST_DECL_GROUP("aes", test_wc_AesXtsEncryptDecrypt) \
101+
TEST_DECL_GROUP("aes", test_wc_AesXtsEncryptDecrypt), \
102+
TEST_DECL_GROUP("aes", test_wc_AesCbc_MonteCarlo), \
103+
TEST_DECL_GROUP("aes", test_wc_AesCtr_MonteCarlo), \
104+
TEST_DECL_GROUP("aes", test_wc_AesGcm_MonteCarlo), \
105+
TEST_DECL_GROUP("aes", test_wc_AesCcm_MonteCarlo), \
106+
TEST_DECL_GROUP("aes", test_wc_AesCfb_MonteCarlo), \
107+
TEST_DECL_GROUP("aes", test_wc_AesOfb_MonteCarlo) \
95108
TEST_CRYPTOCB_AES_SETKEY_DECL
96109

97110
#if defined(WOLFSSL_AES_EAX) && defined(WOLFSSL_AES_256) && \

tests/api/test_arc4.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,64 @@ int test_wc_Arc4Process(void)
104104

105105
} /* END test_wc_Arc4Process */
106106

107+
108+
#include <wolfssl/wolfcrypt/random.h>
109+
110+
#define MC_CIPHER_TEST_COUNT 100
111+
#define MC_ARC4_MAX_DATA_SZ 1024
112+
#define MC_ARC4_KEY_SZ 16 /* fixed 128-bit key */
113+
114+
/* Monte Carlo test for ARC4: random key and plaintext each iteration */
115+
int test_wc_Arc4_MonteCarlo(void)
116+
{
117+
EXPECT_DECLS;
118+
#ifndef NO_RC4
119+
Arc4 enc, dec;
120+
WC_RNG rng;
121+
byte key[MC_ARC4_KEY_SZ];
122+
word32 plainLen = 0;
123+
int i;
124+
WC_DECLARE_VAR(plain, byte, MC_ARC4_MAX_DATA_SZ, NULL);
125+
WC_DECLARE_VAR(cipher, byte, MC_ARC4_MAX_DATA_SZ, NULL);
126+
WC_DECLARE_VAR(decrypted, byte, MC_ARC4_MAX_DATA_SZ, NULL);
127+
128+
WC_ALLOC_VAR(plain, byte, MC_ARC4_MAX_DATA_SZ, NULL);
129+
WC_ALLOC_VAR(cipher, byte, MC_ARC4_MAX_DATA_SZ, NULL);
130+
WC_ALLOC_VAR(decrypted, byte, MC_ARC4_MAX_DATA_SZ, NULL);
131+
#ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
132+
ExpectNotNull(plain);
133+
ExpectNotNull(cipher);
134+
ExpectNotNull(decrypted);
135+
#endif
136+
137+
XMEMSET(&enc, 0, sizeof(enc));
138+
XMEMSET(&dec, 0, sizeof(dec));
139+
XMEMSET(&rng, 0, sizeof(rng));
140+
141+
ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
142+
ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
143+
ExpectIntEQ(wc_InitRng(&rng), 0);
144+
145+
for (i = 0; i < MC_CIPHER_TEST_COUNT && EXPECT_SUCCESS(); i++) {
146+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
147+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, (byte*)&plainLen,
148+
sizeof(plainLen)), 0);
149+
plainLen = (plainLen % MC_ARC4_MAX_DATA_SZ) + 1;
150+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, plain, plainLen), 0);
151+
152+
ExpectIntEQ(wc_Arc4SetKey(&enc, key, sizeof(key)), 0);
153+
ExpectIntEQ(wc_Arc4SetKey(&dec, key, sizeof(key)), 0);
154+
ExpectIntEQ(wc_Arc4Process(&enc, cipher, plain, plainLen), 0);
155+
ExpectIntEQ(wc_Arc4Process(&dec, decrypted, cipher, plainLen), 0);
156+
ExpectBufEQ(decrypted, plain, plainLen);
157+
}
158+
159+
wc_Arc4Free(&enc);
160+
wc_Arc4Free(&dec);
161+
wc_FreeRng(&rng);
162+
WC_FREE_VAR(plain, NULL);
163+
WC_FREE_VAR(cipher, NULL);
164+
WC_FREE_VAR(decrypted, NULL);
165+
#endif
166+
return EXPECT_RESULT();
167+
}

tests/api/test_arc4.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626

2727
int test_wc_Arc4SetKey(void);
2828
int test_wc_Arc4Process(void);
29+
int test_wc_Arc4_MonteCarlo(void);
2930

30-
#define TEST_ARC4_DECLS \
31-
TEST_DECL_GROUP("arc4", test_wc_Arc4SetKey), \
32-
TEST_DECL_GROUP("arc4", test_wc_Arc4Process)
31+
#define TEST_ARC4_DECLS \
32+
TEST_DECL_GROUP("arc4", test_wc_Arc4SetKey), \
33+
TEST_DECL_GROUP("arc4", test_wc_Arc4Process), \
34+
TEST_DECL_GROUP("arc4", test_wc_Arc4_MonteCarlo)
3335

3436
#endif /* WOLFCRYPT_TEST_ARC4_H */

tests/api/test_camellia.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,70 @@ int test_wc_CamelliaCbcEncryptDecrypt(void)
214214
return EXPECT_RESULT();
215215
} /* END test_wc_CamelliaCbcEncryptDecrypt */
216216

217+
218+
#include <wolfssl/wolfcrypt/random.h>
219+
220+
#define MC_CIPHER_TEST_COUNT 100
221+
#define MC_CAMELLIA_MAX_DATA_SZ 1024
222+
223+
/* Monte Carlo test for Camellia-CBC: random key, IV, and plaintext each
224+
* iteration */
225+
int test_wc_CamelliaCbc_MonteCarlo(void)
226+
{
227+
EXPECT_DECLS;
228+
#ifdef HAVE_CAMELLIA
229+
static const word32 keySizes[] = {16, 24, 32};
230+
int numKeySizes = (int)(sizeof(keySizes) / sizeof(keySizes[0]));
231+
wc_Camellia camellia;
232+
WC_RNG rng;
233+
byte key[32];
234+
byte iv[WC_CAMELLIA_BLOCK_SIZE];
235+
word32 plainLen = 0, keyLen;
236+
int i;
237+
WC_DECLARE_VAR(plain, byte, MC_CAMELLIA_MAX_DATA_SZ, NULL);
238+
WC_DECLARE_VAR(cipher, byte, MC_CAMELLIA_MAX_DATA_SZ, NULL);
239+
WC_DECLARE_VAR(decrypted, byte, MC_CAMELLIA_MAX_DATA_SZ, NULL);
240+
241+
WC_ALLOC_VAR(plain, byte, MC_CAMELLIA_MAX_DATA_SZ, NULL);
242+
WC_ALLOC_VAR(cipher, byte, MC_CAMELLIA_MAX_DATA_SZ, NULL);
243+
WC_ALLOC_VAR(decrypted, byte, MC_CAMELLIA_MAX_DATA_SZ, NULL);
244+
#ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
245+
ExpectNotNull(plain);
246+
ExpectNotNull(cipher);
247+
ExpectNotNull(decrypted);
248+
#endif
249+
250+
XMEMSET(&camellia, 0, sizeof(camellia));
251+
XMEMSET(&rng, 0, sizeof(rng));
252+
253+
ExpectIntEQ(wc_InitRng(&rng), 0);
254+
255+
for (i = 0; i < MC_CIPHER_TEST_COUNT && EXPECT_SUCCESS(); i++) {
256+
keyLen = keySizes[i % numKeySizes];
257+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, keyLen), 0);
258+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, iv, sizeof(iv)), 0);
259+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, (byte*)&plainLen,
260+
sizeof(plainLen)), 0);
261+
/* Length 1..1024, rounded up to Camellia block size */
262+
plainLen = (plainLen % MC_CAMELLIA_MAX_DATA_SZ) + 1;
263+
plainLen = (plainLen + WC_CAMELLIA_BLOCK_SIZE - 1) &
264+
~((word32)WC_CAMELLIA_BLOCK_SIZE - 1);
265+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, plain, plainLen), 0);
266+
267+
ExpectIntEQ(wc_CamelliaSetKey(&camellia, key, keyLen, iv), 0);
268+
ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, cipher, plain,
269+
plainLen), 0);
270+
/* Reset IV by calling SetKey again before decrypt */
271+
ExpectIntEQ(wc_CamelliaSetKey(&camellia, key, keyLen, iv), 0);
272+
ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, decrypted, cipher,
273+
plainLen), 0);
274+
ExpectBufEQ(decrypted, plain, plainLen);
275+
}
276+
277+
wc_FreeRng(&rng);
278+
WC_FREE_VAR(plain, NULL);
279+
WC_FREE_VAR(cipher, NULL);
280+
WC_FREE_VAR(decrypted, NULL);
281+
#endif
282+
return EXPECT_RESULT();
283+
}

tests/api/test_camellia.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ int test_wc_CamelliaSetKey(void);
2828
int test_wc_CamelliaSetIV(void);
2929
int test_wc_CamelliaEncryptDecryptDirect(void);
3030
int test_wc_CamelliaCbcEncryptDecrypt(void);
31+
int test_wc_CamelliaCbc_MonteCarlo(void);
3132

3233
#define TEST_CAMELLIA_DECLS \
3334
TEST_DECL_GROUP("camellia", test_wc_CamelliaSetKey), \
3435
TEST_DECL_GROUP("camellia", test_wc_CamelliaSetIV), \
3536
TEST_DECL_GROUP("camellia", test_wc_CamelliaEncryptDecryptDirect), \
36-
TEST_DECL_GROUP("camellia", test_wc_CamelliaCbcEncryptDecrypt)
37+
TEST_DECL_GROUP("camellia", test_wc_CamelliaCbcEncryptDecrypt), \
38+
TEST_DECL_GROUP("camellia", test_wc_CamelliaCbc_MonteCarlo)
3739

3840
#endif /* WOLFCRYPT_TEST_CAMELLIA_H */

tests/api/test_chacha.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,64 @@ int test_wc_Chacha_Process_Chunking(void)
372372
} /* END test_wc_Chacha_Process */
373373

374374

375+
376+
#include <wolfssl/wolfcrypt/random.h>
377+
378+
#define MC_CIPHER_TEST_COUNT 100
379+
#define MC_CHACHA_MAX_DATA_SZ 1024
380+
381+
/* Monte Carlo test for ChaCha20: random key, IV, and plaintext each
382+
* iteration */
383+
int test_wc_Chacha_MonteCarlo(void)
384+
{
385+
EXPECT_DECLS;
386+
#ifdef HAVE_CHACHA
387+
ChaCha enc, dec;
388+
WC_RNG rng;
389+
byte key[CHACHA_MAX_KEY_SZ];
390+
byte nonce[CHACHA_IV_BYTES];
391+
word32 plainLen = 0;
392+
int i;
393+
WC_DECLARE_VAR(plain, byte, MC_CHACHA_MAX_DATA_SZ, NULL);
394+
WC_DECLARE_VAR(cipher, byte, MC_CHACHA_MAX_DATA_SZ, NULL);
395+
WC_DECLARE_VAR(decrypted, byte, MC_CHACHA_MAX_DATA_SZ, NULL);
396+
397+
WC_ALLOC_VAR(plain, byte, MC_CHACHA_MAX_DATA_SZ, NULL);
398+
WC_ALLOC_VAR(cipher, byte, MC_CHACHA_MAX_DATA_SZ, NULL);
399+
WC_ALLOC_VAR(decrypted, byte, MC_CHACHA_MAX_DATA_SZ, NULL);
400+
#ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
401+
ExpectNotNull(plain);
402+
ExpectNotNull(cipher);
403+
ExpectNotNull(decrypted);
404+
#endif
405+
406+
XMEMSET(&enc, 0, sizeof(enc));
407+
XMEMSET(&dec, 0, sizeof(dec));
408+
XMEMSET(&rng, 0, sizeof(rng));
409+
410+
ExpectIntEQ(wc_InitRng(&rng), 0);
411+
412+
for (i = 0; i < MC_CIPHER_TEST_COUNT && EXPECT_SUCCESS(); i++) {
413+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
414+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, nonce, sizeof(nonce)), 0);
415+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, (byte*)&plainLen,
416+
sizeof(plainLen)), 0);
417+
plainLen = (plainLen % MC_CHACHA_MAX_DATA_SZ) + 1;
418+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, plain, plainLen), 0);
419+
420+
ExpectIntEQ(wc_Chacha_SetKey(&enc, key, sizeof(key)), 0);
421+
ExpectIntEQ(wc_Chacha_SetKey(&dec, key, sizeof(key)), 0);
422+
ExpectIntEQ(wc_Chacha_SetIV(&enc, nonce, 0), 0);
423+
ExpectIntEQ(wc_Chacha_SetIV(&dec, nonce, 0), 0);
424+
ExpectIntEQ(wc_Chacha_Process(&enc, cipher, plain, plainLen), 0);
425+
ExpectIntEQ(wc_Chacha_Process(&dec, decrypted, cipher, plainLen), 0);
426+
ExpectBufEQ(decrypted, plain, plainLen);
427+
}
428+
429+
wc_FreeRng(&rng);
430+
WC_FREE_VAR(plain, NULL);
431+
WC_FREE_VAR(cipher, NULL);
432+
WC_FREE_VAR(decrypted, NULL);
433+
#endif
434+
return EXPECT_RESULT();
435+
}

tests/api/test_chacha.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
int test_wc_Chacha_SetKey(void);
2828
int test_wc_Chacha_Process(void);
2929
int test_wc_Chacha_Process_Chunking(void);
30+
int test_wc_Chacha_MonteCarlo(void);
3031

31-
#define TEST_CHACHA_DECLS \
32-
TEST_DECL_GROUP("chacha", test_wc_Chacha_SetKey), \
33-
TEST_DECL_GROUP("chacha", test_wc_Chacha_Process), \
34-
TEST_DECL_GROUP("chacha", test_wc_Chacha_Process_Chunking)
32+
#define TEST_CHACHA_DECLS \
33+
TEST_DECL_GROUP("chacha", test_wc_Chacha_SetKey), \
34+
TEST_DECL_GROUP("chacha", test_wc_Chacha_Process), \
35+
TEST_DECL_GROUP("chacha", test_wc_Chacha_Process_Chunking), \
36+
TEST_DECL_GROUP("chacha", test_wc_Chacha_MonteCarlo)
3537

3638
#endif /* WOLFCRYPT_TEST_CHACHA_H */

tests/api/test_chacha20_poly1305.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,60 @@ int test_wc_XChaCha20Poly1305_aead(void)
283283
#endif
284284
return EXPECT_RESULT();
285285
} /* END test_wc_XChaCha20Poly1305_aead */
286+
287+
#include <wolfssl/wolfcrypt/random.h>
288+
289+
#define MC_CIPHER_TEST_COUNT 100
290+
#define MC_CHACHA20P1305_MAX_SZ 1024
291+
292+
/* Monte Carlo test for ChaCha20-Poly1305: random key, nonce, and plaintext
293+
* each iteration */
294+
int test_wc_ChaCha20Poly1305_MonteCarlo(void)
295+
{
296+
EXPECT_DECLS;
297+
#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
298+
WC_RNG rng;
299+
byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
300+
byte nonce[CHACHA20_POLY1305_AEAD_IV_SIZE];
301+
byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
302+
word32 plainLen = 0;
303+
int i;
304+
WC_DECLARE_VAR(plain, byte, MC_CHACHA20P1305_MAX_SZ, NULL);
305+
WC_DECLARE_VAR(cipher, byte, MC_CHACHA20P1305_MAX_SZ, NULL);
306+
WC_DECLARE_VAR(decrypted, byte, MC_CHACHA20P1305_MAX_SZ, NULL);
307+
308+
WC_ALLOC_VAR(plain, byte, MC_CHACHA20P1305_MAX_SZ, NULL);
309+
WC_ALLOC_VAR(cipher, byte, MC_CHACHA20P1305_MAX_SZ, NULL);
310+
WC_ALLOC_VAR(decrypted, byte, MC_CHACHA20P1305_MAX_SZ, NULL);
311+
#ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
312+
ExpectNotNull(plain);
313+
ExpectNotNull(cipher);
314+
ExpectNotNull(decrypted);
315+
#endif
316+
317+
XMEMSET(&rng, 0, sizeof(rng));
318+
319+
ExpectIntEQ(wc_InitRng(&rng), 0);
320+
321+
for (i = 0; i < MC_CIPHER_TEST_COUNT && EXPECT_SUCCESS(); i++) {
322+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
323+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, nonce, sizeof(nonce)), 0);
324+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, (byte*)&plainLen,
325+
sizeof(plainLen)), 0);
326+
plainLen = (plainLen % MC_CHACHA20P1305_MAX_SZ) + 1;
327+
ExpectIntEQ(wc_RNG_GenerateBlock(&rng, plain, plainLen), 0);
328+
329+
ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, nonce, NULL, 0,
330+
plain, plainLen, cipher, tag), 0);
331+
ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, nonce, NULL, 0,
332+
cipher, plainLen, tag, decrypted), 0);
333+
ExpectBufEQ(decrypted, plain, plainLen);
334+
}
335+
336+
wc_FreeRng(&rng);
337+
WC_FREE_VAR(plain, NULL);
338+
WC_FREE_VAR(cipher, NULL);
339+
WC_FREE_VAR(decrypted, NULL);
340+
#endif
341+
return EXPECT_RESULT();
342+
}

tests/api/test_chacha20_poly1305.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626

2727
int test_wc_ChaCha20Poly1305_aead(void);
2828
int test_wc_XChaCha20Poly1305_aead(void);
29+
int test_wc_ChaCha20Poly1305_MonteCarlo(void);
2930

30-
#define TEST_CHACHA20_POLY1305_DECLS \
31-
TEST_DECL_GROUP("chacha20-poly1305", test_wc_ChaCha20Poly1305_aead), \
32-
TEST_DECL_GROUP("xchacha20-poly1305", test_wc_XChaCha20Poly1305_aead)
31+
#define TEST_CHACHA20_POLY1305_DECLS \
32+
TEST_DECL_GROUP("chacha20-poly1305", test_wc_ChaCha20Poly1305_aead), \
33+
TEST_DECL_GROUP("xchacha20-poly1305", test_wc_XChaCha20Poly1305_aead), \
34+
TEST_DECL_GROUP("chacha20-poly1305", test_wc_ChaCha20Poly1305_MonteCarlo)
3335

3436
#endif /* WOLFCRYPT_TEST_CHACHA20_POLY1305_H */

0 commit comments

Comments
 (0)