1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Microchip / Atmel ECC (I2C) driver. 4 * 5 * Copyright (c) 2017, Microchip Technology Inc. 6 * Author: Tudor Ambarus 7 */ 8 9 #include <linux/delay.h> 10 #include <linux/device.h> 11 #include <linux/err.h> 12 #include <linux/errno.h> 13 #include <linux/i2c.h> 14 #include <linux/init.h> 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/of.h> 18 #include <linux/scatterlist.h> 19 #include <linux/slab.h> 20 #include <linux/workqueue.h> 21 #include <crypto/internal/kpp.h> 22 #include <crypto/ecdh.h> 23 #include <crypto/kpp.h> 24 #include "atmel-i2c.h" 25 26 static struct atmel_ecc_driver_data driver_data; 27 28 /** 29 * struct atmel_ecdh_ctx - transformation context 30 * @client: I2C client device 31 * @fallback: ECDH fallback used for caller-provided private keys 32 * @public_key: cached public key for the device-generated private key 33 * @do_fallback: true when ECDH operations should use @fallback 34 * 35 * The caller must not invoke set_secret() while generate_public_key() 36 * or compute_shared_secret() are in flight. 37 */ 38 struct atmel_ecdh_ctx { 39 struct i2c_client *client; 40 struct crypto_kpp *fallback; 41 const u8 *public_key; 42 bool do_fallback; 43 }; 44 45 static void atmel_ecdh_done(struct atmel_i2c_work_data *work_data, void *areq, 46 int status) 47 { 48 struct kpp_request *req = areq; 49 struct atmel_i2c_cmd *cmd = &work_data->cmd; 50 size_t copied, n_sz; 51 52 if (status) 53 goto free_work_data; 54 55 /* copy only as much as requested, capped at 32 bytes */ 56 n_sz = min(ATMEL_ECC_NIST_P256_N_SIZE, req->dst_len); 57 58 /* copy the shared secret */ 59 copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst, n_sz), 60 &cmd->data[RSP_DATA_IDX], n_sz); 61 if (copied != n_sz) 62 status = -EINVAL; 63 64 free_work_data: 65 kfree_sensitive(work_data); 66 kpp_request_complete(req, status); 67 } 68 69 /* 70 * If no private key is provided, generate one in the device and cache 71 * the corresponding public key. The generated private key never leaves 72 * the device. 73 */ 74 static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf, 75 unsigned int len) 76 { 77 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm); 78 struct atmel_i2c_cmd *cmd; 79 void *public_key; 80 struct ecdh params; 81 int ret = -ENOMEM; 82 83 kfree(ctx->public_key); 84 ctx->public_key = NULL; 85 ctx->do_fallback = false; 86 87 if (crypto_ecdh_decode_key(buf, len, ¶ms) < 0) { 88 dev_err(&ctx->client->dev, "crypto_ecdh_decode_key failed\n"); 89 return -EINVAL; 90 } 91 92 if (params.key_size) { 93 ret = crypto_kpp_set_secret(ctx->fallback, buf, len); 94 ctx->do_fallback = !ret; 95 return ret; 96 } 97 98 cmd = kmalloc_obj(*cmd); 99 if (!cmd) 100 return -ENOMEM; 101 102 public_key = kmalloc(ATMEL_ECC_PUBKEY_SIZE, GFP_KERNEL); 103 if (!public_key) 104 goto free_cmd; 105 106 atmel_i2c_init_genkey_cmd(cmd, DATA_SLOT_2); 107 108 ret = atmel_i2c_send_receive(ctx->client, cmd); 109 if (ret) { 110 kfree(public_key); 111 goto free_cmd; 112 } 113 114 memcpy(public_key, &cmd->data[RSP_DATA_IDX], ATMEL_ECC_PUBKEY_SIZE); 115 ctx->public_key = public_key; 116 117 free_cmd: 118 kfree(cmd); 119 return ret; 120 } 121 122 static int atmel_ecdh_generate_public_key(struct kpp_request *req) 123 { 124 struct crypto_kpp *tfm = crypto_kpp_reqtfm(req); 125 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm); 126 size_t copied, nbytes; 127 128 if (ctx->do_fallback) { 129 kpp_request_set_tfm(req, ctx->fallback); 130 return crypto_kpp_generate_public_key(req); 131 } 132 133 if (!ctx->public_key) 134 return -EINVAL; 135 136 /* copy only as much as requested, capped at 64 bytes */ 137 nbytes = min(ATMEL_ECC_PUBKEY_SIZE, req->dst_len); 138 139 /* public key was saved at private key generation */ 140 copied = sg_copy_from_buffer(req->dst, 141 sg_nents_for_len(req->dst, nbytes), 142 ctx->public_key, nbytes); 143 if (copied != nbytes) 144 return -EINVAL; 145 146 return 0; 147 } 148 149 static int atmel_ecdh_compute_shared_secret(struct kpp_request *req) 150 { 151 struct crypto_kpp *tfm = crypto_kpp_reqtfm(req); 152 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm); 153 struct atmel_i2c_work_data *work_data; 154 gfp_t gfp; 155 int ret; 156 157 if (ctx->do_fallback) { 158 kpp_request_set_tfm(req, ctx->fallback); 159 return crypto_kpp_compute_shared_secret(req); 160 } 161 162 if (!ctx->public_key) 163 return -EINVAL; 164 165 /* A P-256 public key must contain two 32-byte coordinates */ 166 if (req->src_len != ATMEL_ECC_PUBKEY_SIZE) 167 return -EINVAL; 168 169 gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL : 170 GFP_ATOMIC; 171 172 work_data = kmalloc_obj(*work_data, gfp); 173 if (!work_data) 174 return -ENOMEM; 175 176 work_data->ctx = ctx; 177 work_data->client = ctx->client; 178 179 ret = atmel_i2c_init_ecdh_cmd(&work_data->cmd, req->src); 180 if (ret) 181 goto free_work_data; 182 183 atmel_i2c_enqueue(work_data, atmel_ecdh_done, req); 184 185 return -EINPROGRESS; 186 187 free_work_data: 188 kfree(work_data); 189 return ret; 190 } 191 192 static struct i2c_client *atmel_ecc_i2c_client_alloc(void) 193 { 194 struct atmel_i2c_client_priv *i2c_priv, *min_i2c_priv = NULL; 195 struct i2c_client *client = ERR_PTR(-ENODEV); 196 int min_tfm_cnt = INT_MAX; 197 int tfm_cnt; 198 199 spin_lock(&driver_data.i2c_list_lock); 200 201 if (list_empty(&driver_data.i2c_client_list)) { 202 spin_unlock(&driver_data.i2c_list_lock); 203 return ERR_PTR(-ENODEV); 204 } 205 206 list_for_each_entry(i2c_priv, &driver_data.i2c_client_list, 207 i2c_client_list_node) { 208 tfm_cnt = atomic_read(&i2c_priv->tfm_count); 209 if (tfm_cnt < min_tfm_cnt) { 210 min_tfm_cnt = tfm_cnt; 211 min_i2c_priv = i2c_priv; 212 } 213 if (!min_tfm_cnt) 214 break; 215 } 216 217 if (min_i2c_priv) { 218 atomic_inc(&min_i2c_priv->tfm_count); 219 client = min_i2c_priv->client; 220 } 221 222 spin_unlock(&driver_data.i2c_list_lock); 223 224 return client; 225 } 226 227 static void atmel_ecc_i2c_client_free(struct i2c_client *client) 228 { 229 struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client); 230 231 atomic_dec(&i2c_priv->tfm_count); 232 } 233 234 static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm) 235 { 236 const char *alg = kpp_alg_name(tfm); 237 struct crypto_kpp *fallback; 238 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm); 239 240 ctx->client = atmel_ecc_i2c_client_alloc(); 241 if (IS_ERR(ctx->client)) { 242 pr_err("tfm - i2c_client binding failed\n"); 243 return PTR_ERR(ctx->client); 244 } 245 246 fallback = crypto_alloc_kpp(alg, 0, CRYPTO_ALG_NEED_FALLBACK); 247 if (IS_ERR(fallback)) { 248 dev_err(&ctx->client->dev, "Failed to allocate transformation for '%s': %ld\n", 249 alg, PTR_ERR(fallback)); 250 atmel_ecc_i2c_client_free(ctx->client); 251 return PTR_ERR(fallback); 252 } 253 254 crypto_kpp_set_flags(fallback, crypto_kpp_get_flags(tfm)); 255 ctx->fallback = fallback; 256 257 return 0; 258 } 259 260 static void atmel_ecdh_exit_tfm(struct crypto_kpp *tfm) 261 { 262 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm); 263 264 kfree(ctx->public_key); 265 crypto_free_kpp(ctx->fallback); 266 atmel_ecc_i2c_client_free(ctx->client); 267 } 268 269 static unsigned int atmel_ecdh_max_size(struct crypto_kpp *tfm) 270 { 271 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm); 272 273 return crypto_kpp_maxsize(ctx->fallback); 274 } 275 276 static struct kpp_alg atmel_ecdh_nist_p256 = { 277 .set_secret = atmel_ecdh_set_secret, 278 .generate_public_key = atmel_ecdh_generate_public_key, 279 .compute_shared_secret = atmel_ecdh_compute_shared_secret, 280 .init = atmel_ecdh_init_tfm, 281 .exit = atmel_ecdh_exit_tfm, 282 .max_size = atmel_ecdh_max_size, 283 .base = { 284 .cra_flags = CRYPTO_ALG_NEED_FALLBACK, 285 .cra_name = "ecdh-nist-p256", 286 .cra_driver_name = "atmel-ecdh", 287 .cra_priority = ATMEL_ECC_PRIORITY, 288 .cra_module = THIS_MODULE, 289 .cra_ctxsize = sizeof(struct atmel_ecdh_ctx), 290 }, 291 }; 292 293 static int atmel_ecc_probe(struct i2c_client *client) 294 { 295 struct atmel_i2c_client_priv *i2c_priv; 296 int ret; 297 298 ret = atmel_i2c_probe(client); 299 if (ret) 300 return ret; 301 302 i2c_priv = i2c_get_clientdata(client); 303 304 spin_lock(&driver_data.i2c_list_lock); 305 list_add_tail(&i2c_priv->i2c_client_list_node, 306 &driver_data.i2c_client_list); 307 spin_unlock(&driver_data.i2c_list_lock); 308 309 ret = crypto_register_kpp(&atmel_ecdh_nist_p256); 310 if (ret) { 311 spin_lock(&driver_data.i2c_list_lock); 312 list_del(&i2c_priv->i2c_client_list_node); 313 spin_unlock(&driver_data.i2c_list_lock); 314 315 dev_err(&client->dev, "%s alg registration failed\n", 316 atmel_ecdh_nist_p256.base.cra_driver_name); 317 } else { 318 dev_info(&client->dev, "atmel ecc algorithms registered in /proc/crypto\n"); 319 } 320 321 return ret; 322 } 323 324 static void atmel_ecc_remove(struct i2c_client *client) 325 { 326 struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client); 327 328 /* Return EBUSY if i2c client already allocated. */ 329 if (atomic_read(&i2c_priv->tfm_count)) { 330 /* 331 * After we return here, the memory backing the device is freed. 332 * That happens no matter what the return value of this function 333 * is because in the Linux device model there is no error 334 * handling for unbinding a driver. 335 * If there is still some action pending, it probably involves 336 * accessing the freed memory. 337 */ 338 dev_emerg(&client->dev, "Device is busy, expect memory corruption.\n"); 339 return; 340 } 341 342 crypto_unregister_kpp(&atmel_ecdh_nist_p256); 343 344 spin_lock(&driver_data.i2c_list_lock); 345 list_del(&i2c_priv->i2c_client_list_node); 346 spin_unlock(&driver_data.i2c_list_lock); 347 } 348 349 static const struct of_device_id atmel_ecc_dt_ids[] = { 350 { .compatible = "atmel,atecc508a", }, 351 { .compatible = "atmel,atecc608b", }, 352 { } 353 }; 354 MODULE_DEVICE_TABLE(of, atmel_ecc_dt_ids); 355 356 static const struct i2c_device_id atmel_ecc_id[] = { 357 { .name = "atecc508a" }, 358 { .name = "atecc608b" }, 359 { } 360 }; 361 MODULE_DEVICE_TABLE(i2c, atmel_ecc_id); 362 363 static struct i2c_driver atmel_ecc_driver = { 364 .driver = { 365 .name = "atmel-ecc", 366 .of_match_table = atmel_ecc_dt_ids, 367 }, 368 .probe = atmel_ecc_probe, 369 .remove = atmel_ecc_remove, 370 .id_table = atmel_ecc_id, 371 }; 372 373 static int __init atmel_ecc_init(void) 374 { 375 spin_lock_init(&driver_data.i2c_list_lock); 376 INIT_LIST_HEAD(&driver_data.i2c_client_list); 377 return i2c_add_driver(&atmel_ecc_driver); 378 } 379 380 static void __exit atmel_ecc_exit(void) 381 { 382 atmel_i2c_flush_queue(); 383 i2c_del_driver(&atmel_ecc_driver); 384 } 385 386 module_init(atmel_ecc_init); 387 module_exit(atmel_ecc_exit); 388 389 MODULE_AUTHOR("Tudor Ambarus"); 390 MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver"); 391 MODULE_LICENSE("GPL v2"); 392