2016-05-18 13:16:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Simple AES GCM test program, uses the same NIST data used for the FIPS
|
2012-10-16 23:03:45 +00:00
|
|
|
* self test but uses the application level EVP APIs.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
|
|
|
/* AES-GCM test data from NIST public test vectors */
|
|
|
|
|
|
|
|
static const unsigned char gcm_key[] = {
|
2015-01-22 03:40:55 +00:00
|
|
|
0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,
|
|
|
|
0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,
|
|
|
|
0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f
|
2012-10-16 23:03:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char gcm_iv[] = {
|
2015-01-22 03:40:55 +00:00
|
|
|
0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84
|
2012-10-16 23:03:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char gcm_pt[] = {
|
2015-01-22 03:40:55 +00:00
|
|
|
0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea,
|
|
|
|
0xcc, 0x2b, 0xf2, 0xa5
|
2012-10-16 23:03:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char gcm_aad[] = {
|
2015-01-22 03:40:55 +00:00
|
|
|
0x4d, 0x23, 0xc3, 0xce, 0xc3, 0x34, 0xb4, 0x9b, 0xdb, 0x37, 0x0c, 0x43,
|
|
|
|
0x7f, 0xec, 0x78, 0xde
|
2012-10-16 23:03:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char gcm_ct[] = {
|
2015-01-22 03:40:55 +00:00
|
|
|
0xf7, 0x26, 0x44, 0x13, 0xa8, 0x4c, 0x0e, 0x7c, 0xd5, 0x36, 0x86, 0x7e,
|
|
|
|
0xb9, 0xf2, 0x17, 0x36
|
2012-10-16 23:03:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char gcm_tag[] = {
|
2015-01-22 03:40:55 +00:00
|
|
|
0x67, 0xba, 0x05, 0x10, 0x26, 0x2a, 0xe4, 0x87, 0xd7, 0x37, 0xee, 0x62,
|
|
|
|
0x98, 0xf7, 0x7e, 0x0c
|
2012-10-16 23:03:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void aes_gcm_encrypt(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
EVP_CIPHER_CTX *ctx;
|
|
|
|
int outlen, tmplen;
|
|
|
|
unsigned char outbuf[1024];
|
|
|
|
printf("AES GCM Encrypt:\n");
|
|
|
|
printf("Plaintext:\n");
|
|
|
|
BIO_dump_fp(stdout, gcm_pt, sizeof(gcm_pt));
|
|
|
|
ctx = EVP_CIPHER_CTX_new();
|
|
|
|
/* Set cipher type and mode */
|
|
|
|
EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
|
|
|
|
/* Set IV length if default 96 bits is not appropriate */
|
2015-01-27 14:05:07 +00:00
|
|
|
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(gcm_iv), NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Initialise key and IV */
|
|
|
|
EVP_EncryptInit_ex(ctx, NULL, NULL, gcm_key, gcm_iv);
|
|
|
|
/* Zero or more calls to specify any AAD */
|
|
|
|
EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad));
|
|
|
|
/* Encrypt plaintext */
|
|
|
|
EVP_EncryptUpdate(ctx, outbuf, &outlen, gcm_pt, sizeof(gcm_pt));
|
|
|
|
/* Output encrypted block */
|
|
|
|
printf("Ciphertext:\n");
|
|
|
|
BIO_dump_fp(stdout, outbuf, outlen);
|
|
|
|
/* Finalise: note get no output for GCM */
|
|
|
|
EVP_EncryptFinal_ex(ctx, outbuf, &outlen);
|
|
|
|
/* Get tag */
|
2015-01-27 14:05:07 +00:00
|
|
|
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16, outbuf);
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Output tag */
|
|
|
|
printf("Tag:\n");
|
|
|
|
BIO_dump_fp(stdout, outbuf, 16);
|
|
|
|
EVP_CIPHER_CTX_free(ctx);
|
|
|
|
}
|
2012-10-16 23:03:45 +00:00
|
|
|
|
|
|
|
void aes_gcm_decrypt(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
EVP_CIPHER_CTX *ctx;
|
|
|
|
int outlen, tmplen, rv;
|
|
|
|
unsigned char outbuf[1024];
|
|
|
|
printf("AES GCM Derypt:\n");
|
|
|
|
printf("Ciphertext:\n");
|
|
|
|
BIO_dump_fp(stdout, gcm_ct, sizeof(gcm_ct));
|
|
|
|
ctx = EVP_CIPHER_CTX_new();
|
|
|
|
/* Select cipher */
|
|
|
|
EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
|
|
|
|
/* Set IV length, omit for 96 bits */
|
2015-01-27 14:05:07 +00:00
|
|
|
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, sizeof(gcm_iv), NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Specify key and IV */
|
|
|
|
EVP_DecryptInit_ex(ctx, NULL, NULL, gcm_key, gcm_iv);
|
|
|
|
/* Zero or more calls to specify any AAD */
|
|
|
|
EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad));
|
|
|
|
/* Decrypt plaintext */
|
|
|
|
EVP_DecryptUpdate(ctx, outbuf, &outlen, gcm_ct, sizeof(gcm_ct));
|
|
|
|
/* Output decrypted block */
|
|
|
|
printf("Plaintext:\n");
|
|
|
|
BIO_dump_fp(stdout, outbuf, outlen);
|
2015-02-02 16:08:16 +00:00
|
|
|
/* Set expected tag value. */
|
2017-06-06 15:35:43 +00:00
|
|
|
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(gcm_tag),
|
2017-05-08 13:23:01 +00:00
|
|
|
(void *)gcm_tag);
|
2015-01-22 03:40:55 +00:00
|
|
|
/* Finalise: note get no output for GCM */
|
|
|
|
rv = EVP_DecryptFinal_ex(ctx, outbuf, &outlen);
|
|
|
|
/*
|
|
|
|
* Print out return value. If this is not successful authentication
|
|
|
|
* failed and plaintext is not trustworthy.
|
|
|
|
*/
|
|
|
|
printf("Tag Verify %s\n", rv > 0 ? "Successful!" : "Failed!");
|
|
|
|
EVP_CIPHER_CTX_free(ctx);
|
|
|
|
}
|
2012-10-16 23:03:45 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
aes_gcm_encrypt();
|
|
|
|
aes_gcm_decrypt();
|
|
|
|
}
|