everest.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Interface to code from Project Everest
  3. *
  4. * Copyright 2016-2018 INRIA and Microsoft Corporation
  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. #include "common.h"
  22. #include <string.h>
  23. #include "mbedtls/ecdh.h"
  24. #include "everest/x25519.h"
  25. #include "everest/everest.h"
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #define mbedtls_calloc calloc
  30. #define mbedtls_free free
  31. #endif
  32. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  33. int mbedtls_everest_setup( mbedtls_ecdh_context_everest *ctx, int grp_id )
  34. {
  35. if( grp_id != MBEDTLS_ECP_DP_CURVE25519 )
  36. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  37. mbedtls_x25519_init( &ctx->ctx );
  38. return 0;
  39. }
  40. void mbedtls_everest_free( mbedtls_ecdh_context_everest *ctx )
  41. {
  42. mbedtls_x25519_free( &ctx->ctx );
  43. }
  44. int mbedtls_everest_make_params( mbedtls_ecdh_context_everest *ctx, size_t *olen,
  45. unsigned char *buf, size_t blen,
  46. int( *f_rng )( void *, unsigned char *, size_t ),
  47. void *p_rng )
  48. {
  49. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  50. return mbedtls_x25519_make_params( x25519_ctx, olen, buf, blen, f_rng, p_rng );
  51. }
  52. int mbedtls_everest_read_params( mbedtls_ecdh_context_everest *ctx,
  53. const unsigned char **buf,
  54. const unsigned char *end )
  55. {
  56. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  57. return mbedtls_x25519_read_params( x25519_ctx, buf, end );
  58. }
  59. int mbedtls_everest_get_params( mbedtls_ecdh_context_everest *ctx,
  60. const mbedtls_ecp_keypair *key,
  61. mbedtls_everest_ecdh_side side )
  62. {
  63. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  64. mbedtls_x25519_ecdh_side s = side == MBEDTLS_EVEREST_ECDH_OURS ?
  65. MBEDTLS_X25519_ECDH_OURS :
  66. MBEDTLS_X25519_ECDH_THEIRS;
  67. return mbedtls_x25519_get_params( x25519_ctx, key, s );
  68. }
  69. int mbedtls_everest_make_public( mbedtls_ecdh_context_everest *ctx, size_t *olen,
  70. unsigned char *buf, size_t blen,
  71. int( *f_rng )( void *, unsigned char *, size_t ),
  72. void *p_rng )
  73. {
  74. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  75. return mbedtls_x25519_make_public( x25519_ctx, olen, buf, blen, f_rng, p_rng );
  76. }
  77. int mbedtls_everest_read_public( mbedtls_ecdh_context_everest *ctx,
  78. const unsigned char *buf, size_t blen )
  79. {
  80. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  81. return mbedtls_x25519_read_public ( x25519_ctx, buf, blen );
  82. }
  83. int mbedtls_everest_calc_secret( mbedtls_ecdh_context_everest *ctx, size_t *olen,
  84. unsigned char *buf, size_t blen,
  85. int( *f_rng )( void *, unsigned char *, size_t ),
  86. void *p_rng )
  87. {
  88. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  89. return mbedtls_x25519_calc_secret( x25519_ctx, olen, buf, blen, f_rng, p_rng );
  90. }
  91. #endif /* MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */