5 EVP_KDF-SSHKDF - The SSHKDF EVP_KDF implementation
9 Support for computing the B<SSHKDF> KDF through the B<EVP_KDF> API.
11 The EVP_KDF-SSHKDF algorithm implements the SSHKDF key derivation function.
12 It is defined in RFC 4253, section 7.2 and is used by SSH to derive IVs,
13 encryption keys and integrity keys.
14 Five inputs are required to perform key derivation: The hashing function
15 (for example SHA256), the Initial Key, the Exchange Hash, the Session ID,
16 and the derivation key type.
20 "SSHKDF" is the name for this implementation; it
21 can be used with the EVP_KDF_fetch() function.
23 =head2 Supported parameters
25 The supported parameters are:
29 =item "properties" (B<OSSL_KDF_PARAM_PROPERTIES>) <UTF8 string>
31 =item "digest" (B<OSSL_KDF_PARAM_DIGEST>) <UTF8 string>
33 =item "key" (B<OSSL_KDF_PARAM_KEY>) <octet string>
35 These parameters work as described in L<EVP_KDF(3)/PARAMETERS>.
37 =item "xcghash" (B<OSSL_KDF_PARAM_SSHKDF_XCGHASH>) <octet string>
39 =item "session_id" (B<OSSL_KDF_PARAM_SSHKDF_SESSION_ID>) <octet string>
41 These parameters set the respective values for the KDF.
42 If a value is already set, the contents are replaced.
44 =item "type" (B<OSSL_KDF_PARAM_SSHKDF_TYPE>) <UTF8 string>
46 This parameter sets the type for the SSHKDF operation.
47 There are six supported types:
51 =item EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV
53 The Initial IV from client to server.
54 Char array initializer of value {65, 0}, i.e., ASCII string "A".
56 =item EVP_KDF_SSHKDF_TYPE_INITIAL_IV_SRV_TO_CLI
58 The Initial IV from server to client
59 Char array initializer of value {66, 0}, i.e., ASCII string "B".
61 =item EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_CLI_TO_SRV
63 The Encryption Key from client to server
64 Char array initializer of value {67, 0}, i.e., ASCII string "C".
66 =item EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_SRV_TO_CLI
68 The Encryption Key from server to client
69 Char array initializer of value {68, 0}, i.e., ASCII string "D".
71 =item EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_CLI_TO_SRV
73 The Integrity Key from client to server
74 Char array initializer of value {69, 0}, i.e., ASCII string "E".
76 =item EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_SRV_TO_CLI
78 The Integrity Key from client to server
79 Char array initializer of value {70, 0}, i.e., ASCII string "F".
87 A context for SSHKDF can be obtained by calling:
89 EVP_KDF *kdf = EVP_KDF_fetch(NULL, "SSHKDF", NULL);
90 EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);
92 The output length of the SSHKDF derivation is specified via the I<keylen>
93 parameter to the L<EVP_KDF_derive(3)> function.
94 Since the SSHKDF output length is variable, calling L<EVP_KDF_CTX_get_kdf_size(3)>
95 to obtain the requisite length is not meaningful. The caller must
96 allocate a buffer of the desired length, and pass that buffer to the
97 L<EVP_KDF_derive(3)> function along with the desired length.
101 This example derives an 8 byte IV using SHA-256 with a 1K "key" and appropriate
102 "xcghash" and "session_id" values:
106 const char type[] = EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV;
107 unsigned char key[1024] = "01234...";
108 unsigned char xcghash[32] = "012345...";
109 unsigned char session_id[32] = "012345...";
110 unsigned char out[8];
111 size_t outlen = sizeof(out);
112 OSSL_PARAM params[6], *p = params;
114 kdf = EVP_KDF_fetch(NULL, "SSHKDF", NULL);
115 kctx = EVP_KDF_CTX_new(kdf);
118 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
119 SN_sha256, strlen(SN_sha256));
120 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
122 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH,
123 xcghash, (size_t)32);
124 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
125 session_id, (size_t)32);
126 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE,
127 type, sizeof(type) - 1);
128 *p = OSSL_PARAM_construct_end();
129 if (EVP_KDF_CTX_set_params(kctx, params) <= 0)
132 if (EVP_KDF_derive(kctx, out, &outlen) <= 0)
143 L<EVP_KDF_CTX_new(3)>,
144 L<EVP_KDF_CTX_free(3)>,
145 L<EVP_KDF_CTX_set_params(3)>,
146 L<EVP_KDF_CTX_get_kdf_size(3)>,
147 L<EVP_KDF_derive(3)>,
148 L<EVP_KDF(3)/PARAMETERS>
152 Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
154 Licensed under the Apache License 2.0 (the "License"). You may not use
155 this file except in compliance with the License. You can obtain a copy
156 in the file LICENSE in the source distribution or at
157 L<https://www.openssl.org/source/license.html>.