Skip to content

Commit 1a20b96

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: arm/aes - don't use IV buffer to return final keystream block
The ARM bit sliced AES core code uses the IV buffer to pass the final keystream block back to the glue code if the input is not a multiple of the block size, so that the asm code does not have to deal with anything except 16 byte blocks. This is done under the assumption that the outgoing IV is meaningless anyway in this case, given that chaining is no longer possible under these circumstances. However, as it turns out, the CCM driver does expect the IV to retain a value that is equal to the original IV except for the counter value, and even interprets byte zero as a length indicator, which may result in memory corruption if the IV is overwritten with something else. So use a separate buffer to return the final keystream block. Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 88a3f58 commit 1a20b96

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

arch/arm/crypto/aes-neonbs-core.S

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -779,14 +779,15 @@ ENDPROC(aesbs_cbc_decrypt)
779779

780780
/*
781781
* aesbs_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[],
782-
* int rounds, int blocks, u8 ctr[], bool final)
782+
* int rounds, int blocks, u8 ctr[], u8 final[])
783783
*/
784784
ENTRY(aesbs_ctr_encrypt)
785785
mov ip, sp
786786
push {r4-r10, lr}
787787

788788
ldm ip, {r5-r7} // load args 4-6
789-
add r5, r5, r7 // one extra block if final == 1
789+
teq r7, #0
790+
addne r5, r5, #1 // one extra block if final != 0
790791

791792
vld1.8 {q0}, [r6] // load counter
792793
vrev32.8 q1, q0
@@ -865,19 +866,20 @@ ENTRY(aesbs_ctr_encrypt)
865866
veor q2, q2, q14
866867
vst1.8 {q2}, [r0]!
867868
teq r4, #0 // skip last block if 'final'
868-
W(bne) 4f
869+
W(bne) 5f
869870
3: veor q5, q5, q15
870871
vst1.8 {q5}, [r0]!
871872

872-
next_ctr q0
873+
4: next_ctr q0
873874

874875
subs r5, r5, #8
875876
bgt 99b
876877

877-
vmov q5, q0
878-
879-
4: vst1.8 {q5}, [r6]
878+
vst1.8 {q0}, [r6]
880879
pop {r4-r10, pc}
880+
881+
5: vst1.8 {q5}, [r4]
882+
b 4b
881883
ENDPROC(aesbs_ctr_encrypt)
882884

883885
.macro next_tweak, out, in, const, tmp

arch/arm/crypto/aes-neonbs-glue.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ asmlinkage void aesbs_cbc_decrypt(u8 out[], u8 const in[], u8 const rk[],
3535
int rounds, int blocks, u8 iv[]);
3636

3737
asmlinkage void aesbs_ctr_encrypt(u8 out[], u8 const in[], u8 const rk[],
38-
int rounds, int blocks, u8 ctr[], bool final);
38+
int rounds, int blocks, u8 ctr[], u8 final[]);
3939

4040
asmlinkage void aesbs_xts_encrypt(u8 out[], u8 const in[], u8 const rk[],
4141
int rounds, int blocks, u8 iv[]);
@@ -186,19 +186,20 @@ static int ctr_encrypt(struct skcipher_request *req)
186186
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
187187
struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
188188
struct skcipher_walk walk;
189+
u8 buf[AES_BLOCK_SIZE];
189190
int err;
190191

191192
err = skcipher_walk_virt(&walk, req, true);
192193

193194
kernel_neon_begin();
194195
while (walk.nbytes > 0) {
195196
unsigned int blocks = walk.nbytes / AES_BLOCK_SIZE;
196-
bool final = (walk.total % AES_BLOCK_SIZE) != 0;
197+
u8 *final = (walk.total % AES_BLOCK_SIZE) ? buf : NULL;
197198

198199
if (walk.nbytes < walk.total) {
199200
blocks = round_down(blocks,
200201
walk.stride / AES_BLOCK_SIZE);
201-
final = false;
202+
final = NULL;
202203
}
203204

204205
aesbs_ctr_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
@@ -210,7 +211,7 @@ static int ctr_encrypt(struct skcipher_request *req)
210211

211212
if (dst != src)
212213
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
213-
crypto_xor(dst, walk.iv, walk.total % AES_BLOCK_SIZE);
214+
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
214215

215216
err = skcipher_walk_done(&walk, 0);
216217
break;

0 commit comments

Comments
 (0)