| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* AES CTR routines supporting the Power 7+ Nest Accelerators driver |
| 4 |
* |
| 5 |
* Copyright (C) 2011-2012 International Business Machines Inc. |
| 6 |
* |
| 7 |
* Author: Kent Yoder <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <crypto/aes.h> |
| 11 |
#include <crypto/ctr.h> |
| 12 |
#include <crypto/internal/skcipher.h> |
| 13 |
#include <linux/err.h> |
| 14 |
#include <linux/kernel.h> |
| 15 |
#include <linux/module.h> |
| 16 |
#include <linux/spinlock.h> |
| 17 |
#include <linux/string.h> |
| 18 |
#include <asm/vio.h> |
| 19 |
|
| 20 |
#include "nx_csbcpb.h" |
| 21 |
#include "nx.h" |
| 22 |
|
| 23 |
|
| 24 |
static int ctr_aes_nx_set_key(struct crypto_skcipher *tfm, |
| 25 |
const u8 *in_key, |
| 26 |
unsigned int key_len) |
| 27 |
{ |
| 28 |
struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm); |
| 29 |
struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 30 |
|
| 31 |
nx_ctx_init(nx_ctx, HCOP_FC_AES); |
| 32 |
|
| 33 |
switch (key_len) { |
| 34 |
case AES_KEYSIZE_128: |
| 35 |
NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128); |
| 36 |
nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128]; |
| 37 |
break; |
| 38 |
case AES_KEYSIZE_192: |
| 39 |
NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192); |
| 40 |
nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192]; |
| 41 |
break; |
| 42 |
case AES_KEYSIZE_256: |
| 43 |
NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256); |
| 44 |
nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256]; |
| 45 |
break; |
| 46 |
default: |
| 47 |
return -EINVAL; |
| 48 |
} |
| 49 |
|
| 50 |
csbcpb->cpb.hdr.mode = NX_MODE_AES_CTR; |
| 51 |
memcpy(csbcpb->cpb.aes_ctr.key, in_key, key_len); |
| 52 |
|
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
static int ctr3686_aes_nx_set_key(struct crypto_skcipher *tfm, |
| 57 |
const u8 *in_key, |
| 58 |
unsigned int key_len) |
| 59 |
{ |
| 60 |
struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm); |
| 61 |
|
| 62 |
if (key_len < CTR_RFC3686_NONCE_SIZE) |
| 63 |
return -EINVAL; |
| 64 |
|
| 65 |
memcpy(nx_ctx->priv.ctr.nonce, |
| 66 |
in_key + key_len - CTR_RFC3686_NONCE_SIZE, |
| 67 |
CTR_RFC3686_NONCE_SIZE); |
| 68 |
|
| 69 |
key_len -= CTR_RFC3686_NONCE_SIZE; |
| 70 |
|
| 71 |
return ctr_aes_nx_set_key(tfm, in_key, key_len); |
| 72 |
} |
| 73 |
|
| 74 |
static int ctr_aes_nx_crypt(struct skcipher_request *req, u8 *iv) |
| 75 |
{ |
| 76 |
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 77 |
struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm); |
| 78 |
struct nx_csbcpb *csbcpb = nx_ctx->csbcpb; |
| 79 |
unsigned long irq_flags; |
| 80 |
unsigned int processed = 0, to_process; |
| 81 |
int rc; |
| 82 |
|
| 83 |
spin_lock_irqsave(&nx_ctx->lock, irq_flags); |
| 84 |
|
| 85 |
do { |
| 86 |
to_process = req->cryptlen - processed; |
| 87 |
|
| 88 |
rc = nx_build_sg_lists(nx_ctx, iv, req->dst, req->src, |
| 89 |
&to_process, processed, |
| 90 |
csbcpb->cpb.aes_ctr.iv); |
| 91 |
if (rc) |
| 92 |
goto out; |
| 93 |
|
| 94 |
if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) { |
| 95 |
rc = -EINVAL; |
| 96 |
goto out; |
| 97 |
} |
| 98 |
|
| 99 |
rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 100 |
req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 101 |
if (rc) |
| 102 |
goto out; |
| 103 |
|
| 104 |
memcpy(iv, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE); |
| 105 |
|
| 106 |
atomic_inc(&(nx_ctx->stats->aes_ops)); |
| 107 |
atomic64_add(be32_to_cpu(csbcpb->csb.processed_byte_count), |
| 108 |
&(nx_ctx->stats->aes_bytes)); |
| 109 |
|
| 110 |
processed += to_process; |
| 111 |
} while (processed < req->cryptlen); |
| 112 |
out: |
| 113 |
spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); |
| 114 |
return rc; |
| 115 |
} |
| 116 |
|
| 117 |
static int ctr3686_aes_nx_crypt(struct skcipher_request *req) |
| 118 |
{ |
| 119 |
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 120 |
struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm); |
| 121 |
u8 iv[16]; |
| 122 |
|
| 123 |
memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_NONCE_SIZE); |
| 124 |
memcpy(iv + CTR_RFC3686_NONCE_SIZE, req->iv, CTR_RFC3686_IV_SIZE); |
| 125 |
iv[12] = iv[13] = iv[14] = 0; |
| 126 |
iv[15] = 1; |
| 127 |
|
| 128 |
return ctr_aes_nx_crypt(req, iv); |
| 129 |
} |
| 130 |
|
| 131 |
struct skcipher_alg nx_ctr3686_aes_alg = { |
| 132 |
.base.cra_name = "rfc3686(ctr(aes))", |
| 133 |
.base.cra_driver_name = "rfc3686-ctr-aes-nx", |
| 134 |
.base.cra_priority = 300, |
| 135 |
.base.cra_blocksize = 1, |
| 136 |
.base.cra_ctxsize = sizeof(struct nx_crypto_ctx), |
| 137 |
.base.cra_module = THIS_MODULE, |
| 138 |
.init = nx_crypto_ctx_aes_ctr_init, |
| 139 |
.exit = nx_crypto_ctx_skcipher_exit, |
| 140 |
.min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE, |
| 141 |
.max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE, |
| 142 |
.ivsize = CTR_RFC3686_IV_SIZE, |
| 143 |
.setkey = ctr3686_aes_nx_set_key, |
| 144 |
.encrypt = ctr3686_aes_nx_crypt, |
| 145 |
.decrypt = ctr3686_aes_nx_crypt, |
| 146 |
.chunksize = AES_BLOCK_SIZE, |
| 147 |
}; |