blob: 195445310f0c5b72a01af5581d0d1fd9303200ec [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 */
125 if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
126 crypto4xx_free_sa(ctx);
127
128 rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
129 if (rc)
130 return rc;
131
132 if (ctx->state_record_dma_addr == 0) {
133 rc = crypto4xx_alloc_state_record(ctx);
134 if (rc) {
135 crypto4xx_free_sa(ctx);
136 return rc;
137 }
138 }
139 /* Setup SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200140 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100141
142 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, SA_NOT_SAVE_IV,
143 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
144 SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
145 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
146 SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
147 DIR_INBOUND);
148
149 set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
150 fb, SA_EXTENDED_SN_OFF,
151 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
152 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
153 SA_NOT_COPY_HDR);
Christian Lamparter4865b1222017-10-04 01:00:10 +0200154 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
155 key, keylen);
Christian Lamparter453e3092017-08-25 15:47:19 +0200156 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
James Hsiao049359d2009-02-05 16:18:13 +1100157 sa->sa_command_1.bf.key_len = keylen >> 3;
James Hsiao049359d2009-02-05 16:18:13 +1100158
159 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200160 sa = ctx->sa_out;
James Hsiao049359d2009-02-05 16:18:13 +1100161 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
162
163 return 0;
164}
165
166int crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher *cipher,
167 const u8 *key, unsigned int keylen)
168{
169 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
170 CRYPTO_FEEDBACK_MODE_NO_FB);
171}
172
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200173int crypto4xx_setkey_aes_cfb(struct crypto_ablkcipher *cipher,
174 const u8 *key, unsigned int keylen)
175{
176 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
177 CRYPTO_FEEDBACK_MODE_128BIT_CFB);
178}
179
180int crypto4xx_setkey_aes_ecb(struct crypto_ablkcipher *cipher,
181 const u8 *key, unsigned int keylen)
182{
183 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
184 CRYPTO_FEEDBACK_MODE_NO_FB);
185}
186
187int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
188 const u8 *key, unsigned int keylen)
189{
190 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
191 CRYPTO_FEEDBACK_MODE_64BIT_OFB);
192}
193
194int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
195 const u8 *key, unsigned int keylen)
196{
197 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
198 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
199 int rc;
200
201 rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
202 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
203 if (rc)
204 return rc;
205
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200206 crypto4xx_memcpy_to_le32(ctx->state_record->save_iv,
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200207 key + keylen - CTR_RFC3686_NONCE_SIZE, CTR_RFC3686_NONCE_SIZE);
208
209 return 0;
210}
211
212int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req)
213{
214 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200215 __le32 iv[AES_IV_SIZE / 4] = {
216 ctx->state_record->save_iv[0],
217 cpu_to_le32p((u32 *) req->info),
218 cpu_to_le32p((u32 *) (req->info + 4)),
219 cpu_to_le32(1) };
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200220
221 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200222 req->nbytes, iv, AES_IV_SIZE,
223 ctx->sa_out, ctx->sa_len);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200224}
225
226int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req)
227{
228 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200229 __le32 iv[AES_IV_SIZE / 4] = {
230 ctx->state_record->save_iv[0],
231 cpu_to_le32p((u32 *) req->info),
232 cpu_to_le32p((u32 *) (req->info + 4)),
233 cpu_to_le32(1) };
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200234
235 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200236 req->nbytes, iv, AES_IV_SIZE,
237 ctx->sa_out, ctx->sa_len);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200238}
239
James Hsiao049359d2009-02-05 16:18:13 +1100240/**
241 * HASH SHA1 Functions
242 */
243static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
244 unsigned int sa_len,
245 unsigned char ha,
246 unsigned char hm)
247{
248 struct crypto_alg *alg = tfm->__crt_alg;
249 struct crypto4xx_alg *my_alg = crypto_alg_to_crypto4xx_alg(alg);
250 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200251 struct dynamic_sa_hash160 *sa;
James Hsiao049359d2009-02-05 16:18:13 +1100252 int rc;
253
254 ctx->dev = my_alg->dev;
James Hsiao049359d2009-02-05 16:18:13 +1100255
256 /* Create SA */
257 if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
258 crypto4xx_free_sa(ctx);
259
260 rc = crypto4xx_alloc_sa(ctx, sa_len);
261 if (rc)
262 return rc;
263
264 if (ctx->state_record_dma_addr == 0) {
265 crypto4xx_alloc_state_record(ctx);
266 if (!ctx->state_record_dma_addr) {
267 crypto4xx_free_sa(ctx);
268 return -ENOMEM;
269 }
270 }
271
Herbert Xu6b1679f2009-07-12 23:08:28 +0800272 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
273 sizeof(struct crypto4xx_ctx));
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200274 sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
275 set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
James Hsiao049359d2009-02-05 16:18:13 +1100276 SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
277 SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
278 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
279 SA_OPCODE_HASH, DIR_INBOUND);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200280 set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
James Hsiao049359d2009-02-05 16:18:13 +1100281 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
282 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
283 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
284 SA_NOT_COPY_HDR);
James Hsiao049359d2009-02-05 16:18:13 +1100285 /* Need to zero hash digest in SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200286 memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
287 memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
James Hsiao049359d2009-02-05 16:18:13 +1100288
289 return 0;
290}
291
292int crypto4xx_hash_init(struct ahash_request *req)
293{
294 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
295 int ds;
296 struct dynamic_sa_ctl *sa;
297
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200298 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100299 ds = crypto_ahash_digestsize(
300 __crypto_ahash_cast(req->base.tfm));
301 sa->sa_command_0.bf.digest_len = ds >> 2;
302 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
James Hsiao049359d2009-02-05 16:18:13 +1100303
304 return 0;
305}
306
307int crypto4xx_hash_update(struct ahash_request *req)
308{
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200309 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
James Hsiao049359d2009-02-05 16:18:13 +1100310 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200311 struct scatterlist dst;
312 unsigned int ds = crypto_ahash_digestsize(ahash);
James Hsiao049359d2009-02-05 16:18:13 +1100313
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200314 sg_init_one(&dst, req->result, ds);
James Hsiao049359d2009-02-05 16:18:13 +1100315
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200316 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
317 req->nbytes, NULL, 0, ctx->sa_in,
318 ctx->sa_len);
James Hsiao049359d2009-02-05 16:18:13 +1100319}
320
321int crypto4xx_hash_final(struct ahash_request *req)
322{
323 return 0;
324}
325
326int crypto4xx_hash_digest(struct ahash_request *req)
327{
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200328 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
James Hsiao049359d2009-02-05 16:18:13 +1100329 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200330 struct scatterlist dst;
331 unsigned int ds = crypto_ahash_digestsize(ahash);
James Hsiao049359d2009-02-05 16:18:13 +1100332
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200333 sg_init_one(&dst, req->result, ds);
James Hsiao049359d2009-02-05 16:18:13 +1100334
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200335 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
336 req->nbytes, NULL, 0, ctx->sa_in,
337 ctx->sa_len);
James Hsiao049359d2009-02-05 16:18:13 +1100338}
339
340/**
341 * SHA1 Algorithm
342 */
343int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
344{
345 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
346 SA_HASH_MODE_HASH);
347}
348
349