blob: 0e1d110a6405591fc9d34a7de24a2191f0fad6fc [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);
78
79 ctx->direction = DIR_OUTBOUND;
James Hsiao049359d2009-02-05 16:18:13 +110080 ctx->is_hash = 0;
81 ctx->pd_ctl = 0x1;
82
83 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparter249c8d92017-08-25 15:47:20 +020084 req->nbytes, req->info,
85 crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req)));
James Hsiao049359d2009-02-05 16:18:13 +110086}
87
88int crypto4xx_decrypt(struct ablkcipher_request *req)
89{
90 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
91
92 ctx->direction = DIR_INBOUND;
James Hsiao049359d2009-02-05 16:18:13 +110093 ctx->is_hash = 0;
94 ctx->pd_ctl = 1;
95
96 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparter249c8d92017-08-25 15:47:20 +020097 req->nbytes, req->info,
98 crypto_ablkcipher_ivsize(crypto_ablkcipher_reqtfm(req)));
James Hsiao049359d2009-02-05 16:18:13 +110099}
100
101/**
102 * AES Functions
103 */
104static int crypto4xx_setkey_aes(struct crypto_ablkcipher *cipher,
105 const u8 *key,
106 unsigned int keylen,
107 unsigned char cm,
108 u8 fb)
109{
110 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
111 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
112 struct dynamic_sa_ctl *sa;
113 int rc;
114
115 if (keylen != AES_KEYSIZE_256 &&
116 keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
117 crypto_ablkcipher_set_flags(cipher,
118 CRYPTO_TFM_RES_BAD_KEY_LEN);
119 return -EINVAL;
120 }
121
122 /* Create SA */
123 if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
124 crypto4xx_free_sa(ctx);
125
126 rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
127 if (rc)
128 return rc;
129
130 if (ctx->state_record_dma_addr == 0) {
131 rc = crypto4xx_alloc_state_record(ctx);
132 if (rc) {
133 crypto4xx_free_sa(ctx);
134 return rc;
135 }
136 }
137 /* Setup SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200138 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100139
140 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, SA_NOT_SAVE_IV,
141 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
142 SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
143 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
144 SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
145 DIR_INBOUND);
146
147 set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
148 fb, SA_EXTENDED_SN_OFF,
149 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
150 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
151 SA_NOT_COPY_HDR);
Christian Lamparter4865b1222017-10-04 01:00:10 +0200152 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
153 key, keylen);
Christian Lamparter453e3092017-08-25 15:47:19 +0200154 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
James Hsiao049359d2009-02-05 16:18:13 +1100155 sa->sa_command_1.bf.key_len = keylen >> 3;
156 ctx->is_hash = 0;
157 ctx->direction = DIR_INBOUND;
Christian Lamparter249c8d92017-08-25 15:47:20 +0200158 memcpy(sa + get_dynamic_sa_offset_state_ptr_field(sa),
159 (void *)&ctx->state_record_dma_addr, 4);
160 ctx->offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(sa);
James Hsiao049359d2009-02-05 16:18:13 +1100161
162 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200163 sa = ctx->sa_out;
James Hsiao049359d2009-02-05 16:18:13 +1100164 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
165
166 return 0;
167}
168
169int crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher *cipher,
170 const u8 *key, unsigned int keylen)
171{
172 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
173 CRYPTO_FEEDBACK_MODE_NO_FB);
174}
175
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200176int crypto4xx_setkey_aes_cfb(struct crypto_ablkcipher *cipher,
177 const u8 *key, unsigned int keylen)
178{
179 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
180 CRYPTO_FEEDBACK_MODE_128BIT_CFB);
181}
182
183int crypto4xx_setkey_aes_ecb(struct crypto_ablkcipher *cipher,
184 const u8 *key, unsigned int keylen)
185{
186 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
187 CRYPTO_FEEDBACK_MODE_NO_FB);
188}
189
190int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
191 const u8 *key, unsigned int keylen)
192{
193 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
194 CRYPTO_FEEDBACK_MODE_64BIT_OFB);
195}
196
197int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
198 const u8 *key, unsigned int keylen)
199{
200 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
201 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
202 int rc;
203
204 rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
205 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
206 if (rc)
207 return rc;
208
209 memcpy(ctx->state_record,
210 key + keylen - CTR_RFC3686_NONCE_SIZE, CTR_RFC3686_NONCE_SIZE);
211
212 return 0;
213}
214
215int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req)
216{
217 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
218 __be32 iv[AES_IV_SIZE / 4] = { *(u32 *)ctx->state_record,
219 *(u32 *) req->info, *(u32 *) (req->info + 4), cpu_to_be32(1) };
220
221 ctx->direction = DIR_OUTBOUND;
222 ctx->pd_ctl = 1;
223
224 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
225 req->nbytes, iv, AES_IV_SIZE);
226}
227
228int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req)
229{
230 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
231 __be32 iv[AES_IV_SIZE / 4] = { *(u32 *)ctx->state_record,
232 *(u32 *) req->info, *(u32 *) (req->info + 4), cpu_to_be32(1) };
233
234 ctx->direction = DIR_INBOUND;
235 ctx->pd_ctl = 1;
236
237 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
238 req->nbytes, iv, AES_IV_SIZE);
239}
240
James Hsiao049359d2009-02-05 16:18:13 +1100241/**
242 * HASH SHA1 Functions
243 */
244static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
245 unsigned int sa_len,
246 unsigned char ha,
247 unsigned char hm)
248{
249 struct crypto_alg *alg = tfm->__crt_alg;
250 struct crypto4xx_alg *my_alg = crypto_alg_to_crypto4xx_alg(alg);
251 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200252 struct dynamic_sa_hash160 *sa;
James Hsiao049359d2009-02-05 16:18:13 +1100253 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));
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200276 sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
277 set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
James Hsiao049359d2009-02-05 16:18:13 +1100278 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);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200282 set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
James Hsiao049359d2009-02-05 16:18:13 +1100283 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;
James Hsiao049359d2009-02-05 16:18:13 +1100288 /* Need to zero hash digest in SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200289 memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
290 memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
291 sa->state_ptr = ctx->state_record_dma_addr;
292 ctx->offset_to_sr_ptr =
293 get_dynamic_sa_offset_state_ptr_field(&sa->ctrl);
James Hsiao049359d2009-02-05 16:18:13 +1100294
295 return 0;
296}
297
298int crypto4xx_hash_init(struct ahash_request *req)
299{
300 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
301 int ds;
302 struct dynamic_sa_ctl *sa;
303
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200304 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100305 ds = crypto_ahash_digestsize(
306 __crypto_ahash_cast(req->base.tfm));
307 sa->sa_command_0.bf.digest_len = ds >> 2;
308 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
309 ctx->is_hash = 1;
310 ctx->direction = DIR_INBOUND;
311
312 return 0;
313}
314
315int crypto4xx_hash_update(struct ahash_request *req)
316{
317 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
318
319 ctx->is_hash = 1;
James Hsiao049359d2009-02-05 16:18:13 +1100320 ctx->pd_ctl = 0x11;
321 ctx->direction = DIR_INBOUND;
322
323 return crypto4xx_build_pd(&req->base, ctx, req->src,
324 (struct scatterlist *) req->result,
325 req->nbytes, NULL, 0);
326}
327
328int crypto4xx_hash_final(struct ahash_request *req)
329{
330 return 0;
331}
332
333int crypto4xx_hash_digest(struct ahash_request *req)
334{
335 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
336
James Hsiao049359d2009-02-05 16:18:13 +1100337 ctx->pd_ctl = 0x11;
338 ctx->direction = DIR_INBOUND;
339
340 return crypto4xx_build_pd(&req->base, ctx, req->src,
341 (struct scatterlist *) req->result,
342 req->nbytes, NULL, 0);
343}
344
345/**
346 * SHA1 Algorithm
347 */
348int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
349{
350 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
351 SA_HASH_MODE_HASH);
352}
353
354