Last active
May 18, 2022 18:02
-
-
Save agrare/90510235853901e8f0b494f861fd727c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <openssl/evp.h> | |
#include <openssl/err.h> | |
void print_ssl_error_stack(void) | |
{ | |
int err; | |
while ((err = ERR_get_error()) != 0) { | |
fprintf(stderr, "ERR lib=%d reason=%d err=%s\n", ERR_GET_LIB(err), ERR_GET_REASON(err), ERR_reason_error_string(err)); | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
unsigned char key[] = "0123456789abcdeF"; | |
unsigned char iv[] = "1234567887654321"; | |
unsigned char data[] = "this is a secret"; | |
unsigned char *out; | |
int size, outlen, tmplen, err; | |
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); | |
err = EVP_CipherInit_ex(ctx, EVP_chacha20_poly1305(), NULL, key, iv, 1); | |
if (err != 1) { | |
print_ssl_error_stack(); | |
return 1; | |
} | |
size = sizeof(data); | |
out = malloc(size); | |
err = EVP_CipherUpdate(ctx, out, &outlen, data, size); | |
if (!err) { | |
print_ssl_error_stack(); | |
return 1; | |
} | |
err = EVP_CipherFinal_ex(ctx, out, &outlen); | |
if (!err) { | |
print_ssl_error_stack(); | |
return 1; | |
} | |
return 0; | |
} |
Author
agrare
commented
May 18, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment