root/maint/gnulib/tests/test-hmac-md5.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.  */
  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 /* Test vectors from RFC 2104. */
  28 
  29 static void
  30 hmac_check (const void *key, size_t key_len,
     /* [previous][next][first][last][top][bottom][index][help] */
  31             const void *data, size_t data_len, const char *digest)
  32 {
  33   char out[16];
  34 
  35   if (hmac_md5 (key, key_len, data, data_len, out) != 0)
  36     {
  37       printf ("call failure\n");
  38       exit (1);
  39     }
  40 
  41   if (memcmp (digest, out, 16) != 0)
  42     {
  43       size_t i;
  44       printf ("hash 1 mismatch. expected:\n");
  45       for (i = 0; i < 16; i++)
  46         printf ("%02x ", digest[i] & 0xFF);
  47       printf ("\ncomputed:\n");
  48       for (i = 0; i < 16; i++)
  49         printf ("%02x ", out[i] & 0xFF);
  50       printf ("\n");
  51       exit (1);
  52     }
  53 }
  54 
  55 int
  56 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58   {
  59     char key[16];
  60     size_t key_len = sizeof key;
  61     memset (key, '\x0b', sizeof key);
  62     char *data = "Hi There";
  63     size_t data_len = 8;
  64     char *digest =
  65       "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
  66     hmac_check (key, key_len, data, data_len, digest);
  67   }
  68 
  69   {
  70     char *key = "Jefe";
  71     size_t key_len = 4;
  72     char *data = "what do ya want for nothing?";
  73     size_t data_len = 28;
  74     char *digest =
  75       "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
  76     hmac_check (key, key_len, data, data_len, digest);
  77   }
  78 
  79   {
  80     char key[16];
  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       "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
  88     hmac_check (key, key_len, data, data_len, digest);
  89   }
  90 
  91   {
  92     char key[65];
  93     size_t key_len = sizeof key;
  94     memset (key, '\x0b', sizeof key);
  95     char *data = "Hi There";
  96     size_t data_len = 8;
  97     char *digest =
  98       "\xd6\x07\x5b\xee\x4d\x91\x80\xd8\xd1\xa2\x99\x29\x5e\x7c\xc9\xcb";
  99     hmac_check (key, key_len, data, data_len, digest);
 100   }
 101 
 102   return 0;
 103 }

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