summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ecdh/ecdh.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/ecdh/ecdh.c (renamed from src/lib/libcrypto/ecdh/ech_key.c)64
1 files changed, 63 insertions, 1 deletions
diff --git a/src/lib/libcrypto/ecdh/ech_key.c b/src/lib/libcrypto/ecdh/ecdh.c
index 5efb49ba59..d2de3a09af 100644
--- a/src/lib/libcrypto/ecdh/ech_key.c
+++ b/src/lib/libcrypto/ecdh/ecdh.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ech_key.c,v 1.33 2023/07/05 08:39:40 tb Exp $ */ 1/* $OpenBSD: ecdh.c,v 1.1 2023/07/05 12:31:14 tb Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 * 4 *
@@ -77,10 +77,72 @@
77#include <openssl/ec.h> 77#include <openssl/ec.h>
78#include <openssl/ecdh.h> 78#include <openssl/ecdh.h>
79#include <openssl/err.h> 79#include <openssl/err.h>
80#include <openssl/evp.h>
80 81
81#include "ec_local.h" 82#include "ec_local.h"
82 83
83/* 84/*
85 * Key derivation function from X9.63/SECG.
86 */
87
88/* Way more than we will ever need */
89#define ECDH_KDF_MAX (1 << 30)
90
91int
92ecdh_KDF_X9_63(unsigned char *out, size_t outlen, const unsigned char *Z,
93 size_t Zlen, const unsigned char *sinfo, size_t sinfolen, const EVP_MD *md)
94{
95 EVP_MD_CTX *mctx = NULL;
96 unsigned int i;
97 size_t mdlen;
98 unsigned char ctr[4];
99 int rv = 0;
100
101 if (sinfolen > ECDH_KDF_MAX || outlen > ECDH_KDF_MAX ||
102 Zlen > ECDH_KDF_MAX)
103 return 0;
104 mctx = EVP_MD_CTX_new();
105 if (mctx == NULL)
106 return 0;
107 mdlen = EVP_MD_size(md);
108 for (i = 1;; i++) {
109 unsigned char mtmp[EVP_MAX_MD_SIZE];
110 if (!EVP_DigestInit_ex(mctx, md, NULL))
111 goto err;
112 ctr[3] = i & 0xFF;
113 ctr[2] = (i >> 8) & 0xFF;
114 ctr[1] = (i >> 16) & 0xFF;
115 ctr[0] = (i >> 24) & 0xFF;
116 if (!EVP_DigestUpdate(mctx, Z, Zlen))
117 goto err;
118 if (!EVP_DigestUpdate(mctx, ctr, sizeof(ctr)))
119 goto err;
120 if (!EVP_DigestUpdate(mctx, sinfo, sinfolen))
121 goto err;
122 if (outlen >= mdlen) {
123 if (!EVP_DigestFinal(mctx, out, NULL))
124 goto err;
125 outlen -= mdlen;
126 if (outlen == 0)
127 break;
128 out += mdlen;
129 } else {
130 if (!EVP_DigestFinal(mctx, mtmp, NULL))
131 goto err;
132 memcpy(out, mtmp, outlen);
133 explicit_bzero(mtmp, mdlen);
134 break;
135 }
136 }
137 rv = 1;
138
139 err:
140 EVP_MD_CTX_free(mctx);
141
142 return rv;
143}
144
145/*
84 * Based on the ECKAS-DH1 and ECSVDP-DH primitives in the IEEE 1363 standard. 146 * Based on the ECKAS-DH1 and ECSVDP-DH primitives in the IEEE 1363 standard.
85 */ 147 */
86/* XXX - KDF handling moved to ECDH_compute_key(). See OpenSSL e2285d87. */ 148/* XXX - KDF handling moved to ECDH_compute_key(). See OpenSSL e2285d87. */