2017-12-07 11 views
0

우리 모두는 mbedtls 라이브러리가 매우 가벼운 C 라이브러리라는 것을 알고 있습니다. 라이브러리를 사용하여 문자열을 암호화하고 싶습니다. 그래서 나는이 같은 기능을 가지고 :mbedtls를 사용하여 문자열을 암호화하는 방법은 무엇입니까?

aes_encrypt.h :

#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 

및 구현 :

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, &cipher_len)); 

그리고 I :

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(&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(&cipher_ctx, cipher_info)) != 0) 
{ 
    printf("Cipher Setup ERR!\n"); 
    goto EXIT; 
} 

if ((ret = mbedtls_cipher_setkey(&cipher_ctx, KEY, cipher_info->key_bitlen, MBEDTLS_ENCRYPT)) != 0) 
{ 
    printf("Cipher SetKey ERR!\n"); 
    goto EXIT; 
} 

// if ((ret = mbedtls_cipher_set_padding_mode(&cipher_ctx, 1)) != 0) { 
//  printf("Cipher SetPadding ERR!\n"); 
//  goto EXIT; 
// } 


if ((ret = mbedtls_cipher_reset(&cipher_ctx)) != 0) 
{ 
    printf("Cipher Reset ERR!\n"); 
    goto EXIT; 
} 

// encrypt 
if ((ret = mbedtls_cipher_update(&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(&cipher_ctx); 
    return ret; 
} 

내가 기능 울부 짖는 소리처럼 호출 다음과 같은 오류 메시지를 받으십시오 :

CIPHER - Decryption of block requires a full block 

누구든지 나를 도와 주며 오류 메시지의 의미를 설명 할 수 있습니까? 감사.

+2

는 당신이 암호화 원하는 것은 너무 짧은 것을 나타냅니다 선택하는 당신에게 달려 있습니다. 그것을 채 웁니다. – Yunnosch

+0

그러나 doc (mbedtls 헤더 파일)은 기본 채우기가 pkcs7이라고 말했습니다. 수동으로 설정해야합니까? – Andy

+0

Cipher.h에서 MBEDTLS_CIPHER_MODE_WITH_PADDING을 찾으십시오. –

답변

0

설명 :

CIPHER - Decryption of block requires a full block
Can anyone help me out and explain what is the meaning of the error message?

AES는이 시점에서 128 비트 블록을 암호화하도록 구현 된 수단 block cipher이다. 더 이상은 아니지만 덜합니다.

소스 데이터가 단일 블록보다 짧은 경우 데이터를 필요한 길이로 연장하려면 padding을 사용해야합니다.

일반적으로 모든 길이의 데이터를 암호화하기 위해 우리는 mode of operation을 사용합니다.

여전히 암호화가 안전하지 않습니다. IV (salt)와 authentication tag이 필요합니다.

We all know that mbedtls library is a very lightweight c library

분명히 mbedtls 이미 작업의 여러 모드를 지원, 그것은 "전체 블록을"필요

+0

나는 AES의 이론을 이해한다. 내가 모르는 것은 mbedtls 라이브러리에서 패딩을 설정하는 방법이다. mbedtls를 사용할 때 문자열을 한 번만 암호화하고 다음 번에 문자열의 다음 블록을 암호화해야합니까? – Andy

+0

@Andy 나는 관리자에게 mbedtls의 내부를 알지 못한다. 그러나 나는 이것에 대해 살펴볼 것이다. 이것은 * 작동 모드 *가 작동하는 곳이다. (보안을 유지하기 위해 블록을 암호화하는 방법). 대부분의 라이브러리는 이러한 모드를 즉시 사용할 수 있도록 구현하므로 세부 사항을 신경 쓸 필요가 없으며 모든 길이의 데이터를 암호화 할 수 있습니다. * mbedtls *가 자체적으로 수행한다고 가정합니다. 매개 변수를 모르겠습니다. 내가 그들을 발견하면, 나는 그들을 포함시킬 것이다. – gusto2