CS 2011, A Term 1999
Introduction to Machine Organization and Assembly Language
Homework 5 (due Oct. 1)
Objective
In this assignment you will use 80X86 assembly language to implement a private key
encryption scheme based on a linear congruence pseudorandom number generator.
You will need to combine several assembly language programming tools, including
DOS functions for file I/O, MUL and DIV instructions for the linear congruence
generator, bit-level manipulation for encryption and decryption, and procedures
to encapsulate some of the main ingredients.
Instructions
You will write an assembly language program that reads character data from
a user-specified input file, encrypts it by XOR-ing it with a pseudorandom
character sequence, and writes the encrypted version to a user-specified
output file. Your program should assemble, link, and debug correctly using
tasm / tlink / td.
- Write an assembly language procedure that accepts an unsigned integer as a parameter (seed)
and generates a pseudorandom number using the linear congruence method
described below. Debug and test this procedure using td.
- Write assembly language instructions that fill a mask buffer with
characters obtained using repeated calls to your pseudorandom number
generator procedure. The characters in the mask buffer will be used to encrypt
characters stored in a separate data buffer.
- Define a data buffer separate from the mask buffer.
Write a procedure that encrypts the character sequence in the data buffer
by replacing each character with the result of XOR-ing that character with the character
in the corresponding position of the mask buffer.
- Write a procedure that reads character data from
a previously existing input file and stores it in the data buffer. Use a
text editor to create a test file and store a message in the file.
Debug your code using td, making sure that your message is being
correctly stored in memory.
- Write a procedure that writes the characters in the data buffer to an output file.
Debug your code to check that the message from the input file is transferred correctly
to the output file when the output procedure is called after the input procedure
from the preceding step.
- Add console I/O to your program so that the user can specify the
names of the input and output files. You may use appropriate functions
from the Irvine library.
- Combine the code from the above steps and debug it. The resulting program
should prompt the user to provide the names of the input and output files;
it should then read the data from the input file, encrypt it by XOR-ing
each data character with the corresponding mask character, and write
the encrypted version to the output file.
- Test your program for various input files. Notice that the XOR masking method
has the property that if the encrypted input is used as the new input for the
same encryption scheme, then the original input is recovered. Make sure your
program behaves in this way.
Deliverables
- Submit a floppy disk containing the assembly source code and documentation
(follow the
WPI CS documentation standard described at
http://www.cs.wpi.edu/Help/documentation-standard.html), together with two
different examples of text files and their encryptions generated by your
program. Give complete DOS command-line information needed to assemble and link
your program using tasm and tlink.
- Include detailed descriptions of all parameter values used, in particular
the values of the various parameters used to generate the pseudorandom
masking sequence in each case.
Also include detailed instructions allowing the TA to feed an input file containing
a brief message such as "Assembly rules!" to your
program and to inspect both the encrypted and decrypted versions of the message
in the respective output files.
- Explain why the sequences produced by your pseudorandom number generator
aren't really "random". If someone showed you a long sequence of characters
generated using a linear congruence generator as described below, how could
you tell that the sequence was produced by a non-random process?
Explain carefully.
- Submit hardcopies of the above items.
Everything you submit (files too) must include your name, login, and section number.
Background Information
XOR encoding
Let x and y be arbitrary bits. Then, using infix notation,
(x XOR y) XOR y = x.
Why? Just check all the cases. The y bit is either 0 or 1. Suppose y is 0.
In this case, x XOR y = x XOR 0 = x by the XOR truth table, and XOR-ing with y
again we get x again as claimed. Now suppose y is 1. Then x XOR y = x XOR 1 = ~x
(the negation, or toggle, of x). XOR-ing this with y just toggles the result
back to x. So (x XOR y) XOR y = x in all cases.
The above XOR formula might seem like a complicated way of doing nothing,
since we get x back again at the end. But suppose that we only know the
intermediate result x XOR y. Then, unless we know the value of y, we can't
recover x. So we have an encryption method. Keep y in a safe place.
Use y to XOR an incoming bit x. Store x XOR y, but don't reveal y.
When you need x again, whip out y and XOR the stored result to
recover x.
Pseudorandom sequences via the linear congruence method
This method uses fixed integers called the multiplier, the increment, and
the modulus to produce periodic integer sequences with potentially very long
periods that approximate random sequences. The core of the method is a recurrence
relation which takes an initial value, or seed, and generates an integer from it.
That new integer is used as the seed for the next iteration, and so on.
In formula form:
newseed = remainder in the integer division (multiplier*seed + increment) / modulus
Many different values of the defining parameters may be considered.
For starters try:
- multiplier = 40
- increment = 3641
- modulus = 729
- seed = 1
The pseudorandom sequences generated by this method may be used to encrypt
ASCII character sequences using the XOR-masking method described above.
Only 8 bits of the remainder are used in this context.
File I/O
Everything you need to know is in Irvine 12.1.1, 12.3.
Examples of DOS functions you can use are:
function 3Dh to open file
function 3Fh to read from file
function 40h to write to file
function 3Eh to close file
Feel free to use appropriate functions from the Irvine link library
if you'd like.