x25519.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include <stdlib.h>
  23. #include "curve25519.h"
  24. #include "picotls.h"
  25. #include "picotls/minicrypto.h"
  26. #define X25519_KEY_SIZE 32
  27. struct st_x25519_key_exchange_t {
  28. ptls_key_exchange_context_t super;
  29. uint8_t priv[X25519_KEY_SIZE];
  30. uint8_t pub[X25519_KEY_SIZE];
  31. };
  32. static void x25519_create_keypair(uint8_t *priv, uint8_t *pub)
  33. {
  34. ptls_minicrypto_random_bytes(priv, X25519_KEY_SIZE);
  35. cf_curve25519_mul_base(pub, priv);
  36. }
  37. static int x25519_derive_secret(ptls_iovec_t *secret, const uint8_t *clientpriv, const uint8_t *clientpub,
  38. const uint8_t *serverpriv, const uint8_t *serverpub)
  39. {
  40. if ((secret->base = malloc(X25519_KEY_SIZE)) == NULL)
  41. return PTLS_ERROR_NO_MEMORY;
  42. cf_curve25519_mul(secret->base, clientpriv != NULL ? clientpriv : serverpriv, clientpriv != NULL ? serverpub : clientpub);
  43. static const uint8_t zeros[X25519_KEY_SIZE] = {0};
  44. if (ptls_mem_equal(secret->base, zeros, sizeof(zeros))) {
  45. free(secret->base);
  46. secret->base = NULL;
  47. return PTLS_ERROR_INCOMPATIBLE_KEY;
  48. }
  49. secret->len = X25519_KEY_SIZE;
  50. return 0;
  51. }
  52. static int x25519_on_exchange(ptls_key_exchange_context_t **_ctx, int release, ptls_iovec_t *secret, ptls_iovec_t peerkey)
  53. {
  54. struct st_x25519_key_exchange_t *ctx = (struct st_x25519_key_exchange_t *)*_ctx;
  55. int ret;
  56. if (secret == NULL) {
  57. ret = 0;
  58. goto Exit;
  59. }
  60. if (peerkey.len != X25519_KEY_SIZE) {
  61. ret = PTLS_ALERT_DECRYPT_ERROR;
  62. goto Exit;
  63. }
  64. ret = x25519_derive_secret(secret, ctx->priv, ctx->pub, NULL, peerkey.base);
  65. Exit:
  66. if (release) {
  67. ptls_clear_memory(ctx->priv, sizeof(ctx->priv));
  68. free(ctx);
  69. *_ctx = NULL;
  70. }
  71. return ret;
  72. }
  73. static int x25519_create_key_exchange(ptls_key_exchange_algorithm_t *algo, ptls_key_exchange_context_t **_ctx)
  74. {
  75. struct st_x25519_key_exchange_t *ctx;
  76. if ((ctx = (struct st_x25519_key_exchange_t *)malloc(sizeof(*ctx))) == NULL)
  77. return PTLS_ERROR_NO_MEMORY;
  78. ctx->super = (ptls_key_exchange_context_t){algo, ptls_iovec_init(ctx->pub, sizeof(ctx->pub)), x25519_on_exchange};
  79. x25519_create_keypair(ctx->priv, ctx->pub);
  80. *_ctx = &ctx->super;
  81. return 0;
  82. }
  83. static int x25519_key_exchange(ptls_key_exchange_algorithm_t *algo, ptls_iovec_t *pubkey, ptls_iovec_t *secret,
  84. ptls_iovec_t peerkey)
  85. {
  86. uint8_t priv[X25519_KEY_SIZE], *pub = NULL;
  87. int ret;
  88. if (peerkey.len != X25519_KEY_SIZE) {
  89. ret = PTLS_ALERT_DECRYPT_ERROR;
  90. goto Exit;
  91. }
  92. if ((pub = malloc(X25519_KEY_SIZE)) == NULL) {
  93. ret = PTLS_ERROR_NO_MEMORY;
  94. goto Exit;
  95. }
  96. x25519_create_keypair(priv, pub);
  97. if ((ret = x25519_derive_secret(secret, NULL, peerkey.base, priv, pub)) != 0)
  98. goto Exit;
  99. *pubkey = ptls_iovec_init(pub, X25519_KEY_SIZE);
  100. ret = 0;
  101. Exit:
  102. ptls_clear_memory(priv, sizeof(priv));
  103. if (pub != NULL && ret != 0) {
  104. ptls_clear_memory(pub, X25519_KEY_SIZE);
  105. free(pub);
  106. }
  107. return ret;
  108. }
  109. ptls_key_exchange_algorithm_t ptls_minicrypto_x25519 = {
  110. .id = PTLS_GROUP_X25519, .name = PTLS_GROUP_NAME_X25519, .create = x25519_create_key_exchange, .exchange = x25519_key_exchange};