aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexei A. Smekalkine <ikle@ikle.ru>2020-12-01 00:20:59 +0300
committerDmitry Belyavskiy <beldmit@gmail.com>2020-12-06 18:54:38 +0300
commitfbd7748fde5397eb39e398f06ee31c1827e68201 (patch)
tree973d6232bc0e197bde2d415388514334c35379b6
parent672ef82b66374cde3b74140a9b78891cda451fac (diff)
downloadgost-engine-fbd7748fde5397eb39e398f06ee31c1827e68201.zip
gost-engine-fbd7748fde5397eb39e398f06ee31c1827e68201.tar.gz
gost-engine-fbd7748fde5397eb39e398f06ee31c1827e68201.tar.bz2
gost_crypt: process full available block in CFB and CNT mode
If at the input of the encryption function in the CFB mode we have an integer number of blocks, then in the main loop all blocks will be processed, except for the last one due to an incorrect border check. The last block will be fully processed as a "partial" remainder, but the initialization vector will not be updated. And, thus, the value of IV will always be incorrect in this case. This breaks stateless protocols due to an invalid initialization vector: all messages except the first cannot be decrypted. (Naturally, we are talking about a case with disabled key meshing, which does not allow context recovery due to an erroneous implementation.) It is worth noting here that the code for processing partial blocks (both at the input of the encryption functions and at the output) is a historically unnecessary artifact, since we do not set the EVP_CIPH_FLAG_CUSTOM_CIPHER flag and, as a result, OpenSSL processes partial blocks for us. This patch corrects the checking of the main loop boundary. A similar error is present in the code for the CNT mode, but there it does not manifest itself in any way, because the restoration of the state in this mode is impossible: even after disabling key meshing, we still have the state-dependent transformation of the IV. As an extra result of this fix, the code for processing partial blocks can be completely removed now. (cherry picked from commit cf402dd4d89271d5b1ca4ea938ce7a2f13a44d58)
-rw-r--r--gost_crypt.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/gost_crypt.c b/gost_crypt.c
index e9f311e..3af9abe 100644
--- a/gost_crypt.c
+++ b/gost_crypt.c
@@ -762,7 +762,7 @@ int gost_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
}
}
- for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
+ for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
/*
* block cipher current iv
*/
@@ -823,7 +823,7 @@ static int gost_cipher_do_cnt(EVP_CIPHER_CTX *ctx, unsigned char *out,
}
}
- for (; i + 8 < inl; i += 8, in_ptr += 8, out_ptr += 8) {
+ for (; (inl - i) >= 8; i += 8, in_ptr += 8, out_ptr += 8) {
/*
* block cipher current iv
*/