James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 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> |
Christian Lamparter | a0aae82 | 2017-10-04 01:00:15 +0200 | [diff] [blame] | 29 | #include <crypto/aead.h> |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 30 | #include <crypto/aes.h> |
Christian Lamparter | 5923136 | 2017-10-04 01:00:17 +0200 | [diff] [blame] | 31 | #include <crypto/gcm.h> |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 32 | #include <crypto/sha.h> |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 33 | #include <crypto/ctr.h> |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 34 | #include <crypto/skcipher.h> |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 35 | #include "crypto4xx_reg_def.h" |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 36 | #include "crypto4xx_core.h" |
Christian Lamparter | 249c8d9 | 2017-08-25 15:47:20 +0200 | [diff] [blame] | 37 | #include "crypto4xx_sa.h" |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 38 | |
Jingoo Han | 3a4eac7 | 2013-08-09 16:59:57 +0900 | [diff] [blame] | 39 | static 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 Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 43 | { |
| 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 Han | 3a4eac7 | 2013-08-09 16:59:57 +0900 | [diff] [blame] | 59 | static 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 Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 63 | { |
| 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 Lamparter | 5a4326d3 | 2017-10-04 01:00:05 +0200 | [diff] [blame] | 69 | sa->sa_command_1.bf.hmac_muting = hmac_mc; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 70 | 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 Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 78 | static inline int crypto4xx_crypt(struct skcipher_request *req, |
Christian Lamparter | a8d79d7 | 2018-04-19 18:41:51 +0200 | [diff] [blame] | 79 | const unsigned int ivlen, bool decrypt) |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 80 | { |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 81 | struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req); |
| 82 | struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher); |
Christian Lamparter | c4e9065 | 2018-04-19 18:41:53 +0200 | [diff] [blame] | 83 | __le32 iv[AES_IV_SIZE]; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 84 | |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 85 | if (ivlen) |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 86 | crypto4xx_memcpy_to_le32(iv, req->iv, ivlen); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 87 | |
| 88 | return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst, |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 89 | req->cryptlen, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out, |
Christian Lamparter | a8d79d7 | 2018-04-19 18:41:51 +0200 | [diff] [blame] | 90 | ctx->sa_len, 0); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 91 | } |
| 92 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 93 | int crypto4xx_encrypt_noiv(struct skcipher_request *req) |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 94 | { |
Christian Lamparter | a8d79d7 | 2018-04-19 18:41:51 +0200 | [diff] [blame] | 95 | return crypto4xx_crypt(req, 0, false); |
| 96 | } |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 97 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 98 | int crypto4xx_encrypt_iv(struct skcipher_request *req) |
Christian Lamparter | a8d79d7 | 2018-04-19 18:41:51 +0200 | [diff] [blame] | 99 | { |
| 100 | return crypto4xx_crypt(req, AES_IV_SIZE, false); |
| 101 | } |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 102 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 103 | int crypto4xx_decrypt_noiv(struct skcipher_request *req) |
Christian Lamparter | a8d79d7 | 2018-04-19 18:41:51 +0200 | [diff] [blame] | 104 | { |
| 105 | return crypto4xx_crypt(req, 0, true); |
| 106 | } |
| 107 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 108 | int crypto4xx_decrypt_iv(struct skcipher_request *req) |
Christian Lamparter | a8d79d7 | 2018-04-19 18:41:51 +0200 | [diff] [blame] | 109 | { |
| 110 | return crypto4xx_crypt(req, AES_IV_SIZE, true); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * AES Functions |
| 115 | */ |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 116 | static int crypto4xx_setkey_aes(struct crypto_skcipher *cipher, |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 117 | const u8 *key, |
| 118 | unsigned int keylen, |
| 119 | unsigned char cm, |
| 120 | u8 fb) |
| 121 | { |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 122 | struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 123 | 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 Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 128 | crypto_skcipher_set_flags(cipher, |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 129 | CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
| 133 | /* Create SA */ |
Christian Lamparter | 2f77690 | 2017-10-04 01:00:14 +0200 | [diff] [blame] | 134 | if (ctx->sa_in || ctx->sa_out) |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 135 | 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 Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 141 | /* Setup SA */ |
Christian Lamparter | 9e0a0b3 | 2017-08-25 15:47:25 +0200 | [diff] [blame] | 142 | sa = ctx->sa_in; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 143 | |
Christian Lamparter | fc34011 | 2018-04-19 18:41:55 +0200 | [diff] [blame] | 144 | set_dynamic_sa_command_0(sa, SA_NOT_SAVE_HASH, (cm == CRYPTO_MODE_CBC ? |
| 145 | SA_SAVE_IV : SA_NOT_SAVE_IV), |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 146 | SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE, |
| 147 | SA_NO_HEADER_PROC, SA_HASH_ALG_NULL, |
| 148 | SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO, |
| 149 | SA_OP_GROUP_BASIC, SA_OPCODE_DECRYPT, |
| 150 | DIR_INBOUND); |
| 151 | |
| 152 | set_dynamic_sa_command_1(sa, cm, SA_HASH_MODE_HASH, |
| 153 | fb, SA_EXTENDED_SN_OFF, |
| 154 | SA_SEQ_MASK_OFF, SA_MC_ENABLE, |
| 155 | SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD, |
| 156 | SA_NOT_COPY_HDR); |
Christian Lamparter | 4865b122 | 2017-10-04 01:00:10 +0200 | [diff] [blame] | 157 | crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), |
| 158 | key, keylen); |
Christian Lamparter | 453e309 | 2017-08-25 15:47:19 +0200 | [diff] [blame] | 159 | sa->sa_contents.w = SA_AES_CONTENTS | (keylen << 2); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 160 | sa->sa_command_1.bf.key_len = keylen >> 3; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 161 | |
| 162 | memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4); |
Christian Lamparter | 9e0a0b3 | 2017-08-25 15:47:25 +0200 | [diff] [blame] | 163 | sa = ctx->sa_out; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 164 | sa->sa_command_0.bf.dir = DIR_OUTBOUND; |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 169 | int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher, |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 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 Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 176 | int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher, |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 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 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 183 | int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher, |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 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 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 190 | int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher, |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 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 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 197 | int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher, |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 198 | const u8 *key, unsigned int keylen) |
| 199 | { |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 200 | struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher); |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 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 | |
Christian Lamparter | 2f77690 | 2017-10-04 01:00:14 +0200 | [diff] [blame] | 208 | ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen - |
| 209 | CTR_RFC3686_NONCE_SIZE]); |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 214 | int crypto4xx_rfc3686_encrypt(struct skcipher_request *req) |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 215 | { |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 216 | struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req); |
| 217 | struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher); |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 218 | __le32 iv[AES_IV_SIZE / 4] = { |
Christian Lamparter | 2f77690 | 2017-10-04 01:00:14 +0200 | [diff] [blame] | 219 | ctx->iv_nonce, |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 220 | cpu_to_le32p((u32 *) req->iv), |
| 221 | cpu_to_le32p((u32 *) (req->iv + 4)), |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 222 | cpu_to_le32(1) }; |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 223 | |
| 224 | return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst, |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 225 | req->cryptlen, iv, AES_IV_SIZE, |
Christian Lamparter | a0aae82 | 2017-10-04 01:00:15 +0200 | [diff] [blame] | 226 | ctx->sa_out, ctx->sa_len, 0); |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 229 | int crypto4xx_rfc3686_decrypt(struct skcipher_request *req) |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 230 | { |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 231 | struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req); |
| 232 | struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher); |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 233 | __le32 iv[AES_IV_SIZE / 4] = { |
Christian Lamparter | 2f77690 | 2017-10-04 01:00:14 +0200 | [diff] [blame] | 234 | ctx->iv_nonce, |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 235 | cpu_to_le32p((u32 *) req->iv), |
| 236 | cpu_to_le32p((u32 *) (req->iv + 4)), |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 237 | cpu_to_le32(1) }; |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 238 | |
| 239 | return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst, |
Christian Lamparter | ce05ffe | 2018-04-19 18:41:52 +0200 | [diff] [blame] | 240 | req->cryptlen, iv, AES_IV_SIZE, |
Christian Lamparter | a0aae82 | 2017-10-04 01:00:15 +0200 | [diff] [blame] | 241 | ctx->sa_out, ctx->sa_len, 0); |
Christian Lamparter | f2a13e7 | 2017-08-25 15:47:21 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Christian Lamparter | 98e87e3 | 2018-04-19 18:41:54 +0200 | [diff] [blame] | 244 | static int |
| 245 | crypto4xx_ctr_crypt(struct skcipher_request *req, bool encrypt) |
| 246 | { |
| 247 | struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req); |
| 248 | struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher); |
| 249 | size_t iv_len = crypto_skcipher_ivsize(cipher); |
| 250 | unsigned int counter = be32_to_cpup((__be32 *)(req->iv + iv_len - 4)); |
| 251 | unsigned int nblks = ALIGN(req->cryptlen, AES_BLOCK_SIZE) / |
| 252 | AES_BLOCK_SIZE; |
| 253 | |
| 254 | /* |
| 255 | * The hardware uses only the last 32-bits as the counter while the |
| 256 | * kernel tests (aes_ctr_enc_tv_template[4] for example) expect that |
| 257 | * the whole IV is a counter. So fallback if the counter is going to |
| 258 | * overlow. |
| 259 | */ |
| 260 | if (counter + nblks < counter) { |
| 261 | struct skcipher_request *subreq = skcipher_request_ctx(req); |
| 262 | int ret; |
| 263 | |
| 264 | skcipher_request_set_tfm(subreq, ctx->sw_cipher.cipher); |
| 265 | skcipher_request_set_callback(subreq, req->base.flags, |
| 266 | NULL, NULL); |
| 267 | skcipher_request_set_crypt(subreq, req->src, req->dst, |
| 268 | req->cryptlen, req->iv); |
| 269 | ret = encrypt ? crypto_skcipher_encrypt(subreq) |
| 270 | : crypto_skcipher_decrypt(subreq); |
| 271 | skcipher_request_zero(subreq); |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | return encrypt ? crypto4xx_encrypt_iv(req) |
| 276 | : crypto4xx_decrypt_iv(req); |
| 277 | } |
| 278 | |
| 279 | static int crypto4xx_sk_setup_fallback(struct crypto4xx_ctx *ctx, |
| 280 | struct crypto_skcipher *cipher, |
| 281 | const u8 *key, |
| 282 | unsigned int keylen) |
| 283 | { |
| 284 | int rc; |
| 285 | |
| 286 | crypto_skcipher_clear_flags(ctx->sw_cipher.cipher, |
| 287 | CRYPTO_TFM_REQ_MASK); |
| 288 | crypto_skcipher_set_flags(ctx->sw_cipher.cipher, |
| 289 | crypto_skcipher_get_flags(cipher) & CRYPTO_TFM_REQ_MASK); |
| 290 | rc = crypto_skcipher_setkey(ctx->sw_cipher.cipher, key, keylen); |
| 291 | crypto_skcipher_clear_flags(cipher, CRYPTO_TFM_RES_MASK); |
| 292 | crypto_skcipher_set_flags(cipher, |
| 293 | crypto_skcipher_get_flags(ctx->sw_cipher.cipher) & |
| 294 | CRYPTO_TFM_RES_MASK); |
| 295 | |
| 296 | return rc; |
| 297 | } |
| 298 | |
| 299 | int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher, |
| 300 | const u8 *key, unsigned int keylen) |
| 301 | { |
| 302 | struct crypto4xx_ctx *ctx = crypto_skcipher_ctx(cipher); |
| 303 | int rc; |
| 304 | |
| 305 | rc = crypto4xx_sk_setup_fallback(ctx, cipher, key, keylen); |
| 306 | if (rc) |
| 307 | return rc; |
| 308 | |
| 309 | return crypto4xx_setkey_aes(cipher, key, keylen, |
| 310 | CRYPTO_MODE_CTR, CRYPTO_FEEDBACK_MODE_NO_FB); |
| 311 | } |
| 312 | |
| 313 | int crypto4xx_encrypt_ctr(struct skcipher_request *req) |
| 314 | { |
| 315 | return crypto4xx_ctr_crypt(req, true); |
| 316 | } |
| 317 | |
| 318 | int crypto4xx_decrypt_ctr(struct skcipher_request *req) |
| 319 | { |
| 320 | return crypto4xx_ctr_crypt(req, false); |
| 321 | } |
| 322 | |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 323 | static inline bool crypto4xx_aead_need_fallback(struct aead_request *req, |
Christian Lamparter | 584201f | 2018-04-19 18:41:56 +0200 | [diff] [blame^] | 324 | unsigned int len, |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 325 | bool is_ccm, bool decrypt) |
| 326 | { |
| 327 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
| 328 | |
| 329 | /* authsize has to be a multiple of 4 */ |
| 330 | if (aead->authsize & 3) |
| 331 | return true; |
| 332 | |
| 333 | /* |
Christian Lamparter | 584201f | 2018-04-19 18:41:56 +0200 | [diff] [blame^] | 334 | * hardware does not handle cases where plaintext |
| 335 | * is less than a block. |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 336 | */ |
Christian Lamparter | 584201f | 2018-04-19 18:41:56 +0200 | [diff] [blame^] | 337 | if (len < AES_BLOCK_SIZE) |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 338 | return true; |
| 339 | |
Christian Lamparter | 584201f | 2018-04-19 18:41:56 +0200 | [diff] [blame^] | 340 | /* assoc len needs to be a multiple of 4 and <= 1020 */ |
| 341 | if (req->assoclen & 0x3 || req->assoclen > 1020) |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 342 | return true; |
| 343 | |
| 344 | /* CCM supports only counter field length of 2 and 4 bytes */ |
| 345 | if (is_ccm && !(req->iv[0] == 1 || req->iv[0] == 3)) |
| 346 | return true; |
| 347 | |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | |
| 351 | static int crypto4xx_aead_fallback(struct aead_request *req, |
| 352 | struct crypto4xx_ctx *ctx, bool do_decrypt) |
| 353 | { |
Christian Lamparter | c4e9065 | 2018-04-19 18:41:53 +0200 | [diff] [blame] | 354 | struct aead_request *subreq = aead_request_ctx(req); |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 355 | |
| 356 | aead_request_set_tfm(subreq, ctx->sw_cipher.aead); |
| 357 | aead_request_set_callback(subreq, req->base.flags, |
| 358 | req->base.complete, req->base.data); |
| 359 | aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, |
| 360 | req->iv); |
| 361 | aead_request_set_ad(subreq, req->assoclen); |
| 362 | return do_decrypt ? crypto_aead_decrypt(subreq) : |
| 363 | crypto_aead_encrypt(subreq); |
| 364 | } |
| 365 | |
Christian Lamparter | 98e87e3 | 2018-04-19 18:41:54 +0200 | [diff] [blame] | 366 | static int crypto4xx_aead_setup_fallback(struct crypto4xx_ctx *ctx, |
| 367 | struct crypto_aead *cipher, |
| 368 | const u8 *key, |
| 369 | unsigned int keylen) |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 370 | { |
| 371 | int rc; |
| 372 | |
| 373 | crypto_aead_clear_flags(ctx->sw_cipher.aead, CRYPTO_TFM_REQ_MASK); |
| 374 | crypto_aead_set_flags(ctx->sw_cipher.aead, |
| 375 | crypto_aead_get_flags(cipher) & CRYPTO_TFM_REQ_MASK); |
| 376 | rc = crypto_aead_setkey(ctx->sw_cipher.aead, key, keylen); |
| 377 | crypto_aead_clear_flags(cipher, CRYPTO_TFM_RES_MASK); |
| 378 | crypto_aead_set_flags(cipher, |
| 379 | crypto_aead_get_flags(ctx->sw_cipher.aead) & |
| 380 | CRYPTO_TFM_RES_MASK); |
| 381 | |
| 382 | return rc; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * AES-CCM Functions |
| 387 | */ |
| 388 | |
| 389 | int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher, const u8 *key, |
| 390 | unsigned int keylen) |
| 391 | { |
| 392 | struct crypto_tfm *tfm = crypto_aead_tfm(cipher); |
| 393 | struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm); |
| 394 | struct dynamic_sa_ctl *sa; |
| 395 | int rc = 0; |
| 396 | |
Christian Lamparter | 98e87e3 | 2018-04-19 18:41:54 +0200 | [diff] [blame] | 397 | rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen); |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 398 | if (rc) |
| 399 | return rc; |
| 400 | |
| 401 | if (ctx->sa_in || ctx->sa_out) |
| 402 | crypto4xx_free_sa(ctx); |
| 403 | |
| 404 | rc = crypto4xx_alloc_sa(ctx, SA_AES128_CCM_LEN + (keylen - 16) / 4); |
| 405 | if (rc) |
| 406 | return rc; |
| 407 | |
| 408 | /* Setup SA */ |
| 409 | sa = (struct dynamic_sa_ctl *) ctx->sa_in; |
| 410 | sa->sa_contents.w = SA_AES_CCM_CONTENTS | (keylen << 2); |
| 411 | |
Christian Lamparter | 0b5a7f7 | 2017-12-23 19:45:46 +0100 | [diff] [blame] | 412 | set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV, |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 413 | SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE, |
| 414 | SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC, |
| 415 | SA_CIPHER_ALG_AES, |
| 416 | SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC, |
| 417 | SA_OPCODE_HASH_DECRYPT, DIR_INBOUND); |
| 418 | |
| 419 | set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH, |
| 420 | CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF, |
| 421 | SA_SEQ_MASK_OFF, SA_MC_ENABLE, |
| 422 | SA_NOT_COPY_PAD, SA_COPY_PAYLOAD, |
| 423 | SA_NOT_COPY_HDR); |
| 424 | |
| 425 | sa->sa_command_1.bf.key_len = keylen >> 3; |
| 426 | |
| 427 | crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), key, keylen); |
| 428 | |
| 429 | memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4); |
| 430 | sa = (struct dynamic_sa_ctl *) ctx->sa_out; |
| 431 | |
| 432 | set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV, |
| 433 | SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE, |
| 434 | SA_NO_HEADER_PROC, SA_HASH_ALG_CBC_MAC, |
| 435 | SA_CIPHER_ALG_AES, |
| 436 | SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC, |
| 437 | SA_OPCODE_ENCRYPT_HASH, DIR_OUTBOUND); |
| 438 | |
| 439 | set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH, |
| 440 | CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF, |
| 441 | SA_SEQ_MASK_OFF, SA_MC_ENABLE, |
| 442 | SA_COPY_PAD, SA_COPY_PAYLOAD, |
| 443 | SA_NOT_COPY_HDR); |
| 444 | |
| 445 | sa->sa_command_1.bf.key_len = keylen >> 3; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int crypto4xx_crypt_aes_ccm(struct aead_request *req, bool decrypt) |
| 450 | { |
| 451 | struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 452 | struct crypto_aead *aead = crypto_aead_reqtfm(req); |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 453 | __le32 iv[16]; |
Christian Lamparter | c4e9065 | 2018-04-19 18:41:53 +0200 | [diff] [blame] | 454 | u32 tmp_sa[SA_AES128_CCM_LEN + 4]; |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 455 | struct dynamic_sa_ctl *sa = (struct dynamic_sa_ctl *)tmp_sa; |
Christian Lamparter | 584201f | 2018-04-19 18:41:56 +0200 | [diff] [blame^] | 456 | unsigned int len = req->cryptlen; |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 457 | |
| 458 | if (decrypt) |
| 459 | len -= crypto_aead_authsize(aead); |
| 460 | |
Christian Lamparter | 584201f | 2018-04-19 18:41:56 +0200 | [diff] [blame^] | 461 | if (crypto4xx_aead_need_fallback(req, len, true, decrypt)) |
| 462 | return crypto4xx_aead_fallback(req, ctx, decrypt); |
| 463 | |
Christian Lamparter | c4e9065 | 2018-04-19 18:41:53 +0200 | [diff] [blame] | 464 | memcpy(tmp_sa, decrypt ? ctx->sa_in : ctx->sa_out, ctx->sa_len * 4); |
Christian Lamparter | 65ea8b6 | 2017-10-04 01:00:16 +0200 | [diff] [blame] | 465 | sa->sa_command_0.bf.digest_len = crypto_aead_authsize(aead) >> 2; |
| 466 | |
| 467 | if (req->iv[0] == 1) { |
| 468 | /* CRYPTO_MODE_AES_ICM */ |
| 469 | sa->sa_command_1.bf.crypto_mode9_8 = 1; |
| 470 | } |
| 471 | |
| 472 | iv[3] = cpu_to_le32(0); |
| 473 | crypto4xx_memcpy_to_le32(iv, req->iv, 16 - (req->iv[0] + 1)); |
| 474 | |
| 475 | return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst, |
| 476 | len, iv, sizeof(iv), |
| 477 | sa, ctx->sa_len, req->assoclen); |
| 478 | } |
| 479 | |
| 480 | int crypto4xx_encrypt_aes_ccm(struct aead_request *req) |
| 481 | { |
| 482 | return crypto4xx_crypt_aes_ccm(req, false); |
| 483 | } |
| 484 | |
| 485 | int crypto4xx_decrypt_aes_ccm(struct aead_request *req) |
| 486 | { |
| 487 | return crypto4xx_crypt_aes_ccm(req, true); |
| 488 | } |
| 489 | |
| 490 | int crypto4xx_setauthsize_aead(struct crypto_aead *cipher, |
| 491 | unsigned int authsize) |
| 492 | { |
| 493 | struct crypto_tfm *tfm = crypto_aead_tfm(cipher); |
| 494 | struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm); |
| 495 | |
| 496 | return crypto_aead_setauthsize(ctx->sw_cipher.aead, authsize); |
| 497 | } |
| 498 | |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 499 | /** |
Christian Lamparter | 5923136 | 2017-10-04 01:00:17 +0200 | [diff] [blame] | 500 | * AES-GCM Functions |
| 501 | */ |
| 502 | |
| 503 | static int crypto4xx_aes_gcm_validate_keylen(unsigned int keylen) |
| 504 | { |
| 505 | switch (keylen) { |
| 506 | case 16: |
| 507 | case 24: |
| 508 | case 32: |
| 509 | return 0; |
| 510 | default: |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | static int crypto4xx_compute_gcm_hash_key_sw(__le32 *hash_start, const u8 *key, |
| 516 | unsigned int keylen) |
| 517 | { |
| 518 | struct crypto_cipher *aes_tfm = NULL; |
| 519 | uint8_t src[16] = { 0 }; |
| 520 | int rc = 0; |
| 521 | |
| 522 | aes_tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC | |
| 523 | CRYPTO_ALG_NEED_FALLBACK); |
| 524 | if (IS_ERR(aes_tfm)) { |
| 525 | rc = PTR_ERR(aes_tfm); |
| 526 | pr_warn("could not load aes cipher driver: %d\n", rc); |
| 527 | return rc; |
| 528 | } |
| 529 | |
| 530 | rc = crypto_cipher_setkey(aes_tfm, key, keylen); |
| 531 | if (rc) { |
| 532 | pr_err("setkey() failed: %d\n", rc); |
| 533 | goto out; |
| 534 | } |
| 535 | |
| 536 | crypto_cipher_encrypt_one(aes_tfm, src, src); |
| 537 | crypto4xx_memcpy_to_le32(hash_start, src, 16); |
| 538 | out: |
| 539 | crypto_free_cipher(aes_tfm); |
| 540 | return rc; |
| 541 | } |
| 542 | |
| 543 | int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher, |
| 544 | const u8 *key, unsigned int keylen) |
| 545 | { |
| 546 | struct crypto_tfm *tfm = crypto_aead_tfm(cipher); |
| 547 | struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm); |
| 548 | struct dynamic_sa_ctl *sa; |
| 549 | int rc = 0; |
| 550 | |
| 551 | if (crypto4xx_aes_gcm_validate_keylen(keylen) != 0) { |
| 552 | crypto_aead_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 553 | return -EINVAL; |
| 554 | } |
| 555 | |
Christian Lamparter | 98e87e3 | 2018-04-19 18:41:54 +0200 | [diff] [blame] | 556 | rc = crypto4xx_aead_setup_fallback(ctx, cipher, key, keylen); |
Christian Lamparter | 5923136 | 2017-10-04 01:00:17 +0200 | [diff] [blame] | 557 | if (rc) |
| 558 | return rc; |
| 559 | |
| 560 | if (ctx->sa_in || ctx->sa_out) |
| 561 | crypto4xx_free_sa(ctx); |
| 562 | |
| 563 | rc = crypto4xx_alloc_sa(ctx, SA_AES128_GCM_LEN + (keylen - 16) / 4); |
| 564 | if (rc) |
| 565 | return rc; |
| 566 | |
| 567 | sa = (struct dynamic_sa_ctl *) ctx->sa_in; |
| 568 | |
| 569 | sa->sa_contents.w = SA_AES_GCM_CONTENTS | (keylen << 2); |
| 570 | set_dynamic_sa_command_0(sa, SA_SAVE_HASH, SA_NOT_SAVE_IV, |
| 571 | SA_LOAD_HASH_FROM_SA, SA_LOAD_IV_FROM_STATE, |
| 572 | SA_NO_HEADER_PROC, SA_HASH_ALG_GHASH, |
| 573 | SA_CIPHER_ALG_AES, SA_PAD_TYPE_ZERO, |
| 574 | SA_OP_GROUP_BASIC, SA_OPCODE_HASH_DECRYPT, |
| 575 | DIR_INBOUND); |
| 576 | set_dynamic_sa_command_1(sa, CRYPTO_MODE_CTR, SA_HASH_MODE_HASH, |
| 577 | CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF, |
| 578 | SA_SEQ_MASK_ON, SA_MC_DISABLE, |
| 579 | SA_NOT_COPY_PAD, SA_COPY_PAYLOAD, |
| 580 | SA_NOT_COPY_HDR); |
| 581 | |
| 582 | sa->sa_command_1.bf.key_len = keylen >> 3; |
| 583 | |
| 584 | crypto4xx_memcpy_to_le32(get_dynamic_sa_key_field(sa), |
| 585 | key, keylen); |
| 586 | |
| 587 | rc = crypto4xx_compute_gcm_hash_key_sw(get_dynamic_sa_inner_digest(sa), |
| 588 | key, keylen); |
| 589 | if (rc) { |
| 590 | pr_err("GCM hash key setting failed = %d\n", rc); |
| 591 | goto err; |
| 592 | } |
| 593 | |
| 594 | memcpy(ctx->sa_out, ctx->sa_in, ctx->sa_len * 4); |
| 595 | sa = (struct dynamic_sa_ctl *) ctx->sa_out; |
| 596 | sa->sa_command_0.bf.dir = DIR_OUTBOUND; |
| 597 | sa->sa_command_0.bf.opcode = SA_OPCODE_ENCRYPT_HASH; |
| 598 | |
| 599 | return 0; |
| 600 | err: |
| 601 | crypto4xx_free_sa(ctx); |
| 602 | return rc; |
| 603 | } |
| 604 | |
| 605 | static inline int crypto4xx_crypt_aes_gcm(struct aead_request *req, |
| 606 | bool decrypt) |
| 607 | { |
| 608 | struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Christian Lamparter | 584201f | 2018-04-19 18:41:56 +0200 | [diff] [blame^] | 609 | struct crypto4xx_aead_reqctx *rctx = aead_request_ctx(req); |
Christian Lamparter | 5923136 | 2017-10-04 01:00:17 +0200 | [diff] [blame] | 610 | __le32 iv[4]; |
Christian Lamparter | 584201f | 2018-04-19 18:41:56 +0200 | [diff] [blame^] | 611 | unsigned int len = req->cryptlen; |
Christian Lamparter | 5923136 | 2017-10-04 01:00:17 +0200 | [diff] [blame] | 612 | |
Christian Lamparter | 584201f | 2018-04-19 18:41:56 +0200 | [diff] [blame^] | 613 | if (decrypt) |
| 614 | len -= crypto_aead_authsize(crypto_aead_reqtfm(req)); |
| 615 | |
| 616 | if (crypto4xx_aead_need_fallback(req, len, false, decrypt)) |
Christian Lamparter | 5923136 | 2017-10-04 01:00:17 +0200 | [diff] [blame] | 617 | return crypto4xx_aead_fallback(req, ctx, decrypt); |
| 618 | |
| 619 | crypto4xx_memcpy_to_le32(iv, req->iv, GCM_AES_IV_SIZE); |
| 620 | iv[3] = cpu_to_le32(1); |
| 621 | |
Christian Lamparter | 5923136 | 2017-10-04 01:00:17 +0200 | [diff] [blame] | 622 | return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst, |
| 623 | len, iv, sizeof(iv), |
| 624 | decrypt ? ctx->sa_in : ctx->sa_out, |
| 625 | ctx->sa_len, req->assoclen); |
| 626 | } |
| 627 | |
| 628 | int crypto4xx_encrypt_aes_gcm(struct aead_request *req) |
| 629 | { |
| 630 | return crypto4xx_crypt_aes_gcm(req, false); |
| 631 | } |
| 632 | |
| 633 | int crypto4xx_decrypt_aes_gcm(struct aead_request *req) |
| 634 | { |
| 635 | return crypto4xx_crypt_aes_gcm(req, true); |
| 636 | } |
| 637 | |
| 638 | /** |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 639 | * HASH SHA1 Functions |
| 640 | */ |
| 641 | static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm, |
| 642 | unsigned int sa_len, |
| 643 | unsigned char ha, |
| 644 | unsigned char hm) |
| 645 | { |
| 646 | struct crypto_alg *alg = tfm->__crt_alg; |
Christian Lamparter | a0aae82 | 2017-10-04 01:00:15 +0200 | [diff] [blame] | 647 | struct crypto4xx_alg *my_alg; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 648 | struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm); |
Christian Lamparter | 9e0a0b3 | 2017-08-25 15:47:25 +0200 | [diff] [blame] | 649 | struct dynamic_sa_hash160 *sa; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 650 | int rc; |
| 651 | |
Christian Lamparter | a0aae82 | 2017-10-04 01:00:15 +0200 | [diff] [blame] | 652 | my_alg = container_of(__crypto_ahash_alg(alg), struct crypto4xx_alg, |
| 653 | alg.u.hash); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 654 | ctx->dev = my_alg->dev; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 655 | |
| 656 | /* Create SA */ |
Christian Lamparter | 2f77690 | 2017-10-04 01:00:14 +0200 | [diff] [blame] | 657 | if (ctx->sa_in || ctx->sa_out) |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 658 | crypto4xx_free_sa(ctx); |
| 659 | |
| 660 | rc = crypto4xx_alloc_sa(ctx, sa_len); |
| 661 | if (rc) |
| 662 | return rc; |
| 663 | |
Herbert Xu | 6b1679f | 2009-07-12 23:08:28 +0800 | [diff] [blame] | 664 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
| 665 | sizeof(struct crypto4xx_ctx)); |
Christian Lamparter | 9e0a0b3 | 2017-08-25 15:47:25 +0200 | [diff] [blame] | 666 | sa = (struct dynamic_sa_hash160 *)ctx->sa_in; |
| 667 | set_dynamic_sa_command_0(&sa->ctrl, SA_SAVE_HASH, SA_NOT_SAVE_IV, |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 668 | SA_NOT_LOAD_HASH, SA_LOAD_IV_FROM_SA, |
| 669 | SA_NO_HEADER_PROC, ha, SA_CIPHER_ALG_NULL, |
| 670 | SA_PAD_TYPE_ZERO, SA_OP_GROUP_BASIC, |
| 671 | SA_OPCODE_HASH, DIR_INBOUND); |
Christian Lamparter | 9e0a0b3 | 2017-08-25 15:47:25 +0200 | [diff] [blame] | 672 | set_dynamic_sa_command_1(&sa->ctrl, 0, SA_HASH_MODE_HASH, |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 673 | CRYPTO_FEEDBACK_MODE_NO_FB, SA_EXTENDED_SN_OFF, |
| 674 | SA_SEQ_MASK_OFF, SA_MC_ENABLE, |
| 675 | SA_NOT_COPY_PAD, SA_NOT_COPY_PAYLOAD, |
| 676 | SA_NOT_COPY_HDR); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 677 | /* Need to zero hash digest in SA */ |
Christian Lamparter | 9e0a0b3 | 2017-08-25 15:47:25 +0200 | [diff] [blame] | 678 | memset(sa->inner_digest, 0, sizeof(sa->inner_digest)); |
| 679 | memset(sa->outer_digest, 0, sizeof(sa->outer_digest)); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | int crypto4xx_hash_init(struct ahash_request *req) |
| 685 | { |
| 686 | struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
| 687 | int ds; |
| 688 | struct dynamic_sa_ctl *sa; |
| 689 | |
Christian Lamparter | 9e0a0b3 | 2017-08-25 15:47:25 +0200 | [diff] [blame] | 690 | sa = ctx->sa_in; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 691 | ds = crypto_ahash_digestsize( |
| 692 | __crypto_ahash_cast(req->base.tfm)); |
| 693 | sa->sa_command_0.bf.digest_len = ds >> 2; |
| 694 | sa->sa_command_0.bf.load_hash_state = SA_LOAD_HASH_FROM_SA; |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | int crypto4xx_hash_update(struct ahash_request *req) |
| 700 | { |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 701 | struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 702 | struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 703 | struct scatterlist dst; |
| 704 | unsigned int ds = crypto_ahash_digestsize(ahash); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 705 | |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 706 | sg_init_one(&dst, req->result, ds); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 707 | |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 708 | return crypto4xx_build_pd(&req->base, ctx, req->src, &dst, |
| 709 | req->nbytes, NULL, 0, ctx->sa_in, |
Christian Lamparter | a0aae82 | 2017-10-04 01:00:15 +0200 | [diff] [blame] | 710 | ctx->sa_len, 0); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | int crypto4xx_hash_final(struct ahash_request *req) |
| 714 | { |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | int crypto4xx_hash_digest(struct ahash_request *req) |
| 719 | { |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 720 | struct crypto_ahash *ahash = crypto_ahash_reqtfm(req); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 721 | struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm); |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 722 | struct scatterlist dst; |
| 723 | unsigned int ds = crypto_ahash_digestsize(ahash); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 724 | |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 725 | sg_init_one(&dst, req->result, ds); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 726 | |
Christian Lamparter | cd4dcd6 | 2017-10-04 01:00:11 +0200 | [diff] [blame] | 727 | return crypto4xx_build_pd(&req->base, ctx, req->src, &dst, |
| 728 | req->nbytes, NULL, 0, ctx->sa_in, |
Christian Lamparter | a0aae82 | 2017-10-04 01:00:15 +0200 | [diff] [blame] | 729 | ctx->sa_len, 0); |
James Hsiao | 049359d | 2009-02-05 16:18:13 +1100 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /** |
| 733 | * SHA1 Algorithm |
| 734 | */ |
| 735 | int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm) |
| 736 | { |
| 737 | return crypto4xx_hash_alg_init(tfm, SA_HASH160_LEN, SA_HASH_ALG_SHA1, |
| 738 | SA_HASH_MODE_HASH); |
| 739 | } |