diff options
| author | Herbert Xu <herbert@gondor.apana.org.au> | 2015-07-07 05:30:25 -0400 |
|---|---|---|
| committer | Herbert Xu <herbert@gondor.apana.org.au> | 2015-07-08 03:14:13 -0400 |
| commit | 030f4e968741d65aea9cd5f7814d1164967801ef (patch) | |
| tree | f5bca6adaa4ba8f237cf0ef8a1e941e968b75ed0 | |
| parent | acb33cc541d7a5495b16a133702d4c401ea4e294 (diff) | |
crypto: nx - Fix reentrancy bugs
This patch fixes a host of reentrancy bugs in the nx driver. The
following algorithms are affected:
* CCM
* GCM
* CTR
* XCBC
* SHA256
* SHA512
The crypto API allows a single transform to be used by multiple
threads simultaneously. For example, IPsec will use a single tfm
to process packets for a given SA. As packets may arrive on
multiple CPUs that tfm must be reentrant.
The nx driver does try to deal with this by using a spin lock.
Unfortunately only the basic AES/CBC/ECB algorithms do this in
the correct way.
The symptom of these bugs may range from the generation of incorrect
output to memory corruption.
Cc: stable@vger.kernel.org
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
| -rw-r--r-- | drivers/crypto/nx/nx-aes-ccm.c | 6 | ||||
| -rw-r--r-- | drivers/crypto/nx/nx-aes-ctr.c | 7 | ||||
| -rw-r--r-- | drivers/crypto/nx/nx-aes-gcm.c | 17 | ||||
| -rw-r--r-- | drivers/crypto/nx/nx-aes-xcbc.c | 70 | ||||
| -rw-r--r-- | drivers/crypto/nx/nx-sha256.c | 43 | ||||
| -rw-r--r-- | drivers/crypto/nx/nx-sha512.c | 44 | ||||
| -rw-r--r-- | drivers/crypto/nx/nx.c | 3 | ||||
| -rw-r--r-- | drivers/crypto/nx/nx.h | 14 |
8 files changed, 125 insertions, 79 deletions
diff --git a/drivers/crypto/nx/nx-aes-ccm.c b/drivers/crypto/nx/nx-aes-ccm.c index 67f80813a06f..e4311ce0cd78 100644 --- a/drivers/crypto/nx/nx-aes-ccm.c +++ b/drivers/crypto/nx/nx-aes-ccm.c | |||
| @@ -494,8 +494,9 @@ out: | |||
| 494 | static int ccm4309_aes_nx_encrypt(struct aead_request *req) | 494 | static int ccm4309_aes_nx_encrypt(struct aead_request *req) |
| 495 | { | 495 | { |
| 496 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); | 496 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); |
| 497 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); | ||
| 497 | struct blkcipher_desc desc; | 498 | struct blkcipher_desc desc; |
| 498 | u8 *iv = nx_ctx->priv.ccm.iv; | 499 | u8 *iv = rctx->iv; |
| 499 | 500 | ||
| 500 | iv[0] = 3; | 501 | iv[0] = 3; |
| 501 | memcpy(iv + 1, nx_ctx->priv.ccm.nonce, 3); | 502 | memcpy(iv + 1, nx_ctx->priv.ccm.nonce, 3); |
| @@ -525,8 +526,9 @@ static int ccm_aes_nx_encrypt(struct aead_request *req) | |||
| 525 | static int ccm4309_aes_nx_decrypt(struct aead_request *req) | 526 | static int ccm4309_aes_nx_decrypt(struct aead_request *req) |
| 526 | { | 527 | { |
| 527 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); | 528 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); |
| 529 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); | ||
| 528 | struct blkcipher_desc desc; | 530 | struct blkcipher_desc desc; |
| 529 | u8 *iv = nx_ctx->priv.ccm.iv; | 531 | u8 *iv = rctx->iv; |
| 530 | 532 | ||
| 531 | iv[0] = 3; | 533 | iv[0] = 3; |
| 532 | memcpy(iv + 1, nx_ctx->priv.ccm.nonce, 3); | 534 | memcpy(iv + 1, nx_ctx->priv.ccm.nonce, 3); |
diff --git a/drivers/crypto/nx/nx-aes-ctr.c b/drivers/crypto/nx/nx-aes-ctr.c index 2617cd4d54dd..dd7e9f3f5b6b 100644 --- a/drivers/crypto/nx/nx-aes-ctr.c +++ b/drivers/crypto/nx/nx-aes-ctr.c | |||
| @@ -72,7 +72,7 @@ static int ctr3686_aes_nx_set_key(struct crypto_tfm *tfm, | |||
| 72 | if (key_len < CTR_RFC3686_NONCE_SIZE) | 72 | if (key_len < CTR_RFC3686_NONCE_SIZE) |
| 73 | return -EINVAL; | 73 | return -EINVAL; |
| 74 | 74 | ||
| 75 | memcpy(nx_ctx->priv.ctr.iv, | 75 | memcpy(nx_ctx->priv.ctr.nonce, |
| 76 | in_key + key_len - CTR_RFC3686_NONCE_SIZE, | 76 | in_key + key_len - CTR_RFC3686_NONCE_SIZE, |
| 77 | CTR_RFC3686_NONCE_SIZE); | 77 | CTR_RFC3686_NONCE_SIZE); |
| 78 | 78 | ||
| @@ -131,14 +131,15 @@ static int ctr3686_aes_nx_crypt(struct blkcipher_desc *desc, | |||
| 131 | unsigned int nbytes) | 131 | unsigned int nbytes) |
| 132 | { | 132 | { |
| 133 | struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm); | 133 | struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm); |
| 134 | u8 *iv = nx_ctx->priv.ctr.iv; | 134 | u8 iv[16]; |
| 135 | 135 | ||
| 136 | memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_IV_SIZE); | ||
| 136 | memcpy(iv + CTR_RFC3686_NONCE_SIZE, | 137 | memcpy(iv + CTR_RFC3686_NONCE_SIZE, |
| 137 | desc->info, CTR_RFC3686_IV_SIZE); | 138 | desc->info, CTR_RFC3686_IV_SIZE); |
| 138 | iv[12] = iv[13] = iv[14] = 0; | 139 | iv[12] = iv[13] = iv[14] = 0; |
| 139 | iv[15] = 1; | 140 | iv[15] = 1; |
| 140 | 141 | ||
| 141 | desc->info = nx_ctx->priv.ctr.iv; | 142 | desc->info = iv; |
| 142 | 143 | ||
| 143 | return ctr_aes_nx_crypt(desc, dst, src, nbytes); | 144 | return ctr_aes_nx_crypt(desc, dst, src, nbytes); |
| 144 | } | 145 | } |
diff --git a/drivers/crypto/nx/nx-aes-gcm.c b/drivers/crypto/nx/nx-aes-gcm.c index 08ac6d48688c..92c993f08213 100644 --- a/drivers/crypto/nx/nx-aes-gcm.c +++ b/drivers/crypto/nx/nx-aes-gcm.c | |||
| @@ -317,6 +317,7 @@ out: | |||
| 317 | static int gcm_aes_nx_crypt(struct aead_request *req, int enc) | 317 | static int gcm_aes_nx_crypt(struct aead_request *req, int enc) |
| 318 | { | 318 | { |
| 319 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); | 319 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); |
| 320 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); | ||
| 320 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; | 321 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 321 | struct blkcipher_desc desc; | 322 | struct blkcipher_desc desc; |
| 322 | unsigned int nbytes = req->cryptlen; | 323 | unsigned int nbytes = req->cryptlen; |
| @@ -326,7 +327,7 @@ static int gcm_aes_nx_crypt(struct aead_request *req, int enc) | |||
| 326 | 327 | ||
| 327 | spin_lock_irqsave(&nx_ctx->lock, irq_flags); | 328 | spin_lock_irqsave(&nx_ctx->lock, irq_flags); |
| 328 | 329 | ||
| 329 | desc.info = nx_ctx->priv.gcm.iv; | 330 | desc.info = rctx->iv; |
| 330 | /* initialize the counter */ | 331 | /* initialize the counter */ |
| 331 | *(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1; | 332 | *(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1; |
| 332 | 333 | ||
| @@ -424,8 +425,8 @@ out: | |||
| 424 | 425 | ||
| 425 | static int gcm_aes_nx_encrypt(struct aead_request *req) | 426 | static int gcm_aes_nx_encrypt(struct aead_request *req) |
| 426 | { | 427 | { |
| 427 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); | 428 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); |
| 428 | char *iv = nx_ctx->priv.gcm.iv; | 429 | char *iv = rctx->iv; |
| 429 | 430 | ||
| 430 | memcpy(iv, req->iv, 12); | 431 | memcpy(iv, req->iv, 12); |
| 431 | 432 | ||
| @@ -434,8 +435,8 @@ static int gcm_aes_nx_encrypt(struct aead_request *req) | |||
| 434 | 435 | ||
| 435 | static int gcm_aes_nx_decrypt(struct aead_request *req) | 436 | static int gcm_aes_nx_decrypt(struct aead_request *req) |
| 436 | { | 437 | { |
| 437 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); | 438 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); |
| 438 | char *iv = nx_ctx->priv.gcm.iv; | 439 | char *iv = rctx->iv; |
| 439 | 440 | ||
| 440 | memcpy(iv, req->iv, 12); | 441 | memcpy(iv, req->iv, 12); |
| 441 | 442 | ||
| @@ -445,7 +446,8 @@ static int gcm_aes_nx_decrypt(struct aead_request *req) | |||
| 445 | static int gcm4106_aes_nx_encrypt(struct aead_request *req) | 446 | static int gcm4106_aes_nx_encrypt(struct aead_request *req) |
| 446 | { | 447 | { |
| 447 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); | 448 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); |
| 448 | char *iv = nx_ctx->priv.gcm.iv; | 449 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); |
| 450 | char *iv = rctx->iv; | ||
| 449 | char *nonce = nx_ctx->priv.gcm.nonce; | 451 | char *nonce = nx_ctx->priv.gcm.nonce; |
| 450 | 452 | ||
| 451 | memcpy(iv, nonce, NX_GCM4106_NONCE_LEN); | 453 | memcpy(iv, nonce, NX_GCM4106_NONCE_LEN); |
| @@ -457,7 +459,8 @@ static int gcm4106_aes_nx_encrypt(struct aead_request *req) | |||
| 457 | static int gcm4106_aes_nx_decrypt(struct aead_request *req) | 459 | static int gcm4106_aes_nx_decrypt(struct aead_request *req) |
| 458 | { | 460 | { |
| 459 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); | 461 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm); |
| 460 | char *iv = nx_ctx->priv.gcm.iv; | 462 | struct nx_gcm_rctx *rctx = aead_request_ctx(req); |
| 463 | char *iv = rctx->iv; | ||
| 461 | char *nonce = nx_ctx->priv.gcm.nonce; | 464 | char *nonce = nx_ctx->priv.gcm.nonce; |
| 462 | 465 | ||
| 463 | memcpy(iv, nonce, NX_GCM4106_NONCE_LEN); | 466 | memcpy(iv, nonce, NX_GCM4106_NONCE_LEN); |
diff --git a/drivers/crypto/nx/nx-aes-xcbc.c b/drivers/crypto/nx/nx-aes-xcbc.c index 8c2faffab4a3..c2f7d4befb55 100644 --- a/drivers/crypto/nx/nx-aes-xcbc.c +++ b/drivers/crypto/nx/nx-aes-xcbc.c | |||
| @@ -42,6 +42,7 @@ static int nx_xcbc_set_key(struct crypto_shash *desc, | |||
| 42 | unsigned int key_len) | 42 | unsigned int key_len) |
| 43 | { | 43 | { |
| 44 | struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(desc); | 44 | struct nx_crypto_ctx *nx_ctx = crypto_shash_ctx(desc); |
| 45 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; | ||
| 45 | 46 | ||
| 46 | switch (key_len) { | 47 | switch (key_len) { |
| 47 | case AES_KEYSIZE_128: | 48 | case AES_KEYSIZE_128: |
| @@ -51,7 +52,7 @@ static int nx_xcbc_set_key(struct crypto_shash *desc, | |||
| 51 | return -EINVAL; | 52 | return -EINVAL; |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | memcpy(nx_ctx->priv.xcbc.key, in_key, key_len); | 55 | memcpy(csbcpb->cpb.aes_xcbc.key, in_key, key_len); |
| 55 | 56 | ||
| 56 | return 0; | 57 | return 0; |
| 57 | } | 58 | } |
| @@ -148,32 +149,29 @@ out: | |||
| 148 | return rc; | 149 | return rc; |
| 149 | } | 150 | } |
| 150 | 151 | ||
| 151 | static int nx_xcbc_init(struct shash_desc *desc) | 152 | static int nx_crypto_ctx_aes_xcbc_init2(struct crypto_tfm *tfm) |
| 152 | { | 153 | { |
| 153 | struct xcbc_state *sctx = shash_desc_ctx(desc); | 154 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm); |
| 154 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); | ||
| 155 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; | 155 | struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 156 | struct nx_sg *out_sg; | 156 | int err; |
| 157 | int len; | ||
| 158 | 157 | ||
| 159 | nx_ctx_init(nx_ctx, HCOP_FC_AES); | 158 | err = nx_crypto_ctx_aes_xcbc_init(tfm); |
| 159 | if (err) | ||
| 160 | return err; | ||
| 160 | 161 | ||
| 161 | memset(sctx, 0, sizeof *sctx); | 162 | nx_ctx_init(nx_ctx, HCOP_FC_AES); |
| 162 | 163 | ||
| 163 | NX_CPB_SET_KEY_SIZE(csbcpb | ||
