root/maint/gnulib/tests/test-hmac-sha256.c

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

DEFINITIONS

This source file includes following definitions.
  1. hmac_check
  2. main

   1 /*
   2  * Copyright (C) 2005, 2010-2021 Free Software Foundation, Inc.
   3  *
   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 3 of the License, or
   7  * (at your option) any later version.
   8  *
   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.
  13  *
  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/>.  */
  16 
  17 /* Written by Simon Josefsson.  Test vectors from RFC 4231.  */
  18 
  19 #include <config.h>
  20 
  21 #include "hmac.h"
  22 
  23 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <string.h>
  26 
  27 static void
  28 hmac_check (const void *key, size_t key_len,
     /* [previous][next][first][last][top][bottom][index][help] */
  29             const void *data, size_t data_len, const char *digest)
  30 {
  31   char out[32];
  32 
  33   if (hmac_sha256 (key, key_len, data, data_len, out) != 0)
  34     {
  35       printf ("call failure\n");
  36       exit (1);
  37     }
  38 
  39   if (memcmp (digest, out, 32) != 0)
  40     {
  41       size_t i;
  42       printf ("hash 1 mismatch. expected:\n");
  43       for (i = 0; i < 32; i++)
  44         printf ("%02x ", digest[i] & 0xFF);
  45       printf ("\ncomputed:\n");
  46       for (i = 0; i < 32; i++)
  47         printf ("%02x ", out[i] & 0xFF);
  48       printf ("\n");
  49       exit (1);
  50     }
  51 }
  52 
  53 int
  54 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  55 {
  56   {
  57     char key[20];
  58     size_t key_len = sizeof key;
  59     memset (key, '\x0b', sizeof key);
  60     char *data = "Hi There";
  61     size_t data_len = 8;
  62     char *digest =
  63       "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b"
  64       "\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7";
  65     hmac_check (key, key_len, data, data_len, digest);
  66   }
  67 
  68   {
  69     char *key = "Jefe";
  70     size_t key_len = 4;
  71     char *data = "what do ya want for nothing?";
  72     size_t data_len = 28;
  73     char *digest =
  74       "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7"
  75       "\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43";
  76     hmac_check (key, key_len, data, data_len, digest);
  77   }
  78 
  79   {
  80     char key[20];
  81     size_t key_len = sizeof key;
  82     memset (key, '\xAA', sizeof key);
  83     char data[50];
  84     size_t data_len = sizeof data;
  85     memset (data, '\xDD', sizeof data);
  86     char *digest =
  87       "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81\xa7"
  88       "\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5\x65\xfe";
  89     hmac_check (key, key_len, data, data_len, digest);
  90   }
  91 
  92   {
  93     char key[65];
  94     size_t key_len = sizeof key;
  95     memset (key, '\x0b', sizeof key);
  96     char *data = "Hi There";
  97     size_t data_len = 8;
  98     char *digest =
  99       "\x72\x7b\x82\xfb\xa2\x64\x39\x3c\x5d\x67\xfd\x6d\x6a\xd7\x83\xe9"
 100       "\x01\x9a\x1f\xa6\xa8\x57\xfc\xcb\x70\xf5\x85\x2f\x04\xbe\x5d\x5d";
 101     hmac_check (key, key_len, data, data_len, digest);
 102   }
 103 
 104   return 0;
 105 }

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