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 variables
String fileOutput, obverse;                                    // More strings…
String PadNumber;                                              // Filename
String BlkLn, Bump;
String BlBlock;
char rndchar;                                                  // random character made from the RNG function
int n = 0;                                                     // Filename increment
int x = 0;
int i = 0;
int alphanum = 0;                                              // random placeholder
int sensorValue = 0;                                           // Total of Sensor Values to Mod of “span”
int modulus = 0;                                               // Random Number Output
int maxchar = 126;                                             // Highest ASCII value in the OTP lines
int minchar = 32;                                              // Lowest ASCII value in the OTP lines
int span = maxchar – minchar;                                  // The difference between the highest and lowest
long linecount = 15;                                           // Number of lines of the PADfile
int chipSelect = 4;                                            // setup the cs output line to the SD card socket
void setup() {                                                 // This is where the setup part of the program starts, the part that only runs once at startup
  Serial.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 value
    BlkLn.concat(BlBlock);                                    // Add the next character to the end of the stack
    }
  header = “”;                                                 // Blank the header
  for (int i=minchar; i<=maxchar; i++) {                       // For loop to stack the ASCII characters for the header from lowest to highest value
    header.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 over
  PadNumber=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 batch
  RNG();                                                               // Call the RNG function and pull a true random number
  PADln = “”;                                     // 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 number
      rndchar = char(modulus);                                         // Convert that random number to a ASCII character so we can check it out
      if (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 server
    fileOutput=””;
    fileOutput.concat(Bump);
    fileOutput.concat(header);
    fileOutput.concat(Bump);
    Serial.println(fileOutput);                                              // Drop the header out to the server
    fileOutput=””;
    fileOutput.concat(Bump);
    fileOutput.concat(obverse);
    fileOutput.concat(Bump);
    Serial.println(fileOutput);                                          // Send the line out the server
    Serial.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
}
Spread the love
                

Share This Story, Choose Your Platform!

About the Author: NC Scout

NC Scout is the nom de guerre of a former Infantry Scout and Sergeant in one of the Army’s best Reconnaissance Units. He has combat tours in both Iraq and Afghanistan. He teaches a series of courses focusing on small unit skills rarely if ever taught anywhere else in the prepping and survival field, including his RTO Course which focuses on small unit communications. In his free time he is an avid hunter, bushcrafter, writer, long range shooter, prepper, amateur radio operator and Libertarian activist. He can be contacted at [email protected] or via his blog at brushbeater.wordpress.com .

13 Comments

  1. JD January 23, 2021 at 09:26

    Thanks for posting this

  2. vyt1az January 23, 2021 at 10:37

    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. ;-)

  3. a.b.c.d.e.f January 23, 2021 at 15:12

    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.

    • vyt1az January 24, 2021 at 19:12

      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?

  4. Giorgio January 23, 2021 at 17:01

    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.

  5. MortarGuy January 23, 2021 at 17:29

    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

    • Jack January 23, 2021 at 23:23

      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.

  6. Anonymous January 23, 2021 at 20:11

    5

  7. LoupGarou January 24, 2021 at 13:53

    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

  8. LoupGarou January 24, 2021 at 20:09

    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

    • MortarGuy January 25, 2021 at 00:18

      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

    • stranger January 27, 2021 at 03:26

      How to set up that motion sensor light.
      https://dronebotworkshop.com/rcwl-0516-experiments/

  9. Knowbuddy Yuno January 25, 2021 at 23:37

    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

Comments are closed.

GUNS N GEAR

Categories

Archives

Spread the love