SWE-350 TOTP Generator Milestone 5
The DE-10 board has six 7-segment displays, this can be used to display and generate a time based one-time pin (TOTP).
Loading...
Searching...
No Matches
sha1.c
Go to the documentation of this file.
1#include <string.h>
2#include "sha1.h"
3
4#define SHA1_K0 0x5a827999
5#define SHA1_K20 0x6ed9eba1
6#define SHA1_K40 0x8f1bbcdc
7#define SHA1_K60 0xca62c1d6
8
9uint8_t sha1InitState[] = {
10 0x01,0x23,0x45,0x67, // H0
11 0x89,0xab,0xcd,0xef, // H1
12 0xfe,0xdc,0xba,0x98, // H2
13 0x76,0x54,0x32,0x10, // H3
14 0xf0,0xe1,0xd2,0xc3 // H4
15};
16
17void init(void) {
19 byteCount = 0;
20 bufferOffset = 0;
21}
22
23uint32_t rol32(uint32_t number, uint8_t bits) {
24 return ((number << bits) | (uint32_t)(number >> (32-bits)));
25}
26
27void hashBlock() {
28 uint8_t i;
29 uint32_t a,b,c,d,e,t;
30
31 a=state.w[0];
32 b=state.w[1];
33 c=state.w[2];
34 d=state.w[3];
35 e=state.w[4];
36 for (i=0; i<80; i++) {
37 if (i>=16) {
38 t = buffer.w[(i+13)&15] ^ buffer.w[(i+8)&15] ^ buffer.w[(i+2)&15] ^ buffer.w[i&15];
39 buffer.w[i&15] = rol32(t,1);
40 }
41 if (i<20) {
42 t = (d ^ (b & (c ^ d))) + SHA1_K0;
43 } else if (i<40) {
44 t = (b ^ c ^ d) + SHA1_K20;
45 } else if (i<60) {
46 t = ((b & c) | (d & (b | c))) + SHA1_K40;
47 } else {
48 t = (b ^ c ^ d) + SHA1_K60;
49 }
50 t+=rol32(a,5) + e + buffer.w[i&15];
51 e=d;
52 d=c;
53 c=rol32(b,30);
54 b=a;
55 a=t;
56 }
57 state.w[0] += a;
58 state.w[1] += b;
59 state.w[2] += c;
60 state.w[3] += d;
61 state.w[4] += e;
62}
63
64void addUncounted(uint8_t data) {
65 buffer.b[bufferOffset ^ 3] = data;
68 hashBlock();
69 bufferOffset = 0;
70 }
71}
72
73void write(uint8_t data) {
74 ++byteCount;
75 addUncounted(data);
76
77 return;
78}
79
80void writeArray(uint8_t *buffer, uint8_t size){
81 while (size--) {
82 write(*buffer++);
83 }
84}
85
86void pad() {
87 // Implement SHA-1 padding (fips180-2 5.1.1)
88
89 // Pad with 0x80 followed by 0x00 until the end of the block
90 addUncounted(0x80);
91 while (bufferOffset != 56) addUncounted(0x00);
92
93 // Append length in the last 8 bytes
94 addUncounted(0); // We're only using 32 bit lengths
95 addUncounted(0); // But SHA-1 supports 64 bit lengths
96 addUncounted(0); // So zero pad the top bits
97 addUncounted(byteCount >> 29); // Shifting to multiply by 8
98 addUncounted(byteCount >> 21); // as SHA-1 supports bitstreams as well as
99 addUncounted(byteCount >> 13); // byte.
102}
103
104uint8_t* result(void) {
105 // Pad to complete the last block
106 pad();
107
108 // Swap byte order back
109 uint8_t i;
110 for (i=0; i<5; i++) {
111 uint32_t a,b;
112 a=state.w[i];
113 b=a<<24;
114 b|=(a<<8) & 0x00ff0000;
115 b|=(a>>8) & 0x0000ff00;
116 b|=a>>24;
117 state.w[i]=b;
118 }
119
120 // Return pointer to hash (20 characters)
121 return state.b;
122}
123
124#define HMAC_IPAD 0x36
125#define HMAC_OPAD 0x5c
126
127void initHmac(const uint8_t* key, uint8_t keyLength) {
128 uint8_t i;
129 memset(keyBuffer,0,BLOCK_LENGTH);
130 if (keyLength > BLOCK_LENGTH) {
131 // Hash long keys
132 init();
133 for (;keyLength--;) write(*key++);
134 memcpy(keyBuffer,result(),HASH_LENGTH);
135 } else {
136 // Block length keys are used as is
137 memcpy(keyBuffer,key,keyLength);
138 }
139 // Start inner hash
140 init();
141 for (i=0; i<BLOCK_LENGTH; i++) {
143 }
144}
145
146uint8_t* resultHmac(void) {
147 uint8_t i;
148 // Complete inner hash
149 memcpy(innerHash,result(),HASH_LENGTH);
150 // Calculate outer hash
151 init();
152 for (i=0; i<BLOCK_LENGTH; i++) write(keyBuffer[i] ^ HMAC_OPAD);
153 for (i=0; i<HASH_LENGTH; i++) write(innerHash[i]);
154 return result();
155}
#define SHA1_K60
Definition sha1.c:7
#define HMAC_OPAD
Definition sha1.c:125
void init(void)
Definition sha1.c:17
void writeArray(uint8_t *buffer, uint8_t size)
Definition sha1.c:80
void initHmac(const uint8_t *key, uint8_t keyLength)
Definition sha1.c:127
#define SHA1_K0
Definition sha1.c:4
uint8_t * result(void)
Definition sha1.c:104
void addUncounted(uint8_t data)
Definition sha1.c:64
#define SHA1_K40
Definition sha1.c:6
void hashBlock()
Definition sha1.c:27
uint8_t sha1InitState[]
Definition sha1.c:9
#define SHA1_K20
Definition sha1.c:5
void pad()
Definition sha1.c:86
#define HMAC_IPAD
Definition sha1.c:124
uint8_t * resultHmac(void)
Definition sha1.c:146
uint32_t rol32(uint32_t number, uint8_t bits)
Definition sha1.c:23
void write(uint8_t data)
Definition sha1.c:73
#define BLOCK_LENGTH
Definition sha1.h:4
union _state state
#define HASH_LENGTH
Definition sha1.h:3
uint8_t innerHash[HASH_LENGTH]
Definition sha1.h:18
uint32_t byteCount
Definition sha1.h:16
union _buffer buffer
uint8_t keyBuffer[BLOCK_LENGTH]
Definition sha1.h:17
uint8_t bufferOffset
Definition sha1.h:15
uint32_t w[BLOCK_LENGTH/4]
Definition sha1.h:8
uint8_t b[BLOCK_LENGTH]
Definition sha1.h:7
uint32_t w[HASH_LENGTH/4]
Definition sha1.h:12
uint8_t b[HASH_LENGTH]
Definition sha1.h:11