Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 1 | /** |
| 2 | * SHA-256 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> |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 26 | #include <asm/byteorder.h> |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 27 | |
| 28 | #include "nx_csbcpb.h" |
| 29 | #include "nx.h" |
| 30 | |
| 31 | |
| 32 | static int nx_sha256_init(struct shash_desc *desc) |
| 33 | { |
| 34 | struct sha256_state *sctx = shash_desc_ctx(desc); |
| 35 | 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] | 36 | int len; |
| 37 | int rc; |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 38 | |
| 39 | nx_ctx_init(nx_ctx, HCOP_FC_SHA); |
| 40 | |
| 41 | memset(sctx, 0, sizeof *sctx); |
| 42 | |
| 43 | nx_ctx->ap = &nx_ctx->props[NX_PROPS_SHA256]; |
| 44 | |
| 45 | NX_CPB_SET_DIGEST_SIZE(nx_ctx->csbcpb, NX_DS_SHA256); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 46 | |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 47 | len = SHA256_DIGEST_SIZE; |
| 48 | rc = nx_sha_build_sg_list(nx_ctx, nx_ctx->out_sg, |
| 49 | &nx_ctx->op.outlen, |
| 50 | &len, |
| 51 | (u8 *) sctx->state, |
| 52 | NX_DS_SHA256); |
| 53 | |
| 54 | if (rc) |
| 55 | goto out; |
| 56 | |
| 57 | sctx->state[0] = __cpu_to_be32(SHA256_H0); |
| 58 | sctx->state[1] = __cpu_to_be32(SHA256_H1); |
| 59 | sctx->state[2] = __cpu_to_be32(SHA256_H2); |
| 60 | sctx->state[3] = __cpu_to_be32(SHA256_H3); |
| 61 | sctx->state[4] = __cpu_to_be32(SHA256_H4); |
| 62 | sctx->state[5] = __cpu_to_be32(SHA256_H5); |
| 63 | sctx->state[6] = __cpu_to_be32(SHA256_H6); |
| 64 | sctx->state[7] = __cpu_to_be32(SHA256_H7); |
| 65 | sctx->count = 0; |
| 66 | |
| 67 | out: |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int nx_sha256_update(struct shash_desc *desc, const u8 *data, |
| 72 | unsigned int len) |
| 73 | { |
| 74 | struct sha256_state *sctx = shash_desc_ctx(desc); |
| 75 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); |
| 76 | struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb; |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 77 | u64 to_process = 0, leftover, total; |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 78 | unsigned long irq_flags; |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 79 | int rc = 0; |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 80 | int data_len; |
| 81 | u64 buf_len = (sctx->count % SHA256_BLOCK_SIZE); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 82 | |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 83 | spin_lock_irqsave(&nx_ctx->lock, irq_flags); |
| 84 | |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 85 | /* 2 cases for total data len: |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame] | 86 | * 1: < SHA256_BLOCK_SIZE: copy into state, return 0 |
| 87 | * 2: >= SHA256_BLOCK_SIZE: process X blocks, copy in leftover |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 88 | */ |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 89 | total = (sctx->count % SHA256_BLOCK_SIZE) + len; |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame] | 90 | if (total < SHA256_BLOCK_SIZE) { |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 91 | memcpy(sctx->buf + buf_len, data, len); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 92 | sctx->count += len; |
| 93 | goto out; |
| 94 | } |
| 95 | |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 96 | memcpy(csbcpb->cpb.sha256.message_digest, sctx->state, SHA256_DIGEST_SIZE); |
| 97 | NX_CPB_FDM(csbcpb) |= NX_FDM_INTERMEDIATE; |
| 98 | NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 99 | |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame] | 100 | do { |
| 101 | /* |
| 102 | * to_process: the SHA256_BLOCK_SIZE data chunk to process in |
| 103 | * this update. This value is also restricted by the sg list |
| 104 | * limits. |
| 105 | */ |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 106 | to_process = total - to_process; |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame] | 107 | to_process = to_process & ~(SHA256_BLOCK_SIZE - 1); |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 108 | |
| 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_SHA256); |
| 116 | |
| 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_SHA256); |
| 127 | |
| 128 | if (rc) |
| 129 | goto out; |
| 130 | |
| 131 | to_process = (data_len + buf_len); |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame] | 132 | leftover = total - to_process; |
| 133 | |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 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.sha256.input_partial_digest, |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame] | 139 | csbcpb->cpb.sha256.message_digest, |
| 140 | SHA256_DIGEST_SIZE); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +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 | 528e396 | 2012-05-14 11:06:20 +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 | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 151 | |
Marcelo Cerri | d311149 | 2013-08-02 12:09:52 +0000 | [diff] [blame] | 152 | atomic_inc(&(nx_ctx->stats->sha256_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 >= SHA256_BLOCK_SIZE); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +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 | |
| 164 | sctx->count += len; |
| 165 | memcpy(sctx->state, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 166 | out: |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 167 | spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 168 | return rc; |
| 169 | } |
| 170 | |
| 171 | static int nx_sha256_final(struct shash_desc *desc, u8 *out) |
| 172 | { |
| 173 | struct sha256_state *sctx = shash_desc_ctx(desc); |
| 174 | struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&desc->tfm->base); |
| 175 | struct nx_csbcpb *csbcpb = (struct nx_csbcpb *)nx_ctx->csbcpb; |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 176 | unsigned long irq_flags; |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 177 | int rc; |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 178 | int len; |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +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 | 528e396 | 2012-05-14 11:06:20 +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 >= SHA256_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.sha256.input_partial_digest, sctx->state, SHA256_DIGEST_SIZE); |
| 188 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; |
| 189 | NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION; |
| 190 | } else { |
| 191 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_INTERMEDIATE; |
| 192 | NX_CPB_FDM(csbcpb) &= ~NX_FDM_CONTINUATION; |
| 193 | } |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 194 | |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 195 | csbcpb->cpb.sha256.message_bit_length = (u64) (sctx->count * 8); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 196 | |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 197 | len = sctx->count & (SHA256_BLOCK_SIZE - 1); |
| 198 | rc = nx_sha_build_sg_list(nx_ctx, nx_ctx->in_sg, |
| 199 | &nx_ctx->op.inlen, |
| 200 | &len, |
| 201 | (u8 *) sctx->buf, |
| 202 | NX_DS_SHA256); |
| 203 | |
| 204 | if (rc || len != (sctx->count & (SHA256_BLOCK_SIZE - 1))) |
| 205 | goto out; |
| 206 | |
| 207 | len = SHA256_DIGEST_SIZE; |
| 208 | rc = nx_sha_build_sg_list(nx_ctx, nx_ctx->out_sg, |
| 209 | &nx_ctx->op.outlen, |
| 210 | &len, |
| 211 | out, |
| 212 | NX_DS_SHA256); |
| 213 | |
| 214 | if (rc || len != SHA256_DIGEST_SIZE) |
| 215 | goto out; |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 216 | |
| 217 | if (!nx_ctx->op.outlen) { |
| 218 | rc = -EINVAL; |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | rc = nx_hcall_sync(nx_ctx, &nx_ctx->op, |
| 223 | desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP); |
| 224 | if (rc) |
| 225 | goto out; |
| 226 | |
| 227 | atomic_inc(&(nx_ctx->stats->sha256_ops)); |
| 228 | |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 229 | atomic64_add(sctx->count, &(nx_ctx->stats->sha256_bytes)); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 230 | memcpy(out, csbcpb->cpb.sha256.message_digest, SHA256_DIGEST_SIZE); |
| 231 | out: |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 232 | spin_unlock_irqrestore(&nx_ctx->lock, irq_flags); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 233 | return rc; |
| 234 | } |
| 235 | |
| 236 | static int nx_sha256_export(struct shash_desc *desc, void *out) |
| 237 | { |
| 238 | struct sha256_state *sctx = shash_desc_ctx(desc); |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 239 | |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 240 | memcpy(out, sctx, sizeof(*sctx)); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 241 | |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int nx_sha256_import(struct shash_desc *desc, const void *in) |
| 246 | { |
| 247 | struct sha256_state *sctx = shash_desc_ctx(desc); |
Marcelo Cerri | c849163 | 2013-08-12 18:49:37 -0300 | [diff] [blame] | 248 | |
Leonidas S. Barbosa | 0008511 | 2014-10-28 15:49:46 -0200 | [diff] [blame] | 249 | memcpy(sctx, in, sizeof(*sctx)); |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 250 | |
Kent Yoder | 528e396 | 2012-05-14 11:06:20 +0000 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | struct shash_alg nx_shash_sha256_alg = { |
| 255 | .digestsize = SHA256_DIGEST_SIZE, |
| 256 | .init = nx_sha256_init, |
| 257 | .update = nx_sha256_update, |
| 258 | .final = nx_sha256_final, |
| 259 | .export = nx_sha256_export, |
| 260 | .import = nx_sha256_import, |
| 261 | .descsize = sizeof(struct sha256_state), |
| 262 | .statesize = sizeof(struct sha256_state), |
| 263 | .base = { |
| 264 | .cra_name = "sha256", |
| 265 | .cra_driver_name = "sha256-nx", |
| 266 | .cra_priority = 300, |
| 267 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
| 268 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 269 | .cra_module = THIS_MODULE, |
| 270 | .cra_ctxsize = sizeof(struct nx_crypto_ctx), |
| 271 | .cra_init = nx_crypto_ctx_sha_init, |
| 272 | .cra_exit = nx_crypto_ctx_exit, |
| 273 | } |
| 274 | }; |