xref: /netbsd-src/crypto/external/bsd/openssl/dist/doc/man7/EVP_KEYEXCH-ECDH.pod (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos=pod
2*b0d17251Schristos
3*b0d17251Schristos=head1 NAME
4*b0d17251Schristos
5*b0d17251SchristosEVP_KEYEXCH-ECDH - ECDH Key Exchange algorithm support
6*b0d17251Schristos
7*b0d17251Schristos=head1 DESCRIPTION
8*b0d17251Schristos
9*b0d17251SchristosKey exchange support for the B<ECDH> key type.
10*b0d17251Schristos
11*b0d17251Schristos=head2 ECDH Key Exchange parameters
12*b0d17251Schristos
13*b0d17251Schristos=over 4
14*b0d17251Schristos
15*b0d17251Schristos=item "ecdh-cofactor-mode" (B<OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE>) <integer>
16*b0d17251Schristos
17*b0d17251SchristosSets or gets the ECDH mode of operation for the associated key exchange ctx.
18*b0d17251Schristos
19*b0d17251SchristosIn the context of an Elliptic Curve Diffie-Hellman key exchange, this parameter
20*b0d17251Schristoscan be used to select between the plain Diffie-Hellman (DH) or Cofactor
21*b0d17251SchristosDiffie-Hellman (CDH) variants of the key exchange algorithm.
22*b0d17251Schristos
23*b0d17251SchristosWhen setting, the value should be 1, 0 or -1, respectively forcing cofactor mode
24*b0d17251Schristoson, off, or resetting it to the default for the private key associated with the
25*b0d17251Schristosgiven key exchange ctx.
26*b0d17251Schristos
27*b0d17251SchristosWhen getting, the value should be either 1 or 0, respectively signaling if the
28*b0d17251Schristoscofactor mode is on or off.
29*b0d17251Schristos
30*b0d17251SchristosSee also L<provider-keymgmt(7)> for the related
31*b0d17251SchristosB<OSSL_PKEY_PARAM_USE_COFACTOR_ECDH> parameter that can be set on a
32*b0d17251Schristosper-key basis.
33*b0d17251Schristos
34*b0d17251Schristos=item "kdf-type" (B<OSSL_EXCHANGE_PARAM_KDF_TYPE>) <UTF8 string>
35*b0d17251Schristos
36*b0d17251SchristosSee L<provider-keyexch(7)/Common Key Exchange parameters>.
37*b0d17251Schristos
38*b0d17251Schristos=item "kdf-digest" (B<OSSL_EXCHANGE_PARAM_KDF_DIGEST>) <UTF8 string>
39*b0d17251Schristos
40*b0d17251SchristosSee L<provider-keyexch(7)/Common Key Exchange parameters>.
41*b0d17251Schristos
42*b0d17251Schristos=item "kdf-digest-props" (B<OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS>) <UTF8 string>
43*b0d17251Schristos
44*b0d17251SchristosSee L<provider-keyexch(7)/Common Key Exchange parameters>.
45*b0d17251Schristos
46*b0d17251Schristos=item "kdf-outlen" (B<OSSL_EXCHANGE_PARAM_KDF_OUTLEN>) <unsigned integer>
47*b0d17251Schristos
48*b0d17251SchristosSee L<provider-keyexch(7)/Common Key Exchange parameters>.
49*b0d17251Schristos
50*b0d17251Schristos=item "kdf-ukm" (B<OSSL_EXCHANGE_PARAM_KDF_UKM>) <octet string>
51*b0d17251Schristos
52*b0d17251SchristosSee L<provider-keyexch(7)/Common Key Exchange parameters>.
53*b0d17251Schristos
54*b0d17251Schristos=back
55*b0d17251Schristos
56*b0d17251Schristos=head1 EXAMPLES
57*b0d17251Schristos
58*b0d17251SchristosKeys for the host and peer must be generated as shown in
59*b0d17251SchristosL<EVP_PKEY-EC(7)/Examples> using the same curve name.
60*b0d17251Schristos
61*b0d17251SchristosThe code to generate a shared secret for the normal case is identical to
62*b0d17251SchristosL<EVP_KEYEXCH-DH(7)/Examples>.
63*b0d17251Schristos
64*b0d17251SchristosTo derive a shared secret on the host using the host's key and the peer's public
65*b0d17251Schristoskey but also using X963KDF with a user key material:
66*b0d17251Schristos
67*b0d17251Schristos    /* It is assumed that the host_key, peer_pub_key and ukm are set up */
68*b0d17251Schristos    void derive_secret(EVP_PKEY *host_key, EVP_PKEY *peer_key,
69*b0d17251Schristos                       unsigned char *ukm, size_t ukm_len)
70*b0d17251Schristos    {
71*b0d17251Schristos        unsigned char secret[64];
72*b0d17251Schristos        size_t out_len = sizeof(secret);
73*b0d17251Schristos        size_t secret_len = out_len;
74*b0d17251Schristos        unsigned int pad = 1;
75*b0d17251Schristos        OSSL_PARAM params[6];
76*b0d17251Schristos        EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_new_from_pkey(NULL, host_key, NULL);
77*b0d17251Schristos
78*b0d17251Schristos        EVP_PKEY_derive_init(dctx);
79*b0d17251Schristos
80*b0d17251Schristos        params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &pad);
81*b0d17251Schristos        params[1] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_TYPE,
82*b0d17251Schristos                                                     "X963KDF", 0);
83*b0d17251Schristos        params[2] = OSSL_PARAM_construct_utf8_string(OSSL_EXCHANGE_PARAM_KDF_DIGEST,
84*b0d17251Schristos                                                     "SHA1", 0);
85*b0d17251Schristos        params[3] = OSSL_PARAM_construct_size_t(OSSL_EXCHANGE_PARAM_KDF_OUTLEN,
86*b0d17251Schristos                                                &out_len);
87*b0d17251Schristos        params[4] = OSSL_PARAM_construct_octet_string(OSSL_EXCHANGE_PARAM_KDF_UKM,
88*b0d17251Schristos                                                      ukm, ukm_len);
89*b0d17251Schristos        params[5] = OSSL_PARAM_construct_end();
90*b0d17251Schristos        EVP_PKEY_CTX_set_params(dctx, params);
91*b0d17251Schristos
92*b0d17251Schristos        EVP_PKEY_derive_set_peer(dctx, peer_pub_key);
93*b0d17251Schristos        EVP_PKEY_derive(dctx, secret, &secret_len);
94*b0d17251Schristos        ...
95*b0d17251Schristos        OPENSSL_clear_free(secret, secret_len);
96*b0d17251Schristos        EVP_PKEY_CTX_free(dctx);
97*b0d17251Schristos    }
98*b0d17251Schristos
99*b0d17251Schristos=head1 SEE ALSO
100*b0d17251Schristos
101*b0d17251SchristosL<EVP_PKEY-EC(7)>
102*b0d17251SchristosL<EVP_PKEY(3)>,
103*b0d17251SchristosL<provider-keyexch(7)>,
104*b0d17251SchristosL<provider-keymgmt(7)>,
105*b0d17251SchristosL<OSSL_PROVIDER-default(7)>,
106*b0d17251SchristosL<OSSL_PROVIDER-FIPS(7)>,
107*b0d17251Schristos
108*b0d17251Schristos=head1 COPYRIGHT
109*b0d17251Schristos
110*b0d17251SchristosCopyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
111*b0d17251Schristos
112*b0d17251SchristosLicensed under the Apache License 2.0 (the "License").  You may not use
113*b0d17251Schristosthis file except in compliance with the License.  You can obtain a copy
114*b0d17251Schristosin the file LICENSE in the source distribution or at
115*b0d17251SchristosL<https://www.openssl.org/source/license.html>.
116*b0d17251Schristos
117*b0d17251Schristos=cut
118