wolfssl_dh.c 4.3 KB

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