aes-common.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
  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 <stdlib.h>
  23. #include <string.h>
  24. #include "aes.h"
  25. #include "modes.h"
  26. #include "sha2.h"
  27. #include "picotls.h"
  28. #include "picotls/minicrypto.h"
  29. struct aesecb_context_t {
  30. ptls_cipher_context_t super;
  31. cf_aes_context aes;
  32. };
  33. static inline void aesecb_dispose(ptls_cipher_context_t *_ctx)
  34. {
  35. struct aesecb_context_t *ctx = (struct aesecb_context_t *)_ctx;
  36. ptls_clear_memory(ctx, sizeof(*ctx));
  37. }
  38. static inline void aesecb_encrypt(ptls_cipher_context_t *_ctx, void *output, const void *input, size_t len)
  39. {
  40. struct aesecb_context_t *ctx = (struct aesecb_context_t *)_ctx;
  41. assert(len % AES_BLOCKSZ == 0);
  42. cf_aes_encrypt(&ctx->aes, input, output);
  43. }
  44. static inline void aesecb_decrypt(ptls_cipher_context_t *_ctx, void *output, const void *input, size_t len)
  45. {
  46. struct aesecb_context_t *ctx = (struct aesecb_context_t *)_ctx;
  47. assert(len % AES_BLOCKSZ == 0);
  48. cf_aes_decrypt(&ctx->aes, input, output);
  49. }
  50. static inline int aesecb_setup_crypto(ptls_cipher_context_t *_ctx, int is_enc, const void *key)
  51. {
  52. struct aesecb_context_t *ctx = (struct aesecb_context_t *)_ctx;
  53. ctx->super.do_dispose = aesecb_dispose;
  54. ctx->super.do_init = NULL;
  55. ctx->super.do_transform = is_enc ? aesecb_encrypt : aesecb_decrypt;
  56. cf_aes_init(&ctx->aes, key, ctx->super.algo->key_size);
  57. return 0;
  58. }
  59. struct aesctr_context_t {
  60. ptls_cipher_context_t super;
  61. cf_aes_context aes;
  62. cf_ctr ctr;
  63. };
  64. static inline void aesctr_dispose(ptls_cipher_context_t *_ctx)
  65. {
  66. struct aesctr_context_t *ctx = (struct aesctr_context_t *)_ctx;
  67. ptls_clear_memory(ctx, sizeof(*ctx));
  68. }
  69. static inline void aesctr_init(ptls_cipher_context_t *_ctx, const void *iv)
  70. {
  71. struct aesctr_context_t *ctx = (struct aesctr_context_t *)_ctx;
  72. cf_ctr_init(&ctx->ctr, &cf_aes, &ctx->aes, iv);
  73. }
  74. static inline void aesctr_transform(ptls_cipher_context_t *_ctx, void *output, const void *input, size_t len)
  75. {
  76. struct aesctr_context_t *ctx = (struct aesctr_context_t *)_ctx;
  77. cf_ctr_cipher(&ctx->ctr, input, output, len);
  78. }
  79. static inline int aesctr_setup_crypto(ptls_cipher_context_t *_ctx, int is_enc, const void *key)
  80. {
  81. struct aesctr_context_t *ctx = (struct aesctr_context_t *)_ctx;
  82. ctx->super.do_dispose = aesctr_dispose;
  83. ctx->super.do_init = aesctr_init;
  84. ctx->super.do_transform = aesctr_transform;
  85. cf_aes_init(&ctx->aes, key, ctx->super.algo->key_size);
  86. return 0;
  87. }
  88. struct aesgcm_context_t {
  89. ptls_aead_context_t super;
  90. cf_aes_context aes;
  91. cf_gcm_ctx gcm;
  92. uint8_t static_iv[PTLS_AESGCM_IV_SIZE];
  93. };
  94. static inline void aesgcm_dispose_crypto(ptls_aead_context_t *_ctx)
  95. {
  96. struct aesgcm_context_t *ctx = (struct aesgcm_context_t *)_ctx;
  97. /* clear all memory except super */
  98. ptls_clear_memory((uint8_t *)ctx + sizeof(ctx->super), sizeof(*ctx) - sizeof(ctx->super));
  99. }
  100. static inline void aesgcm_encrypt_init(ptls_aead_context_t *_ctx, uint64_t seq, const void *aad, size_t aadlen)
  101. {
  102. struct aesgcm_context_t *ctx = (struct aesgcm_context_t *)_ctx;
  103. uint8_t iv[PTLS_AES_BLOCK_SIZE];
  104. ptls_aead__build_iv(ctx->super.algo, iv, ctx->static_iv, seq);
  105. cf_gcm_encrypt_init(&cf_aes, &ctx->aes, &ctx->gcm, aad, aadlen, iv, PTLS_AESGCM_IV_SIZE);
  106. }
  107. static inline size_t aesgcm_encrypt_update(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen)
  108. {
  109. struct aesgcm_context_t *ctx = (struct aesgcm_context_t *)_ctx;
  110. cf_gcm_encrypt_update(&ctx->gcm, input, inlen, output);
  111. return inlen;
  112. }
  113. static inline size_t aesgcm_encrypt_final(ptls_aead_context_t *_ctx, void *output)
  114. {
  115. struct aesgcm_context_t *ctx = (struct aesgcm_context_t *)_ctx;
  116. cf_gcm_encrypt_final(&ctx->gcm, output, PTLS_AESGCM_TAG_SIZE);
  117. return PTLS_AESGCM_TAG_SIZE;
  118. }
  119. static inline size_t aesgcm_decrypt(ptls_aead_context_t *_ctx, void *output, const void *input, size_t inlen, uint64_t seq,
  120. const void *aad, size_t aadlen)
  121. {
  122. struct aesgcm_context_t *ctx = (struct aesgcm_context_t *)_ctx;
  123. uint8_t iv[PTLS_AES_BLOCK_SIZE];
  124. if (inlen < PTLS_AESGCM_TAG_SIZE)
  125. return SIZE_MAX;
  126. size_t tag_offset = inlen - PTLS_AESGCM_TAG_SIZE;
  127. ptls_aead__build_iv(ctx->super.algo, iv, ctx->static_iv, seq);
  128. if (cf_gcm_decrypt(&cf_aes, &ctx->aes, input, tag_offset, aad, aadlen, iv, PTLS_AESGCM_IV_SIZE, (uint8_t *)input + tag_offset,
  129. PTLS_AESGCM_TAG_SIZE, output) != 0)
  130. return SIZE_MAX;
  131. return tag_offset;
  132. }
  133. static inline void aesgcm_get_iv(ptls_aead_context_t *_ctx, void *iv)
  134. {
  135. struct aesgcm_context_t *ctx = (struct aesgcm_context_t *)_ctx;
  136. memcpy(iv, ctx->static_iv, sizeof(ctx->static_iv));
  137. }
  138. static inline void aesgcm_set_iv(ptls_aead_context_t *_ctx, const void *iv)
  139. {
  140. struct aesgcm_context_t *ctx = (struct aesgcm_context_t *)_ctx;
  141. memcpy(ctx->static_iv, iv, sizeof(ctx->static_iv));
  142. }
  143. static inline int aead_aesgcm_setup_crypto(ptls_aead_context_t *_ctx, int is_enc, const void *key, const void *iv)
  144. {
  145. struct aesgcm_context_t *ctx = (struct aesgcm_context_t *)_ctx;
  146. ctx->super.dispose_crypto = aesgcm_dispose_crypto;
  147. ctx->super.do_get_iv = aesgcm_get_iv;
  148. ctx->super.do_set_iv = aesgcm_set_iv;
  149. if (is_enc) {
  150. ctx->super.do_encrypt_init = aesgcm_encrypt_init;
  151. ctx->super.do_encrypt_update = aesgcm_encrypt_update;
  152. ctx->super.do_encrypt_final = aesgcm_encrypt_final;
  153. ctx->super.do_encrypt = ptls_aead__do_encrypt;
  154. ctx->super.do_encrypt_v = ptls_aead__do_encrypt_v;
  155. ctx->super.do_decrypt = NULL;
  156. } else {
  157. ctx->super.do_encrypt_init = NULL;
  158. ctx->super.do_encrypt_update = NULL;
  159. ctx->super.do_encrypt_final = NULL;
  160. ctx->super.do_encrypt = NULL;
  161. ctx->super.do_encrypt_v = NULL;
  162. ctx->super.do_decrypt = aesgcm_decrypt;
  163. }
  164. cf_aes_init(&ctx->aes, key, ctx->super.algo->key_size);
  165. memcpy(ctx->static_iv, iv, sizeof(ctx->static_iv));
  166. return 0;
  167. }