<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf8">
<title>/usr/web/sources/contrib/maht/hmac.c - Plan 9 from Bell Labs</title>
<!-- THIS FILE IS AUTOMATICALLY GENERATED. -->
<!-- EDIT sources.tr INSTEAD. -->
</meta>
</head>
<body>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"><a href="/plan9/">Plan 9 from Bell Labs</a>’s /usr/web/sources/contrib/maht/hmac.c</span></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
Copyright © 2009 Alcatel-Lucent.<br />
Distributed under the
<a href="/plan9/license.html">Lucent Public License version 1.02</a>.
<br />
<a href="/plan9/download.html">Download the Plan 9 distribution.</a>
</font>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<table width="100%" cellspacing=0 border=0><tr><td align="center">
<table cellspacing=0 cellpadding=5 bgcolor="#eeeeff"><tr><td align="left">
<pre>
<!-- END HEADER -->
#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
<!-- BEGIN TAIL -->
</pre>
</td></tr></table>
</td></tr></table>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"></span></p>
<p style="margin-top: 0; margin-bottom: 0.50in"></p>
<p style="margin-top: 0; margin-bottom: 0.33in"></p>
<center><table border="0"><tr>
<td valign="middle"><a href="
http://www.alcatel-lucent.com/"><img border="0" src="/plan9/img/logo_ft.gif" alt="Bell Labs" />
</a></td>
<td valign="middle"><a href="
http://www.opensource.org"><img border="0" alt="OSI certified" src="/plan9/img/osi-certified-60x50.gif" />
</a></td>
<td><img style="padding-right: 45px;" alt="Powered by Plan 9" src="/plan9/img/power36.gif" />
</td>
</tr></table></center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center>
<span style="font-size: 10pt">(<a href="/plan9/">Return to Plan 9 Home Page</a>)</span>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
<span style="font-size: 10pt"><a href="
http://www.lucent.com/copyright.html">Copyright</a></span>
<span style="font-size: 10pt">© 2009 Alcatel-Lucent.</span>
<span style="font-size: 10pt">All Rights Reserved.</span>
<br />
<span style="font-size: 10pt">Comments to</span>
<span style="font-size: 10pt"><a href="mailto:
[email protected]">
[email protected]</a>.</span>
</font></center>
</body>
</html>