blob: 22875ec2b2c8f3fb2859a4bb5b9e872a32593114 [file] [log] [blame]
James Hsiao049359d2009-02-05 16:18:13 +11001/**
2 * AMCC SoC PPC4xx Crypto Driver
3 *
4 * Copyright (c) 2008 Applied Micro Circuits Corporation.
5 * All rights reserved. James Hsiao <jhsiao@amcc.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * This file implements the Linux crypto algorithms.
18 */
19
20#include <linux/kernel.h>
21#include <linux/interrupt.h>
22#include <linux/spinlock_types.h>
23#include <linux/scatterlist.h>
24#include <linux/crypto.h>
25#include <linux/hash.h>
26#include <crypto/internal/hash.h>
27#include <linux/dma-mapping.h>
28#include <crypto/algapi.h>
29#include <crypto/aes.h>
30#include <crypto/sha.h>
Christian Lamparterf2a13e72017-08-25 15:47:21 +020031#include <crypto/ctr.h>
James Hsiao049359d2009-02-05 16:18:13 +110032#include "crypto4xx_reg_def.h"
James Hsiao049359d2009-02-05 16:18:13 +110033#include "crypto4xx_core.h"
Christian Lamparter249c8d92017-08-25 15:47:20 +020034#include "crypto4xx_sa.h"
James Hsiao049359d2009-02-05 16:18:13 +110035
Jingoo Han3a4eac72013-08-09 16:59:57 +090036static void set_dynamic_sa_command_0(struct dynamic_sa_ctl *sa, u32 save_h,
37 u32 save_iv, u32 ld_h, u32 ld_iv,
38 u32 hdr_proc, u32 h, u32 c, u32 pad_type,
39 u32 op_grp, u32 op, u32 dir)
James Hsiao049359d2009-02-05 16:18:13 +110040{
41 sa->sa_command_0.w = 0;
42 sa->sa_command_0.bf.save_hash_state = save_h;
43 sa->sa_command_0.bf.save_iv = save_iv;
44 sa->sa_command_0.bf.load_hash_state = ld_h;
45 sa->sa_command_0.bf.load_iv = ld_iv;
46 sa->sa_command_0.bf.hdr_proc = hdr_proc;
47 sa->sa_command_0.bf.hash_alg = h;
48 sa->sa_command_0.bf.cipher_alg = c;
49 sa->sa_command_0.bf.pad_type = pad_type & 3;
50 sa->sa_command_0.bf.extend_pad = pad_type >> 2;
51 sa->sa_command_0.bf.op_group = op_grp;
52 sa->sa_command_0.bf.opcode = op;
53 sa->sa_command_0.bf.dir = dir;
54}
55
Jingoo Han3a4eac72013-08-09 16:59:57 +090056static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
57 u32 hmac_mc, u32 cfb, u32 esn,
58 u32 sn_mask, u32 mute, u32 cp_pad,
59 u32 cp_pay, u32 cp_hdr)
James Hsiao049359d2009-02-05 16:18:13 +110060{
61 sa->sa_command_1.w = 0;
62 sa->sa_command_1.bf.crypto_mode31 = (cm & 4) >> 2;
63 sa->sa_command_1.bf.crypto_mode9_8 = cm & 3;
64 sa->sa_command_1.bf.feedback_mode = cfb,
65 sa->sa_command_1.bf.sa_rev = 1;
Christian Lamparter5a4326d32017-10-04 01:00:05 +020066 sa->sa_command_1.bf.hmac_muting = hmac_mc;
James Hsiao049359d2009-02-05 16:18:13 +110067 sa->sa_command_1.bf.extended_seq_num = esn;
68 sa->sa_command_1.bf.seq_num_mask = sn_mask;
69 sa->sa_command_1.bf.mutable_bit_proc = mute;
70 sa->sa_command_1.bf.copy_pad = cp_pad;
71 sa->sa_command_1.bf.copy_payload = cp_pay;
72 sa->sa_command_1.bf.copy_hdr = cp_hdr;
73}
74
75int crypto4xx_encrypt(struct ablkcipher_request *req)
76{
77 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +020078 unsigned int ivlen = crypto_ablkcipher_ivsize(
79 crypto_ablkcipher_reqtfm(req));
80 __le32 iv[ivlen];
James Hsiao049359d2009-02-05 16:18:13 +110081
Christian Lampartercd4dcd62017-10-04 01:00:11 +020082 if (ivlen)
83 crypto4xx_memcpy_to_le32(iv, req->info, ivlen);
James Hsiao049359d2009-02-05 16:18:13 +110084
85 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lampartercd4dcd62017-10-04 01:00:11 +020086 req->nbytes, iv, ivlen, ctx->sa_out, ctx->sa_len);
James Hsiao049359d2009-02-05 16:18:13 +110087}
88
89int crypto4xx_decrypt(struct ablkcipher_request *req)
90{
91 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +020092 unsigned int ivlen = crypto_ablkcipher_ivsize(
93 crypto_ablkcipher_reqtfm(req));
94 __le32 iv[ivlen];
James Hsiao049359d2009-02-05 16:18:13 +110095
Christian Lampartercd4dcd62017-10-04 01:00:11 +020096 if (ivlen)
97 crypto4xx_memcpy_to_le32(iv, req->info, ivlen);
James Hsiao049359d2009-02-05 16:18:13 +110098
99 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200100 req->nbytes, iv, ivlen, ctx->sa_in, ctx->sa_len);
James Hsiao049359d2009-02-05 16:18:13 +1100101}
102
103/**
104 * AES Functions
105 */
106static int crypto4xx_setkey_aes(struct crypto_ablkcipher *cipher,
107 const u8 *key,
108 unsigned int keylen,
109 unsigned char cm,
110 u8 fb)
111{
112 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
113 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
114 struct dynamic_sa_ctl *sa;
115 int rc;
116
117 if (keylen != AES_KEYSIZE_256 &&
118 keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
119 crypto_ablkcipher_set_flags(cipher,
120 CRYPTO_TFM_RES_BAD_KEY_LEN);
121 return -EINVAL;
122 }
123
124 /* Create SA */
Christian Lamparter2f776902017-10-04 01:00:14 +0200125 if (ctx->sa_in || ctx->sa_out)
James Hsiao049359d2009-02-05 16:18:13 +1100126 crypto4xx_free_sa(ctx);
127
128 rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
129 if (rc)
130 return rc;
131
James Hsiao049359d2009-02-05 16:18:13 +1100132 /* Setup SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200133 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100134
135 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, SA_NOT_SAVE_IV,
136 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
137 SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
138 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
139 SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
140 DIR_INBOUND);
141
142 set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
143 fb, SA_EXTENDED_SN_OFF,
144 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
145 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
146 SA_NOT_COPY_HDR);
Christian Lamparter4865b1222017-10-04 01:00:10 +0200147 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
148 key, keylen);
Christian Lamparter453e3092017-08-25 15:47:19 +0200149 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
James Hsiao049359d2009-02-05 16:18:13 +1100150 sa->sa_command_1.bf.key_len = keylen >> 3;
James Hsiao049359d2009-02-05 16:18:13 +1100151
152 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200153 sa = ctx->sa_out;
James Hsiao049359d2009-02-05 16:18:13 +1100154 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
155
156 return 0;
157}
158
159int crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher *cipher,
160 const u8 *key, unsigned int keylen)
161{
162 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
163 CRYPTO_FEEDBACK_MODE_NO_FB);
164}
165
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200166int crypto4xx_setkey_aes_cfb(struct crypto_ablkcipher *cipher,
167 const u8 *key, unsigned int keylen)
168{
169 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
170 CRYPTO_FEEDBACK_MODE_128BIT_CFB);
171}
172
173int crypto4xx_setkey_aes_ecb(struct crypto_ablkcipher *cipher,
174 const u8 *key, unsigned int keylen)
175{
176 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
177 CRYPTO_FEEDBACK_MODE_NO_FB);
178}
179
180int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
181 const u8 *key, unsigned int keylen)
182{
183 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
184 CRYPTO_FEEDBACK_MODE_64BIT_OFB);
185}
186
187int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
188 const u8 *key, unsigned int keylen)
189{
190 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
191 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
192 int rc;
193
194 rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
195 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
196 if (rc)
197 return rc;
198
Christian Lamparter2f776902017-10-04 01:00:14 +0200199 ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
200 CTR_RFC3686_NONCE_SIZE]);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200201
202 return 0;
203}
204
205int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req)
206{
207 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200208 __le32 iv[AES_IV_SIZE / 4] = {
Christian Lamparter2f776902017-10-04 01:00:14 +0200209 ctx->iv_nonce,
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200210 cpu_to_le32p((u32 *) req->info),
211 cpu_to_le32p((u32 *) (req->info + 4)),
212 cpu_to_le32(1) };
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200213
214 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200215 req->nbytes, iv, AES_IV_SIZE,
216 ctx->sa_out, ctx->sa_len);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200217}
218
219int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req)
220{
221 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200222 __le32 iv[AES_IV_SIZE / 4] = {
Christian Lamparter2f776902017-10-04 01:00:14 +0200223 ctx->iv_nonce,
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200224 cpu_to_le32p((u32 *) req->info),
225 cpu_to_le32p((u32 *) (req->info + 4)),
226 cpu_to_le32(1) };
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200227
228 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200229 req->nbytes, iv, AES_IV_SIZE,
230 ctx->sa_out, ctx->sa_len);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200231}
232
James Hsiao049359d2009-02-05 16:18:13 +1100233/**
234 * HASH SHA1 Functions
235 */
236static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
237 unsigned int sa_len,
238 unsigned char ha,
239 unsigned char hm)
240{
241 struct crypto_alg *alg = tfm->__crt_alg;
242 struct crypto4xx_alg *my_alg = crypto_alg_to_crypto4xx_alg(alg);
243 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200244 struct dynamic_sa_hash160 *sa;
James Hsiao049359d2009-02-05 16:18:13 +1100245 int rc;
246
247 ctx->dev = my_alg->dev;
James Hsiao049359d2009-02-05 16:18:13 +1100248
249 /* Create SA */
Christian Lamparter2f776902017-10-04 01:00:14 +0200250 if (ctx->sa_in || ctx->sa_out)
James Hsiao049359d2009-02-05 16:18:13 +1100251 crypto4xx_free_sa(ctx);
252
253 rc = crypto4xx_alloc_sa(ctx, sa_len);
254 if (rc)
255 return rc;
256
Herbert Xu6b1679f2009-07-12 23:08:28 +0800257 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
258 sizeof(struct crypto4xx_ctx));
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200259 sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
260 set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
James Hsiao049359d2009-02-05 16:18:13 +1100261 SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
262 SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
263 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
264 SA_OPCODE_HASH, DIR_INBOUND);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200265 set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
James Hsiao049359d2009-02-05 16:18:13 +1100266 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
267 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
268 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
269 SA_NOT_COPY_HDR);
James Hsiao049359d2009-02-05 16:18:13 +1100270 /* Need to zero hash digest in SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200271 memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
272 memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
James Hsiao049359d2009-02-05 16:18:13 +1100273
274 return 0;
275}
276
277int crypto4xx_hash_init(struct ahash_request *req)
278{
279 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
280 int ds;
281 struct dynamic_sa_ctl *sa;
282
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200283 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100284 ds = crypto_ahash_digestsize(
285 __crypto_ahash_cast(req->base.tfm));
286 sa->sa_command_0.bf.digest_len = ds >> 2;
287 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
James Hsiao049359d2009-02-05 16:18:13 +1100288
289 return 0;
290}
291
292int crypto4xx_hash_update(struct ahash_request *req)
293{
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200294 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
James Hsiao049359d2009-02-05 16:18:13 +1100295 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200296 struct scatterlist dst;
297 unsigned int ds = crypto_ahash_digestsize(ahash);
James Hsiao049359d2009-02-05 16:18:13 +1100298
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200299 sg_init_one(&dst, req->result, ds);
James Hsiao049359d2009-02-05 16:18:13 +1100300
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200301 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
302 req->nbytes, NULL, 0, ctx->sa_in,
303 ctx->sa_len);
James Hsiao049359d2009-02-05 16:18:13 +1100304}
305
306int crypto4xx_hash_final(struct ahash_request *req)
307{
308 return 0;
309}
310
311int crypto4xx_hash_digest(struct ahash_request *req)
312{
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200313 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
James Hsiao049359d2009-02-05 16:18:13 +1100314 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200315 struct scatterlist dst;
316 unsigned int ds = crypto_ahash_digestsize(ahash);
James Hsiao049359d2009-02-05 16:18:13 +1100317
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200318 sg_init_one(&dst, req->result, ds);
James Hsiao049359d2009-02-05 16:18:13 +1100319
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200320 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
321 req->nbytes, NULL, 0, ctx->sa_in,
322 ctx->sa_len);
James Hsiao049359d2009-02-05 16:18:13 +1100323}
324
325/**
326 * SHA1 Algorithm
327 */
328int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
329{
330 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
331 SA_HASH_MODE_HASH);
332}
333
334