gmac.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*-
  2. * Copyright (c) 2014 The FreeBSD Foundation
  3. * All rights reserved.
  4. *
  5. * This software was developed by John-Mark Gurney under
  6. * the sponsorship of the FreeBSD Foundation and
  7. * Rubicon Communications, LLC (Netgate).
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * $FreeBSD$
  30. *
  31. */
  32. #include <sys/types.h>
  33. #include <sys/systm.h>
  34. #include <opencrypto/gfmult.h>
  35. #include <opencrypto/gmac.h>
  36. void
  37. AES_GMAC_Init(void *ctx)
  38. {
  39. struct aes_gmac_ctx *agc;
  40. agc = ctx;
  41. bzero(agc, sizeof *agc);
  42. }
  43. void
  44. AES_GMAC_Setkey(void *ctx, const uint8_t *key, u_int klen)
  45. {
  46. struct aes_gmac_ctx *agc;
  47. const uint8_t zeros[GMAC_BLOCK_LEN] = {};
  48. struct gf128 h;
  49. uint8_t hbuf[GMAC_BLOCK_LEN];
  50. agc = ctx;
  51. agc->rounds = rijndaelKeySetupEnc(agc->keysched, key, klen * 8);
  52. rijndaelEncrypt(agc->keysched, agc->rounds, zeros, hbuf);
  53. h = gf128_read(hbuf);
  54. gf128_genmultable4(h, &agc->ghashtbl);
  55. explicit_bzero(&h, sizeof h);
  56. explicit_bzero(hbuf, sizeof hbuf);
  57. }
  58. void
  59. AES_GMAC_Reinit(void *ctx, const uint8_t *iv, u_int ivlen)
  60. {
  61. struct aes_gmac_ctx *agc;
  62. agc = ctx;
  63. KASSERT(ivlen <= sizeof agc->counter, ("passed ivlen too large!"));
  64. bcopy(iv, agc->counter, ivlen);
  65. }
  66. int
  67. AES_GMAC_Update(void *ctx, const void *vdata, u_int len)
  68. {
  69. struct aes_gmac_ctx *agc;
  70. const uint8_t *data;
  71. struct gf128 v;
  72. uint8_t buf[GMAC_BLOCK_LEN] = {};
  73. int i;
  74. agc = ctx;
  75. data = vdata;
  76. v = agc->hash;
  77. while (len > 0) {
  78. if (len >= 4*GMAC_BLOCK_LEN) {
  79. i = 4*GMAC_BLOCK_LEN;
  80. v = gf128_mul4b(v, data, &agc->ghashtbl);
  81. } else if (len >= GMAC_BLOCK_LEN) {
  82. i = GMAC_BLOCK_LEN;
  83. v = gf128_add(v, gf128_read(data));
  84. v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
  85. } else {
  86. i = len;
  87. bcopy(data, buf, i);
  88. v = gf128_add(v, gf128_read(&buf[0]));
  89. v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
  90. explicit_bzero(buf, sizeof buf);
  91. }
  92. len -= i;
  93. data += i;
  94. }
  95. agc->hash = v;
  96. explicit_bzero(&v, sizeof v);
  97. return (0);
  98. }
  99. void
  100. AES_GMAC_Final(uint8_t *digest, void *ctx)
  101. {
  102. struct aes_gmac_ctx *agc;
  103. uint8_t enccntr[GMAC_BLOCK_LEN];
  104. struct gf128 a;
  105. /* XXX - zero additional bytes? */
  106. agc = ctx;
  107. agc->counter[GMAC_BLOCK_LEN - 1] = 1;
  108. rijndaelEncrypt(agc->keysched, agc->rounds, agc->counter, enccntr);
  109. a = gf128_add(agc->hash, gf128_read(enccntr));
  110. gf128_write(a, digest);
  111. explicit_bzero(enccntr, sizeof enccntr);
  112. }