gcm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #include "handy.h"
  15. #include "prp.h"
  16. #include "modes.h"
  17. #include "blockwise.h"
  18. #include "bitops.h"
  19. #include "gf128.h"
  20. #include "tassert.h"
  21. #include <string.h>
  22. #define STATE_INVALID 0
  23. #define STATE_AAD 1
  24. #define STATE_CIPHER 2
  25. static void ghash_init(ghash_ctx *ctx, uint8_t H[16])
  26. {
  27. memset(ctx, 0, sizeof *ctx);
  28. cf_gf128_frombytes_be(H, ctx->H);
  29. ctx->state = STATE_AAD;
  30. }
  31. static void ghash_block(void *vctx, const uint8_t *data)
  32. {
  33. ghash_ctx *ctx = vctx;
  34. cf_gf128 gfdata;
  35. cf_gf128_frombytes_be(data, gfdata);
  36. cf_gf128_add(gfdata, ctx->Y, ctx->Y);
  37. cf_gf128_mul(ctx->Y, ctx->H, ctx->Y);
  38. }
  39. static void ghash_add(ghash_ctx *ctx, const uint8_t *buf, size_t n)
  40. {
  41. cf_blockwise_accumulate(ctx->buffer, &ctx->buffer_used,
  42. sizeof ctx->buffer,
  43. buf, n,
  44. ghash_block,
  45. ctx);
  46. }
  47. static void ghash_add_pad(ghash_ctx *ctx)
  48. {
  49. if (ctx->buffer_used == 0)
  50. return;
  51. memset(ctx->buffer + ctx->buffer_used, 0, sizeof(ctx->buffer) - ctx->buffer_used);
  52. ghash_block(ctx, ctx->buffer);
  53. ctx->buffer_used = 0;
  54. }
  55. static void ghash_add_aad(ghash_ctx *ctx, const uint8_t *buf, size_t n)
  56. {
  57. assert(ctx->state == STATE_AAD);
  58. ctx->len_aad += n;
  59. ghash_add(ctx, buf, n);
  60. }
  61. static void ghash_add_cipher(ghash_ctx *ctx, const uint8_t *buf, size_t n)
  62. {
  63. if (ctx->state == STATE_AAD)
  64. {
  65. ghash_add_pad(ctx);
  66. ctx->state = STATE_CIPHER;
  67. }
  68. assert(ctx->state == STATE_CIPHER);
  69. ctx->len_cipher += n;
  70. ghash_add(ctx, buf, n);
  71. }
  72. static void ghash_final(ghash_ctx *ctx, uint8_t out[16])
  73. {
  74. uint8_t lenbuf[8];
  75. if (ctx->state == STATE_AAD || ctx->state == STATE_CIPHER)
  76. {
  77. ghash_add_pad(ctx);
  78. ctx->state = STATE_INVALID;
  79. }
  80. /* Add len(A) || len(C) */
  81. write64_be(ctx->len_aad * 8, lenbuf);
  82. ghash_add(ctx, lenbuf, sizeof lenbuf);
  83. write64_be(ctx->len_cipher * 8, lenbuf);
  84. ghash_add(ctx, lenbuf, sizeof lenbuf);
  85. assert(ctx->buffer_used == 0);
  86. cf_gf128_tobytes_be(ctx->Y, out);
  87. }
  88. void cf_gcm_encrypt_init(const cf_prp *prp, void *prpctx, cf_gcm_ctx *gcmctx,
  89. const uint8_t *header, size_t nheader,
  90. const uint8_t *nonce, size_t nnonce)
  91. {
  92. uint8_t H[16] = { 0 };
  93. /* H = E_K(0^128) */
  94. prp->encrypt(prpctx, H, H);
  95. /* Produce CTR nonce, Y_0:
  96. *
  97. * if len(IV) == 96
  98. * Y_0 = IV || 0^31 || 1
  99. * otherwise
  100. * Y_0 = GHASH(H, {}, IV)
  101. */
  102. if (nnonce == 12)
  103. {
  104. memcpy(gcmctx->Y0, nonce, nnonce);
  105. gcmctx->Y0[12] = gcmctx->Y0[13] = gcmctx->Y0[14] = 0x00;
  106. gcmctx->Y0[15] = 0x01;
  107. } else {
  108. ghash_init(&gcmctx->gh, H);
  109. ghash_add_cipher(&gcmctx->gh, nonce, nnonce);
  110. ghash_final(&gcmctx->gh, gcmctx->Y0);
  111. }
  112. /* Hash AAD */
  113. ghash_init(&gcmctx->gh, H);
  114. ghash_add_aad(&gcmctx->gh, header, nheader);
  115. /* Produce ciphertext */
  116. memset(gcmctx->e_Y0, 0, sizeof(gcmctx->e_Y0));
  117. cf_ctr_init(&gcmctx->ctr, prp, prpctx, gcmctx->Y0);
  118. cf_ctr_custom_counter(&gcmctx->ctr, 12, 4); /* counter is 2^32 */
  119. cf_ctr_cipher(&gcmctx->ctr, gcmctx->e_Y0, gcmctx->e_Y0, sizeof gcmctx->e_Y0); /* first block is tag offset */
  120. mem_clean(H, sizeof H);
  121. }
  122. void cf_gcm_encrypt_update(cf_gcm_ctx *gcmctx, const uint8_t *plain, size_t nplain, uint8_t *cipher)
  123. {
  124. cf_ctr_cipher(&gcmctx->ctr, plain, cipher, nplain);
  125. ghash_add_cipher(&gcmctx->gh, cipher, nplain);
  126. }
  127. void cf_gcm_encrypt_final(cf_gcm_ctx *gcmctx, uint8_t *tag, size_t ntag)
  128. {
  129. /* Post-process ghash output */
  130. uint8_t full_tag[16] = { 0 };
  131. ghash_final(&gcmctx->gh, full_tag);
  132. assert(ntag > 1 && ntag <= 16);
  133. xor_bb(tag, full_tag, gcmctx->e_Y0, ntag);
  134. mem_clean(full_tag, sizeof full_tag);
  135. mem_clean(gcmctx, sizeof *gcmctx);
  136. }
  137. void cf_gcm_encrypt(const cf_prp *prp, void *prpctx,
  138. const uint8_t *plain, size_t nplain,
  139. const uint8_t *header, size_t nheader,
  140. const uint8_t *nonce, size_t nnonce,
  141. uint8_t *cipher, /* the same size as nplain */
  142. uint8_t *tag, size_t ntag)
  143. {
  144. cf_gcm_ctx gcmctx;
  145. cf_gcm_encrypt_init(prp, prpctx, &gcmctx, header, nheader, nonce, nnonce);
  146. cf_gcm_encrypt_update(&gcmctx, plain, nplain, cipher);
  147. cf_gcm_encrypt_final(&gcmctx, tag, ntag);
  148. }
  149. int cf_gcm_decrypt(const cf_prp *prp, void *prpctx,
  150. const uint8_t *cipher, size_t ncipher,
  151. const uint8_t *header, size_t nheader,
  152. const uint8_t *nonce, size_t nnonce,
  153. const uint8_t *tag, size_t ntag,
  154. uint8_t *plain)
  155. {
  156. uint8_t H[16] = { 0 };
  157. uint8_t Y0[16];
  158. /* H = E_K(0^128) */
  159. prp->encrypt(prpctx, H, H);
  160. /* Produce CTR nonce, Y_0:
  161. *
  162. * if len(IV) == 96
  163. * Y_0 = IV || 0^31 || 1
  164. * otherwise
  165. * Y_0 = GHASH(H, {}, IV)
  166. */
  167. if (nnonce == 12)
  168. {
  169. memcpy(Y0, nonce, nnonce);
  170. Y0[12] = Y0[13] = Y0[14] = 0x00;
  171. Y0[15] = 0x01;
  172. } else {
  173. ghash_ctx gh;
  174. ghash_init(&gh, H);
  175. ghash_add_cipher(&gh, nonce, nnonce);
  176. ghash_final(&gh, Y0);
  177. }
  178. /* Hash AAD. */
  179. ghash_ctx gh;
  180. ghash_init(&gh, H);
  181. ghash_add_aad(&gh, header, nheader);
  182. /* Start counter mode, to obtain offset on tag. */
  183. uint8_t e_Y0[16] = { 0 };
  184. cf_ctr ctr;
  185. cf_ctr_init(&ctr, prp, prpctx, Y0);
  186. cf_ctr_custom_counter(&ctr, 12, 4);
  187. cf_ctr_cipher(&ctr, e_Y0, e_Y0, sizeof e_Y0);
  188. /* Hash ciphertext. */
  189. ghash_add_cipher(&gh, cipher, ncipher);
  190. /* Produce tag. */
  191. uint8_t full_tag[16];
  192. ghash_final(&gh, full_tag);
  193. assert(ntag > 1 && ntag <= 16);
  194. xor_bb(full_tag, full_tag, e_Y0, ntag);
  195. int err = 1;
  196. if (!mem_eq(full_tag, tag, ntag))
  197. goto x_err;
  198. /* Complete decryption. */
  199. cf_ctr_cipher(&ctr, cipher, plain, ncipher);
  200. err = 0;
  201. x_err:
  202. mem_clean(H, sizeof H);
  203. mem_clean(Y0, sizeof Y0);
  204. mem_clean(e_Y0, sizeof e_Y0);
  205. mem_clean(full_tag, sizeof full_tag);
  206. mem_clean(&gh, sizeof gh);
  207. mem_clean(&ctr, sizeof ctr);
  208. return err;
  209. }