Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tips for saving RAM? #55

Open
RicardoPires19 opened this issue Jun 1, 2020 · 7 comments
Open

Tips for saving RAM? #55

RicardoPires19 opened this issue Jun 1, 2020 · 7 comments

Comments

@RicardoPires19
Copy link

Hello,

I've been trying out this library (which is great by the way!), but it seems to consume a lot of SRAM. I've already tried to squeeze as much RAM as possible from my program, (not using Strings and allocating variables to flash with PROGMEM) and I don't know where else to cut.

I'm using the XTS-AES128 and SHA256 alongside the SD and Serial libraries. I managed to have the encryption functioning, but once I add the SHA256 there isn't enough memory to open files in the SD card. Both the XTS and SHA256 are declared as variables, should I use pointers instead?

So, what do you recommend?

(I'm using an Arduino UNO, btw)

@rweather
Copy link
Owner

rweather commented Jun 1, 2020

XTS is very heavy - it needs two AES128 objects internally. That will use up to 400 bytes of RAM, which is nearly a quarter of all RAM on a Uno.

The random number generator RNG also consumes about 150 bytes even if you don't use it. So if you don't need that you could remove it from the library or comment it out.

I assume that you are using XTS to encrypt sectors on the SD card? It may be possible to rearrange the code to use only 1 BlockCipher object but keeping the key passed to XTSCommon::setKey() around and then setting each half of the key on the cipher during setTweak(), encryptSector(), and decryptSector(). But it would be slower to set up keys at the start of each sector encrypt/decrypt operation.

You may have to switch to an Arduino with more memory, sorry.

@rweather
Copy link
Owner

rweather commented Jun 1, 2020

Another thing you could try is to use the AESSmall128 version of AES instead of AES128. It is slower, but it uses a lot less RAM for the key schedule.

@RicardoPires19
Copy link
Author

Ok, I'll try to start by removing the RNG function, I think 150 bytes will be enough. But is the random() Arduino built-in function not secure?

@rweather
Copy link
Owner

rweather commented Jun 1, 2020

If you need a source of random numbers for your application, then random() is not secure enough and you'll need RNG. But if you are encrypting/decrypting the SD card contents with a fixed XTS key then you may not need RNG.

@RicardoPires19
Copy link
Author

Why isn't is secure? Because of the lack of randomness? Isn't the randomSeed() function good enough to compensate?

@rweather
Copy link
Owner

rweather commented Jun 2, 2020

System random number generators like random() in Arduino and rand() in C are never safe to use for cryptographic purposes.

The randomSeed() function takes an unsigned long, which is only 32 bits of entropy, even assuming that you have a good source to get entropy from. And the random() function uses a predictable algorithm for converting one seed value into the next.

Such random number generators are fine for controlling the random firing from a monster in a video game. But not for cryptography.

Here is some more information on system random number generators: https://crypto.stackexchange.com/questions/15662/how-vulnerable-is-the-c-rand-in-public-cryptography-protocols

@profdc9
Copy link

profdc9 commented Jun 2, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants