(root)/
libxcrypt-4.4.36/
test/
alg-gost3411-2012.c
       1  /* Copyright (C) 2018 vt@altlinux.org
       2   * Copyright (C) 2018 Björn Esser besser82@fedoraproject.org
       3   *
       4   * Redistribution and use in source and binary forms, with or without
       5   * modification, are permitted.
       6   *
       7   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
       8   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       9   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      10   * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
      11   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      12   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      13   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      14   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      15   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      16   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      17   * SUCH DAMAGE.
      18   */
      19  
      20  #include "crypt-port.h"
      21  
      22  #if INCLUDE_gost_yescrypt
      23  
      24  #include "alg-gost3411-2012-hmac.h"
      25  
      26  #include <stdio.h>
      27  
      28  static void
      29  dumphex(const void *ptr, size_t size)
      30  {
      31    size_t i;
      32  
      33    for (i = 0; i < size; i++)
      34      printf("%02x", ((const unsigned char *)ptr)[i]);
      35    printf("\n");
      36  }
      37  
      38  static int
      39  test_gost2012_hash(const char *subject, const char *t, const char *match)
      40  {
      41    size_t i;
      42    uint8_t digest[32];
      43    GOST34112012Context ctx;
      44  
      45    gost_hash256((const uint8_t *)t, strlen(t), digest, &ctx);
      46  
      47    char dgt[32 * 2 + 1];
      48    for (i = 0; i < sizeof(digest); i++)
      49      sprintf(&dgt[i * 2], "%02x", digest[i]);
      50  
      51    if (strcmp(dgt, match) != 0)
      52      {
      53        fprintf(stderr, "ERROR: %s\n", subject);
      54        printf("   t[%zu] = ", strlen(t));
      55        dumphex(t, strlen(t));
      56        printf("   digest(%zu) = %s",
      57               sizeof(digest), dgt);
      58        printf("   expected(%zu) = %s\n",
      59               strlen(match) / 2, match);
      60        return 1;
      61      }
      62    else
      63      fprintf(stderr, "   ok: %s\n", subject);
      64  
      65    return 0;
      66  }
      67  
      68  static int
      69  test_gost2012_hash512(const char *subject, const char *t, const char *match)
      70  {
      71    size_t i;
      72    size_t len = strlen(t);
      73    size_t lh = len / 2;
      74    uint8_t digest[64];
      75    GOST34112012Context ctx;
      76  
      77    GOST34112012Init(&ctx, GOSTR3411_2012_BITS * 2);
      78  
      79    /* Operate on len < 64 for coverage */
      80    GOST34112012Update(&ctx, (const uint8_t *)t, lh);
      81    GOST34112012Update(&ctx, (const uint8_t *)t + lh, len - lh);
      82  
      83    GOST34112012Final(&ctx, digest);
      84  
      85    char dgt[64 * 2 + 1];
      86    for (i = 0; i < sizeof(digest); i++)
      87      sprintf(&dgt[i * 2], "%02x", digest[i]);
      88  
      89    if (strcmp(dgt, match) != 0)
      90      {
      91        fprintf(stderr, "ERROR: %s\n", subject);
      92        printf("   t[%zu] = ", strlen(t));
      93        dumphex(t, strlen(t));
      94        printf("   digest(%zu) = %s",
      95               sizeof(digest), dgt);
      96        printf("   expected(%zu) = %s\n",
      97               strlen(match) / 2, match);
      98        return 1;
      99      }
     100    else
     101      fprintf(stderr, "   ok: %s\n", subject);
     102  
     103    return 0;
     104  }
     105  
     106  int
     107  main (void)
     108  {
     109    int result = 0;
     110  
     111    result |= test_gost2012_hash(
     112                "test vector from example A.1 from GOST-34.11-2012 (256 Bit)",
     113                "012345678901234567890123456789012345678901234567890123456789012",
     114                "9d151eefd8590b89daa6ba6cb74af9275dd051026bb149a452fd84e5e57b5500");
     115  
     116    result |= !test_gost2012_hash(
     117                "false positive test vector (256 Bit)",
     118                "012345678901234567890123456789012345678901234567890123456789012",
     119                "012345678901234567890123456789012345678901234567890123456789012");
     120  
     121    result |= test_gost2012_hash(
     122                "test vector from example A.2 from GOST-34.11-2012 (256 Bit)",
     123                "\xD1\xE5\x20\xE2\xE5\xF2\xF0\xE8\x2C\x20\xD1\xF2\xF0\xE8\xE1\xEE"
     124                "\xE6\xE8\x20\xE2\xED\xF3\xF6\xE8\x2C\x20\xE2\xE5\xFE\xF2\xFA\x20"
     125                "\xF1\x20\xEC\xEE\xF0\xFF\x20\xF1\xF2\xF0\xE5\xEB\xE0\xEC\xE8\x20"
     126                "\xED\xE0\x20\xF5\xF0\xE0\xE1\xF0\xFB\xFF\x20\xEF\xEB\xFA\xEA\xFB"
     127                "\x20\xC8\xE3\xEE\xF0\xE5\xE2\xFB",
     128                "9dd2fe4e90409e5da87f53976d7405b0c0cac628fc669a741d50063c557e8f50");
     129  
     130    /* carry test */
     131    result |= test_gost2012_hash(
     132                "carry test vector from gost-engine (256 Bit)",
     133                "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
     134                "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
     135                "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
     136                "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
     137                "\x16\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
     138                "\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
     139                "\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
     140                "\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x16",
     141                "81bb632fa31fcc38b4c379a662dbc58b9bed83f50d3a1b2ce7271ab02d25babb");
     142  
     143    /* 512 bit hash test for completeness */
     144    result |= test_gost2012_hash512(
     145                "test vector from example A.1 from GOST-34.11-2012 (512 bit)",
     146                "012345678901234567890123456789012345678901234567890123456789012",
     147                "1b54d01a4af5b9d5cc3d86d68d285462b19abc2475222f35c085122be4ba1ffa"
     148                "00ad30f8767b3a82384c6574f024c311e2a481332b08ef7f41797891c1646f48");
     149  
     150    result |= !test_gost2012_hash512(
     151                "false positive test vector (512 bit)",
     152                "012345678901234567890123456789012345678901234567890123456789012",
     153                "0123456789012345678901234567890123456789012345678901234567890120"
     154                "1234567890123456789012345678901234567890123456789012345678901234");
     155  
     156    result |= test_gost2012_hash512(
     157                "test vector from example A.2 from GOST-34.11-2012 (512 bit)",
     158                "\xD1\xE5\x20\xE2\xE5\xF2\xF0\xE8\x2C\x20\xD1\xF2\xF0\xE8\xE1\xEE"
     159                "\xE6\xE8\x20\xE2\xED\xF3\xF6\xE8\x2C\x20\xE2\xE5\xFE\xF2\xFA\x20"
     160                "\xF1\x20\xEC\xEE\xF0\xFF\x20\xF1\xF2\xF0\xE5\xEB\xE0\xEC\xE8\x20"
     161                "\xED\xE0\x20\xF5\xF0\xE0\xE1\xF0\xFB\xFF\x20\xEF\xEB\xFA\xEA\xFB"
     162                "\x20\xC8\xE3\xEE\xF0\xE5\xE2\xFB",
     163                "1e88e62226bfca6f9994f1f2d51569e0daf8475a3b0fe61a5300eee46d961376"
     164                "035fe83549ada2b8620fcd7c496ce5b33f0cb9dddc2b6460143b03dabac9fb28");
     165  
     166    /* carry test */
     167    result |= test_gost2012_hash512(
     168                "carry test vector from gost-engine (512 Bit)",
     169                "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
     170                "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
     171                "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
     172                "\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
     173                "\x16\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
     174                "\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
     175                "\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11"
     176                "\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x16",
     177                "8b06f41e59907d9636e892caf5942fcdfb71fa31169a5e70f0edb873664df41c"
     178                "2cce6e06dc6755d15a61cdeb92bd607cc4aaca6732bf3568a23a210dd520fd41");
     179  
     180    return result;
     181  }
     182  
     183  #else
     184  
     185  int
     186  main (void)
     187  {
     188    return 77; /* UNSUPPORTED */
     189  }
     190  
     191  #endif /* INCLUDE_gost_yescrypt */