Saturday, February 6, 2021

Enigma in Software

 

My Software Enigma Machine Simulator
My Software Enigma Machine Simulator

In a previous post, I shared my long-held fascination with the Enigma cipher machine used by the German military during World War II.  Once I learned in detail how the machine functions, I decided to test my understanding by coding a working model of it.  I settled on HTML 5 and JavaScript for my platform of choice.  This way, I could run the entire simulator on the client, the JavaScript runtime of the browser.  This should make it accessible to anyone who wants to fool around with it.  It can be accessed from this link.  If you want to review the source code, you can find it here on GitHub.

The main entry point for the simulator is a file called index.html.  It contains the UI, as well as the JavaScript that drives the interaction with the machine.  The UI is a basic bootstrap styled simple form-based GUI.  I'll not go into much if any detail on this.  Instead I'll focus on the parts of the solution that are specific to the simulation of the Enigma.  The workings of the machine itself are abstracted inside a set of JavaScript classes that represent the major components of the machine.  In the HTML head section you find these classes being imported via script tags and the objects being instantiated.

<script src="enigma.js"></script>
<script src="machine.js"></script>
<script src="rotor.js"></script>
<script src="plugboard.js"></script>
<script type="application/javascript">
var enigma = new Enigma();
var machine = new Machine('M3',
new Rotor('B', enigma.arrReflector["B"], 1, 1, [0,0]),
new Rotor('III', enigma.arrRotors[3], 1, 1, enigma.arrKnockpoints[3]),
new Rotor('II', enigma.arrRotors[2], 1, 1, enigma.arrKnockpoints[2]),
new Rotor('I', enigma.arrRotors[1], 1, 1, enigma.arrKnockpoints[1]),
null,
new Plugboard([]));

The Enigma object contains constants which define things like the configuration of the standard internal components of the M3 and M4 German Naval Enigma Machines.  I chose these two models because they can be configured to encipher / decipher any message from any variant of the Enigma machine.  These components are primarily the rotors and their "knock points" or notches which control when they rotate, and the reflectors.  If you need a primer on Enigma Machine parts or how the machine is operated, see this site.

The bulk of the work is done by the Machine object.  When you construct it, you pass in the type of machine, either 'M3' for a three-rotor Naval Enigma or 'M4' for the four-rotor variant introduced late in the war, as the first parameter.  The next 5 parameters must be of type Rotor.  These are the rotor stack of the Enigma, which is made up of three (or four in the case of the M4) rotors from the collection of eight possible rotors, and one of two reflectors.  The final parameter is the set of plugboard mappings.

Rotors themselves are objects because they have specialized state and configuration.  First, the internal wiring varies by rotor.  These are fixed for each numbered rotor, so the wiring can be found in the Enigma.arrRotors array.  Second, each rotor can have notches that allow the rotor to rotate when in a particular position.  These positions are referred to as knock points.  They are fixed for each numbered rotor and their positions are stored in an array of positions in the Enigma.arrKnockpoints array.  The rotor also has an outer ring of numbers which identifies the current position of the rotor.  When configuring the machine for use, the operator can rotate the outer ring, thus shifting the position of the outer ring, altering the mapping, shifting it actually.  This is known as the ring setting.  Lastly, the initial position of the rotor is required.  It can be changed by the machine operator without removing the rotor stack, but it is still part of the initial configuration.

To construct a Rotor object for each rotor or reflector, you need to pass the rotor's name, used for display purposes, the wiring map for the rotor, the ring setting, the initial position, and lastly the array of notches.  An empty array is a valid option, and it needs to be.  The reflector is essentially a rotor with a fixed position, a fixed ring setting, and no knock points.  The first rotor passed into the Machine constructor is actually this reflector.  The reflector mappings are stored in the Enigma.arrReflector array.  The next three (or four in the case of the M4) Rotor objects represent the right, middle, and left rotors in the stack.  If you are constructing an M4 Machine, you must also pass a 4th rotor, often called the greek rotor.  It is smaller, doesn't rotate, but has mapping and ring settings, and initial position like any other rotor.  Only two rotors are allowed in this position, 'b' (beta) and 'g' (gamma).  I don't enforce this in the code in case you want to make your machine even more difficult to crack than the original Enigma.

