File: TR_hmac_test.sh

package info (click to toggle)
cctools 9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,624 kB
  • sloc: ansic: 192,539; python: 20,827; cpp: 20,199; sh: 11,719; perl: 4,106; xml: 3,688; makefile: 1,224
file content (142 lines) | stat: -rwxr-xr-x 3,416 bytes parent folder | download | duplicates (2)
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
#!/bin/sh

. ../../dttools/test/test_runner_common.sh

exe="hmac_test.test"

prepare()
{
	${CC} -g $CCTOOLS_TEST_CCFLAGS -o "$exe" -I ../src/ -x c - -x none ../src/libdttools.a -lm <<EOF
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hmac.h"
#include "md5.h"

int main(int argc, char **argv)
{
	unsigned char md5_digest[MD5_DIGEST_LENGTH];
	unsigned char sha1_digest[SHA1_DIGEST_LENGTH];
	char md5_ref1[] = "9294727a3638bb1c13f48ef8158bfc9d";
	char md5_ref2[] = "750c783e6ab0b503eaa86e310a5db738";
	char md5_ref3[] = "56be34521d144c88dbb8c733f0e8b3f6";
	char sha1_ref1[] = "b617318655057264e28bc0b6fb378c8ef146be00";
	char sha1_ref2[] = "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79";
	char sha1_ref3[] = "125d7342b9ac11cd91a39af48aa17b4f63f175d3";

	char data[51];
	const char *string;
	int verbose = 0;

	if(argc > 1 && !strcmp(argv[1], "-v"))
		verbose = 1;

	if(verbose)
		printf("MD5 Tests\n===========\n");

	// Test Str 1
	hmac_md5("Hi There", 8, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16, md5_digest);
	string = md5_string(md5_digest);
	if(verbose) {
		printf("MD5 Test 1 ref: \t0x%s\n", md5_ref1);
		printf("MD5 Test 1 digest:\t0x%s\n", string);
	}
	if(strcmp(md5_ref1, string)) {
		if(verbose)
			printf("MD5 Test 1 Failed\n");
		return -1;
	}

	hmac_md5("what do ya want for nothing?", 28, "Jefe", 4, md5_digest);
	string = md5_string(md5_digest);
	if(verbose) {
		printf("\n");
		printf("MD5 Test 2 ref: \t0x%s\n", md5_ref2);
		printf("MD5 Test 2 digest:\t0x%s\n", string);
	}
	if(strcmp(md5_ref2, string)) {
		if(verbose)
			printf("MD5 Test 2 Failed\n");
		return -1;
	}

	memset(data, '\xDD', 50);
	data[50] = 0;
	hmac_md5(data, 50, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 16, md5_digest);
	string = md5_string(md5_digest);
	if(verbose) {
		printf("\n");
		printf("MD5 Test 3 ref: \t0x%s\n", md5_ref3);
		printf("MD5 Test 3 digest:\t0x%s\n", string);
	}
	if(strcmp(md5_ref3, string)) {
		if(verbose)
			printf("MD5 Test 3 Failed\n");
		return -1;
	}

	if(verbose)
		printf("\n\nSHA1 Tests\n===========\n");

	// Test Str 1
	hmac_sha1("Hi There", 8, "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 20, sha1_digest);
	string = sha1_string(sha1_digest);
	if(verbose) {
		printf("SHA1 Test 1 ref: \t0x%s\n", sha1_ref1);
		printf("SHA1 Test 1 digest:\t0x%s\n", string);
	}
	if(strcmp(sha1_ref1, string)) {
		if(verbose)
			printf("SHA1 Test 1 Failed\n");
		return -1;
	}

	hmac_sha1("what do ya want for nothing?", 28, "Jefe", 4, sha1_digest);
	string = sha1_string(sha1_digest);
	if(verbose) {
		printf("\n");
		printf("SHA1 Test 2 ref: \t0x%s\n", sha1_ref2);
		printf("SHA1 Test 2 digest:\t0x%s\n", string);
	}
	if(strcmp(sha1_ref2, string)) {
		if(verbose)
			printf("SHA1 Test 2 Failed\n");
		return -1;
	}

	memset(data, '\xDD', 50);
	data[50] = 0;
	hmac_sha1(data, 50, "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA", 20, sha1_digest);
	string = sha1_string(sha1_digest);
	if(verbose) {
		printf("\n");
		printf("SHA1 Test 3 ref: \t0x%s\n", sha1_ref3);
		printf("SHA1 Test 3 digest:\t0x%s\n", string);
	}
	if(strcmp(sha1_ref3, string)) {
		if(verbose)
			printf("SHA1 Test 3 Failed\n");
		return -1;
	}

	return 0;
}
EOF
	return $?
}

run()
{
	./"$exe" -v
}

clean()
{
	rm -f "$exe"
	return 0
}

dispatch "$@"

# vim: set noexpandtab tabstop=4: