|
| 1 | +/* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8 -*- */ |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | +#include <openssl/err.h> |
| 6 | +#include <openssl/evp.h> |
| 7 | + |
| 8 | +int encryptdata(const unsigned char* input, size_t inputlen, |
| 9 | + unsigned char* output, size_t* outputlen, |
| 10 | + const unsigned char* key); |
| 11 | +int decryptdata(const unsigned char* input, size_t inputlen, |
| 12 | + unsigned char* output, size_t* outputlen, |
| 13 | + const unsigned char* key); |
| 14 | +void cmd_main(void); |
| 15 | + |
| 16 | +static void handle_openssl_error(const char *message) { |
| 17 | + fprintf(stderr, "%s\n", message); |
| 18 | + ERR_print_errors_fp(stderr); |
| 19 | +} |
| 20 | + |
| 21 | +int encryptdata(const unsigned char* input, size_t inputlen, |
| 22 | + unsigned char* output, size_t* outputlen, |
| 23 | + const unsigned char* key) { |
| 24 | + const EVP_CIPHER *cipher_type = EVP_aes_256_cbc(); |
| 25 | + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); |
| 26 | + int outlen; |
| 27 | + if (ctx == NULL) { |
| 28 | + handle_openssl_error("EVP_CIPHER_CTX_new failed"); |
| 29 | + return -1; |
| 30 | + } |
| 31 | + if (EVP_EncryptInit_ex(ctx, cipher_type, NULL, key, key+32) != 1) { |
| 32 | + handle_openssl_error("EVP_EncryptInit_ex failed"); |
| 33 | + EVP_CIPHER_CTX_free(ctx); |
| 34 | + return -1; |
| 35 | + } |
| 36 | + if (EVP_EncryptUpdate(ctx, output, &outlen, input, inputlen) != 1) { |
| 37 | + handle_openssl_error("EVP_EncryptUpdate failed"); |
| 38 | + EVP_CIPHER_CTX_free(ctx); |
| 39 | + return -1; |
| 40 | + } |
| 41 | + *outputlen = outlen; |
| 42 | + if (EVP_EncryptFinal_ex(ctx, output + *outputlen, &outlen) != 1) { |
| 43 | + handle_openssl_error("EVP_EncryptFinal_ex failed"); |
| 44 | + EVP_CIPHER_CTX_free(ctx); |
| 45 | + return -1; |
| 46 | + } |
| 47 | + *outputlen += outlen; |
| 48 | + EVP_CIPHER_CTX_free(ctx); |
| 49 | + return 0; |
| 50 | +} |
| 51 | + |
| 52 | +int decryptdata(const unsigned char* input, size_t inputlen, |
| 53 | + unsigned char* output, size_t* outputlen, |
| 54 | + const unsigned char* key) { |
| 55 | + const EVP_CIPHER *cipher_type = EVP_aes_256_cbc(); |
| 56 | + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); |
| 57 | + int outlen; |
| 58 | + if (ctx == NULL) { |
| 59 | + handle_openssl_error("EVP_CIPHER_CTX_new failed"); |
| 60 | + return -1; |
| 61 | + } |
| 62 | + if (EVP_DecryptInit_ex(ctx, cipher_type, NULL, key, key+32) != 1) { |
| 63 | + handle_openssl_error("EVP_DecryptInit_ex failed"); |
| 64 | + EVP_CIPHER_CTX_free(ctx); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + if (EVP_DecryptUpdate(ctx, output, &outlen, input, inputlen) != 1) { |
| 68 | + handle_openssl_error("EVP_DecryptUpdate failed"); |
| 69 | + EVP_CIPHER_CTX_free(ctx); |
| 70 | + return -1; |
| 71 | + } |
| 72 | + *outputlen = outlen; |
| 73 | + if (EVP_DecryptFinal_ex(ctx, output + *outputlen, &outlen) != 1) { |
| 74 | + handle_openssl_error("EVP_DecryptFinal_ex failed"); |
| 75 | + EVP_CIPHER_CTX_free(ctx); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + *outputlen += outlen; |
| 79 | + EVP_CIPHER_CTX_free(ctx); |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +void cmd_main(void) { |
| 84 | +} |
0 commit comments