mac-gmac.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* mac-gmac.c - GMAC glue for MAC API
  2. * Copyright (C) 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "g10lib.h"
  25. #include "cipher.h"
  26. #include "./mac-internal.h"
  27. static int
  28. map_mac_algo_to_cipher (int mac_algo)
  29. {
  30. switch (mac_algo)
  31. {
  32. default:
  33. return GCRY_CIPHER_NONE;
  34. case GCRY_MAC_GMAC_AES:
  35. return GCRY_CIPHER_AES;
  36. case GCRY_MAC_GMAC_CAMELLIA:
  37. return GCRY_CIPHER_CAMELLIA128;
  38. case GCRY_MAC_GMAC_TWOFISH:
  39. return GCRY_CIPHER_TWOFISH;
  40. case GCRY_MAC_GMAC_SERPENT:
  41. return GCRY_CIPHER_SERPENT128;
  42. case GCRY_MAC_GMAC_SEED:
  43. return GCRY_CIPHER_SEED;
  44. case GCRY_MAC_GMAC_SM4:
  45. return GCRY_CIPHER_SM4;
  46. case GCRY_MAC_GMAC_ARIA:
  47. return GCRY_CIPHER_ARIA128;
  48. }
  49. }
  50. static gcry_err_code_t
  51. gmac_open (gcry_mac_hd_t h)
  52. {
  53. gcry_err_code_t err;
  54. gcry_cipher_hd_t hd;
  55. int secure = (h->magic == CTX_MAC_MAGIC_SECURE);
  56. int cipher_algo;
  57. unsigned int flags;
  58. cipher_algo = map_mac_algo_to_cipher (h->spec->algo);
  59. flags = (secure ? GCRY_CIPHER_SECURE : 0);
  60. err = _gcry_cipher_open_internal (&hd, cipher_algo, GCRY_CIPHER_MODE_GCM,
  61. flags);
  62. if (err)
  63. return err;
  64. h->u.gmac.cipher_algo = cipher_algo;
  65. h->u.gmac.ctx = hd;
  66. return 0;
  67. }
  68. static void
  69. gmac_close (gcry_mac_hd_t h)
  70. {
  71. _gcry_cipher_close (h->u.gmac.ctx);
  72. h->u.gmac.ctx = NULL;
  73. }
  74. static gcry_err_code_t
  75. gmac_setkey (gcry_mac_hd_t h, const unsigned char *key, size_t keylen)
  76. {
  77. return _gcry_cipher_setkey (h->u.gmac.ctx, key, keylen);
  78. }
  79. static gcry_err_code_t
  80. gmac_setiv (gcry_mac_hd_t h, const unsigned char *iv, size_t ivlen)
  81. {
  82. return _gcry_cipher_setiv (h->u.gmac.ctx, iv, ivlen);
  83. }
  84. static gcry_err_code_t
  85. gmac_reset (gcry_mac_hd_t h)
  86. {
  87. return _gcry_cipher_reset (h->u.gmac.ctx);
  88. }
  89. static gcry_err_code_t
  90. gmac_write (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
  91. {
  92. return _gcry_cipher_authenticate (h->u.gmac.ctx, buf, buflen);
  93. }
  94. static gcry_err_code_t
  95. gmac_read (gcry_mac_hd_t h, unsigned char *outbuf, size_t * outlen)
  96. {
  97. if (*outlen > GCRY_GCM_BLOCK_LEN)
  98. *outlen = GCRY_GCM_BLOCK_LEN;
  99. return _gcry_cipher_gettag (h->u.gmac.ctx, outbuf, *outlen);
  100. }
  101. static gcry_err_code_t
  102. gmac_verify (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
  103. {
  104. return _gcry_cipher_checktag (h->u.gmac.ctx, buf, buflen);
  105. }
  106. static unsigned int
  107. gmac_get_maclen (int algo)
  108. {
  109. (void)algo;
  110. return GCRY_GCM_BLOCK_LEN;
  111. }
  112. static unsigned int
  113. gmac_get_keylen (int algo)
  114. {
  115. return _gcry_cipher_get_algo_keylen (map_mac_algo_to_cipher (algo));
  116. }
  117. static gcry_mac_spec_ops_t gmac_ops = {
  118. gmac_open,
  119. gmac_close,
  120. gmac_setkey,
  121. gmac_setiv,
  122. gmac_reset,
  123. gmac_write,
  124. gmac_read,
  125. gmac_verify,
  126. gmac_get_maclen,
  127. gmac_get_keylen,
  128. NULL,
  129. NULL
  130. };
  131. #if USE_AES
  132. const gcry_mac_spec_t _gcry_mac_type_spec_gmac_aes = {
  133. GCRY_MAC_GMAC_AES, {0, 0}, "GMAC_AES",
  134. &gmac_ops
  135. };
  136. #endif
  137. #if USE_TWOFISH
  138. const gcry_mac_spec_t _gcry_mac_type_spec_gmac_twofish = {
  139. GCRY_MAC_GMAC_TWOFISH, {0, 0}, "GMAC_TWOFISH",
  140. &gmac_ops
  141. };
  142. #endif
  143. #if USE_SERPENT
  144. const gcry_mac_spec_t _gcry_mac_type_spec_gmac_serpent = {
  145. GCRY_MAC_GMAC_SERPENT, {0, 0}, "GMAC_SERPENT",
  146. &gmac_ops
  147. };
  148. #endif
  149. #if USE_SEED
  150. const gcry_mac_spec_t _gcry_mac_type_spec_gmac_seed = {
  151. GCRY_MAC_GMAC_SEED, {0, 0}, "GMAC_SEED",
  152. &gmac_ops
  153. };
  154. #endif
  155. #if USE_CAMELLIA
  156. const gcry_mac_spec_t _gcry_mac_type_spec_gmac_camellia = {
  157. GCRY_MAC_GMAC_CAMELLIA, {0, 0}, "GMAC_CAMELLIA",
  158. &gmac_ops
  159. };
  160. #endif
  161. #if USE_SM4
  162. const gcry_mac_spec_t _gcry_mac_type_spec_gmac_sm4 = {
  163. GCRY_MAC_GMAC_SM4, {0, 0}, "GMAC_SM4",
  164. &gmac_ops
  165. };
  166. #endif
  167. #if USE_ARIA
  168. const gcry_mac_spec_t _gcry_mac_type_spec_gmac_aria = {
  169. GCRY_MAC_GMAC_ARIA, {0, 0}, "GMAC_ARIA",
  170. &gmac_ops
  171. };
  172. #endif