title: how to encrypt a string in aes waywith mbedtls?
body: <p>We all know that mbedtls library is a very lightweight c library. I want to use the library to encrypt a string. So I have a function like this:</p>
  
  <p>aes_encrypt.h:</p>
  
  <pre><code>#ifndef AES_ENCRYPT_H
  #define AES_ENCRYPT_H
  
  #define BOOL int
  #define TRUE 1
  #define FALSE 0
  
  extern const unsigned char key[16];
  
  BOOL ENC_STR(unsigned char *plain, size_t plain_len, 
               unsigned char *cipher, size_t *cipher_len);
  #endif
  </code></pre>
  
  <p>and the implementation:</p>
  
  <pre><code>const unsigned char KEY[16] = { 0x00, 0x01, 0x02, 0x03,
                                  0x04, 0x05, 0x06, 0x07, 
                                  0x08, 0x09, 0x0A, 0x0B, 
                                  0x0C, 0x0D, 0x0F, 0xA0 };
  
  BOOL ENC_STR(unsigned char *plain, size_t plain_len, unsigned char *cipher, size_t *cipher_len)
  {
  BOOL ret = FALSE;
  
  // Prepare the cipher context
  const mbedtls_cipher_info_t *cipher_info;
  mbedtls_cipher_context_t cipher_ctx;
  mbedtls_cipher_init(&amp;cipher_ctx);
  
  if ((cipher_info = mbedtls_cipher_info_from_type(AES_PARM)) == NULL)
  {
      printf("Cipher Info ERR!\n");
      ret = -1;
      goto EXIT;
  }
  
  if ( (ret = mbedtls_cipher_setup(&amp;cipher_ctx, cipher_info)) != 0)
  {
      printf("Cipher Setup ERR!\n");
      goto EXIT;
  }
  
  if ( (ret = mbedtls_cipher_setkey(&amp;cipher_ctx, KEY, cipher_info-&gt;key_bitlen, MBEDTLS_ENCRYPT)) != 0)
  {
      printf("Cipher SetKey ERR!\n");
      goto EXIT;
  }
  
  // if ( (ret = mbedtls_cipher_set_padding_mode(&amp;cipher_ctx, 1)) != 0) {
  //     printf("Cipher SetPadding ERR!\n");
  //     goto EXIT;
  // }
  
  
  if ( (ret = mbedtls_cipher_reset(&amp;cipher_ctx)) != 0)
  {
      printf("Cipher Reset ERR!\n");
      goto EXIT;
  }
  
  // encrypt
  if ((ret = mbedtls_cipher_update(&amp;cipher_ctx, plain, plain_len, cipher, cipher_len)) != 0) {
      printf("Cipher Update ERR!\n");
      goto EXIT;
  }
  
  EXIT:
      if (ret != TRUE) {
          char buf[1024] = {0};
          mbedtls_strerror(ret, buf, 1024);
          printf("Error Msg:\t%s\n", buf);
      }
  
      mbedtls_cipher_free(&amp;cipher_ctx);
      return ret;
  }
  </code></pre>
  
  <p>I call the function like bellow:</p>
  
  <pre><code>unsigned char *plain = (unsigned char*)"hello world";
  size_t plain_len = 12;
  unsigned char cipher[128] = {0};
  size_t cipher_len = -1;
  printf("the encrypt result is:\t%d\n", ENC_STR(plain, plain_len, cipher, &amp;cipher_len));
  </code></pre>
  
  <p>And I get the err msg like below:</p>
  
  <pre><code>CIPHER - Decryption of block requires a full block
  </code></pre>
  
  <p>Can anyone help me out and explain what is the meaning of the error message? Thanks.</p>