Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nx-aes-ecb.c
Go to the documentation of this file.
1 
22 #include <crypto/aes.h>
23 #include <crypto/algapi.h>
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/crypto.h>
27 #include <asm/vio.h>
28 
29 #include "nx_csbcpb.h"
30 #include "nx.h"
31 
32 
33 static int ecb_aes_nx_set_key(struct crypto_tfm *tfm,
34  const u8 *in_key,
35  unsigned int key_len)
36 {
37  struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
38  struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
39 
40  nx_ctx_init(nx_ctx, HCOP_FC_AES);
41 
42  switch (key_len) {
43  case AES_KEYSIZE_128:
45  nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
46  break;
47  case AES_KEYSIZE_192:
49  nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
50  break;
51  case AES_KEYSIZE_256:
53  nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
54  break;
55  default:
56  return -EINVAL;
57  }
58 
59  csbcpb->cpb.hdr.mode = NX_MODE_AES_ECB;
60  memcpy(csbcpb->cpb.aes_ecb.key, in_key, key_len);
61 
62  return 0;
63 }
64 
65 static int ecb_aes_nx_crypt(struct blkcipher_desc *desc,
66  struct scatterlist *dst,
67  struct scatterlist *src,
68  unsigned int nbytes,
69  int enc)
70 {
71  struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm);
72  struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
73  int rc;
74 
75  if (nbytes > nx_ctx->ap->databytelen)
76  return -EINVAL;
77 
78  if (enc)
80  else
81  NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
82 
83  rc = nx_build_sg_lists(nx_ctx, desc, dst, src, nbytes, NULL);
84  if (rc)
85  goto out;
86 
87  if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
88  rc = -EINVAL;
89  goto out;
90  }
91 
92  rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
94  if (rc)
95  goto out;
96 
97  atomic_inc(&(nx_ctx->stats->aes_ops));
98  atomic64_add(csbcpb->csb.processed_byte_count,
99  &(nx_ctx->stats->aes_bytes));
100 out:
101  return rc;
102 }
103 
104 static int ecb_aes_nx_encrypt(struct blkcipher_desc *desc,
105  struct scatterlist *dst,
106  struct scatterlist *src,
107  unsigned int nbytes)
108 {
109  return ecb_aes_nx_crypt(desc, dst, src, nbytes, 1);
110 }
111 
112 static int ecb_aes_nx_decrypt(struct blkcipher_desc *desc,
113  struct scatterlist *dst,
114  struct scatterlist *src,
115  unsigned int nbytes)
116 {
117  return ecb_aes_nx_crypt(desc, dst, src, nbytes, 0);
118 }
119 
121  .cra_name = "ecb(aes)",
122  .cra_driver_name = "ecb-aes-nx",
123  .cra_priority = 300,
124  .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
125  .cra_blocksize = AES_BLOCK_SIZE,
126  .cra_ctxsize = sizeof(struct nx_crypto_ctx),
127  .cra_type = &crypto_blkcipher_type,
128  .cra_module = THIS_MODULE,
129  .cra_init = nx_crypto_ctx_aes_ecb_init,
130  .cra_exit = nx_crypto_ctx_exit,
131  .cra_blkcipher = {
132  .min_keysize = AES_MIN_KEY_SIZE,
133  .max_keysize = AES_MAX_KEY_SIZE,
134  .setkey = ecb_aes_nx_set_key,
135  .encrypt = ecb_aes_nx_encrypt,
136  .decrypt = ecb_aes_nx_decrypt,
137  }
138 };