Skip to content

Commit a176670

Browse files
authored
Merge pull request wolfSSL#214 from miyazakh/f-644_readofMAXLEN
F-644: fix read of exactly MAX_LEN bytes treated as error
2 parents 9bdc4ae + 7f91683 commit a176670

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/crypto/clu_decrypt.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,17 @@ int wolfCLU_decrypt(int alg, char* mode, byte* pwdKey, byte* key, int size,
156156
}
157157
else {
158158
ret = (int)XFREAD(input, 1, MAX_LEN, inFile);
159-
if ((ret > 0 && ret != MAX_LEN) || feof(inFile)) {
159+
if (ret > 0) {
160160
tempMax = ret;
161161
ret = 0; /* success */
162162
}
163163
else {
164-
wolfCLU_LogError("Input file does not exist.");
164+
if (feof(inFile)) {
165+
wolfCLU_LogError("Unexpected end of file.");
166+
}
167+
else {
168+
wolfCLU_LogError("File read error.");
169+
}
165170
ret = FREAD_ERROR;
166171
}
167172
}

tests/encrypt/enc-test.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,29 @@ fi
185185
rm -f test-dec.der
186186
rm -f test-enc.der
187187

188+
# camellia: decrypt file whose size is a multiple of MAX_LEN (2 x 1024 bytes)
189+
# to ensure the exact-boundary read case is covered (non-EVP path)
190+
if grep -q "HAVE_CAMELLIA" wolfssl/wolfssl/options.h 2>/dev/null; then
191+
dd if=/dev/urandom bs=2048 count=1 of=test_maxlen_camellia.bin 2>/dev/null
192+
./wolfssl encrypt camellia-cbc-128 -pwd testpwd \
193+
-in test_maxlen_camellia.bin -out test_maxlen_camellia.enc
194+
if [ $? != 0 ]; then
195+
echo "failed to encrypt in MAX_LEN boundary test"
196+
exit 99
197+
fi
198+
./wolfssl decrypt camellia-cbc-128 \
199+
-in test_maxlen_camellia.enc -out test_maxlen_camellia.dec -pwd testpwd
200+
if [ $? != 0 ]; then
201+
echo "failed to decrypt in MAX_LEN boundary test"
202+
exit 99
203+
fi
204+
diff test_maxlen_camellia.bin test_maxlen_camellia.dec &> /dev/null
205+
if [ $? != 0 ]; then
206+
echo "MAX_LEN boundary: decrypted file does not match original"
207+
exit 99
208+
fi
209+
rm -f test_maxlen_camellia.bin test_maxlen_camellia.enc test_maxlen_camellia.dec
210+
fi
211+
188212
echo "Done"
189213
exit 0

0 commit comments

Comments
 (0)