Created
March 27, 2020 12:46
-
-
Save a-dma/68da97bddb875ed41499d4a19acd990b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <ctype.h> | |
#include <errno.h> | |
#include <stdbool.h> | |
#include <stddef.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#ifdef __WIN32 | |
#include <winsock.h> | |
#else | |
#include <arpa/inet.h> | |
#endif | |
#include <openssl/evp.h> | |
#include <openssl/rand.h> | |
#define NONCE_LEN 13 | |
#define TAG_LEN 16 | |
static uint8_t KEY[] = { | |
0x58, 0xdf, 0xe5, 0x21, 0x06, 0xa1, 0x61, 0x43, 0x71, 0x00, 0x1f, 0x66, | |
0x9f, 0x50, 0x36, 0x36, 0xe9, 0x35, 0x4c, 0x18, 0x91, 0xa5, 0x35, 0xf4, | |
0x69, 0xb7, 0x05, 0x67, 0xb7, 0xd9, 0x77, 0x68 | |
}; | |
static uint8_t wrapped_ed25519[] = { | |
0x9a, 0x73, 0x7a, 0x13, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x03, 0x94, 0x8d, 0x24, 0x1d, 0x35, 0x1c, 0x60, 0xca, 0xcd, 0x49, 0xc5, | |
0xad, 0x01, 0xad, 0xd4, 0x17, 0xbb, 0xe8, 0xf8, 0xab, 0xa7, 0x64, 0x27, | |
0xdc, 0x19, 0xe9, 0x3e, 0x9f, 0x24, 0xe4, 0x32, 0xda, 0xd9, 0x16, 0x0b, | |
0x65, 0xf0, 0x9d, 0x51, 0x8e, 0x9d, 0x52, 0x41, 0x8d, 0x0e, 0xc1, 0xfb, | |
0x6f, 0xe0, 0x90, 0xef, 0x3c, 0x23, 0x8f, 0x95, 0x19, 0x72, 0x93, 0x6b, | |
0xcd, 0x13, 0x47, 0x9c, 0xe2, 0x9b, 0xc4, 0x7e, 0x4a, 0xac, 0x8d, 0x57, | |
0xd6, 0xf9, 0xaf, 0x24, 0x79, 0x92, 0xf4, 0x68, 0x1d, 0xb0, 0xcc, 0x7c, | |
0xcd, 0xc6, 0x12, 0x8a, 0x29, 0x4c, 0xe6, 0xe2, 0xc7, 0xbd, 0xa8, 0x12, | |
0xc6, 0x77, 0xae, 0xd0, 0x4e, 0xf7, 0x6a, 0xdc, 0xcf, 0x0d, 0x38, 0x94, | |
0xad, 0x87, 0x45, 0x52, 0x07, 0x4c, 0x6b, 0xf2, 0xd0, 0x45, 0xcc, 0x1c, | |
0x69, 0x8e, 0xc6, 0xcf, 0x2d, 0xb8, 0xf3, 0xe3, 0x10, 0xfd, 0xdc, 0x0e, | |
0x1f, 0x4b, 0xff, 0x21, 0x7b, 0x08, 0x1a, 0xa0, 0x79, 0xa3, 0x57, 0x5c, | |
0x3b, 0x37, 0x03, 0xe0, 0xd1, 0xf2, 0xa2, 0x42, 0x14, 0xbf, 0x6e, 0xfb, | |
0xd0, 0x4d, 0x94, 0x78, 0xfa, 0x6d, 0xec, 0x29, 0x5e, 0xe2, 0x79, 0xc1, | |
0x8b, 0xd2, 0x67, 0x7c | |
}; | |
static bool unwrap_data(uint8_t *key, size_t key_len, | |
uint8_t *nonce, size_t nonce_len, | |
uint8_t *in, size_t in_len, | |
uint8_t *out, size_t *out_len) { | |
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); | |
const EVP_CIPHER *cipher_type; | |
int len = 0; | |
switch (key_len) { | |
case 16: | |
cipher_type = EVP_aes_128_ccm(); | |
break; | |
case 24: | |
cipher_type = EVP_aes_192_ccm(); | |
break; | |
case 32: | |
cipher_type = EVP_aes_256_ccm(); | |
break; | |
default: | |
return false; | |
} | |
// Select cipher | |
if (EVP_DecryptInit_ex(ctx, cipher_type, NULL, NULL, NULL) != 1) { | |
return false; | |
} | |
// Set nonce length | |
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, nonce_len, NULL) != 1) { | |
return false; | |
} | |
// Set tag length | |
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, TAG_LEN, in + in_len - TAG_LEN) != 1) { | |
return false; | |
} | |
// Initialize key and IV | |
if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, nonce) != 1) { | |
return false; | |
} | |
// Provide the message to be decrypted, and obtain the decrypted output | |
if (EVP_DecryptUpdate(ctx, out, &len, in, in_len - TAG_LEN) != 1) { | |
return false; | |
} | |
*out_len = len; | |
// Clean up | |
EVP_CIPHER_CTX_free(ctx); | |
return true; | |
} | |
int main(int argc, char *argv[]) { | |
uint8_t out[2048]; | |
size_t out_len = sizeof(out); | |
if (unwrap_data(KEY, sizeof(KEY), wrapped_ed25519, NONCE_LEN, wrapped_ed25519 + NONCE_LEN, sizeof(wrapped_ed25519) - NONCE_LEN, out, &out_len) == false) { | |
printf("Decryption failed\n"); | |
} | |
else { | |
for (int i = 0; i < out_len; i++) { | |
printf("%c", out[i]); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment