Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nx-sha512.c
Go to the documentation of this file.
1 
22 #include <crypto/internal/hash.h>
23 #include <crypto/sha.h>
24 #include <linux/module.h>
25 #include <asm/vio.h>
26 
27 #include "nx_csbcpb.h"
28 #include "nx.h"
29 
30 
31 static int nx_sha512_init(struct shash_desc *desc)
32 {
33  struct sha512_state *sctx = shash_desc_ctx(desc);
34  struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
35  struct nx_sg *out_sg;
36 
37  nx_ctx_init(nx_ctx, HCOP_FC_SHA);
38 
39  memset(sctx, 0, sizeof *sctx);
40 
41  nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512];
42 
44  out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state,
45  SHA512_DIGEST_SIZE, nx_ctx->ap->sglen);
46  nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
47 
48  return 0;
49 }
50 
51 static int nx_sha512_update(struct shash_desc *desc, const u8 *data,
52  unsigned int len)
53 {
54  struct sha512_state *sctx = shash_desc_ctx(desc);
55  struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
56  struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
57  struct nx_sg *in_sg;
58  u64 to_process, leftover, spbc_bits;
59  int rc = 0;
60 
61  if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
62  /* we've hit the nx chip previously and we're updating again,
63  * so copy over the partial digest */
64  memcpy(csbcpb->cpb.sha512.input_partial_digest,
65  csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
66  }
67 
68  /* 2 cases for total data len:
69  * 1: <= SHA512_BLOCK_SIZE: copy into state, return 0
70  * 2: > SHA512_BLOCK_SIZE: process X blocks, copy in leftover
71  */
72  if ((u64)len + sctx->count[0] <= SHA512_BLOCK_SIZE) {
73  memcpy(sctx->buf + sctx->count[0], data, len);
74  sctx->count[0] += len;
75  goto out;
76  }
77 
78  /* to_process: the SHA512_BLOCK_SIZE data chunk to process in this
79  * update */
80  to_process = (sctx->count[0] + len) & ~(SHA512_BLOCK_SIZE - 1);
81  leftover = (sctx->count[0] + len) & (SHA512_BLOCK_SIZE - 1);
82 
83  if (sctx->count[0]) {
84  in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)sctx->buf,
85  sctx->count[0], nx_ctx->ap->sglen);
86  in_sg = nx_build_sg_list(in_sg, (u8 *)data,
87  to_process - sctx->count[0],
88  nx_ctx->ap->sglen);
89  nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
90  sizeof(struct nx_sg);
91  } else {
92  in_sg = nx_build_sg_list(nx_ctx->in_sg, (u8 *)data,
93  to_process, nx_ctx->ap->sglen);
94  nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) *
95  sizeof(struct nx_sg);
96  }
97 
99 
100  if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
101  rc = -EINVAL;
102  goto out;
103  }
104 
105  rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
107  if (rc)
108  goto out;
109 
110  atomic_inc(&(nx_ctx->stats->sha512_ops));
111 
112  /* copy the leftover back into the state struct */
113  memcpy(sctx->buf, data + len - leftover, leftover);
114  sctx->count[0] = leftover;
115 
116  spbc_bits = csbcpb->cpb.sha512.spbc * 8;
117  csbcpb->cpb.sha512.message_bit_length_lo += spbc_bits;
118  if (csbcpb->cpb.sha512.message_bit_length_lo < spbc_bits)
119  csbcpb->cpb.sha512.message_bit_length_hi++;
120 
121  /* everything after the first update is continuation */
122  NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
123 out:
124  return rc;
125 }
126 
127 static int nx_sha512_final(struct shash_desc *desc, u8 *out)
128 {
129  struct sha512_state *sctx = shash_desc_ctx(desc);
130  struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
131  struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
132  struct nx_sg *in_sg, *out_sg;
133  u64 count0;
134  int rc;
135 
136  if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) {
137  /* we've hit the nx chip previously, now we're finalizing,
138  * so copy over the partial digest */
139  memcpy(csbcpb->cpb.sha512.input_partial_digest,
140  csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
141  }
142 
143  /* final is represented by continuing the operation and indicating that
144  * this is not an intermediate operation */
145  NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE;
146 
147  count0 = sctx->count[0] * 8;
148 
149  csbcpb->cpb.sha512.message_bit_length_lo += count0;
150  if (csbcpb->cpb.sha512.message_bit_length_lo < count0)
151  csbcpb->cpb.sha512.message_bit_length_hi++;
152 
153  in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, sctx->count[0],
154  nx_ctx->ap->sglen);
155  out_sg = nx_build_sg_list(nx_ctx->out_sg, out, SHA512_DIGEST_SIZE,
156  nx_ctx->ap->sglen);
157  nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg);
158  nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg);
159 
160  if (!nx_ctx->op.outlen) {
161  rc = -EINVAL;
162  goto out;
163  }
164 
165  rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
167  if (rc)
168  goto out;
169 
170  atomic_inc(&(nx_ctx->stats->sha512_ops));
171  atomic64_add(csbcpb->cpb.sha512.message_bit_length_lo,
172  &(nx_ctx->stats->sha512_bytes));
173 
174  memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE);
175 out:
176  return rc;
177 }
178 
179 static int nx_sha512_export(struct shash_desc *desc, void *out)
180 {
181  struct sha512_state *sctx = shash_desc_ctx(desc);
182  struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
183  struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
184  struct sha512_state *octx = out;
185 
186  /* move message_bit_length (128 bits) into count and convert its value
187  * to bytes */
188  octx->count[0] = csbcpb->cpb.sha512.message_bit_length_lo >> 3 |
189  ((csbcpb->cpb.sha512.message_bit_length_hi & 7) << 61);
190  octx->count[1] = csbcpb->cpb.sha512.message_bit_length_hi >> 3;
191 
192  octx->count[0] += sctx->count[0];
193  if (octx->count[0] < sctx->count[0])
194  octx->count[1]++;
195 
196  memcpy(octx->buf, sctx->buf, sizeof(octx->buf));
197 
198  /* if no data has been processed yet, we need to export SHA512's
199  * initial data, in case this context gets imported into a software
200  * context */
201  if (csbcpb->cpb.sha512.message_bit_length_hi ||
202  csbcpb->cpb.sha512.message_bit_length_lo)
203  memcpy(octx->state, csbcpb->cpb.sha512.message_digest,
205  else {
206  octx->state[0] = SHA512_H0;
207  octx->state[1] = SHA512_H1;
208  octx->state[2] = SHA512_H2;
209  octx->state[3] = SHA512_H3;
210  octx->state[4] = SHA512_H4;
211  octx->state[5] = SHA512_H5;
212  octx->state[6] = SHA512_H6;
213  octx->state[7] = SHA512_H7;
214  }
215 
216  return 0;
217 }
218 
219 static int nx_sha512_import(struct shash_desc *desc, const void *in)
220 {
221  struct sha512_state *sctx = shash_desc_ctx(desc);
222  struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base);
223  struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb;
224  const struct sha512_state *ictx = in;
225 
226  memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf));
227  sctx->count[0] = ictx->count[0] & 0x3f;
228  csbcpb->cpb.sha512.message_bit_length_lo = (ictx->count[0] & ~0x3f)
229  << 3;
230  csbcpb->cpb.sha512.message_bit_length_hi = ictx->count[1] << 3 |
231  ictx->count[0] >> 61;
232 
233  if (csbcpb->cpb.sha512.message_bit_length_hi ||
234  csbcpb->cpb.sha512.message_bit_length_lo) {
235  memcpy(csbcpb->cpb.sha512.message_digest, ictx->state,
237 
238  NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
239  NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE;
240  }
241 
242  return 0;
243 }
244 
246  .digestsize = SHA512_DIGEST_SIZE,
247  .init = nx_sha512_init,
248  .update = nx_sha512_update,
249  .final = nx_sha512_final,
250  .export = nx_sha512_export,
251  .import = nx_sha512_import,
252  .descsize = sizeof(struct sha512_state),
253  .statesize = sizeof(struct sha512_state),
254  .base = {
255  .cra_name = "sha512",
256  .cra_driver_name = "sha512-nx",
257  .cra_priority = 300,
258  .cra_flags = CRYPTO_ALG_TYPE_SHASH,
259  .cra_blocksize = SHA512_BLOCK_SIZE,
260  .cra_module = THIS_MODULE,
261  .cra_ctxsize = sizeof(struct nx_crypto_ctx),
262  .cra_init = nx_crypto_ctx_sha_init,
263  .cra_exit = nx_crypto_ctx_exit,
264  }
265 };