{
    "content": [
        {
            "type": "text",
            "text": "# EVP_KEYEXCH-DH (man)\n\n## NAME\n\nEVPKEYEXCH-DH - DH Key Exchange algorithm support\n\n## DESCRIPTION\n\nKey exchange support for the DH key type.\n\n## Sections\n\n- **NAME**\n- **DESCRIPTION** (1 subsections)\n- **EXAMPLES**\n- **SEE ALSO**\n- **COPYRIGHT**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "EVP_KEYEXCH-DH",
        "section": "",
        "mode": "man",
        "summary": "EVPKEYEXCH-DH - DH Key Exchange algorithm support",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "The examples assume a host and peer both generate keys using the same named group (or domain",
            "parameters). See \"Examples\" in EVPPKEY-DH(7).  Both the host and peer transfer their public",
            "key to each other.",
            "To convert the peer's generated key pair to a public key in DER format in order to transfer",
            "to the host:",
            "EVPPKEY *peerkey; /* It is assumed this contains the peers generated key */",
            "unsigned char *peerpubder = NULL;",
            "int peerpubderlen;",
            "peerpubderlen = i2dPUBKEY(peerkey, &peerpubder);",
            "...",
            "OPENSSLfree(peerpubder);",
            "To convert the received peer's public key from DER format on the host:",
            "const unsigned char *pd = peerpubder;",
            "EVPPKEY *peerpubkey = d2iPUBKEY(NULL, &pd, peerpubderlen);",
            "...",
            "EVPPKEYfree(peerpubkey);",
            "To derive a shared secret on the host using the host's key and the peer's public key:",
            "/* It is assumed that the hostkey and peerpubkey are set up */",
            "void derivesecret(EVPKEY *hostkey, EVPPKEY *peerpubkey)",
            "unsigned int pad = 1;",
            "OSSLPARAM params[2];",
            "unsigned char *secret = NULL;",
            "sizet secretlen = 0;",
            "EVPPKEYCTX *dctx = EVPPKEYCTXnewfrompkey(NULL, hostkey, NULL);",
            "EVPPKEYderiveinit(dctx);",
            "/* Optionally set the padding */",
            "params[0] = OSSLPARAMconstructuint(OSSLEXCHANGEPARAMPAD, &pad);",
            "params[1] = OSSLPARAMconstructend();",
            "EVPPKEYCTXsetparams(dctx, params);",
            "EVPPKEYderivesetpeer(dctx, peerpubkey);",
            "/* Get the size by passing NULL as the buffer */",
            "EVPPKEYderive(dctx, NULL, &secretlen);",
            "secret = OPENSSLzalloc(secretlen);",
            "EVPPKEYderive(dctx, secret, &secretlen);",
            "...",
            "OPENSSLclearfree(secret, secretlen);",
            "EVPPKEYCTXfree(dctx);",
            "Very similar code can be used by the peer to derive the same shared secret using the host's",
            "public key and the peer's generated key pair."
        ],
        "see_also": [
            {
                "name": "EVPPKEY-DH",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/EVPPKEY-DH/7/json"
            },
            {
                "name": "EVPPKEY-FFC",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/EVPPKEY-FFC/7/json"
            },
            {
                "name": "EVPPKEY",
                "section": "3",
                "url": "https://www.chedong.com/phpMan.php/man/EVPPKEY/3/json"
            },
            {
                "name": "provider-keyexch",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/provider-keyexch/7/json"
            },
            {
                "name": "provider-keymgmt",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/provider-keymgmt/7/json"
            },
            {
                "name": "OSSLPROVIDER-default",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/OSSLPROVIDER-default/7/json"
            },
            {
                "name": "OSSLPROVIDER-FIPS",
                "section": "7",
                "url": "https://www.chedong.com/phpMan.php/man/OSSLPROVIDER-FIPS/7/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 2,
                "subsections": [
                    {
                        "name": "DH key exchange parameters",
                        "lines": 27
                    }
                ]
            },
            {
                "name": "EXAMPLES",
                "lines": 55,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 3,
                "subsections": []
            },
            {
                "name": "COPYRIGHT",
                "lines": 9,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "EVPKEYEXCH-DH - DH Key Exchange algorithm support\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "Key exchange support for the DH key type.\n",
                "subsections": [
                    {
                        "name": "DH key exchange parameters",
                        "content": "\"pad\" (OSSLEXCHANGEPARAMPAD) <unsigned integer>\nSets the padding mode for the associated key exchange ctx.  Setting a value of 1 will\nturn padding on.  Setting a value of 0 will turn padding off.  If padding is off then the\nderived shared secret may be smaller than the largest possible secret size.  If padding\nis on then the derived shared secret will have its first bytes filled with zeros where\nnecessary to make the shared secret the same size as the largest possible secret size.\nThe padding mode parameter is ignored (and padding implicitly enabled) when the KDF type\nis set to \"X942KDF-ASN1\" (OSSLKDFNAMEX942KDFASN1).\n\n\"kdf-type\" (OSSLEXCHANGEPARAMKDFTYPE) <UTF8 string>\nSee \"Common Key Exchange parameters\" in provider-keyexch(7).\n\n\"kdf-digest\" (OSSLEXCHANGEPARAMKDFDIGEST) <UTF8 string>\nSee \"Common Key Exchange parameters\" in provider-keyexch(7).\n\n\"kdf-digest-props\" (OSSLEXCHANGEPARAMKDFDIGESTPROPS) <UTF8 string>\nSee \"Common Key Exchange parameters\" in provider-keyexch(7).\n\n\"kdf-outlen\" (OSSLEXCHANGEPARAMKDFOUTLEN) <unsigned integer>\nSee \"Common Key Exchange parameters\" in provider-keyexch(7).\n\n\"kdf-ukm\" (OSSLEXCHANGEPARAMKDFUKM) <octet string>\nSee \"Common Key Exchange parameters\" in provider-keyexch(7).\n\n\"cekalg\" (OSSLKDFPARAMCEKALG) <octet string ptr>\nSee \"KDF Parameters\" in provider-kdf(7).\n"
                    }
                ]
            },
            "EXAMPLES": {
                "content": "The examples assume a host and peer both generate keys using the same named group (or domain\nparameters). See \"Examples\" in EVPPKEY-DH(7).  Both the host and peer transfer their public\nkey to each other.\n\nTo convert the peer's generated key pair to a public key in DER format in order to transfer\nto the host:\n\nEVPPKEY *peerkey; /* It is assumed this contains the peers generated key */\nunsigned char *peerpubder = NULL;\nint peerpubderlen;\n\npeerpubderlen = i2dPUBKEY(peerkey, &peerpubder);\n...\nOPENSSLfree(peerpubder);\n\nTo convert the received peer's public key from DER format on the host:\n\nconst unsigned char *pd = peerpubder;\nEVPPKEY *peerpubkey = d2iPUBKEY(NULL, &pd, peerpubderlen);\n...\nEVPPKEYfree(peerpubkey);\n\nTo derive a shared secret on the host using the host's key and the peer's public key:\n\n/* It is assumed that the hostkey and peerpubkey are set up */\nvoid derivesecret(EVPKEY *hostkey, EVPPKEY *peerpubkey)\n{\nunsigned int pad = 1;\nOSSLPARAM params[2];\nunsigned char *secret = NULL;\nsizet secretlen = 0;\nEVPPKEYCTX *dctx = EVPPKEYCTXnewfrompkey(NULL, hostkey, NULL);\n\nEVPPKEYderiveinit(dctx);\n\n/* Optionally set the padding */\nparams[0] = OSSLPARAMconstructuint(OSSLEXCHANGEPARAMPAD, &pad);\nparams[1] = OSSLPARAMconstructend();\nEVPPKEYCTXsetparams(dctx, params);\n\nEVPPKEYderivesetpeer(dctx, peerpubkey);\n\n/* Get the size by passing NULL as the buffer */\nEVPPKEYderive(dctx, NULL, &secretlen);\nsecret = OPENSSLzalloc(secretlen);\n\nEVPPKEYderive(dctx, secret, &secretlen);\n...\nOPENSSLclearfree(secret, secretlen);\nEVPPKEYCTXfree(dctx);\n}\n\nVery similar code can be used by the peer to derive the same shared secret using the host's\npublic key and the peer's generated key pair.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "EVPPKEY-DH(7), EVPPKEY-FFC(7), EVPPKEY(3), provider-keyexch(7), provider-keymgmt(7),\nOSSLPROVIDER-default(7), OSSLPROVIDER-FIPS(7),\n",
                "subsections": []
            },
            "COPYRIGHT": {
                "content": "Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.\n\nLicensed under the Apache License 2.0 (the \"License\").  You may not use this file except in\ncompliance with the License.  You can obtain a copy in the file LICENSE in the source\ndistribution or at <https://www.openssl.org/source/license.html>.\n\n\n\n3.0.2                                        2026-06-02                         EVPKEYEXCH-DH(7SSL)",
                "subsections": []
            }
        }
    }
}