You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keypairs_cache.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "keypairs_cache.h"
  18. #include "keypair_private.h"
  19. #include "libutil/util.h"
  20. #include "hash.h"
  21. struct rspamd_keypair_elt {
  22. struct rspamd_cryptobox_nm *nm;
  23. guchar pair[rspamd_cryptobox_HASHBYTES * 2];
  24. };
  25. struct rspamd_keypair_cache {
  26. rspamd_lru_hash_t *hash;
  27. };
  28. static void
  29. rspamd_keypair_destroy(gpointer ptr)
  30. {
  31. struct rspamd_keypair_elt *elt = (struct rspamd_keypair_elt *) ptr;
  32. REF_RELEASE(elt->nm);
  33. g_free(elt);
  34. }
  35. static guint
  36. rspamd_keypair_hash(gconstpointer ptr)
  37. {
  38. struct rspamd_keypair_elt *elt = (struct rspamd_keypair_elt *) ptr;
  39. return rspamd_cryptobox_fast_hash(elt->pair, sizeof(elt->pair),
  40. rspamd_hash_seed());
  41. }
  42. static gboolean
  43. rspamd_keypair_equal(gconstpointer p1, gconstpointer p2)
  44. {
  45. struct rspamd_keypair_elt *e1 = (struct rspamd_keypair_elt *) p1,
  46. *e2 = (struct rspamd_keypair_elt *) p2;
  47. return memcmp(e1->pair, e2->pair, sizeof(e1->pair)) == 0;
  48. }
  49. struct rspamd_keypair_cache *
  50. rspamd_keypair_cache_new(guint max_items)
  51. {
  52. struct rspamd_keypair_cache *c;
  53. g_assert(max_items > 0);
  54. c = g_malloc0(sizeof(*c));
  55. c->hash = rspamd_lru_hash_new_full(max_items, NULL,
  56. rspamd_keypair_destroy, rspamd_keypair_hash, rspamd_keypair_equal);
  57. return c;
  58. }
  59. void rspamd_keypair_cache_process(struct rspamd_keypair_cache *c,
  60. struct rspamd_cryptobox_keypair *lk,
  61. struct rspamd_cryptobox_pubkey *rk)
  62. {
  63. struct rspamd_keypair_elt search, *new;
  64. g_assert(lk != NULL);
  65. g_assert(rk != NULL);
  66. g_assert(rk->alg == lk->alg);
  67. g_assert(rk->type == lk->type);
  68. g_assert(rk->type == RSPAMD_KEYPAIR_KEX);
  69. memset(&search, 0, sizeof(search));
  70. memcpy(search.pair, rk->id, rspamd_cryptobox_HASHBYTES);
  71. memcpy(&search.pair[rspamd_cryptobox_HASHBYTES], lk->id,
  72. rspamd_cryptobox_HASHBYTES);
  73. new = rspamd_lru_hash_lookup(c->hash, &search, time(NULL));
  74. if (rk->nm) {
  75. REF_RELEASE(rk->nm);
  76. rk->nm = NULL;
  77. }
  78. if (new == NULL) {
  79. new = g_malloc0(sizeof(*new));
  80. if (posix_memalign((void **) &new->nm, 32, sizeof(*new->nm)) != 0) {
  81. abort();
  82. }
  83. REF_INIT_RETAIN(new->nm, rspamd_cryptobox_nm_dtor);
  84. memcpy(new->pair, rk->id, rspamd_cryptobox_HASHBYTES);
  85. memcpy(&new->pair[rspamd_cryptobox_HASHBYTES], lk->id,
  86. rspamd_cryptobox_HASHBYTES);
  87. memcpy(&new->nm->sk_id, lk->id, sizeof(uint64_t));
  88. if (rk->alg == RSPAMD_CRYPTOBOX_MODE_25519) {
  89. struct rspamd_cryptobox_pubkey_25519 *rk_25519 =
  90. RSPAMD_CRYPTOBOX_PUBKEY_25519(rk);
  91. struct rspamd_cryptobox_keypair_25519 *sk_25519 =
  92. RSPAMD_CRYPTOBOX_KEYPAIR_25519(lk);
  93. rspamd_cryptobox_nm(new->nm->nm, rk_25519->pk, sk_25519->sk, rk->alg);
  94. }
  95. else {
  96. struct rspamd_cryptobox_pubkey_nist *rk_nist =
  97. RSPAMD_CRYPTOBOX_PUBKEY_NIST(rk);
  98. struct rspamd_cryptobox_keypair_nist *sk_nist =
  99. RSPAMD_CRYPTOBOX_KEYPAIR_NIST(lk);
  100. rspamd_cryptobox_nm(new->nm->nm, rk_nist->pk, sk_nist->sk, rk->alg);
  101. }
  102. rspamd_lru_hash_insert(c->hash, new, new, time(NULL), -1);
  103. }
  104. g_assert(new != NULL);
  105. rk->nm = new->nm;
  106. REF_RETAIN(rk->nm);
  107. }
  108. void rspamd_keypair_cache_destroy(struct rspamd_keypair_cache *c)
  109. {
  110. if (c != NULL) {
  111. rspamd_lru_hash_destroy(c->hash);
  112. g_free(c);
  113. }
  114. }