raddirad
raddirad

Reputation: 331

C++ 'invalid conversion from ‘void*’ to ‘crypto_aes_ctx*’

in order to get a feel for vectors and parallelisation I am currently trying to parallelize my programm with VC (http://code.compeng.uni-frankfurt.de/projects/vc). My programm is written in C, but VC requires to use C++. So i renamed my files to .cpp and tried to compile them. I get three compile errors which are all the same

error: invalid conversion from ‘void*’ to ‘crypto_aes_ctx*’

the code is the following

int crypto_aes_set_key(struct crypto_tfm *tfm, const uint8_t *in_key,
    unsigned int key_len)
{
    struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
    uint32_t *flags = &tfm->crt_flags;
    int ret;
    ret = crypto_aes_expand_key(ctx, in_key, key_len);
    if (!ret)
        return 0;

    *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
    return -EINVAL;
}

how can I fix this to get my code working with a c++ compiler?

Upvotes: 0

Views: 811

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

In C++ you may not assign a pointer of type void * to any other pointer of other type without explicit reinterpret casting.

If the error occured in statement

struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);

then you have to write

struct crypto_aes_ctx *ctx = reinterpret_cast<struct crypto_aes_ctx *>( crypto_tfm_ctx(tfm) );

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409422

Typing in C++ is stricter than C, so you have to use casting to tell the compiler what the void pointer actually is.

struct crypto_aes_ctx *ctx = (struct crypto_aes_ctx*) crypto_tfm_ctx(tfm);

Note that I'm using a C-style cast, in case you want to continue with the code in C. For C++ you would otherwise use reinterpret_cast.

Upvotes: 3

Related Questions