Path: blob/main/crypto/openssl/demos/cipher/aeskeywrap.c
34879 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, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84,300x99, 0xaa, 0x3e, 0x68,31};3233/* Example plaintext to encrypt */34static const unsigned char wrap_pt[] = {350xad, 0x4f, 0xc9, 0xfc, 0x77, 0x69, 0xc9, 0xea, 0xfc, 0xdf, 0x00, 0xac,360x34, 0xec, 0x40, 0xbc, 0x28, 0x3f, 0xa4, 0x5e, 0xd8, 0x99, 0xe4, 0x5d,370x5e, 0x7a, 0xc4, 0xe6, 0xca, 0x7b, 0xa5, 0xb7,38};3940/* Expected ciphertext value */41static const unsigned char wrap_ct[] = {420x97, 0x99, 0x55, 0xca, 0xf6, 0x3e, 0x95, 0x54, 0x39, 0xd6, 0xaf, 0x63, 0xff, 0x2c, 0xe3, 0x96,430xf7, 0x0d, 0x2c, 0x9c, 0xc7, 0x43, 0xc0, 0xb6, 0x31, 0x43, 0xb9, 0x20, 0xac, 0x6b, 0xd3, 0x67,440xad, 0x01, 0xaf, 0xa7, 0x32, 0x74, 0x26, 0x92,45};4647/*48* A library context and property query can be used to select & filter49* algorithm implementations. If they are NULL then the default library50* context and properties are used.51*/52static OSSL_LIB_CTX *libctx = NULL;53static const char *propq = NULL;5455static int aes_wrap_encrypt(void)56{57int ret = 0;58EVP_CIPHER_CTX *ctx;59EVP_CIPHER *cipher = NULL;60int outlen, tmplen;61unsigned char outbuf[1024];6263printf("aes wrap Encrypt:\n");64printf("Plaintext:\n");65BIO_dump_fp(stdout, wrap_pt, sizeof(wrap_pt));6667/* Create a context for the encrypt operation */68if ((ctx = EVP_CIPHER_CTX_new()) == NULL)69goto err;7071EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);7273/* Fetch the cipher implementation */74if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-WRAP", propq)) == NULL)75goto err;7677/*78* Initialise an encrypt operation with the cipher/mode, key and IV.79* We are not setting any custom params so let params be just NULL.80*/81if (!EVP_EncryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL))82goto err;8384/* Encrypt plaintext */85if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, wrap_pt, sizeof(wrap_pt)))86goto err;8788/* Finalise: there can be some additional output from padding */89if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen))90goto err;91outlen += tmplen;9293/* Output encrypted block */94printf("Ciphertext (outlen:%d):\n", outlen);95BIO_dump_fp(stdout, outbuf, outlen);9697if (sizeof(wrap_ct) == outlen && !CRYPTO_memcmp(outbuf, wrap_ct, outlen))98printf("Final ciphertext matches expected ciphertext\n");99else100printf("Final ciphertext differs from expected ciphertext\n");101102ret = 1;103err:104if (!ret)105ERR_print_errors_fp(stderr);106107EVP_CIPHER_free(cipher);108EVP_CIPHER_CTX_free(ctx);109110return ret;111}112113static int aes_wrap_decrypt(void)114{115int ret = 0;116EVP_CIPHER_CTX *ctx;117EVP_CIPHER *cipher = NULL;118int outlen, tmplen;119unsigned char outbuf[1024];120121printf("aes wrap Decrypt:\n");122printf("Ciphertext:\n");123BIO_dump_fp(stdout, wrap_ct, sizeof(wrap_ct));124125if ((ctx = EVP_CIPHER_CTX_new()) == NULL)126goto err;127128EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);129130/* Fetch the cipher implementation */131if ((cipher = EVP_CIPHER_fetch(libctx, "aes-256-wrap", propq)) == NULL)132goto err;133134/*135* Initialise an encrypt operation with the cipher/mode, key and IV.136* We are not setting any custom params so let params be just NULL.137*/138if (!EVP_DecryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL))139goto err;140141/* Decrypt plaintext */142if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, wrap_ct, sizeof(wrap_ct)))143goto err;144145/* Finalise: there can be some additional output from padding */146if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen))147goto err;148outlen += tmplen;149150/* Output decrypted block */151printf("Plaintext (outlen:%d):\n", outlen);152BIO_dump_fp(stdout, outbuf, outlen);153154if (sizeof(wrap_pt) == outlen && !CRYPTO_memcmp(outbuf, wrap_pt, outlen))155printf("Final plaintext matches original plaintext\n");156else157printf("Final plaintext differs from original plaintext\n");158159ret = 1;160err:161if (!ret)162ERR_print_errors_fp(stderr);163164EVP_CIPHER_free(cipher);165EVP_CIPHER_CTX_free(ctx);166167return ret;168}169170int main(int argc, char **argv)171{172if (!aes_wrap_encrypt())173return EXIT_FAILURE;174175if (!aes_wrap_decrypt())176return EXIT_FAILURE;177178return EXIT_SUCCESS;179}180181182