mbedtls_dh.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (C) 2017 Fortanix, Inc.
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <errno.h>
  14. #include <limits.h>
  15. #include "api.h"
  16. #include "assert.h"
  17. #include "mbedtls_adapter.h"
  18. #include "pal.h"
  19. #include "pal_crypto.h"
  20. #include "pal_debug.h"
  21. #include "pal_error.h"
  22. #define BITS_PER_BYTE 8
  23. /* This is declared in pal_internal.h, but that can't be included here. */
  24. size_t _DkRandomBitsRead(void* buffer, size_t size);
  25. /* Wrapper to provide mbedtls the RNG interface it expects. It passes an
  26. * extra context parameter, and expects a return value of 0 for success
  27. * and nonzero for failure. */
  28. static int RandomWrapper(void* private, unsigned char* data, size_t size) {
  29. __UNUSED(private);
  30. return _DkRandomBitsRead(data, size);
  31. }
  32. int lib_DhInit(LIB_DH_CONTEXT* context) {
  33. int ret;
  34. mbedtls_dhm_init(context);
  35. /* Configure parameters. Note that custom Diffie-Hellman parameters
  36. * are considered more secure, but require more data be exchanged
  37. * between the two parties to establish the parameters, so we haven't
  38. * implemented that yet. */
  39. ret = mbedtls_mpi_read_string(&context->P, 16 /* radix */, MBEDTLS_DHM_RFC3526_MODP_2048_P);
  40. if (ret != 0) {
  41. pal_printf("D-H initialization failed: %d\n", ret);
  42. return mbedtls_to_pal_error(ret);
  43. }
  44. ret = mbedtls_mpi_read_string(&context->G, 16 /* radix */, MBEDTLS_DHM_RFC3526_MODP_2048_G);
  45. if (ret != 0) {
  46. pal_printf("D-H initialization failed: %d\n", ret);
  47. return mbedtls_to_pal_error(ret);
  48. }
  49. context->len = mbedtls_mpi_size(&context->P);
  50. return 0;
  51. }
  52. int lib_DhCreatePublic(LIB_DH_CONTEXT* context, uint8_t* public, uint64_t* public_size) {
  53. int ret;
  54. if (*public_size != DH_SIZE)
  55. return -PAL_ERROR_INVAL;
  56. /* The RNG here is used to generate secret exponent X. */
  57. ret = mbedtls_dhm_make_public(context, context->len, public, *public_size, RandomWrapper, NULL);
  58. if (ret != 0)
  59. return mbedtls_to_pal_error(ret);
  60. /* mbedtls writes leading zeros in the big-endian output to pad to
  61. * public_size, so leave caller's public_size unchanged */
  62. return 0;
  63. }
  64. int lib_DhCalcSecret(LIB_DH_CONTEXT* context, uint8_t* peer, uint64_t peer_size, uint8_t* secret,
  65. uint64_t* secret_size) {
  66. int ret;
  67. if (*secret_size != DH_SIZE)
  68. return -PAL_ERROR_INVAL;
  69. ret = mbedtls_dhm_read_public(context, peer, peer_size);
  70. if (ret != 0)
  71. return mbedtls_to_pal_error(ret);
  72. /* The RNG here is used for blinding against timing attacks if X is
  73. * reused and not used otherwise. mbedtls recommends always passing
  74. * in an RNG. */
  75. ret = mbedtls_dhm_calc_secret(context, secret, *secret_size, secret_size, RandomWrapper, NULL);
  76. return mbedtls_to_pal_error(ret);
  77. }
  78. void lib_DhFinal(LIB_DH_CONTEXT* context) {
  79. /* This call zeros out context for us. */
  80. mbedtls_dhm_free(context);
  81. }