1 /**
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>
31 #include "crypto4xx_reg_def.h"
32 #include "crypto4xx_sa.h"
33 #include "crypto4xx_core.h"
34
set_dynamic_sa_command_0(struct dynamic_sa_ctl * sa,u32 save_h,u32 save_iv,u32 ld_h,u32 ld_iv,u32 hdr_proc,u32 h,u32 c,u32 pad_type,u32 op_grp,u32 op,u32 dir)35 static void set_dynamic_sa_command_0(struct dynamic_sa_ctl *sa, u32 save_h,
36 u32 save_iv, u32 ld_h, u32 ld_iv,
37 u32 hdr_proc, u32 h, u32 c, u32 pad_type,
38 u32 op_grp, u32 op, u32 dir)
39 {
40 sa->sa_command_0.w = 0;
41 sa->sa_command_0.bf.save_hash_state = save_h;
42 sa->sa_command_0.bf.save_iv = save_iv;
43 sa->sa_command_0.bf.load_hash_state = ld_h;
44 sa->sa_command_0.bf.load_iv = ld_iv;
45 sa->sa_command_0.bf.hdr_proc = hdr_proc;
46 sa->sa_command_0.bf.hash_alg = h;
47 sa->sa_command_0.bf.cipher_alg = c;
48 sa->sa_command_0.bf.pad_type = pad_type & 3;
49 sa->sa_command_0.bf.extend_pad = pad_type >> 2;
50 sa->sa_command_0.bf.op_group = op_grp;
51 sa->sa_command_0.bf.opcode = op;
52 sa->sa_command_0.bf.dir = dir;
53 }
54
set_dynamic_sa_command_1(struct dynamic_sa_ctl * sa,u32 cm,u32 hmac_mc,u32 cfb,u32 esn,u32 sn_mask,u32 mute,u32 cp_pad,u32 cp_pay,u32 cp_hdr)55 static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
56 u32 hmac_mc, u32 cfb, u32 esn,
57 u32 sn_mask, u32 mute, u32 cp_pad,
58 u32 cp_pay, u32 cp_hdr)
59 {
60 sa->sa_command_1.w = 0;
61 sa->sa_command_1.bf.crypto_mode31 = (cm & 4) >> 2;
62 sa->sa_command_1.bf.crypto_mode9_8 = cm & 3;
63 sa->sa_command_1.bf.feedback_mode = cfb,
64 sa->sa_command_1.bf.sa_rev = 1;
65 sa->sa_command_1.bf.extended_seq_num = esn;
66 sa->sa_command_1.bf.seq_num_mask = sn_mask;
67 sa->sa_command_1.bf.mutable_bit_proc = mute;
68 sa->sa_command_1.bf.copy_pad = cp_pad;
69 sa->sa_command_1.bf.copy_payload = cp_pay;
70 sa->sa_command_1.bf.copy_hdr = cp_hdr;
71 }
72
crypto4xx_encrypt(struct ablkcipher_request * req)73 int crypto4xx_encrypt(struct ablkcipher_request *req)
74 {
75 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
76
77 ctx->direction = DIR_OUTBOUND;
78 ctx->hash_final = 0;
79 ctx->is_hash = 0;
80 ctx->pd_ctl = 0x1;
81
82 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
83 req->nbytes, req->info,
84 get_dynamic_sa_iv_size(ctx));
85 }
86
crypto4xx_decrypt(struct ablkcipher_request * req)87 int crypto4xx_decrypt(struct ablkcipher_request *req)
88 {
89 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
90
91 ctx->direction = DIR_INBOUND;
92 ctx->hash_final = 0;
93 ctx->is_hash = 0;
94 ctx->pd_ctl = 1;
95
96 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
97 req->nbytes, req->info,
98 get_dynamic_sa_iv_size(ctx));
99 }
100
101 /**
102 * AES Functions
103 */
crypto4xx_setkey_aes(struct crypto_ablkcipher * cipher,const u8 * key,unsigned int keylen,unsigned char cm,u8 fb)104 static 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 */
138 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
139 ctx->hash_final = 0;
140
141 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_CBC ?
142 SA_SAVE_IV : 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);
154 crypto4xx_memcpy_le(ctx->sa_in + get_dynamic_sa_offset_key_field(ctx),
155 key, keylen);
156 sa->sa_contents = SA_AES_CONTENTS | (keylen << 2);
157 sa->sa_command_1.bf.key_len = keylen >> 3;
158 ctx->is_hash = 0;
159 ctx->direction = DIR_INBOUND;
160 memcpy(ctx->sa_in + get_dynamic_sa_offset_state_ptr_field(ctx),
161 (void *)&ctx->state_record_dma_addr, 4);
162 ctx->offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(ctx);
163
164 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
165 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
166 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
167
168 return 0;
169 }
170
crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher * cipher,const u8 * key,unsigned int keylen)171 int crypto4xx_setkey_aes_cbc(struct crypto_ablkcipher *cipher,
172 const u8 *key, unsigned int keylen)
173 {
174 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
175 CRYPTO_FEEDBACK_MODE_NO_FB);
176 }
177
178 /**
179 * HASH SHA1 Functions
180 */
crypto4xx_hash_alg_init(struct crypto_tfm * tfm,unsigned int sa_len,unsigned char ha,unsigned char hm)181 static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
182 unsigned int sa_len,
183 unsigned char ha,
184 unsigned char hm)
185 {
186 struct crypto_alg *alg = tfm->__crt_alg;
187 struct crypto4xx_alg *my_alg = crypto_alg_to_crypto4xx_alg(alg);
188 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
189 struct dynamic_sa_ctl *sa;
190 struct dynamic_sa_hash160 *sa_in;
191 int rc;
192
193 ctx->dev = my_alg->dev;
194 ctx->is_hash = 1;
195 ctx->hash_final = 0;
196
197 /* Create SA */
198 if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
199 crypto4xx_free_sa(ctx);
200
201 rc = crypto4xx_alloc_sa(ctx, sa_len);
202 if (rc)
203 return rc;
204
205 if (ctx->state_record_dma_addr == 0) {
206 crypto4xx_alloc_state_record(ctx);
207 if (!ctx->state_record_dma_addr) {
208 crypto4xx_free_sa(ctx);
209 return -ENOMEM;
210 }
211 }
212
213 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
214 sizeof(struct crypto4xx_ctx));
215 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
216 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
217 SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
218 SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
219 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
220 SA_OPCODE_HASH, DIR_INBOUND);
221 set_dynamic_sa_command_1(sa, 0, SA_HASH_MODE_HASH,
222 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
223 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
224 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
225 SA_NOT_COPY_HDR);
226 ctx->direction = DIR_INBOUND;
227 sa->sa_contents = SA_HASH160_CONTENTS;
228 sa_in = (struct dynamic_sa_hash160 *) ctx->sa_in;
229 /* Need to zero hash digest in SA */
230 memset(sa_in->inner_digest, 0, sizeof(sa_in->inner_digest));
231 memset(sa_in->outer_digest, 0, sizeof(sa_in->outer_digest));
232 sa_in->state_ptr = ctx->state_record_dma_addr;
233 ctx->offset_to_sr_ptr = get_dynamic_sa_offset_state_ptr_field(ctx);
234
235 return 0;
236 }
237
crypto4xx_hash_init(struct ahash_request * req)238 int crypto4xx_hash_init(struct ahash_request *req)
239 {
240 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
241 int ds;
242 struct dynamic_sa_ctl *sa;
243
244 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
245 ds = crypto_ahash_digestsize(
246 __crypto_ahash_cast(req->base.tfm));
247 sa->sa_command_0.bf.digest_len = ds >> 2;
248 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
249 ctx->is_hash = 1;
250 ctx->direction = DIR_INBOUND;
251
252 return 0;
253 }
254
crypto4xx_hash_update(struct ahash_request * req)255 int crypto4xx_hash_update(struct ahash_request *req)
256 {
257 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
258
259 ctx->is_hash = 1;
260 ctx->hash_final = 0;
261 ctx->pd_ctl = 0x11;
262 ctx->direction = DIR_INBOUND;
263
264 return crypto4xx_build_pd(&req->base, ctx, req->src,
265 (struct scatterlist *) req->result,
266 req->nbytes, NULL, 0);
267 }
268
crypto4xx_hash_final(struct ahash_request * req)269 int crypto4xx_hash_final(struct ahash_request *req)
270 {
271 return 0;
272 }
273
crypto4xx_hash_digest(struct ahash_request * req)274 int crypto4xx_hash_digest(struct ahash_request *req)
275 {
276 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
277
278 ctx->hash_final = 1;
279 ctx->pd_ctl = 0x11;
280 ctx->direction = DIR_INBOUND;
281
282 return crypto4xx_build_pd(&req->base, ctx, req->src,
283 (struct scatterlist *) req->result,
284 req->nbytes, NULL, 0);
285 }
286
287 /**
288 * SHA1 Algorithm
289 */
crypto4xx_sha1_alg_init(struct crypto_tfm * tfm)290 int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
291 {
292 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
293 SA_HASH_MODE_HASH);
294 }
295
296
297