/*1* ECDH helper functions - KPP wrappings2*3* Copyright (C) 2017 Intel Corporation4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License version 2 as7* published by the Free Software Foundation;8*9* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS10* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,11* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.12* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY13* CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*18* ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,19* COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS20* SOFTWARE IS DISCLAIMED.21*/22#include "ecdh_helper.h"2324#include <linux/scatterlist.h>25#include <crypto/ecdh.h>2627static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)28{29int i;3031for (i = 0; i < ndigits; i++)32out[i] = __swab64(in[ndigits - 1 - i]);33}3435/* compute_ecdh_secret() - function assumes that the private key was36* already set.37* @tfm: KPP tfm handle allocated with crypto_alloc_kpp().38* @public_key: pair's ecc public key.39* secret: memory where the ecdh computed shared secret will be saved.40*41* Return: zero on success; error code in case of error.42*/43int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],44u8 secret[32])45{46DECLARE_CRYPTO_WAIT(result);47struct kpp_request *req;48u8 *tmp;49struct scatterlist src, dst;50int err;5152tmp = kmalloc(64, GFP_KERNEL);53if (!tmp)54return -ENOMEM;5556req = kpp_request_alloc(tfm, GFP_KERNEL);57if (!req) {58err = -ENOMEM;59goto free_tmp;60}6162swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */63swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */6465sg_init_one(&src, tmp, 64);66sg_init_one(&dst, secret, 32);67kpp_request_set_input(req, &src, 64);68kpp_request_set_output(req, &dst, 32);69kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,70crypto_req_done, &result);71err = crypto_kpp_compute_shared_secret(req);72err = crypto_wait_req(err, &result);73if (err < 0) {74pr_err("alg: ecdh: compute shared secret failed. err %d\n",75err);76goto free_all;77}7879swap_digits((u64 *)secret, (u64 *)tmp, 4);80memcpy(secret, tmp, 32);8182free_all:83kpp_request_free(req);84free_tmp:85kfree_sensitive(tmp);86return err;87}8889/* set_ecdh_privkey() - set or generate ecc private key.90*91* Function generates an ecc private key in the crypto subsystem when receiving92* a NULL private key or sets the received key when not NULL.93*94* @tfm: KPP tfm handle allocated with crypto_alloc_kpp().95* @private_key: user's ecc private key. When not NULL, the key is expected96* in little endian format.97*98* Return: zero on success; error code in case of error.99*/100int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])101{102u8 *buf, *tmp = NULL;103unsigned int buf_len;104int err;105struct ecdh p = {0};106107if (private_key) {108tmp = kmalloc(32, GFP_KERNEL);109if (!tmp)110return -ENOMEM;111swap_digits((u64 *)private_key, (u64 *)tmp, 4);112p.key = tmp;113p.key_size = 32;114}115116buf_len = crypto_ecdh_key_len(&p);117buf = kmalloc(buf_len, GFP_KERNEL);118if (!buf) {119err = -ENOMEM;120goto free_tmp;121}122123err = crypto_ecdh_encode_key(buf, buf_len, &p);124if (err)125goto free_all;126127err = crypto_kpp_set_secret(tfm, buf, buf_len);128/* fall through */129free_all:130kfree_sensitive(buf);131free_tmp:132kfree_sensitive(tmp);133return err;134}135136/* generate_ecdh_public_key() - function assumes that the private key was137* already set.138*139* @tfm: KPP tfm handle allocated with crypto_alloc_kpp().140* @public_key: memory where the computed ecc public key will be saved.141*142* Return: zero on success; error code in case of error.143*/144int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])145{146DECLARE_CRYPTO_WAIT(result);147struct kpp_request *req;148u8 *tmp;149struct scatterlist dst;150int err;151152tmp = kmalloc(64, GFP_KERNEL);153if (!tmp)154return -ENOMEM;155156req = kpp_request_alloc(tfm, GFP_KERNEL);157if (!req) {158err = -ENOMEM;159goto free_tmp;160}161162sg_init_one(&dst, tmp, 64);163kpp_request_set_input(req, NULL, 0);164kpp_request_set_output(req, &dst, 64);165kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,166crypto_req_done, &result);167168err = crypto_kpp_generate_public_key(req);169err = crypto_wait_req(err, &result);170if (err < 0)171goto free_all;172173/* The public key is handed back in little endian as expected by174* the Security Manager Protocol.175*/176swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */177swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */178179free_all:180kpp_request_free(req);181free_tmp:182kfree(tmp);183return err;184}185186/* generate_ecdh_keys() - generate ecc key pair.187*188* @tfm: KPP tfm handle allocated with crypto_alloc_kpp().189* @public_key: memory where the computed ecc public key will be saved.190*191* Return: zero on success; error code in case of error.192*/193int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])194{195int err;196197err = set_ecdh_privkey(tfm, NULL);198if (err)199return err;200201return generate_ecdh_public_key(tfm, public_key);202}203204205