aboutsummaryrefslogtreecommitdiff
path: root/lib/hmac_sha1.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2012-09-19 11:28:28 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2012-09-19 18:30:01 +0300
commit1153970626892573e5966e135e8d81185f4ea53c (patch)
tree773fdf5a8d00ec2ef752c3a8fd5f32c79829cf23 /lib/hmac_sha1.c
downloadeclat-1153970626892573e5966e135e8d81185f4ea53c.tar.gz
eclat-1153970626892573e5966e135e8d81185f4ea53c.tar.bz2
Initial commit
Diffstat (limited to 'lib/hmac_sha1.c')
-rw-r--r--lib/hmac_sha1.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/hmac_sha1.c b/lib/hmac_sha1.c
new file mode 100644
index 0000000..2b4c0c0
--- /dev/null
+++ b/lib/hmac_sha1.c
@@ -0,0 +1,76 @@
+/* This file is part of Eclat
+ Copyright (C) 2012 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 "libeclat.h"
+#include "sha1.h"
+#include <string.h>
+
+#define IPAD 0x36
+#define OPAD 0x5c
+
+void
+hmac_sha1(const void *text, size_t textlen,
+ const void *key, size_t keylen,
+ void *digest)
+{
+ size_t i;
+ struct sha1_ctx ctx;
+ char innerhash[20];
+ char keybuf[20];
+ unsigned char ipad[64]; /* inner padding - key ^ ipad[64] */
+ unsigned char opad[64]; /* outer padding - key ^ opad[64] */
+ unsigned char *kp = (unsigned char *)key;
+
+ /* if key is longer than 64 bytes reset it to key=SHA1(key) */
+ if (keylen > 64) {
+ struct sha1_ctx keyhash;
+
+ sha1_init_ctx(&keyhash);
+ sha1_process_bytes(key, keylen, &keyhash);
+ sha1_finish_ctx(&keyhash, keybuf);
+ key = keybuf;
+ keylen = sizeof(keybuf);
+ }
+
+ /* Compute SHA1(K XOR opad, SHA1(K XOR ipad, text)),
+ where
+ K is an key, padded to 64 bytes with zeros,
+ ipad and opad are the respective defines above repeated 64 times
+ text is the text argument (textlen bytes long)
+ */
+ memset(ipad, IPAD, sizeof(ipad));
+ memset(opad, OPAD, sizeof(opad));
+
+ for (i = 0; i < keylen; i++, kp++) {
+ ipad[i] ^= *kp;
+ opad[i] ^= *kp;
+ }
+
+ /* Compute inner hash */
+ sha1_init_ctx(&ctx);
+ sha1_process_block(ipad, sizeof(ipad), &ctx);
+ sha1_process_bytes(text, textlen, &ctx);
+ sha1_finish_ctx(&ctx, innerhash);
+
+ /* Compute outer hash */
+ sha1_init_ctx(&ctx);
+ sha1_process_block(opad, sizeof(opad), &ctx);
+ sha1_process_bytes(innerhash, sizeof(innerhash), &ctx);
+ sha1_finish_ctx(&ctx, digest);
+}
+
+
+

Return to:

Send suggestions and report system problems to the System administrator.