This is an old revision of the document!
Before you start solving the exercises below, download the lab archive.
The archive contains the source code for Exercise 3, but sadly it is encrypted. Luckily, we also forgot to remove the password file from the archive.
Use openssl
commands to decrypt the source file.
In this exercise we'll use OpenSSL to encrypt and decrypt with AES-128-GCM.
Unfortunately, AES-GCM is not supported by the command line utilities of OpenSSL.
Open the file you decrypted in the previous exercise and inspect the code.
There are two functions that need to be completed: aes_gcm_encrypt
and aes_gcm_decrypt
.
Below we have included an example of encryption with RC2 (taken from the OpenSSL man pages). The AES-GCM encryption implementation is quite similar - the authentication tag is automatically appended when finalizing the encryption context.
int do_crypt(FILE *in, FILE *out, int do_encrypt) { /* Allow enough space in output buffer for additional block */ inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; int inlen, outlen; /* Bogus key and IV: we'd normally set these from * another source. */ unsigned char key[] = "0123456789"; unsigned char iv[] = "12345678"; /* Don't set key or IV because we will modify the parameters */ EVP_CIPHER_CTX_init(&ctx); EVP_CipherInit_ex(&ctx, EVP_rc2(), NULL, NULL, NULL, do_encrypt); EVP_CIPHER_CTX_set_key_length(&ctx, 10); /* We finished modifying parameters so now we can set key and IV */ EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt); for(;;) { inlen = fread(inbuf, 1, 1024, in); if(inlen <= 0) break; if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) { /* Error */ EVP_CIPHER_CTX_cleanup(&ctx); return 0; } fwrite(outbuf, 1, outlen, out); } if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) { /* Error */ EVP_CIPHER_CTX_cleanup(&ctx); return 0; } fwrite(outbuf, 1, outlen, out); EVP_CIPHER_CTX_cleanup(&ctx); return 1; }