blob: c9597824a5150bef81a08e40fea536c7cc34b62e [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;
66 sa->sa_command_1.bf.extended_seq_num = esn;
67 sa->sa_command_1.bf.seq_num_mask = sn_mask;
68 sa->sa_command_1.bf.mutable_bit_proc = mute;
69 sa->sa_command_1.bf.copy_pad = cp_pad;
70 sa->sa_command_1.bf.copy_payload = cp_pay;
71 sa->sa_command_1.bf.copy_hdr = cp_hdr;
72}
73
74int crypto4xx_encrypt(struct ablkcipher_request *req)
75{
76 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
77
78 ctx->direction = DIR_OUTBOUND;
James Hsiao049359d2009-02-05 16:18:13 +110079 ctx->is_hash = 0;
80 ctx->pd_ctl = 0x1;
81
82 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparter249c8d92017-08-25 15:47:20 +020083 req->nbytes, req->info,
84 crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req)));
James Hsiao049359d2009-02-05 16:18:13 +110085}
86
87int crypto4xx_decrypt(struct ablkcipher_request *req)
88{
89 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
90
91 ctx->direction = DIR_INBOUND;
James Hsiao049359d2009-02-05 16:18:13 +110092 ctx->is_hash = 0;
93 ctx->pd_ctl = 1;
94
95 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparter249c8d92017-08-25 15:47:20 +020096 req->nbytes, req->info,
97 crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req)));
James Hsiao049359d2009-02-05 16:18:13 +110098}
99
100/**
101 * AES Functions
102 */
103static int crypto4xx_setkey_aes(struct crypto_ablkcipher *cipher,
104 const u8 *key,
105 unsigned int keylen,
106 unsigned char cm,
107 u8 fb)
108{
109 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
110 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
111 struct dynamic_sa_ctl *sa;
112 int rc;
113
114 if (keylen != AES_KEYSIZE_256 &&
115 keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
116 crypto_ablkcipher_set_flags(cipher,
117 CRYPTO_TFM_RES_BAD_KEY_LEN);
118 return -EINVAL;
119 }
120
121 /* Create SA */
122 if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
123 crypto4xx_free_sa(ctx);
124
125 rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
126 if (rc)
127 return rc;
128
129 if (ctx->state_record_dma_addr == 0) {
130 rc = crypto4xx_alloc_state_record(ctx);
131 if (rc) {
132 crypto4xx_free_sa(ctx);
133 return rc;
134 }
135 }
136 /* Setup SA */
137 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100138
139 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, SA_NOT_SAVE_IV,
140 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
141 SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
142 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
143 SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
144 DIR_INBOUND);
145
146 set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
147 fb, SA_EXTENDED_SN_OFF,
148 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
149 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
150 SA_NOT_COPY_HDR);
Christian Lamparter249c8d92017-08-25 15:47:20 +0200151 crypto4xx_memcpy_le(get_dynamic_sa_key_field(sa),
James Hsiao049359d2009-02-05 16:18:13 +1100152 key, keylen);
Christian Lamparter453e3092017-08-25 15:47:19 +0200153 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
James Hsiao049359d2009-02-05 16:18:13 +1100154 sa->sa_command_1.bf.key_len = keylen >> 3;
155 ctx->is_hash = 0;
156 ctx->direction = DIR_INBOUND;
Christian Lamparter249c8d92017-08-25 15:47:20 +0200157 memcpy(sa + get_dynamic_sa_offset_state_ptr_field(sa),
158 (void *)&ctx->state_record_dma_addr, 4);
159 ctx->offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(sa);
James Hsiao049359d2009-02-05 16:18:13 +1100160
161 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
162 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
163 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
164
165 return 0;
166}
167
168int crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher *cipher,
169 const u8 *key, unsigned int keylen)
170{
171 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
172 CRYPTO_FEEDBACK_MODE_NO_FB);
173}
174
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200175int crypto4xx_setkey_aes_cfb(struct crypto_ablkcipher *cipher,
176 const u8 *key, unsigned int keylen)
177{
178 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
179 CRYPTO_FEEDBACK_MODE_128BIT_CFB);
180}
181
182int crypto4xx_setkey_aes_ecb(struct crypto_ablkcipher *cipher,
183 const u8 *key, unsigned int keylen)
184{
185 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
186 CRYPTO_FEEDBACK_MODE_NO_FB);
187}
188
189int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
190 const u8 *key, unsigned int keylen)
191{
192 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
193 CRYPTO_FEEDBACK_MODE_64BIT_OFB);
194}
195
196int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
197 const u8 *key, unsigned int keylen)
198{
199 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
200 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
201 int rc;
202
203 rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
204 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
205 if (rc)
206 return rc;
207
208 memcpy(ctx->state_record,
209 key + keylen - CTR_RFC3686_NONCE_SIZE, CTR_RFC3686_NONCE_SIZE);
210
211 return 0;
212}
213
214int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req)
215{
216 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
217 __be32 iv[AES_IV_SIZE / 4] = { *(u32 *)ctx->state_record,
218 *(u32 *) req->info, *(u32 *) (req->info + 4), cpu_to_be32(1) };
219
220 ctx->direction = DIR_OUTBOUND;
221 ctx->pd_ctl = 1;
222
223 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
224 req->nbytes, iv, AES_IV_SIZE);
225}
226
227int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req)
228{
229 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
230 __be32 iv[AES_IV_SIZE / 4] = { *(u32 *)ctx->state_record,
231 *(u32 *) req->info, *(u32 *) (req->info + 4), cpu_to_be32(1) };
232
233 ctx->direction = DIR_INBOUND;
234 ctx->pd_ctl = 1;
235
236 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
237 req->nbytes, iv, AES_IV_SIZE);
238}
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);
251 struct dynamic_sa_ctl *sa;
252 struct dynamic_sa_hash160 *sa_in;
253 int rc;
254
255 ctx->dev = my_alg->dev;
256 ctx->is_hash = 1;
James Hsiao049359d2009-02-05 16:18:13 +1100257
258 /* Create SA */
259 if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
260 crypto4xx_free_sa(ctx);
261
262 rc = crypto4xx_alloc_sa(ctx, sa_len);
263 if (rc)
264 return rc;
265
266 if (ctx->state_record_dma_addr == 0) {
267 crypto4xx_alloc_state_record(ctx);
268 if (!ctx->state_record_dma_addr) {
269 crypto4xx_free_sa(ctx);
270 return -ENOMEM;
271 }
272 }
273
Herbert Xu6b1679f2009-07-12 23:08:28 +0800274 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
275 sizeof(struct crypto4xx_ctx));
James Hsiao049359d2009-02-05 16:18:13 +1100276 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
277 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
278 SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
279 SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
280 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
281 SA_OPCODE_HASH, DIR_INBOUND);
282 set_dynamic_sa_command_1(sa, 0, SA_HASH_MODE_HASH,
283 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
284 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
285 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
286 SA_NOT_COPY_HDR);
287 ctx->direction = DIR_INBOUND;
Christian Lamparter453e3092017-08-25 15:47:19 +0200288 sa->sa_contents.w = SA_HASH160_CONTENTS;
James Hsiao049359d2009-02-05 16:18:13 +1100289 sa_in = (struct dynamic_sa_hash160 *) ctx->sa_in;
290 /* Need to zero hash digest in SA */
291 memset(sa_in->inner_digest, 0, sizeof(sa_in->inner_digest));
292 memset(sa_in->outer_digest, 0, sizeof(sa_in->outer_digest));
293 sa_in->state_ptr = ctx->state_record_dma_addr;
Christian Lamparter249c8d92017-08-25 15:47:20 +0200294 ctx->offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(sa);
James Hsiao049359d2009-02-05 16:18:13 +1100295
296 return 0;
297}
298
299int crypto4xx_hash_init(struct ahash_request *req)
300{
301 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
302 int ds;
303 struct dynamic_sa_ctl *sa;
304
305 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
306 ds = crypto_ahash_digestsize(
307 __crypto_ahash_cast(req->base.tfm));
308 sa->sa_command_0.bf.digest_len = ds >> 2;
309 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
310 ctx->is_hash = 1;
311 ctx->direction = DIR_INBOUND;
312
313 return 0;
314}
315
316int crypto4xx_hash_update(struct ahash_request *req)
317{
318 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
319
320 ctx->is_hash = 1;
James Hsiao049359d2009-02-05 16:18:13 +1100321 ctx->pd_ctl = 0x11;
322 ctx->direction = DIR_INBOUND;
323
324 return crypto4xx_build_pd(&req->base, ctx, req->src,
325 (struct scatterlist *) req->result,
326 req->nbytes, NULL, 0);
327}
328
329int crypto4xx_hash_final(struct ahash_request *req)
330{
331 return 0;
332}
333
334int crypto4xx_hash_digest(struct ahash_request *req)
335{
336 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
337
James Hsiao049359d2009-02-05 16:18:13 +1100338 ctx->pd_ctl = 0x11;
339 ctx->direction = DIR_INBOUND;
340
341 return crypto4xx_build_pd(&req->base, ctx, req->src,
342 (struct scatterlist *) req->result,
343 req->nbytes, NULL, 0);
344}
345
346/**
347 * SHA1 Algorithm
348 */
349int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
350{
351 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
352 SA_HASH_MODE_HASH);
353}
354
355