12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
23c4b2390SSalvatore Benedetto /*
33c4b2390SSalvatore Benedetto * Copyright (c) 2016, Intel Corporation
43c4b2390SSalvatore Benedetto * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
53c4b2390SSalvatore Benedetto */
63c4b2390SSalvatore Benedetto #include <linux/kernel.h>
73c4b2390SSalvatore Benedetto #include <linux/export.h>
83c4b2390SSalvatore Benedetto #include <linux/err.h>
93c4b2390SSalvatore Benedetto #include <linux/string.h>
103c4b2390SSalvatore Benedetto #include <crypto/ecdh.h>
113c4b2390SSalvatore Benedetto #include <crypto/kpp.h>
123c4b2390SSalvatore Benedetto
13*6763f5eaSMeng Yu #define ECDH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + sizeof(short))
143c4b2390SSalvatore Benedetto
ecdh_pack_data(void * dst,const void * src,size_t sz)153c4b2390SSalvatore Benedetto static inline u8 *ecdh_pack_data(void *dst, const void *src, size_t sz)
163c4b2390SSalvatore Benedetto {
173c4b2390SSalvatore Benedetto memcpy(dst, src, sz);
183c4b2390SSalvatore Benedetto return dst + sz;
193c4b2390SSalvatore Benedetto }
203c4b2390SSalvatore Benedetto
ecdh_unpack_data(void * dst,const void * src,size_t sz)213c4b2390SSalvatore Benedetto static inline const u8 *ecdh_unpack_data(void *dst, const void *src, size_t sz)
223c4b2390SSalvatore Benedetto {
233c4b2390SSalvatore Benedetto memcpy(dst, src, sz);
243c4b2390SSalvatore Benedetto return src + sz;
253c4b2390SSalvatore Benedetto }
263c4b2390SSalvatore Benedetto
crypto_ecdh_key_len(const struct ecdh * params)276e97e08dSTudor-Dan Ambarus unsigned int crypto_ecdh_key_len(const struct ecdh *params)
283c4b2390SSalvatore Benedetto {
293c4b2390SSalvatore Benedetto return ECDH_KPP_SECRET_MIN_SIZE + params->key_size;
303c4b2390SSalvatore Benedetto }
313c4b2390SSalvatore Benedetto EXPORT_SYMBOL_GPL(crypto_ecdh_key_len);
323c4b2390SSalvatore Benedetto
crypto_ecdh_encode_key(char * buf,unsigned int len,const struct ecdh * params)333c4b2390SSalvatore Benedetto int crypto_ecdh_encode_key(char *buf, unsigned int len,
343c4b2390SSalvatore Benedetto const struct ecdh *params)
353c4b2390SSalvatore Benedetto {
363c4b2390SSalvatore Benedetto u8 *ptr = buf;
373c4b2390SSalvatore Benedetto struct kpp_secret secret = {
383c4b2390SSalvatore Benedetto .type = CRYPTO_KPP_SECRET_TYPE_ECDH,
393c4b2390SSalvatore Benedetto .len = len
403c4b2390SSalvatore Benedetto };
413c4b2390SSalvatore Benedetto
423c4b2390SSalvatore Benedetto if (unlikely(!buf))
433c4b2390SSalvatore Benedetto return -EINVAL;
443c4b2390SSalvatore Benedetto
453c4b2390SSalvatore Benedetto if (len != crypto_ecdh_key_len(params))
463c4b2390SSalvatore Benedetto return -EINVAL;
473c4b2390SSalvatore Benedetto
483c4b2390SSalvatore Benedetto ptr = ecdh_pack_data(ptr, &secret, sizeof(secret));
493c4b2390SSalvatore Benedetto ptr = ecdh_pack_data(ptr, ¶ms->key_size, sizeof(params->key_size));
503c4b2390SSalvatore Benedetto ecdh_pack_data(ptr, params->key, params->key_size);
513c4b2390SSalvatore Benedetto
523c4b2390SSalvatore Benedetto return 0;
533c4b2390SSalvatore Benedetto }
543c4b2390SSalvatore Benedetto EXPORT_SYMBOL_GPL(crypto_ecdh_encode_key);
553c4b2390SSalvatore Benedetto
crypto_ecdh_decode_key(const char * buf,unsigned int len,struct ecdh * params)563c4b2390SSalvatore Benedetto int crypto_ecdh_decode_key(const char *buf, unsigned int len,
573c4b2390SSalvatore Benedetto struct ecdh *params)
583c4b2390SSalvatore Benedetto {
593c4b2390SSalvatore Benedetto const u8 *ptr = buf;
603c4b2390SSalvatore Benedetto struct kpp_secret secret;
613c4b2390SSalvatore Benedetto
623c4b2390SSalvatore Benedetto if (unlikely(!buf || len < ECDH_KPP_SECRET_MIN_SIZE))
633c4b2390SSalvatore Benedetto return -EINVAL;
643c4b2390SSalvatore Benedetto
653c4b2390SSalvatore Benedetto ptr = ecdh_unpack_data(&secret, ptr, sizeof(secret));
663c4b2390SSalvatore Benedetto if (secret.type != CRYPTO_KPP_SECRET_TYPE_ECDH)
673c4b2390SSalvatore Benedetto return -EINVAL;
683c4b2390SSalvatore Benedetto
69a53ab94eSDaniele Alessandrelli if (unlikely(len < secret.len))
70a53ab94eSDaniele Alessandrelli return -EINVAL;
71a53ab94eSDaniele Alessandrelli
723c4b2390SSalvatore Benedetto ptr = ecdh_unpack_data(¶ms->key_size, ptr, sizeof(params->key_size));
733c4b2390SSalvatore Benedetto if (secret.len != crypto_ecdh_key_len(params))
743c4b2390SSalvatore Benedetto return -EINVAL;
753c4b2390SSalvatore Benedetto
763c4b2390SSalvatore Benedetto /* Don't allocate memory. Set pointer to data
773c4b2390SSalvatore Benedetto * within the given buffer
783c4b2390SSalvatore Benedetto */
793c4b2390SSalvatore Benedetto params->key = (void *)ptr;
803c4b2390SSalvatore Benedetto
813c4b2390SSalvatore Benedetto return 0;
823c4b2390SSalvatore Benedetto }
833c4b2390SSalvatore Benedetto EXPORT_SYMBOL_GPL(crypto_ecdh_decode_key);
84