spaniakos / Cryptosuite

Cryptographic suite for Arduino & RPi (SHA, HMAC-SHA)
http://spaniakos.github.io/Cryptosuite/
GNU Affero General Public License v3.0
23 stars 11 forks source link

problem with write_L function #13

Closed kSuccess closed 7 years ago

kSuccess commented 7 years ago

hi i try to use HMAC Algorithm fo Arduino in order to implement the integrity but when i call the function write_L(const uint8_t *buffer, size_t size) in my code i have this error : class Sha1Class' has no member named 'write_L'

spaniakos commented 7 years ago

write_L is an internal function that is used from Linux based devises. it is called from the print method. Please follow the examples for arduino.

#if defined(SHA1_LINUX)
    size_t Sha1Class::write_L(const char *str){
        if (str == NULL) return 0;
        return write_L((const uint8_t *)str, strlen(str));
    }   
    size_t Sha1Class::write_L(const uint8_t *buffer,size_t size){
        size_t n = 0;
        while (size--){
            n +=write(*buffer++);
        }
        return n;
    }
    size_t Sha1Class::print(const char *str){
        return write_L(str);
    }

    double Sha1Class::millis(){
        gettimeofday(&tv, NULL);
        return (tv.tv_sec + 0.000001 * tv.tv_usec);
    }
#endif
kSuccess commented 7 years ago

this is my problem : prob

spaniakos commented 7 years ago

write_L is an internal function used from linux based devices like RasberryPi, and it should't be used in your sketch, especially in arduino. plus, i see that you are trying to hash an array of integers, not a string. please read the example sketch and if you have any questions about it, please tell me.

Thank you Spaniakos

kSuccess commented 7 years ago

ok thnak you for ur quick replay i would like to know if there will be a problem if i want to hash an array of integers ?? thanks again

spaniakos commented 7 years ago

normally you has strings, therefore the functions reads pointers. for the encryption algorythms, everything is 0 and 1. they will hash what ever you are giving them. therefore if you hash an array of intergers, the result will not be the one expected. as if you has the integers are strings.

On Thu, Jun 8, 2017 at 2:02 PM, kSuccess notifications@github.com wrote:

ok thnak you for ur quick replay i would like to know if there will be a problem if i want to hash an array of integers ?? thanks again

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/spaniakos/Cryptosuite/issues/13#issuecomment-307071292, or mute the thread https://github.com/notifications/unsubscribe-auth/ABnM6rjz8enuKYsi6dRESh_hwEXLkq5pks5sB9SrgaJpZM4Nza-L .

kSuccess commented 7 years ago

this idea is that i want to send an array of integers between two arduino via nRF24 and i want to encrypt the data(array of integer) via AES and try to verify the integrity of the data via HMAC

thanks

spaniakos commented 7 years ago

just keep the hypothesis that you are transmitting integers. the problem with integers is that depending on the target system (x86 x64 etc.) they have different bytesize. therefore the encryption will be different. Thank why you need to turn them to strings. characters have static size and will have the same result in all machines.

now if you are certain that both of you machines have the same size for integers, you can cast the integers to (void) in order to have plain encryption as void (knowing the size). then decrypt using void again, and when you need to print cast to int.

think of it like: ar_int = [1, 2, 3, 4, 5] (Array of integers) The next line shows you the hex representation. 0x00000001 0x00000002 0x00000003 0x00000004 0x00000005

when you cast to void (void)ar_int you pass the pointer of the array as void type. (you need to know the size, that is 5) then encrypt. for the encryption i use byte to byte encryption, therefore i dont care about the type. i just encrypt blindly. Same for decryption, i decrypt byte to byte. therefore, the result will be the same: 0x00000001 0x00000002 0x00000003 0x00000004 0x00000005 if you do something like int* result = sha256.result.... then you will have a pointer to the memory, that will read integers. now as we all know, an array name is just a pointer to the first byte. so now if you do result[0] the compiler will read the 0x00000001 and print 1. if you do result[1] 0x00000002 will be the result.

hope i helped.

btw i am making a complete framework in order to use cryptography and rf communications. hopefully i will release it soon.

kSuccess commented 7 years ago

thanks alot for your great explication