23 #include <crypto/aes.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
35 static int ccm_aes_nx_set_key(
struct crypto_aead *tfm,
56 memcpy(csbcpb->
cpb.aes_ccm.key, in_key, key_len);
59 memcpy(csbcpb_aead->
cpb.aes_cca.key, in_key, key_len);
65 static int ccm4309_aes_nx_set_key(
struct crypto_aead *tfm,
78 return ccm_aes_nx_set_key(tfm, in_key, key_len);
81 static int ccm_aes_nx_setauthsize(
struct crypto_aead *tfm,
82 unsigned int authsize)
97 crypto_aead_crt(tfm)->authsize = authsize;
102 static int ccm4309_aes_nx_setauthsize(
struct crypto_aead *tfm,
103 unsigned int authsize)
114 crypto_aead_crt(tfm)->authsize = authsize;
120 static int set_msg_len(
u8 *
block,
unsigned int msglen,
int csize)
129 else if (msglen > (
unsigned int)(1 << (8 * csize)))
133 memcpy(block - csize, (
u8 *)&data + 4 - csize, csize);
139 static inline int crypto_ccm_check_iv(
const u8 *
iv)
142 if (1 > iv[0] || iv[0] > 7)
149 static int generate_b0(
u8 *iv,
unsigned int assoclen,
unsigned int authsize,
150 unsigned int cryptlen,
u8 *b0)
152 unsigned int l,
lp,
m = authsize;
161 *b0 |= (8 * ((m - 2) / 2));
167 rc = set_msg_len(b0 + 16 - l, cryptlen, l);
172 static int generate_pat(
u8 *iv,
175 unsigned int authsize,
181 unsigned int iauth_len = 0;
182 struct vio_pfo_op *
op =
NULL;
187 memset(iv + 15 - iv[0], 0, iv[0] + 1);
190 b0 = nx_ctx->
csbcpb->cpb.aes_ccm.in_pat_or_b0;
195 b0 = nx_ctx->
csbcpb->cpb.aes_ccm.in_pat_or_b0;
196 b1 = nx_ctx->
priv.
ccm.iauth_tag;
205 nx_ctx->
op.inlen = (nx_ctx->
in_sg - nx_insg) *
206 sizeof(
struct nx_sg);
207 nx_ctx->
op.outlen = (nx_ctx->
out_sg - nx_outsg) *
208 sizeof(
struct nx_sg);
214 result = nx_ctx->
csbcpb->cpb.aes_ccm.out_pat_or_mac;
215 }
else if (req->
assoclen <= 65280) {
225 req->
assoc, iauth_len,
228 sizeof(
struct nx_sg);
231 result = nx_ctx->
csbcpb_aead->cpb.aes_cca.out_pat_or_b0;
236 pr_err(
"associated data len is %u bytes (returning -EINVAL)\n",
241 rc = generate_b0(iv, req->
assoclen, authsize, nbytes, b0);
271 unsigned int nbytes = req->
cryptlen;
272 unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
276 if (nbytes > nx_ctx->
ap->databytelen)
283 req->
src, nbytes, authsize,
286 rc = generate_pat(desc->
info, req, nx_ctx, authsize, nbytes,
287 csbcpb->
cpb.aes_ccm.in_pat_or_b0);
292 csbcpb->
cpb.aes_ccm.iv_or_ctr);
306 &(nx_ctx->
stats->aes_bytes));
319 unsigned int nbytes = req->
cryptlen;
320 unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
323 if (nbytes > nx_ctx->
ap->databytelen)
326 rc = generate_pat(desc->
info, req, nx_ctx, authsize, nbytes,
327 csbcpb->
cpb.aes_ccm.in_pat_or_b0);
332 csbcpb->
cpb.aes_ccm.iv_or_ctr);
346 &(nx_ctx->
stats->aes_bytes));
350 req->
dst, nbytes, authsize,
356 static int ccm4309_aes_nx_encrypt(
struct aead_request *req)
369 return ccm_nx_encrypt(req, &desc);
380 rc = crypto_ccm_check_iv(desc.
info);
384 return ccm_nx_encrypt(req, &desc);
387 static int ccm4309_aes_nx_decrypt(
struct aead_request *req)
400 return ccm_nx_decrypt(req, &desc);
411 rc = crypto_ccm_check_iv(desc.
info);
415 return ccm_nx_decrypt(req, &desc);
424 .cra_name =
"ccm(aes)",
425 .cra_driver_name =
"ccm-aes-nx",
438 .setkey = ccm_aes_nx_set_key,
439 .setauthsize = ccm_aes_nx_setauthsize,
440 .encrypt = ccm_aes_nx_encrypt,
441 .decrypt = ccm_aes_nx_decrypt,
446 .cra_name =
"rfc4309(ccm(aes))",
447 .cra_driver_name =
"rfc4309-ccm-aes-nx",
460 .setkey = ccm4309_aes_nx_set_key,
461 .setauthsize = ccm4309_aes_nx_setauthsize,
462 .encrypt = ccm4309_aes_nx_encrypt,
463 .decrypt = ccm4309_aes_nx_decrypt,