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_crypto_ctx_sha512_init(struct crypto_tfm *tfm) 32 { 33 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm); 34 int err; 35 36 err = nx_crypto_ctx_sha_init(tfm); 37 if (err) 38 return err; 39 40 nx_ctx_init(nx_ctx, HCOP_FC_SHA); 41 42 nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA512]; 43 44 NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA512); 45 46 return 0; 47 } 48 49 static int nx_sha512_init(struct shash_desc *desc) 50 { 51 struct sha512_state *sctx = shash_desc_ctx(desc); 52 53 memset(sctx, 0, sizeof *sctx); 54 55 sctx->state[0] = __cpu_to_be64(SHA512_H0); 56 sctx->state[1] = __cpu_to_be64(SHA512_H1); 57 sctx->state[2] = __cpu_to_be64(SHA512_H2); 58 sctx->state[3] = __cpu_to_be64(SHA512_H3); 59 sctx->state[4] = __cpu_to_be64(SHA512_H4); 60 sctx->state[5] = __cpu_to_be64(SHA512_H5); 61 sctx->state[6] = __cpu_to_be64(SHA512_H6); 62 sctx->state[7] = __cpu_to_be64(SHA512_H7); 63 sctx->count[0] = 0; 64 65 return 0; 66 } 67 68 static int nx_sha512_update(struct shash_desc *desc, const u8 *data, 69 unsigned int len) 70 { 71 struct sha512_state *sctx = shash_desc_ctx(desc); 72 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); 73 struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb; 74 struct nx_sg *out_sg; 75 u64 to_process, leftover = 0, total; 76 unsigned long irq_flags; 77 int rc = 0; 78 int data_len; 79 u32 max_sg_len; 80 u64 buf_len = (sctx->count[0] % SHA512_BLOCK_SIZE); 81 82 spin_lock_irqsave(&nx_ctx->lock, irq_flags); 83 84 /* 2 cases for total data len: 85 * 1: < SHA512_BLOCK_SIZE: copy into state, return 0 86 * 2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover 87 */ 88 total = (sctx->count[0] % SHA512_BLOCK_SIZE) + len; 89 if (total < SHA512_BLOCK_SIZE) { 90 memcpy(sctx->buf + buf_len, data, len); 91 sctx->count[0] += len; 92 goto out; 93 } 94 95 memcpy(csbcpb->cpb.sha512.message_digest, sctx->state, SHA512_DIGEST_SIZE); 96 NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE; 97 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; 98 99 max_sg_len = min_t(u64, nx_ctx->ap->sglen, 100 nx_driver.of.max_sg_len/sizeof(struct nx_sg)); 101 max_sg_len = min_t(u64, max_sg_len, 102 nx_ctx->ap->databytelen/NX_PAGE_SIZE); 103 104 data_len = SHA512_DIGEST_SIZE; 105 out_sg = nx_build_sg_list(nx_ctx->out_sg, (u8 *)sctx->state, 106 &data_len, max_sg_len); 107 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg); 108 109 if (data_len != SHA512_DIGEST_SIZE) { 110 rc = -EINVAL; 111 goto out; 112 } 113 114 do { 115 int used_sgs = 0; 116 struct nx_sg *in_sg = nx_ctx->in_sg; 117 118 if (buf_len) { 119 data_len = buf_len; 120 in_sg = nx_build_sg_list(in_sg, 121 (u8 *) sctx->buf, 122 &data_len, max_sg_len); 123 124 if (data_len != buf_len) { 125 rc = -EINVAL; 126 goto out; 127 } 128 used_sgs = in_sg - nx_ctx->in_sg; 129 } 130 131 /* to_process: SHA512_BLOCK_SIZE aligned chunk to be 132 * processed in this iteration. This value is restricted 133 * by sg list limits and number of sgs we already used 134 * for leftover data. (see above) 135 * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len, 136 * but because data may not be aligned, we need to account 137 * for that too. */ 138 to_process = min_t(u64, total, 139 (max_sg_len - 1 - used_sgs) * NX_PAGE_SIZE); 140 to_process = to_process & ~(SHA512_BLOCK_SIZE - 1); 141 142 data_len = to_process - buf_len; 143 in_sg = nx_build_sg_list(in_sg, (u8 *) data, 144 &data_len, max_sg_len); 145 146 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg); 147 148 if (data_len != (to_process - buf_len)) { 149 rc = -EINVAL; 150 goto out; 151 } 152 153 to_process = data_len + buf_len; 154 leftover = total - to_process; 155 156 /* 157 * we've hit the nx chip previously and we're updating 158 * again, so copy over the partial digest. 159 */ 160 memcpy(csbcpb->cpb.sha512.input_partial_digest, 161 csbcpb->cpb.sha512.message_digest, 162 SHA512_DIGEST_SIZE); 163 164 if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) { 165 rc = -EINVAL; 166 goto out; 167 } 168 169 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0); 170 if (rc) 171 goto out; 172 173 atomic_inc(&(nx_ctx->stats->sha512_ops)); 174 175 total -= to_process; 176 data += to_process - buf_len; 177 buf_len = 0; 178 179 } while (leftover >= SHA512_BLOCK_SIZE); 180 181 /* copy the leftover back into the state struct */ 182 if (leftover) 183 memcpy(sctx->buf, data, leftover); 184 sctx->count[0] += len; 185 memcpy(sctx->state, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE); 186 out: 187 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); 188 return rc; 189 } 190 191 static int nx_sha512_final(struct shash_desc *desc, u8 *out) 192 { 193 struct sha512_state *sctx = shash_desc_ctx(desc); 194 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); 195 struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb; 196 struct nx_sg *in_sg, *out_sg; 197 u32 max_sg_len; 198 u64 count0; 199 unsigned long irq_flags; 200 int rc = 0; 201 int len; 202 203 spin_lock_irqsave(&nx_ctx->lock, irq_flags); 204 205 max_sg_len = min_t(u64, nx_ctx->ap->sglen, 206 nx_driver.of.max_sg_len/sizeof(struct nx_sg)); 207 max_sg_len = min_t(u64, max_sg_len, 208 nx_ctx->ap->databytelen/NX_PAGE_SIZE); 209 210 /* final is represented by continuing the operation and indicating that 211 * this is not an intermediate operation */ 212 if (sctx->count[0] >= SHA512_BLOCK_SIZE) { 213 /* we've hit the nx chip previously, now we're finalizing, 214 * so copy over the partial digest */ 215 memcpy(csbcpb->cpb.sha512.input_partial_digest, sctx->state, 216 SHA512_DIGEST_SIZE); 217 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; 218 NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; 219 } else { 220 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; 221 NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION; 222 } 223 224 NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; 225 226 count0 = sctx->count[0] * 8; 227 228 csbcpb->cpb.sha512.message_bit_length_lo = count0; 229 230 len = sctx->count[0] & (SHA512_BLOCK_SIZE - 1); 231 in_sg = nx_build_sg_list(nx_ctx->in_sg, sctx->buf, &len, 232 max_sg_len); 233 234 if (len != (sctx->count[0] & (SHA512_BLOCK_SIZE - 1))) { 235 rc = -EINVAL; 236 goto out; 237 } 238 239 len = SHA512_DIGEST_SIZE; 240 out_sg = nx_build_sg_list(nx_ctx->out_sg, out, &len, 241 max_sg_len); 242 243 nx_ctx->op.inlen = (nx_ctx->in_sg - in_sg) * sizeof(struct nx_sg); 244 nx_ctx->op.outlen = (nx_ctx->out_sg - out_sg) * sizeof(struct nx_sg); 245 246 if (!nx_ctx->op.outlen) { 247 rc = -EINVAL; 248 goto out; 249 } 250 251 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, 0); 252 if (rc) 253 goto out; 254 255 atomic_inc(&(nx_ctx->stats->sha512_ops)); 256 atomic64_add(sctx->count[0], &(nx_ctx->stats->sha512_bytes)); 257 258 memcpy(out, csbcpb->cpb.sha512.message_digest, SHA512_DIGEST_SIZE); 259 out: 260 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); 261 return rc; 262 } 263 264 static int nx_sha512_export(struct shash_desc *desc, void *out) 265 { 266 struct sha512_state *sctx = shash_desc_ctx(desc); 267 268 memcpy(out, sctx, sizeof(*sctx)); 269 270 return 0; 271 } 272 273 static int nx_sha512_import(struct shash_desc *desc, const void *in) 274 { 275 struct sha512_state *sctx = shash_desc_ctx(desc); 276 277 memcpy(sctx, in, sizeof(*sctx)); 278 279 return 0; 280 } 281 282 struct shash_alg nx_shash_sha512_alg = { 283 .digestsize = SHA512_DIGEST_SIZE, 284 .init = nx_sha512_init, 285 .update = nx_sha512_update, 286 .final = nx_sha512_final, 287 .export = nx_sha512_export, 288 .import = nx_sha512_import, 289 .descsize = sizeof(struct sha512_state), 290 .statesize = sizeof(struct sha512_state), 291 .base = { 292 .cra_name = "sha512", 293 .cra_driver_name = "sha512-nx", 294 .cra_priority = 300, 295 .cra_blocksize = SHA512_BLOCK_SIZE, 296 .cra_module = THIS_MODULE, 297 .cra_ctxsize = sizeof(struct nx_crypto_ctx), 298 .cra_init = nx_crypto_ctx_sha512_init, 299 .cra_exit = nx_crypto_ctx_exit, 300 } 301 }; 302