Blame test-alg-sha1.c

Packit Bot e13ba5
/*
Packit Bot e13ba5
 * SHA-1 in C
Packit Bot e13ba5
 * By Steve Reid <sreid@sea-to-sky.net>
Packit Bot e13ba5
 * 100% Public Domain
Packit Bot e13ba5
*/
Packit Bot e13ba5
Packit Bot e13ba5
#include "crypt-port.h"
Packit Bot e13ba5
#include "alg-sha1.h"
Packit Bot e13ba5
Packit Bot e13ba5
#include <stdio.h>
Packit Bot e13ba5
Packit Bot e13ba5
#if INCLUDE_sha1
Packit Bot e13ba5
Packit Bot e13ba5
/* Test Vectors (from FIPS PUB 180-1) */
Packit Bot e13ba5
const char *test_data[3] =
Packit Bot e13ba5
{
Packit Bot e13ba5
  "abc",
Packit Bot e13ba5
  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
Packit Bot e13ba5
  "A million repetitions of 'a'"
Packit Bot e13ba5
};
Packit Bot e13ba5
Packit Bot e13ba5
const char *test_results[3] =
Packit Bot e13ba5
{
Packit Bot e13ba5
  "a9993e364706816aba3e25717850c26c9cd0d89d",
Packit Bot e13ba5
  "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
Packit Bot e13ba5
  "34aa973cd4c4daa4f61eeb2bdbad27316534016f"
Packit Bot e13ba5
};
Packit Bot e13ba5
Packit Bot e13ba5
Packit Bot e13ba5
static inline void
Packit Bot e13ba5
bin_to_hex (uint8_t *digest, char *output)
Packit Bot e13ba5
{
Packit Bot e13ba5
  for (uint8_t i = 0; i < 20; ++i)
Packit Bot e13ba5
    {
Packit Bot e13ba5
      sprintf (output, "%02x", *digest);
Packit Bot e13ba5
      ++digest;
Packit Bot e13ba5
      output += 2;
Packit Bot e13ba5
    }
Packit Bot e13ba5
}
Packit Bot e13ba5
Packit Bot e13ba5
Packit Bot e13ba5
int
Packit Bot e13ba5
main (void)
Packit Bot e13ba5
{
Packit Bot e13ba5
  int k;
Packit Bot e13ba5
  struct sha1_ctx ctx;
Packit Bot e13ba5
  uint8_t digest[20];
Packit Bot e13ba5
  char output[80];
Packit Bot e13ba5
  uint8_t retval = 0;
Packit Bot e13ba5
Packit Bot e13ba5
  for (k = 0; k < 2; k++)
Packit Bot e13ba5
    {
Packit Bot e13ba5
      sha1_init_ctx (&ctx;;
Packit Bot e13ba5
      sha1_process_bytes ((const uint8_t*)test_data[k], &ctx, strlen(test_data[k]));
Packit Bot e13ba5
      sha1_finish_ctx (&ctx, digest);
Packit Bot e13ba5
      bin_to_hex(digest, output);
Packit Bot e13ba5
Packit Bot e13ba5
      if (strcmp(output, test_results[k]))
Packit Bot e13ba5
        {
Packit Bot e13ba5
          fprintf(stdout, "FAIL\n");
Packit Bot e13ba5
          fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[k]);
Packit Bot e13ba5
          fprintf(stderr,"\t%s returned\n", output);
Packit Bot e13ba5
          fprintf(stderr,"\t%s is correct\n", test_results[k]);
Packit Bot e13ba5
          retval = 1;
Packit Bot e13ba5
        }
Packit Bot e13ba5
    }
Packit Bot e13ba5
  /* million 'a' vector we feed separately */
Packit Bot e13ba5
  sha1_init_ctx (&ctx;;
Packit Bot e13ba5
  for (k = 0; k < 1000000; k++)
Packit Bot e13ba5
    sha1_process_bytes ((const uint8_t*)"a", &ctx, 1);
Packit Bot e13ba5
  sha1_finish_ctx (&ctx, digest);
Packit Bot e13ba5
  bin_to_hex(digest, output);
Packit Bot e13ba5
  if (strcmp(output, test_results[2]))
Packit Bot e13ba5
    {
Packit Bot e13ba5
      fprintf(stdout, "FAIL\n");
Packit Bot e13ba5
      fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]);
Packit Bot e13ba5
      fprintf(stderr,"\t%s returned\n", output);
Packit Bot e13ba5
      fprintf(stderr,"\t%s is correct\n", test_results[2]);
Packit Bot e13ba5
      retval = 1;
Packit Bot e13ba5
    }
Packit Bot e13ba5
Packit Bot e13ba5
  /* success */
Packit Bot e13ba5
  return retval;
Packit Bot e13ba5
}
Packit Bot e13ba5
Packit Bot e13ba5
#else
Packit Bot e13ba5
Packit Bot e13ba5
int
Packit Bot e13ba5
main (void)
Packit Bot e13ba5
{
Packit Bot e13ba5
  return 77; /* UNSUPPORTED */
Packit Bot e13ba5
}
Packit Bot e13ba5
Packit Bot e13ba5
#endif