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
TOTP.c
Go to the documentation of this file.
1#include "TOTP.h"
2#include "sha1.h"
3
4uint8_t* _hmacKey;
5uint8_t _keyLength;
7uint32_t _timeStep;
8
9// Init the library with the private key, its length and the timeStep duration
10void TOTP(uint8_t* hmacKey, uint8_t keyLength, uint32_t timeStep) {
11 _hmacKey = hmacKey;
12 _keyLength = keyLength;
13 _timeStep = timeStep;
14}
15
16void setTimezone(uint8_t timezone){
17 _timeZoneOffset = timezone;
18}
22uint32_t TimeStruct2Timestamp(struct tm time){
23 //time.tm_mon -= 1;
24 //time.tm_year -= 1900;
25 return mktime(&(time)) - (_timeZoneOffset * 3600) - 2208988800;
26}
27
28// Generate a code, using the timestamp provided
29uint32_t getCodeFromTimestamp(uint32_t timeStamp) {
30 uint32_t steps = timeStamp / _timeStep;
31 return getCodeFromSteps(steps);
32}
33
34// Generate a code, using the timestamp provided
35uint32_t getCodeFromTimeStruct(struct tm time) {
37}
38
39// Generate a code, using the number of steps provided
40uint32_t getCodeFromSteps(uint32_t steps) {
41 // STEP 0, map the number of steps in a 8-bytes array (counter value)
42 uint8_t _byteArray[8];
43 _byteArray[0] = 0x00;
44 _byteArray[1] = 0x00;
45 _byteArray[2] = 0x00;
46 _byteArray[3] = 0x00;
47 _byteArray[4] = (uint8_t)((steps >> 24) & 0xFF);
48 _byteArray[5] = (uint8_t)((steps >> 16) & 0xFF);
49 _byteArray[6] = (uint8_t)((steps >> 8) & 0XFF);
50 _byteArray[7] = (uint8_t)((steps & 0XFF));
51
52 // STEP 1, get the HMAC-SHA1 hash from counter and key
54 writeArray(_byteArray, 8);
55 uint8_t* _hash = resultHmac();
56
57 // STEP 2, apply dynamic truncation to obtain a 4-bytes string
58 uint32_t _truncatedHash = 0;
59 uint8_t _offset = _hash[20 - 1] & 0xF;
60 uint8_t j;
61 for (j = 0; j < 4; ++j) {
62 _truncatedHash <<= 8;
63 _truncatedHash |= _hash[_offset + j];
64 }
65
66 // STEP 3, compute the OTP value
67 _truncatedHash &= 0x7FFFFFFF; //Disabled
68 _truncatedHash %= 1000000;
69
70 return _truncatedHash;
71}
void TOTP(uint8_t *hmacKey, uint8_t keyLength, uint32_t timeStep)
TOTP Object, main object for the code generator.
Definition TOTP.c:10
uint32_t getCodeFromSteps(uint32_t steps)
Accepts a number of steps for a HMAC based code, not used for this project From the TOTP library.
Definition TOTP.c:40
uint32_t _timeStep
Definition TOTP.c:7
uint8_t _timeZoneOffset
Definition TOTP.c:6
uint8_t * _hmacKey
Definition TOTP.c:4
uint8_t _keyLength
Definition TOTP.c:5
uint32_t getCodeFromTimestamp(uint32_t timeStamp)
Accepts a timestamp from uint32_t, returns the code in uint32_t.
Definition TOTP.c:29
uint32_t TimeStruct2Timestamp(struct tm time)
Accepts a time struct from struct tm, returns it as a timestamp.
Definition TOTP.c:22
uint32_t getCodeFromTimeStruct(struct tm time)
Accepts a timestamp from struct tm, returns the code in uint32_t, not used for the current project Fr...
Definition TOTP.c:35
void setTimezone(uint8_t timezone)
Set the timezone from UTC, Arizona is always -7, so this is what is hard encoded into main.
Definition TOTP.c:16
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
uint8_t * resultHmac(void)
Definition sha1.c:146