aboutsummaryrefslogtreecommitdiff
path: root/lib/libtpm/sha_test.h
blob: af82fac25b7c1b2ec7691ea4c807886f5e4df646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*****************************************************************************
 * Copyright (c) 2021 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#ifndef SHA_TEST_H
#define SHA_TEST_H

#include <stdio.h>

#include "helpers.h"

/* to avoid compilation issues do not include openssl/sha.h */
unsigned char *SHA1(const unsigned char *, size_t, unsigned char *);
unsigned char *SHA256(const unsigned char *, size_t, unsigned char *);
unsigned char *SHA384(const unsigned char *, size_t, unsigned char *);
unsigned char *SHA512(const unsigned char *, size_t, unsigned char *);

typedef void (*hashfunc)(const uint8_t *data, uint32_t length, uint8_t *hash);
typedef unsigned char *(*osslhashfunc)(const unsigned char *, size_t,
				       unsigned char *);

#define TESTVECTORS(NAME) \
char *NAME[] = {	\
	"",		\
	"abc",		\
	"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", \
	"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" \
};

static inline int
test_hash(hashfunc hf, uint8_t *hash, size_t hashlen,
	   const char *data, uint32_t length,
	   osslhashfunc osslhf)
{
	unsigned char expected[hashlen];
	int ret = 0;

	osslhf((const unsigned char *)data, length, expected);

	hf((uint8_t *)data, length, hash);
	if (!memcmp(hash, expected, hashlen)) {
		printf("PASS: input length: %u\n", length);
	} else {
		printf("FAIL data: %s\n", data);
		ret = 1;
	}

	return ret;
}

#endif /* SHA_TEST_H */