summaryrefslogtreecommitdiffstats
path: root/sha1/test/totp2.c
diff options
context:
space:
mode:
Diffstat (limited to 'sha1/test/totp2.c')
-rw-r--r--sha1/test/totp2.c257
1 files changed, 257 insertions, 0 deletions
diff --git a/sha1/test/totp2.c b/sha1/test/totp2.c
new file mode 100644
index 0000000..9aacd2a
--- /dev/null
+++ b/sha1/test/totp2.c
@@ -0,0 +1,257 @@
+/*******************************************************************************
+ * Teeny SHA-1
+ *
+ * The below sha1digest() calculates a SHA-1 hash value for a
+ * specified data buffer and generates a hex representation of the
+ * result. This implementation is a re-forming of the SHA-1 code at
+ * https://github.com/jinqiangshou/EncryptionLibrary.
+ *
+ * Copyright (c) 2017 CTrabant
+ *
+ * License: MIT, see included LICENSE file for details.
+ *
+ * To use the sha1digest() function either copy it into an existing
+ * project source code file or include this file in a project and put
+ * the declaration (example below) in the sources files where needed.
+ ******************************************************************************/
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+uint8_t key[]={ 0x89,0x0e,0x38,0x6e, 0xbb,0x3c,0xcf, 0xe9,0x00,0x00,0x4f,0xe4};
+uint8_t msg[]={ 0x00,0x00,0x00,0x00,0xa6,0xa7,0x00,0x00};
+
+
+uint8_t data[24];
+uint32_t data2;
+uint8_t w2[16];
+uint8_t dlen;
+uint32_t key_or;
+
+uint8_t buf[128];
+
+
+uint32_t get_w32_016(int idx,int loop)
+{
+uint32_t t1;
+
+if (!loop) {
+
+if (idx<3) {
+ memcpy(&t1,&key[idx<<2],4);
+} else {
+ memset(&t1,0,4);
+}
+
+t1^=key_or;
+
+ return t1;
+}
+
+
+if (idx<dlen) {
+ memcpy(&t1,&data[idx<<2],4);
+ return t1;
+ }
+if (idx<15)
+ return 0;
+return data2;
+}
+
+#define SHA1ROTATELEFT(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
+uint32_t Wbuf[16];
+
+uint32_t get_w32(int idx,int loop)
+{
+ int i;
+ uint32_t t2;
+
+ for (i=0;i<16;++i)
+ Wbuf[i]=get_w32_016(i,loop);
+
+
+ if (idx<16)
+ return Wbuf[idx];
+
+ idx-=16;
+
+
+ while (1) {
+ t2=SHA1ROTATELEFT(Wbuf[13] ^Wbuf[8] ^ Wbuf[2] ^ Wbuf[0],1);
+ if (!idx) return t2;
+
+ memmove(Wbuf,Wbuf+1,60);
+ Wbuf[15]=t2;
+
+ idx--;
+ }
+}
+
+
+
+
+
+
+
+
+int
+sha1digest(uint8_t *digest)
+{
+
+ uint32_t H[] = {0x67452301,
+ 0xEFCDAB89,
+ 0x98BADCFE,
+ 0x10325476,
+ 0xC3D2E1F0};
+ uint32_t a;
+ uint32_t b;
+ uint32_t c;
+ uint32_t d;
+ uint32_t e;
+ uint32_t f = 0;
+ uint32_t k = 0;
+
+ uint32_t idx;
+ uint32_t lidx;
+ uint32_t widx;
+ uint32_t didx = 0;
+
+ int32_t wcount;
+ uint32_t temp;
+ uint8_t datatail[128] = {0};
+
+
+ /* Process each 512-bit chunk */
+ for (lidx = 0; lidx < 2; lidx++)
+ {
+
+ {
+ uint32_t w;;
+ printf("w2=");
+ for (idx=0;idx<80;++idx) {
+ w=get_w32(idx,lidx);
+ printf("%02x%02x%02x%02x",
+ (w>>0) &0xff,
+ (w>>8) &0xff,
+ (w>>16) &0xff,
+ (w>>24) &0xff);
+ }
+ printf("\n");
+ }
+
+ /* Main loop */
+ a = H[0];
+ b = H[1];
+ c = H[2];
+ d = H[3];
+ e = H[4];
+
+ for (idx = 0; idx <= 79; idx++)
+ {
+ if (idx <= 19)
+ {
+ f = (b & c) | ((~b) & d);
+ k = 0x5A827999;
+ }
+ else if (idx >= 20 && idx <= 39)
+ {
+ f = b ^ c ^ d;
+ k = 0x6ED9EBA1;
+ }
+ else if (idx >= 40 && idx <= 59)
+ {
+ f = (b & c) | (b & d) | (c & d);
+ k = 0x8F1BBCDC;
+ }
+ else if (idx >= 60 && idx <= 79)
+ {
+ f = b ^ c ^ d;
+ k = 0xCA62C1D6;
+ }
+ temp = SHA1ROTATELEFT (a, 5) + f + e + k + get_w32(idx,lidx);
+ e = d;
+ d = c;
+ c = SHA1ROTATELEFT (b, 30);
+ b = a;
+ a = temp;
+ }
+
+ H[0] += a;
+ H[1] += b;
+ H[2] += c;
+ H[3] += d;
+ H[4] += e;
+ }
+
+ /* Store binary digest in supplied buffer */
+ if (digest)
+ {
+ for (idx = 0; idx < 5; idx++)
+ {
+ digest[idx * 4 + 0] = (uint8_t) (H[idx] >> 0);
+ digest[idx * 4 + 1] = (uint8_t) (H[idx] >> 8);
+ digest[idx * 4 + 2] = (uint8_t) (H[idx] >> 16);
+ digest[idx * 4 + 3] = (uint8_t) (H[idx] >> 24);
+ }
+ }
+
+ printf("out=");
+ for (idx=0;idx<20;++idx) {
+ printf("%02x",digest[idx]);
+ }
+ printf("\n");
+
+ return 0;
+} /* End of sha1digest() */
+
+
+
+
+int main(int argc,char * argv[])
+{
+int i;
+
+memcpy(data,msg,8);
+data[8]=0;
+data[9]=0;
+data[10]=0;
+data[11]=0x80;
+
+data2=0x00000240;
+
+
+key_or=0x36363636;
+dlen=3;
+sha1digest(data);
+
+data[20]=0;
+data[21]=0;
+data[22]=0;
+data[23]=0x80;
+
+data2=0x000002a0;
+
+key_or=0x5c5c5c5c;
+dlen=6;
+sha1digest(buf);
+
+
+for (i=0;i<20;++i)
+{
+printf("%02x",buf[i]);
+}
+printf("\n");
+
+
+return 0;
+
+
+}
+
+
+
+
+