LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2018, 04:25 PM   #1
DeusDingo
LQ Newbie
 
Registered: Jun 2018
Posts: 10

Rep: Reputation: Disabled
EVP_EncryptUpdate crashes when using EVP_aes_256_ecb cryptography


when i try to encrypt i get:

Program received signal SIGSEGV, Segmentation fault.
0xb6e850c4 in EVP_EncryptUpdate () from /opt/ti-processor-sdk-linux-am437x-evm-04.03.00.05/targetNFS/usr/lib/libcrypto.so.1.0.0



some background:

I am developing on a Texas Instruments EVM board for the AM4378. I am new to linux and linux development. my dev machine is running ubuntu 16.04, my embedded linux board is running linux 4.9.69 (i believe it's TI's kernal but i am not really sure how it works yet). i am cross compiling with gnueabihf- and i am debugging using gdbserver and the code composer IDE (i'm not ready to develop entirely command line yet). everything is written in C. the libraries i have linked in are "ssl" and "crypto". i am sure i forgot something

i found the code for AES as an example project in our SDK, so i modified it to be what we needed. when i left it at cbc cryptography everything seemed to be working fine, when i switched it to ebc (and nothing else) it crashed on me.

i found this website and it doesn't seem to show anything different in the way i am doing things:

https://github.com/rockyxshen/block_...er/aescipher.c



so am i missing something in the setup? something else entirely? there isn't a whole lot of documentation on how any of this works that i could find so i am a bit at a loss.

here is my code: (the key comes from somewhere else, 32 bytes)
Code:
EVP_CIPHER_CTX en, de;

void aes_init(void)
{
/****************Enabling use of a hardware engine******************/
    ENGINE *e;

    ENGINE_load_builtin_engines();
    if (!(e = ENGINE_by_id("cryptodev")))
        fprintf(stderr, "Error finding specified ENGINE\n");
    else if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
        fprintf(stderr, "Error using ENGINE\n");
    else
        fprintf(stderr, "Engine successfully enabled\n");
/*******************************************************************/



  EVP_CIPHER_CTX_init(&en);
  EVP_EncryptInit_ex(&en, EVP_aes_256_ecb(), e, AesKey, NULL);  //sets up cipher context e_ctx for encryption with aes_256_ecb cipher type
  EVP_CIPHER_CTX_init(&de);
  EVP_DecryptInit_ex(&de, EVP_aes_256_ecb(), e, AesKey, NULL);  //sets up cipher context d_ctx for decryption with aes_256_ecb cipher type

 /* Release the structural reference from ENGINE_by_id() */
 ENGINE_free(e);
}

/********************************************************************
*   AES encryption function
*********************************************************************/
int aes_encrypt(unsigned char *input, unsigned char *output, unsigned int len)
{
  int c_len = 0;
  int ret_len = 0;
  int EncLen = 0;

  EVP_EncryptInit_ex(&en, NULL, NULL, NULL, NULL);

  //encryption works on factors of 16, if there are any stragglers we have to round up to a x16 value
  if((len & 0x0F) != 0)
  {
      EncLen = (len & 0xFFF0) + 0x10;
  }
  else
  {
      EncLen = len;
  }

  EVP_EncryptUpdate(&en, output, &c_len, input, EncLen);
  ret_len = c_len;

  return ret_len;
}


/********************************************************************
*   AES decryption function
*********************************************************************/
int aes_decrypt(unsigned char *input, unsigned char *output, unsigned int len)
{
  int c_len = 0;
  int ret_len = 0;

  EVP_DecryptInit_ex(&de, NULL, NULL, NULL, NULL);

  EVP_DecryptUpdate(&de, output, &c_len, input, len);
  ret_len = c_len;

  return ret_len;
}

/*********************************************************************
 *
 *********************************************************************/
void aes_close(void)
{
    EVP_CIPHER_CTX_cleanup(&en);
    EVP_CIPHER_CTX_cleanup(&de);
}
if none of this gets answered does anyone know if i need the additional "init" function calls before update and if i need the "final" function calls? i can't quite figure out what they do. the plan for this stuff is to constantly be encrypting and decrypting data on an open connection so it isn't planned to be a "one and done" deal.

thank you for any help you can give me
 
Old 06-14-2018, 11:23 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
If you grow up expected input length parameter to EVP_EncryptUpdate() I bet there is a risk that this function will search for things outside input bounds
 
Old 06-15-2018, 01:32 PM   #3
DeusDingo
LQ Newbie
 
Registered: Jun 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
@keefaz yeah i'm not entirely sure what you mean but it crashes the first time i run it trying to encrypt 16 bytes
 
Old 06-15-2018, 02:14 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
EncLen variable should contain input exact length value, no more
 
Old 06-15-2018, 02:34 PM   #5
DeusDingo
LQ Newbie
 
Registered: Jun 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
yeah, it does. i am not busting input or output. i have it set to encrypt 16 bytes input (which is a 32 byte buffer) and the output buffer is 32 bytes
 
Old 06-16-2018, 12:32 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,383

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
Quote:
Originally Posted by DeusDingo View Post
Code:
int aes_encrypt(unsigned char *input, unsigned char *output, unsigned int len)
{
  int c_len = 0;
  [...]
  EVP_EncryptUpdate(&en, output, &c_len, input, EncLen);
I'm not familiar with this EVP api, but I notice that the other code you linked to sets the output buffer length to a non-zero value on the way in, perhaps this is significant.

Code:
unsigned char *aes_encrypt(EVP_CIPHER_CTX *e, unsigned char *plaintext, int *len)
{
    int c_len = *len + AES_BLOCK_SIZE, f_len = 0;
    [...]
    EVP_EncryptUpdate(e, ciphertext, &c_len, plaintext, *len);
Otherwise, try running bt in gdb after you segfault, that might give some more hints about what is going wrong.
 
Old 06-18-2018, 10:21 AM   #7
DeusDingo
LQ Newbie
 
Registered: Jun 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
that is a good catch ntubski, i completely glossed over that. however, it did not change the outcome. i still get SIGSEGV, Segmentation fault. thank you for looking at my post, though
 
Old 06-18-2018, 12:59 PM   #8
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
Don't know if this is your problem or not, but if you are using openssl 1.1 the api has changed a little. You have to get a EVP_CIPHER_CTX pointer with EVP_CIPHER_CTX_new() and pass the pointer to your functions.
Code:
EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
https://www.openssl.org/docs/man1.1.....html#EXAMPLES
 
Old 06-18-2018, 01:13 PM   #9
DeusDingo
LQ Newbie
 
Registered: Jun 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
thanks norobro, unfortunately it did not fix it. thank you for trying to help, though.
 
Old 06-19-2018, 10:07 AM   #10
DeusDingo
LQ Newbie
 
Registered: Jun 2018
Posts: 10

Original Poster
Rep: Reputation: Disabled
so if i take out the cryptography engine it runs just fine. i guess our processor doesn't support that format.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cryptography and Linux zinnia Linux - Newbie 8 02-17-2016 10:23 AM
what is the cryptography amitrathore General 2 01-03-2012 07:25 AM
Slackware+Cryptography netpumber Linux - Software 2 01-12-2009 04:30 PM
Cryptography wwnexc Linux - Security 1 03-29-2006 08:21 PM
Cryptography Libraries makk0 Programming 4 09-21-2004 10:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration