Arduino One Time Pad Source Code
Sent in by a reader. OTP encryption, both the methodology and philosophy of use, are things we cover in the Advanced RTO Course. You might wanna get in on that while you can. -NCS
Afternoon Scout,I didn’t know if you wanted to put the Arduino OTP code up on AP as a post, but I would be OK with that. Here is the source as inline text (I will also include the two PDF files I put on the old forum as well):#include <SD.h> // Make sure the compiler knows to add the SD card library#include <SPI.h>String header, PADln, nxtchar; // Setup the string variablesString fileOutput, obverse; // More strings…String PadNumber; // FilenameString BlkLn, Bump;String BlBlock;char rndchar; // random character made from the RNG functionint n = 0; // Filename incrementint x = 0;int i = 0;int alphanum = 0; // random placeholderint sensorValue = 0; // Total of Sensor Values to Mod of “span”int modulus = 0; // Random Number Outputint maxchar = 126; // Highest ASCII value in the OTP linesint minchar = 32; // Lowest ASCII value in the OTP linesint span = maxchar – minchar; // The difference between the highest and lowestlong linecount = 15; // Number of lines of the PADfileint chipSelect = 4; // setup the cs output line to the SD card socketvoid setup() { // This is where the setup part of the program starts, the part that only runs once at startupSerial.begin(115200); // initialize serial communications at 19200 bps for either PC or LCD deisplay:BlBlock = char(177);Bump = BlBlock+BlBlock+BlBlock+BlBlock;for (int i=minchar; i<=maxchar+8; i++) { // For loop to stack the ASCII characters for the header from lowest to highest valueBlkLn.concat(BlBlock); // Add the next character to the end of the stack}header = “”; // Blank the headerfor (int i=minchar; i<=maxchar; i++) { // For loop to stack the ASCII characters for the header from lowest to highest valueheader.concat(char(i)); // Add the next character to the end of the stack}n=0;}void loop() { // This is the main loop of the program, it just keeps doing all of the next segment over and overPadNumber=n;Serial.println(“Page#:” + PadNumber);Serial.println(BlkLn);for (int i=0; i<linecount; i++) { // Count lines of output till “linecount”, and then let it pause for 10 seconds, then start another batchRNG(); // Call the RNG function and pull a true random numberPADln = “”; // Set the variable for the PAD line generation “PADln”obverse= “”;while (PADln.length() < span+1) { // While the length of PADln is less than the length of the header line, go and do the following…RNG(); // Grab another random numberrndchar = char(modulus); // Convert that random number to a ASCII character so we can check it outif (header.indexOf(rndchar)>=0 && PADln.indexOf(rndchar)==-1) { // IF it is in the header but not in “PADln” yet, add it to the end of “PADln”PADln.concat(rndchar); // the actual add it to the end line}}for (int i=0; i<=span; i++) {x = PADln.indexOf(header.charAt(i));obverse.concat(header.charAt(x));}fileOutput=””;fileOutput.concat(Bump);fileOutput.concat(PADln);fileOutput.concat(Bump);Serial.println(fileOutput); // Send the line out the serverfileOutput=””;fileOutput.concat(Bump);fileOutput.concat(header);fileOutput.concat(Bump);Serial.println(fileOutput); // Drop the header out to the serverfileOutput=””;fileOutput.concat(Bump);fileOutput.concat(obverse);fileOutput.concat(Bump);Serial.println(fileOutput); // Send the line out the serverSerial.println(BlkLn);}fileOutput=””;Serial.println(fileOutput);Serial.println(fileOutput);n++; // add one to the filename and keep going}// Functions from here on down…void RNG() {sensorValue = 0; // read all the analog values of the 6 analog ports, adding them together in “sensorvalue”sensorValue = analogRead(0) + analogRead(1) + analogRead(2) + analogRead(3) + analogRead(4) + analogRead(5);modulus = minchar + sensorValue % (span+1); // take the remainder of the total into “span+1” and add it to the minimum ASCII value to get// random numbers that are in a tighter range and work quicker through the system// delay(1); // (REMMED OUT) delay at least a millisecond between RNG reads so that the ADCs have a chance to settle}
Share This Story, Choose Your Platform!
13 Comments
Comments are closed.
Thanks for posting this
If things like this are on GitHub, GitLab, or other repos, it’s a good idea to clone them to your local machine and back up for safe keeping. That makes it a lot easier to get the code running quickly.
Git is meant to be completely decentralized and can be used both offline and online between computers without a central server. With that said, servers like GitLab are open source and obviously convenient. You can run them on a local network or online, and with your own hardware.
Keeping paper copies of critical items like this is also handy in case your connection / infrastructure goes down. Printed Linux manuals, including manuals for older versions are also important to keep. That way if you run into a Linux install without an online connection, you still have a reference.
With Linux distros, the storage cost is often too high to keep old versions online. This means they’re deleted, which can make learning them difficult if you run into them while scavenging the wastelands with your RobCo Industries Pip-Boy. ;-)
If you’re using a unix system from the past couple decades, you probably have the /dev/random device available (for this sort of work do not use /dev/urandom which uses a pseudorandom sequence generator, but /dev/random which will block on the kernel entropy pool) and can use even a very simple command to turn that into a printable sequence of digits.
something like this would work fine for numeric code groups:
$ dd if=/dev/random bs=1 count=100 | od -d
0000000 64663 43990 40679 22724 58102 37887 11950 35046
0000020 36894 46186 52736 12686 57937 5948 36694 9665
0000040 51137 1044 53631 31253 59482 11765 60897 25846
0000060 62914 29073 45587 60343 65195 12474 669 30291
0000100 38826 49070 30351 57838 7075 11784 510 51712
0000120 51297 29684 13978 33379 10882 43725 35354 5931
0000140 1277 53165
0000144
100+0 records in
100+0 records out
100 bytes copied, 26.3584 s, 0.0 kB/s
$
here we used dd with a blocksize of 1 byte ad told it 100 blocks, because otherwise the read from /dev/random will
return with whatever bits are in the entropy pool thus far- probably fewer than your block size. this way it will wait until it has collected as many bytes as you wanted. od is a simple tool to print binary data in decimal, hex, octal, quick and dirty.
This is absolutely awesome. You deserve a six pack for this.
This should add enough entropy to remove potential manipulation of RNGs that have been put into certain libraries / hardware as well correct?
Just an FYI, copying the source into the arduino IDE creates several \342 errors. Basically the quotation marks are wrong.
The symbol ” is not “. Those are called ‘smart quotes’ and are usually found in rich documents or blogs. Would be good to have a link to a plain text version. Also worth explaining the hardware setup. I assume you need something plugged into the a0-a5 ports.
Does this generate true random numbers? Other arduino true random number generators I’ve seen use something like geiger tubes.
Okay, I’ll play your silly game and jump on the grenade here. Going out on a limb here, but I’ll wager many readers here are unfamiliar with Arduino, Raspberry Pi, Unix, Linux, coding, and other esoteric details of this nerdy subset of what is essentially for our purposes the topic of electronic warfare and the means to effect it.
I’d request a bit of background on behalf of the readership for the purposes of introducing Arduino and what it can do for us (versus Raspberry Pi, for example) and a quick rundown on what is needed to make it work and maybe a source of useful references for newbs like myself.
This article is GREATLY appreciated. I just don’t understand the context of how to get it into operation or where to efficiently look myself for that information.
As always, thanks for the excellent information. If I overlooked a prelim article or intro to this subject, the fault is mine. Thanks again.
73
Yeah what he said!
I think it would be easier to understand Mandarin Chinese.
Audio, I am 100% solid. From 1 to 100,000…i can choose, setup, run and understand those systems.
But this 10101010 “ ‘ goto remout rng{} shtuff.
Yeah…it’s Alien. Liiiiiiile ET Alien.
5
This was the source code (I still need to clean it up a bit because emailing after copy&pasting from the IDE likes to mess up formatting) that I posted on the Forum when it was up and a few people asked about it. As soon as another forum is up for here I will post it and the rest of the series. This one just sends the OTP pads back through the console port to the PC.
I do have ones that send it directly over the network ports, as well as ones that make files on MicroSD cards, and one that sends it back through the HID keyboard port so that they appear in Notapad or Word to be saved to disk (but that one wont run on Arduino Unos).
If NC Scout wants me to, I can post/email more or upload some of my other projects that I have done that would very beneficial. This one just generates true Random Pads for encryption/decryption by hand using the analog inputs as random number generators. If you attack a 1-2 foot long piece of wire into the A0-A5 analog ports, it speeds up random number generation.
Loup
I’m trying to repost a copy without the line by line comments as they are what is messing the copy/paste back into the IDE up. I may put it up on Github or similar, but it is part of a much larger set of projects I am working on.
The main point of why I wanted to get this back up after the Forum went down is to get people thinking of the ways that different things can be used to help with our communication, security, or other needs and make it easier for us. Tools. Force Multipliers (and we need a bunch of them). Things like OTP generators of different types.
And they can be simple things, like hooking an IR LED to a RCWL-0516 Doppler motion detector board with a Li-ION battery and slipping the thing in a PVC pipe on a tree. Use a bunch of them and at night with your NODs you have additional help seeing who is on your trails in the woods. A nice force multiplier for under $3 each.
Loup
Loup, you’re picking up what I’m putting’ down here. I get it – some of you guys are dialed in to this tech nerd stuff. For that, I applaud you. I’d love to opportunity to pick your brain for a week or two and sit at your elbow while you tinker. Unfortunately, there’s probably lots of us here that don’t have a clue because we grew up in the caveman era and never had the opportunity to get off the short bus. Anything to point us in the right direction to shorten the learning curve is immensely appreciated. Thanks again. This is good stuff.
MortarGuy
How to set up that motion sensor light.
https://dronebotworkshop.com/rcwl-0516-experiments/
If you think you might need to generate truly random numbers in a grid down situation – say, for a one time pad – you can always use ten sided dice.
Yes, it’s annoyingly slow to do it manually, but it’s truly random. Buy the dice in bulk so you don’t have to re-roll too often.
Just be sure to use TEN SIDED dice, and don’t keep a one time pad key on a computer.
https://www.americanpartisan.org/2019/12/revisited-clandestine-communications-part-one-one-time-pads/
http://users.telenet.be/d.rijmenants/en/onetimepad.htm