root/maint/gnulib/tests/test-sm3-buffer.c

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

DEFINITIONS

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

   1 /*
   2  * Copyright (C) 2017-2021 Free Software Foundation, Inc.
   3  * Written by Jia Zhang <qianyue.zj@alibaba-inc.com>
   4  *
   5  * This program is free software: you can redistribute it and/or modify
   6  * it under the terms of the GNU General Public License as published by
   7  * the Free Software Foundation; either version 3 of the License, or
   8  * (at your option) any later version.
   9  *
  10  * This program is distributed in the hope that it will be useful,
  11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  * GNU General Public License for more details.
  14  *
  15  * You should have received a copy of the GNU General Public License
  16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  17 
  18 #include <config.h>
  19 
  20 #include "sm3.h"
  21 
  22 #include <stdio.h>
  23 #include <string.h>
  24 
  25 static int
  26 test (const char *in, const char *out)
     /* [previous][next][first][last][top][bottom][index][help] */
  27 {
  28   char buf[SM3_DIGEST_SIZE];
  29 
  30   if (memcmp (sm3_buffer (in, strlen (in), buf),
  31               out, SM3_DIGEST_SIZE) != 0)
  32     {
  33       size_t i;
  34       printf ("expected:\n");
  35       for (i = 0; i < SM3_DIGEST_SIZE; i++)
  36         printf ("%02x ", out[i] & 0xFFu);
  37       printf ("\ncomputed:\n");
  38       for (i = 0; i < SM3_DIGEST_SIZE; i++)
  39         printf ("%02x ", buf[i] & 0xFFu);
  40       printf ("\n");
  41       return 1;
  42     }
  43 
  44   return 0;
  45 }
  46 
  47 int
  48 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50   /* Test vectors from GM/T 004-2012 */
  51   const char *in[] =
  52     {
  53       "abc",
  54       "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd",
  55     };
  56   const char *out[] =
  57     {
  58       "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4\xe2"
  59       "\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b\xa8\xe0",
  60       "\xde\xbe\x9f\xf9\x22\x75\xb8\xa1\x38\x60\x48\x89\xc1\x8e\x5a\x4d"
  61       "\x6f\xdb\x70\xe5\x38\x7e\x57\x65\x29\x3d\xcb\xa3\x9c\x0c\x57\x32",
  62     };
  63   size_t i;
  64 
  65   for (i = 0; i < sizeof (in) / sizeof (in[0]); i++)
  66     {
  67       if (test (in[i], out[i]))
  68         return 1;
  69     }
  70 
  71   return 0;
  72 }

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