ecdh_curve25519.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Example ECDHE with Curve25519 program
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if !defined(MBEDTLS_ECDH_C) || \
  22. !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
  23. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
  24. int main(void)
  25. {
  26. mbedtls_printf("MBEDTLS_ECDH_C and/or "
  27. "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
  28. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
  29. "not defined\n");
  30. mbedtls_exit(0);
  31. }
  32. #else
  33. #include "mbedtls/entropy.h"
  34. #include "mbedtls/ctr_drbg.h"
  35. #include "mbedtls/ecdh.h"
  36. #include <string.h>
  37. int main(int argc, char *argv[])
  38. {
  39. int ret = 1;
  40. int exit_code = MBEDTLS_EXIT_FAILURE;
  41. mbedtls_ecdh_context ctx_cli, ctx_srv;
  42. mbedtls_entropy_context entropy;
  43. mbedtls_ctr_drbg_context ctr_drbg;
  44. unsigned char cli_to_srv[36], srv_to_cli[33];
  45. const char pers[] = "ecdh";
  46. size_t srv_olen;
  47. size_t cli_olen;
  48. unsigned char secret_cli[32] = { 0 };
  49. unsigned char secret_srv[32] = { 0 };
  50. const unsigned char *p_cli_to_srv = cli_to_srv;
  51. ((void) argc);
  52. ((void) argv);
  53. mbedtls_ecdh_init(&ctx_cli);
  54. mbedtls_ecdh_init(&ctx_srv);
  55. mbedtls_ctr_drbg_init(&ctr_drbg);
  56. /*
  57. * Initialize random number generation
  58. */
  59. mbedtls_printf(" . Seed the random number generator...");
  60. fflush(stdout);
  61. mbedtls_entropy_init(&entropy);
  62. if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
  63. &entropy,
  64. (const unsigned char *) pers,
  65. sizeof(pers))) != 0) {
  66. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
  67. ret);
  68. goto exit;
  69. }
  70. mbedtls_printf(" ok\n");
  71. /*
  72. * Client: initialize context and generate keypair
  73. */
  74. mbedtls_printf(" . Set up client context, generate EC key pair...");
  75. fflush(stdout);
  76. ret = mbedtls_ecdh_setup(&ctx_cli, MBEDTLS_ECP_DP_CURVE25519);
  77. if (ret != 0) {
  78. mbedtls_printf(" failed\n ! mbedtls_ecdh_setup returned %d\n", ret);
  79. goto exit;
  80. }
  81. ret = mbedtls_ecdh_make_params(&ctx_cli, &cli_olen, cli_to_srv,
  82. sizeof(cli_to_srv),
  83. mbedtls_ctr_drbg_random, &ctr_drbg);
  84. if (ret != 0) {
  85. mbedtls_printf(" failed\n ! mbedtls_ecdh_make_params returned %d\n",
  86. ret);
  87. goto exit;
  88. }
  89. mbedtls_printf(" ok\n");
  90. /*
  91. * Server: initialize context and generate keypair
  92. */
  93. mbedtls_printf(" . Server: read params, generate public key...");
  94. fflush(stdout);
  95. ret = mbedtls_ecdh_read_params(&ctx_srv, &p_cli_to_srv,
  96. p_cli_to_srv + sizeof(cli_to_srv));
  97. if (ret != 0) {
  98. mbedtls_printf(" failed\n ! mbedtls_ecdh_read_params returned %d\n",
  99. ret);
  100. goto exit;
  101. }
  102. ret = mbedtls_ecdh_make_public(&ctx_srv, &srv_olen, srv_to_cli,
  103. sizeof(srv_to_cli),
  104. mbedtls_ctr_drbg_random, &ctr_drbg);
  105. if (ret != 0) {
  106. mbedtls_printf(" failed\n ! mbedtls_ecdh_make_public returned %d\n",
  107. ret);
  108. goto exit;
  109. }
  110. mbedtls_printf(" ok\n");
  111. /*
  112. * Client: read public key
  113. */
  114. mbedtls_printf(" . Client: read public key...");
  115. fflush(stdout);
  116. ret = mbedtls_ecdh_read_public(&ctx_cli, srv_to_cli,
  117. sizeof(srv_to_cli));
  118. if (ret != 0) {
  119. mbedtls_printf(" failed\n ! mbedtls_ecdh_read_public returned %d\n",
  120. ret);
  121. goto exit;
  122. }
  123. mbedtls_printf(" ok\n");
  124. /*
  125. * Calculate secrets
  126. */
  127. mbedtls_printf(" . Calculate secrets...");
  128. fflush(stdout);
  129. ret = mbedtls_ecdh_calc_secret(&ctx_cli, &cli_olen, secret_cli,
  130. sizeof(secret_cli),
  131. mbedtls_ctr_drbg_random, &ctr_drbg);
  132. if (ret != 0) {
  133. mbedtls_printf(" failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
  134. ret);
  135. goto exit;
  136. }
  137. ret = mbedtls_ecdh_calc_secret(&ctx_srv, &srv_olen, secret_srv,
  138. sizeof(secret_srv),
  139. mbedtls_ctr_drbg_random, &ctr_drbg);
  140. if (ret != 0) {
  141. mbedtls_printf(" failed\n ! mbedtls_ecdh_calc_secret returned %d\n",
  142. ret);
  143. goto exit;
  144. }
  145. mbedtls_printf(" ok\n");
  146. /*
  147. * Verification: are the computed secrets equal?
  148. */
  149. mbedtls_printf(" . Check if both calculated secrets are equal...");
  150. fflush(stdout);
  151. ret = memcmp(secret_srv, secret_cli, srv_olen);
  152. if (ret != 0 || (cli_olen != srv_olen)) {
  153. mbedtls_printf(" failed\n ! Shared secrets not equal.\n");
  154. goto exit;
  155. }
  156. mbedtls_printf(" ok\n");
  157. exit_code = MBEDTLS_EXIT_SUCCESS;
  158. exit:
  159. mbedtls_ecdh_free(&ctx_srv);
  160. mbedtls_ecdh_free(&ctx_cli);
  161. mbedtls_ctr_drbg_free(&ctr_drbg);
  162. mbedtls_entropy_free(&entropy);
  163. mbedtls_exit(exit_code);
  164. }
  165. #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
  166. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */