void CCryptKey::DecryptData(const std::vector<unsigned char>& encrypted, std::vector<unsigned char>& data)
{
    ies_ctx_t *ctx;
    char error[1024] = "Unknown error";
    cryptogram_t *cryptogram;
    size_t length;
    unsigned char *decrypted;

    ctx = create_context(pkey);
    if (!EC_KEY_get0_private_key(ctx->user_key))
        throw key_error("Given EC key is not private key");

    size_t key_length = ctx->stored_key_length;
    size_t mac_length = EVP_MD_size(ctx->md);
    cryptogram = cryptogram_alloc(key_length, mac_length, encrypted.size() - key_length - mac_length);

    memcpy(cryptogram_key_data(cryptogram), &encrypted[0], encrypted.size());

    decrypted = ecies_decrypt(ctx, cryptogram, &length, error);
    cryptogram_free(cryptogram);
    free(ctx);

    if (decrypted == NULL) {
        throw key_error(std::string("Error in decryption: %s") + error);
    }

    data.resize(length);
    memcpy(&data[0], decrypted, length);
    free(decrypted);
}
Exemple #2
0
int processor(int iteration) {

        int tlen;
        size_t olen;
        EC_KEY *key = NULL;
        secure_t *ciphered = NULL;
        char *hex_pub = NULL, *hex_priv = NULL;
        unsigned char *text = NULL, *copy = NULL, *original = NULL;

        // Generate random size for the block of data were going to encrypt. Use a min value of 1 KB and a max of 1 MB.
        do {
                tlen = (rand() % (1024 * 1024));
        } while (tlen < 1024);

        if (!(text = malloc(tlen + 1)) || !(copy = malloc(tlen + 1))) {
                printf("Memory error.\n");
                processor_cleanup(key, ciphered, hex_pub, hex_priv, text, copy, original);
                return -1;
        }

        // Wipe and then fill the data blocks with random data.
        memset(copy, 0, tlen + 1);
        memset(text, 0, tlen + 1);
        for (uint64_t j = 0; j < tlen; j++) {
                *(copy + j) = *(text + j) = (rand() % 255);
        }

        // Generate a key for our theoretical user.
        if (!(key = ecies_key_create())) {
                printf("Key creation failed.\n");
                processor_cleanup(key, ciphered, hex_pub, hex_priv, text, copy, original);
                return -1;
        }

        // Since we'll store the keys as hex values in reali life, extract the appropriate hex values and release the original key structure.
        if (!(hex_pub = ecies_key_public_get_hex(key)) || !(hex_priv = ecies_key_private_get_hex(key))) {
                printf("Serialization of the key to a pair of hex strings failed.\n");
                processor_cleanup(key, ciphered, hex_pub, hex_priv, text, copy, original);
                return -1;
        }

        if (!(ciphered = ecies_encrypt(hex_pub, text, tlen))) {
                printf("The encryption process failed!\n");
                processor_cleanup(key, ciphered, hex_pub, hex_priv, text, copy, original);
                return -1;
        }

        if (!(original = ecies_decrypt(hex_priv, ciphered, &olen))) {
                printf("The decryption process failed!\n");
                processor_cleanup(key, ciphered, hex_pub, hex_priv, text, copy, original);
                return -1;
        }

        if (olen != tlen || memcmp(original, copy, tlen)) {
                printf("Comparison failure.\n");
                processor_cleanup(key, ciphered, hex_pub, hex_priv, text, copy, original);
                return -1;
        }

        processor_cleanup(key, ciphered, hex_pub, hex_priv, text, copy, original);
        printf(" ... %i ... %i\n", iteration + 1, tlen);

        return 0;
}