Skip to content

Commit 0f7a813

Browse files
chunkeeyherbertx
authored andcommitted
crypto: crypto4xx - block ciphers should only accept complete blocks
The hardware automatically zero pads incomplete block ciphers blocks without raising any errors. This is a screw-up. This was noticed by CONFIG_CRYPTO_MANAGER_EXTRA_TESTS tests that sent a incomplete blocks and expect them to fail. This fixes: cbc-aes-ppc4xx encryption unexpectedly succeeded on test vector "random: len=2409 klen=32"; expected_error=-22, cfg="random: may_sleep use_digest src_divs=[96.90%@+2295, 2.34%@+4066, 0.32%@alignmask+12, 0.34%@+4087, 0.9%@alignmask+1787, 0.1%@+3767] iv_offset=6" ecb-aes-ppc4xx encryption unexpectedly succeeded on test vector "random: len=1011 klen=32"; expected_error=-22, cfg="random: may_sleep use_digest src_divs=[100.0%@alignmask+20] dst_divs=[3.12%@+3001, 96.88%@+4070]" Cc: Eric Biggers <ebiggers@kernel.org> Cc: stable@vger.kernel.org [4.19, 5.0 and 5.1] Signed-off-by: Christian Lamparter <chunkeey@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 70c4997 commit 0f7a813

3 files changed

Lines changed: 39 additions & 23 deletions

File tree

‎drivers/crypto/amcc/crypto4xx_alg.c‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
7676
}
7777

7878
static inline int crypto4xx_crypt(struct skcipher_request *req,
79-
const unsigned int ivlen, bool decrypt)
79+
const unsigned int ivlen, bool decrypt,
80+
bool check_blocksize)
8081
{
8182
struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
8283
struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
8384
__le32 iv[AES_IV_SIZE];
8485

86+
if (check_blocksize && !IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE))
87+
return -EINVAL;
88+
8589
if (ivlen)
8690
crypto4xx_memcpy_to_le32(iv, req->iv, ivlen);
8791

@@ -90,24 +94,34 @@ static inline int crypto4xx_crypt(struct skcipher_request *req,
9094
ctx->sa_len, 0, NULL);
9195
}
9296

93-
int crypto4xx_encrypt_noiv(struct skcipher_request *req)
97+
int crypto4xx_encrypt_noiv_block(struct skcipher_request *req)
98+
{
99+
return crypto4xx_crypt(req, 0, false, true);
100+
}
101+
102+
int crypto4xx_encrypt_iv_stream(struct skcipher_request *req)
103+
{
104+
return crypto4xx_crypt(req, AES_IV_SIZE, false, false);
105+
}
106+
107+
int crypto4xx_decrypt_noiv_block(struct skcipher_request *req)
94108
{
95-
return crypto4xx_crypt(req, 0, false);
109+
return crypto4xx_crypt(req, 0, true, true);
96110
}
97111

98-
int crypto4xx_encrypt_iv(struct skcipher_request *req)
112+
int crypto4xx_decrypt_iv_stream(struct skcipher_request *req)
99113
{
100-
return crypto4xx_crypt(req, AES_IV_SIZE, false);
114+
return crypto4xx_crypt(req, AES_IV_SIZE, true, false);
101115
}
102116

103-
int crypto4xx_decrypt_noiv(struct skcipher_request *req)
117+
int crypto4xx_encrypt_iv_block(struct skcipher_request *req)
104118
{
105-
return crypto4xx_crypt(req, 0, true);
119+
return crypto4xx_crypt(req, AES_IV_SIZE, false, true);
106120
}
107121

108-
int crypto4xx_decrypt_iv(struct skcipher_request *req)
122+
int crypto4xx_decrypt_iv_block(struct skcipher_request *req)
109123
{
110-
return crypto4xx_crypt(req, AES_IV_SIZE, true);
124+
return crypto4xx_crypt(req, AES_IV_SIZE, true, true);
111125
}
112126

