wolfssl_dh.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "api.h"
  14. #include "pal.h"
  15. #include "pal_crypto.h"
  16. #include "pal_error.h"
  17. static struct {
  18. uint8_t p[DH_SIZE], q[20], g[DH_SIZE];
  19. } dh_param = {
  20. {
  21. 0xfd, 0x7f, 0x53, 0x81, 0x1d, 0x75, 0x12, 0x29, 0x52, 0xdf, 0x4a, 0x9c, 0x2e, 0xec, 0xe4,
  22. 0xe7, 0xf6, 0x11, 0xb7, 0x52, 0x3c, 0xef, 0x44, 0x00, 0xc3, 0x1e, 0x3f, 0x80, 0xb6, 0x51,
  23. 0x26, 0x69, 0x45, 0x5d, 0x40, 0x22, 0x51, 0xfb, 0x59, 0x3d, 0x8d, 0x58, 0xfa, 0xbf, 0xc5,
  24. 0xf5, 0xba, 0x30, 0xf6, 0xcb, 0x9b, 0x55, 0x6c, 0xd7, 0x81, 0x3b, 0x80, 0x1d, 0x34, 0x6f,
  25. 0xf2, 0x66, 0x60, 0xb7, 0x6b, 0x99, 0x50, 0xa5, 0xa4, 0x9f, 0x9f, 0xe8, 0x04, 0x7b, 0x10,
  26. 0x22, 0xc2, 0x4f, 0xbb, 0xa9, 0xd7, 0xfe, 0xb7, 0xc6, 0x1b, 0xf8, 0x3b, 0x57, 0xe7, 0xc6,
  27. 0xa8, 0xa6, 0x15, 0x0f, 0x04, 0xfb, 0x83, 0xf6, 0xd3, 0xc5, 0x1e, 0xc3, 0x02, 0x35, 0x54,
  28. 0x13, 0x5a, 0x16, 0x91, 0x32, 0xf6, 0x75, 0xf3, 0xae, 0x2b, 0x61, 0xd7, 0x2a, 0xef, 0xf2,
  29. 0x22, 0x03, 0x19, 0x9d, 0xd1, 0x48, 0x01, 0xc7,
  30. },
  31. {
  32. 0x97, 0x60, 0x50, 0x8f, 0x15, 0x23, 0x0b, 0xcc, 0xb2, 0x92,
  33. 0xb9, 0x82, 0xa2, 0xeb, 0x84, 0x0b, 0xf0, 0x58, 0x1c, 0xf5,
  34. },
  35. {
  36. 0xf7, 0xe1, 0xa0, 0x85, 0xd6, 0x9b, 0x3d, 0xde, 0xcb, 0xbc, 0xab, 0x5c, 0x36, 0xb8, 0x57,
  37. 0xb9, 0x79, 0x94, 0xaf, 0xbb, 0xfa, 0x3a, 0xea, 0x82, 0xf9, 0x57, 0x4c, 0x0b, 0x3d, 0x07,
  38. 0x82, 0x67, 0x51, 0x59, 0x57, 0x8e, 0xba, 0xd4, 0x59, 0x4f, 0xe6, 0x71, 0x07, 0x10, 0x81,
  39. 0x80, 0xb4, 0x49, 0x16, 0x71, 0x23, 0xe8, 0x4c, 0x28, 0x16, 0x13, 0xb7, 0xcf, 0x09, 0x32,
  40. 0x8c, 0xc8, 0xa6, 0xe1, 0x3c, 0x16, 0x7a, 0x8b, 0x54, 0x7c, 0x8d, 0x28, 0xe0, 0xa3, 0xae,
  41. 0x1e, 0x2b, 0xb3, 0xa6, 0x75, 0x91, 0x6e, 0xa3, 0x7f, 0x0b, 0xfa, 0x21, 0x35, 0x62, 0xf1,
  42. 0xfb, 0x62, 0x7a, 0x01, 0x24, 0x3b, 0xcc, 0xa4, 0xf1, 0xbe, 0xa8, 0x51, 0x90, 0x89, 0xa8,
  43. 0x83, 0xdf, 0xe1, 0x5a, 0xe5, 0x9f, 0x06, 0x92, 0x8b, 0x66, 0x5e, 0x80, 0x7b, 0x55, 0x25,
  44. 0x64, 0x01, 0x4c, 0x3b, 0xfe, 0xcf, 0x49, 0x2a,
  45. },
  46. };
  47. int lib_DhInit(LIB_DH_CONTEXT* context) {
  48. memset(context, 0, sizeof *context);
  49. InitDhKey(&context->key);
  50. return DhSetKey(&context->key, dh_param.p, sizeof dh_param.p, dh_param.g, sizeof dh_param.g);
  51. }
  52. int lib_DhCreatePublic(LIB_DH_CONTEXT* context, uint8_t* public, uint64_t* _public_size) {
  53. uint32_t public_size;
  54. int ret;
  55. if (*_public_size != DH_SIZE)
  56. return -PAL_ERROR_INVAL;
  57. public_size = (uint32_t)*_public_size;
  58. ret =
  59. DhGenerateKeyPair(&context->key, context->priv, &context->priv_size, public, &public_size);
  60. *_public_size = public_size;
  61. return ret;
  62. }
  63. int lib_DhCalcSecret(LIB_DH_CONTEXT* context, uint8_t* peer, uint64_t peer_size, uint8_t* secret,
  64. uint64_t* _secret_size) {
  65. int ret;
  66. uint32_t secret_size;
  67. if (peer_size > DH_SIZE)
  68. return -PAL_ERROR_INVAL;
  69. if (*_secret_size != DH_SIZE)
  70. return -PAL_ERROR_INVAL;
  71. secret_size = (uint32_t)*_secret_size;
  72. ret = DhAgree(&context->key, secret, &secret_size, context->priv, context->priv_size, peer,
  73. (uint32_t)peer_size);
  74. *_secret_size = secret_size;
  75. return ret;
  76. }
  77. void lib_DhFinal(LIB_DH_CONTEXT* context) {
  78. /* Frees memory associated with the bignums. */
  79. FreeDhKey(&context->key);
  80. /* Clear the buffer to avoid any potential information leaks. */
  81. memset(context, 0, sizeof *context);
  82. }