1. Philipp Gesang
  2. enigma


enigma / doc / context / third / enigma / enigma_manual.tex

%% If you’re looking for instructions concerning how to build this
%% manual, please for now drop me an email: ·phg42.2a at gmail dot com·
%% as you will need a certain mtx-script.
\defineitemgroup [credits]
\setupitemgroup  [credits] [
  % before={\startnarrower[middle]},
  % after=\stopnarrower,
\useurl [codebook]   [http://simonsingh.net/books/the-code-book/]
\useurl [luatex-web] [http://www.luatex.org/]
\useurl [chickenize] [https://github.com/alt/chickenize]


\startdocsection[title=Loading the Module/Package]
  \TODO{instuctions for plain, latex + ctx}
  The intention is for the \modulename{Enigma} codebase to integrate
  with the three most popular (as of 2012) \TEX\ formats:
    \PLAIN, and
  If the user interface does not fully conform with the common practise
  of the latter two, please be lenient toward the author whose
  intuitions are for the most part informed by \CONTEXT.
  For this reason, a couple words concerning the interfaces will be
  The examples in this manual will be cycling through all three formats,
  but once you get the general idea of how it works, you will have no
  problem translating between coding styles.
  Those familiar with \CONTEXT\ might, therefore, skip the following
  paragraphs and continue directly with the next section on
  The package is loaded as usual. For \PLAIN, issue a
  \LATEX-users need to place \type{\usepackage{enigma}} somewhere inside
  the preamble.
  (There are no package options.)
  From this point on, instructions for both formats are the same.

  The interface provides two basic macros from which all functionality
  will be derived:
    \texmacro{defineenigma} and \texmacro{setupenigma}.
  Both are a kind of \emph{meta-macros}, meaning that they generate
  other macros which may then be employed to access the functionality of
  the package.
  As such they naturally belong inside the preamble (if you chose to use
  \modulename{Enigma} with \LATEX, that is).
  It follows that the correct and only meaningful order is to
  \texmacro{defineenigma} an enigma machine first and then
  \texmacro{setupenigma} it.
  The former takes a single, the latter a double argument.
  Thus, \type{\defineenigma{encrypt}} creates a new environment
  consisting of the macros \texmacro{startencrypt} and
    \CONTEXT-users will have noticed that there is no simple macro
    \type{\encrypt{foo}}. The reason for this is that the callback
    which the module relies on operates on node-level.
    This means that for the Enigma encryption to have an effect it will
    have to process entire paragraphs.
    As encrypted passages are supposed to stand on their own, this
    small limitation should not have a severe impact on functionality.
    If you should, however, need a macro that works for smaller portions
    of text, please send a feature request to the maintainer
  However, these are not usable right away, as we still have to set the
  initial state of the machine.
  This is achieved by the second meta-macro,
  \type{\setupenigma{encrypt}{<args>}}, where \type{<args>} is a
  placeholder for a list of \emph{assignments}, i.\,e. pairs of
  \emph{key=value} statements by means of which all further parameters
  are specified.
  A list of possible parameters can be found in the next section,
  examples of their effects will be given further below in the section
  on functionality (see \at{page}[sec:fun]).%
    If you grasp the concept of paired \type{\define<foo>} --
    \type{\setup<foo>} macros, then congratulations are in order:
    you qualify for migration from your current macro package to

\startdocsection[title=Options Explained,reference=sec:opts]

  \def\item#1#2{% 1 name, 2 type
  \item{other_chars}{boolean} How to handle non-encodable characters, i.\,e.
    glyphs outside the bare set of Latin letters; see below on
  \item{day_key}{string} Encryption key, i.\,e. a description of the initial
    setup of an Enigma machine: the reflector used, the choice and
    positions of the three rotors, the ring settings, and the plugboard
  \item{rotor_setting}{string} The initial rotor advancement.
  \item{verbose}{integer} Controls overall verbosity level (\emph{global}!).

\TODO{day key syntax, rotor settings strings etc.}

%%% other_chars
Most documents don’t naturally adhere to the machine-imposed restriction
to the 26 letters of the Latin alphabet. The original encipherment
directives comprised substitution tables to compensate for a set of
intrinsic peculiarities of the German language, like umlauts and common
digraphs.  The \modulename{Enigma} simulation module strives to apply
these automatically but there is no guarantee of completeness.

However,\reference[explained:other_chars]{} the Enigma lacks means of
handling languages other than German.  When the substitution lookup
fails, there are two ways of proceeding: either to ignore the current
character or to pass it on to the output as if nothing happened. The
default behaviour is to drop alien letters and move on. If the user
intends to keep these foreign characters instead, E can achieve this by
setting the \identifier{other_chars} key in the \modulename{Enigma}
setup to the value \emph{true}. An example of how the result of both
methods may look, other things being equal, is given in below listing
(example for \CONTEXT; see the file \type{enigma-example-context.tex} in
the \type{doc/} subtree of your installation path).

\usemodule [enigma]
\defineenigma [secretmessage]
\setupenigma  [secretmessage] [
  other_chars = yes,
  day_key = B V III II 12 03 01 GI JV KZ WM PU QY AD CN ET FL,
  rotor_setting = ben,

\defineenigma [othermessage] [secretmessage]
\setupenigma  [othermessage] [other_chars=wrong]


  føo bąr baž
  føo bąr baž


Both methods have their disadvantages: if the user chooses to have the
unknown characters removed it might distort the decrypted text to
becoming illegible. Far more serious, however, are the consequences of
keeping them. As artefacts in the ciphertext they would convey
information about the structure of the plain text.


\startdocsection[title=Basic Functionality,reference=sec:fun]

Encrypt the text of your document using the script interface. For
a start try out the settings as given in below listing.

mtxrun --script mtx-t-enigma                    \
       --setup="day_key = B I II III 01 01 01,  \
                rotor_setting = xyz,            \
                verbose=0"                      \
       --text="Gentlemen don’t read each other’s mail, Mr. Turing\!"

This will result in the thoroughly scrambled string
Then, use the same settings you encrypted the text with in your

\defineenigma [secretmessage]
\setupenigma  [secretmessage] [
  day_key = B I II III 01 01 01,
  rotor_setting = xyz,




If you compile this document with \CONTEXT, the plain text will
Notice that punctuation is substituted with the letter “x” before
encryption and that spaces are omitted.

\useURL[kgbuch] [http://de.wikipedia.org/wiki/Kenngruppenbuch]
       []       [code book]
Now it’s certainly not wise to carry around the key to encrypted
documents as plain text within those documents. The keys will have to be
distributed via an independent channel, e.\,g. a \from[kgbuch].
Keys in general don’t have to be supplied inside the document. If there
is none specified, the module will interrupt the \TEX\ run and
\emph{ask} for user input. Suppose Alice wanted to send an encrypted
file to Bob and already generated the cipher text as follows:

mtxrun --script mtx-t-enigma                                    \
        --setup="day_key =B I IV V 22 07 10 AZ DG IE YJ QM CW,  \
                 rotor_setting = bar,                           \
                 verbose=0"                                     \
        --text="I have nothing to hide. From the NSA, that is."

Alice would then include the result of this line in her \LATEX\ document
as follows:

%% Encryption key not given in the setup.
  rotor_setting = bar,
  verbose       = 3,



She subsequently mails this file to Bob and conveys the key through a
secure channel. They only thing that will be left for Bob to do now, is
to enter the key at the prompt when compiling the document with

\startdocsection[title=Uses and Abuses]

In \LUATEX, \identifier{callback}s may stack. This allows filtering the
input through many enigma machines successively. For instance, in the
following listing, two instances of the same machine are generated and

\usemodule[enigma]              %% load the module
\defineenigma [secretmessage]   %% generate and
\setupenigma  [secretmessage] [ %% configure a machine
  day_key = B IV V II 01 01 01 AD CN ET FL GI JV KZ PU QY WX,
  rotor_setting = foo,

%% now, copy the first machine’s settings
\defineenigma [othermessage] [secretmessage]

%% here we go!

\startothermessage  %% enable machine 1
\startsecretmessage %% enable machine 2 while no 1 is active
Encryption equals decryption.

\stoptext \endinput




© 2012 \emph{Philipp Gesang}. All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

  \item Redistributions of source code must retain the above
    copyright notice, this list of conditions and the following
  \item Redistributions in binary form must reproduce the
    above copyright notice, this list of conditions and the
    following disclaimer in the documentation and/or other
    materials provided with the distribution.

\setuptolerance [horizontal,strict]
\startalignment [right,nothyphenated]
  this software is provided by the copyright
  holder “as is” and any express or implied warranties,
  including, but not limited to, the implied warranties of
  merchantability and fitness for a particular purpose are
  disclaimed. in no event shall the copyright holder or
  contributors be liable for any direct, indirect, incidental,
  special, exemplary, or consequential damages (including, but
  not limited to, procurement of substitute goods or services;
  loss of use, data, or profits; or business interruption)
  however caused and on any theory of liability, whether in
  contract, strict liability, or tort (including negligence or
  otherwise) arising in any way out of the use of this software,
  even if advised of the possibility of such damage.

  \item The idea to implement the \emph{Enigma} cipher for \TEX\ came up
        while I was reading \emph{The Code Book} by Simon Singh.
        This work contains an excellent portrayal of the history of
        German military cryptography and Allied cryptanalysis before and
        during the Second World War.\par
  \item Also, the \modulename{Enigma} module drew lots of inspiration
        from Arno Trautmann’s \modulename{Chickenize} package, which
        remains the unsurpassed hands-on introduction to callback
  \item Finally, without \LUATEX, encryption on node-level would not
        have been possible.\par