The final parameter defines the pairs of letters swapped by the plugboard.  In the example above, the plugboard has no letters swapped, thus an empty array is passed to the plugboard constructor.

Once you have created a Machine, the "magic" of encoding is done with the Machine.evaluate() method.  It is pretty short and simple, much like the Enigma itself, but makes for a very challenging cipher to crack.  Just ask Alan Turning!

evaluate(inputLetter) {
// rotate the rotors
this.rotateRotors();

// map the input letter through the plugboard
var l = this.plugboard.mapLetter(inputLetter);

// map the resulting letter into the rotors
l = this.rRotor.mapLetter(l);
l = this.mRotor.mapLetter(l);
l = this.lRotor.mapLetter(l);
if (this.zRotor) {
l = this.zRotor.mapLetter(l);
}
l = this.reflector.mapLetterReflector(l);
if (this.zRotor) {
l = this.zRotor.mapLetter(l, true);
}
l = this.lRotor.mapLetter(l, true);
l = this.mRotor.mapLetter(l, true);
l = this.rRotor.mapLetter(l, true);

// map back through the plugboard
var l = this.plugboard.mapLetter(l);

// return the resulting encoded letter
return l;
}

When you press a key on the keyboard, that key is passed into the evaluate() method as its inputLetter parameter.  Before evaluating, the machine rotates its rotor stack.  Why before encoding?  The mechanical process of pressing the key down pushes on a bar that actuates prongs that actually rotate the rotors, if they are on a notch (knockpoint), so they have to be rotated first.  The rest of the evaluate() method follows the same path through the virtual machine that current flows through the actual machine.  First you map the letter through the plugboard.  The Plugboard object's mapLetter() method knows how to swap any mapped letters.  Next you map the resulting letter through the right, middle, and left rotors.  If there is a greek rotor in this machine, you map through it as well.  Then, you map the letter through the configured reflector, and you reverse the process back out.  Simple, right?  Well, the complexity is hidden mainly in the Rotor.mapLetter() method.  Lets take a look at that next.

mapLetter(inputLetter, reverse) {
// given an input letter, return the output letter for
// the current rotor position

var mappedChar = "";
var outputPos;

var inputPos = this.plaintext.indexOf(inputLetter);
// adjust for position
var adjPos = inputPos + (this.position - 1);
adjPos = this.safetyPos(adjPos);

// adjust for Ring Setting
adjPos = adjPos - (this.ringSetting - 1);
adjPos = this.safetyPos(adjPos);
if (! reverse ) {
mappedChar = this.wireing.charAt(adjPos);
outputPos = this.plaintext.indexOf(mappedChar);
} else {
mappedChar = this.plaintext.charAt(adjPos);
outputPos = this.wireing.indexOf(mappedChar);
};

// readjust for Ring Setting
outputPos = outputPos + (this.ringSetting - 1);
outputPos = this.safetyPos(outputPos);

// readjust for position
outputPos = outputPos - (this.position - 1);
outputPos = this.safetyPos(outputPos);
return this.plaintext.charAt(outputPos);
}

The comments in this method explain what is going on with each block of code.  Basically, you convert the character passed in to its position in the alphabet.  Then you adjust the current position taking into consideration the current position of the rotor.  As the rotor turns, its relative position is increased.  Next you adjust for the ring setting.  When you rotate the ring setting, you are subtracting from the relative position.  The internal wiring of the rotors is handled next.  You have to take into account which side of the rotor is getting the current.  Is the letter coming in from the keyboard or on its way back out of the rotor stack on its way to the lampboard?  The reverse parameter lets the mapLetter() method know.  Finally, you readjust ring setting and position, and return the newly mapped letter.

In addition, let me call your attention to the safetyPos() method of the Rotor class.  The mapLetter() function calls this method every time it adjusts the position.  Why?  Because the rotors are round, and thus they loop back on themselves.  There is no letter 27, so what happens when you adjust position for a letter and the new position is >26 or <=0?  That is right, you have to start back over at the top or the bottom of the list respectively.  safetyPos() does this for the class.

