ngtcp2_crypto.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "ngtcp2_crypto.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "ngtcp2_net.h"
  29. int ngtcp2_crypto_km_new(ngtcp2_crypto_km **pckm, const uint8_t *secret,
  30. size_t secretlen,
  31. const ngtcp2_crypto_aead_ctx *aead_ctx,
  32. const uint8_t *iv, size_t ivlen,
  33. const ngtcp2_mem *mem) {
  34. int rv = ngtcp2_crypto_km_nocopy_new(pckm, secretlen, ivlen, mem);
  35. if (rv != 0) {
  36. return rv;
  37. }
  38. if (secretlen) {
  39. memcpy((*pckm)->secret.base, secret, secretlen);
  40. }
  41. if (aead_ctx) {
  42. (*pckm)->aead_ctx = *aead_ctx;
  43. }
  44. memcpy((*pckm)->iv.base, iv, ivlen);
  45. return 0;
  46. }
  47. int ngtcp2_crypto_km_nocopy_new(ngtcp2_crypto_km **pckm, size_t secretlen,
  48. size_t ivlen, const ngtcp2_mem *mem) {
  49. size_t len;
  50. uint8_t *p;
  51. len = sizeof(ngtcp2_crypto_km) + secretlen + ivlen;
  52. *pckm = ngtcp2_mem_malloc(mem, len);
  53. if (*pckm == NULL) {
  54. return NGTCP2_ERR_NOMEM;
  55. }
  56. p = (uint8_t *)(*pckm) + sizeof(ngtcp2_crypto_km);
  57. (*pckm)->secret.base = p;
  58. (*pckm)->secret.len = secretlen;
  59. p += secretlen;
  60. (*pckm)->iv.base = p;
  61. (*pckm)->iv.len = ivlen;
  62. (*pckm)->aead_ctx.native_handle = NULL;
  63. (*pckm)->pkt_num = -1;
  64. (*pckm)->use_count = 0;
  65. (*pckm)->flags = NGTCP2_CRYPTO_KM_FLAG_NONE;
  66. return 0;
  67. }
  68. void ngtcp2_crypto_km_del(ngtcp2_crypto_km *ckm, const ngtcp2_mem *mem) {
  69. if (ckm == NULL) {
  70. return;
  71. }
  72. if (ckm->secret.len) {
  73. #ifdef WIN32
  74. SecureZeroMemory(ckm->secret.base, ckm->secret.len);
  75. #elif defined(HAVE_EXPLICIT_BZERO)
  76. explicit_bzero(ckm->secret.base, ckm->secret.len);
  77. #elif defined(HAVE_MEMSET_S)
  78. memset_s(ckm->secret.base, ckm->secret.len, 0, ckm->secret.len);
  79. #endif /* defined(HAVE_MEMSET_S) */
  80. }
  81. ngtcp2_mem_free(mem, ckm);
  82. }
  83. void ngtcp2_crypto_create_nonce(uint8_t *dest, const uint8_t *iv, size_t ivlen,
  84. int64_t pkt_num) {
  85. uint64_t n;
  86. assert(ivlen >= sizeof(n));
  87. memcpy(dest, iv, ivlen);
  88. dest += ivlen - sizeof(n);
  89. memcpy(&n, dest, sizeof(n));
  90. n ^= ngtcp2_htonl64((uint64_t)pkt_num);
  91. memcpy(dest, &n, sizeof(n));
  92. }