summaryrefslogtreecommitdiffstats
path: root/tests/ncr.c
blob: 362cdaaef939be05f21e9765e3b2f82ae7636bf0 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Demo on how to use /dev/crypto device for HMAC.
 *
 * Placed under public domain.
 *
 */
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <stdlib.h>

#include "../alg.h"

struct hash_vectors_st {
	const char *algorithm;
	const uint8_t *key;	/* if hmac */
	int key_size;
	const uint8_t *plaintext;
	int plaintext_size;
	const uint8_t *output;
	int output_size;
} hash_vectors[] = {
	{
	.algorithm = "sha1",.key = NULL,.plaintext =
		    (uint8_t *) "what do ya want for nothing?",.
		    plaintext_size =
		    sizeof("what do ya want for nothing?") - 1,.output =
		    (uint8_t *)
		    "\x8f\x82\x03\x94\xf9\x53\x35\x18\x20\x45\xda\x24\xf3\x4d\xe5\x2b\xf8\xbc\x34\x32",.
		    output_size = 20}
	,
#if 0
{
	.algorithm = "hmac(md5)",.key = (uint8_t *) "Jefe",.key_size =
		    4,.plaintext =
		    (uint8_t *) "what do ya want for nothing?",.
		    plaintext_size =
		    sizeof("what do ya want for nothing?") - 1,.output =
		    (uint8_t *)
		    "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38",.
		    output_size = 16}
	,
	    /* from rfc4231 */
	{
	.algorithm = "hmac(sha224)",.key =
		    (uint8_t *)
		    "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",.
		    key_size = 20,.plaintext =
		    (uint8_t *) "Hi There",.plaintext_size =
		    sizeof("Hi There") - 1,.output =
		    (uint8_t *)
		    "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22",.
		    output_size = 28}
	, {
	.algorithm = "hmac(sha256)",.key =
		    (uint8_t *)
		    "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",.
		    key_size = 20,.plaintext =
		    (uint8_t *) "Hi There",.plaintext_size =
		    sizeof("Hi There") - 1,.output =
		    (uint8_t *)
		    "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7",.
		    output_size = 32}
	, {
	.algorithm = "hmac(sha384)",.key =
		    (uint8_t *)
		    "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",.
		    key_size = 20,.plaintext =
		    (uint8_t *) "Hi There",.plaintext_size =
		    sizeof("Hi There") - 1,.output =
		    (uint8_t *)
		    "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2\xfa\x9c\xb6",.
		    output_size = 48}
	, {
	.algorithm = "hmac(sha512)",.key =
		    (uint8_t *)
		    "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",.
		    key_size = 20,.plaintext =
		    (uint8_t *) "Hi There",.plaintext_size =
		    sizeof("Hi There") - 1,.output =
		    (uint8_t *)
		    "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54",.
		    output_size = 64}
#endif
};

#define HASH_DATA_SIZE 64

/* SHA1 and other hashes */
static int test_ncr_hash(int with_accept)
{
	uint8_t data[HASH_DATA_SIZE];
	int i, j;
	ssize_t data_size;

	/* convert it to key */
	if (with_accept)
		fprintf(stdout, "Tests on Hashes with accept()\n");
	else
		fprintf(stdout, "Tests on Hashes without accept()\n");
	for (i = 0; i < sizeof(hash_vectors) / sizeof(hash_vectors[0]); i++) {
		struct sockaddr_alg salg;
		int fd, hfd;

		fprintf(stdout, "\t%s:\n", hash_vectors[i].algorithm);

		fd = socket(PF_ALG, SOCK_STREAM, 0);
		if (fd < 0) {
			perror("socket()");
			return 1;
		}

		salg.salg_family = AF_ALG;
		strcpy(salg.salg_type, "hash");
		strcpy(salg.salg_tfm, hash_vectors[i].algorithm);
		if (bind(fd, (struct sockaddr *)&salg, sizeof(salg)) != 0) {
			perror("bind()");
			return 1;
		}

		if (with_accept) {
			if (listen(fd, 1) != 0) {
				perror("listen()");
				return 1;
			}

			hfd = accept(fd, NULL, NULL);
			if (hfd < 0) {
				perror("accept()");
				return 1;
			}

			close(fd);
		} else
			hfd = fd;

		errno = 0;
		if (write(hfd, hash_vectors[i].plaintext,
			  hash_vectors[i].plaintext_size)
		    != hash_vectors[i].plaintext_size) {
			perror("write()");
			return 1;
		}

		errno = 0;
		data_size = read(hfd, data, sizeof(data));
		if (data_size < 0) {
			perror("read()");
			return 1;
		}

		close(hfd);

		if (data_size != hash_vectors[i].output_size
		    || memcmp(data, hash_vectors[i].output,
			   hash_vectors[i].output_size) != 0) {
			fprintf(stderr, "HASH test vector %d failed!\n", i);

			fprintf(stderr, "Output[%d]: ", (int)data_size);
			for (j = 0; j < data_size; j++)
				fprintf(stderr, "%.2x:", (int)data[j]);
			fprintf(stderr, "\n");

			fprintf(stderr, "Expected[%d]: ",
				hash_vectors[i].output_size);
			for (j = 0; j < hash_vectors[i].output_size; j++)
				fprintf(stderr, "%.2x:",
					(int)hash_vectors[i].output[j]);
			fprintf(stderr, "\n");
			return 1;
		}
	}

	fprintf(stdout, "\n");

	return 0;

}

int main()
{
	if (test_ncr_hash(0))
		return 1;
	if (test_ncr_hash(1))
		return 1;

	return 0;
}