Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/hmac.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <mp.h>
#include <libsec.h>

/* 
	This does hmac encoding of plain data with a key.
	It prints each encoding with it's plain.
	The reason it was written was to generate and verify tokens for given days without the need for a shared key i.e. :
	generate : hmac -t sha1 -k KEY 'Wed Jun 18 hh:mm:ss EDT 2008'
	verify : hmac -t sha1 -k KEY 'Wed Jun 18 hh:mm:ss EDT 2008' 'Thu Jun 19 hh:mm:ss EDT 2008' | grep -s '^1EF503BF7317D955A8957E5FA4E170B0757FB9CD' && echo verified || echo not found

	it also signs stdin

	-q supresses printing the plain
	-n supresses printing the \n as well
BUGS
	aes has a TODO in libsec and prints BEBAFECABEBAFECA0000000000000000

	why does nl = "" work ?
*/

void
usage(void) {
	fprint(2, "hmac [-q] [-t sha1(default) | md5 | aes] -k key plain0 plain1 ... plainn\n");
	exits("usage");
}

#define BUFSIZE 1024

int
main(int argc, char **argv) {
	char *key = nil;
	char *nl = "\n";
	int i = 0;
	int fromstdin;
	int quiet = 0;
	uchar *digest;

	DigestState* (*hmac)( uchar *data, ulong dlen, uchar *key, ulong klen, uchar *digest, DigestState *state) = hmac_sha1;
	int dlen = SHA1dlen;

	char *t;

	ARGBEGIN {
		case 'k' :
			key = ARGF();
			break;
		case 'q' :
			quiet = 1;
			break;
		case 'n' :
			quiet = 1;
			nl = "";
			break;
		case 't' :
			t = ARGF();
			if(strcmp(t, "sha1") == 0) {
				hmac = hmac_sha1;
				dlen = SHA1dlen;
			} else if (strcmp(t, "md5") == 0) {
				hmac = hmac_md5;
				dlen = MD5dlen;
			} else if (strcmp(t, "aes") == 0) {
				hmac = hmac_aes;
				dlen = AESdlen;
			} else
				usage();
			break;
	}ARGEND;

	if(!key)
		usage();

	fmtinstall('H', encodefmt);
	digest  = (uchar*)malloc(dlen);
	for(i = 0; i < argc; i++) {
		hmac((uchar*)argv[i], strlen(argv[i]), (uchar*)key, strlen(key), digest, nil);
		if(quiet)
			print("%.*H%s", dlen, digest, nl);
		else
			print("%.*H\t%s\n", dlen, digest, argv[i]);
	}

	uchar buf[BUFSIZE];
	DigestState *s = nil;
	while((i = read(0, buf, BUFSIZE)) > 0) {
		fromstdin = 1;
		s = hmac(buf, i, (uchar*)key, strlen(key), nil, s);
	}
	hmac(nil, 0, (uchar*)key, strlen(key), digest, s);
	
	if(fromstdin) {
		if(quiet)
			print("%.*H%s", dlen, digest, nl);
		else
			print("%.*H\t<stdin>\n", dlen, digest);
	}
	free(digest);
	exits(nil);
}

# 8c hmac.c && 8l hmac.8 && 8.out -k key2 < /tmp/512

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.