libaegis.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2023 Frank Denis
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include <aegis.h>
  23. #include "picotls.h"
  24. // AEGIS-128L
  25. struct aegis128l_context_t {
  26. ptls_aead_context_t super;
  27. aegis128l_state st;
  28. uint8_t key[PTLS_AEGIS128L_KEY_SIZE];
  29. uint8_t static_iv[PTLS_AEGIS128L_IV_SIZE];
  30. };
  31. static void aegis128l_get_iv(ptls_aead_context_t *_ctx, void *iv)
  32. {
  33. struct aegis128l_context_t *ctx = (struct aegis128l_context_t *)_ctx;
  34. memcpy(iv, ctx->static_iv, sizeof(ctx->static_iv));
  35. }
  36. static void aegis128l_set_iv(ptls_aead_context_t *_ctx, const void *iv)
  37. {
  38. struct aegis128l_context_t *ctx = (struct aegis128l_context_t *)_ctx;
  39. memcpy(ctx->static_iv, iv, sizeof(ctx->static_iv));
  40. }
  41. static void aegis128l_init(ptls_aead_context_t *_ctx, uint64_t seq, const void *aad, size_t aadlen)
  42. {
  43. struct aegis128l_context_t *ctx = (struct aegis128l_context_t *)_ctx;
  44. uint8_t iv[PTLS_AEGIS128L_IV_SIZE];
  45. ptls_aead__build_iv(ctx->super.algo, iv, ctx->static_iv, seq);
  46. aegis128l_state_init(&ctx->st, (const uint8_t *)aad, aadlen, iv, ctx->key);
  47. return;
  48. }
  49. static size_t aegis128l_encrypt_update(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen)
  50. {
  51. struct aegis128l_context_t *ctx = (struct aegis128l_context_t *)_ctx;
  52. size_t written;
  53. aegis128l_state_encrypt_update(&ctx->st, (uint8_t *)output, inlen + aegis128l_TAILBYTES_MAX, &written, (const uint8_t *)input,
  54. inlen);
  55. return written;
  56. }
  57. static size_t aegis128l_encrypt_final(ptls_aead_context_t *_ctx, void *output)
  58. {
  59. struct aegis128l_context_t *ctx = (struct aegis128l_context_t *)_ctx;
  60. size_t written;
  61. aegis128l_state_encrypt_final(&ctx->st, (uint8_t *)output, aegis128l_TAILBYTES_MAX + PTLS_AEGIS128L_TAG_SIZE, &written,
  62. PTLS_AEGIS128L_TAG_SIZE);
  63. return written;
  64. }
  65. static size_t aegis128l_decrypt_oneshot(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen, uint64_t seq,
  66. const void *aad, size_t aadlen)
  67. {
  68. struct aegis128l_context_t *ctx = (struct aegis128l_context_t *)_ctx;
  69. uint8_t iv[PTLS_AEGIS128L_IV_SIZE] = {0};
  70. if (inlen < PTLS_AEGIS128L_TAG_SIZE) {
  71. return SIZE_MAX;
  72. }
  73. ptls_aead__build_iv(ctx->super.algo, iv, ctx->static_iv, seq);
  74. if (aegis128l_decrypt((uint8_t *)output, (const uint8_t *)input, inlen, PTLS_AEGIS128L_TAG_SIZE, (const uint8_t *)aad, aadlen,
  75. iv, ctx->key) != 0) {
  76. return SIZE_MAX;
  77. }
  78. return inlen - PTLS_AEGIS128L_TAG_SIZE;
  79. }
  80. static void aegis128l_dispose_crypto(ptls_aead_context_t *_ctx)
  81. {
  82. struct aegis128l_context_t *ctx = (struct aegis128l_context_t *)_ctx;
  83. ptls_clear_memory(ctx->key, sizeof(ctx->key));
  84. return;
  85. }
  86. static int aegis128l_setup_crypto(ptls_aead_context_t *_ctx, int is_enc, const void *key, const void *iv)
  87. {
  88. struct aegis128l_context_t *ctx = (struct aegis128l_context_t *)_ctx;
  89. ctx->super.dispose_crypto = aegis128l_dispose_crypto;
  90. ctx->super.do_get_iv = aegis128l_get_iv;
  91. ctx->super.do_set_iv = aegis128l_set_iv;
  92. if (is_enc) {
  93. ctx->super.do_encrypt_init = aegis128l_init;
  94. ctx->super.do_encrypt_update = aegis128l_encrypt_update;
  95. ctx->super.do_encrypt_final = aegis128l_encrypt_final;
  96. ctx->super.do_encrypt = ptls_aead__do_encrypt;
  97. ctx->super.do_encrypt_v = ptls_aead__do_encrypt_v;
  98. ctx->super.do_decrypt = NULL;
  99. } else {
  100. ctx->super.do_encrypt_init = NULL;
  101. ctx->super.do_encrypt_update = NULL;
  102. ctx->super.do_encrypt_final = NULL;
  103. ctx->super.do_encrypt = NULL;
  104. ctx->super.do_encrypt_v = NULL;
  105. ctx->super.do_decrypt = aegis128l_decrypt_oneshot;
  106. }
  107. memcpy(ctx->key, key, sizeof(ctx->key));
  108. memcpy(ctx->static_iv, iv, sizeof(ctx->static_iv));
  109. return 0;
  110. }
  111. // AEGIS-256
  112. struct aegis256_context_t {
  113. ptls_aead_context_t super;
  114. aegis256_state st;
  115. uint8_t key[PTLS_AEGIS256_KEY_SIZE];
  116. uint8_t static_iv[PTLS_AEGIS256_IV_SIZE];
  117. };
  118. static void aegis256_get_iv(ptls_aead_context_t *_ctx, void *iv)
  119. {
  120. struct aegis256_context_t *ctx = (struct aegis256_context_t *)_ctx;
  121. memcpy(iv, ctx->static_iv, sizeof(ctx->static_iv));
  122. }
  123. static void aegis256_set_iv(ptls_aead_context_t *_ctx, const void *iv)
  124. {
  125. struct aegis256_context_t *ctx = (struct aegis256_context_t *)_ctx;
  126. memcpy(ctx->static_iv, iv, sizeof(ctx->static_iv));
  127. }
  128. static void aegis256_init(ptls_aead_context_t *_ctx, uint64_t seq, const void *aad, size_t aadlen)
  129. {
  130. struct aegis256_context_t *ctx = (struct aegis256_context_t *)_ctx;
  131. uint8_t iv[PTLS_AEGIS256_IV_SIZE] = {0};
  132. ptls_aead__build_iv(ctx->super.algo, iv, ctx->static_iv, seq);
  133. aegis256_state_init(&ctx->st, (const uint8_t *)aad, aadlen, iv, ctx->key);
  134. return;
  135. }
  136. static size_t aegis256_encrypt_update(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen)
  137. {
  138. struct aegis256_context_t *ctx = (struct aegis256_context_t *)_ctx;
  139. size_t written;
  140. aegis256_state_encrypt_update(&ctx->st, (uint8_t *)output, inlen + aegis256_TAILBYTES_MAX, &written, (const uint8_t *)input,
  141. inlen);
  142. return written;
  143. }
  144. static size_t aegis256_encrypt_final(ptls_aead_context_t *_ctx, void *output)
  145. {
  146. struct aegis256_context_t *ctx = (struct aegis256_context_t *)_ctx;
  147. size_t written;
  148. aegis256_state_encrypt_final(&ctx->st, (uint8_t *)output, aegis256_TAILBYTES_MAX + PTLS_AEGIS256_TAG_SIZE, &written,
  149. PTLS_AEGIS256_TAG_SIZE);
  150. return written;
  151. }
  152. static size_t aegis256_decrypt_oneshot(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen, uint64_t seq,
  153. const void *aad, size_t aadlen)
  154. {
  155. struct aegis256_context_t *ctx = (struct aegis256_context_t *)_ctx;
  156. uint8_t iv[PTLS_AEGIS256_IV_SIZE];
  157. if (inlen < PTLS_AEGIS256_TAG_SIZE) {
  158. return SIZE_MAX;
  159. }
  160. ptls_aead__build_iv(ctx->super.algo, iv, ctx->static_iv, seq);
  161. if (aegis256_decrypt((uint8_t *)output, (const uint8_t *)input, inlen, PTLS_AEGIS256_TAG_SIZE, (const uint8_t *)aad, aadlen, iv,
  162. ctx->key) != 0) {
  163. return SIZE_MAX;
  164. }
  165. return inlen - PTLS_AEGIS256_TAG_SIZE;
  166. }
  167. static void aegis256_dispose_crypto(ptls_aead_context_t *_ctx)
  168. {
  169. struct aegis256_context_t *ctx = (struct aegis256_context_t *)_ctx;
  170. ptls_clear_memory(ctx->key, sizeof(ctx->key));
  171. return;
  172. }
  173. static int aegis256_setup_crypto(ptls_aead_context_t *_ctx, int is_enc, const void *key, const void *iv)
  174. {
  175. struct aegis256_context_t *ctx = (struct aegis256_context_t *)_ctx;
  176. ctx->super.dispose_crypto = aegis256_dispose_crypto;
  177. ctx->super.do_get_iv = aegis256_get_iv;
  178. ctx->super.do_set_iv = aegis256_set_iv;
  179. if (is_enc) {
  180. ctx->super.do_encrypt_init = aegis256_init;
  181. ctx->super.do_encrypt_update = aegis256_encrypt_update;
  182. ctx->super.do_encrypt_final = aegis256_encrypt_final;
  183. ctx->super.do_encrypt = ptls_aead__do_encrypt;
  184. ctx->super.do_encrypt_v = ptls_aead__do_encrypt_v;
  185. ctx->super.do_decrypt = NULL;
  186. } else {
  187. ctx->super.do_encrypt_init = NULL;
  188. ctx->super.do_encrypt_update = NULL;
  189. ctx->super.do_encrypt_final = NULL;
  190. ctx->super.do_encrypt = NULL;
  191. ctx->super.do_encrypt_v = NULL;
  192. ctx->super.do_decrypt = aegis256_decrypt_oneshot;
  193. }
  194. memcpy(ctx->key, key, sizeof(ctx->key));
  195. memcpy(ctx->static_iv, iv, sizeof(ctx->static_iv));
  196. return 0;
  197. }