alg_encr.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <jose/jose.h>
  19. #include <assert.h>
  20. #include <string.h>
  21. const char *const pts[] = {
  22. "",
  23. "abc",
  24. "aosidmfoasidhtaoirnigaoiurebxlicjnvalsiouerhnoaiusdnvaisudfrhpqowuiefnali",
  25. NULL
  26. };
  27. static void
  28. test(const jose_hook_alg_t *a, const char *pt, json_t *cek, bool iter)
  29. {
  30. json_auto_t *jwe = json_object();
  31. jose_io_auto_t *eb = NULL;
  32. jose_io_auto_t *db = NULL;
  33. jose_io_auto_t *e = NULL;
  34. jose_io_auto_t *d = NULL;
  35. void *ebuf = NULL;
  36. void *dbuf = NULL;
  37. size_t elen = 0;
  38. size_t dlen = 0;
  39. eb = jose_io_malloc(NULL, &ebuf, &elen);
  40. assert(eb);
  41. e = a->encr.enc(a, NULL, jwe, cek, eb);
  42. assert(e);
  43. if (iter) {
  44. for (size_t i = 0; pt[i]; i++)
  45. assert(e->feed(e, &pt[i], 1));
  46. } else {
  47. assert(e->feed(e, pt, strlen(pt)));
  48. }
  49. assert(e->done(e));
  50. assert(json_object_get(jwe, "tag"));
  51. db = jose_io_malloc(NULL, &dbuf, &dlen);
  52. assert(db);
  53. d = a->encr.dec(a, NULL, jwe, cek, db);
  54. assert(d);
  55. if (iter) {
  56. if (elen) {
  57. uint8_t *xxx = ebuf;
  58. for (size_t i = 0; i < elen; i++) {
  59. assert(d->feed(d, &xxx[i], 1));
  60. }
  61. }
  62. } else {
  63. assert(d->feed(d, ebuf, elen));
  64. }
  65. assert(d->done(d));
  66. assert(dlen == strlen(pt));
  67. assert(memcmp(pt, dbuf, dlen) == 0);
  68. }
  69. int
  70. main(int argc, char *argv[])
  71. {
  72. for (const jose_hook_alg_t *a = jose_hook_alg_list(); a; a = a->next) {
  73. json_auto_t *cek = NULL;
  74. if (a->kind != JOSE_HOOK_ALG_KIND_ENCR)
  75. continue;
  76. fprintf(stderr, "alg: %s\n", a->name);
  77. assert((cek = json_pack("{s:s}", "alg", a->name)));
  78. assert(jose_jwk_gen(NULL, cek));
  79. for (size_t i = 0; pts[i]; i++) {
  80. test(a, pts[i], cek, false);
  81. test(a, pts[i], cek, true);
  82. }
  83. }
  84. return EXIT_SUCCESS;
  85. }