0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/kernel.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/spinlock_types.h>
0014 #include <linux/scatterlist.h>
0015 #include <linux/crypto.h>
0016 #include <linux/hash.h>
0017 #include <crypto/internal/hash.h>
0018 #include <linux/dma-mapping.h>
0019 #include <crypto/algapi.h>
0020 #include <crypto/aead.h>
0021 #include <crypto/aes.h>
0022 #include <crypto/gcm.h>
0023 #include <crypto/sha1.h>
0024 #include <crypto/ctr.h>
0025 #include <crypto/skcipher.h>
0026 #include "crypto4xx_reg_def.h"
0027 #include "crypto4xx_core.h"
0028 #include "crypto4xx_sa.h"
0029
0030 static void set_dynamic_sa_command_0(struct dynamic_sa_ctl *sa, u32 save_h,
0031 u32 save_iv, u32 ld_h, u32 ld_iv,
0032 u32 hdr_proc, u32 h, u32 c, u32 pad_type,
0033 u32 op_grp, u32 op, u32 dir)
0034 {
0035 sa->sa_command_0.w = 0;
0036 sa->sa_command_0.bf.save_hash_state = save_h;
0037 sa->sa_command_0.bf.save_iv = save_iv;
0038 sa->sa_command_0.bf.load_hash_state = ld_h;
0039 sa->sa_command_0.bf.load_iv = ld_iv;
0040 sa->sa_command_0.bf.hdr_proc = hdr_proc;
0041 sa->sa_command_0.bf.hash_alg = h;
0042 sa->sa_command_0.bf.cipher_alg = c;
0043 sa->sa_command_0.bf.pad_type = pad_type & 3;
0044 sa->sa_command_0.bf.extend_pad = pad_type >> 2;
0045 sa->sa_command_0.bf.op_group = op_grp;
0046 sa->sa_command_0.bf.opcode = op;
0047 sa->sa_command_0.bf.dir = dir;
0048 }
0049
0050 static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
0051 u32 hmac_mc, u32 cfb, u32 esn,
0052 u32 sn_mask, u32 mute, u32 cp_pad,
0053 u32 cp_pay, u32 cp_hdr)
0054 {
0055 sa->sa_command_1.w = 0;
0056 sa->sa_command_1.bf.crypto_mode31 = (cm & 4) >> 2;
0057 sa->sa_command_1.bf.crypto_mode9_8 = cm & 3;
0058 sa->sa_command_1.bf.feedback_mode = cfb;
0059 sa->sa_command_1.bf.sa_rev = 1;
0060 sa->sa_command_1.bf.hmac_muting = hmac_mc;
0061 sa->sa_command_1.bf.extended_seq_num = esn;
0062 sa->sa_command_1.bf.seq_num_mask = sn_mask;
0063 sa->sa_command_1.bf.mutable_bit_proc = mute;
0064 sa->sa_command_1.bf.copy_pad = cp_pad;
0065 sa->sa_command_1.bf.copy_payload = cp_pay;
0066 sa->sa_command_1.bf.copy_hdr = cp_hdr;
0067 }
0068
0069 static inline int crypto4xx_crypt(struct skcipher_request *req,
0070 const unsigned int ivlen, bool decrypt,
0071 bool check_blocksize)
0072 {
0073 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
0074 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
0075 __le32 iv[AES_IV_SIZE];
0076
0077 if (check_blocksize && !IS_ALIGNED(req->cryptlen, AES_BLOCK_SIZE))
0078 return -EINVAL;
0079
0080 if (ivlen)
0081 crypto4xx_memcpy_to_le32(iv, req->iv, ivlen);
0082
0083 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
0084 req->cryptlen, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
0085 ctx->sa_len, 0, NULL);
0086 }
0087
0088 int crypto4xx_encrypt_noiv_block(struct skcipher_request *req)
0089 {
0090 return crypto4xx_crypt(req, 0, false, true);
0091 }
0092
0093 int crypto4xx_encrypt_iv_stream(struct skcipher_request *req)
0094 {
0095 return crypto4xx_crypt(req, AES_IV_SIZE, false, false);
0096 }
0097
0098 int crypto4xx_decrypt_noiv_block(struct skcipher_request *req)
0099 {
0100 return crypto4xx_crypt(req, 0, true, true);
0101 }
0102
0103 int crypto4xx_decrypt_iv_stream(struct skcipher_request *req)
0104 {
0105 return crypto4xx_crypt(req, AES_IV_SIZE, true, false);
0106 }
0107
0108 int crypto4xx_encrypt_iv_block(struct skcipher_request *req)
0109 {
0110 return crypto4xx_crypt(req, AES_IV_SIZE, false, true);
0111 }
0112
0113 int crypto4xx_decrypt_iv_block(struct skcipher_request *req)
0114 {
0115 return crypto4xx_crypt(req, AES_IV_SIZE, true, true);
0116 }
0117
0118
0119
0120
0121 static int crypto4xx_setkey_aes(struct crypto_skcipher *cipher,
0122 const u8 *key,
0123 unsigned int keylen,
0124 unsigned char cm,
0125 u8 fb)
0126 {
0127 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
0128 struct dynamic_sa_ctl *sa;
0129 int rc;
0130
0131 if (keylen != AES_KEYSIZE_256 && keylen != AES_KEYSIZE_192 &&
0132 keylen != AES_KEYSIZE_128)
0133 return -EINVAL;
0134
0135
0136 if (ctx->sa_in || ctx->sa_out)
0137 crypto4xx_free_sa(ctx);
0138
0139 rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
0140 if (rc)
0141 return rc;
0142
0143
0144 sa = ctx->sa_in;
0145
0146 set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_ECB ?
0147 SA_NOT_SAVE_IV : SA_SAVE_IV),
0148 SA_NOT_LOAD_HASH, (cm == CRYPTO_MODE_ECB ?
0149 SA_LOAD_IV_FROM_SA : SA_LOAD_IV_FROM_STATE),
0150 SA_NO_HEADER_PROC, SA_HASH_ALG_NULL,
0151 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
0152 SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT,
0153 DIR_INBOUND);
0154
0155 set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH,
0156 fb, SA_EXTENDED_SN_OFF,
0157 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
0158 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
0159 SA_NOT_COPY_HDR);
0160 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
0161 key, keylen);
0162 sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2);
0163 sa->sa_command_1.bf.key_len = keylen >> 3;
0164
0165 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
0166 sa = ctx->sa_out;
0167 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
0168
0169
0170
0171
0172 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT;
0173
0174 return 0;
0175 }
0176
0177 int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
0178 const u8 *key, unsigned int keylen)
0179 {
0180 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CBC,
0181 CRYPTO_FEEDBACK_MODE_NO_FB);
0182 }
0183
0184 int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher,
0185 const u8 *key, unsigned int keylen)
0186 {
0187 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_CFB,
0188 CRYPTO_FEEDBACK_MODE_128BIT_CFB);
0189 }
0190
0191 int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
0192 const u8 *key, unsigned int keylen)
0193 {
0194 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_ECB,
0195 CRYPTO_FEEDBACK_MODE_NO_FB);
0196 }
0197
0198 int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher,
0199 const u8 *key, unsigned int keylen)
0200 {
0201 return crypto4xx_setkey_aes(cipher, key, keylen, CRYPTO_MODE_OFB,
0202 CRYPTO_FEEDBACK_MODE_64BIT_OFB);
0203 }
0204
0205 int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
0206 const u8 *key, unsigned int keylen)
0207 {
0208 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
0209 int rc;
0210
0211 rc = crypto4xx_setkey_aes(cipher, key, keylen - CTR_RFC3686_NONCE_SIZE,
0212 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
0213 if (rc)
0214 return rc;
0215
0216 ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
0217 CTR_RFC3686_NONCE_SIZE]);
0218
0219 return 0;
0220 }
0221
0222 int crypto4xx_rfc3686_encrypt(struct skcipher_request *req)
0223 {
0224 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
0225 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
0226 __le32 iv[AES_IV_SIZE / 4] = {
0227 ctx->iv_nonce,
0228 cpu_to_le32p((u32 *) req->iv),
0229 cpu_to_le32p((u32 *) (req->iv + 4)),
0230 cpu_to_le32(1) };
0231
0232 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
0233 req->cryptlen, iv, AES_IV_SIZE,
0234 ctx->sa_out, ctx->sa_len, 0, NULL);
0235 }
0236
0237 int crypto4xx_rfc3686_decrypt(struct skcipher_request *req)
0238 {
0239 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
0240 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
0241 __le32 iv[AES_IV_SIZE / 4] = {
0242 ctx->iv_nonce,
0243 cpu_to_le32p((u32 *) req->iv),
0244 cpu_to_le32p((u32 *) (req->iv + 4)),
0245 cpu_to_le32(1) };
0246
0247 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
0248 req->cryptlen, iv, AES_IV_SIZE,
0249 ctx->sa_out, ctx->sa_len, 0, NULL);
0250 }
0251
0252 static int
0253 crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt)
0254 {
0255 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
0256 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
0257 size_t iv_len = crypto_skcipher_ivsize(cipher);
0258 unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4));
0259 unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) /
0260 AES_BLOCK_SIZE;
0261
0262
0263
0264
0265
0266
0267
0268 if (counter + nblks < counter) {
0269 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher.cipher);
0270 int ret;
0271
0272 skcipher_request_set_sync_tfm(subreq, ctx->sw_cipher.cipher);
0273 skcipher_request_set_callback(subreq, req->base.flags,
0274 NULL, NULL);
0275 skcipher_request_set_crypt(subreq, req->src, req->dst,
0276 req->cryptlen, req->iv);
0277 ret = encrypt ? crypto_skcipher_encrypt(subreq)
0278 : crypto_skcipher_decrypt(subreq);
0279 skcipher_request_zero(subreq);
0280 return ret;
0281 }
0282
0283 return encrypt ? crypto4xx_encrypt_iv_stream(req)
0284 : crypto4xx_decrypt_iv_stream(req);
0285 }
0286
0287 static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx,
0288 struct crypto_skcipher *cipher,
0289 const u8 *key,
0290 unsigned int keylen)
0291 {
0292 crypto_sync_skcipher_clear_flags(ctx->sw_cipher.cipher,
0293 CRYPTO_TFM_REQ_MASK);
0294 crypto_sync_skcipher_set_flags(ctx->sw_cipher.cipher,
0295 crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
0296 return crypto_sync_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen);
0297 }
0298
0299 int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
0300 const u8 *key, unsigned int keylen)
0301 {
0302 struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher);
0303 int rc;
0304
0305 rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen);
0306 if (rc)
0307 return rc;
0308
0309 return crypto4xx_setkey_aes(cipher, key, keylen,
0310 CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB);
0311 }
0312
0313 int crypto4xx_encrypt_ctr(struct skcipher_request *req)
0314 {
0315 return crypto4xx_ctr_crypt(req, true);
0316 }
0317
0318 int crypto4xx_decrypt_ctr(struct skcipher_request *req)
0319 {
0320 return crypto4xx_ctr_crypt(req, false);
0321 }
0322
0323 static inline bool crypto4xx_aead_need_fallback(struct aead_request *req,
0324 unsigned int len,
0325 bool is_ccm, bool decrypt)
0326 {
0327 struct crypto_aead *aead = crypto_aead_reqtfm(req);
0328
0329
0330 if (aead->authsize & 3)
0331 return true;
0332
0333
0334
0335
0336
0337 if (len < AES_BLOCK_SIZE)
0338 return true;
0339
0340
0341 if (req->assoclen & 0x3 || req->assoclen > 1020)
0342 return true;
0343
0344
0345 if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3))
0346 return true;
0347
0348 return false;
0349 }
0350
0351 static int crypto4xx_aead_fallback(struct aead_request *req,
0352 struct crypto4xx_ctx *ctx, bool do_decrypt)
0353 {
0354 struct aead_request *subreq = aead_request_ctx(req);
0355
0356 aead_request_set_tfm(subreq, ctx->sw_cipher.aead);
0357 aead_request_set_callback(subreq, req->base.flags,
0358 req->base.complete, req->base.data);
0359 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
0360 req->iv);
0361 aead_request_set_ad(subreq, req->assoclen);
0362 return do_decrypt ? crypto_aead_decrypt(subreq) :
0363 crypto_aead_encrypt(subreq);
0364 }
0365
0366 static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx,
0367 struct crypto_aead *cipher,
0368 const u8 *key,
0369 unsigned int keylen)
0370 {
0371 crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK);
0372 crypto_aead_set_flags(ctx->sw_cipher.aead,
0373 crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK);
0374 return crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen);
0375 }
0376
0377
0378
0379
0380
0381 int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key,
0382 unsigned int keylen)
0383 {
0384 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
0385 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
0386 struct dynamic_sa_ctl *sa;
0387 int rc = 0;
0388
0389 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
0390 if (rc)
0391 return rc;
0392
0393 if (ctx->sa_in || ctx->sa_out)
0394 crypto4xx_free_sa(ctx);
0395
0396 rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4);
0397 if (rc)
0398 return rc;
0399
0400
0401 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
0402 sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2);
0403
0404 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
0405 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
0406 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
0407 SA_CIPHER_ALG_AES,
0408 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
0409 SA_OPCODE_HASH_DECRYPT, DIR_INBOUND);
0410
0411 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
0412 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
0413 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
0414 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
0415 SA_NOT_COPY_HDR);
0416
0417 sa->sa_command_1.bf.key_len = keylen >> 3;
0418
0419 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen);
0420
0421 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
0422 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
0423
0424 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
0425 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
0426 SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC,
0427 SA_CIPHER_ALG_AES,
0428 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
0429 SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND);
0430
0431 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
0432 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
0433 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
0434 SA_COPY_PAD, SA_COPY_PAYLOAD,
0435 SA_NOT_COPY_HDR);
0436
0437 sa->sa_command_1.bf.key_len = keylen >> 3;
0438 return 0;
0439 }
0440
0441 static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt)
0442 {
0443 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
0444 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
0445 struct crypto_aead *aead = crypto_aead_reqtfm(req);
0446 __le32 iv[16];
0447 u32 tmp_sa[SA_AES128_CCM_LEN + 4];
0448 struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa;
0449 unsigned int len = req->cryptlen;
0450
0451 if (decrypt)
0452 len -= crypto_aead_authsize(aead);
0453
0454 if (crypto4xx_aead_need_fallback(req, len, true, decrypt))
0455 return crypto4xx_aead_fallback(req, ctx, decrypt);
0456
0457 memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4);
0458 sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2;
0459
0460 if (req->iv[0] == 1) {
0461
0462 sa->sa_command_1.bf.crypto_mode9_8 = 1;
0463 }
0464
0465 iv[3] = cpu_to_le32(0);
0466 crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1));
0467
0468 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
0469 len, iv, sizeof(iv),
0470 sa, ctx->sa_len, req->assoclen, rctx->dst);
0471 }
0472
0473 int crypto4xx_encrypt_aes_ccm(struct aead_request *req)
0474 {
0475 return crypto4xx_crypt_aes_ccm(req, false);
0476 }
0477
0478 int crypto4xx_decrypt_aes_ccm(struct aead_request *req)
0479 {
0480 return crypto4xx_crypt_aes_ccm(req, true);
0481 }
0482
0483 int crypto4xx_setauthsize_aead(struct crypto_aead *cipher,
0484 unsigned int authsize)
0485 {
0486 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
0487 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
0488
0489 return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize);
0490 }
0491
0492
0493
0494
0495
0496 static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen)
0497 {
0498 switch (keylen) {
0499 case 16:
0500 case 24:
0501 case 32:
0502 return 0;
0503 default:
0504 return -EINVAL;
0505 }
0506 }
0507
0508 static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key,
0509 unsigned int keylen)
0510 {
0511 struct crypto_aes_ctx ctx;
0512 uint8_t src[16] = { 0 };
0513 int rc;
0514
0515 rc = aes_expandkey(&ctx, key, keylen);
0516 if (rc) {
0517 pr_err("aes_expandkey() failed: %d\n", rc);
0518 return rc;
0519 }
0520
0521 aes_encrypt(&ctx, src, src);
0522 crypto4xx_memcpy_to_le32(hash_start, src, 16);
0523 memzero_explicit(&ctx, sizeof(ctx));
0524 return 0;
0525 }
0526
0527 int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
0528 const u8 *key, unsigned int keylen)
0529 {
0530 struct crypto_tfm *tfm = crypto_aead_tfm(cipher);
0531 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
0532 struct dynamic_sa_ctl *sa;
0533 int rc = 0;
0534
0535 if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0)
0536 return -EINVAL;
0537
0538 rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen);
0539 if (rc)
0540 return rc;
0541
0542 if (ctx->sa_in || ctx->sa_out)
0543 crypto4xx_free_sa(ctx);
0544
0545 rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4);
0546 if (rc)
0547 return rc;
0548
0549 sa = (struct dynamic_sa_ctl *) ctx->sa_in;
0550
0551 sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2);
0552 set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV,
0553 SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE,
0554 SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH,
0555 SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO,
0556 SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT,
0557 DIR_INBOUND);
0558 set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH,
0559 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
0560 SA_SEQ_MASK_ON, SA_MC_DISABLE,
0561 SA_NOT_COPY_PAD, SA_COPY_PAYLOAD,
0562 SA_NOT_COPY_HDR);
0563
0564 sa->sa_command_1.bf.key_len = keylen >> 3;
0565
0566 crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa),
0567 key, keylen);
0568
0569 rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa),
0570 key, keylen);
0571 if (rc) {
0572 pr_err("GCM hash key setting failed = %d\n", rc);
0573 goto err;
0574 }
0575
0576 memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4);
0577 sa = (struct dynamic_sa_ctl *) ctx->sa_out;
0578 sa->sa_command_0.bf.dir = DIR_OUTBOUND;
0579 sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH;
0580
0581 return 0;
0582 err:
0583 crypto4xx_free_sa(ctx);
0584 return rc;
0585 }
0586
0587 static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req,
0588 bool decrypt)
0589 {
0590 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
0591 struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req);
0592 __le32 iv[4];
0593 unsigned int len = req->cryptlen;
0594
0595 if (decrypt)
0596 len -= crypto_aead_authsize(crypto_aead_reqtfm(req));
0597
0598 if (crypto4xx_aead_need_fallback(req, len, false, decrypt))
0599 return crypto4xx_aead_fallback(req, ctx, decrypt);
0600
0601 crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE);
0602 iv[3] = cpu_to_le32(1);
0603
0604 return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
0605 len, iv, sizeof(iv),
0606 decrypt ? ctx->sa_in : ctx->sa_out,
0607 ctx->sa_len, req->assoclen, rctx->dst);
0608 }
0609
0610 int crypto4xx_encrypt_aes_gcm(struct aead_request *req)
0611 {
0612 return crypto4xx_crypt_aes_gcm(req, false);
0613 }
0614
0615 int crypto4xx_decrypt_aes_gcm(struct aead_request *req)
0616 {
0617 return crypto4xx_crypt_aes_gcm(req, true);
0618 }
0619
0620
0621
0622
0623 static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
0624 unsigned int sa_len,
0625 unsigned char ha,
0626 unsigned char hm)
0627 {
0628 struct crypto_alg *alg = tfm->__crt_alg;
0629 struct crypto4xx_alg *my_alg;
0630 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
0631 struct dynamic_sa_hash160 *sa;
0632 int rc;
0633
0634 my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg,
0635 alg.u.hash);
0636 ctx->dev = my_alg->dev;
0637
0638
0639 if (ctx->sa_in || ctx->sa_out)
0640 crypto4xx_free_sa(ctx);
0641
0642 rc = crypto4xx_alloc_sa(ctx, sa_len);
0643 if (rc)
0644 return rc;
0645
0646 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
0647 sizeof(struct crypto4xx_ctx));
0648 sa = (struct dynamic_sa_hash160 *)ctx->sa_in;
0649 set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV,
0650 SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA,
0651 SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL,
0652 SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC,
0653 SA_OPCODE_HASH, DIR_INBOUND);
0654 set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH,
0655 CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF,
0656 SA_SEQ_MASK_OFF, SA_MC_ENABLE,
0657 SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD,
0658 SA_NOT_COPY_HDR);
0659
0660 memset(sa->inner_digest, 0, sizeof(sa->inner_digest));
0661 memset(sa->outer_digest, 0, sizeof(sa->outer_digest));
0662
0663 return 0;
0664 }
0665
0666 int crypto4xx_hash_init(struct ahash_request *req)
0667 {
0668 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
0669 int ds;
0670 struct dynamic_sa_ctl *sa;
0671
0672 sa = ctx->sa_in;
0673 ds = crypto_ahash_digestsize(
0674 __crypto_ahash_cast(req->base.tfm));
0675 sa->sa_command_0.bf.digest_len = ds >> 2;
0676 sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA;
0677
0678 return 0;
0679 }
0680
0681 int crypto4xx_hash_update(struct ahash_request *req)
0682 {
0683 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
0684 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
0685 struct scatterlist dst;
0686 unsigned int ds = crypto_ahash_digestsize(ahash);
0687
0688 sg_init_one(&dst, req->result, ds);
0689
0690 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
0691 req->nbytes, NULL, 0, ctx->sa_in,
0692 ctx->sa_len, 0, NULL);
0693 }
0694
0695 int crypto4xx_hash_final(struct ahash_request *req)
0696 {
0697 return 0;
0698 }
0699
0700 int crypto4xx_hash_digest(struct ahash_request *req)
0701 {
0702 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
0703 struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
0704 struct scatterlist dst;
0705 unsigned int ds = crypto_ahash_digestsize(ahash);
0706
0707 sg_init_one(&dst, req->result, ds);
0708
0709 return crypto4xx_build_pd(&req->base, ctx, req->src, &dst,
0710 req->nbytes, NULL, 0, ctx->sa_in,
0711 ctx->sa_len, 0, NULL);
0712 }
0713
0714
0715
0716
0717 int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm)
0718 {
0719 return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1,
0720 SA_HASH_MODE_HASH);
0721 }