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