int main()

in testing/drivers/crypto/hash.c [382:673]


int main(void)
{
  crypto_context md5_ctx;
  crypto_context sha1_ctx;
  crypto_context sha2_256_ctx;
  crypto_context sha2_512_ctx;
  unsigned char output[64];
  unsigned char buf[1024];
  int ret = 0;
  int i;
  int j;

  ret += syshash_init(&md5_ctx);
  ret += syshash_init(&sha1_ctx);
  ret += syshash_init(&sha2_256_ctx);
  ret += syshash_init(&sha2_512_ctx);
  if (ret != 0)
    {
      printf("syshash init failed\n");
    }

  for (i = 0; i < nitems(md5_testcase); i++)
    {
      ret = syshash_start(&md5_ctx, CRYPTO_MD5);
      if (ret != 0)
        {
          printf("syshash md5 start failed\n");
          goto err;
        }

      ret = syshash_update(&md5_ctx, md5_testcase[i].data,
                           md5_testcase[i].datalen);
      if (ret)
        {
          printf("syshash md5 update failed\n");
          goto err;
        }

      ret = syshash_finish(&md5_ctx, output);
      if (ret)
        {
          printf("syshash md5 finish failed\n");
          goto err;
        }

      ret = match(md5_result[i], output, MD5_DIGEST_LENGTH);
      if (ret)
        {
          printf("match md5 failed\n");
          goto err;
        }
      else
        {
          printf("hash md5 success\n");
        }
    }

  for (i = 0; i < nitems(sha_testcase); i++)
    {
      ret = syshash_start(&sha1_ctx, CRYPTO_SHA1);
      if (ret != 0)
        {
          printf("syshash sha1 start failed\n");
          goto err;
        }

      if (i == 2)
        {
          memset(buf, 'a', sha_testcase[i].datalen);
          for (j = 0; j < 1000; j++)
            {
              ret = syshash_update(&sha1_ctx, (char *)buf,
                                   sha_testcase[i].datalen);
              if (ret)
                {
                  break;
                }
            }
        }
      else
        {
          ret = syshash_update(&sha1_ctx, sha_testcase[i].data,
                               sha_testcase[i].datalen);
        }

      if (ret)
        {
          printf("sha1 update failed\n");
          goto err;
        }

      ret = syshash_finish(&sha1_ctx, output);
      if (ret)
        {
          printf("sha1 finish failed\n");
          goto err;
        }

      ret = match((unsigned char *)sha1_result[i],
                  (unsigned char *)output,
                  SHA1_DIGEST_LENGTH);
      if (ret)
        {
          printf("match sha1 failed\n");
          goto err;
        }
      else
        {
          printf("hash sha1 success\n");
        }
    }

  for (i = 0; i < nitems(sha_testcase); i++)
    {
      ret = syshash_start(&sha2_256_ctx, CRYPTO_SHA2_256);
      if (ret != 0)
        {
          printf("sha256 start failed\n");
          goto err;
        }

      if (i == 2)
        {
          memset(buf, 'a', sha_testcase[i].datalen);
          for (j = 0; j < 1000; j++)
            {
              ret = syshash_update(&sha2_256_ctx, (char *)buf,
                                   sha_testcase[i].datalen);
              if (ret)
                {
                  break;
                }
            }
        }
      else
        {
          ret = syshash_update(&sha2_256_ctx, sha_testcase[i].data,
                               sha_testcase[i].datalen);
        }

      if (ret)
        {
          printf("sha256 update failed\n");
          goto err;
        }

      ret = syshash_finish(&sha2_256_ctx, output);
      if (ret)
        {
          printf("sha256 finish failed\n");
        }

      ret = match((unsigned char *)sha256_result[i],
                  (unsigned char *)output,
                  SHA256_DIGEST_LENGTH);
      if (ret)
        {
          printf("match sha256 failed\n");
        }
      else
        {
          printf("hash sha256 success\n");
        }
    }

  for (i = 0; i < nitems(sha512_testcase); i++)
    {
      ret = syshash_start(&sha2_512_ctx, CRYPTO_SHA2_512);
      if (ret != 0)
        {
          printf("sha512 start failed\n");
          goto err;
        }

      if (i == 2)
        {
          memset(buf, 'a', sha512_testcase[i].datalen);
          for (j = 0; j < 1000; j++)
            {
              ret = syshash_update(&sha2_512_ctx, (char *)buf,
                                   sha512_testcase[i].datalen);
              if (ret)
                {
                  break;
                }
            }
        }
      else
        {
          ret = syshash_update(&sha2_512_ctx, sha512_testcase[i].data,
                               sha512_testcase[i].datalen);
        }

      if (ret)
        {
          printf("sha512 update failed\n");
          goto err;
        }

      ret = syshash_finish(&sha2_512_ctx, output);
      if (ret)
        {
          printf("sha512 finish failed\n");
          goto err;
        }

      ret = match((unsigned char *)sha512_result[i],
                  (unsigned char *)output,
                  SHA512_DIGEST_LENGTH);
      if (ret)
        {
          printf("match sha512 failed\n");
          goto err;
        }
      else
        {
          printf("hash sha512 success\n");
        }
    }

#ifdef CONFIG_TESTING_CRYPTO_HASH_HUGE_BLOCK
  unsigned char *huge_block;
  huge_block = (unsigned char *)malloc(HASH_HUGE_BLOCK_SIZE);
  if (huge_block == NULL)
    {
      printf("huge block test no memory\n");
      goto err;
    }

  memset(huge_block, 'a', HASH_HUGE_BLOCK_SIZE);
  ret = testing_hash_huge_block(&md5_ctx, CRYPTO_MD5,
                                huge_block, HASH_HUGE_BLOCK_SIZE,
                                md5_huge_block_result,
                                MD5_DIGEST_LENGTH);
  if (ret != 0)
    {
      printf("md5 huge block test failed\n");
    }
  else
    {
      printf("md5 huge block test success\n");
    }

  ret = testing_hash_huge_block(&sha1_ctx, CRYPTO_SHA1,
                                huge_block, HASH_HUGE_BLOCK_SIZE,
                                sha1_huge_block_result,
                                SHA1_DIGEST_LENGTH);
  if (ret != 0)
    {
      printf("sha1 huge block test failed\n");
    }
  else
    {
      printf("sha1 huge block test success\n");
    }

  ret = testing_hash_huge_block(&sha2_256_ctx, CRYPTO_SHA2_256,
                                huge_block, HASH_HUGE_BLOCK_SIZE,
                                sha256_huge_block_result,
                                SHA256_DIGEST_LENGTH);
  if (ret != 0)
    {
      printf("sha256 huge block test failed\n");
    }
  else
    {
      printf("sha256 huge block test success\n");
    }

  ret = testing_hash_huge_block(&sha2_512_ctx, CRYPTO_SHA2_512,
                                huge_block, HASH_HUGE_BLOCK_SIZE,
                                sha512_huge_block_result,
                                SHA512_DIGEST_LENGTH);
  if (ret != 0)
    {
      printf("sha512 huge block test failed\n");
    }
  else
    {
      printf("sha512 huge block test success\n");
    }

  free(huge_block);
#endif

err:
  syshash_free(&md5_ctx);
  syshash_free(&sha1_ctx);
  syshash_free(&sha2_256_ctx);
  syshash_free(&sha2_512_ctx);
  return 0;
}