-2

I have a populated nested structure in memory. I want to change values of few members.

struct evp_cipher_ctx_st {
    const EVP_CIPHER *cipher;
    int somevalue;                
} /* EVP_CIPHER_CTX */ ;

Here the cipher element is also a structure and its like this :

struct evp_cipher_st {
    int nid;
    int block_size;
} /* EVP_CIPHER */ ;

I want to change the value of "block_size" element of the structure EVP_CIPHER nested inside EVP_CIPHER_CTX.

So I did this:

EVP_CIPHER_CTX  ctx;
EVP_CIPHER *MY_EVP_CYPHER;
MY_EVP_CYPHER = (EVP_CIPHER*)ctx.cipher;
MY_EVP_CYPHER -> block_size = 20;

My Program is crashing at this point :

    37:     MY_EVP_CYPHER = (EVP_CIPHER*)ctx.cipher;
00D914C5 8B 85 6C FF FF FF    mov         eax,dword ptr [ebp-94h]  
00D914CB 89 85 60 FF FF FF    mov         dword ptr [ebp-0A0h],eax  
    38:     MY_EVP_CYPHER -> block_size = 20;
00D914D1 8B 85 60 FF FF FF    mov         eax,dword ptr [ebp-0A0h]  
00D914D7 C7 40 04 11 00 00 00 mov         dword ptr [eax+4],11h  

Can someone point out the problem.

Dev.K.
  • 2,428
  • 5
  • 35
  • 49
  • Isn't `cipher` a pointer to `const EVP_CIPHER`? How do you plan to modify it? – CinCout Jun 09 '16 at 08:54
  • 4
    Is there a reason why you have a comment saying that the type of the struct is EVP_CIPHER_CTX, rather than just using `typedef struct {} EVP_CIPHER_CTX;`? – Lundin Jun 09 '16 at 08:58
  • @Lundin I'm using this function https://github.com/openssl/openssl/blob/aa6bb1352b1026b20a23b49da4efdcf171926eb0/crypto/evp/evp_locl.h#L24 – Dev.K. Jun 09 '16 at 09:00
  • @CinCout Edited the question.. – Dev.K. Jun 09 '16 at 09:04
  • 1
    @Dev.K. please modify your question and show us an [MCVE](http://stackoverflow.com/help/mcve). – Jabberwocky Jun 09 '16 at 09:06

1 Answers1

3

You are attempting to access ctx.cipher before you've allocated any memory to it. Since ctx.cipher is a pointer to an EVP_CIPHER you need to actually create an EVP_CIPHER and assign its address to ctx.cipher before you can use it.

Otherwise you are accessing an undetermined address in memory, which rarely ends well.

If the example you provided is how you're instantiating those structs, you should do something like the following:

EVP_CIPHER_CTX  ctx;
ctx.cipher = malloc(sizeof(*ctx.cipher));

EVP_CIPHER *MY_EVP_CYPHER;
MY_EVP_CYPHER = (EVP_CIPHER*)ctx.cipher;
MY_EVP_CYPHER -> block_size = 20;

Just don't forget to free that cipher once you're done with it.

sokkyoku
  • 2,161
  • 1
  • 20
  • 22