Path: blob/main/crypto/openssl/demos/cipher/aeskeywrap.c
106879 views
/*1* Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* Simple aes wrap encryption demonstration program.11*/1213#include <stdio.h>14#include <openssl/err.h>15#include <openssl/bio.h>16#include <openssl/evp.h>17#include <openssl/crypto.h>18#include <openssl/core_names.h>1920/* aes key */21static const unsigned char wrap_key[] = {220xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66,230x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,240xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f25};2627/* Unique initialisation vector */28static const unsigned char wrap_iv[] = {290x99,300xaa,310x3e,320x68,330xed,340x81,350x73,360xa0,370xee,380xd0,390x66,400x84,410x99,420xaa,430x3e,440x68,45};4647/* Example plaintext to encrypt */48static const unsigned char wrap_pt[] = {490xad,500x4f,510xc9,520xfc,530x77,540x69,550xc9,560xea,570xfc,580xdf,590x00,600xac,610x34,620xec,630x40,640xbc,650x28,660x3f,670xa4,680x5e,690xd8,700x99,710xe4,720x5d,730x5e,740x7a,750xc4,760xe6,770xca,780x7b,790xa5,800xb7,81};8283/* Expected ciphertext value */84static const unsigned char wrap_ct[] = {850x97,860x99,870x55,880xca,890xf6,900x3e,910x95,920x54,930x39,940xd6,950xaf,960x63,970xff,980x2c,990xe3,1000x96,1010xf7,1020x0d,1030x2c,1040x9c,1050xc7,1060x43,1070xc0,1080xb6,1090x31,1100x43,1110xb9,1120x20,1130xac,1140x6b,1150xd3,1160x67,1170xad,1180x01,1190xaf,1200xa7,1210x32,1220x74,1230x26,1240x92,125};126127/*128* A library context and property query can be used to select & filter129* algorithm implementations. If they are NULL then the default library130* context and properties are used.131*/132static OSSL_LIB_CTX *libctx = NULL;133static const char *propq = NULL;134135static int aes_wrap_encrypt(void)136{137int ret = 0;138EVP_CIPHER_CTX *ctx;139EVP_CIPHER *cipher = NULL;140int outlen, tmplen;141unsigned char outbuf[1024];142143printf("aes wrap Encrypt:\n");144printf("Plaintext:\n");145BIO_dump_fp(stdout, wrap_pt, sizeof(wrap_pt));146147/* Create a context for the encrypt operation */148if ((ctx = EVP_CIPHER_CTX_new()) == NULL)149goto err;150151EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);152153/* Fetch the cipher implementation */154if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-WRAP", propq)) == NULL)155goto err;156157/*158* Initialise an encrypt operation with the cipher/mode, key and IV.159* We are not setting any custom params so let params be just NULL.160*/161if (!EVP_EncryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL))162goto err;163164/* Encrypt plaintext */165if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, wrap_pt, sizeof(wrap_pt)))166goto err;167168/* Finalise: there can be some additional output from padding */169if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))170goto err;171outlen += tmplen;172173/* Output encrypted block */174printf("Ciphertext (outlen:%d):\n", outlen);175BIO_dump_fp(stdout, outbuf, outlen);176177if (sizeof(wrap_ct) == outlen && !CRYPTO_memcmp(outbuf, wrap_ct, outlen))178printf("Final ciphertext matches expected ciphertext\n");179else180printf("Final ciphertext differs from expected ciphertext\n");181182ret = 1;183err:184if (!ret)185ERR_print_errors_fp(stderr);186187EVP_CIPHER_free(cipher);188EVP_CIPHER_CTX_free(ctx);189190return ret;191}192193static int aes_wrap_decrypt(void)194{195int ret = 0;196EVP_CIPHER_CTX *ctx;197EVP_CIPHER *cipher = NULL;198int outlen, tmplen;199unsigned char outbuf[1024];200201printf("aes wrap Decrypt:\n");202printf("Ciphertext:\n");203BIO_dump_fp(stdout, wrap_ct, sizeof(wrap_ct));204205if ((ctx = EVP_CIPHER_CTX_new()) == NULL)206goto err;207208EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);209210/* Fetch the cipher implementation */211if ((cipher = EVP_CIPHER_fetch(libctx, "aes-256-wrap", propq)) == NULL)212goto err;213214/*215* Initialise an encrypt operation with the cipher/mode, key and IV.216* We are not setting any custom params so let params be just NULL.217*/218if (!EVP_DecryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL))219goto err;220221/* Decrypt plaintext */222if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, wrap_ct, sizeof(wrap_ct)))223goto err;224225/* Finalise: there can be some additional output from padding */226if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))227goto err;228outlen += tmplen;229230/* Output decrypted block */231printf("Plaintext (outlen:%d):\n", outlen);232BIO_dump_fp(stdout, outbuf, outlen);233234if (sizeof(wrap_pt) == outlen && !CRYPTO_memcmp(outbuf, wrap_pt, outlen))235printf("Final plaintext matches original plaintext\n");236else237printf("Final plaintext differs from original plaintext\n");238239ret = 1;240err:241if (!ret)242ERR_print_errors_fp(stderr);243244EVP_CIPHER_free(cipher);245EVP_CIPHER_CTX_free(ctx);246247return ret;248}249250int main(int argc, char **argv)251{252if (!aes_wrap_encrypt())253return EXIT_FAILURE;254255if (!aes_wrap_decrypt())256return EXIT_FAILURE;257258return EXIT_SUCCESS;259}260261262