ecdh_curve25519.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #if defined(MBEDTLS_PLATFORM_C)
  25. #include "mbedtls/platform.h"
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define mbedtls_printf printf
  30. #define mbedtls_exit exit
  31. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  32. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  33. #endif /* MBEDTLS_PLATFORM_C */
  34. #if !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDH_LEGACY_CONTEXT) || \
  35. !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
  36. !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
  37. int main( void )
  38. {
  39. mbedtls_printf( "MBEDTLS_ECDH_C and/or MBEDTLS_ECDH_LEGACY_CONTEXT and/or "
  40. "MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
  41. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
  42. "not defined\n" );
  43. mbedtls_exit( 0 );
  44. }
  45. #else
  46. #include "mbedtls/entropy.h"
  47. #include "mbedtls/ctr_drbg.h"
  48. #include "mbedtls/ecdh.h"
  49. int main( int argc, char *argv[] )
  50. {
  51. int ret = 1;
  52. int exit_code = MBEDTLS_EXIT_FAILURE;
  53. mbedtls_ecdh_context ctx_cli, ctx_srv;
  54. mbedtls_entropy_context entropy;
  55. mbedtls_ctr_drbg_context ctr_drbg;
  56. unsigned char cli_to_srv[32], srv_to_cli[32];
  57. const char pers[] = "ecdh";
  58. ((void) argc);
  59. ((void) argv);
  60. mbedtls_ecdh_init( &ctx_cli );
  61. mbedtls_ecdh_init( &ctx_srv );
  62. mbedtls_ctr_drbg_init( &ctr_drbg );
  63. /*
  64. * Initialize random number generation
  65. */
  66. mbedtls_printf( " . Seeding the random number generator..." );
  67. fflush( stdout );
  68. mbedtls_entropy_init( &entropy );
  69. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  70. (const unsigned char *) pers,
  71. sizeof pers ) ) != 0 )
  72. {
  73. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  74. goto exit;
  75. }
  76. mbedtls_printf( " ok\n" );
  77. /*
  78. * Client: initialize context and generate keypair
  79. */
  80. mbedtls_printf( " . Setting up client context..." );
  81. fflush( stdout );
  82. ret = mbedtls_ecp_group_load( &ctx_cli.grp, MBEDTLS_ECP_DP_CURVE25519 );
  83. if( ret != 0 )
  84. {
  85. mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
  86. goto exit;
  87. }
  88. ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, &ctx_cli.d, &ctx_cli.Q,
  89. mbedtls_ctr_drbg_random, &ctr_drbg );
  90. if( ret != 0 )
  91. {
  92. mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
  93. goto exit;
  94. }
  95. ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, cli_to_srv, 32 );
  96. if( ret != 0 )
  97. {
  98. mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
  99. goto exit;
  100. }
  101. mbedtls_printf( " ok\n" );
  102. /*
  103. * Server: initialize context and generate keypair
  104. */
  105. mbedtls_printf( " . Setting up server context..." );
  106. fflush( stdout );
  107. ret = mbedtls_ecp_group_load( &ctx_srv.grp, MBEDTLS_ECP_DP_CURVE25519 );
  108. if( ret != 0 )
  109. {
  110. mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
  111. goto exit;
  112. }
  113. ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q,
  114. mbedtls_ctr_drbg_random, &ctr_drbg );
  115. if( ret != 0 )
  116. {
  117. mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
  118. goto exit;
  119. }
  120. ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli, 32 );
  121. if( ret != 0 )
  122. {
  123. mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
  124. goto exit;
  125. }
  126. mbedtls_printf( " ok\n" );
  127. /*
  128. * Server: read peer's key and generate shared secret
  129. */
  130. mbedtls_printf( " . Server reading client key and computing secret..." );
  131. fflush( stdout );
  132. ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, 1 );
  133. if( ret != 0 )
  134. {
  135. mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
  136. goto exit;
  137. }
  138. ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, cli_to_srv, 32 );
  139. if( ret != 0 )
  140. {
  141. mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
  142. goto exit;
  143. }
  144. ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, &ctx_srv.z,
  145. &ctx_srv.Qp, &ctx_srv.d,
  146. mbedtls_ctr_drbg_random, &ctr_drbg );
  147. if( ret != 0 )
  148. {
  149. mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
  150. goto exit;
  151. }
  152. mbedtls_printf( " ok\n" );
  153. /*
  154. * Client: read peer's key and generate shared secret
  155. */
  156. mbedtls_printf( " . Client reading server key and computing secret..." );
  157. fflush( stdout );
  158. ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 );
  159. if( ret != 0 )
  160. {
  161. mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
  162. goto exit;
  163. }
  164. ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli, 32 );
  165. if( ret != 0 )
  166. {
  167. mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
  168. goto exit;
  169. }
  170. ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z,
  171. &ctx_cli.Qp, &ctx_cli.d,
  172. mbedtls_ctr_drbg_random, &ctr_drbg );
  173. if( ret != 0 )
  174. {
  175. mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
  176. goto exit;
  177. }
  178. mbedtls_printf( " ok\n" );
  179. /*
  180. * Verification: are the computed secrets equal?
  181. */
  182. mbedtls_printf( " . Checking if both computed secrets are equal..." );
  183. fflush( stdout );
  184. ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z );
  185. if( ret != 0 )
  186. {
  187. mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
  188. goto exit;
  189. }
  190. mbedtls_printf( " ok\n" );
  191. exit_code = MBEDTLS_EXIT_SUCCESS;
  192. exit:
  193. #if defined(_WIN32)
  194. mbedtls_printf( " + Press Enter to exit this program.\n" );
  195. fflush( stdout ); getchar();
  196. #endif
  197. mbedtls_ecdh_free( &ctx_srv );
  198. mbedtls_ecdh_free( &ctx_cli );
  199. mbedtls_ctr_drbg_free( &ctr_drbg );
  200. mbedtls_entropy_free( &entropy );
  201. mbedtls_exit( exit_code );
  202. }
  203. #endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
  204. MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */