2

I am currently working on an arduino project using a Teensy 3.2.

The project uses some encryption code that exists in the library folder. When building the encryption code, any padding that was needed was hard-coded until the rest of the encryption worked. With the encryption working, I moved to randomize the padding process.

I am currently using the random() function from Wprogram.h.

When using manual padding, the output seems fine. However as soon as the random number generator is used, the output becomes wild and unintelligible.

When using manual padding:

//st is defined as byte[4][4]
st[0][0]=message; //character from original message, usually 'a' for testing
st[0][1]='1';
st[0][2]='2';
st[0][3]='3';
...
st[3][3]='k'; 


//Output
//set state: a123CDEF89fghijk //state prior to encryption
//encrypted: ⸮$2⸮⸮k6⸮tʠ&⸮⸮ //this should look funny
//decrypted to: a123CDEF89fghijk //matches plaintext

When using random padding, I've tried a few different ways:

//starting off with one random char for ease of testing
//random value seeded by  randomSeed(analogRead(7));
st[0][0]=message;
st[0][1]=byte(random(33,127));//using 33 to 127 to avoid non-print chars
st[0][2]='2';
st[0][3]='3';
...
st[3][3]='k'; 

//-----Also Trying------
//It almost seemed like the data from the random number generator was flawed
//So I put in a scheme to ensure a hard coded character
if(random(100)%2)
  st[0][1]='a';
else
  st[0][1]='b';

//--------Also trying----------
//Since it seems like it is simply the existance of the random value that is
// problematic, I tried calling it without using it

int randval=random(100);
st[0][0]=message;
st[0][1]='1';
st[0][2]='2';
st[0][3]='3';
...
st[3][3]='k'; 

//Typical Output
//set state: a123CDEF89fghijk //state prior to encrypting(random char shows)
//encrypted: *3}3⸮⸮⸮⸮⸮⸮⸮⸮⸮N //this should look funny
//decrypted to: ⸮⸮⸮⸮⸮⸮⸮⸮k⸮-⸮⸮ //this should not look funny

From trying several different ways to create a random character and from also seeing that just the existence of the random value is causing trouble. I am suspecting that there is something buggy about how the random() function works on arduino/teensy.

Something else I noticed after setting up the random number and not using it, is that when I remove this line of code I have to recompile a couple of times before the code works correctly again. This is causing me to suspect the function even more or being buggy.

As I noted in the code samples, I am seeding the random number generator with a unused pin. Even though I would suspect it would be an issue here, I have tried a few different pins just to be safe.

So I guess my questions here might be: does anybody know what might be causing this problem or can possibly suggest and alternate way to get a random value?

  • Without a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) it's going to be very hard to help you. – Some programmer dude Mar 22 '18 at 02:53
  • I placed some samples in the questions, can you clarify what you are looking for? – Steve Paytosh Mar 22 '18 at 02:56
  • It's unlikely the random value itself if the culprit of the problem, it might be the catalyst that causes an existing problem to manifests itself . It's more likely that the problem is either in your "encryption" or your "decryption" (or both) functions. I also recommend you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Mar 22 '18 at 03:00
  • 1
    It is very possible that there is an issue with my encryption or decryption, especially since I recently had to remove anything from the c standard library. However, most of my debugging efforts so far have been focused on showing that this either is or isn't an issue with the cryptocode. I point to the second and third examples of attempts to create a random character to reinforce this point. What I should probably do here is take a more formal approach to proving that the error isn't coming from cryptocode but rather from the random value. – Steve Paytosh Mar 22 '18 at 03:20

0 Answers0