blob: 307f5cfa9ba48cfc8f12549ed4e9379a51a7408c [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>
Christian Lampartera0aae822017-10-04 01:00:15 +020029#include <crypto/aead.h>
James Hsiao049359d2009-02-05 16:18:13 +110030#include <crypto/aes.h>
Christian Lamparter59231362017-10-04 01:00:17 +020031#include <crypto/gcm.h>
James Hsiao049359d2009-02-05 16:18:13 +110032#include <crypto/sha.h>
Christian Lamparterf2a13e72017-08-25 15:47:21 +020033#include <crypto/ctr.h>
Christian Lamparterce05ffe2018-04-19 18:41:52 +020034#include <crypto/skcipher.h>
James Hsiao049359d2009-02-05 16:18:13 +110035#include "crypto4xx_reg_def.h"
James Hsiao049359d2009-02-05 16:18:13 +110036#include "crypto4xx_core.h"
Christian Lamparter249c8d92017-08-25 15:47:20 +020037#include "crypto4xx_sa.h"
James Hsiao049359d2009-02-05 16:18:13 +110038
Jingoo Han3a4eac72013-08-09 16:59:57 +090039static void set_dynamic_sa_command_0(struct dynamic_sa_ctl *sa, u32 save_h,
40 u32 save_iv, u32 ld_h, u32 ld_iv,
41 u32 hdr_proc, u32 h, u32 c, u32 pad_type,
42 u32 op_grp, u32 op, u32 dir)
James Hsiao049359d2009-02-05 16:18:13 +110043{
44 sa->sa_command_0.w = 0;
45 sa->sa_command_0.bf.save_hash_state = save_h;
46 sa->sa_command_0.bf.save_iv = save_iv;
47 sa->sa_command_0.bf.load_hash_state = ld_h;
48 sa->sa_command_0.bf.load_iv = ld_iv;
49 sa->sa_command_0.bf.hdr_proc = hdr_proc;
50 sa->sa_command_0.bf.hash_alg = h;
51 sa->sa_command_0.bf.cipher_alg = c;
52 sa->sa_command_0.bf.pad_type = pad_type & 3;
53 sa->sa_command_0.bf.extend_pad = pad_type >> 2;
54 sa->sa_command_0.bf.op_group = op_grp;
55 sa->sa_command_0.bf.opcode = op;
56 sa->sa_command_0.bf.dir = dir;
57}
58
Jingoo Han3a4eac72013-08-09 16:59:57 +090059static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
60 u32 hmac_mc, u32 cfb, u32 esn,
61 u32 sn_mask, u32 mute, u32 cp_pad,
62 u32 cp_pay, u32 cp_hdr)
James Hsiao049359d2009-02-05 16:18:13 +110063{
64 sa->sa_command_1.w = 0;
65 sa->sa_command_1.bf.crypto_mode31 = (cm & 4) >> 2;
66 sa->sa_command_1.bf.crypto_mode9_8 = cm & 3;
67 sa->sa_command_1.bf.feedback_mode = cfb,
68 sa->sa_command_1.bf.sa_rev = 1;
Christian Lamparter5a4326d32017-10-04 01:00:05 +020069 sa->sa_command_1.bf.hmac_muting = hmac_mc;
James Hsiao049359d2009-02-05 16:18:13 +110070 sa->sa_command_1.bf.extended_seq_num = esn;
71 sa->sa_command_1.bf.seq_num_mask = sn_mask;
72 sa->sa_command_1.bf.mutable_bit_proc = mute;
73 sa->sa_command_1.bf.copy_pad = cp_pad;
74 sa->sa_command_1.bf.copy_payload = cp_pay;
75 sa->sa_command_1.bf.copy_hdr = cp_hdr;
76}
77
Christian Lamparterce05ffe2018-04-19 18:41:52 +020078static inline int crypto4xx_crypt(struct skcipher_request *req,
Christian Lampartera8d79d72018-04-19 18:41:51 +020079 const unsigned int ivlen, bool decrypt)
James Hsiao049359d2009-02-05 16:18:13 +110080{
Christian Lamparterce05ffe2018-04-19 18:41:52 +020081 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
82 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
Christian Lamparterc4e90652018-04-19 18:41:53 +020083 __le32 iv[AES_IV_SIZE];
James Hsiao049359d2009-02-05 16:18:13 +110084
Christian Lampartercd4dcd62017-10-04 01:00:11 +020085 if (ivlen)
Christian Lamparterce05ffe2018-04-19 18:41:52 +020086 crypto4xx_memcpy_to_le32(iv, req->iv, ivlen);
James Hsiao049359d2009-02-05 16:18:13 +110087
88 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparterce05ffe2018-04-19 18:41:52 +020089 req->cryptlen, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +020090 ctx->sa_len, 0, NULL);
James Hsiao049359d2009-02-05 16:18:13 +110091}
92
Christian Lamparterce05ffe2018-04-19 18:41:52 +020093int crypto4xx_encrypt_noiv(struct skcipher_request *req)
James Hsiao049359d2009-02-05 16:18:13 +110094{
Christian Lampartera8d79d72018-04-19 18:41:51 +020095 return crypto4xx_crypt(req, 0, false);
96}
James Hsiao049359d2009-02-05 16:18:13 +110097
Christian Lamparterce05ffe2018-04-19 18:41:52 +020098int crypto4xx_encrypt_iv(struct skcipher_request *req)
Christian Lampartera8d79d72018-04-19 18:41:51 +020099{
100 return crypto4xx_crypt(req, AES_IV_SIZE, false);
101}
James Hsiao049359d2009-02-05 16:18:13 +1100102
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200103int crypto4xx_decrypt_noiv(struct skcipher_request *req)
Christian Lampartera8d79d72018-04-19 18:41:51 +0200104{
105 return crypto4xx_crypt(req, 0, true);
106}
107
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200108int crypto4xx_decrypt_iv(struct skcipher_request *req)
Christian Lampartera8d79d72018-04-19 18:41:51 +0200109{
110 return crypto4xx_crypt(req, AES_IV_SIZE, true);
James Hsiao049359d2009-02-05 16:18:13 +1100111}
112
113/**
114 * AES Functions
115 */
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200116static int crypto4xx_setkey_aes(struct crypto_skcipher *cipher,
James Hsiao049359d2009-02-05 16:18:13 +1100117 const u8 *key,
118 unsigned int keylen,
119 unsigned char cm,
120 u8 fb)
121{
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200122 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
James Hsiao049359d2009-02-05 16:18:13 +1100123 struct dynamic_sa_ctl *sa;
124 int rc;
125
126 if (keylen != AES_KEYSIZE_256 &&
127 keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_128) {
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200128 crypto_skcipher_set_flags(cipher,
James Hsiao049359d2009-02-05 16:18:13 +1100129 CRYPTO_TFM_RES_BAD_KEY_LEN);
130 return -EINVAL;
131 }
132
133 /* Create SA */
Christian Lamparter2f776902017-10-04 01:00:14 +0200134 if (ctx->sa_in || ctx->sa_out)
James Hsiao049359d2009-02-05 16:18:13 +1100135 crypto4xx_free_sa(ctx);
136
137 rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
138 if (rc)
139 return rc;
140
James Hsiao049359d2009-02-05 16:18:13 +1100141 /* Setup SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200142 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100143
Christian Lamparter25baaf82019-04-22 13:25:58 +0200144 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_ECB ?
145 SA_NOT_SAVE_IV : SA_SAVE_IV),
146 SA_NOT_LOAD_HASH, (cm == CRYPTO_MODE_ECB ?
147 SA_LOAD_IV_FROM_SA : SA_LOAD_IV_FROM_STATE),
James Hsiao049359d2009-02-05 16:18:13 +1100148 SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
149 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
150 SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
151 DIR_INBOUND);
152
153 set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
154 fb, SA_EXTENDED_SN_OFF,
155 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
156 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
157 SA_NOT_COPY_HDR);
Christian Lamparter4865b1222017-10-04 01:00:10 +0200158 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
159 key, keylen);
Christian Lamparter453e3092017-08-25 15:47:19 +0200160 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
James Hsiao049359d2009-02-05 16:18:13 +1100161 sa->sa_command_1.bf.key_len = keylen >> 3;
James Hsiao049359d2009-02-05 16:18:13 +1100162
163 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200164 sa = ctx->sa_out;
James Hsiao049359d2009-02-05 16:18:13 +1100165 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
Christian Lamparter25baaf82019-04-22 13:25:58 +0200166 /*
167 * SA_OPCODE_ENCRYPT is the same value as SA_OPCODE_DECRYPT.
168 * it's the DIR_(IN|OUT)BOUND that matters
169 */
170 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT;
James Hsiao049359d2009-02-05 16:18:13 +1100171
172 return 0;
173}
174
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200175int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
James Hsiao049359d2009-02-05 16:18:13 +1100176 const u8 *key, unsigned int keylen)
177{
178 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
179 CRYPTO_FEEDBACK_MODE_NO_FB);
180}
181
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200182int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher,
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200183 const u8 *key, unsigned int keylen)
184{
185 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
186 CRYPTO_FEEDBACK_MODE_128BIT_CFB);
187}
188
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200189int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200190 const u8 *key, unsigned int keylen)
191{
192 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
193 CRYPTO_FEEDBACK_MODE_NO_FB);
194}
195
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200196int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher,
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200197 const u8 *key, unsigned int keylen)
198{
199 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
200 CRYPTO_FEEDBACK_MODE_64BIT_OFB);
201}
202
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200203int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200204 const u8 *key, unsigned int keylen)
205{
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200206 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200207 int rc;
208
209 rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
210 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
211 if (rc)
212 return rc;
213
Christian Lamparter2f776902017-10-04 01:00:14 +0200214 ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
215 CTR_RFC3686_NONCE_SIZE]);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200216
217 return 0;
218}
219
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200220int crypto4xx_rfc3686_encrypt(struct skcipher_request *req)
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200221{
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200222 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
223 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200224 __le32 iv[AES_IV_SIZE / 4] = {
Christian Lamparter2f776902017-10-04 01:00:14 +0200225 ctx->iv_nonce,
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200226 cpu_to_le32p((u32 *) req->iv),
227 cpu_to_le32p((u32 *) (req->iv + 4)),
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200228 cpu_to_le32(1) };
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200229
230 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200231 req->cryptlen, iv, AES_IV_SIZE,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200232 ctx->sa_out, ctx->sa_len, 0, NULL);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200233}
234
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200235int crypto4xx_rfc3686_decrypt(struct skcipher_request *req)
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200236{
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200237 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
238 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200239 __le32 iv[AES_IV_SIZE / 4] = {
Christian Lamparter2f776902017-10-04 01:00:14 +0200240 ctx->iv_nonce,
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200241 cpu_to_le32p((u32 *) req->iv),
242 cpu_to_le32p((u32 *) (req->iv + 4)),
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200243 cpu_to_le32(1) };
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200244
245 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
Christian Lamparterce05ffe2018-04-19 18:41:52 +0200246 req->cryptlen, iv, AES_IV_SIZE,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200247 ctx->sa_out, ctx->sa_len, 0, NULL);
Christian Lamparterf2a13e72017-08-25 15:47:21 +0200248}
249
Christian Lamparter98e87e32018-04-19 18:41:54 +0200250static int
251crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
252{
253 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
254 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
255 size_t iv_len = crypto_skcipher_ivsize(cipher);
256 unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4));
257 unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
258 AES_BLOCK_SIZE;
259
260 /*
261 * The hardware uses only the last 32-bits as the counter while the
262 * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that
263 * the whole IV is a counter. So fallback if the counter is going to
264 * overlow.
265 */
266 if (counter + nblks < counter) {
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200267 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher.cipher);
Christian Lamparter98e87e32018-04-19 18:41:54 +0200268 int ret;
269
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200270 skcipher_request_set_sync_tfm(subreq, ctx->sw_cipher.cipher);
Christian Lamparter98e87e32018-04-19 18:41:54 +0200271 skcipher_request_set_callback(subreq, req->base.flags,
272 NULL, NULL);
273 skcipher_request_set_crypt(subreq, req->src, req->dst,
274 req->cryptlen, req->iv);
275 ret = encrypt ? crypto_skcipher_encrypt(subreq)
276 : crypto_skcipher_decrypt(subreq);
277 skcipher_request_zero(subreq);
278 return ret;
279 }
280
281 return encrypt ? crypto4xx_encrypt_iv(req)
282 : crypto4xx_decrypt_iv(req);
283}
284
285static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,
286 struct crypto_skcipher *cipher,
287 const u8 *key,
288 unsigned int keylen)
289{
290 int rc;
291
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200292 crypto_sync_skcipher_clear_flags(ctx->sw_cipher.cipher,
Christian Lamparter98e87e32018-04-19 18:41:54 +0200293 CRYPTO_TFM_REQ_MASK);
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200294 crypto_sync_skcipher_set_flags(ctx->sw_cipher.cipher,
Christian Lamparter98e87e32018-04-19 18:41:54 +0200295 crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200296 rc = crypto_sync_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen);
Christian Lamparter98e87e32018-04-19 18:41:54 +0200297 crypto_skcipher_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
298 crypto_skcipher_set_flags(cipher,
Christian Lamparter9848e4c2019-04-22 13:26:00 +0200299 crypto_sync_skcipher_get_flags(ctx->sw_cipher.cipher) &
Christian Lamparter98e87e32018-04-19 18:41:54 +0200300 CRYPTO_TFM_RES_MASK);
301
302 return rc;
303}
304
305int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
306 const u8 *key, unsigned int keylen)
307{
308 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
309 int rc;
310
311 rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen);
312 if (rc)
313 return rc;
314
315 return crypto4xx_setkey_aes(cipher, key, keylen,
316 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
317}
318
319int crypto4xx_encrypt_ctr(struct skcipher_request *req)
320{
321 return crypto4xx_ctr_crypt(req, true);
322}
323
324int crypto4xx_decrypt_ctr(struct skcipher_request *req)
325{
326 return crypto4xx_ctr_crypt(req, false);
327}
328
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200329static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
Christian Lamparter584201f2018-04-19 18:41:56 +0200330 unsigned int len,
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200331 bool is_ccm, bool decrypt)
332{
333 struct crypto_aead *aead = crypto_aead_reqtfm(req);
334
335 /* authsize has to be a multiple of 4 */
336 if (aead->authsize & 3)
337 return true;
338
339 /*
Christian Lamparter584201f2018-04-19 18:41:56 +0200340 * hardware does not handle cases where plaintext
341 * is less than a block.
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200342 */
Christian Lamparter584201f2018-04-19 18:41:56 +0200343 if (len < AES_BLOCK_SIZE)
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200344 return true;
345
Christian Lamparter584201f2018-04-19 18:41:56 +0200346 /* assoc len needs to be a multiple of 4 and <= 1020 */
347 if (req->assoclen & 0x3 || req->assoclen > 1020)
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200348 return true;
349
350 /* CCM supports only counter field length of 2 and 4 bytes */
351 if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
352 return true;
353
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200354 return false;
355}
356
357static int crypto4xx_aead_fallback(struct aead_request *req,
358 struct crypto4xx_ctx *ctx, bool do_decrypt)
359{
Christian Lamparterc4e90652018-04-19 18:41:53 +0200360 struct aead_request *subreq = aead_request_ctx(req);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200361
362 aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
363 aead_request_set_callback(subreq, req->base.flags,
364 req->base.complete, req->base.data);
365 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
366 req->iv);
367 aead_request_set_ad(subreq, req->assoclen);
368 return do_decrypt ? crypto_aead_decrypt(subreq) :
369 crypto_aead_encrypt(subreq);
370}
371
Christian Lamparter98e87e32018-04-19 18:41:54 +0200372static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx,
373 struct crypto_aead *cipher,
374 const u8 *key,
375 unsigned int keylen)
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200376{
377 int rc;
378
379 crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
380 crypto_aead_set_flags(ctx->sw_cipher.aead,
381 crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
382 rc = crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
383 crypto_aead_clear_flags(cipher, CRYPTO_TFM_RES_MASK);
384 crypto_aead_set_flags(cipher,
385 crypto_aead_get_flags(ctx->sw_cipher.aead) &
386 CRYPTO_TFM_RES_MASK);
387
388 return rc;
389}
390
391/**
392 * AES-CCM Functions
393 */
394
395int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
396 unsigned int keylen)
397{
398 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
399 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
400 struct dynamic_sa_ctl *sa;
401 int rc = 0;
402
Christian Lamparter98e87e32018-04-19 18:41:54 +0200403 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200404 if (rc)
405 return rc;
406
407 if (ctx->sa_in || ctx->sa_out)
408 crypto4xx_free_sa(ctx);
409
410 rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
411 if (rc)
412 return rc;
413
414 /* Setup SA */
415 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
416 sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
417
Christian Lamparter0b5a7f72017-12-23 19:45:46 +0100418 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200419 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
420 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
421 SA_CIPHER_ALG_AES,
422 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
423 SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
424
425 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
426 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
427 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
428 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
429 SA_NOT_COPY_HDR);
430
431 sa->sa_command_1.bf.key_len = keylen >> 3;
432
433 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
434
435 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
436 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
437
438 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
439 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
440 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
441 SA_CIPHER_ALG_AES,
442 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
443 SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
444
445 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
446 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
447 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
448 SA_COPY_PAD, SA_COPY_PAYLOAD,
449 SA_NOT_COPY_HDR);
450
451 sa->sa_command_1.bf.key_len = keylen >> 3;
452 return 0;
453}
454
455static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
456{
457 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200458 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200459 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200460 __le32 iv[16];
Christian Lamparterc4e90652018-04-19 18:41:53 +0200461 u32 tmp_sa[SA_AES128_CCM_LEN + 4];
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200462 struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
Christian Lamparter584201f2018-04-19 18:41:56 +0200463 unsigned int len = req->cryptlen;
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200464
465 if (decrypt)
466 len -= crypto_aead_authsize(aead);
467
Christian Lamparter584201f2018-04-19 18:41:56 +0200468 if (crypto4xx_aead_need_fallback(req, len, true, decrypt))
469 return crypto4xx_aead_fallback(req, ctx, decrypt);
470
Christian Lamparterc4e90652018-04-19 18:41:53 +0200471 memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200472 sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
473
474 if (req->iv[0] == 1) {
475 /* CRYPTO_MODE_AES_ICM */
476 sa->sa_command_1.bf.crypto_mode9_8 = 1;
477 }
478
479 iv[3] = cpu_to_le32(0);
480 crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
481
482 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
483 len, iv, sizeof(iv),
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200484 sa, ctx->sa_len, req->assoclen, rctx->dst);
Christian Lamparter65ea8b62017-10-04 01:00:16 +0200485}
486
487int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
488{
489 return crypto4xx_crypt_aes_ccm(req, false);
490}
491
492int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
493{
494 return crypto4xx_crypt_aes_ccm(req, true);
495}
496
497int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
498 unsigned int authsize)
499{
500 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
501 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
502
503 return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
504}
505
James Hsiao049359d2009-02-05 16:18:13 +1100506/**
Christian Lamparter59231362017-10-04 01:00:17 +0200507 * AES-GCM Functions
508 */
509
510static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
511{
512 switch (keylen) {
513 case 16:
514 case 24:
515 case 32:
516 return 0;
517 default:
518 return -EINVAL;
519 }
520}
521
522static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
523 unsigned int keylen)
524{
525 struct crypto_cipher *aes_tfm = NULL;
526 uint8_t src[16] = { 0 };
527 int rc = 0;
528
Eric Biggers1ad0f162018-11-14 12:19:39 -0800529 aes_tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_NEED_FALLBACK);
Christian Lamparter59231362017-10-04 01:00:17 +0200530 if (IS_ERR(aes_tfm)) {
531 rc = PTR_ERR(aes_tfm);
532 pr_warn("could not load aes cipher driver: %d\n", rc);
533 return rc;
534 }
535
536 rc = crypto_cipher_setkey(aes_tfm, key, keylen);
537 if (rc) {
538 pr_err("setkey() failed: %d\n", rc);
539 goto out;
540 }
541
542 crypto_cipher_encrypt_one(aes_tfm, src, src);
543 crypto4xx_memcpy_to_le32(hash_start, src, 16);
544out:
545 crypto_free_cipher(aes_tfm);
546 return rc;
547}
548
549int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
550 const u8 *key, unsigned int keylen)
551{
552 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
553 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
554 struct dynamic_sa_ctl *sa;
555 int rc = 0;
556
557 if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0) {
558 crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
559 return -EINVAL;
560 }
561
Christian Lamparter98e87e32018-04-19 18:41:54 +0200562 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
Christian Lamparter59231362017-10-04 01:00:17 +0200563 if (rc)
564 return rc;
565
566 if (ctx->sa_in || ctx->sa_out)
567 crypto4xx_free_sa(ctx);
568
569 rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
570 if (rc)
571 return rc;
572
573 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
574
575 sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
576 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
577 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
578 SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
579 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
580 SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
581 DIR_INBOUND);
582 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
583 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
584 SA_SEQ_MASK_ON, SA_MC_DISABLE,
585 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
586 SA_NOT_COPY_HDR);
587
588 sa->sa_command_1.bf.key_len = keylen >> 3;
589
590 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
591 key, keylen);
592
593 rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
594 key, keylen);
595 if (rc) {
596 pr_err("GCM hash key setting failed = %d\n", rc);
597 goto err;
598 }
599
600 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
601 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
602 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
603 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
604
605 return 0;
606err:
607 crypto4xx_free_sa(ctx);
608 return rc;
609}
610
611static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
612 bool decrypt)
613{
614 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lamparter584201f2018-04-19 18:41:56 +0200615 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
Christian Lamparter59231362017-10-04 01:00:17 +0200616 __le32 iv[4];
Christian Lamparter584201f2018-04-19 18:41:56 +0200617 unsigned int len = req->cryptlen;
Christian Lamparter59231362017-10-04 01:00:17 +0200618
Christian Lamparter584201f2018-04-19 18:41:56 +0200619 if (decrypt)
620 len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
621
622 if (crypto4xx_aead_need_fallback(req, len, false, decrypt))
Christian Lamparter59231362017-10-04 01:00:17 +0200623 return crypto4xx_aead_fallback(req, ctx, decrypt);
624
625 crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
626 iv[3] = cpu_to_le32(1);
627
Christian Lamparter59231362017-10-04 01:00:17 +0200628 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
629 len, iv, sizeof(iv),
630 decrypt ? ctx->sa_in : ctx->sa_out,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200631 ctx->sa_len, req->assoclen, rctx->dst);
Christian Lamparter59231362017-10-04 01:00:17 +0200632}
633
634int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
635{
636 return crypto4xx_crypt_aes_gcm(req, false);
637}
638
639int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
640{
641 return crypto4xx_crypt_aes_gcm(req, true);
642}
643
644/**
James Hsiao049359d2009-02-05 16:18:13 +1100645 * HASH SHA1 Functions
646 */
647static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
648 unsigned int sa_len,
649 unsigned char ha,
650 unsigned char hm)
651{
652 struct crypto_alg *alg = tfm->__crt_alg;
Christian Lampartera0aae822017-10-04 01:00:15 +0200653 struct crypto4xx_alg *my_alg;
James Hsiao049359d2009-02-05 16:18:13 +1100654 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200655 struct dynamic_sa_hash160 *sa;
James Hsiao049359d2009-02-05 16:18:13 +1100656 int rc;
657
Christian Lampartera0aae822017-10-04 01:00:15 +0200658 my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
659 alg.u.hash);
James Hsiao049359d2009-02-05 16:18:13 +1100660 ctx->dev = my_alg->dev;
James Hsiao049359d2009-02-05 16:18:13 +1100661
662 /* Create SA */
Christian Lamparter2f776902017-10-04 01:00:14 +0200663 if (ctx->sa_in || ctx->sa_out)
James Hsiao049359d2009-02-05 16:18:13 +1100664 crypto4xx_free_sa(ctx);
665
666 rc = crypto4xx_alloc_sa(ctx, sa_len);
667 if (rc)
668 return rc;
669
Herbert Xu6b1679f2009-07-12 23:08:28 +0800670 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
671 sizeof(struct crypto4xx_ctx));
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200672 sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
673 set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
James Hsiao049359d2009-02-05 16:18:13 +1100674 SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
675 SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
676 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
677 SA_OPCODE_HASH, DIR_INBOUND);
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200678 set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
James Hsiao049359d2009-02-05 16:18:13 +1100679 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
680 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
681 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
682 SA_NOT_COPY_HDR);
James Hsiao049359d2009-02-05 16:18:13 +1100683 /* Need to zero hash digest in SA */
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200684 memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
685 memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
James Hsiao049359d2009-02-05 16:18:13 +1100686
687 return 0;
688}
689
690int crypto4xx_hash_init(struct ahash_request *req)
691{
692 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
693 int ds;
694 struct dynamic_sa_ctl *sa;
695
Christian Lamparter9e0a0b32017-08-25 15:47:25 +0200696 sa = ctx->sa_in;
James Hsiao049359d2009-02-05 16:18:13 +1100697 ds = crypto_ahash_digestsize(
698 __crypto_ahash_cast(req->base.tfm));
699 sa->sa_command_0.bf.digest_len = ds >> 2;
700 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
James Hsiao049359d2009-02-05 16:18:13 +1100701
702 return 0;
703}
704
705int crypto4xx_hash_update(struct ahash_request *req)
706{
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200707 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
James Hsiao049359d2009-02-05 16:18:13 +1100708 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200709 struct scatterlist dst;
710 unsigned int ds = crypto_ahash_digestsize(ahash);
James Hsiao049359d2009-02-05 16:18:13 +1100711
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200712 sg_init_one(&dst, req->result, ds);
James Hsiao049359d2009-02-05 16:18:13 +1100713
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200714 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
715 req->nbytes, NULL, 0, ctx->sa_in,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200716 ctx->sa_len, 0, NULL);
James Hsiao049359d2009-02-05 16:18:13 +1100717}
718
719int crypto4xx_hash_final(struct ahash_request *req)
720{
721 return 0;
722}
723
724int crypto4xx_hash_digest(struct ahash_request *req)
725{
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200726 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
James Hsiao049359d2009-02-05 16:18:13 +1100727 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200728 struct scatterlist dst;
729 unsigned int ds = crypto_ahash_digestsize(ahash);
James Hsiao049359d2009-02-05 16:18:13 +1100730
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200731 sg_init_one(&dst, req->result, ds);
James Hsiao049359d2009-02-05 16:18:13 +1100732
Christian Lampartercd4dcd62017-10-04 01:00:11 +0200733 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
734 req->nbytes, NULL, 0, ctx->sa_in,
Christian Lamparter658c9d2b92018-04-19 18:41:57 +0200735 ctx->sa_len, 0, NULL);
James Hsiao049359d2009-02-05 16:18:13 +1100736}
737
738/**
739 * SHA1 Algorithm
740 */
741int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
742{
743 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
744 SA_HASH_MODE_HASH);
745}