root/maint/gnulib/lib/hmac.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. hmac_hash
  2. GL_HMAC_FN

   1 /* hmac.c -- hashed message authentication codes
   2    Copyright (C) 2005-2006, 2009-2021 Free Software Foundation, Inc.
   3 
   4    This file is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU Lesser General Public License as
   6    published by the Free Software Foundation; either version 2.1 of the
   7    License, or (at your option) any later version.
   8 
   9    This file 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 Lesser General Public License for more details.
  13 
  14    You should have received a copy of the GNU Lesser General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 #include <string.h>
  18 
  19 #include "memxor.h"
  20 
  21 #define IPAD 0x36
  22 #define OPAD 0x5c
  23 
  24 /* Concatenate two preprocessor tokens.  */
  25 #define _GLHMAC_CONCAT_(prefix, suffix) prefix##suffix
  26 #define _GLHMAC_CONCAT(prefix, suffix) _GLHMAC_CONCAT_ (prefix, suffix)
  27 
  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
  33 
  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)
  40 
  41 static void
  42 hmac_hash (const void *key, size_t keylen,
     /* [previous][next][first][last][top][bottom][index][help] */
  43            const void *in, size_t inlen,
  44            int pad, void *resbuf)
  45 {
  46   struct GL_HMAC_CTX hmac_ctx;
  47   char block[GL_HMAC_BLOCKSIZE];
  48 
  49   memset (block, pad, sizeof block);
  50   memxor (block, key, keylen);
  51 
  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);
  56 }
  57 
  58 int
  59 GL_HMAC_FN (const void *key, size_t keylen,
     /* [previous][next][first][last][top][bottom][index][help] */
  60             const void *in, size_t inlen, void *resbuf)
  61 {
  62   char optkeybuf[GL_HMAC_HASHSIZE];
  63   char innerhash[GL_HMAC_HASHSIZE];
  64 
  65   /* Ensure key size is <= block size.  */
  66   if (keylen > GL_HMAC_BLOCKSIZE)
  67     {
  68       struct GL_HMAC_CTX keyhash;
  69 
  70       GL_HMAC_FN_INIT (&keyhash);
  71       GL_HMAC_FN_PROC (key, keylen, &keyhash);
  72       GL_HMAC_FN_FINI (&keyhash, optkeybuf);
  73 
  74       key = optkeybuf;
  75       /* zero padding of the key to the block size
  76          is implicit in the memxor.  */
  77       keylen = sizeof optkeybuf;
  78     }
  79 
  80   /* Compute INNERHASH from KEY and IN.  */
  81   hmac_hash (key, keylen, in, inlen, IPAD, innerhash);
  82 
  83   /* Compute result from KEY and INNERHASH.  */
  84   hmac_hash (key, keylen, innerhash, sizeof innerhash, OPAD, resbuf);
  85 
  86   return 0;
  87 }

/* [previous][next][first][last][top][bottom][index][help] */