x25519.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/evp.h>
  13. /*
  14. * This is a demonstration of key exchange using X25519.
  15. *
  16. * The variables beginning `peer1_` / `peer2_` are data which would normally be
  17. * accessible to that peer.
  18. *
  19. * Ordinarily you would use random keys, which are demonstrated
  20. * below when use_kat=0. A known answer test is demonstrated
  21. * when use_kat=1.
  22. */
  23. /* A property query used for selecting the X25519 implementation. */
  24. static const char *propq = NULL;
  25. static const unsigned char peer1_privk_data[32] = {
  26. 0x80, 0x5b, 0x30, 0x20, 0x25, 0x4a, 0x70, 0x2c,
  27. 0xad, 0xa9, 0x8d, 0x7d, 0x47, 0xf8, 0x1b, 0x20,
  28. 0x89, 0xd2, 0xf9, 0x14, 0xac, 0x92, 0x27, 0xf2,
  29. 0x10, 0x7e, 0xdb, 0x21, 0xbd, 0x73, 0x73, 0x5d
  30. };
  31. static const unsigned char peer2_privk_data[32] = {
  32. 0xf8, 0x84, 0x19, 0x69, 0x79, 0x13, 0x0d, 0xbd,
  33. 0xb1, 0x76, 0xd7, 0x0e, 0x7e, 0x0f, 0xb6, 0xf4,
  34. 0x8c, 0x4a, 0x8c, 0x5f, 0xd8, 0x15, 0x09, 0x0a,
  35. 0x71, 0x78, 0x74, 0x92, 0x0f, 0x85, 0xc8, 0x43
  36. };
  37. static const unsigned char expected_result[32] = {
  38. 0x19, 0x71, 0x26, 0x12, 0x74, 0xb5, 0xb1, 0xce,
  39. 0x77, 0xd0, 0x79, 0x24, 0xb6, 0x0a, 0x5c, 0x72,
  40. 0x0c, 0xa6, 0x56, 0xc0, 0x11, 0xeb, 0x43, 0x11,
  41. 0x94, 0x3b, 0x01, 0x45, 0xca, 0x19, 0xfe, 0x09
  42. };
  43. typedef struct peer_data_st {
  44. const char *name; /* name of peer */
  45. EVP_PKEY *privk; /* privk generated for peer */
  46. unsigned char pubk_data[32]; /* generated pubk to send to other peer */
  47. unsigned char *secret; /* allocated shared secret buffer */
  48. size_t secret_len;
  49. } PEER_DATA;
  50. /*
  51. * Prepare for X25519 key exchange. The public key to be sent to the remote peer
  52. * is put in pubk_data, which should be a 32-byte buffer. Returns 1 on success.
  53. */
  54. static int keyexch_x25519_before(
  55. OSSL_LIB_CTX *libctx,
  56. const unsigned char *kat_privk_data,
  57. PEER_DATA *local_peer)
  58. {
  59. int rv = 0;
  60. size_t pubk_data_len = 0;
  61. /* Generate or load X25519 key for the peer */
  62. if (kat_privk_data != NULL)
  63. local_peer->privk =
  64. EVP_PKEY_new_raw_private_key_ex(libctx, "X25519", propq,
  65. kat_privk_data,
  66. sizeof(peer1_privk_data));
  67. else
  68. local_peer->privk = EVP_PKEY_Q_keygen(libctx, propq, "X25519");
  69. if (local_peer->privk == NULL) {
  70. fprintf(stderr, "Could not load or generate private key\n");
  71. goto end;
  72. }
  73. /* Get public key corresponding to the private key */
  74. if (EVP_PKEY_get_octet_string_param(local_peer->privk,
  75. OSSL_PKEY_PARAM_PUB_KEY,
  76. local_peer->pubk_data,
  77. sizeof(local_peer->pubk_data),
  78. &pubk_data_len) == 0) {
  79. fprintf(stderr, "EVP_PKEY_get_octet_string_param() failed\n");
  80. goto end;
  81. }
  82. /* X25519 public keys are always 32 bytes */
  83. if (pubk_data_len != 32) {
  84. fprintf(stderr, "EVP_PKEY_get_octet_string_param() "
  85. "yielded wrong length\n");
  86. goto end;
  87. }
  88. rv = 1;
  89. end:
  90. if (rv == 0) {
  91. EVP_PKEY_free(local_peer->privk);
  92. local_peer->privk = NULL;
  93. }
  94. return rv;
  95. }
  96. /*
  97. * Complete X25519 key exchange. remote_peer_pubk_data should be the 32 byte
  98. * public key value received from the remote peer. On success, returns 1 and the
  99. * secret is pointed to by *secret. The caller must free it.
  100. */
  101. static int keyexch_x25519_after(
  102. OSSL_LIB_CTX *libctx,
  103. int use_kat,
  104. PEER_DATA *local_peer,
  105. const unsigned char *remote_peer_pubk_data)
  106. {
  107. int rv = 0;
  108. EVP_PKEY *remote_peer_pubk = NULL;
  109. EVP_PKEY_CTX *ctx = NULL;
  110. local_peer->secret = NULL;
  111. /* Load public key for remote peer. */
  112. remote_peer_pubk =
  113. EVP_PKEY_new_raw_public_key_ex(libctx, "X25519", propq,
  114. remote_peer_pubk_data, 32);
  115. if (remote_peer_pubk == NULL) {
  116. fprintf(stderr, "EVP_PKEY_new_raw_public_key_ex() failed\n");
  117. goto end;
  118. }
  119. /* Create key exchange context. */
  120. ctx = EVP_PKEY_CTX_new_from_pkey(libctx, local_peer->privk, propq);
  121. if (ctx == NULL) {
  122. fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
  123. goto end;
  124. }
  125. /* Initialize derivation process. */
  126. if (EVP_PKEY_derive_init(ctx) == 0) {
  127. fprintf(stderr, "EVP_PKEY_derive_init() failed\n");
  128. goto end;
  129. }
  130. /* Configure each peer with the other peer's public key. */
  131. if (EVP_PKEY_derive_set_peer(ctx, remote_peer_pubk) == 0) {
  132. fprintf(stderr, "EVP_PKEY_derive_set_peer() failed\n");
  133. goto end;
  134. }
  135. /* Determine the secret length. */
  136. if (EVP_PKEY_derive(ctx, NULL, &local_peer->secret_len) == 0) {
  137. fprintf(stderr, "EVP_PKEY_derive() failed\n");
  138. goto end;
  139. }
  140. /*
  141. * We are using X25519, so the secret generated will always be 32 bytes.
  142. * However for exposition, the code below demonstrates a generic
  143. * implementation for arbitrary lengths.
  144. */
  145. if (local_peer->secret_len != 32) { /* unreachable */
  146. fprintf(stderr, "Secret is always 32 bytes for X25519\n");
  147. goto end;
  148. }
  149. /* Allocate memory for shared secrets. */
  150. local_peer->secret = OPENSSL_malloc(local_peer->secret_len);
  151. if (local_peer->secret == NULL) {
  152. fprintf(stderr, "Could not allocate memory for secret\n");
  153. goto end;
  154. }
  155. /* Derive the shared secret. */
  156. if (EVP_PKEY_derive(ctx, local_peer->secret,
  157. &local_peer->secret_len) == 0) {
  158. fprintf(stderr, "EVP_PKEY_derive() failed\n");
  159. goto end;
  160. }
  161. printf("Shared secret (%s):\n", local_peer->name);
  162. BIO_dump_indent_fp(stdout, local_peer->secret, local_peer->secret_len, 2);
  163. putchar('\n');
  164. rv = 1;
  165. end:
  166. EVP_PKEY_CTX_free(ctx);
  167. EVP_PKEY_free(remote_peer_pubk);
  168. if (rv == 0) {
  169. OPENSSL_clear_free(local_peer->secret, local_peer->secret_len);
  170. local_peer->secret = NULL;
  171. }
  172. return rv;
  173. }
  174. static int keyexch_x25519(int use_kat)
  175. {
  176. int rv = 0;
  177. OSSL_LIB_CTX *libctx = NULL;
  178. PEER_DATA peer1 = {"peer 1"}, peer2 = {"peer 2"};
  179. /*
  180. * Each peer generates its private key and sends its public key
  181. * to the other peer. The private key is stored locally for
  182. * later use.
  183. */
  184. if (keyexch_x25519_before(libctx, use_kat ? peer1_privk_data : NULL,
  185. &peer1) == 0)
  186. return 0;
  187. if (keyexch_x25519_before(libctx, use_kat ? peer2_privk_data : NULL,
  188. &peer2) == 0)
  189. return 0;
  190. /*
  191. * Each peer uses the other peer's public key to perform key exchange.
  192. * After this succeeds, each peer has the same secret in its
  193. * PEER_DATA.
  194. */
  195. if (keyexch_x25519_after(libctx, use_kat, &peer1, peer2.pubk_data) == 0)
  196. return 0;
  197. if (keyexch_x25519_after(libctx, use_kat, &peer2, peer1.pubk_data) == 0)
  198. return 0;
  199. /*
  200. * Here we demonstrate the secrets are equal for exposition purposes.
  201. *
  202. * Although in practice you will generally not need to compare secrets
  203. * produced through key exchange, if you do compare cryptographic secrets,
  204. * always do so using a constant-time function such as CRYPTO_memcmp, never
  205. * using memcmp(3).
  206. */
  207. if (CRYPTO_memcmp(peer1.secret, peer2.secret, peer1.secret_len) != 0) {
  208. fprintf(stderr, "Negotiated secrets do not match\n");
  209. goto end;
  210. }
  211. /* If we are doing the KAT, the secret should equal our reference result. */
  212. if (use_kat && CRYPTO_memcmp(peer1.secret, expected_result,
  213. peer1.secret_len) != 0) {
  214. fprintf(stderr, "Did not get expected result\n");
  215. goto end;
  216. }
  217. rv = 1;
  218. end:
  219. /* The secrets are sensitive, so ensure they are erased before freeing. */
  220. OPENSSL_clear_free(peer1.secret, peer1.secret_len);
  221. OPENSSL_clear_free(peer2.secret, peer2.secret_len);
  222. EVP_PKEY_free(peer1.privk);
  223. EVP_PKEY_free(peer2.privk);
  224. OSSL_LIB_CTX_free(libctx);
  225. return rv;
  226. }
  227. int main(int argc, char **argv)
  228. {
  229. /* Test X25519 key exchange with known result. */
  230. printf("Key exchange using known answer (deterministic):\n");
  231. if (keyexch_x25519(1) == 0)
  232. return 1;
  233. /* Test X25519 key exchange with random keys. */
  234. printf("Key exchange using random keys:\n");
  235. if (keyexch_x25519(0) == 0)
  236. return 1;
  237. return 0;
  238. }