summaryrefslogtreecommitdiffabout
path: root/tests/thmac.c
blob: 5c957dc4f29f1d843ec8c082d74396c1e40cdd4c (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
/* This file is part of Eclat.
   Copyright (C) 2012, 2013 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "libeclat.h"
#include "sha256.h"

char *progname;

static int
xdecode(const char *in, void **pout, size_t *outlen)
{
	unsigned char *out;
	size_t len;
	
	if (in[0] == '0' && in[1] == 'x') {
		size_t i, slen;
		unsigned char *p;
		
		slen = strlen(in);
		if (slen % 2)
			return -1;
		len = slen / 2 - 1; 
		out = malloc(len);
		if (!out) {
			perror("malloc");
			abort();
		}
		for (i = 2, p = out; i < slen; i+=2, p++) {
			static char xchar[] = "0123456789abcdef";
			char *q;
			unsigned char c;

			q = strchr(xchar, tolower(in[i]));
			if (q == 0)
				return i;
			c = (q - xchar);

			q = strchr(xchar, tolower(in[i+1]));
			if (q == 0)
				return i+1;
			*p = (c << 4) + (q - xchar);
		}
	} else {
		len = strlen(in);
		out = malloc(len);
		if (!out) {
			perror("malloc");
			abort();
		}
		memcpy(out, in, len);
	}
	*pout = out;
	*outlen = len;
	return 0;
}

void
xdecode_assert(int rc, char *in, char *what)
{
	switch (rc) {
	case -1:
		fprintf(stderr, "%s: odd %s length\n", progname, what);
		exit(1);

	case 0:
		break;

	default:
		fprintf(stderr, "%s: invalid hex character in %s near %s\n",
			progname, what, in + rc);
		exit(1);
	}
}

void
xdump(unsigned char *in, size_t len)
{
	printf("0x");
	while (len--)
		printf("%02x", *in++);
	printf("\n");
}

int
main(int argc, char **argv)
{
	int rc;
	void *key;
	size_t keylen;
	void *text;
	size_t textlen;
	unsigned char digest[SHA256_DIGEST_SIZE];
	
	progname = argv[0];
	if (argc != 3) {
		fprintf(stderr, "usage: %s key data\n", progname);
		return 1;
	}

	rc = xdecode(argv[1], &key, &keylen);
	xdecode_assert(rc, argv[1], "key");
	
	rc = xdecode(argv[2], &text, &textlen);
	xdecode_assert(rc, argv[2], "text");

	hmac_sha256(text, textlen, key, keylen, digest);

	xdump(digest, sizeof(digest));
	return 0;
}

		

Return to:

Send suggestions and report system problems to the System administrator.