Skip to content

Commit 87fb793

Browse files
authored
Merge pull request #125 from JacobBarthelmeh/unit-tests
script and adjustments for valgrind
2 parents f6a16f5 + d5b2931 commit 87fb793

File tree

11 files changed

+107
-192
lines changed

11 files changed

+107
-192
lines changed

scripts/valgrind-test.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
START=1
3+
END=`./test/unit.test --list | grep -c "test"`
4+
LOGFILE="unit-test-valgrind.log"
5+
6+
if [ ! -z "$1" ]; then
7+
START=$1
8+
fi
9+
10+
i=$START
11+
printf "Running valgrind test on individual unit test:\n"
12+
printf "(note use -DPURIFY with OpenSSL 1.0.2h)\n"
13+
while [[ $i -le $END ]]; do
14+
printf "testing case $i ..."
15+
valgrind --tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=5 --log-fd=9 --leak-check=full --show-leak-kinds=all ./test/unit.test --valgrind --static $i &> $LOGFILE
16+
if [ $? != 0 ]; then
17+
printf "failed\n"
18+
cat $LOGFILE
19+
printf "Error log stored in the file `pwd`/${LOGFILE}\n"
20+
exit 1
21+
fi
22+
printf "done\n"
23+
((i = i + 1))
24+
done
25+
printf "Completed all tests\n"
26+
rm $LOGFILE
27+
exit 0
28+

src/we_aes_block.c

Lines changed: 19 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,18 @@ static int we_aes_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
8585
}
8686
}
8787

88-
if ((ret == 1) && (!aes->init)) {
88+
if ((ret == 1) && (key != NULL)) {
8989
WOLFENGINE_MSG(WE_LOG_CIPHER,
9090
"Initializing wolfCrypt Aes structure: %p", &aes->aes);
9191
rc = wc_AesInit(&aes->aes, NULL, INVALID_DEVID);
9292
if (rc != 0) {
9393
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc);
9494
ret = 0;
9595
}
96+
aes->init = 1;
9697
}
9798

98-
if (ret == 1) {
99-
/* Must have initialized wolfSSL AES object when here. */
100-
aes->init = 1;
99+
if (ret == 1 && (aes->init == 1)) {
101100
aes->over = 0;
102101
/* Store whether encrypting. */
103102
aes->enc = enc;
@@ -148,87 +147,12 @@ static int we_aes_cbc_encrypt(we_AesBlock* aes, unsigned char *out,
148147

149148
WOLFENGINE_ENTER(WE_LOG_CIPHER, "we_aes_cbc_encrypt");
150149

151-
/* Length of 0 means Final called. */
152-
if (len == 0) {
153-
if (aes->over != 0) {
154-
WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER,
155-
"No Pad - last encrypt block not full");
156-
ret = 0;
157-
}
158-
}
159-
160-
if (ret == 1) {
161-
unsigned int l;
162-
163-
/* Check for cached data. */
164-
if (aes->over > 0) {
165-
WOLFENGINE_MSG(WE_LOG_CIPHER, "Encrypting leftover cached data, "
166-
"aes->over = %d", aes->over);
167-
168-
/* Partial block not yet encrypted. */
169-
l = AES_BLOCK_SIZE - aes->over;
170-
if (l > len) {
171-
l = (int)len;
172-
}
173-
174-
/* Copy as much of input as possible to fill in block. */
175-
if (l > 0) {
176-
XMEMCPY(aes->lastBlock + aes->over, in, l);
177-
aes->over += l;
178-
in += l;
179-
len -= l;
180-
}
181-
/* Check if we have a complete block to encrypt. */
182-
if (aes->over == AES_BLOCK_SIZE) {
183-
/* Encrypt and return block. */
184-
rc = wc_AesCbcEncrypt(&aes->aes, out, aes->lastBlock,
185-
AES_BLOCK_SIZE);
186-
if (rc != 0) {
187-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER,
188-
"wc_AesCbcEncrypt", rc);
189-
ret = 0;
190-
}
191-
else {
192-
WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER,
193-
"Encrypted %d bytes (AES-CBC)",
194-
AES_BLOCK_SIZE);
195-
WOLFENGINE_BUFFER(WE_LOG_CIPHER, out, AES_BLOCK_SIZE);
196-
}
197-
198-
/* Data put to output. */
199-
out += AES_BLOCK_SIZE;
200-
/* No more cached data. */
201-
aes->over = 0;
202-
203-
WOLFENGINE_MSG(WE_LOG_CIPHER, "Encrypted all cached data");
204-
}
205-
}
206-
/* Encrypt full blocks from remaining input. */
207-
if ((ret == 1) && (len >= AES_BLOCK_SIZE)) {
208-
/* Calculate full blocks. */
209-
l = (int)len & (~(AES_BLOCK_SIZE - 1));
210-
211-
rc = wc_AesCbcEncrypt(&aes->aes, out, in, l);
212-
if (rc != 0) {
213-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesCbcEncrypt", rc);
214-
ret = 0;
215-
}
216-
else {
217-
WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER,
218-
"Encrypted %d bytes (AES-CBC)", l);
219-
WOLFENGINE_BUFFER(WE_LOG_CIPHER, out, l);
220-
}
221-
222-
in += l;
223-
len -= l;
224-
}
225-
if ((ret == 1) && (len > 0)) {
226-
/* Copy remaining input as incomplete block. */
227-
XMEMCPY(aes->lastBlock, in, len);
228-
aes->over = (int)len;
229-
}
150+
/* padding is handled by OpenSSL before passed to we_aes_cbc_encrypt */
151+
rc = wc_AesCbcEncrypt(&aes->aes, out, in, (unsigned int)len);
152+
if (rc != 0) {
153+
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesCbcEncrypt", rc);
154+
ret = -1;
230155
}
231-
232156
WOLFENGINE_LEAVE(WE_LOG_CIPHER, "we_aes_cbc_encrypt", ret);
233157

