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
/Users/erickgrant/Documents/Computer Programming/Software-Class/swe350MS/main.c
Go to the documentation of this file.
1// ============================================================================
2// The following code uses excerpts from Terasic Technologies Inc.
3// These are used primarily for the LCD.
4// Below is the copyright for their portion of code
5//
6// Copyright (c) 2013 by Terasic Technologies Inc.
7// ============================================================================
8//
9// Permission:
10//
11// Terasic grants permission to use and modify this code for use
12// in synthesis for all Terasic Development Boards and Altera Development
13// Kits made by Terasic. Other use of this code, including the selling
14// ,duplication, or modification of any portion is strictly prohibited.
15//
16// Disclaimer:
17//
18// This VHDL/Verilog or C/C++ source code is intended as a design reference
19// which illustrates how these types of functions can be implemented.
20// It is the user's responsibility to verify their design for
21// consistency and functionality through the use of formal
22// verification methods. Terasic provides no warranty regarding the use
23// or functionality of this code.
24//
25// ============================================================================
26//
27// Terasic Technologies Inc
28// 9F., No.176, Sec.2, Gongdao 5th Rd, East Dist, Hsinchu City, 30070. Taiwan
29//
30//
31// web: http://www.terasic.com/
32// email: support@terasic.com
33//
34// ============================================================================
35
36// Standard Libraries
37// User defined libraries
39#include "LCD/LCD_Lib.h"
40#include "LCD/lcd_graphic.h"
41#include "LCD/font.h"
42// Random Key Generator
43#include "randomKey/randomKey.h"
44//#include "pushbutton/pushbutton_irq_handler.h"
45#include "address_map_arm.h"
46// Increment LEDS (for seconds remaining)
48
49// 7-segment displays
50#include "7-segment/7seg.h"
51
52// QR Code
53// #include "qr_code/qrcode.h"
54#include "QRCode/src/qrcode.h"
55
56#include <stdio.h>
58#include <time.h>
59
60#include <stdint.h>
61#include <string.h>
62
63
64
65//#include "pushbutton/pushbutton_irq_handler.h"
66#include "address_map_arm.h"
67
68
69
70
71// Define register locations for the DE-10 Standard Board
72
73 #define HW_REGS_BASE ( ALT_STM_OFST )
74 #define HW_REGS_SPAN ( 0x04000000 )
75 #define HW_REGS_MASK ( HW_REGS_SPAN - 1 )
76
77// 7-segment lights
85 #define HEX3_HEX0_BASE 0x00000020 // 3,4,5,6
86
94 #define HEX5_HEX4_BASE 0x00000030 // 1, 2
95
96
97
103// uint8_t* decoded_array; // Will be used to hold the key
104
106// \brief Holds the clone of the key in uint8_t format
107//
108// The key is cloned to prevent issues with key regeneration,
109// stores about 20 bytes for a standard 32-bit key.
110//*/
111//uint8_t* clonedArray; // Clones the decoded array
119char knownKey[32]; // Holds the random key
126size_t output_len; // Size of decoded array
127
128// General setup, including LCD
140volatile int *HEX_ptr; // virtual address pointer to the HEX displays (7-seg)
146volatile int *HEX45_ptr; // virtual address pointer to the HEX 45 displays (7-seg)
147
152int fd = -1; // used to open /dev/mem for access to physical addresses
157void *LW_virtual; // used to map physical addresses for the light-weight bridge
158
159
160// Reset Button
161// Define the button address
162//#define BUTTON_ADDR 0x00000050 // WILL BE DEVELOPED IN A FUTURE VERSION
168volatile int *KEY_ptr; // virtual address for the KEY port
169
175time_t seconds; // Time in seconds from epoch
176
182LCD_CANVAS LcdCanvas; // Create canvas object
183
184// Display Key (QR Code)
191
192
198char codeStr[6]; // Character array for the code
199
205int secondsRemaining = 0; // Prep the time remaining
206
212uint32_t newCode;
213
214
215
216
217
218// Unit Testing
225void runTests() {
226 test7Segment2(); // 7 segment Unit Tests
228}
229
230
231/* GPIO Bit Structure, 6 nibbles (or 24 bits) are tied to Hex 0- Hex 5 BCD Decoders inputs */
232typedef struct
233{
234 unsigned int gpio0 : 4; // lsb
235 unsigned int gpio1 : 4;
236 unsigned int gpio2 : 4;
237 unsigned int gpio3 : 4;
238 unsigned int gpio4 : 4;
239 unsigned int gpio5 : 4;
240 unsigned int gpiou : 11; // msb
243
244volatile unsigned int *JP1_ptr; // Virtual address pointer to JP1 Expansion Port
245
246void displayCode(int x) {
247 int og = x;
248 int b1 = (x % 10);
249 x = x / 10;
250 int b2 = (x % 10);
251 x = x / 10;
252 int b3 = (x % 10);
253 x = x / 10;
254 int b4 = (x % 10);
255 x = x / 10;
256 int b5 = (x % 10);
257 x = x / 10;
258 int b6 = (x % 10);
259
260 gpioRegister->gpio0 = b1;
261 gpioRegister->gpio1 = b2;
262 gpioRegister->gpio2 = b3;
263 gpioRegister->gpio3 = b4;
264 gpioRegister->gpio4 = b5;
265 gpioRegister->gpio5 = b6;
266 printf("Wrote value of %d\r\n", og);
267
268}
269
275int main() {
276
277
278 runTests(); // Runs Unit Testing before running rest of project.
279
280 // Generate initial key, at launch
281 generateStringKey(knownKey,32); // todo: may generate a 134 exit code
282
283 // printf("Redo of known key: %s\n",knownKey);
284 char totpSetupURIinit[100] = "otpauth://totp/EG:MS?secret=";
285
286 // Copy the key to the setup URI
287 strcat(totpSetupURIinit, knownKey);
288 // Print the key to the console
289 printf("Setup key: %s\n", totpSetupURIinit);
290 // Base32 encoded string
291 const char* input = knownKey;
292 // Length of the key in bytes (typically 20)
293 size_t output_len;
294 // Key for the TOTP generator
295 uint8_t* decoded_array = base32_decode2(input, &output_len);
296
297 // Define the TOTP key, 20 for the uin8_t key (20 bytes), 30 seconds for period between codes.
298 TOTP(decoded_array, 20, 30); // Third arg should be 30 for 30 seconds // Set the data for the TOTP
299
300 setTimezone(-7); // Arizona for timezone // leave this on
301 // Begin Showing to user
302
303
304// Code from Terasic
305 // Create virtual memory access to the FPGA light-weight bridge
306 if ((fd = open_physical (fd)) == -1)
307 return (-1);
309 return (-1);
310 JP1_ptr = (unsigned int *) (LW_virtual + JP1_BASE);
311 *(JP1_ptr + 1) = 0x00FFFFFF;
313
314
315 // 7-segment displays creator
316 // HEX_ptr = (unsigned int *) (LW_virtual + HEX3_HEX0_BASE);
317 // HEX45_ptr = (unsigned int *) (LW_virtual + HEX5_HEX4_BASE);
318
319 // *LEDR_ptr = 0;
320 //*HEX_ptr = 0;
321 // *HEX45_ptr = 0;
322// End code from Terasic
323 // Buttons
324 KEY_ptr = LW_virtual + KEY_BASE; // init virtual address for KEY port
325
326 // map the address space for the LED registers into user space so we can interact with them.
327 // we'll actually map in the entire CSR span of the HPS since we want to access various registers within that span
328 if( ( fd = open( "/dev/mem", ( O_RDWR | O_SYNC ) ) ) == -1 ) {
329 printf( "ERROR: could not open \"/dev/mem\"...\n" );
330 return( 1 );
331 }
332
333 virtual_base = mmap( NULL, HW_REGS_SPAN, ( PROT_READ | PROT_WRITE ), MAP_SHARED, fd, HW_REGS_BASE );
334
335 if( virtual_base == MAP_FAILED ) {
336 printf( "ERROR: mmap() failed...\n" );
337 close( fd );
338 return( 1 );
339 }
340
341
346 LcdCanvas.pFrame = (void *)malloc(LcdCanvas.FrameSize);
347
348 if (LcdCanvas.pFrame == NULL){
349 printf("failed to allocate lcd frame buffer\r\n");
350 }else{
351
352
354
355 // turn on LCD backlight
356 LCDHW_BackLight(true);
357
358 // Output to display
359 LCD_Init();
360
361 // clear screen
363
364 //strcat(totpSetupURIinit, knownKey); // Copy the key to the setup URI
365
366 // Print the setup URI (contents of QR code) to the console
367 printf("Key URI: %s\n", totpSetupURIinit);
368
369
370 // Allocate a chunk of memory to store the QR code
371 uint8_t qrcodeBytes[qrcode_getBufferSize(11)];
372
373 // Generate QR code
374 qrcode_initText(&qrcode, qrcodeBytes, 11, ECC_LOW, totpSetupURIinit);
375
376
377
378 // Print QR Code welcome
379 DRAW_Clear(&LcdCanvas, LCD_WHITE); // Clear LCD
380 DRAW_PrintString(&LcdCanvas, 60, 30, "Scan the", LCD_BLACK, &font_16x16);
382 // Output the QR code
384
385 // Print QR Code
386 for (int y = 0; y < qrcode.size; y++) {
387 for (int x = 0; x < qrcode.size; x++) {
388 if (qrcode_getModule(&qrcode, x, y)) {
389 //Serial.print("**");
390 DRAW_Pixel(&LcdCanvas,x,y,1);
391 } else {
392
393 }
394 }
395 }
396 // Refresh display with new QR code
398
399
400 // Update the current time for initial run
401 // Current seconds from epoch
402 seconds = time(NULL);
403 // Get current time code
405 // Output the code to the console
406 printf("\nThe init Code is: %d\n", newCode);
407 // Pointers for hex displays
408 // Output to the last four 7-segment displays
409 //*HEX_ptr = dectodec7(newCode);
410 // Output to the first two 7-segment displays
411 // *HEX45_ptr = leftDec2Dec7(newCode);
413
414// Output the current code
415 // infinite loop
416 for (;;) {
417 // set seconds to current
418 seconds = time(NULL);
419 snprintf(codeStr, sizeof(codeStr), "%6d", newCode); // Character array, copy newcode (int) into the codeStr as string,
420
421 // Runs every 30 seconds
422 if (seconds % 30 == 0 ) {
423 newCode = getCodeFromTimestamp(seconds); // Update the code in uint8_t
424 printf("The code in dec form: %d", newCode);
425
426 // Output to 7-segment displays
427 // *HEX_ptr = dectodec7(newCode); // Output to the 3-6 7-segment displays
428 // *HEX45_ptr = leftDec2Dec7(newCode); // Control left 2 digits of the 7-segment displays.
430 snprintf(codeStr, sizeof(codeStr), "%6d", newCode); // Convert the int into a character array of the pin
431 printf("The Code is: %s\n",codeStr); // print the code to the console
432
433 sleep(1); // Prevent duplication, wait one second between runs
434 } else
435 {
436 // Update the clock on the 7-segment display
437 secondsRemaining = 30 - (seconds % 30); // calculate the time remaining as int.
438 increment_leds(secondsRemaining); // Output to the 10 lights
439 }
440
441 }
442
443 }
446 return 0;
447 // // clean up memory mapping and exit
448 // if( munmap( virtual_base, HW_REGS_SPAN ) != 0 ) {
449 // printf( "ERROR: munmap() failed...\n" );
450 // close( fd );
451 // return( 1 );
452 // }
453 // close( fd );
454 //
455 // // end
456 // return( 0 );
457}
int test7Segment2()
void LCDHW_Init(void *virtual_base)
Definition LCD_Hw.c:84
void LCDHW_BackLight(bool bON)
Definition LCD_Hw.c:235
void LCD_Init(void)
Definition LCD_Lib.c:38
#define LCD_WIDTH
Definition LCD_Lib.h:37
#define LCD_HEIGHT
Definition LCD_Lib.h:38
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 getCodeFromTimestamp(uint32_t timeStamp)
Accepts a timestamp from uint32_t, returns the code in uint32_t.
Definition TOTP.c:29
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
#define LW_BRIDGE_SPAN
#define JP1_BASE
#define LW_BRIDGE_BASE
#define KEY_BASE
FONT_TABLE font_16x16
Definition font.c:1032
int open_physical(int)
int increment_leds(int total)
void close_physical(int)
int unmap_physical(void *, unsigned int)
void * map_physical(int, unsigned int, unsigned int)
void DRAW_PrintString(LCD_CANVAS *pCanvas, int X0, int Y0, char *pText, int Color, FONT_TABLE *font_table)
void DRAW_Clear(LCD_CANVAS *pCanvas, int nValue)
void DRAW_Pixel(LCD_CANVAS *pCanvas, int X, int Y, int Color)
Definition lcd_graphic.c:46
void DRAW_Refresh(LCD_CANVAS *pCanvas)
Definition lcd_graphic.c:65
#define LCD_WHITE
Definition lcd_graphic.h:19
#define LCD_BLACK
Definition lcd_graphic.h:20
void displayCode(int x)
Definition main.c:246
size_t output_len
The length of the uint_8 key in bytes.
Definition main.c:126
volatile int * HEX45_ptr
Virtual address pointer Pointer for the 1-2 7-segment display lights Used to define the address of th...
Definition main.c:146
#define HW_REGS_BASE
Definition main.c:73
void * LW_virtual
Definition main.c:157
volatile int * KEY_ptr
Pointer to the buttons on the board.
Definition main.c:168
void runTests()
Main Unit Testing function This function is run initially at runtime, tests all features made by Eric...
Definition main.c:225
void * virtual_base
Definition main.c:134
int fd
Definition main.c:152
char codeStr[6]
Stores the 6-digit code in a string format.
Definition main.c:198
volatile int * HEX_ptr
Virtual address pointer Pointer for the 3-6 7-segment display lights Used to define the address of th...
Definition main.c:140
QRCode qrcode
Creates a QRCode object.
Definition main.c:190
#define HW_REGS_SPAN
Definition main.c:74
uint32_t newCode
Code as a byte array.
Definition main.c:212
GpioRegister * gpioRegister
Definition main.c:242
LCD_CANVAS LcdCanvas
Creates a LCD_CANVAS object.
Definition main.c:182
int secondsRemaining
Seconds remaining.
Definition main.c:205
char knownKey[32]
Holds the key in a human readable format.
Definition main.c:119
time_t seconds
Used to return the time.
Definition main.c:175
int main()
Main Function Runs all other code.
Definition main.c:275
volatile unsigned int * JP1_ptr
Definition main.c:244
bool qrcode_getModule(QRCode *qrcode, uint8_t x, uint8_t y)
Definition qrcode.c:860
uint16_t qrcode_getBufferSize(uint8_t version)
Definition qrcode.c:775
int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data)
Definition qrcode.c:856
#define ECC_LOW
Definition qrcode.h:55
int testRandomKey()
Definition randomKey.c:209
uint8_t * base32_decode2(const char *input, size_t *output_len)
Decodes a Base32-encoded string into a binary byte array.
Definition randomKey.c:128
void generateStringKey(char *keyString, int length)
Generates a full string of random characters.
Definition randomKey.c:82
unsigned int gpio4
Definition main.c:238
unsigned int gpio5
Definition main.c:239
unsigned int gpio0
Definition main.c:234
unsigned int gpio3
Definition main.c:237
unsigned int gpiou
Definition main.c:240
unsigned int gpio2
Definition main.c:236
unsigned int gpio1
Definition main.c:235
uint8_t * pFrame
Definition lcd_graphic.h:16
int BitPerPixel
Definition lcd_graphic.h:14
uint8_t size
Definition qrcode.h:70