Programming with Cryptographic Libraries Exercises


Exercises.
From the previous sections, you have learned to write a cryptographic
program using the OpenSSL library, and how to compile your
program. In this section, you will be presented with a series
of exercises requiring you to write programs using the knowledge you
have learned from previous sections. You need to solve each problem
using both Java and C.
If you are submitting your work include your name in the header
of each program. You also need to include a printout of your
program's result as seen on the screen. Use the "script" program on Unix to capture your
screen typing. Zip all source code into a single folder where your
file name is lab1<your name>.zip.
It is not necessary to submit the Worksheet
Solutions document as long as you include a printout of the
following answers.
Problem 1. Given a Base64 encoded ciphertext, key and IV that has been
encrypted using AES in CBC mode, find the plaintext.
 Based64encoded ciphertext:
hTpVHO39rnpFyThzbcI+gg0rdBcbHL+5OqWFFY8QDF6T/nX4+O1lXFgJnDbRFaC1lL5hFY3uVsbQ8mP14yeSD
NnD2dFnhBZLbkjqriE8IwJfcA3yL1Q3LhKQVUWPgPrZVvL98RtKl9ZEYCW/Sb7egw==
 Key: Have you failed?
 IV: This is your IV!
Hints:
 The encoded ciphertext, the key, and the IV are stringtype
 Ciphertext should be on one line or one string to avoid the newline character (\n) in the string.
 For encoding in OpenSSL, use EVP_EncodeBlock and EVP_DecodeBlock
for Base64 encoding and decoding, respectively. Here are function
headers and how to use them. Find more details in openssl/crypto/evp/encode.c
file
 int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int
dlen)
 elen = EVP_EncodeBlock(encoded, ciphertext, clen);
 This function encodes a ciphertext string into encoded string, and
encodes it for clen bytes, which is the total length of the
ciphertext string. This function returns the amount of encoded
string in bytes.
 int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int
n);
 dlen = EVP_DecodeBlock(decoded, encoded, elen); \
 In contrast, this function decodes the encoded string for elen bytes
to decoded string, and returns the amount of decoded string in
bytes.
 You key and IV must be 16 bytes long, which is used for AES cipher.
Problem 2. Given a partial secret key (the first 13 bytes) and
the IV, try
breaking an AES encryption in CBC mode by searching for the last 3
bytes of the key to find the associated plaintext.
 Partial key (the first 13 bytes):
{0x01,0x23,0x45,0x67,0x89,0x1a,0xbc,0xde,0xf0,0x01,0x23,0x45,0x67}
 IV:
{0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0,0x00}
 Known beginning text: "The unknown message is:"
 Base64encoded ciphertext:
A8K+2+60yXYmEBQHOW4BN/2f/ubS5JMHy/B4hpGiaHDbR4qGLBL0AiwvUKi/th+lHt2meNQ82Zmfmnk2+rqd6
dBERgBe82v7Smvri2T3VNuhu00h42l6CWHfPguguX5Ya1MNvmBxvMWygk21q+t0nA==
Hints:
 The partial key and the IV are in hexadecimal form, but the ciphertext are encoded using Base64
 Ciphertext should be on one line or one string to avoid the
newline character (\n) in the string.
 You need to append three bytes at the end of the given partial
key, and change them to find the right key.
 You will need to search within 224 output spaces and find the
meaningful output, which is the original plaintext.
 The plaintext has known beginning. Thus, you will know you find
the right key when the beginning of decrypted plaintext matches the known beginning.
 You can reuse your previous program to crack this problem.
 It may be easier if you start your program to search from smaller
space size such as 256 (28) key spaces, and assume the second last
byte. After you are confident in your program, you can increase the
key search space to one or more bytes.
Problem 3. Implement AES encryption using the Counter Mode Encryption (CTR) or AESCTR,
and use your own key, IV, and
message. Submit your key, IV, plaintext, and the Base64 encoded ciphertext for grading.
Do not forget to encode your ciphertext using
Base64 encoding so it can be readable for grading.
Problem 4. Use the AESCTR mode to decrypt the given ciphertext using a given
key and IV. Find and submit the associated plaintext.
You will use a CTR cipher, a stream mode cipher, to encrypt a counter to
produce a stream of pseudorandom numbers that
are used to encrypt plaintext, which is simply an XOR encryption
operation.
 Key: What do you see?
 IV: Who would ya be?
 Ciphertext: (Encoded by Base64):
dIR1uyU0YhU9s3+jLwgzLhXQbbt2I2IeMqlo9jAZL35cyiW7ICJ5AXSve/MzHyIqW81xp21nahZ0r3v3NQA/K0GEdrszNCsMP
KUr7CwdOStB0Wu3Ij4rETrgbvU5Hy96Uc1juD8kfhQguSWjcU0FMkeEUrc4NH8XOuBI6ykfNTxcyGn+fnYzT2DtOrpqWH8=
 You MUST use AES in CFB mode to encrypt the
counter, which starts from zero.
 Ciphertext should be on one line or one string to avoid the
newline character (\n) in the string.
 Here is the pseudocode for the CTR program.
1 encrypted_counter = AES_Encrypt(counter);
2 for (the number of plaintext bytes)
3 if (we don't have enough encrypted counter bytes) {
4 increase the counter;
5 encrypt the counter;
6 }
7 ciphertext = pseudorandom plaintext;
 On line 1, the first block of random bytes
are generated by encrypting a counter number. Using AES
you will have 16 random bytes from one encryption. This
means you will need 6 AES encryptions for plaintext with a
length between 81  96 bytes. Lines 3 6 will check
whether there are enough encrypted bytes for a XOR operation.
If not the counter should be increased. Line 7 a XOR
operation is performed on the plaintext using the random
bytes.
 CTR's decryption mode is similar to the
process of encrypting the counter in the previous step.
To find the associated plaintext use XOR operations on the
ciphertext using the random bytes generated by the counter. For more information about
the CTR mode visit
http://csrc.nist.gov/CryptoToolkit/modes/workshop1/papers/lipmaactr.pdf.
Hints:
 Define a counter as a 16byte string for simplicity.
 To encrypt a block of 16 bytes of the counter using AES in CFB
mode.
 For each 16 bytes of plaintext, you need to do the counter
encryption, and need to increase the counter by one to encrypt other
16 bytes for next encryption
 Make sure your counter start from zero.
Worksheet Solutions. The following questions you are
required to submit.
Problem 1. Plaintext
____________________________________________________________________
Problem 2. The last three bytes of the key are
______________________________________________
Plaintext
____________________________________________________________________
Problem 3: Message ____________________________________________________________________
Key
________________________________________________________________________
IV __________________________________________________________________________
Ciphertext
___________________________________________________________________
Problem 4: The plaintext is
_______________________________________________________________
