diff --git a/Line_Follower.c b/Line_Follower.c new file mode 100644 index 0000000..6fb7dc0 --- /dev/null +++ b/Line_Follower.c @@ -0,0 +1,334 @@ +/* C module with equivalen functionality like the original Line_Follwer.py */ + +#include +#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" +#include "xmhfcrypto.h" +#include "picar-s.h" + +/* Globals */ +#define RAW_LEN (2*NUM_REF) +int references[NUM_REF] = {200,200,200,200,200}; +const int SLAVE_ADDRESS = 0x11; +int bus = 1; + +__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 + +__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; + int ret_length=0; + int i; + unsigned long digest_size = HMAC_DIGEST_SIZE; + 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); + //----- 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; //<<<<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"); + 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: "); + for(i=0;i 1024){ + continue; + } + } + return analog_result; + } + else{ + break; + } + } + printf("Line follower read error. Please check the wiring.\n"); + return NULL; +} + +__attribute__ ((section("i2c_section"))) static int digital_list[NUM_REF] = {0}; + +int * read_digital(){ + int * lt; + int i; + 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; + } + } + printf("\n"); + } + //printf("read_digital() :: digital_list address : %p \n",digital_list); + 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;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 +} + diff --git a/Line_Follower.h b/Line_Follower.h new file mode 100644 index 0000000..4cb2adb --- /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(); +int lib_init(void); +int lib_exit(void); diff --git a/Line_Follower_Test.c b/Line_Follower_Test.c new file mode 100644 index 0000000..7a1d487 --- /dev/null +++ b/Line_Follower_Test.c @@ -0,0 +1,26 @@ +/* C module with equivalen functionality like the original Line_Follwer.py */ + +#include +#include "Line_Follower.h" + +int main(){ + printf("Line Follower C Test Application Running\n"); + int *dig_list; + int i; + int cnt = 0; + lib_init(); + for(cnt=0;cnt<20;cnt++){ + dig_list = read_digital(); + printf("i2c buffer address %p \n",dig_list); + for(i=0;i $@.$$$$; \ + sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ +rm -f $@.$$$$ 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 $@.$$$$ 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 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__ */ diff --git a/hmac256/stdint.h b/hmac256/stdint.h new file mode 100644 index 0000000..8fa020d --- /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_ */ diff --git a/hmac256/xmhfcrypto.h b/hmac256/xmhfcrypto.h new file mode 100644 index 0000000..1a52ad2 --- /dev/null +++ b/hmac256/xmhfcrypto.h @@ -0,0 +1,270 @@ +/* + * @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__ +#include +#include + +//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__ */