2N/A/* hash-common.c - Common code for hash algorithms
2N/A * Copyright (C) 2008 Free Software Foundation, Inc.
2N/A *
2N/A * This file is part of Libgcrypt.
2N/A *
2N/A * Libgcrypt is free software; you can redistribute it and/or modify
2N/A * it under the terms of the GNU Lesser General Public License as
2N/A * published by the Free Software Foundation; either version 2.1 of
2N/A * the License, or (at your option) any later version.
2N/A *
2N/A * Libgcrypt is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU Lesser General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU Lesser General Public
2N/A * License along with this program; if not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <config.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#ifdef HAVE_STDINT_H
2N/A# include <stdint.h>
2N/A#endif
2N/A
2N/A#include "g10lib.h"
2N/A#include "hash-common.h"
2N/A
2N/A
2N/A/* Run a selftest for hash algorithm ALGO. If the resulting digest
2N/A matches EXPECT/EXPECTLEN and everything else is fine as well,
2N/A return NULL. If an error occurs, return a static text string
2N/A describing the error.
2N/A
2N/A DATAMODE controls what will be hashed according to this table:
2N/A
2N/A 0 - Hash the supplied DATA of DATALEN.
2N/A 1 - Hash one million times a 'a'. DATA and DATALEN are ignored.
2N/A
2N/A*/
2N/Aconst char *
2N/A_gcry_hash_selftest_check_one (int algo,
2N/A int datamode, const void *data, size_t datalen,
2N/A const void *expect, size_t expectlen)
2N/A{
2N/A const char *result = NULL;
2N/A gcry_error_t err = 0;
2N/A gcry_md_hd_t hd;
2N/A unsigned char *digest;
2N/A
2N/A if (_gcry_md_get_algo_dlen (algo) != expectlen)
2N/A return "digest size does not match expected size";
2N/A
2N/A err = _gcry_md_open (&hd, algo, 0);
2N/A if (err)
2N/A return "gcry_md_open failed";
2N/A
2N/A switch (datamode)
2N/A {
2N/A case 0:
2N/A _gcry_md_write (hd, data, datalen);
2N/A break;
2N/A
2N/A case 1: /* Hash one million times an "a". */
2N/A {
2N/A char aaa[1000];
2N/A int i;
2N/A
2N/A /* Write in odd size chunks so that we test the buffering. */
2N/A memset (aaa, 'a', 1000);
2N/A for (i = 0; i < 1000; i++)
2N/A _gcry_md_write (hd, aaa, 1000);
2N/A }
2N/A break;
2N/A
2N/A default:
2N/A result = "invalid DATAMODE";
2N/A }
2N/A
2N/A if (!result)
2N/A {
2N/A digest = _gcry_md_read (hd, algo);
2N/A
2N/A if ( memcmp (digest, expect, expectlen) )
2N/A result = "digest mismatch";
2N/A }
2N/A
2N/A _gcry_md_close (hd);
2N/A
2N/A return result;
2N/A}
2N/A