ecdh_curve25519.c 6.7 KB

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