Kivuos
Kivuos

Reputation: 13

EVP_DecryptUpdate() returns 0

For some reason, I am getting 0 from EVP_DecryptUpdate() function when I am passing 32 as its last parameter but when I changed it to 64 it returns 1. The buffer size is 32.

According to the documentation, I should get a 1 if the decrypt is successful.

#include <openssl/evp.h>
#include <stdio.h>
#include <string.h>

void dump_head(unsigned char *buf, size_t len)
{
    unsigned end, i;
    for (end = len; end > 0; end--)
        if (buf[end-1] != 0)
            break;
    printf("buf = {");
    for (i = 0; i < end; i++)
        printf(" %02hhx,", buf[i]);
    printf(" }\n");
}

int main(void)
{
    unsigned char key[] = "0123456789abcdef";
    unsigned char iv[] = "1234567887654321";
    unsigned char indata[32] = "0123456789abcdeffedcba9876543210";
    unsigned char buf[4096];
    unsigned pos;
    int cipher_len;
    EVP_CIPHER_CTX *ctx;

    ctx = EVP_CIPHER_CTX_new();
    EVP_CIPHER_CTX_init(ctx);
    EVP_CIPHER_CTX_set_padding(ctx, 0);
    EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);  
    printf("decrypt init: %d\n", EVP_DecryptInit_ex(ctx, EVP_aes_256_wrap_pad(), NULL, key, iv));

    printf("decrypt update: %d\n", EVP_DecryptUpdate(ctx, (unsigned char *)buf, &cipher_len, indata, 32));
        
    printf("Got %d\n", cipher_len);
    dump_head(buf, sizeof(buf));
        
    printf("Final!\n");
    memset(buf, 0, sizeof(buf));
    EVP_DecryptFinal_ex(ctx, buf, &cipher_len);
    printf("Got %d\n", cipher_len);
    dump_head(buf, sizeof(buf));

    return 0;
}

In both cases, I am getting cipher_len as 0. Are there some parameters I am missing?

Upvotes: 0

Views: 1176

Answers (0)

Related Questions