Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * ECDH helper functions - KPP wrappings
0003  *
0004  * Copyright (C) 2017 Intel Corporation
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License version 2 as
0008  * published by the Free Software Foundation;
0009  *
0010  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0011  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0012  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
0013  * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
0014  * CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
0015  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0016  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0017  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0018  *
0019  * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
0020  * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
0021  * SOFTWARE IS DISCLAIMED.
0022  */
0023 #include "ecdh_helper.h"
0024 
0025 #include <linux/scatterlist.h>
0026 #include <crypto/ecdh.h>
0027 
0028 struct ecdh_completion {
0029     struct completion completion;
0030     int err;
0031 };
0032 
0033 static void ecdh_complete(struct crypto_async_request *req, int err)
0034 {
0035     struct ecdh_completion *res = req->data;
0036 
0037     if (err == -EINPROGRESS)
0038         return;
0039 
0040     res->err = err;
0041     complete(&res->completion);
0042 }
0043 
0044 static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
0045 {
0046     int i;
0047 
0048     for (i = 0; i < ndigits; i++)
0049         out[i] = __swab64(in[ndigits - 1 - i]);
0050 }
0051 
0052 /* compute_ecdh_secret() - function assumes that the private key was
0053  *                         already set.
0054  * @tfm:          KPP tfm handle allocated with crypto_alloc_kpp().
0055  * @public_key:   pair's ecc public key.
0056  * secret:        memory where the ecdh computed shared secret will be saved.
0057  *
0058  * Return: zero on success; error code in case of error.
0059  */
0060 int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],
0061             u8 secret[32])
0062 {
0063     struct kpp_request *req;
0064     u8 *tmp;
0065     struct ecdh_completion result;
0066     struct scatterlist src, dst;
0067     int err;
0068 
0069     tmp = kmalloc(64, GFP_KERNEL);
0070     if (!tmp)
0071         return -ENOMEM;
0072 
0073     req = kpp_request_alloc(tfm, GFP_KERNEL);
0074     if (!req) {
0075         err = -ENOMEM;
0076         goto free_tmp;
0077     }
0078 
0079     init_completion(&result.completion);
0080 
0081     swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */
0082     swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */
0083 
0084     sg_init_one(&src, tmp, 64);
0085     sg_init_one(&dst, secret, 32);
0086     kpp_request_set_input(req, &src, 64);
0087     kpp_request_set_output(req, &dst, 32);
0088     kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
0089                  ecdh_complete, &result);
0090     err = crypto_kpp_compute_shared_secret(req);
0091     if (err == -EINPROGRESS) {
0092         wait_for_completion(&result.completion);
0093         err = result.err;
0094     }
0095     if (err < 0) {
0096         pr_err("alg: ecdh: compute shared secret failed. err %d\n",
0097                err);
0098         goto free_all;
0099     }
0100 
0101     swap_digits((u64 *)secret, (u64 *)tmp, 4);
0102     memcpy(secret, tmp, 32);
0103 
0104 free_all:
0105     kpp_request_free(req);
0106 free_tmp:
0107     kfree_sensitive(tmp);
0108     return err;
0109 }
0110 
0111 /* set_ecdh_privkey() - set or generate ecc private key.
0112  *
0113  * Function generates an ecc private key in the crypto subsystem when receiving
0114  * a NULL private key or sets the received key when not NULL.
0115  *
0116  * @tfm:           KPP tfm handle allocated with crypto_alloc_kpp().
0117  * @private_key:   user's ecc private key. When not NULL, the key is expected
0118  *                 in little endian format.
0119  *
0120  * Return: zero on success; error code in case of error.
0121  */
0122 int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])
0123 {
0124     u8 *buf, *tmp = NULL;
0125     unsigned int buf_len;
0126     int err;
0127     struct ecdh p = {0};
0128 
0129     if (private_key) {
0130         tmp = kmalloc(32, GFP_KERNEL);
0131         if (!tmp)
0132             return -ENOMEM;
0133         swap_digits((u64 *)private_key, (u64 *)tmp, 4);
0134         p.key = tmp;
0135         p.key_size = 32;
0136     }
0137 
0138     buf_len = crypto_ecdh_key_len(&p);
0139     buf = kmalloc(buf_len, GFP_KERNEL);
0140     if (!buf) {
0141         err = -ENOMEM;
0142         goto free_tmp;
0143     }
0144 
0145     err = crypto_ecdh_encode_key(buf, buf_len, &p);
0146     if (err)
0147         goto free_all;
0148 
0149     err = crypto_kpp_set_secret(tfm, buf, buf_len);
0150     /* fall through */
0151 free_all:
0152     kfree_sensitive(buf);
0153 free_tmp:
0154     kfree_sensitive(tmp);
0155     return err;
0156 }
0157 
0158 /* generate_ecdh_public_key() - function assumes that the private key was
0159  *                              already set.
0160  *
0161  * @tfm:          KPP tfm handle allocated with crypto_alloc_kpp().
0162  * @public_key:   memory where the computed ecc public key will be saved.
0163  *
0164  * Return: zero on success; error code in case of error.
0165  */
0166 int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])
0167 {
0168     struct kpp_request *req;
0169     u8 *tmp;
0170     struct ecdh_completion result;
0171     struct scatterlist dst;
0172     int err;
0173 
0174     tmp = kmalloc(64, GFP_KERNEL);
0175     if (!tmp)
0176         return -ENOMEM;
0177 
0178     req = kpp_request_alloc(tfm, GFP_KERNEL);
0179     if (!req) {
0180         err = -ENOMEM;
0181         goto free_tmp;
0182     }
0183 
0184     init_completion(&result.completion);
0185     sg_init_one(&dst, tmp, 64);
0186     kpp_request_set_input(req, NULL, 0);
0187     kpp_request_set_output(req, &dst, 64);
0188     kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
0189                  ecdh_complete, &result);
0190 
0191     err = crypto_kpp_generate_public_key(req);
0192     if (err == -EINPROGRESS) {
0193         wait_for_completion(&result.completion);
0194         err = result.err;
0195     }
0196     if (err < 0)
0197         goto free_all;
0198 
0199     /* The public key is handed back in little endian as expected by
0200      * the Security Manager Protocol.
0201      */
0202     swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
0203     swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
0204 
0205 free_all:
0206     kpp_request_free(req);
0207 free_tmp:
0208     kfree(tmp);
0209     return err;
0210 }
0211 
0212 /* generate_ecdh_keys() - generate ecc key pair.
0213  *
0214  * @tfm:          KPP tfm handle allocated with crypto_alloc_kpp().
0215  * @public_key:   memory where the computed ecc public key will be saved.
0216  *
0217  * Return: zero on success; error code in case of error.
0218  */
0219 int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])
0220 {
0221     int err;
0222 
0223     err = set_ecdh_privkey(tfm, NULL);
0224     if (err)
0225         return err;
0226 
0227     return generate_ecdh_public_key(tfm, public_key);
0228 }