234158
return ret;
@@ -249,94 +173,21 @@ static int we_aes_cbc_encrypt(we_AesBlock* aes, unsigned char *out,
249173
static int we_aes_cbc_decrypt(we_AesBlock* aes, unsigned char *out,
250174
const unsigned char *in, size_t len)
251175
{
252-
int ret = 1;
176+
int ret = 0;
253177
int rc;
254178

255179
WOLFENGINE_ENTER(WE_LOG_CIPHER, "we_aes_cbc_decrypt");
256180

257-
/* Length of 0 means Final called. */
258-
if (len == 0) {
259-
if (aes->over != 0) {
260-
WOLFENGINE_ERROR_MSG(WE_LOG_CIPHER,
261-
"No Pad - last decrypt block not full");
262-
ret = 0;
263-
}
181+
/* padding is handled by OpenSSL before passed to we_aes_cbc_decrypt */
182+
rc = wc_AesCbcDecrypt(&aes->aes, out, in, (unsigned int)len);
183+
if (rc == 0) {
184+
ret = (int)len;
264185
}
265-
if (ret == 1) {
266-
unsigned int l;
267-
268-
/* Check for cached data. */
269-
if (aes->over > 0) {
270-
WOLFENGINE_MSG(WE_LOG_CIPHER, "Decrypting leftover cached data, "
271-
"aes->over = %d", aes->over);
272-
273-
/* Calculate amount of input that can be used. */
274-
l = AES_BLOCK_SIZE - aes->over;
275-
if (l > len) {
276-
l = (int)len;
277-
}
278-
279-
if (l > 0) {
280-
/* Copy as much of input as possible to fill in block. */
281-
XMEMCPY(aes->lastBlock + aes->over, in, l);
282-
aes->over += l;
283-
in += l;
284-
len -= l;
285-
}
286-
/* Padding and not last full block or not padding and full block. */
287-
if ((aes->over == AES_BLOCK_SIZE) || len > 0) {
288-
/* Decrypt block cached block. */
289-
rc = wc_AesCbcDecrypt(&aes->aes, out, aes->lastBlock,
290-
AES_BLOCK_SIZE);
291-
if (rc != 0) {
292-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER,
293-
"wc_AesCbcDecrypt", rc);
294-
ret = 0;
295-
}
296-
else {
297-
WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER,
298-
"Decrypted %d bytes (AES-CBC)",
299-
AES_BLOCK_SIZE);
300-
WOLFENGINE_BUFFER(WE_LOG_CIPHER, out, AES_BLOCK_SIZE);
301-
}
302-
303-
/* Data put to output. */
304-
out += AES_BLOCK_SIZE;
305-
/* No more cached data. */
306-
aes->over = 0;
307-
}
308-
}
309-
/* Decrypt full blocks from remaining input. */
310-
if ((ret == 1) && (len >= AES_BLOCK_SIZE)) {
311-
/* Calculate full blocks. */
312-
l = (int)len & (~(AES_BLOCK_SIZE - 1));
313-
314-
if (l > 0) {
315-
rc = wc_AesCbcDecrypt(&aes->aes, out, in, l);
316-
if (rc != 0) {
317-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER,
318-
"wc_AesCbcDecrypt", rc);
319-
ret = 0;
320-
}
321-
else {
322-
WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER,
323-
"Decrypted %d bytes (AES-CBC)", l);
324-
WOLFENGINE_BUFFER(WE_LOG_CIPHER, out, l);
325-
}
326-
}
327-
328-
in += l;
329-
len -= l;
330-
}
331-
if ((ret == 1) && (len > 0)) {
332-
/* Copy remaining input as incomplete block. */
333-
XMEMCPY(aes->lastBlock, in, len);
334-
aes->over = (int)len;
335-
}
186+
else {
187+
ret = -1;
336188
}
337189

