bitset: properly use false/true instead of 0/1 for Booleans
[gnulib.git] / lib / hmac.c
blobf3d20d63d13e49f8f423f4d0e8bd515fbd4a8c91
1 /* hmac.c -- hashed message authentication codes
2 Copyright (C) 2005-2006, 2009-2018 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 #include <string.h>
19 #include "memxor.h"
21 #define IPAD 0x36
22 #define OPAD 0x5c
24 /* Concatenate two preprocessor tokens. */
25 #define _GLHMAC_CONCAT_(prefix, suffix) prefix##suffix
26 #define _GLHMAC_CONCAT(prefix, suffix) _GLHMAC_CONCAT_ (prefix, suffix)
28 #if GL_HMAC_NAME == 5
29 # define HMAC_ALG md5
30 #else
31 # define HMAC_ALG _GLHMAC_CONCAT (sha, GL_HMAC_NAME)
32 #endif
34 #define GL_HMAC_CTX _GLHMAC_CONCAT (HMAC_ALG, _ctx)
35 #define GL_HMAC_FN _GLHMAC_CONCAT (hmac_, HMAC_ALG)
36 #define GL_HMAC_FN_INIT _GLHMAC_CONCAT (HMAC_ALG, _init_ctx)
37 #define GL_HMAC_FN_BLOC _GLHMAC_CONCAT (HMAC_ALG, _process_block)
38 #define GL_HMAC_FN_PROC _GLHMAC_CONCAT (HMAC_ALG, _process_bytes)
39 #define GL_HMAC_FN_FINI _GLHMAC_CONCAT (HMAC_ALG, _finish_ctx)
41 static void
42 hmac_hash (const void *key, size_t keylen,
43 const void *in, size_t inlen,
44 int pad, void *resbuf)
46 struct GL_HMAC_CTX hmac_ctx;
47 char block[GL_HMAC_BLOCKSIZE];
49 memset (block, pad, sizeof block);
50 memxor (block, key, keylen);
52 GL_HMAC_FN_INIT (&hmac_ctx);
53 GL_HMAC_FN_BLOC (block, sizeof block, &hmac_ctx);
54 GL_HMAC_FN_PROC (in, inlen, &hmac_ctx);
55 GL_HMAC_FN_FINI (&hmac_ctx, resbuf);
58 int
59 GL_HMAC_FN (const void *key, size_t keylen,
60 const void *in, size_t inlen, void *resbuf)
62 char optkeybuf[GL_HMAC_HASHSIZE];
63 char innerhash[GL_HMAC_HASHSIZE];
65 /* Ensure key size is <= block size. */
66 if (keylen > GL_HMAC_BLOCKSIZE)
68 struct GL_HMAC_CTX keyhash;
70 GL_HMAC_FN_INIT (&keyhash);
71 GL_HMAC_FN_PROC (key, keylen, &keyhash);
72 GL_HMAC_FN_FINI (&keyhash, optkeybuf);
74 key = optkeybuf;
75 /* zero padding of the key to the block size
76 is implicit in the memxor. */
77 keylen = sizeof optkeybuf;
80 /* Compute INNERHASH from KEY and IN. */
81 hmac_hash (key, keylen, in, inlen, IPAD, innerhash);
83 /* Compute result from KEY and INNERHASH. */
84 hmac_hash (key, keylen, innerhash, sizeof innerhash, OPAD, resbuf);
86 return 0;