hash.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2016 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "misc.h"
  18. #include "../hooks.h"
  19. typedef struct {
  20. jose_io_t io;
  21. jose_io_t *next;
  22. EVP_MD_CTX *emc;
  23. } io_t;
  24. static bool
  25. hsh_feed(jose_io_t *io, const void *in, size_t len)
  26. {
  27. io_t *i = containerof(io, io_t, io);
  28. return EVP_DigestUpdate(i->emc, in, len) > 0;
  29. }
  30. static bool
  31. hsh_done(jose_io_t *io)
  32. {
  33. io_t *i = containerof(io, io_t, io);
  34. uint8_t hsh[EVP_MD_CTX_size(i->emc)];
  35. unsigned int l = 0;
  36. if (EVP_DigestFinal(i->emc, hsh, &l) <= 0)
  37. return SIZE_MAX;
  38. if (!i->next->feed(i->next, hsh, l) || !i->next->done(i->next))
  39. return SIZE_MAX;
  40. return l;
  41. }
  42. static void
  43. hsh_free(jose_io_t *io)
  44. {
  45. io_t *i = containerof(io, io_t, io);
  46. jose_io_decref(i->next);
  47. EVP_MD_CTX_free(i->emc);
  48. free(i);
  49. }
  50. static jose_io_t *
  51. hsh(const jose_hook_alg_t *alg, jose_cfg_t *cfg, jose_io_t *next)
  52. {
  53. jose_io_auto_t *io = NULL;
  54. const EVP_MD *md = NULL;
  55. io_t *i = NULL;
  56. switch (str2enum(alg->name, "S512", "S384", "S256", "S224", "S1", NULL)) {
  57. case 0: md = EVP_sha512(); break;
  58. case 1: md = EVP_sha384(); break;
  59. case 2: md = EVP_sha256(); break;
  60. case 3: md = EVP_sha224(); break;
  61. case 4: md = EVP_sha1(); break;
  62. }
  63. i = calloc(1, sizeof(*i));
  64. if (!i)
  65. return NULL;
  66. io = jose_io_incref(&i->io);
  67. io->feed = hsh_feed;
  68. io->done = hsh_done;
  69. io->free = hsh_free;
  70. i->next = jose_io_incref(next);
  71. i->emc = EVP_MD_CTX_new();
  72. if (!i->next || !i->emc)
  73. return NULL;
  74. if (EVP_DigestInit(i->emc, md) <= 0)
  75. return NULL;
  76. return jose_io_incref(io);
  77. }
  78. static void __attribute__((constructor))
  79. constructor(void)
  80. {
  81. static jose_hook_alg_t algs[] = {
  82. { .kind = JOSE_HOOK_ALG_KIND_HASH, .name = "S512",
  83. .hash.size = 64, .hash.hsh = hsh },
  84. { .kind = JOSE_HOOK_ALG_KIND_HASH, .name = "S384",
  85. .hash.size = 48, .hash.hsh = hsh },
  86. { .kind = JOSE_HOOK_ALG_KIND_HASH, .name = "S256",
  87. .hash.size = 32, .hash.hsh = hsh },
  88. { .kind = JOSE_HOOK_ALG_KIND_HASH, .name = "S224",
  89. .hash.size = 28, .hash.hsh = hsh },
  90. { .kind = JOSE_HOOK_ALG_KIND_HASH, .name = "S1",
  91. .hash.size = 20, .hash.hsh = hsh },
  92. {}
  93. };
  94. for (size_t i = 0; algs[i].name; i++)
  95. jose_hook_alg_push(&algs[i]);
  96. }