Lastly, you may have noticed that on reflectors, mapLetter() isn't called by Machine.evaluate(), instead mapLetterReflector() is called.  Reflectors don't have ring settings or positions to adjust for so their mapping function is much more simplified.  I could have implemented this better.  Future revisions will refactor this for better code reuse.

Phew!  This took quite some time to work out.  Without the help of the models I built, it would have been very hard to work this out.  I have much respect for the folks at Bletchley Park who figured all this out with paper and pencil!

The next article in this series will explore the next phase of my obsession with the Engima as I port my code to Python so it can run on the Raspberry Pi, and begin to allow the machine to take shape in the real world.  Stay tuned.

Friday, February 5, 2021

Raspberry Pi Pico - First Impressions

A Raspberry Pi Pico board in my hand for scale.

The Raspberry Pi Foundation has recently released a new product.  It is called the Raspberry Pi Pico.  Unlike other Raspberry Pi products to-date, this one is not a single-board computer running Linux that also includes GPIO pins that can control electronics.  Rather, it is a microcontroller, more akin to an Arduino than its namesake cousins.  But, that shouldn't reduce your excitement about the new product, in fact for me it enhanced it.  The details about the product and its specs can be found at raspberrypi.org, so I'll not regurgitate them here.  But, it is a very capable little device.  And it retails for only $4 per unit!  I ordered five to get started.

I received my Picos mid-week last week.  I spent most of the next weekend working through the “Getting Started” book they provide in PDF form on their website.  It is a pretty solid introduction into what can be done with the Pico and MicroPython.

First impressions, I amazed at how capable a device this little thing is. Here are a few things to be aware of if you are getting started.

  1. It comes without pin headers attached, so be prepared to solder your own on.  And at $4 per unit, it doesn’t ship with headers, so you’ll have to reach into your own parts bin for those.
  2. It has support for both C/C++ and MicroPython development, but the Python toolchain appears more mature.
  3. All the documentation has you using the Thony IDE out the gate, which has support for the Pico in its latest version.  If you prefer VS Code or PyCharm, you will need to do some additional work to easily work with your Pico.  I use VS Code as my IDE so here are a few tips that made my life better:
    1. Install the Pico-Go extension to keep your code in synch with the Pico.  It works pretty well.
    2. If you want IntelliSense (code completion) support for the MicroPython classes in VS Code, you’ll need to install Micropy .  It is a CLI that eases the integration with the IDE, enabling MicroPython IntelliSense and giving the Linter the metadata it needs to help you.  You need to execute it in your project folder, and all is better after that.
    3. You may also want to install some other command line utilities like Minicomm and RShell which will allow you to connect to the Pico outside of an IDE environment.
  4. The examples in the book aren’t bad.  If you don’t have the exact components they use lying around, be prepared to modify their code somewhat.  The good news is that it is easy to do.  I didn’t have the same Adafruit LCD screen they used, I just had a generic 2 line 16 character LCD with an I2C backpack.  It didn’t take too much googling to find this library on GitHub which allowed me to talk to the model I had using MicroPython.

Pi Pico mounted in a breadboard driving multiple components

All and all, I had a great time experimenting with the Pico this weekend.  I’m looking forward to incorporating it into projects in the future.

Saturday, January 16, 2021

It Really Is an Enigma

 

A vintage German Army Enigma Machine
For years now I've been fascinated with the enciphering machine used by the Germans during WWII called the Enigma.  I'm not sure why I'm so fascinated.  Maybe it is the elegance in its design.  Its system of patch cables, rotating cross-wired rotors, and complex configuration options lead to the ability to generate a very strong cipher, but at its heart, it is simply an electrical circuit designed to light a single light bulb.  An electronic circuit so basic that any basic electrical engineering class would cover it in their very first lab session.

Regardless of the reasons why, I have developed a significant interest, some might even call it an obsession with this quirky little device.  This post will be the first in a series of posts that one of my coworkers has encouraged me to to make describing my explorations of the Enigma.

