Blame view
crypto/ecdh_helper.c
2.26 KB
3c4b23901 crypto: ecdh - Ad... |
1 2 3 4 5 |
/* * Copyright (c) 2016, Intel Corporation * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com> * * This program is free software; you can redistribute it and/or |
c0ca1215d crypto: kpp, (ec)... |
6 |
* modify it under the terms of the GNU General Public License |
3c4b23901 crypto: ecdh - Ad... |
7 |
* as published by the Free Software Foundation; either version |
c0ca1215d crypto: kpp, (ec)... |
8 |
* 2 of the License, or (at your option) any later version. |
3c4b23901 crypto: ecdh - Ad... |
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
*/ #include <linux/kernel.h> #include <linux/export.h> #include <linux/err.h> #include <linux/string.h> #include <crypto/ecdh.h> #include <crypto/kpp.h> #define ECDH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 2 * sizeof(short)) static inline u8 *ecdh_pack_data(void *dst, const void *src, size_t sz) { memcpy(dst, src, sz); return dst + sz; } static inline const u8 *ecdh_unpack_data(void *dst, const void *src, size_t sz) { memcpy(dst, src, sz); return src + sz; } int crypto_ecdh_key_len(const struct ecdh *params) { return ECDH_KPP_SECRET_MIN_SIZE + params->key_size; } EXPORT_SYMBOL_GPL(crypto_ecdh_key_len); int crypto_ecdh_encode_key(char *buf, unsigned int len, const struct ecdh *params) { u8 *ptr = buf; struct kpp_secret secret = { .type = CRYPTO_KPP_SECRET_TYPE_ECDH, .len = len }; if (unlikely(!buf)) return -EINVAL; if (len != crypto_ecdh_key_len(params)) return -EINVAL; ptr = ecdh_pack_data(ptr, &secret, sizeof(secret)); ptr = ecdh_pack_data(ptr, ¶ms->curve_id, sizeof(params->curve_id)); ptr = ecdh_pack_data(ptr, ¶ms->key_size, sizeof(params->key_size)); ecdh_pack_data(ptr, params->key, params->key_size); return 0; } EXPORT_SYMBOL_GPL(crypto_ecdh_encode_key); int crypto_ecdh_decode_key(const char *buf, unsigned int len, struct ecdh *params) { const u8 *ptr = buf; struct kpp_secret secret; if (unlikely(!buf || len < ECDH_KPP_SECRET_MIN_SIZE)) return -EINVAL; ptr = ecdh_unpack_data(&secret, ptr, sizeof(secret)); if (secret.type != CRYPTO_KPP_SECRET_TYPE_ECDH) return -EINVAL; ptr = ecdh_unpack_data(¶ms->curve_id, ptr, sizeof(params->curve_id)); ptr = ecdh_unpack_data(¶ms->key_size, ptr, sizeof(params->key_size)); if (secret.len != crypto_ecdh_key_len(params)) return -EINVAL; /* Don't allocate memory. Set pointer to data * within the given buffer */ params->key = (void *)ptr; return 0; } EXPORT_SYMBOL_GPL(crypto_ecdh_decode_key); |