summaryrefslogtreecommitdiffstats
path: root/gnulib-tests/test-sm3-buffer.c
blob: 7fa89e03797e6a132c1e1f0d3a615dc22192b9b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (C) 2017-2023 Free Software Foundation, Inc.
 * Written by Jia Zhang <qianyue.zj@alibaba-inc.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

#include "sm3.h"

#include <stdio.h>
#include <string.h>

static int
test (const char *in, const char *out)
{
  char buf[SM3_DIGEST_SIZE];

  if (memcmp (sm3_buffer (in, strlen (in), buf),
              out, SM3_DIGEST_SIZE) != 0)
    {
      size_t i;
      printf ("expected:\n");
      for (i = 0; i < SM3_DIGEST_SIZE; i++)
        printf ("%02x ", out[i] & 0xFFu);
      printf ("\ncomputed:\n");
      for (i = 0; i < SM3_DIGEST_SIZE; i++)
        printf ("%02x ", buf[i] & 0xFFu);
      printf ("\n");
      return 1;
    }

  return 0;
}

int
main (void)
{
  /* Test vectors from GM/T 004-2012 */
  const char *in[] =
    {
      "abc",
      "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd",
    };
  const char *out[] =
    {
      "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4\xe2"
      "\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b\xa8\xe0",
      "\xde\xbe\x9f\xf9\x22\x75\xb8\xa1\x38\x60\x48\x89\xc1\x8e\x5a\x4d"
      "\x6f\xdb\x70\xe5\x38\x7e\x57\x65\x29\x3d\xcb\xa3\x9c\x0c\x57\x32",
    };
  size_t i;

  for (i = 0; i < sizeof (in) / sizeof (in[0]); i++)
    {
      if (test (in[i], out[i]))
        return 1;
    }

  return 0;
}