everest.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "mbedtls/platform.h"
  27. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  28. int mbedtls_everest_setup( mbedtls_ecdh_context_everest *ctx, int grp_id )
  29. {
  30. if( grp_id != MBEDTLS_ECP_DP_CURVE25519 )
  31. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  32. mbedtls_x25519_init( &ctx->ctx );
  33. return 0;
  34. }
  35. void mbedtls_everest_free( mbedtls_ecdh_context_everest *ctx )
  36. {
  37. mbedtls_x25519_free( &ctx->ctx );
  38. }
  39. int mbedtls_everest_make_params( mbedtls_ecdh_context_everest *ctx, size_t *olen,
  40. unsigned char *buf, size_t blen,
  41. int( *f_rng )( void *, unsigned char *, size_t ),
  42. void *p_rng )
  43. {
  44. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  45. return mbedtls_x25519_make_params( x25519_ctx, olen, buf, blen, f_rng, p_rng );
  46. }
  47. int mbedtls_everest_read_params( mbedtls_ecdh_context_everest *ctx,
  48. const unsigned char **buf,
  49. const unsigned char *end )
  50. {
  51. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  52. return mbedtls_x25519_read_params( x25519_ctx, buf, end );
  53. }
  54. int mbedtls_everest_get_params( mbedtls_ecdh_context_everest *ctx,
  55. const mbedtls_ecp_keypair *key,
  56. mbedtls_everest_ecdh_side side )
  57. {
  58. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  59. mbedtls_x25519_ecdh_side s = side == MBEDTLS_EVEREST_ECDH_OURS ?
  60. MBEDTLS_X25519_ECDH_OURS :
  61. MBEDTLS_X25519_ECDH_THEIRS;
  62. return mbedtls_x25519_get_params( x25519_ctx, key, s );
  63. }
  64. int mbedtls_everest_make_public( mbedtls_ecdh_context_everest *ctx, size_t *olen,
  65. unsigned char *buf, size_t blen,
  66. int( *f_rng )( void *, unsigned char *, size_t ),
  67. void *p_rng )
  68. {
  69. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  70. return mbedtls_x25519_make_public( x25519_ctx, olen, buf, blen, f_rng, p_rng );
  71. }
  72. int mbedtls_everest_read_public( mbedtls_ecdh_context_everest *ctx,
  73. const unsigned char *buf, size_t blen )
  74. {
  75. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  76. return mbedtls_x25519_read_public ( x25519_ctx, buf, blen );
  77. }
  78. int mbedtls_everest_calc_secret( mbedtls_ecdh_context_everest *ctx, size_t *olen,
  79. unsigned char *buf, size_t blen,
  80. int( *f_rng )( void *, unsigned char *, size_t ),
  81. void *p_rng )
  82. {
  83. mbedtls_x25519_context *x25519_ctx = &ctx->ctx;
  84. return mbedtls_x25519_calc_secret( x25519_ctx, olen, buf, blen, f_rng, p_rng );
  85. }
  86. #endif /* MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED */