mac-internal.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* mac-internal.h - Internal defs for mac.c
  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 "g10lib.h"
  21. #include "cipher-proto.h"
  22. #include "gost.h"
  23. /* The data object used to hold a handle to an encryption object. */
  24. struct gcry_mac_handle;
  25. /* The data object used to hold poly1305-mac context. */
  26. struct poly1305mac_context_s;
  27. /*
  28. *
  29. * Message authentication code related definitions.
  30. *
  31. */
  32. /* Magic values for the context structure. */
  33. #define CTX_MAC_MAGIC_NORMAL 0x59d9b8af
  34. #define CTX_MAC_MAGIC_SECURE 0x12c27cd0
  35. /* MAC module functions. */
  36. typedef gcry_err_code_t (*gcry_mac_open_func_t)(gcry_mac_hd_t h);
  37. typedef void (*gcry_mac_close_func_t)(gcry_mac_hd_t h);
  38. typedef gcry_err_code_t (*gcry_mac_setkey_func_t)(gcry_mac_hd_t h,
  39. const unsigned char *key,
  40. size_t keylen);
  41. typedef gcry_err_code_t (*gcry_mac_setiv_func_t)(gcry_mac_hd_t h,
  42. const unsigned char *iv,
  43. size_t ivlen);
  44. typedef gcry_err_code_t (*gcry_mac_reset_func_t)(gcry_mac_hd_t h);
  45. typedef gcry_err_code_t (*gcry_mac_write_func_t)(gcry_mac_hd_t h,
  46. const unsigned char *inbuf,
  47. size_t inlen);
  48. typedef gcry_err_code_t (*gcry_mac_read_func_t)(gcry_mac_hd_t h,
  49. unsigned char *outbuf,
  50. size_t *outlen);
  51. typedef gcry_err_code_t (*gcry_mac_verify_func_t)(gcry_mac_hd_t h,
  52. const unsigned char *inbuf,
  53. size_t inlen);
  54. typedef unsigned int (*gcry_mac_get_maclen_func_t)(int algo);
  55. typedef unsigned int (*gcry_mac_get_keylen_func_t)(int algo);
  56. /* The type used to convey additional information to a MAC. */
  57. typedef gpg_err_code_t (*gcry_mac_set_extra_info_t)
  58. (gcry_mac_hd_t h, int what, const void *buffer, size_t buflen);
  59. typedef struct gcry_mac_spec_ops
  60. {
  61. gcry_mac_open_func_t open;
  62. gcry_mac_close_func_t close;
  63. gcry_mac_setkey_func_t setkey;
  64. gcry_mac_setiv_func_t setiv;
  65. gcry_mac_reset_func_t reset;
  66. gcry_mac_write_func_t write;
  67. gcry_mac_read_func_t read;
  68. gcry_mac_verify_func_t verify;
  69. gcry_mac_get_maclen_func_t get_maclen;
  70. gcry_mac_get_keylen_func_t get_keylen;
  71. gcry_mac_set_extra_info_t set_extra_info;
  72. selftest_func_t selftest;
  73. } gcry_mac_spec_ops_t;
  74. /* Module specification structure for message authentication codes. */
  75. typedef struct gcry_mac_spec
  76. {
  77. int algo;
  78. struct {
  79. unsigned int disabled:1;
  80. unsigned int fips:1;
  81. } flags;
  82. const char *name;
  83. const gcry_mac_spec_ops_t *ops;
  84. } gcry_mac_spec_t;
  85. /* The handle structure. */
  86. struct gcry_mac_handle
  87. {
  88. int magic;
  89. int algo;
  90. const gcry_mac_spec_t *spec;
  91. gcry_ctx_t gcry_ctx;
  92. union {
  93. struct {
  94. gcry_md_hd_t md_ctx;
  95. int md_algo;
  96. } hmac;
  97. struct {
  98. gcry_cipher_hd_t ctx;
  99. int cipher_algo;
  100. unsigned int blklen;
  101. } cmac;
  102. struct {
  103. gcry_cipher_hd_t ctx;
  104. int cipher_algo;
  105. } gmac;
  106. struct {
  107. struct poly1305mac_context_s *ctx;
  108. } poly1305mac;
  109. struct {
  110. GOST28147_context ctx;
  111. u32 n1, n2;
  112. unsigned int unused;
  113. unsigned int count;
  114. unsigned char lastiv[8]; /* IMIT blocksize */
  115. } imit;
  116. } u;
  117. };
  118. /*
  119. * The HMAC algorithm specifications (mac-hmac.c).
  120. */
  121. #if USE_SHA1
  122. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha1;
  123. #endif
  124. #if USE_SHA256
  125. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha256;
  126. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha224;
  127. #endif
  128. #if USE_SHA512
  129. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha512;
  130. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha384;
  131. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha512_224;
  132. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha512_256;
  133. #endif
  134. #if USE_SHA3
  135. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_224;
  136. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_256;
  137. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_384;
  138. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_512;
  139. #endif
  140. #if USE_GOST_R_3411_94
  141. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_gost3411_94;
  142. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_gost3411_cp;
  143. #endif
  144. #if USE_GOST_R_3411_12
  145. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_stribog256;
  146. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_stribog512;
  147. #endif
  148. #if USE_WHIRLPOOL
  149. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_whirlpool;
  150. #endif
  151. #if USE_RMD160
  152. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_rmd160;
  153. #endif
  154. #if USE_TIGER
  155. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_tiger1;
  156. #endif
  157. #if USE_MD5
  158. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_md5;
  159. #endif
  160. #if USE_MD4
  161. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_md4;
  162. #endif
  163. #if USE_BLAKE2
  164. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_512;
  165. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_384;
  166. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_256;
  167. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_160;
  168. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_256;
  169. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_224;
  170. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_160;
  171. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_128;
  172. #endif
  173. #if USE_SM3
  174. extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sm3;
  175. #endif
  176. /*
  177. * The CMAC algorithm specifications (mac-cmac.c).
  178. */
  179. #if USE_BLOWFISH
  180. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_blowfish;
  181. #endif
  182. #if USE_DES
  183. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_tripledes;
  184. #endif
  185. #if USE_CAST5
  186. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_cast5;
  187. #endif
  188. #if USE_AES
  189. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_aes;
  190. #endif
  191. #if USE_TWOFISH
  192. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_twofish;
  193. #endif
  194. #if USE_SERPENT
  195. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_serpent;
  196. #endif
  197. #if USE_RFC2268
  198. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_rfc2268;
  199. #endif
  200. #if USE_SEED
  201. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_seed;
  202. #endif
  203. #if USE_CAMELLIA
  204. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_camellia;
  205. #endif
  206. #if USE_IDEA
  207. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_idea;
  208. #endif
  209. #if USE_GOST28147
  210. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_gost28147;
  211. #endif
  212. #if USE_GOST28147
  213. extern const gcry_mac_spec_t _gcry_mac_type_spec_gost28147_imit;
  214. #endif
  215. #if USE_SM4
  216. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_sm4;
  217. #endif
  218. #if USE_ARIA
  219. extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_aria;
  220. #endif
  221. /*
  222. * The GMAC algorithm specifications (mac-gmac.c).
  223. */
  224. #if USE_AES
  225. extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_aes;
  226. #endif
  227. #if USE_TWOFISH
  228. extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_twofish;
  229. #endif
  230. #if USE_SERPENT
  231. extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_serpent;
  232. #endif
  233. #if USE_SEED
  234. extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_seed;
  235. #endif
  236. #if USE_CAMELLIA
  237. extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_camellia;
  238. #endif
  239. #if USE_SM4
  240. extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_sm4;
  241. #endif
  242. #if USE_ARIA
  243. extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_aria;
  244. #endif
  245. /*
  246. * The Poly1305 MAC algorithm specifications (mac-poly1305.c).
  247. */
  248. extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac;
  249. #if USE_AES
  250. extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_aes;
  251. #endif
  252. #if USE_CAMELLIA
  253. extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_camellia;
  254. #endif
  255. #if USE_TWOFISH
  256. extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_twofish;
  257. #endif
  258. #if USE_SERPENT
  259. extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_serpent;
  260. #endif
  261. #if USE_SEED
  262. extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_seed;
  263. #endif
  264. #if USE_SM4
  265. extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_sm4;
  266. #endif
  267. #if USE_ARIA
  268. extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_aria;
  269. #endif