root/maint/gnulib/tests/test-hmac-sha512.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[64];
  32 
  33   if (hmac_sha512 (key, key_len, data, data_len, out) != 0)
  34     {
  35       printf ("call failure\n");
  36       exit (1);
  37     }
  38 
  39   if (memcmp (digest, out, 64) != 0)
  40     {
  41       size_t i;
  42       printf ("hash 1 mismatch. expected:\n");
  43       for (i = 0; i < 64; i++)
  44         printf ("%02x ", digest[i] & 0xFF);
  45       printf ("\ncomputed:\n");
  46       for (i = 0; i < 64; 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       "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0"
  64       "\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde"
  65       "\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4"
  66       "\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54";
  67     hmac_check (key, key_len, data, data_len, digest);
  68   }
  69 
  70   {
  71     char *key = "Jefe";
  72     size_t key_len = 4;
  73     char *data = "what do ya want for nothing?";
  74     size_t data_len = 28;
  75     char *digest =
  76       "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0\xa3"
  77       "\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25\x05\x54"
  78       "\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8\xf0\xe6\xfd"
  79       "\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a\x38\xbc\xe7\x37";
  80     hmac_check (key, key_len, data, data_len, digest);
  81   }
  82 
  83   {
  84     char key[20];
  85     size_t key_len = sizeof key;
  86     memset (key, '\xAA', sizeof key);
  87     char data[50];
  88     size_t data_len = sizeof data;
  89     memset (data, '\xDD', sizeof data);
  90     char *digest =
  91       "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b\xe9"
  92       "\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27\x9d\x39"
  93       "\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e\x67\xc8\x07"
  94       "\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59\xe1\x32\x92\xfb";
  95     hmac_check (key, key_len, data, data_len, digest);
  96   }
  97 
  98   {
  99     char key[129];
 100     size_t key_len = sizeof key;
 101     memset (key, '\x0b', sizeof key);
 102     char *data = "Hi There";
 103     size_t data_len = 8;
 104     char *digest =
 105       "\xaa\x1c\x23\xfe\x04\x0c\x4f\x3e\x65\x45\xa9\x15\x4e\x33\x9d\x17"
 106       "\xff\xb5\x27\x2e\x0a\x54\x5b\x84\xd3\x8b\x9b\xf8\xe2\xc7\x46\x4d"
 107       "\xf2\xd6\x2b\xb5\x00\x05\x57\x68\x6f\x85\x10\xeb\x43\x02\xa0\xca"
 108       "\xe6\xb5\xdd\x1f\x37\x00\xbe\xae\xde\x75\x5f\x86\xfd\xbe\xb4\x8f";
 109     hmac_check (key, key_len, data, data_len, digest);
 110   }
 111 
 112   return 0;
 113 }

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