Like many engineers, I tend to want to understand not only how a device is used, but how it works.  I've never owned a computer for example that I've not completely disassembled in an effort to fully appreciate and understand its workings.  Unfortunately Enigma machines are quite rare.  The few that remain are in museums and private collections, and they typically do not tolerate curious engineers putting their oily hands on their priceless artifacts.  So, how did I get my hands dirty with the Enigma?

Simulation

Searching around on the Internet, I found lots of sites providing detailed explanations of how the Enigma works.  Some good places to start are:

As I mentioned above, the design of the Enigma is quite elegant, and easy to operate, but that design hides some significant complexity under the hood and inside the components.  Visualizing its operation was hard for me from descriptions and photographs alone.  I needed more.

I first sought out online simulators that emulated the use of an Enigma machine.  Here are some of the best I found:

Those were very helpful in understanding how the machines were used, but were of little or no value in learning how the machines worked.  And without that basic understanding, I had trouble understanding why the Germans used it the way they did, or why it was so challenging to defeat.  I was looking in the wrong place.  What I needed was a way to visualize the inner workings.  I needed a model.

Modeling

Fortunately, the Internet could provide me with just what I needed.  The first one I found helped a lot.  It was the Paper Enigma Machine put together by a gentleman named Michael Koss.  This simple paper-based simulator did a great job of helping me visualize the inner wiring of the rotors, and how they moved based on the physical setup of the machine.  It was good, but it could only take me so far.  It didn't model a plug board, or a way to adjust the ring setting, which I was also having difficulty visualizing.

That led me to the Pringles Can Enigma Machine.  This one is actually a complete, fully functioning, three rotor enigma with ring settings and a plug board if you so desire.  If you'd like to see it in action, and a guide for constructing one of your own, see the video linked above, posted by Videos by Kevin on YouTube.  If you'd prefer to just build it yourself without help, you can download the template here.  One word of caution, the template is formatted for A4 paper.  It is important that you not allow Adobe Acrobat to scale the PDF when it is printed.  You must print it actual size for it to fit properly on your Pringles can.

My Pringles Can Enigma

I hope you can see why this model helped me so much.  It enabled me to follow the traces of the circuit, and to visualize how the movement of the rotors caused the mapping of the letters to shift.  Finally, if you pardon the pun, the connections were made and the light came on for me, so to speak.

Next Steps

This understanding opened a world to me.  Rather than sating my desire to learn more about the Enigma, learning how it worked drove me deeper into its world.  I began reading, and now understanding, more about the device's use during the war.  How the code breakers at Bletchley Park, like Alan Turning were forced to innovate with mechanical computing devices to defeat this simple little electrical circuit.  I was even inspired to create my own versions of the Enigma machine, first based solely on software, but now I'm branching out into hardware replicas as well.  Future articles will go into more detail on those.

I hope this explanation of my early journey to understanding the inner workings of the Enigma has made it less of an enigma to you.

Tuesday, January 12, 2021

Well, it's just Life

In October of 1970, a columnist writing for Scientific American named Martin Gardner published a letter he received the previous March from John Conway, a mathematician at the University of Cambridge titled "The Game of Life."  Life was created 50 years ago.  Who knew?  Seriously, you can read all of Martin Gardner's articles on "The Game of Life" in his book Wheels, Life and other Mathematical Amusements.  The articles on Life can also be found collected in this PDF.

If you studied math or computer science in college, you may have come across Conway's Game of Life.  Its rules are very simple, and thus easy for beginning computer programmers to get their heads around.  So, it often got co-opted by professors as a coding project.  I'm not sure I ever crossed paths with it myself during my education, but I found myself fascinated by it over the 2020 Christmas break after reading the article "The Lasting Lessons of John Conway's Game of Life" in my local paper, sited here where it was originally published in the New York Times.

I wanted to learn to "play" Life the way it was intended to be played before I dove right in and played it software assisted.  So, I started with a spreadsheet, and the rules in the articles above.  I formatted it into a grid of squares and started working through the various Tetrominoes, the famous square patterns used in the game of Tetris.  The T-Tetromino was the most interesting of these in the Game of Life, as you can see from my spreadsheet pictured below.