113127
/**
@@ -278,8 +292,8 @@ crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
278292
return ret;
279293
}
280294

281-
return encrypt ? crypto4xx_encrypt_iv(req)
282-
: crypto4xx_decrypt_iv(req);
295+
return encrypt ? crypto4xx_encrypt_iv_stream(req)
296+
: crypto4xx_decrypt_iv_stream(req);
283297
}
284298

285299
static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,

‎drivers/crypto/amcc/crypto4xx_core.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,8 +1219,8 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = {
12191219
.max_keysize = AES_MAX_KEY_SIZE,
12201220
.ivsize = AES_IV_SIZE,
12211221
.setkey = crypto4xx_setkey_aes_cbc,
1222-
.encrypt = crypto4xx_encrypt_iv,
1223-
.decrypt = crypto4xx_decrypt_iv,
1222+
.encrypt = crypto4xx_encrypt_iv_block,
1223+
.decrypt = crypto4xx_decrypt_iv_block,
12241224
.init = crypto4xx_sk_init,
12251225
.exit = crypto4xx_sk_exit,
12261226
} },
@@ -1239,8 +1239,8 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = {
12391239
.max_keysize = AES_MAX_KEY_SIZE,
12401240
.ivsize = AES_IV_SIZE,
12411241
.setkey = crypto4xx_setkey_aes_cfb,
1242-
.encrypt = crypto4xx_encrypt_iv,
1243-
.decrypt = crypto4xx_decrypt_iv,
1242+
.encrypt = crypto4xx_encrypt_iv_stream,
1243+
.decrypt = crypto4xx_decrypt_iv_stream,
12441244
.init = crypto4xx_sk_init,
12451245
.exit = crypto4xx_sk_exit,
12461246
} },
@@ -1299,8 +1299,8 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = {
12991299
.min_keysize = AES_MIN_KEY_SIZE,
13001300
.max_keysize = AES_MAX_KEY_SIZE,
13011301
.setkey = crypto4xx_setkey_aes_ecb,
1302-
.encrypt = crypto4xx_encrypt_noiv,
1303-
.decrypt = crypto4xx_decrypt_noiv,
1302+
.encrypt = crypto4xx_encrypt_noiv_block,
1303+
.decrypt = crypto4xx_decrypt_noiv_block,
13041304
.init = crypto4xx_sk_init,
13051305
.exit = crypto4xx_sk_exit,
13061306
} },
@@ -1319,8 +1319,8 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = {
13191319
.max_keysize = AES_MAX_KEY_SIZE,
13201320
.ivsize = AES_IV_SIZE,
13211321
.setkey = crypto4xx_setkey_aes_ofb,
1322-
.encrypt = crypto4xx_encrypt_iv,
1323-
.decrypt = crypto4xx_decrypt_iv,
1322+
.encrypt = crypto4xx_encrypt_iv_stream,
1323+
.decrypt = crypto4xx_decrypt_iv_stream,
13241324
.init = crypto4xx_sk_init,
13251325
.exit = crypto4xx_sk_exit,
13261326
} },

‎drivers/crypto/amcc/crypto4xx_core.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
182182
const u8 *key, unsigned int keylen);
183183
int crypto4xx_encrypt_ctr(struct skcipher_request *req);
184184
int crypto4xx_decrypt_ctr(struct skcipher_request *req);
185-
int crypto4xx_encrypt_iv(struct skcipher_request *req);
186-
int crypto4xx_decrypt_iv(struct skcipher_request *req);
187-
int crypto4xx_encrypt_noiv(struct skcipher_request *req);
188-
int crypto4xx_decrypt_noiv(struct skcipher_request *req);
185+
int crypto4xx_encrypt_iv_stream(struct skcipher_request *req);
186+
int crypto4xx_decrypt_iv_stream(struct skcipher_request *req);
187+
int crypto4xx_encrypt_iv_block(struct skcipher_request *req);
188+
int crypto4xx_decrypt_iv_block(struct skcipher_request *req);
189+
int crypto4xx_encrypt_noiv_block(struct skcipher_request *req);
190+
int crypto4xx_decrypt_noiv_block(struct skcipher_request *req);
189191
int crypto4xx_rfc3686_encrypt(struct skcipher_request *req);
190192
int crypto4xx_rfc3686_decrypt(struct skcipher_request *req);
191193
int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm);

0 commit comments

Comments
 (0)