Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Microchip / Atmel ECC (I2C) driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2017, Microchip Technology Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Tudor Ambarus <tudor.ambarus@microchip.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <crypto/internal/kpp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <crypto/ecdh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <crypto/kpp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "atmel-i2c.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static struct atmel_ecc_driver_data driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * atmel_ecdh_ctx - transformation context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * @client     : pointer to i2c client device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * @fallback   : used for unsupported curves or when user wants to use its own
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *               private key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * @public_key : generated when calling set_secret(). It's the responsibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *               of the user to not call set_secret() while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *               generate_public_key() or compute_shared_secret() are in flight.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * @curve_id   : elliptic curve id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * @n_sz       : size in bytes of the n prime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * @do_fallback: true when the device doesn't support the curve or when the user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *               wants to use its own private key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct atmel_ecdh_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct crypto_kpp *fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	const u8 *public_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	unsigned int curve_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	size_t n_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	bool do_fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static void atmel_ecdh_done(struct atmel_i2c_work_data *work_data, void *areq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			    int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct kpp_request *req = areq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct atmel_ecdh_ctx *ctx = work_data->ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct atmel_i2c_cmd *cmd = &work_data->cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	size_t copied, n_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		goto free_work_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	/* might want less than we've got */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	n_sz = min_t(size_t, ctx->n_sz, req->dst_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/* copy the shared secret */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst, n_sz),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 				     &cmd->data[RSP_DATA_IDX], n_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (copied != n_sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) free_work_data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	kfree_sensitive(work_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	kpp_request_complete(req, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static unsigned int atmel_ecdh_supported_curve(unsigned int curve_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (curve_id == ECC_CURVE_NIST_P256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return ATMEL_ECC_NIST_P256_N_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * A random private key is generated and stored in the device. The device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * returns the pair public key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				 unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct atmel_i2c_cmd *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	void *public_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct ecdh params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/* free the old public key, if any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	kfree(ctx->public_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	/* make sure you don't free the old public key twice */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	ctx->public_key = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (crypto_ecdh_decode_key(buf, len, &params) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		dev_err(&ctx->client->dev, "crypto_ecdh_decode_key failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	ctx->n_sz = atmel_ecdh_supported_curve(params.curve_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (!ctx->n_sz || params.key_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		/* fallback to ecdh software implementation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		ctx->do_fallback = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return crypto_kpp_set_secret(ctx->fallback, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	 * The device only supports NIST P256 ECC keys. The public key size will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	 * always be the same. Use a macro for the key size to avoid unnecessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	 * computations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	public_key = kmalloc(ATMEL_ECC_PUBKEY_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (!public_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		goto free_cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	ctx->do_fallback = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	ctx->curve_id = params.curve_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	atmel_i2c_init_genkey_cmd(cmd, DATA_SLOT_2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	ret = atmel_i2c_send_receive(ctx->client, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		goto free_public_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	/* save the public key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	memcpy(public_key, &cmd->data[RSP_DATA_IDX], ATMEL_ECC_PUBKEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	ctx->public_key = public_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) free_public_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	kfree(public_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) free_cmd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int atmel_ecdh_generate_public_key(struct kpp_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	size_t copied, nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (ctx->do_fallback) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		kpp_request_set_tfm(req, ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		return crypto_kpp_generate_public_key(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if (!ctx->public_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/* might want less than we've got */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	nbytes = min_t(size_t, ATMEL_ECC_PUBKEY_SIZE, req->dst_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	/* public key was saved at private key generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	copied = sg_copy_from_buffer(req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				     sg_nents_for_len(req->dst, nbytes),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				     ctx->public_key, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (copied != nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	struct atmel_i2c_work_data *work_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	gfp_t gfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (ctx->do_fallback) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		kpp_request_set_tfm(req, ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		return crypto_kpp_compute_shared_secret(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	/* must have exactly two points to be on the curve */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (req->src_len != ATMEL_ECC_PUBKEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 							     GFP_ATOMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	work_data = kmalloc(sizeof(*work_data), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (!work_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	work_data->ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	work_data->client = ctx->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	ret = atmel_i2c_init_ecdh_cmd(&work_data->cmd, req->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		goto free_work_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	atmel_i2c_enqueue(work_data, atmel_ecdh_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) free_work_data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	kfree(work_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static struct i2c_client *atmel_ecc_i2c_client_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	struct atmel_i2c_client_priv *i2c_priv, *min_i2c_priv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct i2c_client *client = ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int min_tfm_cnt = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	int tfm_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	spin_lock(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (list_empty(&driver_data.i2c_client_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		spin_unlock(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	list_for_each_entry(i2c_priv, &driver_data.i2c_client_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			    i2c_client_list_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		tfm_cnt = atomic_read(&i2c_priv->tfm_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		if (tfm_cnt < min_tfm_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			min_tfm_cnt = tfm_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			min_i2c_priv = i2c_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		if (!min_tfm_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (min_i2c_priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		atomic_inc(&min_i2c_priv->tfm_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		client = min_i2c_priv->client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	spin_unlock(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	return client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static void atmel_ecc_i2c_client_free(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	atomic_dec(&i2c_priv->tfm_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	const char *alg = kpp_alg_name(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	struct crypto_kpp *fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	ctx->client = atmel_ecc_i2c_client_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (IS_ERR(ctx->client)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		pr_err("tfm - i2c_client binding failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		return PTR_ERR(ctx->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	fallback = crypto_alloc_kpp(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (IS_ERR(fallback)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		dev_err(&ctx->client->dev, "Failed to allocate transformation for '%s': %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			alg, PTR_ERR(fallback));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return PTR_ERR(fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	crypto_kpp_set_flags(fallback, crypto_kpp_get_flags(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	ctx->fallback = fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static void atmel_ecdh_exit_tfm(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	kfree(ctx->public_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	crypto_free_kpp(ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	atmel_ecc_i2c_client_free(ctx->client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static unsigned int atmel_ecdh_max_size(struct crypto_kpp *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (ctx->fallback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		return crypto_kpp_maxsize(ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	 * The device only supports NIST P256 ECC keys. The public key size will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	 * always be the same. Use a macro for the key size to avoid unnecessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 * computations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return ATMEL_ECC_PUBKEY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static struct kpp_alg atmel_ecdh = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	.set_secret = atmel_ecdh_set_secret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	.generate_public_key = atmel_ecdh_generate_public_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	.compute_shared_secret = atmel_ecdh_compute_shared_secret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	.init = atmel_ecdh_init_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	.exit = atmel_ecdh_exit_tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	.max_size = atmel_ecdh_max_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	.base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		.cra_name = "ecdh",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		.cra_driver_name = "atmel-ecdh",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		.cra_priority = ATMEL_ECC_PRIORITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		.cra_ctxsize = sizeof(struct atmel_ecdh_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int atmel_ecc_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			   const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct atmel_i2c_client_priv *i2c_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	ret = atmel_i2c_probe(client, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	i2c_priv = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	spin_lock(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	list_add_tail(&i2c_priv->i2c_client_list_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		      &driver_data.i2c_client_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	spin_unlock(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	ret = crypto_register_kpp(&atmel_ecdh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		spin_lock(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		list_del(&i2c_priv->i2c_client_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		spin_unlock(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		dev_err(&client->dev, "%s alg registration failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			atmel_ecdh.base.cra_driver_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		dev_info(&client->dev, "atmel ecc algorithms registered in /proc/crypto\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static int atmel_ecc_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	/* Return EBUSY if i2c client already allocated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (atomic_read(&i2c_priv->tfm_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		dev_err(&client->dev, "Device is busy\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	crypto_unregister_kpp(&atmel_ecdh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	spin_lock(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	list_del(&i2c_priv->i2c_client_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	spin_unlock(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static const struct of_device_id atmel_ecc_dt_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		.compatible = "atmel,atecc508a",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	}, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		/* sentinel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) MODULE_DEVICE_TABLE(of, atmel_ecc_dt_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static const struct i2c_device_id atmel_ecc_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	{ "atecc508a", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) MODULE_DEVICE_TABLE(i2c, atmel_ecc_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static struct i2c_driver atmel_ecc_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		.name	= "atmel-ecc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		.of_match_table = of_match_ptr(atmel_ecc_dt_ids),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	.probe		= atmel_ecc_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	.remove		= atmel_ecc_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	.id_table	= atmel_ecc_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static int __init atmel_ecc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	spin_lock_init(&driver_data.i2c_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	INIT_LIST_HEAD(&driver_data.i2c_client_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	return i2c_add_driver(&atmel_ecc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static void __exit atmel_ecc_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	flush_scheduled_work();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	i2c_del_driver(&atmel_ecc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) module_init(atmel_ecc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) module_exit(atmel_ecc_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) MODULE_AUTHOR("Tudor Ambarus <tudor.ambarus@microchip.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) MODULE_LICENSE("GPL v2");