alg_hash.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2017 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 "../lib/hooks.h"
  18. #include <assert.h>
  19. #include <string.h>
  20. #ifdef __MINGW32__
  21. #define sscanf __mingw_sscanf
  22. #endif
  23. static const struct {
  24. const char *alg;
  25. const char *msg;
  26. const char *hsh;
  27. } v[] = {
  28. { "S1", "abc", "a9993e364706816aba3e25717850c26c9cd0d89d" },
  29. { "S224", "abc", "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" },
  30. { "S256", "abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61"
  31. "f20015ad" },
  32. { "S384", "abc", "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a"
  33. "43ff5bed8086072ba1e7cc2358baeca134c825a7" },
  34. { "S512", "abc", "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee6"
  35. "4b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e"
  36. "2a9ac94fa54ca49f" },
  37. { "S1", "", "da39a3ee5e6b4b0d3255bfef95601890afd80709" },
  38. { "S224", "", "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f" },
  39. { "S256", "", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b"
  40. "7852b855" },
  41. { "S384", "", "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf"
  42. "63f6e1da274edebfe76f65fbd51ad2f14898b95b" },
  43. { "S512", "", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921"
  44. "d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81"
  45. "a538327af927da3e" },
  46. {}
  47. };
  48. static void
  49. test(const jose_hook_alg_t *alg, const char *msg,
  50. const uint8_t *a, size_t al, bool iter)
  51. {
  52. jose_io_auto_t *buf = NULL;
  53. jose_io_auto_t *hsh = NULL;
  54. uint8_t q[alg->hash.size];
  55. size_t ql = sizeof(q);
  56. assert(sizeof(q) == al);
  57. memset(q, 0, sizeof(q));
  58. buf = jose_io_buffer(NULL, q, &ql);
  59. assert(buf);
  60. hsh = alg->hash.hsh(alg, NULL, buf);
  61. assert(hsh);
  62. if (iter) {
  63. for (size_t i = 0; i < strlen(msg); i++)
  64. assert(hsh->feed(hsh, &msg[i], 1));
  65. } else {
  66. assert(hsh->feed(hsh, msg, strlen(msg)));
  67. }
  68. assert(hsh->done(hsh));
  69. assert(ql == al);
  70. assert(memcmp(q, a, al) == 0);
  71. }
  72. int
  73. main(int argc, char *argv[])
  74. {
  75. for (size_t i = 0; v[i].alg; i++) {
  76. const jose_hook_alg_t *alg = NULL;
  77. alg = jose_hook_alg_find(JOSE_HOOK_ALG_KIND_HASH, v[i].alg);
  78. assert(alg);
  79. uint8_t a[alg->hash.size];
  80. assert(strlen(v[i].hsh) == sizeof(a) * 2);
  81. for (size_t j = 0; j < sizeof(a); j++)
  82. sscanf(&v[i].hsh[j * 2], "%02hhx", &a[j]);
  83. test(alg, v[i].msg, a, sizeof(a), false);
  84. test(alg, v[i].msg, a, sizeof(a), true);
  85. }
  86. return EXIT_SUCCESS;
  87. }