The Game of Life - Manual Edition

As you can see, the T-Tetromino results is a repeating pattern after 9 generations.  Things get much more interesting and complicated once you get beyond 4 block patterns, so I found myself in search of software.  The most commonly used open source project for the game of life is Golly.  It is a very mature, and extremely powerful implementation of the Game of Life for all modern desktop operating systems.

Golly with the R-Pentomino

Just adding a single block to the T-Tetromino creates the R-Pentomino.  It will generate life that goes well past 1000 generations, including most of the well-known stable forms, including ones that move in stable ways like the glider.

As powerful as Golly is, I found myself wanting something less robust, something that I could use to step through one generation at a time so I could see things unfolding much more slowly.  So I decided to code my own implementation.  I've been doing a lot of work in my lab in Python of late, more on that soon, so I chose Python as my language of choice.  This seemed like a great way to learn how to write cross-platform GUI applications in Python.  I've some experience with Tk/TCL, which the tkinter library supports, but I wanted to learn something new.  So, I decided on wxPython, which is an interface to the wxWidgets library.  It did the job nicely.

My Game of Life Implementation on Generation 84 of the R-Pentomino

In my version, you can either step through one generation at a time, or you can play it forward, but only at a speed of one generation per second, so you can see life unfold a a more relaxed pace.  This was a fun project.  Feel free to explore my source code if you are interested.  It can be found here.  Please be kind.  I'm pretty new to Python programming in general, and this was my first wxPython project.  Still, it was a pretty easy.  I'll likely do more wxPython work in the future as the need arises here in the lab.

If you want to learn more about the Game of Life, you can lose yourself for longer than the lifespan of the R-Pentomino at LifeWiki.  It is essentially the sum of all human knowledge about the Game of Life.  I guess the name "Bible" was already taken.


Saturday, January 2, 2021

Every NUC and Cranny

Like many fans of Apple, I've been impressed with the stories I've read and videos I've watched about the new M1 chip, and the performance of the new Macs that include them, especially the new Mac Mini.  My lust for one of these to play with was pretty strong.  Given my time off for the holidays, I fully expected to end up with one of these on my lab table, but it didn't happen.  I did end up purchasing a mini desktop computer, but not the one I expected.

While getting lost in a sea of YouTube videos about the new Mac Mini one of my favorite YouTube channels, the Drone Bot Workshop posted a new video titled Build a Developer's Linux Workstation - Complete Guide.  First of all, Drone Bot Workshop is a great channel if you are interested in electronics, micro-controller programming, micro computers like the Raspberry Pi, etc.  This video introduced me to the Intel NUC line of bare-bones computers.  They are essentially the same processor and chip sets included in laptops but packaged in a mini desktop configuration.  This seemed much more up my alley.

In the video, Bill built a nearly maxed out version of a NUC 10.  I'll not go into detail.  The video does a much better job explaining it than I ever could.  I could have built exactly what he did, but that would have set me back over $1000.00.  For my needs, I felt I could get by with less memory, less hard disk space, and even less powerful processors.  I ended up with these components.

My fully-assembled NUC - items included for scale

This modest configuration is still massively more powerful than any other computer in my household, including my work laptop and my own personal Macbook Pro!  The NUC 8 is based on the 8th generation Core i5 processor.  Mine has a single, quad-core processor running at 2.3 GHz.  I maxed out the RAM with 32GB, but went with a moderately sized HDD at only 500 GB, but it screaming fast!  It has the Mesa Intel® Iris(R) Plus Graphics 655 integrated video engine.  It is having no issue driving crystal clear 4k video on my 42" 4K Samsung panel as you can see.  And all this for just over $500.00!

NUC on my lab table, driving 4K output

Like Bill, I'm running Ubuntu Desktop Linux 20.04 LTS on my little NUC.  I've had zero issues with it so far.  I'm more than pleased with the results of my little project.  I'd encourage anyone who is considering a small desktop computer to give these little gems from Intel a hard look.

Enigma in Software

  My Software Enigma Machine Simulator In a previous post, I shared my long-held fascination with the Enigma cipher machine used by the Germ...