Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 1 | /** |
| 2 | * SHA-512 routines supporting the Power 7+ Nest Accelerators driver |
| 3 | * |
| 4 | * Copyright (C) 2011-2012 International Business Machines Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 only. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | * |
| 19 | * Author: Kent Yoder <yoder1@us.ibm.com> |
| 20 | */ |
| 21 | |
| 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 | |
| 43 | NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512); |
| 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; |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 58 | u64 to_process, leftover, total, spbc_bits; |
| 59 | u32 max_sg_len; |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 60 | int rc = 0; |
| 61 | |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 62 | /* 2 cases for total data len: |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 63 | * 1: < SHA512_BLOCK_SIZE: copy into state, return 0 |
| 64 | * 2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 65 | */ |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 66 | total = sctx->count[0] + len; |
| 67 | if (total < SHA512_BLOCK_SIZE) { |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 68 | memcpy(sctx->buf + sctx->count[0], data, len); |
| 69 | sctx->count[0] += len; |
| 70 | goto out; |
| 71 | } |
| 72 | |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 73 | in_sg = nx_ctx->in_sg; |
| 74 | max_sg_len = min_t(u32, nx_driver.of.max_sg_len/sizeof(struct nx_sg), |
| 75 | nx_ctx->ap->sglen); |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 76 | |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 77 | do { |
| 78 | /* |
| 79 | * to_process: the SHA512_BLOCK_SIZE data chunk to process in |
| 80 | * this update. This value is also restricted by the sg list |
| 81 | * limits. |
| 82 | */ |
| 83 | to_process = min_t(u64, total, nx_ctx->ap->databytelen); |
| 84 | to_process = min_t(u64, to_process, |
| 85 | NX_PAGE_SIZE * (max_sg_len - 1)); |
| 86 | to_process = to_process & ~(SHA512_BLOCK_SIZE - 1); |
| 87 | leftover = total - to_process; |
| 88 | |
| 89 | if (sctx->count[0]) { |
| 90 | in_sg = nx_build_sg_list(nx_ctx->in_sg, |
| 91 | (u8 *) sctx->buf, |
| 92 | sctx->count[0], max_sg_len); |
| 93 | } |
| 94 | in_sg = nx_build_sg_list(in_sg, (u8 *) data, |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 95 | to_process - sctx->count[0], |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 96 | max_sg_len); |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 97 | nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * |
| 98 | sizeof(struct nx_sg); |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 99 | |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 100 | if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) { |
| 101 | /* |
| 102 | * we've hit the nx chip previously and we're updating |
| 103 | * again, so copy over the partial digest. |
| 104 | */ |
| 105 | memcpy(csbcpb->cpb.sha512.input_partial_digest, |
| 106 | csbcpb->cpb.sha512.message_digest, |
| 107 | SHA512_DIGEST_SIZE); |
| 108 | } |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 109 | |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 110 | NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE; |
| 111 | if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) { |
| 112 | rc = -EINVAL; |
| 113 | goto out; |
| 114 | } |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 115 | |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 116 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 117 | desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 118 | if (rc) |
| 119 | goto out; |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 120 | |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 121 | atomic_inc(&(nx_ctx->stats->sha512_ops)); |
| 122 | spbc_bits = csbcpb->cpb.sha512.spbc * 8; |
| 123 | csbcpb->cpb.sha512.message_bit_length_lo += spbc_bits; |
| 124 | if (csbcpb->cpb.sha512.message_bit_length_lo < spbc_bits) |
| 125 | csbcpb->cpb.sha512.message_bit_length_hi++; |
| 126 | |
| 127 | /* everything after the first update is continuation */ |
| 128 | NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; |
| 129 | |
| 130 | total -= to_process; |
| 131 | data += to_process; |
| 132 | sctx->count[0] = 0; |
| 133 | in_sg = nx_ctx->in_sg; |
| 134 | } while (leftover >= SHA512_BLOCK_SIZE); |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 135 | |
| 136 | /* copy the leftover back into the state struct */ |
Kent Yoder | 1ad936e | 2013-04-12 17:13:59 +0000 | [diff] [blame] | 137 | if (leftover) |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 138 | memcpy(sctx->buf, data, leftover); |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 139 | sctx->count[0] = leftover; |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 140 | out: |
| 141 | return rc; |
| 142 | } |
| 143 | |
| 144 | static int nx_sha512_final(struct shash_desc *desc, u8 *out) |
| 145 | { |
| 146 | struct sha512_state *sctx = shash_desc_ctx(desc); |
| 147 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); |
| 148 | struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb; |
| 149 | struct nx_sg *in_sg, *out_sg; |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 150 | u32 max_sg_len; |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 151 | u64 count0; |
| 152 | int rc; |
| 153 | |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 154 | max_sg_len = min_t(u32, nx_driver.of.max_sg_len, nx_ctx->ap->sglen); |
| 155 | |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 156 | if (NX_CPB_FDM(csbcpb) & NX_FDM_CONTINUATION) { |
| 157 | /* we've hit the nx chip previously, now we're finalizing, |
| 158 | * so copy over the partial digest */ |
| 159 | memcpy(csbcpb->cpb.sha512.input_partial_digest, |
| 160 | csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE); |
| 161 | } |
| 162 | |
| 163 | /* final is represented by continuing the operation and indicating that |
| 164 | * this is not an intermediate operation */ |
| 165 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; |
| 166 | |
| 167 | count0 = sctx->count[0] * 8; |
| 168 | |
| 169 | csbcpb->cpb.sha512.message_bit_length_lo += count0; |
| 170 | if (csbcpb->cpb.sha512.message_bit_length_lo < count0) |
| 171 | csbcpb->cpb.sha512.message_bit_length_hi++; |
| 172 | |
| 173 | in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, sctx->count[0], |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 174 | max_sg_len); |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 175 | out_sg = nx_build_sg_list(nx_ctx->out_sg, out, SHA512_DIGEST_SIZE, |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame^] | 176 | max_sg_len); |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 177 | nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg); |
| 178 | nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg); |
| 179 | |
| 180 | if (!nx_ctx->op.outlen) { |
| 181 | rc = -EINVAL; |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 186 | desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 187 | if (rc) |
| 188 | goto out; |
| 189 | |
| 190 | atomic_inc(&(nx_ctx->stats->sha512_ops)); |
Kent Yoder | 1ad936e | 2013-04-12 17:13:59 +0000 | [diff] [blame] | 191 | atomic64_add(csbcpb->cpb.sha512.message_bit_length_lo / 8, |
Kent Yoder | fc482a8 | 2012-05-14 11:06:31 +0000 | [diff] [blame] | 192 | &(nx_ctx->stats->sha512_bytes)); |
| 193 | |
| 194 | memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE); |
| 195 | out: |
| 196 | return rc; |
| 197 | } |
| 198 | |
| 199 | static int nx_sha512_export(struct shash_desc *desc, void *out) |
| 200 | { |
| 201 | struct sha512_state *sctx = shash_desc_ctx(desc); |
| 202 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); |
| 203 | struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb; |
| 204 | struct sha512_state *octx = out; |
| 205 | |
| 206 | /* move message_bit_length (128 bits) into count and convert its value |
| 207 | * to bytes */ |
| 208 | octx->count[0] = csbcpb->cpb.sha512.message_bit_length_lo >> 3 | |
| 209 | ((csbcpb->cpb.sha512.message_bit_length_hi & 7) << 61); |
| 210 | octx->count[1] = csbcpb->cpb.sha512.message_bit_length_hi >> 3; |
| 211 | |
| 212 | octx->count[0] += sctx->count[0]; |
| 213 | if (octx->count[0] < sctx->count[0]) |
| 214 | octx->count[1]++; |
| 215 | |
| 216 | memcpy(octx->buf, sctx->buf, sizeof(octx->buf)); |
| 217 | |
| 218 | /* if no data has been processed yet, we need to export SHA512's |
| 219 | * initial data, in case this context gets imported into a software |
| 220 | * context */ |
| 221 | if (csbcpb->cpb.sha512.message_bit_length_hi || |
| 222 | csbcpb->cpb.sha512.message_bit_length_lo) |
| 223 | memcpy(octx->state, csbcpb->cpb.sha512.message_digest, |
| 224 | SHA512_DIGEST_SIZE); |
| 225 | else { |
| 226 | octx->state[0] = SHA512_H0; |
| 227 | octx->state[1] = SHA512_H1; |
| 228 | octx->state[2] = SHA512_H2; |
| 229 | octx->state[3] = SHA512_H3; |
| 230 | octx->state[4] = SHA512_H4; |
| 231 | octx->state[5] = SHA512_H5; |
| 232 | octx->state[6] = SHA512_H6; |
| 233 | octx->state[7] = SHA512_H7; |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int nx_sha512_import(struct shash_desc *desc, const void *in) |
| 240 | { |
| 241 | struct sha512_state *sctx = shash_desc_ctx(desc); |
| 242 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); |
| 243 | struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb; |
| 244 | const struct sha512_state *ictx = in; |
| 245 | |
| 246 | memcpy(sctx->buf, ictx->buf, sizeof(ictx->buf)); |
| 247 | sctx->count[0] = ictx->count[0] & 0x3f; |
| 248 | csbcpb->cpb.sha512.message_bit_length_lo = (ictx->count[0] & ~0x3f) |
| 249 | << 3; |
| 250 | csbcpb->cpb.sha512.message_bit_length_hi = ictx->count[1] << 3 | |
| 251 | ictx->count[0] >> 61; |
| 252 | |
| 253 | if (csbcpb->cpb.sha512.message_bit_length_hi || |
| 254 | csbcpb->cpb.sha512.message_bit_length_lo) { |
| 255 | memcpy(csbcpb->cpb.sha512.message_digest, ictx->state, |
| 256 | SHA512_DIGEST_SIZE); |
| 257 | |
| 258 | NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; |
| 259 | NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE; |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | struct shash_alg nx_shash_sha512_alg = { |
| 266 | .digestsize = SHA512_DIGEST_SIZE, |
| 267 | .init = nx_sha512_init, |
| 268 | .update = nx_sha512_update, |
| 269 | .final = nx_sha512_final, |
| 270 | .export = nx_sha512_export, |
| 271 | .import = nx_sha512_import, |
| 272 | .descsize = sizeof(struct sha512_state), |
| 273 | .statesize = sizeof(struct sha512_state), |
| 274 | .base = { |
| 275 | .cra_name = "sha512", |
| 276 | .cra_driver_name = "sha512-nx", |
| 277 | .cra_priority = 300, |
| 278 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 279 | .cra_blocksize = SHA512_BLOCK_SIZE, |
| 280 | .cra_module = THIS_MODULE, |
| 281 | .cra_ctxsize = sizeof(struct nx_crypto_ctx), |
| 282 | .cra_init = nx_crypto_ctx_sha_init, |
| 283 | .cra_exit = nx_crypto_ctx_exit, |
| 284 | } |
| 285 | }; |