aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2019-02-10 20:57:56 +0100
committerMattias Andrée <maandree@kth.se>2019-02-10 20:57:56 +0100
commit15f132722cae63775dd7b2fef866d477c54c7b8e (patch)
tree27359241e0a1a0b6f304060bc0a9e7c9287efd06
parentFirst commit (diff)
downloadlibsha1-15f132722cae63775dd7b2fef866d477c54c7b8e.tar.gz
libsha1-15f132722cae63775dd7b2fef866d477c54c7b8e.tar.bz2
libsha1-15f132722cae63775dd7b2fef866d477c54c7b8e.tar.xz
Implement SHA-0 and remove .chunk_size1.0
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--digest.c24
-rw-r--r--hmac_digest.c4
-rw-r--r--hmac_marshal.c8
-rw-r--r--hmac_unmarshal.c10
-rw-r--r--hmac_update.c2
-rw-r--r--init.c18
-rw-r--r--libsha1.h9
-rw-r--r--marshal.c7
-rw-r--r--process.c9
-rw-r--r--test.c43
-rw-r--r--unmarshal.c13
-rw-r--r--update.c12
12 files changed, 53 insertions, 106 deletions
diff --git a/digest.c b/digest.c
index fd40e8f..06ceb10 100644
--- a/digest.c
+++ b/digest.c
@@ -23,7 +23,7 @@ libsha1_digest(struct libsha1_state *restrict state, const void *message_, size_
msglen &= (size_t)7;
}
- off = (state->message_size / 8) % state->chunk_size;
+ off = (state->message_size / 8) % sizeof(state->chunk);
if (msglen) {
state->chunk[off] = (unsigned char)(*message << (8 - (int)msglen));
state->chunk[off] |= (unsigned char)(1 << (7 - msglen));
@@ -34,21 +34,21 @@ libsha1_digest(struct libsha1_state *restrict state, const void *message_, size_
}
off += 1;
- if (off > state->chunk_size - (size_t)8) {
- memset(state->chunk + off, 0, state->chunk_size - off);
+ if (off > sizeof(state->chunk) - (size_t)8) {
+ memset(state->chunk + off, 0, sizeof(state->chunk) - off);
off = 0;
libsha1_process(state, state->chunk);
}
- memset(state->chunk + off, 0, state->chunk_size - 8 - off);
- state->chunk[state->chunk_size - 8] = (unsigned char)(state->message_size >> 56);
- state->chunk[state->chunk_size - 7] = (unsigned char)(state->message_size >> 48);
- state->chunk[state->chunk_size - 6] = (unsigned char)(state->message_size >> 40);
- state->chunk[state->chunk_size - 5] = (unsigned char)(state->message_size >> 32);
- state->chunk[state->chunk_size - 4] = (unsigned char)(state->message_size >> 24);
- state->chunk[state->chunk_size - 3] = (unsigned char)(state->message_size >> 16);
- state->chunk[state->chunk_size - 2] = (unsigned char)(state->message_size >> 8);
- state->chunk[state->chunk_size - 1] = (unsigned char)(state->message_size >> 0);
+ memset(state->chunk + off, 0, sizeof(state->chunk) - 8 - off);
+ state->chunk[sizeof(state->chunk) - 8] = (unsigned char)(state->message_size >> 56);
+ state->chunk[sizeof(state->chunk) - 7] = (unsigned char)(state->message_size >> 48);
+ state->chunk[sizeof(state->chunk) - 6] = (unsigned char)(state->message_size >> 40);
+ state->chunk[sizeof(state->chunk) - 5] = (unsigned char)(state->message_size >> 32);
+ state->chunk[sizeof(state->chunk) - 4] = (unsigned char)(state->message_size >> 24);
+ state->chunk[sizeof(state->chunk) - 3] = (unsigned char)(state->message_size >> 16);
+ state->chunk[sizeof(state->chunk) - 2] = (unsigned char)(state->message_size >> 8);
+ state->chunk[sizeof(state->chunk) - 1] = (unsigned char)(state->message_size >> 0);
libsha1_process(state, state->chunk);
n = libsha1_algorithm_output_size(state->algorithm);
diff --git a/hmac_digest.c b/hmac_digest.c
index 4acb1a1..4647101 100644
--- a/hmac_digest.c
+++ b/hmac_digest.c
@@ -21,13 +21,13 @@ libsha1_hmac_digest(struct libsha1_hmac_state *restrict state, const void *data,
{
if (!state->inited) {
libsha1_init(&state->sha1_state, state->sha1_state.algorithm);
- libsha1_update(&state->sha1_state, state->ipad, state->sha1_state.chunk_size * 8);
+ libsha1_update(&state->sha1_state, state->ipad, sizeof(state->sha1_state.chunk) * 8);
}
libsha1_digest(&state->sha1_state, data, n, output);
libsha1_init(&state->sha1_state, state->sha1_state.algorithm);
- libsha1_update(&state->sha1_state, state->opad, state->sha1_state.chunk_size * 8);
+ libsha1_update(&state->sha1_state, state->opad, sizeof(state->sha1_state.chunk) * 8);
libsha1_digest(&state->sha1_state, output, state->outsize, output);
state->inited = 0;
}
diff --git a/hmac_marshal.c b/hmac_marshal.c
index 70d030b..95f4277 100644
--- a/hmac_marshal.c
+++ b/hmac_marshal.c
@@ -30,12 +30,12 @@ libsha1_hmac_marshal(const struct libsha1_hmac_state *restrict state, void *rest
off += sizeof(unsigned char);
if (buf)
- memcpy(&buf[off], state->ipad, state->sha1_state.chunk_size);
- off += state->sha1_state.chunk_size;
+ memcpy(&buf[off], state->ipad, sizeof(state->ipad));
+ off += sizeof(state->ipad);
if (buf)
- memcpy(&buf[off], state->opad, state->sha1_state.chunk_size);
- off += state->sha1_state.chunk_size;
+ memcpy(&buf[off], state->opad, sizeof(state->opad));
+ off += sizeof(state->opad);
return off;
}
diff --git a/hmac_unmarshal.c b/hmac_unmarshal.c
index 8db73c8..bd8f3fe 100644
--- a/hmac_unmarshal.c
+++ b/hmac_unmarshal.c
@@ -33,7 +33,7 @@ libsha1_hmac_unmarshal(struct libsha1_hmac_state *restrict state, const void *re
return 0;
off += r;
- if (bufsize - off < sizeof(size_t) + sizeof(unsigned char) + 2 * state->sha1_state.chunk_size) {
+ if (bufsize - off < sizeof(size_t) + sizeof(unsigned char) + sizeof(state->ipad) + sizeof(state->opad)) {
errno = EINVAL;
return 0;
}
@@ -44,11 +44,11 @@ libsha1_hmac_unmarshal(struct libsha1_hmac_state *restrict state, const void *re
state->inited = *(const unsigned char *)&buf[off];
off += sizeof(unsigned char);
- memcpy(state->ipad, &buf[off], state->sha1_state.chunk_size);
- off += state->sha1_state.chunk_size;
+ memcpy(state->ipad, &buf[off], sizeof(state->ipad));
+ off += sizeof(state->ipad);
- memcpy(state->opad, &buf[off], state->sha1_state.chunk_size);
- off += state->sha1_state.chunk_size;
+ memcpy(state->opad, &buf[off], sizeof(state->opad));
+ off += sizeof(state->opad);
return off;
}
diff --git a/hmac_update.c b/hmac_update.c
index 40528a4..160a293 100644
--- a/hmac_update.c
+++ b/hmac_update.c
@@ -15,7 +15,7 @@ libsha1_hmac_update(struct libsha1_hmac_state *restrict state, const void *restr
{
if (!state->inited) {
libsha1_init(&state->sha1_state, state->sha1_state.algorithm);
- libsha1_update(&state->sha1_state, state->ipad, state->sha1_state.chunk_size * 8);
+ libsha1_update(&state->sha1_state, state->ipad, sizeof(state->sha1_state.chunk) * 8);
state->inited = 1;
}
diff --git a/init.c b/init.c
index 4e31556..556f6a8 100644
--- a/init.c
+++ b/init.c
@@ -3,16 +3,9 @@
/**
- * Initial state for SHA-0
+ * Initial state for SHA-1 and SHA-0
*/
-static const uint32_t H_0[] = {
- 0, 0, 0, 0, 0
-};
-
-/**
- * Initial state for SHA_1
- */
-static const uint32_t H_1[] = {
+static const uint32_t H[] = {
0x67452301UL, 0xEFCDAB89UL, 0x98BADCFEUL, 0x10325476UL, 0xC3D2E1F0UL
};
@@ -33,14 +26,13 @@ libsha1_init(struct libsha1_state *restrict state, enum libsha1_algorithm algori
/* Set initial hash values. */
switch (algorithm) {
- case LIBSHA1_0: memcpy(state->h, H_0, sizeof(H_0)); break;
- case LIBSHA1_1: memcpy(state->h, H_1, sizeof(H_1)); break;
+ case LIBSHA1_0:
+ case LIBSHA1_1:
+ memcpy(state->h, H, sizeof(H)); break;
default:
errno = EINVAL;
return -1;
}
- state->chunk_size = 64;
-
return 0;
}
diff --git a/libsha1.h b/libsha1.h
index a263ea3..6c83319 100644
--- a/libsha1.h
+++ b/libsha1.h
@@ -54,11 +54,6 @@ struct libsha1_state {
unsigned char chunk[64];
/**
- * The size of the chunks, in bytes
- */
- size_t chunk_size;
-
- /**
* The algorithm that is used
*/
enum libsha1_algorithm algorithm;
@@ -98,12 +93,12 @@ struct libsha1_hmac_state {
/**
* Inner pad XOR processed key
*/
- unsigned char ipad[128];
+ unsigned char ipad[64];
/**
* Outer pad XOR processed key
*/
- unsigned char opad[128];
+ unsigned char opad[64];
};
diff --git a/marshal.c b/marshal.c
index c2d1c11..adca8dc 100644
--- a/marshal.c
+++ b/marshal.c
@@ -33,11 +33,8 @@ libsha1_marshal(const struct libsha1_state *restrict state, void *restrict buf_)
off += sizeof(state->h);
if (buf)
- *(size_t *)&buf[off] = state->chunk_size;
- off += sizeof(size_t);
- if (buf)
- memcpy(&buf[off], state->chunk, (state->message_size / 8) % state->chunk_size);
- off += (state->message_size / 8) % state->chunk_size;
+ memcpy(&buf[off], state->chunk, (state->message_size / 8) % sizeof(state->chunk));
+ off += (state->message_size / 8) % sizeof(state->chunk);
return off;
}
diff --git a/process.c b/process.c
index ce5a043..d328bac 100644
--- a/process.c
+++ b/process.c
@@ -36,8 +36,13 @@ libsha1_process(struct libsha1_state *restrict state, const unsigned char *restr
state->w[i] |= (uint32_t)chunk[4 * i + 2] << 8;
state->w[i] |= (uint32_t)chunk[4 * i + 3];
}
- for (; i < 80; i++)
- state->w[i] = rorl(state->w[i - 3] ^ state->w[i - 8] ^ state->w[i - 14] ^ state->w[i - 16], 1);
+ if (state->algorithm == LIBSHA1_1) {
+ for (; i < 80; i++)
+ state->w[i] = rorl(state->w[i - 3] ^ state->w[i - 8] ^ state->w[i - 14] ^ state->w[i - 16], 1);
+ } else {
+ for (; i < 80; i++)
+ state->w[i] = state->w[i - 3] ^ state->w[i - 8] ^ state->w[i - 14] ^ state->w[i - 16];
+ }
a = state->h[0];
b = state->h[1];
c = state->h[2];
diff --git a/test.c b/test.c
index 38b0d85..9cf5116 100644
--- a/test.c
+++ b/test.c
@@ -140,35 +140,13 @@ main(int argc, char *argv[])
test(libsha1_init(&s, ~0) == -1 && errno == EINVAL);
errno = 0;
-#ifdef TODO
- test(!libsha1_init(&s, LIBSHA1_0));
- test(libsha1_state_output_size(&s) == 20);
- libsha1_digest(&s, "", 0, buf);
- libsha1_behex_lower(str, buf, libsha1_state_output_size(&s));
- test_str(str, "");
-#endif
-
test(!libsha1_init(&s, LIBSHA1_1));
test(libsha1_state_output_size(&s) == 20);
libsha1_digest(&s, "", 0, buf);
libsha1_behex_lower(str, buf, libsha1_state_output_size(&s));
test_str(str, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
-#ifdef TODO
- test_repeated(0xFF, 1, LIBSHA1_0, "");
- test_custom("\xE5\xE0\x99\x24", LIBSHA1_0, "");
- test_repeated(0x00, 56, LIBSHA1_0, "");
- test_repeated(0x51, 1000, LIBSHA1_0, "");
- test_repeated(0x41, 1000, LIBSHA1_0, "");
- test_repeated(0x99, 1005, LIBSHA1_0, "");
- test_repeated_huge(0x00, 1000000UL, LIBSHA1_0, "");
- test_repeated_huge(0x41, 0x20000000UL, LIBSHA1_0, "");
- test_repeated_huge(0x00, 0x41000000UL, LIBSHA1_0, "");
- test_repeated_huge(0x84, 0x6000003FUL, LIBSHA1_0, "");
- test_custom("abc", LIBSHA1_0, "");
- test_custom("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", LIBSHA1_1,
- "");
-#endif
+ test_custom("abc", LIBSHA1_0, "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880");
test_repeated(0xFF, 1, LIBSHA1_1, "85e53271e14006f0265921d02d4d736cdc580b0b");
test_custom("\xE5\xE0\x99\x24", LIBSHA1_1, "d1dffbc8a175dd8eebe0da87b1792b6dc1018e82");
@@ -185,20 +163,13 @@ main(int argc, char *argv[])
"84983e441c3bd26ebaae4aa1f95129e5e54670f1");
for (i = 0; i < 1000; i++) {
-#ifdef TODO
- for (j = 0; j < 2; j++) {
-#else
for (j = 1; j < 2; j++) {
-#endif
memset(buf, 0x41, 1000);
test(!libsha1_init(&s, (enum libsha1_algorithm)j));
libsha1_update(&s, buf, i * 8);
libsha1_digest(&s, buf, (1000 - i) * 8, buf);
libsha1_behex_lower(str, buf, libsha1_state_output_size(&s));
- test_str(str, ((const char *[]){
- "",
- "3ae3644d6777a1f56a1defeabc74af9c4b313e49"
- })[j]);
+ test_str(str, "3ae3644d6777a1f56a1defeabc74af9c4b313e49");
memset(buf, 0x41, 1000);
test(!libsha1_init(&s, (enum libsha1_algorithm)j));
@@ -206,10 +177,7 @@ main(int argc, char *argv[])
libsha1_update(&s, buf, (1000 - i) * 8);
libsha1_digest(&s, NULL, 0, buf);
libsha1_behex_lower(str, buf, libsha1_state_output_size(&s));
- test_str(str, ((const char *[]){
- "",
- "3ae3644d6777a1f56a1defeabc74af9c4b313e49"
- })[j]);
+ test_str(str, "3ae3644d6777a1f56a1defeabc74af9c4b313e49");
if (!i)
continue;
@@ -225,10 +193,7 @@ main(int argc, char *argv[])
}
libsha1_digest(&s, buf, (1000 - n) * 8, buf);
libsha1_behex_lower(str, buf, libsha1_state_output_size(&s));
- test_str(str, ((const char *[]){
- "",
- "3ae3644d6777a1f56a1defeabc74af9c4b313e49"
- })[j]);
+ test_str(str, "3ae3644d6777a1f56a1defeabc74af9c4b313e49");
}
}
diff --git a/unmarshal.c b/unmarshal.c
index e89a8dd..12eb652 100644
--- a/unmarshal.c
+++ b/unmarshal.c
@@ -41,19 +41,12 @@ libsha1_unmarshal(struct libsha1_state *restrict state, const void *restrict buf
memcpy(state->h, &buf[off], sizeof(state->h));
off += sizeof(state->h);
- if (bufsize - off < sizeof(size_t)) {
+ if (bufsize - off < (state->message_size / 8) % sizeof(state->chunk)) {
errno = EINVAL;
return 0;
}
- state->chunk_size = *(const size_t *)&buf[off];
- off += sizeof(size_t);
-
- if (bufsize - off < (state->message_size / 8) % state->chunk_size) {
- errno = EINVAL;
- return 0;
- }
- memcpy(state->chunk, &buf[off], (state->message_size / 8) % state->chunk_size);
- off += (state->message_size / 8) % state->chunk_size;
+ memcpy(state->chunk, &buf[off], (state->message_size / 8) % sizeof(state->chunk));
+ off += (state->message_size / 8) % sizeof(state->chunk);
return off;
}
diff --git a/update.c b/update.c
index 7c9ed9b..1725eb8 100644
--- a/update.c
+++ b/update.c
@@ -15,23 +15,23 @@ libsha1_update(struct libsha1_state *restrict state, const void *restrict messag
const char *restrict message = message_;
size_t n, off;
- off = (state->message_size / 8) % state->chunk_size;
+ off = (state->message_size / 8) % sizeof(state->chunk);
state->message_size += msglen;
msglen /= 8;
if (off) {
- n = msglen < state->chunk_size - off ? msglen : state->chunk_size - off;
+ n = msglen < sizeof(state->chunk) - off ? msglen : sizeof(state->chunk) - off;
memcpy(state->chunk + off, message, n);
- if (off + n == state->chunk_size)
+ if (off + n == sizeof(state->chunk))
libsha1_process(state, state->chunk);
message += n;
msglen -= n;
}
- while (msglen >= state->chunk_size) {
+ while (msglen >= sizeof(state->chunk)) {
libsha1_process(state, (const unsigned char *)message);
- message += state->chunk_size;
- msglen -= state->chunk_size;
+ message += sizeof(state->chunk);
+ msglen -= sizeof(state->chunk);
}
if (msglen)