aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-10 19:18:57 +0100
committerMattias Andrée <maandree@kth.se>2019-02-10 19:18:57 +0100
commit3bf8125fce2a7fa71214a03384f2a6dac459ccfc (patch)
tree5a13fb1d9fcc46876c19ce1e82107c152570d7a7
parentFix warnings (diff)
downloadlibsha2-3bf8125fce2a7fa71214a03384f2a6dac459ccfc.tar.gz
libsha2-3bf8125fce2a7fa71214a03384f2a6dac459ccfc.tar.bz2
libsha2-3bf8125fce2a7fa71214a03384f2a6dac459ccfc.tar.xz
Fix HMAC and add tests1.0.1
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--algorithm_output_size.c1
-rw-r--r--hmac_init.c2
-rw-r--r--test.c79
3 files changed, 78 insertions, 4 deletions
diff --git a/algorithm_output_size.c b/algorithm_output_size.c
index 4b287b2..f7a0352 100644
--- a/algorithm_output_size.c
+++ b/algorithm_output_size.c
@@ -1,6 +1,5 @@
/* See LICENSE file for copyright and license details. */
#include "common.h"
-#include <stdio.h>
/**
diff --git a/hmac_init.c b/hmac_init.c
index b60ebef..6983073 100644
--- a/hmac_init.c
+++ b/hmac_init.c
@@ -26,7 +26,7 @@ libsha2_hmac_init(struct libsha2_hmac_state *restrict state, enum libsha2_algori
}
state->inited = 0;
- if (keylen <= state->sha2_state.chunk_size * 8) {
+ if (keylen <= (algorithm <= LIBSHA2_256 ? 64 * 8 : 128 * 8)) {
memset(state->ipad, 0x36, sizeof(state->ipad));
memset(state->opad, 0x5C, sizeof(state->opad));
for (i = 0; i < keylen / 8; i++) {
diff --git a/test.c b/test.c
index 9eb34a6..b0e5c88 100644
--- a/test.c
+++ b/test.c
@@ -71,6 +71,16 @@
test_str(str, EXPECTED);\
} while (0)
+#define test_hmac(ALGO, TEXT, KEY, MAC)\
+ do {\
+ libsha2_unhex(buf, KEY);\
+ test(!libsha2_hmac_init(&hs, ALGO, buf, (sizeof(KEY) - 1) << 2));\
+ libsha2_unhex(buf, TEXT);\
+ libsha2_hmac_digest(&hs, buf, (sizeof(TEXT) - 1) << 2, buf);\
+ libsha2_behex_lower(str, buf, libsha2_hmac_state_output_size(&hs));\
+ test_str(str, MAC);\
+ } while (0)
+
int
main(int argc, char *argv[])
@@ -282,7 +292,7 @@ main(int argc, char *argv[])
test((len = libsha2_marshal(&s, NULL)) && len <= sizeof(str));
test(libsha2_marshal(&s, str) == len);
memset(&s, 0, sizeof(s));
- test(libsha2_unmarshal(&s, str, sizeof(s)) == len);
+ test(libsha2_unmarshal(&s, str, sizeof(str)) == len);
}
libsha2_digest(&s, buf, (1000 - n) * 8, buf);
libsha2_behex_lower(str, buf, libsha2_state_output_size(&s));
@@ -341,8 +351,13 @@ main(int argc, char *argv[])
for (i = 1; i < n; i++) {
test(!libsha2_hmac_init(&hs, LIBSHA2_256, "key", 3 << 3));
test(libsha2_hmac_state_output_size(&hs) == 32);
- for (j = 0; j + i < n; j += i)
+ for (j = 0; j + i < n; j += i) {
libsha2_hmac_update(&hs, &"The quick brown fox jumps over the lazy dog"[j], i << 3);
+ test((len = libsha2_hmac_marshal(&hs, NULL)) && len <= sizeof(str));
+ test(libsha2_hmac_marshal(&hs, str) == len);
+ memset(&hs, 0, sizeof(hs));
+ test(libsha2_hmac_unmarshal(&hs, str, sizeof(str)) == len);
+ }
libsha2_hmac_digest(&hs, &"The quick brown fox jumps over the lazy dog"[j], (n - j) << 3, buf);
libsha2_behex_lower(str, buf, libsha2_hmac_state_output_size(&hs));
test_str(str, "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8");
@@ -350,5 +365,65 @@ main(int argc, char *argv[])
test(!errno);
+ test_hmac(LIBSHA2_224,
+ "53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
+ "c7405e3ae058e8cd30b08b4140248581ed174cb34e1224bcc1efc81b");
+
+ test_hmac(LIBSHA2_224,
+ "53616d706c65206d65737361676520666f72206b65796c656e3c626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b",
+ "e3d249a8cfb67ef8b7a169e9a0a599714a2cecba65999a51beb8fbbe");
+
+ test_hmac(LIBSHA2_224,
+ "53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f60616263",
+ "91c52509e5af8531601ae6230099d90bef88aaefb961f4080abc014d");
+
+ test_hmac(LIBSHA2_256,
+ "53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
+ "8bb9a1db9806f20df7f77b82138c7914d174d59e13dc4d0169c9057b133e1d62");
+
+ test_hmac(LIBSHA2_256,
+ "53616d706c65206d65737361676520666f72206b65796c656e3c626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
+ "a28cf43130ee696a98f14a37678b56bcfcbdd9e5cf69717fecf5480f0ebdf790");
+
+ test_hmac(LIBSHA2_256,
+ "53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f60616263",
+ "bdccb6c72ddeadb500ae768386cb38cc41c63dbb0878ddb9c7a38a431b78378d");
+
+ test_hmac(LIBSHA2_384,
+ "53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f",
+ "63c5daa5e651847ca897c95814ab830bededc7d25e83eef9195cd45857a37f448947858f5af50cc2b1b730ddf29671a9");
+
+ test_hmac(LIBSHA2_384,
+ "53616d706c65206d65737361676520666f72206b65796c656e3c626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f",
+ "6eb242bdbb582ca17bebfa481b1e23211464d2b7f8c20b9ff2201637b93646af5ae9ac316e98db45d9cae773675eeed0");
+
+ test_hmac(LIBSHA2_384,
+ "53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7",
+ "5b664436df69b0ca22551231a3f0a3d5b4f97991713cfa84bff4d0792eff96c27dccbbb6f79b65d548b40e8564cef594");
+
+ test_hmac(LIBSHA2_512,
+ "53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f",
+ "fc25e240658ca785b7a811a8d3f7b4ca48cfa26a8a366bf2cd1f836b05fcb024bd36853081811d6cea4216ebad79da1cfcb95ea4586b8a0ce356596a55fb1347");
+
+ test_hmac(LIBSHA2_512,
+ "53616d706c65206d65737361676520666f72206b65796c656e3c626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
+ "fd44c18bda0bb0a6ce0e82b031bf2818f6539bd56ec00bdc10a8a2d730b3634de2545d639b0f2cf710d0692c72a1896f1f211c2b922d1a96c392e07e7ea9fedc");
+
+ test_hmac(LIBSHA2_512,
+ "53616d706c65206d65737361676520666f72206b65796c656e3d626c6f636b6c656e",
+ "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7",
+ "d93ec8d2de1ad2a9957cb9b83f14e76ad6b5e0cce285079a127d3b14bccb7aa7286d4ac0d4ce64215f2bc9e6870b33d97438be4aaa20cda5c5a912b48b8e27f3");
+
return 0;
}