1  /*
       2   * This crypt(3) validation program shipped with UFC-crypt
       3   * is derived from one distributed with Phil Karns PD DES package.
       4   *
       5   * @(#)cert.c   1.8 11 Aug 1996
       6   */
       7  
       8  #include "crypt-port.h"
       9  #include "alg-des.h"
      10  #include "des-cases.h"
      11  
      12  #include <stdio.h>
      13  
      14  #if INCLUDE_descrypt || INCLUDE_bsdicrypt || INCLUDE_bigcrypt
      15  
      16  static void
      17  v_print (const unsigned char v[8])
      18  {
      19    for (int i = 0; i < 8; i++)
      20      printf ("%02x", (unsigned int)v[i]);
      21  }
      22  
      23  static void
      24  report_failure (size_t n, bool decrypt,
      25                  const struct des_testcase *tc, const unsigned char got[8])
      26  {
      27    printf ("FAIL: %zu/%s: k=", n, decrypt ? "de" : "en");
      28    v_print (tc->key);
      29    fputs ("  exp ", stdout);
      30    if (decrypt)
      31      v_print (tc->plain);
      32    else
      33      v_print (tc->answer);
      34    fputs ("  got ", stdout);
      35    v_print (got);
      36    putchar ('\n');
      37  }
      38  
      39  int
      40  main (void)
      41  {
      42    struct des_ctx ctx;
      43    const struct des_testcase *tc;
      44    unsigned char got[8];
      45    size_t t;
      46    int status = 0;
      47  
      48    des_set_salt (&ctx, 0);
      49  
      50    for (t = 0; t < N_DES_TESTCASES; t++)
      51      {
      52        tc = &des_testcases[t];
      53        des_set_key (&ctx, tc->key);
      54        des_crypt_block (&ctx, got, tc->plain, 0, false);
      55        if (memcmp (got, tc->answer, 8) != 0)
      56          {
      57            status = 1;
      58            report_failure (t, false, tc, got);
      59          }
      60  
      61        des_crypt_block (&ctx, got, tc->answer, 0, true);
      62        if (memcmp (got, tc->plain, 8) != 0)
      63          {
      64            status = 1;
      65            report_failure (t, true, tc, got);
      66          }
      67      }
      68  
      69    return status;
      70  }
      71  
      72  #else
      73  
      74  int
      75  main (void)
      76  {
      77    return 77; /* UNSUPPORTED */
      78  }
      79  
      80  #endif