338190
WOLFENGINE_LEAVE(WE_LOG_CIPHER, "we_aes_cbc_decrypt", ret);
339-
340191
return ret;
341192
}
342193

@@ -601,19 +452,20 @@ static int we_aes_ecb_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
601452
ret = 0;
602453
}
603454

604-
if ((ret == 1) && ((key == NULL) || (!aes->init))) {
455+
if ((ret == 1) && (key == NULL)) {
605456
WOLFENGINE_MSG(WE_LOG_CIPHER,
606457
"Initializing wolfCrypt Aes structure: %p", &aes->aes);
607458
rc = wc_AesInit(&aes->aes, NULL, INVALID_DEVID);
608459
if (rc != 0) {
609460
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc);
610461
ret = 0;
611462
}
463+
else {
464+
aes->init = 1;
465+
}
612466
}
613467

614468
if (ret == 1) {
615-
/* Must have initialized wolfSSL AES object when here. */
616-
aes->init = 1;
617469
aes->over = 0;
618470
/* Store whether encrypting. */
619471
aes->enc = enc;

src/we_aes_ctr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int we_aes_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
6666
ret = 0;
6767
}
6868

69-
if ((ret == 1) && (((key == NULL) && (iv == NULL)) || (!aes->init))) {
69+
if ((ret == 1) && (((key == NULL) && (iv == NULL)))) {
7070
rc = wc_AesInit(&aes->aes, NULL, INVALID_DEVID);
7171
if (rc != 0) {
7272
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc);

src/we_des3_cbc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,22 @@ static int we_des3_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
7777
}
7878
}
7979

80-
if ((ret == 1) && (!des3->init)) {
80+
if ((ret == 1) && (key != NULL)) {
8181
WOLFENGINE_MSG(WE_LOG_CIPHER, "Initializing wolfCrypt Des3 "
8282
"structure: %p", &des3->des3);
8383
rc = wc_Des3Init(&des3->des3, NULL, INVALID_DEVID);
8484
if (rc != 0) {
8585
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_Des3Init", rc);
8686
ret = 0;
8787
}
88+
89+
des3->init = 1;
90+
91+
/* set des3->ivSet to 1 if iv buffer passed in is not NULL */
92+
des3->ivSet = (iv != NULL);
8893
}
8994

9095
if (ret == 1) {
91-
des3->init = 1;
9296
des3->enc = enc;
9397

9498
if (key != NULL) {

test/test_cipher.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static int test_cipher_enc(ENGINE *e, const EVP_CIPHER *cipher,
3131
int err;
3232
EVP_CIPHER_CTX *ctx;
3333
int encLen;
34-
int fLen;
34+
int fLen = 0;
3535

3636
err = (ctx = EVP_CIPHER_CTX_new()) == NULL;
3737
if (err == 0) {
@@ -105,11 +105,11 @@ static int test_cipher_enc_dec(ENGINE *e, void *data, const EVP_CIPHER *cipher,
105105

106106
(void)data;
107107

108-
if (RAND_bytes(key, keyLen) == 0) {
108+
if (RAND_bytes(key, keyLen) != 1) {
109109
err = 1;
110110
}
111111
if (err == 0) {
112-
if (RAND_bytes(iv, ivLen) == 0) {
112+
if (RAND_bytes(iv, ivLen) != 1) {
113113
err = 1;
114114
}
115115
}
@@ -280,11 +280,13 @@ static int test_stream_enc_dec(ENGINE *e, void *data, const EVP_CIPHER *cipher,
280280

281281
(void)data;
282282

283-
if (RAND_bytes(key, keyLen) == 0) {
283+
if (RAND_bytes(key, keyLen) != 1) {
284+
printf("generate key failed\n");
284285
err = 1;
285286
}
286287
if (err == 0) {
287-
if (RAND_bytes(iv, ivLen) == 0) {
288+
if (RAND_bytes(iv, ivLen) != 1) {
289+
printf("generate iv failed\n");
288290
err = 1;
289291
}
290292
}

0 commit comments

Comments
 (0)