Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <opensslevp.h>
- unsigned char key[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
- unsigned char nonce[13] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
- 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c};
- unsigned char test[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
- unsigned char AAD[1] = {0x01};
- void encrypt()
- {
- unsigned char output[16];
- unsigned char tag[4];
- int ciphertext_len = 0;
- int len = 0;
- // Create Cipher
- EVP_CIPHER_CTX *ctx;
- // Create and Initialize context
- if (!(ctx = EVP_CIPHER_CTX_new())) {
- printf("Problem creating Cipher");
- }
- // Initialize Encryption Operation
- if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_ccm(), NULL, NULL, NULL)) {
- printf("Error Initializing Encryption");
- }
- // Set nonce length (setting to 13 bytes)
- if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 13, NULL)) {
- printf("Error setting IV length");
- }
- // Set tag length to 4 bytes
- if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 4, NULL)) {
- printf("Error setting tag size");
- }
- // Initialize Key and IV (nonce)
- if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, nonce)) {
- printf("Error initializing Key and IV");
- }
- // THIS IS WHERE THE ISSUE IS //
- // Provide AAD Data
- if (1 != EVP_EncryptUpdate(ctx, NULL, &len, AAD, sizeof(AAD))) {
- printf("Error providing Additional Authenticated Data");
- }
- // END ISSUE
- // Provide message to be encrypted and obtain encrypted output
- if (1 != EVP_EncryptUpdate(ctx, output, &len, test, sizeof(test))) {
- printf("Error obtaining encrypted output");
- }
- ciphertext_len = len;
- // Finalize encryption
- if (1 != EVP_EncryptFinal(ctx, output + len, &len)) {
- printf("Failed to finalize encryption");
- }
- // Get the tag
- if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 4, tag)) {
- printf("Error collecting tag");
- }
- // Clean up
- EVP_CIPHER_CTX_free(ctx);
- printf("nCiphertext Length = %i", ciphertext_len);
- for (int i = 0; i < ciphertext_len; i++) {
- printf("nByte [%i] = %i", i, output[i]);
- }
- for (int i = 0; i < sizeof(tag); i++) {
- printf("nMAC [%i] = %i", i, tag[i]);
- }
- }
Add Comment
Please, Sign In to add comment