From 72622408bbfb919af5ec8ecb8ff032d1bfd44c75 Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Mon, 12 Oct 2020 17:05:18 -0400 Subject: [PATCH 01/42] Very first version of the port to C. Still i2c call missing and Makefile --- Line_Follower.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 Line_Follower.c diff --git a/Line_Follower.c b/Line_Follower.c new file mode 100644 index 0000000..c528cb3 --- /dev/null +++ b/Line_Follower.c @@ -0,0 +1,159 @@ +/* C module with equivalen functionality like the original Line_Follwer.py */ + +#include +#include +#include + +/* Globals */ +#define NUM_REF 5 +int references[NUM_REF] = {300,300,300,300,300}; +int address = 0x11; +int bus = 1; + +/* Function Prototypes */ +char * read_raw(); +int * read_analog(int trys); +int * read_digital(); +float * get_average(int mount); +int * found_line_in(float timeout); +void wait_tile_status(int *status); +void wait_tile_center(); + + +char * read_raw(){ + int flag = 1; + static char raw_result[NUM_REF*2+1]; + int i; + for(i=0;i 1024){ + continue; + } + } + return analog_result; + } + } + printf("Line follower read error. Please check the wiring.\n"); + return NULL; +} + +int * read_digital(){ + int * lt; + int i; + static int digital_list[NUM_REF]; + lt = read_analog(NUM_REF); + for(i=0;i references[i]){ + digital_list[i] = 0; + } + else if(lt[i] < references[i]){ + digital_list[i] = 1; + } + else{ + digital_list[i] = -1; + } + } +} + +float * get_average(int mount){ + static float average[NUM_REF] = {0.0,0.0,0.0,0.0,0.0}; + float *lt_list [NUM_REF]; + int i,lt_id; + int * lt; + float sum; + for(i=0;i Date: Tue, 13 Oct 2020 09:03:15 -0400 Subject: [PATCH 02/42] Forgot to return in this function --- Line_Follower.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Line_Follower.c b/Line_Follower.c index c528cb3..19f817e 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -78,6 +78,7 @@ int * read_digital(){ digital_list[i] = -1; } } + return digital_list; } float * get_average(int mount){ From 502d8da1302f3a3ba8cddb325ee5f16272a09dcc Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 13 Oct 2020 09:27:01 -0400 Subject: [PATCH 03/42] Added a Makefile for creating a shared library from the .c file --- Makefile | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..17de286 --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +# Specify extensions of files to delete when cleaning +CLEANEXTS = o so d + +# Specify the source files, the target files, +# and the install directory +NAME = Line_Follower +SOURCES = $(NAME).c +OUTPUTFILE = lib$(NAME).so +INSTALLDIR = . + +.PHONY: all +all: $(OUTPUTFILE) + +# Build libgeorgeringo.so from george.o, ringo.o, +# and georgeringo.o; subst is the search-and-replace +# function demonstrated in Recipe 1.16 +$(OUTPUTFILE): $(subst .c,.o,$(SOURCES)) + $(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^ + +.PHONY: install +install: + mkdir -p $(INSTALLDIR) + cp -p $(OUTPUTFILE) $(INSTALLDIR) + +.PHONY: clean +clean: + for file in $(CLEANEXTS); do rm -f *.$$file; done + +# Generate dependencies of .c files on .h files +include $(subst .c,.d,$(SOURCES)) + +%.d: %.c + $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ + sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ +rm -f $@.$$$$ From 32335e4076601ca3be1d267ce0b0113fb7a2d7af Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 13 Oct 2020 10:37:36 -0400 Subject: [PATCH 04/42] Added a simple C test application --- Line_Follower.c | 11 +---------- Line_Follower.h | 14 ++++++++++++++ Line_Follower_Test.c | 15 +++++++++++++++ Makefile | 13 +++++++++---- 4 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 Line_Follower.h create mode 100644 Line_Follower_Test.c diff --git a/Line_Follower.c b/Line_Follower.c index 19f817e..3322c03 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -3,22 +3,13 @@ #include #include #include +#include "Line_Follower.h" /* Globals */ -#define NUM_REF 5 int references[NUM_REF] = {300,300,300,300,300}; int address = 0x11; int bus = 1; -/* Function Prototypes */ -char * read_raw(); -int * read_analog(int trys); -int * read_digital(); -float * get_average(int mount); -int * found_line_in(float timeout); -void wait_tile_status(int *status); -void wait_tile_center(); - char * read_raw(){ int flag = 1; diff --git a/Line_Follower.h b/Line_Follower.h new file mode 100644 index 0000000..9c53e2e --- /dev/null +++ b/Line_Follower.h @@ -0,0 +1,14 @@ +/* Header file to be used by callers of this functionality */ + +#define NUM_REF 5 + +/* Function Prototypes for Line_Follower.c */ +char * read_raw(); +int * read_analog(int trys); +int * read_digital(); +float * get_average(int mount); +int * found_line_in(float timeout); +void wait_tile_status(int *status); +void wait_tile_center(); + + diff --git a/Line_Follower_Test.c b/Line_Follower_Test.c new file mode 100644 index 0000000..43d166d --- /dev/null +++ b/Line_Follower_Test.c @@ -0,0 +1,15 @@ +/* C module with equivalen functionality like the original Line_Follwer.py */ + +#include +#include "Line_Follower.h" + +int main(){ + printf("Line Follower Library Test Application Running\n"); + int *dig_list; + int i; + dig_list = read_digital(); + for(i=0;i Date: Tue, 13 Oct 2020 14:56:45 -0400 Subject: [PATCH 05/42] Added a Python Test Application to Demonstrate How the C Library is Called --- Line_Follower_Test.c | 2 +- Line_Follower_Test.py | 16 ++++++++ Makefile | 2 +- README.md | 85 ++++++++++++++++--------------------------- 4 files changed, 50 insertions(+), 55 deletions(-) create mode 100644 Line_Follower_Test.py diff --git a/Line_Follower_Test.c b/Line_Follower_Test.c index 43d166d..bf9738e 100644 --- a/Line_Follower_Test.c +++ b/Line_Follower_Test.c @@ -4,7 +4,7 @@ #include "Line_Follower.h" int main(){ - printf("Line Follower Library Test Application Running\n"); + printf("Line Follower C Test Application Running\n"); int *dig_list; int i; dig_list = read_digital(); diff --git a/Line_Follower_Test.py b/Line_Follower_Test.py new file mode 100644 index 0000000..6a1a75a --- /dev/null +++ b/Line_Follower_Test.py @@ -0,0 +1,16 @@ +# ctypes_test.py +import ctypes +import pathlib + +if __name__ == "__main__": + print("Line Follower Python Test Application Running") + # Load the shared library into ctypes + libname = pathlib.Path().absolute() / "libLine_Follower.so" + c_lib = ctypes.CDLL(libname) + dt_list = c_lib.read_digital() + ptr = ctypes.cast(dt_list,ctypes.POINTER(ctypes.c_int)) + print(ptr[0]) + print(ptr[1]) + print(ptr[2]) + print(ptr[3]) + print(ptr[4]) diff --git a/Makefile b/Makefile index 38148bd..b413329 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ $(OUTPUTFILE): $(subst .c,.o,$(SOURCES)) test: $(NAME)_Test LD_LIBRARY_PATH=. ./$(NAME)_Test -$(NAME)_Test: lib$(NAME).so +$(NAME)_Test: lib$(NAME).so $(NAME)_Test.c $(CC) $(NAME)_Test.c -o $@ -L. -l$(NAME) .PHONY: install diff --git a/README.md b/README.md index a67b1da..0df34cd 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,32 @@ -## SunFounder UltraSonic_Avoidance -SunFounder UltraSonic_Avoidance - -Quick Links: - - * [About SunFounder UltraSonic_Avoidance](#about_this_module) - * [Update](#update) - * [About SunFounder](#about_sunfounder) - * [License](#license) - * [Contact us](#contact_us) - - -### About SunFounder UltraSonic_Avoidance: -This module is for SunFounder UltraSonic_Avoidance the PCB board, 25KHz Ultra sonic avoidance module - - -### Update: -2016-09-26: - - New Release - ----------------------------------------------- - -### About SunFounder -SunFounder is a technology company focused on Raspberry Pi and Arduino open source community development. Committed to the promotion of open source culture, we strives to bring the fun of electronics making to people all around the world and enable everyone to be a maker. Our products include learning kits, development boards, robots, sensor modules and development tools. In addition to high quality products, SunFounder also offers video tutorials to help you make your own project. If you have interest in open source or making something cool, welcome to join us! - ----------------------------------------------- - -### License -This is the code for SunFounder UltraSonic_Avoidance. -This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied wa rranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -SunFounder UltraSonic_Avoidance comes with ABSOLUTELY NO WARRANTY; for details run ./show w. This is free software, and you are welcome to redistribute it under certain conditions; run ./show c for details. - -SunFounder, Inc., hereby disclaims all copyright interest in the program 'SunFounder UltraSonic_Avoidance' (which makes passes at compilers). - -Mike Huang, 21 August 2015 - -Mike Huang, Chief Executive Officer - -Email: service@sunfounder.com, support@sunfounder.com - ----------------------------------------------- - -### Contact us: -website: - www.sunfounder.com - -E-mail: - service@sunfounder.com, support@sunfounder.com \ No newline at end of file +#Line Follower Modification for Using a C Module + +##Overview: +Line_Follower.c has the same functionality as the original Line_Follower.py +Line_Follower.c is compiled as a shared library with a name libLine_Follower.so +This library can then be called from a C or Python application. +The two '*_Test' files show how this is done respectively in C and Python. + +##Main Files for the Library: +Line_Follower.h +Line_Follower.c +Makefile + +##Test Files: +Line_Follower_Test.c +Line_Follower_Test.py + +##Building the project: +make +make all - Does the same + +##Cleaning the project: +make clean + +##Running tests: +### Running the Line_Follower_Test C Application +export LD_LIBRARY_PATH=. +./Line_Follower +**The following command runs the C Application also +make test - Runs the Line_Follower_Test C executable +### Running the Line_Follower_Test.py Python Application +python3 Line_Follower_Test.py - Runs the Python Test From 6c865352a005dfee344c6372ca2e40aafd8f287d Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 13 Oct 2020 15:01:33 -0400 Subject: [PATCH 06/42] Fixed Markdown for README.md --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0df34cd..db09c43 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,28 @@ -#Line Follower Modification for Using a C Module +# Line Follower Modification for Using a C Module -##Overview: +## Overview: Line_Follower.c has the same functionality as the original Line_Follower.py Line_Follower.c is compiled as a shared library with a name libLine_Follower.so This library can then be called from a C or Python application. The two '*_Test' files show how this is done respectively in C and Python. -##Main Files for the Library: +## Main Files for the Library: Line_Follower.h Line_Follower.c Makefile -##Test Files: +## Test Files: Line_Follower_Test.c Line_Follower_Test.py -##Building the project: +## Building the project: make make all - Does the same -##Cleaning the project: +## Cleaning the project: make clean -##Running tests: +## Running tests: ### Running the Line_Follower_Test C Application export LD_LIBRARY_PATH=. ./Line_Follower From 3344020434b2630b0b3ebb0fb77d24d6c901a0b6 Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 13 Oct 2020 19:09:36 -0400 Subject: [PATCH 07/42] Added an i2c call --- Line_Follower.c | 75 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 14 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 3322c03..5b7835d 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -3,21 +3,63 @@ #include #include #include +#include //Needed for I2C port +#include //Needed for I2C port +#include //Needed for I2C port +#include //Needed for I2C port #include "Line_Follower.h" /* Globals */ +#define RAW_LEN (2*NUM_REF) int references[NUM_REF] = {300,300,300,300,300}; -int address = 0x11; +const int SLAVE_ADDRESS = 0x11; int bus = 1; + +int read_i2c(char *buffer,int length){ + int file_i2c; + //----- OPEN THE I2C BUS ----- + char *filename = (char*)"/dev/i2c-1"; + if ((file_i2c = open(filename, O_RDWR)) < 0) + { + //ERROR HANDLING: you can check errno to see what went wrong + printf("Failed to open the i2c bus"); + return 0; + } + + int addr = SLAVE_ADDRESS; //<<<< references[i]){ - digital_list[i] = 0; - } - else if(lt[i] < references[i]){ - digital_list[i] = 1; - } - else{ - digital_list[i] = -1; + if(lt != NULL){ + for(i=0;i references[i]){ + digital_list[i] = 0; + } + else if(lt[i] < references[i]){ + digital_list[i] = 1; + } + else{ + digital_list[i] = -1; + } } } return digital_list; From 3a1b0a0a25df809b2e740d92a5548210c539deb7 Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Wed, 14 Oct 2020 13:22:28 -0400 Subject: [PATCH 08/42] Added Line breaks in the README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index db09c43..66a35fb 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,16 @@ This library can then be called from a C or Python application. The two '*_Test' files show how this is done respectively in C and Python. ## Main Files for the Library: -Line_Follower.h -Line_Follower.c +Line_Follower.h
+Line_Follower.c
Makefile ## Test Files: -Line_Follower_Test.c +Line_Follower_Test.c
Line_Follower_Test.py ## Building the project: -make +make
make all - Does the same ## Cleaning the project: @@ -24,9 +24,9 @@ make clean ## Running tests: ### Running the Line_Follower_Test C Application -export LD_LIBRARY_PATH=. -./Line_Follower -**The following command runs the C Application also -make test - Runs the Line_Follower_Test C executable +export LD_LIBRARY_PATH=.
+./Line_Follower
+The following command runs the C Application also:
+make test - Runs the Line_Follower_Test C executable
### Running the Line_Follower_Test.py Python Application python3 Line_Follower_Test.py - Runs the Python Test From 7c44bd95450e8c2a84b857fd0853dfbc7e403d36 Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Thu, 29 Oct 2020 11:13:14 -0400 Subject: [PATCH 09/42] Added printfs for the .c line follower module --- Line_Follower.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Line_Follower.c b/Line_Follower.c index 5b7835d..a4c96de 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -21,6 +21,7 @@ int read_i2c(char *buffer,int length){ int file_i2c; //----- OPEN THE I2C BUS ----- char *filename = (char*)"/dev/i2c-1"; + printf("Line_Follower.c :: read_i2c() called\n"); if ((file_i2c = open(filename, O_RDWR)) < 0) { //ERROR HANDLING: you can check errno to see what went wrong @@ -45,7 +46,7 @@ int read_i2c(char *buffer,int length){ } else { - printf("Data read: %s\n", buffer); + printf("Line_Follower.c :: read_i2c() :: Data read: %s\n", buffer); } close(file_i2c); } @@ -54,6 +55,7 @@ char * read_raw(){ int flag = 0; static char raw_result[RAW_LEN+1]; int i; + printf("Line_Follower.c :: read_raw() called\n"); for(i=0;i Date: Thu, 29 Oct 2020 20:59:02 +0000 Subject: [PATCH 10/42] variable declaration not supported by basic compiler --- Line_Follower.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Line_Follower.c b/Line_Follower.c index a4c96de..3d671f4 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -156,11 +156,12 @@ int * found_line_in(float timeout){ float time_during; clock_t time_start; clock_t time_now; + int i; time_start = clock(); time_during = 0; while(time_during < timeout){ lt_status = read_digital(); - for(int i=0;i Date: Mon, 2 Nov 2020 18:20:38 -0500 Subject: [PATCH 11/42] Reverse changes in order to make a pull request from another branch --- Line_Follower.c | 203 ------------------------------------------ Line_Follower.h | 14 --- Line_Follower_Test.c | 15 ---- Line_Follower_Test.py | 16 ---- Makefile | 40 --------- README.md | 85 +++++++++++------- 6 files changed, 53 insertions(+), 320 deletions(-) delete mode 100644 Line_Follower.c delete mode 100644 Line_Follower.h delete mode 100644 Line_Follower_Test.c delete mode 100644 Line_Follower_Test.py delete mode 100644 Makefile diff --git a/Line_Follower.c b/Line_Follower.c deleted file mode 100644 index 3d671f4..0000000 --- a/Line_Follower.c +++ /dev/null @@ -1,203 +0,0 @@ -/* C module with equivalen functionality like the original Line_Follwer.py */ - -#include -#include -#include -#include //Needed for I2C port -#include //Needed for I2C port -#include //Needed for I2C port -#include //Needed for I2C port -#include "Line_Follower.h" - -/* Globals */ -#define RAW_LEN (2*NUM_REF) -int references[NUM_REF] = {300,300,300,300,300}; -const int SLAVE_ADDRESS = 0x11; -int bus = 1; - - - -int read_i2c(char *buffer,int length){ - int file_i2c; - //----- OPEN THE I2C BUS ----- - char *filename = (char*)"/dev/i2c-1"; - printf("Line_Follower.c :: read_i2c() called\n"); - if ((file_i2c = open(filename, O_RDWR)) < 0) - { - //ERROR HANDLING: you can check errno to see what went wrong - printf("Failed to open the i2c bus"); - return 0; - } - - int addr = SLAVE_ADDRESS; //<<<< 1024){ - continue; - } - } - return analog_result; - } - else{ - break; - } - } - printf("Line follower read error. Please check the wiring.\n"); - return NULL; -} - -int * read_digital(){ - int * lt; - int i; - static int digital_list[NUM_REF] = {0}; - printf("Line_Follower.c :: read_digital() called\n"); - lt = read_analog(NUM_REF); - if(lt != NULL){ - for(i=0;i references[i]){ - digital_list[i] = 0; - } - else if(lt[i] < references[i]){ - digital_list[i] = 1; - } - else{ - digital_list[i] = -1; - } - } - } - return digital_list; -} - -float * get_average(int mount){ - static float average[NUM_REF] = {0.0,0.0,0.0,0.0,0.0}; - float *lt_list [NUM_REF]; - int i,lt_id; - int * lt; - float sum; - for(i=0;i -#include "Line_Follower.h" - -int main(){ - printf("Line Follower C Test Application Running\n"); - int *dig_list; - int i; - dig_list = read_digital(); - for(i=0;i $@.$$$$; \ - sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ -rm -f $@.$$$$ diff --git a/README.md b/README.md index 66a35fb..a67b1da 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,53 @@ -# Line Follower Modification for Using a C Module - -## Overview: -Line_Follower.c has the same functionality as the original Line_Follower.py -Line_Follower.c is compiled as a shared library with a name libLine_Follower.so -This library can then be called from a C or Python application. -The two '*_Test' files show how this is done respectively in C and Python. - -## Main Files for the Library: -Line_Follower.h
-Line_Follower.c
-Makefile - -## Test Files: -Line_Follower_Test.c
-Line_Follower_Test.py - -## Building the project: -make
-make all - Does the same - -## Cleaning the project: -make clean - -## Running tests: -### Running the Line_Follower_Test C Application -export LD_LIBRARY_PATH=.
-./Line_Follower
-The following command runs the C Application also:
-make test - Runs the Line_Follower_Test C executable
-### Running the Line_Follower_Test.py Python Application -python3 Line_Follower_Test.py - Runs the Python Test +## SunFounder UltraSonic_Avoidance +SunFounder UltraSonic_Avoidance + +Quick Links: + + * [About SunFounder UltraSonic_Avoidance](#about_this_module) + * [Update](#update) + * [About SunFounder](#about_sunfounder) + * [License](#license) + * [Contact us](#contact_us) + + +### About SunFounder UltraSonic_Avoidance: +This module is for SunFounder UltraSonic_Avoidance the PCB board, 25KHz Ultra sonic avoidance module + + +### Update: +2016-09-26: + - New Release + +---------------------------------------------- + +### About SunFounder +SunFounder is a technology company focused on Raspberry Pi and Arduino open source community development. Committed to the promotion of open source culture, we strives to bring the fun of electronics making to people all around the world and enable everyone to be a maker. Our products include learning kits, development boards, robots, sensor modules and development tools. In addition to high quality products, SunFounder also offers video tutorials to help you make your own project. If you have interest in open source or making something cool, welcome to join us! + +---------------------------------------------- + +### License +This is the code for SunFounder UltraSonic_Avoidance. +This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied wa rranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +SunFounder UltraSonic_Avoidance comes with ABSOLUTELY NO WARRANTY; for details run ./show w. This is free software, and you are welcome to redistribute it under certain conditions; run ./show c for details. + +SunFounder, Inc., hereby disclaims all copyright interest in the program 'SunFounder UltraSonic_Avoidance' (which makes passes at compilers). + +Mike Huang, 21 August 2015 + +Mike Huang, Chief Executive Officer + +Email: service@sunfounder.com, support@sunfounder.com + +---------------------------------------------- + +### Contact us: +website: + www.sunfounder.com + +E-mail: + service@sunfounder.com, support@sunfounder.com \ No newline at end of file From 5b5741aed86dc67aee587b81532c34c5f373aff7 Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Mon, 2 Nov 2020 19:15:59 -0500 Subject: [PATCH 12/42] Adding difference files in cline branch --- Line_Follower.c | 202 ++++++++++++++++++++++++++++++++++++++++++ Line_Follower.h | 14 +++ Line_Follower_Test.c | 15 ++++ Line_Follower_Test.py | 16 ++++ Makefile | 40 +++++++++ README.md | 85 +++++++----------- 6 files changed, 319 insertions(+), 53 deletions(-) create mode 100644 Line_Follower.c create mode 100644 Line_Follower.h create mode 100644 Line_Follower_Test.c create mode 100644 Line_Follower_Test.py create mode 100644 Makefile diff --git a/Line_Follower.c b/Line_Follower.c new file mode 100644 index 0000000..a4c96de --- /dev/null +++ b/Line_Follower.c @@ -0,0 +1,202 @@ +/* C module with equivalen functionality like the original Line_Follwer.py */ + +#include +#include +#include +#include //Needed for I2C port +#include //Needed for I2C port +#include //Needed for I2C port +#include //Needed for I2C port +#include "Line_Follower.h" + +/* Globals */ +#define RAW_LEN (2*NUM_REF) +int references[NUM_REF] = {300,300,300,300,300}; +const int SLAVE_ADDRESS = 0x11; +int bus = 1; + + + +int read_i2c(char *buffer,int length){ + int file_i2c; + //----- OPEN THE I2C BUS ----- + char *filename = (char*)"/dev/i2c-1"; + printf("Line_Follower.c :: read_i2c() called\n"); + if ((file_i2c = open(filename, O_RDWR)) < 0) + { + //ERROR HANDLING: you can check errno to see what went wrong + printf("Failed to open the i2c bus"); + return 0; + } + + int addr = SLAVE_ADDRESS; //<<<< 1024){ + continue; + } + } + return analog_result; + } + else{ + break; + } + } + printf("Line follower read error. Please check the wiring.\n"); + return NULL; +} + +int * read_digital(){ + int * lt; + int i; + static int digital_list[NUM_REF] = {0}; + printf("Line_Follower.c :: read_digital() called\n"); + lt = read_analog(NUM_REF); + if(lt != NULL){ + for(i=0;i references[i]){ + digital_list[i] = 0; + } + else if(lt[i] < references[i]){ + digital_list[i] = 1; + } + else{ + digital_list[i] = -1; + } + } + } + return digital_list; +} + +float * get_average(int mount){ + static float average[NUM_REF] = {0.0,0.0,0.0,0.0,0.0}; + float *lt_list [NUM_REF]; + int i,lt_id; + int * lt; + float sum; + for(i=0;i +#include "Line_Follower.h" + +int main(){ + printf("Line Follower C Test Application Running\n"); + int *dig_list; + int i; + dig_list = read_digital(); + for(i=0;i $@.$$$$; \ + sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ +rm -f $@.$$$$ diff --git a/README.md b/README.md index a67b1da..66a35fb 100644 --- a/README.md +++ b/README.md @@ -1,53 +1,32 @@ -## SunFounder UltraSonic_Avoidance -SunFounder UltraSonic_Avoidance - -Quick Links: - - * [About SunFounder UltraSonic_Avoidance](#about_this_module) - * [Update](#update) - * [About SunFounder](#about_sunfounder) - * [License](#license) - * [Contact us](#contact_us) - - -### About SunFounder UltraSonic_Avoidance: -This module is for SunFounder UltraSonic_Avoidance the PCB board, 25KHz Ultra sonic avoidance module - - -### Update: -2016-09-26: - - New Release - ----------------------------------------------- - -### About SunFounder -SunFounder is a technology company focused on Raspberry Pi and Arduino open source community development. Committed to the promotion of open source culture, we strives to bring the fun of electronics making to people all around the world and enable everyone to be a maker. Our products include learning kits, development boards, robots, sensor modules and development tools. In addition to high quality products, SunFounder also offers video tutorials to help you make your own project. If you have interest in open source or making something cool, welcome to join us! - ----------------------------------------------- - -### License -This is the code for SunFounder UltraSonic_Avoidance. -This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied wa rranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -SunFounder UltraSonic_Avoidance comes with ABSOLUTELY NO WARRANTY; for details run ./show w. This is free software, and you are welcome to redistribute it under certain conditions; run ./show c for details. - -SunFounder, Inc., hereby disclaims all copyright interest in the program 'SunFounder UltraSonic_Avoidance' (which makes passes at compilers). - -Mike Huang, 21 August 2015 - -Mike Huang, Chief Executive Officer - -Email: service@sunfounder.com, support@sunfounder.com - ----------------------------------------------- - -### Contact us: -website: - www.sunfounder.com - -E-mail: - service@sunfounder.com, support@sunfounder.com \ No newline at end of file +# Line Follower Modification for Using a C Module + +## Overview: +Line_Follower.c has the same functionality as the original Line_Follower.py +Line_Follower.c is compiled as a shared library with a name libLine_Follower.so +This library can then be called from a C or Python application. +The two '*_Test' files show how this is done respectively in C and Python. + +## Main Files for the Library: +Line_Follower.h
+Line_Follower.c
+Makefile + +## Test Files: +Line_Follower_Test.c
+Line_Follower_Test.py + +## Building the project: +make
+make all - Does the same + +## Cleaning the project: +make clean + +## Running tests: +### Running the Line_Follower_Test C Application +export LD_LIBRARY_PATH=.
+./Line_Follower
+The following command runs the C Application also:
+make test - Runs the Line_Follower_Test C executable
+### Running the Line_Follower_Test.py Python Application +python3 Line_Follower_Test.py - Runs the Python Test From 120aa4a657264aeefbbb54da884fa2c2e0aa1118 Mon Sep 17 00:00:00 2001 From: antonhristozov Date: Tue, 1 Dec 2020 14:24:20 +0000 Subject: [PATCH 13/42] Fixed code because of a compiler error on the Pi --- Line_Follower.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Line_Follower.c b/Line_Follower.c index a4c96de..3d671f4 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -156,11 +156,12 @@ int * found_line_in(float timeout){ float time_during; clock_t time_start; clock_t time_now; + int i; time_start = clock(); time_during = 0; while(time_during < timeout){ lt_status = read_digital(); - for(int i=0;i Date: Tue, 1 Dec 2020 14:46:15 +0000 Subject: [PATCH 14/42] Had to change the path type for this version of Python on this Raspbian image --- Line_Follower_Test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Line_Follower_Test.py b/Line_Follower_Test.py index 6a1a75a..15b9d47 100644 --- a/Line_Follower_Test.py +++ b/Line_Follower_Test.py @@ -1,11 +1,13 @@ # ctypes_test.py import ctypes import pathlib +import os if __name__ == "__main__": print("Line Follower Python Test Application Running") # Load the shared library into ctypes - libname = pathlib.Path().absolute() / "libLine_Follower.so" + libname = os.path.abspath(".") + "/" + "libLine_Follower.so"; + print(libname) c_lib = ctypes.CDLL(libname) dt_list = c_lib.read_digital() ptr = ctypes.cast(dt_list,ctypes.POINTER(ctypes.c_int)) From 9db2012e17cb3d016b9da1abd99050ad58d3d910 Mon Sep 17 00:00:00 2001 From: antonhristozov Date: Tue, 1 Dec 2020 15:00:51 +0000 Subject: [PATCH 15/42] Had to return the number of bytes read --- Line_Follower.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Line_Follower.c b/Line_Follower.c index 3d671f4..4c6f12a 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -19,6 +19,7 @@ int bus = 1; int read_i2c(char *buffer,int length){ int file_i2c; + int bytes_read = 0; //----- OPEN THE I2C BUS ----- char *filename = (char*)"/dev/i2c-1"; printf("Line_Follower.c :: read_i2c() called\n"); @@ -39,7 +40,7 @@ int read_i2c(char *buffer,int length){ //----- READ BYTES ----- - if (read(file_i2c, buffer, length) != length) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) + if ((bytes_read=read(file_i2c, buffer, length)) != length) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) { //ERROR HANDLING: i2c transaction failed printf("Failed to read from the i2c bus.\n"); @@ -49,6 +50,7 @@ int read_i2c(char *buffer,int length){ printf("Line_Follower.c :: read_i2c() :: Data read: %s\n", buffer); } close(file_i2c); + return bytes_read; } char * read_raw(){ From e63f764b938d718561f0a45ba7be9514d3fa5e5a Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 3 Nov 2020 09:52:32 -0500 Subject: [PATCH 16/42] Adding hmac256 files in a subfolder --- hmac256/hmac-sha256.c | 201 ++++++++++++++++++++++ hmac256/hmac-sha256.h | 55 ++++++ hmac256/sha256.c | 389 ++++++++++++++++++++++++++++++++++++++++++ hmac256/sha256.h | 57 +++++++ 4 files changed, 702 insertions(+) create mode 100644 hmac256/hmac-sha256.c create mode 100644 hmac256/hmac-sha256.h create mode 100644 hmac256/sha256.c create mode 100644 hmac256/sha256.h diff --git a/hmac256/hmac-sha256.c b/hmac256/hmac-sha256.c new file mode 100644 index 0000000..c70b7a2 --- /dev/null +++ b/hmac256/hmac-sha256.c @@ -0,0 +1,201 @@ +/* + * @UBERXMHF_LICENSE_HEADER_START@ + * + * uber eXtensible Micro-Hypervisor Framework (Raspberry Pi) + * + * Copyright 2018 Carnegie Mellon University. All Rights Reserved. + * + * NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING + * INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON + * UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, + * AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR + * PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF + * THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF + * ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT + * INFRINGEMENT. + * + * Released under a BSD (SEI)-style license, please see LICENSE or + * contact permission@sei.cmu.edu for full terms. + * + * [DISTRIBUTION STATEMENT A] This material has been approved for public + * release and unlimited distribution. Please see Copyright notice for + * non-US Government use and distribution. + * + * Carnegie Mellon is registered in the U.S. Patent and Trademark Office by + * Carnegie Mellon University. + * + * @UBERXMHF_LICENSE_HEADER_END@ + */ + +/* + * Author: Amit Vasudevan (amitvasudevan@acm.org) + * + */ + +/* + * hmac-sha2 implementation + * author: amit vasudevan (amitvasudevan@acm.org) + * matt mccormack (matthew.mccormack@live.com) + * adapted from libtomcrypto + */ + + +#include +#include +#include +#include + + +#define LTC_HMAC_SHA2_BLOCKSIZE 64 + +/** + Initialize an HMAC context. + @param hmac The HMAC state + @param hash The index of the hash you want to use + @param key The secret key + @param keylen The length of the secret key (octets) + @return CRYPT_OK if successful +**/ +int hmac_sha256_init(hmac_state *hmac, const unsigned char *key, unsigned long keylen) { + unsigned char buf[LTC_HMAC_SHA2_BLOCKSIZE]; + unsigned long hashsize; + unsigned long i, z; + int err; + + LTC_ARGCHK(hmac != NULL); + LTC_ARGCHK(key != NULL); + + hmac->hash = 0; + hashsize = 32; + + /* valid key length? */ + if (keylen == 0) { + return CRYPT_INVALID_KEYSIZE; + } + + /* (1) make sure we have a large enough key */ + if(keylen > LTC_HMAC_SHA2_BLOCKSIZE) { + z = LTC_HMAC_SHA2_BLOCKSIZE; + if ((err = sha256_memory(key, keylen, hmac->key, &z)) != CRYPT_OK) { + goto LBL_ERR; + } + keylen = hashsize; + } else { + XMEMCPY(hmac->key, key, (size_t)keylen); + } + + if(keylen < LTC_HMAC_SHA2_BLOCKSIZE) { + memset((hmac->key) + keylen, 0, (size_t)(LTC_HMAC_BLOCKSIZE - keylen)); + } + + /* Create the initial vector for step (3) */ + for(i=0; i < LTC_HMAC_SHA2_BLOCKSIZE; i++) { + buf[i] = hmac->key[i] ^ 0x36; + } + + /* Pre-pend that to the hash data */ + if ((err = sha256_init(&hmac->md)) != CRYPT_OK) { + goto LBL_ERR; + } + + if ((err = sha256_process(&hmac->md, buf, LTC_HMAC_SHA2_BLOCKSIZE)) != CRYPT_OK) { + goto LBL_ERR; + } + + goto done; +LBL_ERR: +done: + return err; +} + + +/** + Process data through HMAC + @param hmac The hmac state + @param in The data to send through HMAC + @param inlen The length of the data to HMAC (octets) + @return CRYPT_OK if successful +**/ +int hmac_sha256_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen) { + LTC_ARGCHK(hmac != NULL); + LTC_ARGCHK(in != NULL); + return sha256_process(&hmac->md, in, inlen); +} + + +/** + Terminate an HMAC session + @param hmac The HMAC state + @param out [out] The destination of the HMAC authentication tag + @param outlen [in/out] The max size and resulting size of the HMAC + authentication tag + @return CRYPT_OK if successful +**/ +int hmac_sha256_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen) { + unsigned char buf[LTC_HMAC_SHA2_BLOCKSIZE], isha[32]; + unsigned long hashsize, i; + int err; + + LTC_ARGCHK(hmac != NULL); + LTC_ARGCHK(out != NULL); + + /* get the hash message digest size */ + hashsize = 32; + + /* Get the hash of the first HMAC vector plus the data */ + if ((err = sha256_done(&hmac->md, isha)) != CRYPT_OK) { + goto LBL_ERR; + } + + /* Create the second HMAC vector vector for step (3) */ + for(i=0; i < LTC_HMAC_SHA2_BLOCKSIZE; i++) { + buf[i] = hmac->key[i] ^ 0x5C; + } + + /* Now calculate the "outer" hash for step (5), (6), and (7) */ + if ((err = sha256_init(&hmac->md)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = sha256_process(&hmac->md, buf, LTC_HMAC_SHA2_BLOCKSIZE)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = sha256_process(&hmac->md, isha, hashsize)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = sha256_done(&hmac->md, buf)) != CRYPT_OK) { goto LBL_ERR; } + + /* copy to output */ + for (i = 0; i < hashsize && i < *outlen; i++) { + out[i] = buf[i]; + } + *outlen = i; + + err = CRYPT_OK; +LBL_ERR: + return err; +} + + +/** + HMAC a block of memory to produce the authentication tag + @param hash The index of the hash to use + @param key The secret key + @param keylen The length of the secret key (octets) + @param in The data to HMAC + @param inlen The length of the data to HMAC (octets) + @param out [out] Destination of the authentication tag + @param outlen [in/out] Max size and resulting size of authentication tag + @return CRYPT_OK if successful +**/ +int hmac_sha256_memory(const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen) { + hmac_state hmac; + int err; + + LTC_ARGCHK(key != NULL); + LTC_ARGCHK(in != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + if ((err = hmac_sha256_init(&hmac, key, keylen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = hmac_sha256_process(&hmac, in, inlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = hmac_sha256_done(&hmac, out, outlen)) != CRYPT_OK) { goto LBL_ERR; } + err = CRYPT_OK; +LBL_ERR: + return err; +} diff --git a/hmac256/hmac-sha256.h b/hmac256/hmac-sha256.h new file mode 100644 index 0000000..59ad668 --- /dev/null +++ b/hmac256/hmac-sha256.h @@ -0,0 +1,55 @@ +/* + * @UBERXMHF_LICENSE_HEADER_START@ + * + * uber eXtensible Micro-Hypervisor Framework (Raspberry Pi) + * + * Copyright 2018 Carnegie Mellon University. All Rights Reserved. + * + * NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING + * INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON + * UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, + * AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR + * PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF + * THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF + * ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT + * INFRINGEMENT. + * + * Released under a BSD (SEI)-style license, please see LICENSE or + * contact permission@sei.cmu.edu for full terms. + * + * [DISTRIBUTION STATEMENT A] This material has been approved for public + * release and unlimited distribution. Please see Copyright notice for + * non-US Government use and distribution. + * + * Carnegie Mellon is registered in the U.S. Patent and Trademark Office by + * Carnegie Mellon University. + * + * @UBERXMHF_LICENSE_HEADER_END@ + */ + +/* + * Author: Amit Vasudevan (amitvasudevan@acm.org) + * matt mccormack (matthew.mccormack@live.com) + * + */ + +#ifndef __HMAC_SHA256_H__ +#define __HMAC_SHA256_H__ + +#ifndef __ASSEMBLY__ + +#include + +int hmac_sha256_init(hmac_state *hmac, const unsigned char *key, + unsigned long keylen); +int hmac_sha256_process(hmac_state *hmac, const unsigned char *in, + unsigned long inlen); +int hmac_sha256_done(hmac_state *hmac, unsigned char *out, + unsigned long *outlen); +int hmac_sha256_memory(const unsigned char *key, unsigned long keylen, + const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); + +#endif // __ASSEMBLY__ + +#endif /* __HMAC_SHA256_H__ */ diff --git a/hmac256/sha256.c b/hmac256/sha256.c new file mode 100644 index 0000000..234a66a --- /dev/null +++ b/hmac256/sha256.c @@ -0,0 +1,389 @@ +/* + * @UBERXMHF_LICENSE_HEADER_START@ + * + * uber eXtensible Micro-Hypervisor Framework (Raspberry Pi) + * + * Copyright 2018 Carnegie Mellon University. All Rights Reserved. + * + * NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING + * INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON + * UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, + * AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR + * PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF + * THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF + * ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT + * INFRINGEMENT. + * + * Released under a BSD (SEI)-style license, please see LICENSE or + * contact permission@sei.cmu.edu for full terms. + * + * [DISTRIBUTION STATEMENT A] This material has been approved for public + * release and unlimited distribution. Please see Copyright notice for + * non-US Government use and distribution. + * + * Carnegie Mellon is registered in the U.S. Patent and Trademark Office by + * Carnegie Mellon University. + * + * @UBERXMHF_LICENSE_HEADER_END@ + */ + +/* + * Author: Amit Vasudevan (amitvasudevan@acm.org) + * Matt McCormack (matthew.mccormack@live.com) + * + */ + +#include +#include +#include + +/* Various logical functions */ +#define Ch(x,y,z) (z ^ (x & (y ^ z))) +#define Maj(x,y,z) (((x | y) & z) | (x & y)) +#define S(x, n) RORc((x),(n)) +#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n)) +#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) +#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) +#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) +#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) + + +int sha256_compress(hash_state * md, const unsigned char *buf) { + uint32_t S[8], W[64], t0, t1; + int i; + + /* copy state into S */ + for (i = 0; i < 8; i++) { + S[i] = md->sha256.state[i]; + } + + /* copy the state into 512-bits into W[0..15] */ + for (i = 0; i < 16; i++) { + LOAD32H(W[i], buf + (4*i)); + } + + /* fill W[16..63] */ + for (i = 16; i < 64; i++) { + W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; + } + + #define RND(a,b,c,d,e,f,g,h,i,ki) \ + t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ + t1 = Sigma0(a) + Maj(a, b, c); \ + d += t0; \ + h = t0 + t1; + + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3); + RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee); + RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f); + RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814); + RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208); + RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa); + RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb); + RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7); + RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2); + + #undef RND + + /* feedback */ + for (i = 0; i < 8; i++) { + md->sha256.state[i] = md->sha256.state[i] + S[i]; + } + return CRYPT_OK; +} + +/** + Initialize the hash state + @param md The hash state you wish to initialize + @return CRYPT_OK if successful +*/ +int sha256_init(hash_state * md) { + LTC_ARGCHK(md != NULL); + + md->sha256.curlen = 0; + md->sha256.length = 0; + md->sha256.state[0] = 0x6A09E667UL; + md->sha256.state[1] = 0xBB67AE85UL; + md->sha256.state[2] = 0x3C6EF372UL; + md->sha256.state[3] = 0xA54FF53AUL; + md->sha256.state[4] = 0x510E527FUL; + md->sha256.state[5] = 0x9B05688CUL; + md->sha256.state[6] = 0x1F83D9ABUL; + md->sha256.state[7] = 0x5BE0CD19UL; + return CRYPT_OK; +} + +/** + Process a block of memory though the hash + @param md The hash state + @param in The data to hash + @param inlen The length of the data (octets) + @return CRYPT_OK if successful +*/ +int sha256_process (hash_state * md, const unsigned char *in, + unsigned long inlen) { + unsigned long n; + int err; + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(in != NULL); + if (md->sha256.curlen > sizeof(md->sha256.buf)) { + return CRYPT_INVALID_ARG; + } + if ((md->sha256.length + inlen) < md->sha256.length) { + return CRYPT_HASH_OVERFLOW; + } + while (inlen > 0) { + if (md->sha256.curlen == 0 && inlen >= 64) { + if ((err = sha256_compress (md, (unsigned char *)in)) != CRYPT_OK) { + return err; + } + md->sha256.length += 64 * 8; + in += 64; + inlen -= 64; + } else { + n = MIN(inlen, (64 - md-> sha256.curlen)); + XMEMCPY(md->sha256.buf + md->sha256.curlen, in, (size_t)n); + md->sha256.curlen += n; + in += n; + inlen -= n; + if (md->sha256.curlen == 64) { + if ((err = sha256_compress (md, md->sha256.buf)) != CRYPT_OK) { + return err; + } + md->sha256.length += 8*64; + md->sha256.curlen = 0; + } + } + } + return CRYPT_OK; +} + +/** + Terminate the hash to get the digest + @param md The hash state + @param out [out] The destination of the hash (32 bytes) + @return CRYPT_OK if successful +*/ +int sha256_done(hash_state * md, unsigned char *out) { + int i; + + LTC_ARGCHK(md != NULL); + LTC_ARGCHK(out != NULL); + + if (md->sha256.curlen >= sizeof(md->sha256.buf)) { + return CRYPT_INVALID_ARG; + } + + /* increase the length of the message */ + md->sha256.length += md->sha256.curlen * 8; + + /* append the '1' bit */ + md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md->sha256.curlen > 56) { + while (md->sha256.curlen < 64) { + md->sha256.buf[md->sha256.curlen++] = (unsigned char)0; + } + sha256_compress(md, md->sha256.buf); + md->sha256.curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md->sha256.curlen < 56) { + md->sha256.buf[md->sha256.curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64H(md->sha256.length, md->sha256.buf+56); + sha256_compress(md, md->sha256.buf); + + /* copy output */ + for (i = 0; i < 8; i++) { + STORE32H(md->sha256.state[i], out+(4*i)); + } + return CRYPT_OK; +} + +/** + Self-test the hash + @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled +*/ +int sha256_test(void) { + static const struct { + const char *msg; + unsigned char hash[32]; + } tests[] = { + { "abc", + { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, + 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, + 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, + 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad } + }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, + 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, + 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, + 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 } + }, + }; + + int i; + unsigned char tmp[32]; + hash_state md; + + for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { + sha256_init(&md); + sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); + sha256_done(&md, tmp); + } + return CRYPT_OK; +} + +/** + Hash a block of memory and store the digest. + @param hash The index of the hash you wish to use + @param in The data you wish to hash + @param inlen The length of the data to hash (octets) + @param out [out] Where to store the digest + @param outlen [in/out] Max size and resulting size of the digest + @return CRYPT_OK if successful +*/ +//int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) +int sha256_memory(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen) { + hash_state md; + int err; + + LTC_ARGCHK(in != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + if (*outlen < 32) { + *outlen = 32; + return CRYPT_BUFFER_OVERFLOW; + } + + if ((err = sha256_init(&md)) != CRYPT_OK) { + goto LBL_ERR; + } + if ((err = sha256_process(&md, in, inlen)) != CRYPT_OK) { + goto LBL_ERR; + } + err = sha256_done(&md, out); + *outlen = 32; +LBL_ERR: + + return err; +} + +/** + Hash multiple (non-adjacent) blocks of memory at once. + @param hash The index of the hash you wish to use + @param out [out] Where to store the digest + @param outlen [in/out] Max size and resulting size of the digest + @param in The data you wish to hash + @param inlen The length of the data to hash (octets) + @param ... tuples of (data,len) pairs to hash, terminated with a (NULL,x) (x=don't care) + @return CRYPT_OK if successful +*/ +int sha256_memory_multi(unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...) { + hash_state md; + int err; + va_list args; + const unsigned char *curptr; + unsigned long curlen; + + LTC_ARGCHK(in != NULL); + LTC_ARGCHK(out != NULL); + LTC_ARGCHK(outlen != NULL); + + if (*outlen < 32) { + *outlen = 32; + return CRYPT_BUFFER_OVERFLOW; + } + + if ((err = sha256_init(&md)) != CRYPT_OK) { + goto LBL_ERR; + } + + va_start(args, inlen); + curptr = in; + curlen = inlen; + for (;;) { + /* process buf */ + if ((err = sha256_process(&md, curptr, curlen)) != CRYPT_OK) { + goto LBL_ERR; + } + /* step to next */ + curptr = va_arg(args, const unsigned char*); + if (curptr == NULL) { + break; + } + curlen = va_arg(args, unsigned long); + } + err = sha256_done(&md, out); + *outlen = 32; +LBL_ERR: + va_end(args); + return err; +} diff --git a/hmac256/sha256.h b/hmac256/sha256.h new file mode 100644 index 0000000..3b54a87 --- /dev/null +++ b/hmac256/sha256.h @@ -0,0 +1,57 @@ +/* + * @UBERXMHF_LICENSE_HEADER_START@ + * + * uber eXtensible Micro-Hypervisor Framework (Raspberry Pi) + * + * Copyright 2018 Carnegie Mellon University. All Rights Reserved. + * + * NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING + * INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON + * UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, + * AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR + * PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF + * THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF + * ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT + * INFRINGEMENT. + * + * Released under a BSD (SEI)-style license, please see LICENSE or + * contact permission@sei.cmu.edu for full terms. + * + * [DISTRIBUTION STATEMENT A] This material has been approved for public + * release and unlimited distribution. Please see Copyright notice for + * non-US Government use and distribution. + * + * Carnegie Mellon is registered in the U.S. Patent and Trademark Office by + * Carnegie Mellon University. + * + * @UBERXMHF_LICENSE_HEADER_END@ + */ + +/* + * Author: Amit Vasudevan (amitvasudevan@acm.org) + * Matt McCormack (matthew.mccormack@live.com) + * + */ + +#ifndef __SHA256_H__ +#define __SHA256_H__ + +#include + +#define SHA2_RESULTLEN (256/8) +#define SHA_DIGEST_LENGTH SHA2_RESULTLEN + +#ifndef __ASSEMBLY__ + +int sha256_compress(hash_state *md, const unsigned char *buf); +int sha256_init(hash_state * md); +int sha256_process (hash_state * md, const unsigned char *in, + unsigned long inlen); +int sha256_done(hash_state * md, unsigned char *out); +int sha256_memory(const unsigned char *in, unsigned long inlen, + unsigned char *out, unsigned long *outlen); +int sha256_memory_multi(unsigned char *out, unsigned long *outlen, + const unsigned char *in, unsigned long inlen, ...); +#endif // __ASSEMBLY__ + +#endif /* __SHA256_H__ */ From deea6e353fd23e151e989efd24d362741c60b05a Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 3 Nov 2020 10:08:33 -0500 Subject: [PATCH 17/42] Needed this header file in order to compile the hmac256 files --- hmac256/xmhfcrypto.h | 268 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100644 hmac256/xmhfcrypto.h diff --git a/hmac256/xmhfcrypto.h b/hmac256/xmhfcrypto.h new file mode 100644 index 0000000..ed80070 --- /dev/null +++ b/hmac256/xmhfcrypto.h @@ -0,0 +1,268 @@ +/* + * @UBERXMHF_LICENSE_HEADER_START@ + * + * uber eXtensible Micro-Hypervisor Framework (Raspberry Pi) + * + * Copyright 2018 Carnegie Mellon University. All Rights Reserved. + * + * NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING + * INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON + * UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, + * AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR + * PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF + * THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF + * ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT + * INFRINGEMENT. + * + * Released under a BSD (SEI)-style license, please see LICENSE or + * contact permission@sei.cmu.edu for full terms. + * + * [DISTRIBUTION STATEMENT A] This material has been approved for public + * release and unlimited distribution. Please see Copyright notice for + * non-US Government use and distribution. + * + * Carnegie Mellon is registered in the U.S. Patent and Trademark Office by + * Carnegie Mellon University. + * + * @UBERXMHF_LICENSE_HEADER_END@ + */ + +/* + * Author: Amit Vasudevan (amitvasudevan@acm.org) + * + */ + +#ifndef __XMHFCRYPTO_H__ +#define __XMHFCRYPTO_H__ + +//tomcrypt.h + +/* max size of either a cipher/hash block or symmetric key [largest of the two] */ +#define MAXBLOCKSIZE 128 + +/* descriptor table size */ +#define TAB_SIZE 32 + +/* error codes [will be expanded in future releases] */ +enum { + CRYPT_OK=0, /* Result OK */ + CRYPT_ERROR, /* Generic Error */ + CRYPT_NOP, /* Not a failure but no operation was performed */ + + CRYPT_INVALID_KEYSIZE, /* Invalid key size given */ + CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */ + CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */ + + CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */ + CRYPT_INVALID_PACKET, /* Invalid input packet given */ + + CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */ + CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */ + + CRYPT_INVALID_CIPHER, /* Invalid cipher specified */ + CRYPT_INVALID_HASH, /* Invalid hash specified */ + CRYPT_INVALID_PRNG, /* Invalid PRNG specified */ + + CRYPT_MEM, /* Out of memory */ + + CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */ + CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */ + + CRYPT_INVALID_ARG, /* Generic invalid argument */ + CRYPT_FILE_NOTFOUND, /* File Not Found */ + + CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */ + CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */ + CRYPT_PK_DUP, /* Duplicate key already in key ring */ + CRYPT_PK_NOT_FOUND, /* Key not found in keyring */ + CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */ + + CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */ + CRYPT_PK_INVALID_PADDING, /* Invalid padding on input */ + + CRYPT_HASH_OVERFLOW /* Hash applied to too many bits */ + +}; + + + + + + +//tomcrypt_cfg.h +typedef uint32_t LTC_FAST_TYPE; +#define LTC_FAST_TYPE_PTR_CAST(x) ((LTC_FAST_TYPE*)(void*)(x)) + +//tomcrypt_macros.h +#define STORE32H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ + (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } + +#define LOAD32H(x, y) \ + { x = ((unsigned long)((y)[0] & 255)<<24) | \ + ((unsigned long)((y)[1] & 255)<<16) | \ + ((unsigned long)((y)[2] & 255)<<8) | \ + ((unsigned long)((y)[3] & 255)); } + +#define STORE64H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ + (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ + (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ + (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } + +#ifndef MIN + #define MIN(x, y) ( ((x)<(y))?(x):(y) ) +#endif + +/* rotates the hard way */ +#define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) +#define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) +#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) +#define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) + +/* extract a byte portably */ +#define byte(x, n) (((x) >> (8 * (n))) & 255) + +//tomcrypt_cipher.h +//Empty for now + +struct rijndael_key { + uint32_t eK[60], dK[60]; + int Nr; +}; + + +typedef union Symmetric_key { + struct rijndael_key rijndael; + void *data; +} symmetric_key; + + +/** A block cipher CBC structure */ +typedef struct { + /** The index of the cipher chosen */ + int cipher, + /** The block size of the given cipher */ + blocklen; + /** The current IV */ + unsigned char IV[MAXBLOCKSIZE]; + /** The scheduled key */ + symmetric_key key; +} symmetric_CBC; + + + +/** cipher descriptor table, last entry has "name == NULL" to mark the end of table */ +struct ltc_cipher_descriptor { + /** name of cipher */ + char *name; + /** internal ID */ + unsigned char ID; + /** min keysize (octets) */ + int min_key_length, + /** max keysize (octets) */ + max_key_length, + /** block size (octets) */ + block_length, + /** default number of rounds */ + default_rounds; +}; + + +//tomcrypt_hash.h +struct sha1_state { + uint64_t length; + uint32_t state[5], curlen; + uint8_t buf[64]; +}; + +struct sha256_state { + uint64_t length; + uint32_t state[8], curlen; + uint8_t buf[64]; +}; + + +typedef union Hash_state { + char dummy[1]; + struct sha1_state sha1; + struct sha256_state sha256; + void *data; +} hash_state; + + +//tomcrypt_mac.h +//empty for now + +#define LTC_HMAC_BLOCKSIZE 64 + +typedef struct Hmac_state { + hash_state md; + int hash; + hash_state hashstate; + unsigned char key[LTC_HMAC_BLOCKSIZE]; +} hmac_state; + + +//tomcrypt_custom.h + +/* default no functions */ +#define LTC_MUTEX_GLOBAL(x) +#define LTC_MUTEX_PROTO(x) +#define LTC_MUTEX_TYPE(x) +#define LTC_MUTEX_INIT(x) +#define LTC_MUTEX_LOCK(x) +#define LTC_MUTEX_UNLOCK(x) + +#define XMEMCMP memcmp +#define XMEMCPY memcpy +#define XMEMSET memset +#define XMALLOC malloc +#define XCALLOC calloc +#define XREALLOC realloc +#define XQSORT qsort +#define XFREE free +#define XSTRCMP strcmp + +//tomcrypt_prng.h +//empty for now + + + +//tomcrypt_pk.h +//empty for now + + +//tomcrypt_math.h +//empty for now + + +//tommath.h + +#ifndef MIN + #define MIN(x,y) ((x)<(y)?(x):(y)) +#endif + +#ifndef MAX + #define MAX(x,y) ((x)>(y)?(x):(y)) +#endif + +#ifndef CHAR_BIT + #define CHAR_BIT 8 +#endif + + +//tomcrypt_misc.h +//empty for now + + +//tomcrypt_argchk.h +#define LTC_ARGCHK(x) +#define LTC_ARGCHKVD(x) LTC_ARGCHK(x) + + +//tomcrypt_pkcs.h +//empty for now + + +#endif /* __XMHFCRYPTO_H__ */ From 1d3793c54c9ab70ee14dd46fe7fb6e5d87f7dcd3 Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 3 Nov 2020 10:28:25 -0500 Subject: [PATCH 18/42] Predefined types needed for compilation --- hmac256/stdint.h | 343 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 343 insertions(+) create mode 100644 hmac256/stdint.h diff --git a/hmac256/stdint.h b/hmac256/stdint.h new file mode 100644 index 0000000..7e89920 --- /dev/null +++ b/hmac256/stdint.h @@ -0,0 +1,343 @@ +/* + * @UBERXMHF_LICENSE_HEADER_START@ + * + * uber eXtensible Micro-Hypervisor Framework (Raspberry Pi) + * + * Copyright 2018 Carnegie Mellon University. All Rights Reserved. + * + * NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING + * INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON + * UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, + * AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR + * PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF + * THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF + * ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT + * INFRINGEMENT. + * + * Released under a BSD (SEI)-style license, please see LICENSE or + * contact permission@sei.cmu.edu for full terms. + * + * [DISTRIBUTION STATEMENT A] This material has been approved for public + * release and unlimited distribution. Please see Copyright notice for + * non-US Government use and distribution. + * + * Carnegie Mellon is registered in the U.S. Patent and Trademark Office by + * Carnegie Mellon University. + * + * @UBERXMHF_LICENSE_HEADER_END@ + */ + +/* + * Author: Amit Vasudevan (amitvasudevan@acm.org) + * + */ + +/** + * Modified for XMHF. + */ + +#ifndef _SYS_STDINT_H_ +#define _SYS_STDINT_H_ + +/* + * Basic types upon which most other types are built. + */ +typedef signed char __int8_t; +typedef unsigned char __uint8_t; +typedef short __int16_t; +typedef unsigned short __uint16_t; +typedef int __int32_t; +typedef unsigned int __uint32_t; + +/* LONGLONG */ +typedef long long __int64_t; +/* LONGLONG */ +typedef unsigned long long __uint64_t; + +/* + * Standard type definitions. + */ +typedef unsigned long __clock_t; /* clock()... */ +typedef unsigned int __cpumask_t; +typedef __int32_t __critical_t; +typedef long double __double_t; +typedef long double __float_t; +typedef __int32_t __intfptr_t; +typedef __int64_t __intmax_t; +typedef __int32_t __intptr_t; +typedef __int32_t __int_fast8_t; +typedef __int32_t __int_fast16_t; +typedef __int32_t __int_fast32_t; +typedef __int64_t __int_fast64_t; +typedef __int8_t __int_least8_t; +typedef __int16_t __int_least16_t; +typedef __int32_t __int_least32_t; +typedef __int64_t __int_least64_t; +typedef __int32_t __ptrdiff_t; /* ptr1 - ptr2 */ +typedef __int32_t __register_t; +typedef __int32_t __segsz_t; /* segment size (in pages) */ +typedef __uint32_t __size_t; /* sizeof() */ +typedef __int32_t __ssize_t; /* byte count or error */ +typedef __int32_t __time_t; /* time()... */ +typedef __uint32_t __uintfptr_t; +typedef __uint64_t __uintmax_t; +typedef __uint32_t __uintptr_t; +typedef __uint32_t __uint_fast8_t; +typedef __uint32_t __uint_fast16_t; +typedef __uint32_t __uint_fast32_t; +typedef __uint64_t __uint_fast64_t; +typedef __uint8_t __uint_least8_t; +typedef __uint16_t __uint_least16_t; +typedef __uint32_t __uint_least32_t; +typedef __uint64_t __uint_least64_t; +typedef __uint32_t __u_register_t; + +typedef __uint32_t off_t; + + +#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) + +#define INT8_C(c) (c) +#define INT16_C(c) (c) +#define INT32_C(c) (c) +#define INT64_C(c) (c ## LL) + +#define UINT8_C(c) (c) +#define UINT16_C(c) (c) +#define UINT32_C(c) (c ## U) +#define UINT64_C(c) (c ## ULL) + +#define INTMAX_C(c) (c ## LL) +#define UINTMAX_C(c) (c ## ULL) + +#endif /* !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) */ + +#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) + +/* + * ISO/IEC 9899:1999 + * 7.18.2.1 Limits of exact-width integer types + */ +/* Minimum values of exact-width signed integer types. */ +#define INT8_MIN (-0x7f-1) +#define INT16_MIN (-0x7fff-1) +#define INT32_MIN (-0x7fffffff-1) +#define INT64_MIN (-0x7fffffffffffffffLL-1) + +/* Maximum values of exact-width signed integer types. */ +#define INT8_MAX 0x7f +#define INT16_MAX 0x7fff +#define INT32_MAX 0x7fffffff +#define INT64_MAX 0x7fffffffffffffffLL + +/* Maximum values of exact-width unsigned integer types. */ +#define UINT8_MAX 0xff +#define UINT16_MAX 0xffff +#define UINT32_MAX 0xffffffffU +#define UINT64_MAX 0xffffffffffffffffULL + +/* + * ISO/IEC 9899:1999 + * 7.18.2.2 Limits of minimum-width integer types + */ +/* Minimum values of minimum-width signed integer types. */ +#define INT_LEAST8_MIN INT8_MIN +#define INT_LEAST16_MIN INT16_MIN +#define INT_LEAST32_MIN INT32_MIN +#define INT_LEAST64_MIN INT64_MIN + +/* Maximum values of minimum-width signed integer types. */ +#define INT_LEAST8_MAX INT8_MAX +#define INT_LEAST16_MAX INT16_MAX +#define INT_LEAST32_MAX INT32_MAX +#define INT_LEAST64_MAX INT64_MAX + +/* Maximum values of minimum-width unsigned integer types. */ +#define UINT_LEAST8_MAX UINT8_MAX +#define UINT_LEAST16_MAX UINT16_MAX +#define UINT_LEAST32_MAX UINT32_MAX +#define UINT_LEAST64_MAX UINT64_MAX + +/* + * ISO/IEC 9899:1999 + * 7.18.2.3 Limits of fastest minimum-width integer types + */ +/* Minimum values of fastest minimum-width signed integer types. */ +#define INT_FAST8_MIN INT32_MIN +#define INT_FAST16_MIN INT32_MIN +#define INT_FAST32_MIN INT32_MIN +#define INT_FAST64_MIN INT64_MIN + +/* Maximum values of fastest minimum-width signed integer types. */ +#define INT_FAST8_MAX INT32_MAX +#define INT_FAST16_MAX INT32_MAX +#define INT_FAST32_MAX INT32_MAX +#define INT_FAST64_MAX INT64_MAX + +/* Maximum values of fastest minimum-width unsigned integer types. */ +#define UINT_FAST8_MAX UINT32_MAX +#define UINT_FAST16_MAX UINT32_MAX +#define UINT_FAST32_MAX UINT32_MAX +#define UINT_FAST64_MAX UINT64_MAX + +/* + * ISO/IEC 9899:1999 + * 7.18.2.4 Limits of integer types capable of holding object pointers + */ +#define INTPTR_MIN INT32_MIN +#define INTPTR_MAX INT32_MAX +#define UINTPTR_MAX UINT32_MAX + +/* + * ISO/IEC 9899:1999 + * 7.18.2.5 Limits of greatest-width integer types + */ +#define INTMAX_MIN INT64_MIN +#define INTMAX_MAX INT64_MAX +#define UINTMAX_MAX UINT64_MAX + +/* + * ISO/IEC 9899:1999 + * 7.18.3 Limits of other integer types + */ +/* Limits of ptrdiff_t. */ +#define PTRDIFF_MIN INT32_MIN +#define PTRDIFF_MAX INT32_MAX + +/* Limit of size_t. */ +#define SIZE_MAX UINT32_MAX + +#ifndef WCHAR_MIN /* Limits of wchar_t. */ +#define WCHAR_MIN INT32_MIN +#define WCHAR_MAX INT32_MAX +#endif + +/* Limits of wint_t. */ +#define WINT_MIN INT32_MIN +#define WINT_MAX INT32_MAX + +#endif /* !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) */ + + +typedef unsigned char u_char; +typedef unsigned short u_short; +typedef unsigned int u_int; +typedef unsigned long u_long; +typedef unsigned short ushort; /* Sys V compatibility */ +typedef unsigned int uint; /* Sys V compatibility */ + + +#ifndef _INT8_T_DECLARED +typedef __int8_t int8_t; +#define _INT8_T_DECLARED +#endif + +#ifndef _INT16_T_DECLARED +typedef __int16_t int16_t; +#define _INT16_T_DECLARED +#endif + +#ifndef _INT32_T_DECLARED +typedef __int32_t int32_t; +#define _INT32_T_DECLARED +#endif + +#ifndef _INT64_T_DECLARED +typedef __int64_t int64_t; +#define _INT64_T_DECLARED +#endif + +#ifndef _UINT8_T_DECLARED +typedef __uint8_t uint8_t; +#define _UINT8_T_DECLARED +#endif + +#ifndef _UINT16_T_DECLARED +typedef __uint16_t uint16_t; +#define _UINT16_T_DECLARED +#endif + +#ifndef _UINT32_T_DECLARED +typedef __uint32_t uint32_t; +#define _UINT32_T_DECLARED +#endif + +#ifndef _UINT64_T_DECLARED +typedef __uint64_t uint64_t; +#define _UINT64_T_DECLARED +#endif + +#ifndef _INTPTR_T_DECLARED +typedef __intptr_t intptr_t; +typedef __uintptr_t uintptr_t; +#define _INTPTR_T_DECLARED +#endif + +#ifndef _SIZE_T_DECLARED +typedef __size_t size_t; +#define _SIZE_T_DECLARED +#endif + +#ifndef _SSIZE_T_DECLARED +typedef __ssize_t ssize_t; +#define _SSIZE_T_DECLARED +#endif + + +typedef __int_least8_t int_least8_t; +typedef __int_least16_t int_least16_t; +typedef __int_least32_t int_least32_t; +typedef __int_least64_t int_least64_t; + +typedef __uint_least8_t uint_least8_t; +typedef __uint_least16_t uint_least16_t; +typedef __uint_least32_t uint_least32_t; +typedef __uint_least64_t uint_least64_t; + +typedef __int_fast8_t int_fast8_t; +typedef __int_fast16_t int_fast16_t; +typedef __int_fast32_t int_fast32_t; +typedef __int_fast64_t int_fast64_t; + +typedef __uint_fast8_t uint_fast8_t; +typedef __uint_fast16_t uint_fast16_t; +typedef __uint_fast32_t uint_fast32_t; +typedef __uint_fast64_t uint_fast64_t; + +typedef __intmax_t intmax_t; +typedef __uintmax_t uintmax_t; + +#ifndef _INTPTR_T_DECLARED +typedef __intptr_t intptr_t; +typedef __uintptr_t uintptr_t; +#define _INTPTR_T_DECLARED +#endif + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int64_t s64; +typedef int32_t s32; +typedef int16_t s16; +typedef int8_t s8; + +typedef __uint8_t u_int8_t; /* unsigned integrals (deprecated) */ +typedef __uint16_t u_int16_t; +typedef __uint32_t u_int32_t; +typedef __uint64_t u_int64_t; + +typedef __uint64_t u_quad_t; /* quads (deprecated) */ +typedef __int64_t quad_t; +typedef quad_t * qaddr_t; + +typedef char * caddr_t; /* core address */ +typedef const char * c_caddr_t; /* core address, pointer to const */ +typedef volatile char *v_caddr_t; /* core address, pointer to volatile */ + + + + + +#endif /* !_SYS_STDINT_H_ */ From ac9ecd538cacceee714e71490c2624f5c9043aa0 Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 3 Nov 2020 10:53:22 -0500 Subject: [PATCH 19/42] Added hmac files to the C library --- Makefile | 8 +++++--- hmac256/xmhfcrypto.h | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index b413329..499af88 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,9 @@ CLEANEXTS = o so d # Specify the source files, the target files, # and the install directory NAME = Line_Follower -SOURCES = $(NAME).c +HMACDIR=./hmac256 +SOURCES = $(NAME).c $(HMACDIR)/hmac-sha256.c $(HMACDIR)/sha256.c +CPPFLAGS+=-I$(HMACDIR) OUTPUTFILE = lib$(NAME).so INSTALLDIR = . @@ -28,13 +30,13 @@ install: .PHONY: clean clean: - for file in $(CLEANEXTS); do rm -f *.$$file; done + for file in $(CLEANEXTS); do rm -f *.$$file; rm -f $(HMACDIR)/*.$$file; done rm $(NAME)_Test # Generate dependencies of .c files on .h files include $(subst .c,.d,$(SOURCES)) %.d: %.c - $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ + $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ rm -f $@.$$$$ diff --git a/hmac256/xmhfcrypto.h b/hmac256/xmhfcrypto.h index ed80070..1a52ad2 100644 --- a/hmac256/xmhfcrypto.h +++ b/hmac256/xmhfcrypto.h @@ -34,6 +34,8 @@ #ifndef __XMHFCRYPTO_H__ #define __XMHFCRYPTO_H__ +#include +#include //tomcrypt.h From df055ab2129ccd7d5d5defc6bc7afcfd2d869b74 Mon Sep 17 00:00:00 2001 From: antonhristozov Date: Tue, 1 Dec 2020 15:40:51 +0000 Subject: [PATCH 20/42] Merged with changes from uobjcoll --- Line_Follower.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Line_Follower.c b/Line_Follower.c index 4c6f12a..931719e 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -8,6 +8,7 @@ #include //Needed for I2C port #include //Needed for I2C port #include "Line_Follower.h" +#include "xmhfcrypto.h" /* Globals */ #define RAW_LEN (2*NUM_REF) @@ -15,11 +16,18 @@ int references[NUM_REF] = {300,300,300,300,300}; const int SLAVE_ADDRESS = 0x11; int bus = 1; +__attribute__((section(".data"))) unsigned char uhsign_key[]="super_secret_key_for_hmac"; +#define UHSIGN_KEY_SIZE (sizeof(uhsign_key)) +#define HMAC_DIGEST_SIZE 32 int read_i2c(char *buffer,int length){ int file_i2c; int bytes_read = 0; + int ret_length; + unsigned long digest_size = HMAC_DIGEST_SIZE; + unsigned char digest_result[HMAC_DIGEST_SIZE]; + //----- OPEN THE I2C BUS ----- char *filename = (char*)"/dev/i2c-1"; printf("Line_Follower.c :: read_i2c() called\n"); @@ -38,10 +46,20 @@ int read_i2c(char *buffer,int length){ return 0; } - + ret_length = read(file_i2c, buffer, length); //----- READ BYTES ----- if ((bytes_read=read(file_i2c, buffer, length)) != length) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) + if (ret_length != length) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) { + if(ret_length == length + HMAC_DIGEST_SIZE){ + // Calculate the HMAC + if(hmac_sha256_memory(uhsign_key, (unsigned long) UHSIGN_KEY_SIZE, (unsigned char *) buffer, (unsigned long) length, digest_result, &digest_size)==CRYPT_OK) { + if(memcmp(buffer+length,digest_result,digest_size) != 0){ + printf("HMAC digest does not match \n"); + } + } + + } //ERROR HANDLING: i2c transaction failed printf("Failed to read from the i2c bus.\n"); } From ce1fb0651b873f0a685271f18b145293b4147d56 Mon Sep 17 00:00:00 2001 From: antonhristozov Date: Wed, 2 Dec 2020 00:22:30 +0000 Subject: [PATCH 21/42] Working line following functionality with C library calls --- Line_Follower.c | 22 +++++++--------------- hmac256/stdint.h | 8 ++++---- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 931719e..6235c85 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -12,7 +12,7 @@ /* Globals */ #define RAW_LEN (2*NUM_REF) -int references[NUM_REF] = {300,300,300,300,300}; +int references[NUM_REF] = {200,200,200,200,200}; const int SLAVE_ADDRESS = 0x11; int bus = 1; @@ -23,14 +23,13 @@ __attribute__((section(".data"))) unsigned char uhsign_key[]="super_secret_key_f int read_i2c(char *buffer,int length){ int file_i2c; - int bytes_read = 0; - int ret_length; + int ret_length=0; + int i; unsigned long digest_size = HMAC_DIGEST_SIZE; unsigned char digest_result[HMAC_DIGEST_SIZE]; //----- OPEN THE I2C BUS ----- char *filename = (char*)"/dev/i2c-1"; - printf("Line_Follower.c :: read_i2c() called\n"); if ((file_i2c = open(filename, O_RDWR)) < 0) { //ERROR HANDLING: you can check errno to see what went wrong @@ -45,10 +44,9 @@ int read_i2c(char *buffer,int length){ //ERROR HANDLING; you can check errno to see what went wrong return 0; } - ret_length = read(file_i2c, buffer, length); //----- READ BYTES ----- - if ((bytes_read=read(file_i2c, buffer, length)) != length) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) + //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) if (ret_length != length) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) { if(ret_length == length + HMAC_DIGEST_SIZE){ @@ -63,19 +61,14 @@ int read_i2c(char *buffer,int length){ //ERROR HANDLING: i2c transaction failed printf("Failed to read from the i2c bus.\n"); } - else - { - printf("Line_Follower.c :: read_i2c() :: Data read: %s\n", buffer); - } close(file_i2c); - return bytes_read; + return ret_length; } char * read_raw(){ int flag = 0; static char raw_result[RAW_LEN+1]; int i; - printf("Line_Follower.c :: read_raw() called\n"); for(i=0;i Date: Wed, 2 Dec 2020 04:49:15 +0000 Subject: [PATCH 22/42] HMAC debugging messages added --- Line_Follower.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 6235c85..1d2273f 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -47,19 +47,28 @@ int read_i2c(char *buffer,int length){ ret_length = read(file_i2c, buffer, length); //----- READ BYTES ----- //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) - if (ret_length != length) //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) - { + if (ret_length != length){ if(ret_length == length + HMAC_DIGEST_SIZE){ // Calculate the HMAC if(hmac_sha256_memory(uhsign_key, (unsigned long) UHSIGN_KEY_SIZE, (unsigned char *) buffer, (unsigned long) length, digest_result, &digest_size)==CRYPT_OK) { - if(memcmp(buffer+length,digest_result,digest_size) != 0){ - printf("HMAC digest does not match \n"); - } - } - + if(memcmp(buffer+length,digest_result,digest_size) != 0){ + printf("HMAC digest did not match with driver's digest \n"); + printf("Bytes returned: "); + for(i=0;i Date: Mon, 4 Jan 2021 16:42:13 +0000 Subject: [PATCH 23/42] Extended unit test --- Line_Follower_Test.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Line_Follower_Test.c b/Line_Follower_Test.c index bf9738e..e25fce7 100644 --- a/Line_Follower_Test.c +++ b/Line_Follower_Test.c @@ -7,9 +7,14 @@ int main(){ printf("Line Follower C Test Application Running\n"); int *dig_list; int i; - dig_list = read_digital(); - for(i=0;i Date: Mon, 4 Jan 2021 16:57:56 +0000 Subject: [PATCH 24/42] Line follower Unit Test --- Line_Follower_Test.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Line_Follower_Test.py b/Line_Follower_Test.py index 15b9d47..173db74 100644 --- a/Line_Follower_Test.py +++ b/Line_Follower_Test.py @@ -2,6 +2,7 @@ import ctypes import pathlib import os +import time if __name__ == "__main__": print("Line Follower Python Test Application Running") @@ -9,10 +10,9 @@ libname = os.path.abspath(".") + "/" + "libLine_Follower.so"; print(libname) c_lib = ctypes.CDLL(libname) - dt_list = c_lib.read_digital() - ptr = ctypes.cast(dt_list,ctypes.POINTER(ctypes.c_int)) - print(ptr[0]) - print(ptr[1]) - print(ptr[2]) - print(ptr[3]) - print(ptr[4]) + while True: + dt_list = c_lib.read_digital() + ptr = ctypes.cast(dt_list,ctypes.POINTER(ctypes.c_int)) + print(hex(id(ptr))) + print(ptr[0], ptr[1], ptr[2], ptr[3], ptr[4]) + time.sleep(1) From 966bfc22752dff12f8f9f47e9934e3d0e953178e Mon Sep 17 00:00:00 2001 From: antonhristozov Date: Mon, 4 Jan 2021 17:10:02 +0000 Subject: [PATCH 25/42] Added printing of address --- Line_Follower.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Line_Follower.c b/Line_Follower.c index 1d2273f..fb8771b 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -63,6 +63,9 @@ int read_i2c(char *buffer,int length){ } printf("\n"); } + // else{ + // printf("HMAC digest match\n"); + // } } } else{ @@ -141,6 +144,7 @@ int * read_digital(){ } printf("\n"); } + printf("read_digital() :: digital_list address : %p \n",digital_list); return digital_list; } From 01c8213f04ae551bb5cce6a698c37d907c3e4eb2 Mon Sep 17 00:00:00 2001 From: antonhristozov Date: Mon, 4 Jan 2021 19:15:54 +0000 Subject: [PATCH 26/42] Aadded dynamic array on the heap --- Line_Follower.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Line_Follower.c b/Line_Follower.c index fb8771b..7b2a4c4 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -3,6 +3,7 @@ #include #include #include +#include #include //Needed for I2C port #include //Needed for I2C port #include //Needed for I2C port @@ -125,10 +126,18 @@ int * read_analog(int trys){ return NULL; } +static int *dynamic_list = NULL; + int * read_digital(){ int * lt; int i; static int digital_list[NUM_REF] = {0}; + static int first_time = 0; + if(first_time == 0){ + dynamic_list = (int *)malloc(NUM_REF*sizeof(int)); + first_time++; + printf("read_digital() :: dynamic list %p \n",dynamic_list); + } lt = read_analog(NUM_REF); if(lt != NULL){ for(i=0;i Date: Mon, 4 Jan 2021 22:03:13 +0000 Subject: [PATCH 27/42] Added minor changes to be able to intervene with memory attack --- Line_Follower.c | 9 ++++++--- Line_Follower_Test.c | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 7b2a4c4..3fc76f8 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -136,7 +136,7 @@ int * read_digital(){ if(first_time == 0){ dynamic_list = (int *)malloc(NUM_REF*sizeof(int)); first_time++; - printf("read_digital() :: dynamic list %p \n",dynamic_list); + //printf("read_digital() :: dynamic list %p \n",dynamic_list); } lt = read_analog(NUM_REF); if(lt != NULL){ @@ -154,8 +154,11 @@ int * read_digital(){ } printf("\n"); } - printf("read_digital() :: digital_list address : %p \n",digital_list); - printf("read_digital() :: dynamic_list %p \n",dynamic_list); + //printf("read_digital() :: digital_list address : %p \n",digital_list); + //printf("read_digital() :: dynamic_list %p \n",dynamic_list); + // Sleep for 10ms so that an attack can succeed in overwriting bytes in buffer + // Car still runs fine with this delay + usleep(10000); return dynamic_list; } diff --git a/Line_Follower_Test.c b/Line_Follower_Test.c index e25fce7..2ef14c2 100644 --- a/Line_Follower_Test.c +++ b/Line_Follower_Test.c @@ -15,6 +15,9 @@ int main(){ } printf("\n"); sleep(1); + for(i=0;i Date: Mon, 4 Jan 2021 23:34:06 +0000 Subject: [PATCH 28/42] Need the print statement in ordert to print the address --- Line_Follower.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Line_Follower.c b/Line_Follower.c index 3fc76f8..4f8aa64 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -155,7 +155,7 @@ int * read_digital(){ printf("\n"); } //printf("read_digital() :: digital_list address : %p \n",digital_list); - //printf("read_digital() :: dynamic_list %p \n",dynamic_list); + printf("read_digital() :: dynamic_list %p \n",dynamic_list); // Sleep for 10ms so that an attack can succeed in overwriting bytes in buffer // Car still runs fine with this delay usleep(10000); From 911d922f8f25db65b5387e7f67bf99066fb489e0 Mon Sep 17 00:00:00 2001 From: antonhristozov Date: Tue, 5 Jan 2021 17:48:14 +0000 Subject: [PATCH 29/42] Removed heap related variables. Back to static buffers. --- Line_Follower.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 4f8aa64..ed6e7c5 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -126,18 +126,11 @@ int * read_analog(int trys){ return NULL; } -static int *dynamic_list = NULL; +__attribute__ ((section("i2c_section"))) static int digital_list[NUM_REF] = {0}; int * read_digital(){ int * lt; int i; - static int digital_list[NUM_REF] = {0}; - static int first_time = 0; - if(first_time == 0){ - dynamic_list = (int *)malloc(NUM_REF*sizeof(int)); - first_time++; - //printf("read_digital() :: dynamic list %p \n",dynamic_list); - } lt = read_analog(NUM_REF); if(lt != NULL){ for(i=0;i Date: Tue, 19 Jan 2021 14:38:26 +0000 Subject: [PATCH 30/42] Remove a print statement --- Line_Follower.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Line_Follower.c b/Line_Follower.c index ed6e7c5..9914adc 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -146,7 +146,7 @@ int * read_digital(){ } printf("\n"); } - printf("read_digital() :: digital_list address : %p \n",digital_list); + //printf("read_digital() :: digital_list address : %p \n",digital_list); // Sleep for 10ms so that an attack can succeed in overwriting bytes in buffer // Car still runs fine with this delay usleep(10000); From 3aa12f74ecdae354f900efa9ca6faabad4f862fc Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 19 Jan 2021 17:50:14 -0500 Subject: [PATCH 31/42] Added cross compilation of this library --- Makefile | 3 +++ Makefile.linux | 45 +++++++++++++++++++++++++++++++++++++++++++++ Makefile.rpi | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 Makefile.linux create mode 100644 Makefile.rpi diff --git a/Makefile b/Makefile index 499af88..2ed6757 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,9 @@ SOURCES = $(NAME).c $(HMACDIR)/hmac-sha256.c $(HMACDIR)/sha256.c CPPFLAGS+=-I$(HMACDIR) OUTPUTFILE = lib$(NAME).so INSTALLDIR = . +CC=arm-linux-gnueabihf-cc +LD=arm-linux-gnueabihf-ld +CXX=arm-linux-gnueabihf-g++ .PHONY: all all: $(OUTPUTFILE) $(NAME)_Test diff --git a/Makefile.linux b/Makefile.linux new file mode 100644 index 0000000..2ed6757 --- /dev/null +++ b/Makefile.linux @@ -0,0 +1,45 @@ +# Specify extensions of files to delete when cleaning +CLEANEXTS = o so d + +# Specify the source files, the target files, +# and the install directory +NAME = Line_Follower +HMACDIR=./hmac256 +SOURCES = $(NAME).c $(HMACDIR)/hmac-sha256.c $(HMACDIR)/sha256.c +CPPFLAGS+=-I$(HMACDIR) +OUTPUTFILE = lib$(NAME).so +INSTALLDIR = . +CC=arm-linux-gnueabihf-cc +LD=arm-linux-gnueabihf-ld +CXX=arm-linux-gnueabihf-g++ + +.PHONY: all +all: $(OUTPUTFILE) $(NAME)_Test + +# Build .so from .o, subst is the search-and-replace +$(OUTPUTFILE): $(subst .c,.o,$(SOURCES)) + $(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^ + +test: $(NAME)_Test + LD_LIBRARY_PATH=. ./$(NAME)_Test + +$(NAME)_Test: lib$(NAME).so $(NAME)_Test.c + $(CC) $(NAME)_Test.c -o $@ -L. -l$(NAME) + +.PHONY: install +install: + mkdir -p $(INSTALLDIR) + cp -p $(OUTPUTFILE) $(INSTALLDIR) + +.PHONY: clean +clean: + for file in $(CLEANEXTS); do rm -f *.$$file; rm -f $(HMACDIR)/*.$$file; done + rm $(NAME)_Test + +# Generate dependencies of .c files on .h files +include $(subst .c,.d,$(SOURCES)) + +%.d: %.c + $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ + sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ +rm -f $@.$$$$ diff --git a/Makefile.rpi b/Makefile.rpi new file mode 100644 index 0000000..499af88 --- /dev/null +++ b/Makefile.rpi @@ -0,0 +1,42 @@ +# Specify extensions of files to delete when cleaning +CLEANEXTS = o so d + +# Specify the source files, the target files, +# and the install directory +NAME = Line_Follower +HMACDIR=./hmac256 +SOURCES = $(NAME).c $(HMACDIR)/hmac-sha256.c $(HMACDIR)/sha256.c +CPPFLAGS+=-I$(HMACDIR) +OUTPUTFILE = lib$(NAME).so +INSTALLDIR = . + +.PHONY: all +all: $(OUTPUTFILE) $(NAME)_Test + +# Build .so from .o, subst is the search-and-replace +$(OUTPUTFILE): $(subst .c,.o,$(SOURCES)) + $(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^ + +test: $(NAME)_Test + LD_LIBRARY_PATH=. ./$(NAME)_Test + +$(NAME)_Test: lib$(NAME).so $(NAME)_Test.c + $(CC) $(NAME)_Test.c -o $@ -L. -l$(NAME) + +.PHONY: install +install: + mkdir -p $(INSTALLDIR) + cp -p $(OUTPUTFILE) $(INSTALLDIR) + +.PHONY: clean +clean: + for file in $(CLEANEXTS); do rm -f *.$$file; rm -f $(HMACDIR)/*.$$file; done + rm $(NAME)_Test + +# Generate dependencies of .c files on .h files +include $(subst .c,.d,$(SOURCES)) + +%.d: %.c + $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \ + sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ +rm -f $@.$$$$ From d2751d0560f03871cf43814b1482ed3d646db49c Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 19 Jan 2021 22:07:38 -0500 Subject: [PATCH 32/42] Added a flag in Makefile --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 2ed6757..3d2c8e7 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ NAME = Line_Follower HMACDIR=./hmac256 SOURCES = $(NAME).c $(HMACDIR)/hmac-sha256.c $(HMACDIR)/sha256.c CPPFLAGS+=-I$(HMACDIR) +CPPFLAGS+=-DUOBJCOLL OUTPUTFILE = lib$(NAME).so INSTALLDIR = . CC=arm-linux-gnueabihf-cc From c4fb06e0da50ade4b7f0745db4918322f37ee454 Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Wed, 20 Jan 2021 08:35:07 -0500 Subject: [PATCH 33/42] Adding uhcall dependencies --- Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3d2c8e7..e2ecaae 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,9 @@ NAME = Line_Follower HMACDIR=./hmac256 SOURCES = $(NAME).c $(HMACDIR)/hmac-sha256.c $(HMACDIR)/sha256.c CPPFLAGS+=-I$(HMACDIR) +CPPFLAGS+=-I../uberxmhf/uxmhf-rpi3/uobjcoll/main/include CPPFLAGS+=-DUOBJCOLL +LDFLAGS+=-L../uberxmhf/uxmhf-rpi3/rgapps/linux/libs/libuhcall OUTPUTFILE = lib$(NAME).so INSTALLDIR = . CC=arm-linux-gnueabihf-cc @@ -25,7 +27,7 @@ test: $(NAME)_Test LD_LIBRARY_PATH=. ./$(NAME)_Test $(NAME)_Test: lib$(NAME).so $(NAME)_Test.c - $(CC) $(NAME)_Test.c -o $@ -L. -l$(NAME) + $(CC) $(NAME)_Test.c -o $@ -L. $(LDFLAGS) -l$(NAME) -luhcall .PHONY: install install: From 5d576dfb89508b70e9fc6ff80b9a182f9c8dd36f Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Wed, 20 Jan 2021 09:34:56 -0500 Subject: [PATCH 34/42] Added all code for HMAC calculation --- Line_Follower.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 9914adc..fdf4a30 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -10,6 +10,7 @@ #include //Needed for I2C port #include "Line_Follower.h" #include "xmhfcrypto.h" +#include "picar-s.h" /* Globals */ #define RAW_LEN (2*NUM_REF) @@ -17,7 +18,7 @@ int references[NUM_REF] = {200,200,200,200,200}; const int SLAVE_ADDRESS = 0x11; int bus = 1; -__attribute__((section(".data"))) unsigned char uhsign_key[]="super_secret_key_for_hmac"; +__attribute__((section("i2c_section"))) unsigned char uhsign_key[]="super_secret_key_for_hmac"; #define UHSIGN_KEY_SIZE (sizeof(uhsign_key)) #define HMAC_DIGEST_SIZE 32 @@ -50,6 +51,43 @@ int read_i2c(char *buffer,int length){ //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) if (ret_length != length){ if(ret_length == length + HMAC_DIGEST_SIZE){ +#ifdef UOBJCOLL + picar_s_param_t *ptr_upicar; + int i; + if (posix_memalign((void **)&ptr_upicar, 4096, sizeof(picar_s_param_t)) != 0){ + printf("%s: error: line %u\n", __FUNCTION__); + exit(1); + } + for(i=0;iin[i] = buffer[i]; + } + ptr_upicar->len = length; + // Perform an uobject call + if(!uhcall(UAPP_PICAR_S_FUNCTION_TEST, ptr_upicar, sizeof(picar_s_param_t))) + printf("hypercall FAILED\n"); + else{ + printf("hypercall SUCCESS\n"); + for(i=0;iout[i]; + } + digest_size = HMAC_DIGEST_SIZE; + } + free(ptr_upicar); + if(memcmp(buffer+length,digest_result,digest_size) != 0){ + printf("HMAC digest did not match with driver's digest \n"); + printf("Bytes returned: "); + for(i=0;i Date: Wed, 20 Jan 2021 12:17:20 -0500 Subject: [PATCH 35/42] Linked static library to the shared library --- Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index e2ecaae..3af57f3 100644 --- a/Makefile +++ b/Makefile @@ -21,13 +21,12 @@ all: $(OUTPUTFILE) $(NAME)_Test # Build .so from .o, subst is the search-and-replace $(OUTPUTFILE): $(subst .c,.o,$(SOURCES)) - $(CXX) -shared -fPIC $(LDFLAGS) -o $@ $^ - + $(CXX) -shared -Wl,--whole-archive $(LDFLAGS) -luhcall -Wl,--no-whole-archive -fPIC -o $@ $^ test: $(NAME)_Test LD_LIBRARY_PATH=. ./$(NAME)_Test $(NAME)_Test: lib$(NAME).so $(NAME)_Test.c - $(CC) $(NAME)_Test.c -o $@ -L. $(LDFLAGS) -l$(NAME) -luhcall + $(CC) $(NAME)_Test.c -o $@ -L. $(LDFLAGS) -l$(NAME) .PHONY: install install: From cd704385740e3dfe3df65130db3abdcedfcbe8e1 Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Thu, 21 Jan 2021 12:51:58 -0500 Subject: [PATCH 36/42] Brought uhcall.c to link in the .so library --- Line_Follower.c | 21 +++--- Makefile | 5 +- uhcall.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 uhcall.c diff --git a/Line_Follower.c b/Line_Follower.c index fdf4a30..4557c57 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -22,6 +22,8 @@ __attribute__((section("i2c_section"))) unsigned char uhsign_key[]="super_secret #define UHSIGN_KEY_SIZE (sizeof(uhsign_key)) #define HMAC_DIGEST_SIZE 32 +__attribute__((section(".palign_data"))) __attribute__((aligned(4096))) picar_s_param_t upicar; + int read_i2c(char *buffer,int length){ int file_i2c; @@ -47,17 +49,18 @@ int read_i2c(char *buffer,int length){ return 0; } ret_length = read(file_i2c, buffer, length); + //----- READ BYTES ----- //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) if (ret_length != length){ if(ret_length == length + HMAC_DIGEST_SIZE){ #ifdef UOBJCOLL - picar_s_param_t *ptr_upicar; + picar_s_param_t *ptr_upicar = &upicar; int i; - if (posix_memalign((void **)&ptr_upicar, 4096, sizeof(picar_s_param_t)) != 0){ - printf("%s: error: line %u\n", __FUNCTION__); - exit(1); - } + // if (posix_memalign((void **)&ptr_upicar, 4096, sizeof(picar_s_param_t)) != 0){ + // printf("%s: error: line %u\n", __FUNCTION__); + // exit(1); + //} for(i=0;iin[i] = buffer[i]; } @@ -72,16 +75,16 @@ int read_i2c(char *buffer,int length){ } digest_size = HMAC_DIGEST_SIZE; } - free(ptr_upicar); + //free(ptr_upicar); if(memcmp(buffer+length,digest_result,digest_size) != 0){ printf("HMAC digest did not match with driver's digest \n"); printf("Bytes returned: "); - for(i=0;i +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +////// +// va_to_pa: virtual to physical address mapping +// return true on success, false on error +////// +bool uhcall_va2pa(void *vaddr, uint64_t *paddr) { + FILE *pagemap; + uint32_t offset; + uint64_t page_frame_number = 0; + + //sanity check incoming parameters + if (paddr == NULL) + return false; + + // open the pagemap file for the current process + pagemap = fopen("/proc/self/pagemap", "rb"); + if(pagemap == NULL) + return false; //unable to open pagemap file + + // seek to the page that vaddr is on + offset = ((uint32_t)vaddr / getpagesize()) * UHCALL_PM_LENGTH; + if(fseek(pagemap, (uint32_t)offset, SEEK_SET) != 0) + return false; //Failed to seek pagemap to proper location + + // The page frame number is in bits 0-54 so read the + // 8 bytes and clear evrything but 0-54 + fread(&page_frame_number, UHCALL_PM_LENGTH, 1, pagemap); + + if (page_frame_number & (1ULL << 63)) { // page present ? + *paddr = page_frame_number & ((1ULL << 54) - 1); // pfn mask + *paddr = *paddr * getpagesize(); + fclose(pagemap); + return true; + }else{ + fclose(pagemap); + return false; + } + +#if 0 + page_frame_number &= 0x7FFFFFFFFFFFFFULL; + + fclose(pagemap); + + *paddr = (page_frame_number << UHCALL_PM_PAGE_SHIFT); + return true; +#endif +} + + +////// +// uhcall micro-hypervisor hypercall interface +// return true on success, false on error +////// +bool uhcall(uint32_t uhcall_function, void *uhcall_buffer, uint32_t uhcall_buffer_len){ + int ret, fd; + uhcallkmod_param_t uhcallp; + uint64_t uhcall_buffer_paddr; + + //if uhcall_buffer is NULL then uhcall_buffer_len should be 0 + //for a NULL hypercall test + if(uhcall_buffer == NULL && uhcall_buffer_len != 0){ + printf("%s: error: line %u\n", __FUNCTION__, __LINE__); + return false; + } + + //if uhcall_buffer is not NULL then base address of uhcall_buffer + uhcall_buffer_len + //cannot exceed a page size + if(uhcall_buffer != NULL){ + if ( (((uint32_t)uhcall_buffer % UHCALL_PM_PAGE_SIZE) + uhcall_buffer_len) > UHCALL_PM_PAGE_SIZE ){ + printf("%s: error: line %u\n", __FUNCTION__, __LINE__); + return false; + } + } + +#if 0 + //get buffer physical address + if(!uhcall_va2pa(uhcall_buffer, &uhcall_buffer_paddr) ){ + printf("%s: error: line %u\n", __FUNCTION__, __LINE__); + return false; + } + + + //printf("%s: uhcall_buffer_paddr=0x%08x\n", __FUNCTION__, (uint32_t)uhcall_buffer_paddr); +#endif + + //open uhcallkmod device + fd = open("/dev/uhcallkmod", O_RDWR); + if (fd < 0){ + printf("%s: error: line %u\n", __FUNCTION__, __LINE__); + return false; //Failed to open /dev/uhcallkmod + } + + +#if 1 + //lock uhcall_buffer in memory + if(mlock(uhcall_buffer, uhcall_buffer_len) == -1){ + //if(mlock(uhcall_buffer, 4096) == -1){ + printf("%s: error: line %u\n", __FUNCTION__, __LINE__); + return false; //nFailed to lock page in memory + } +#endif + //populate uhcallkmod_param_t + uhcallp.uhcall_function=uhcall_function; + //uhcallp.uhcall_buffer=(uint32_t)uhcall_buffer; + // uhcallp.uhcall_buffer=(void *)(uint32_t)uhcall_buffer_paddr; + uhcallp.uhcall_buffer=(void *)(uint32_t)uhcall_buffer; + uhcallp.uhcall_buffer_len=uhcall_buffer_len; + + //printf("%s: uhcall_function=%u, uhcall_buffer=0x%08x, uhcall_buffer_len=%u\n", __FUNCTION__, + // uhcallp.uhcall_function, uhcallp.uhcall_buffer, uhcallp.uhcall_buffer_len); + + //issue the hypercall + ret = write(fd, &uhcallp, sizeof(uhcallp)); + if (ret < 0){ + printf("%s: error: line %u\n", __FUNCTION__, __LINE__); + return false; //error in issuing hypercall + } + +#if 1 + //unlock uhcall_buffer page + if(munlock(uhcall_buffer, uhcall_buffer_len) == -1){ + //if(munlock(uhcall_buffer, 4096) == -1){ + printf("%s: error: line %u\n", __FUNCTION__, __LINE__); + return false; //Failed to unlock page in memory + } +#endif + if ( close(fd) < 0 ){ + printf("%s: error: line %u\n", __FUNCTION__, __LINE__); + return false; //error in closing uhcallkmod device + } + + //hypercall succeeded + return true; +} From 933a22c565af84ff9b9a3dca2251ca3e5940013a Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Thu, 21 Jan 2021 20:46:51 -0500 Subject: [PATCH 37/42] Removed local uhcall.c file --- Makefile | 4 +- uhcall.c | 186 ------------------------------------------------------- 2 files changed, 2 insertions(+), 188 deletions(-) delete mode 100644 uhcall.c diff --git a/Makefile b/Makefile index 2b6af93..41c395b 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CLEANEXTS = o so d # and the install directory NAME = Line_Follower HMACDIR=./hmac256 -SOURCES = $(NAME).c uhcall.c $(HMACDIR)/hmac-sha256.c $(HMACDIR)/sha256.c +SOURCES = $(NAME).c $(HMACDIR)/hmac-sha256.c $(HMACDIR)/sha256.c CPPFLAGS+=-I$(HMACDIR) CPPFLAGS+=-I../uberxmhf/uxmhf-rpi3/uobjcoll/main/include CPPFLAGS+=-I../uberxmhf/uxmhf-rpi3/rgapps/linux/libs/libuhcall/include @@ -22,7 +22,7 @@ all: $(OUTPUTFILE) $(NAME)_Test # Build .so from .o, subst is the search-and-replace $(OUTPUTFILE): $(subst .c,.o,$(SOURCES)) - $(CXX) -shared -fPIC -o $@ $^ + $(CXX) -shared -fPIC $(LDFLAGS) -Wl,--whole-archive -luhcall -Wl,--no-whole-archive -o $@ $^ test: $(NAME)_Test LD_LIBRARY_PATH=. ./$(NAME)_Test diff --git a/uhcall.c b/uhcall.c deleted file mode 100644 index b75c144..0000000 --- a/uhcall.c +++ /dev/null @@ -1,186 +0,0 @@ -/* - * @UBERXMHF_LICENSE_HEADER_START@ - * - * uber eXtensible Micro-Hypervisor Framework (Raspberry Pi) - * - * Copyright 2018 Carnegie Mellon University. All Rights Reserved. - * - * NO WARRANTY. THIS CARNEGIE MELLON UNIVERSITY AND SOFTWARE ENGINEERING - * INSTITUTE MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON - * UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, - * AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR - * PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF - * THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF - * ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT - * INFRINGEMENT. - * - * Released under a BSD (SEI)-style license, please see LICENSE or - * contact permission@sei.cmu.edu for full terms. - * - * [DISTRIBUTION STATEMENT A] This material has been approved for public - * release and unlimited distribution. Please see Copyright notice for - * non-US Government use and distribution. - * - * Carnegie Mellon is registered in the U.S. Patent and Trademark Office by - * Carnegie Mellon University. - * - * @UBERXMHF_LICENSE_HEADER_END@ - */ - -/* - * Author: Amit Vasudevan (amitvasudevan@acm.org) - * - */ - -/* - * uhcall -- guest interface for micro hypervisor hypercall - * - * author: amit vasudevan (amitvasudevan@acm.org) - */ - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include - -////// -// va_to_pa: virtual to physical address mapping -// return true on success, false on error -////// -bool uhcall_va2pa(void *vaddr, uint64_t *paddr) { - FILE *pagemap; - uint32_t offset; - uint64_t page_frame_number = 0; - - //sanity check incoming parameters - if (paddr == NULL) - return false; - - // open the pagemap file for the current process - pagemap = fopen("/proc/self/pagemap", "rb"); - if(pagemap == NULL) - return false; //unable to open pagemap file - - // seek to the page that vaddr is on - offset = ((uint32_t)vaddr / getpagesize()) * UHCALL_PM_LENGTH; - if(fseek(pagemap, (uint32_t)offset, SEEK_SET) != 0) - return false; //Failed to seek pagemap to proper location - - // The page frame number is in bits 0-54 so read the - // 8 bytes and clear evrything but 0-54 - fread(&page_frame_number, UHCALL_PM_LENGTH, 1, pagemap); - - if (page_frame_number & (1ULL << 63)) { // page present ? - *paddr = page_frame_number & ((1ULL << 54) - 1); // pfn mask - *paddr = *paddr * getpagesize(); - fclose(pagemap); - return true; - }else{ - fclose(pagemap); - return false; - } - -#if 0 - page_frame_number &= 0x7FFFFFFFFFFFFFULL; - - fclose(pagemap); - - *paddr = (page_frame_number << UHCALL_PM_PAGE_SHIFT); - return true; -#endif -} - - -////// -// uhcall micro-hypervisor hypercall interface -// return true on success, false on error -////// -bool uhcall(uint32_t uhcall_function, void *uhcall_buffer, uint32_t uhcall_buffer_len){ - int ret, fd; - uhcallkmod_param_t uhcallp; - uint64_t uhcall_buffer_paddr; - - //if uhcall_buffer is NULL then uhcall_buffer_len should be 0 - //for a NULL hypercall test - if(uhcall_buffer == NULL && uhcall_buffer_len != 0){ - printf("%s: error: line %u\n", __FUNCTION__, __LINE__); - return false; - } - - //if uhcall_buffer is not NULL then base address of uhcall_buffer + uhcall_buffer_len - //cannot exceed a page size - if(uhcall_buffer != NULL){ - if ( (((uint32_t)uhcall_buffer % UHCALL_PM_PAGE_SIZE) + uhcall_buffer_len) > UHCALL_PM_PAGE_SIZE ){ - printf("%s: error: line %u\n", __FUNCTION__, __LINE__); - return false; - } - } - -#if 0 - //get buffer physical address - if(!uhcall_va2pa(uhcall_buffer, &uhcall_buffer_paddr) ){ - printf("%s: error: line %u\n", __FUNCTION__, __LINE__); - return false; - } - - - //printf("%s: uhcall_buffer_paddr=0x%08x\n", __FUNCTION__, (uint32_t)uhcall_buffer_paddr); -#endif - - //open uhcallkmod device - fd = open("/dev/uhcallkmod", O_RDWR); - if (fd < 0){ - printf("%s: error: line %u\n", __FUNCTION__, __LINE__); - return false; //Failed to open /dev/uhcallkmod - } - - -#if 1 - //lock uhcall_buffer in memory - if(mlock(uhcall_buffer, uhcall_buffer_len) == -1){ - //if(mlock(uhcall_buffer, 4096) == -1){ - printf("%s: error: line %u\n", __FUNCTION__, __LINE__); - return false; //nFailed to lock page in memory - } -#endif - //populate uhcallkmod_param_t - uhcallp.uhcall_function=uhcall_function; - //uhcallp.uhcall_buffer=(uint32_t)uhcall_buffer; - // uhcallp.uhcall_buffer=(void *)(uint32_t)uhcall_buffer_paddr; - uhcallp.uhcall_buffer=(void *)(uint32_t)uhcall_buffer; - uhcallp.uhcall_buffer_len=uhcall_buffer_len; - - //printf("%s: uhcall_function=%u, uhcall_buffer=0x%08x, uhcall_buffer_len=%u\n", __FUNCTION__, - // uhcallp.uhcall_function, uhcallp.uhcall_buffer, uhcallp.uhcall_buffer_len); - - //issue the hypercall - ret = write(fd, &uhcallp, sizeof(uhcallp)); - if (ret < 0){ - printf("%s: error: line %u\n", __FUNCTION__, __LINE__); - return false; //error in issuing hypercall - } - -#if 1 - //unlock uhcall_buffer page - if(munlock(uhcall_buffer, uhcall_buffer_len) == -1){ - //if(munlock(uhcall_buffer, 4096) == -1){ - printf("%s: error: line %u\n", __FUNCTION__, __LINE__); - return false; //Failed to unlock page in memory - } -#endif - if ( close(fd) < 0 ){ - printf("%s: error: line %u\n", __FUNCTION__, __LINE__); - return false; //error in closing uhcallkmod device - } - - //hypercall succeeded - return true; -} From 51b02c58230ec02024b80c255bafc6686ab65b3a Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Mon, 25 Jan 2021 14:53:48 -0500 Subject: [PATCH 38/42] Now attack is goinf to be on raw_buffer --- Line_Follower.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 4557c57..b30e65a 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -120,11 +120,12 @@ int read_i2c(char *buffer,int length){ return ret_length; } -__attribute__((section("i2c_section_2"))) static char raw_result[RAW_LEN+1]; +__attribute__((aligned(4096))) __attribute__((section("i2c_section_2"))) static char raw_result[4096]; char * read_raw(){ int flag = 0; int i; + printf("read_raw() :: raw_result address : %p \n",raw_result); for(i=0;i Date: Tue, 26 Jan 2021 18:15:40 -0500 Subject: [PATCH 39/42] Changes to implement two buffers --- Line_Follower.c | 84 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 16 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index b30e65a..72c2eb1 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -23,7 +23,8 @@ __attribute__((section("i2c_section"))) unsigned char uhsign_key[]="super_secret #define HMAC_DIGEST_SIZE 32 __attribute__((section(".palign_data"))) __attribute__((aligned(4096))) picar_s_param_t upicar; - +__attribute__((aligned(4096))) __attribute__((section("i2c_section_2"))) static char encrypted_buffer[4096]; +__attribute__((aligned(4096))) __attribute__((section("i2c_section_3"))) static char decrypted_buffer[4096]; int read_i2c(char *buffer,int length){ int file_i2c; @@ -32,6 +33,8 @@ int read_i2c(char *buffer,int length){ unsigned long digest_size = HMAC_DIGEST_SIZE; unsigned char digest_result[HMAC_DIGEST_SIZE]; + printf("read_i2c() :: decrypted_buffer address : %p \n",decrypted_buffer); + //----- OPEN THE I2C BUS ----- char *filename = (char*)"/dev/i2c-1"; if ((file_i2c = open(filename, O_RDWR)) < 0) @@ -49,6 +52,12 @@ int read_i2c(char *buffer,int length){ return 0; } ret_length = read(file_i2c, buffer, length); + if(ret_length == 0){ + printf("i2c read() returned 0 bytes\n"); + close(file_i2c); + return ret_length; + } + //----- READ BYTES ----- //read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device) @@ -57,25 +66,17 @@ int read_i2c(char *buffer,int length){ #ifdef UOBJCOLL picar_s_param_t *ptr_upicar = &upicar; int i; - // if (posix_memalign((void **)&ptr_upicar, 4096, sizeof(picar_s_param_t)) != 0){ - // printf("%s: error: line %u\n", __FUNCTION__); - // exit(1); - //} - for(i=0;iin[i] = buffer[i]; - } + ptr_upicar->encrypted_buffer_va = (uint32_t) encrypted_buffer; + ptr_upicar->decrypted_buffer_va = (uint32_t) decrypted_buffer; ptr_upicar->len = length; // Perform an uobject call if(!uhcall(UAPP_PICAR_S_FUNCTION_TEST, ptr_upicar, sizeof(picar_s_param_t))) printf("hypercall FAILED\n"); else{ printf("hypercall SUCCESS\n"); - for(i=0;iout[i]; - } + memcpy(digest_result,(unsigned char *)(ptr_upicar->decrypted_buffer_va),digest_size); digest_size = HMAC_DIGEST_SIZE; } - //free(ptr_upicar); if(memcmp(buffer+length,digest_result,digest_size) != 0){ printf("HMAC digest did not match with driver's digest \n"); printf("Bytes returned: "); @@ -120,15 +121,13 @@ int read_i2c(char *buffer,int length){ return ret_length; } -__attribute__((aligned(4096))) __attribute__((section("i2c_section_2"))) static char raw_result[4096]; char * read_raw(){ int flag = 0; int i; - printf("read_raw() :: raw_result address : %p \n",raw_result); for(i=0;ibuffer_va = (uint32_t)&decrypted_buffer; + + if(mlock(&encrypted_buffer, 4096) == -1){ + printf("could not lock memory backing for encrypted buffer"); + return -1; + } + + if(mlock(ptr_upicar->buffer_va, 4096) == -1){ + printf("could not lock memory backing for decrypted buffer"); + return -1; + } + + if(!uhcall(UAPP_PICAR_S_FUNCTION_PROT, ptr_upicar, sizeof(picar_s_param_t))){ + printf("PROT hypercall FAILED\n"); + return -1; //error + } else{ + printf("PROT hypercall SUCCESS\n"); + return 0; //success + } +} + + + + +//return 0 on success, -1 on any error +int lib_exit(void){ + picar_s_param_t *ptr_upicar = &upicar; + ptr_upicar->buffer_va = (uint32_t)&decrypted_buffer; + + if(!uhcall(UAPP_PICAR_S_FUNCTION_UNPROT, ptr_upicar, sizeof(picar_s_param_t))){ + printf("UNPROT hypercall FAILED\n"); + return -1; //error + } + + if(munlock(ptr_upicar->buffer_va, 4096) == -1){ + printf("could not unlock memory backing for buffer after return from UNPROT hypercall"); + return -1; + } + + if(munlock(&encrypted_buffer, 4096) == -1){ + printf("could not unlock memory backing for encrypted buffer after return from UNPROT hypercall"); + return -1; + } + + return 0; //success +} + From c7abdbe137afdd728d8917c444178e0d93f5befe Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Thu, 28 Jan 2021 16:58:01 -0500 Subject: [PATCH 40/42] Line following worked with hyoer calls --- Line_Follower.c | 10 +++++----- Line_Follower.h | 4 ++-- Line_Follower_Test.c | 5 ++++- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 72c2eb1..0dc35e1 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -33,7 +33,8 @@ int read_i2c(char *buffer,int length){ unsigned long digest_size = HMAC_DIGEST_SIZE; unsigned char digest_result[HMAC_DIGEST_SIZE]; - printf("read_i2c() :: decrypted_buffer address : %p \n",decrypted_buffer); + //printf("read_i2c() :: encrypted_buffer address : %p \n",encrypted_buffer); + //printf("read_i2c() :: decrypted_buffer address : %p \n",decrypted_buffer); //----- OPEN THE I2C BUS ----- char *filename = (char*)"/dev/i2c-1"; @@ -74,10 +75,11 @@ int read_i2c(char *buffer,int length){ printf("hypercall FAILED\n"); else{ printf("hypercall SUCCESS\n"); - memcpy(digest_result,(unsigned char *)(ptr_upicar->decrypted_buffer_va),digest_size); + memcpy(digest_result,decrypted_buffer,digest_size); digest_size = HMAC_DIGEST_SIZE; } - if(memcmp(buffer+length,digest_result,digest_size) != 0){ + + if(memcmp(buffer+length,decrypted_buffer,digest_size) != 0){ printf("HMAC digest did not match with driver's digest \n"); printf("Bytes returned: "); for(i=0;i Date: Thu, 28 Jan 2021 20:21:16 -0500 Subject: [PATCH 41/42] Changes needed for the attack --- Line_Follower.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 0dc35e1..6f462cb 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -34,8 +34,7 @@ int read_i2c(char *buffer,int length){ unsigned char digest_result[HMAC_DIGEST_SIZE]; //printf("read_i2c() :: encrypted_buffer address : %p \n",encrypted_buffer); - //printf("read_i2c() :: decrypted_buffer address : %p \n",decrypted_buffer); - + printf("read_i2c() :: decrypted_buffer address : %p \n",decrypted_buffer); //----- OPEN THE I2C BUS ----- char *filename = (char*)"/dev/i2c-1"; if ((file_i2c = open(filename, O_RDWR)) < 0) @@ -74,7 +73,7 @@ int read_i2c(char *buffer,int length){ if(!uhcall(UAPP_PICAR_S_FUNCTION_TEST, ptr_upicar, sizeof(picar_s_param_t))) printf("hypercall FAILED\n"); else{ - printf("hypercall SUCCESS\n"); + //printf("hypercall SUCCESS\n"); memcpy(digest_result,decrypted_buffer,digest_size); digest_size = HMAC_DIGEST_SIZE; } From 11bd997904f8f1b052382cec429e906816bfa44e Mon Sep 17 00:00:00 2001 From: Anton Hristozov Date: Tue, 2 Feb 2021 11:27:20 -0500 Subject: [PATCH 42/42] Moved the sleep in order to be effective for decrypted buffer attack --- Line_Follower.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Line_Follower.c b/Line_Follower.c index 6f462cb..6fb7dc0 100644 --- a/Line_Follower.c +++ b/Line_Follower.c @@ -77,7 +77,10 @@ int read_i2c(char *buffer,int length){ memcpy(digest_result,decrypted_buffer,digest_size); digest_size = HMAC_DIGEST_SIZE; } - + + // Sleep for 10ms so that an attack can succeed in overwriting bytes in buffer + // Car still runs fine with this delay + usleep(10000); if(memcmp(buffer+length,decrypted_buffer,digest_size) != 0){ printf("HMAC digest did not match with driver's digest \n"); printf("Bytes returned: "); @@ -88,6 +91,9 @@ int read_i2c(char *buffer,int length){ for(i=0;i