|
@@ -9,8 +9,7 @@ |
|
|
#include <openssl/err.h> |
|
|
#include <openssl/err.h> |
|
|
|
|
|
|
|
|
static Result_t crypt( CryptoAttribs_t* attribs, |
|
|
static Result_t crypt( CryptoAttribs_t* attribs, |
|
|
const Key_t* const key, |
|
|
|
|
|
CryptoOperation operation ) |
|
|
|
|
|
|
|
|
const Key_t* const key) |
|
|
{ |
|
|
{ |
|
|
assert(key != NULL); |
|
|
assert(key != NULL); |
|
|
assert(attribs != NULL); |
|
|
assert(attribs != NULL); |
|
@@ -31,7 +30,7 @@ static Result_t crypt( CryptoAttribs_t* attribs, |
|
|
|
|
|
|
|
|
OP_CHECK( |
|
|
OP_CHECK( |
|
|
EVP_CipherInit_ex( &ctx, EVP_aes_128_ecb(), NULL, key->key, NULL, |
|
|
EVP_CipherInit_ex( &ctx, EVP_aes_128_ecb(), NULL, key->key, NULL, |
|
|
(operation == kEncrypt ? 1 : 0)) ); |
|
|
|
|
|
|
|
|
(attribs->operation == kEncrypt ? 1 : 0)) ); |
|
|
OP_CHECK( EVP_CIPHER_CTX_iv_length(&ctx) == 0); |
|
|
OP_CHECK( EVP_CIPHER_CTX_iv_length(&ctx) == 0); |
|
|
EVP_CIPHER_CTX_set_padding(&ctx, 0); |
|
|
EVP_CIPHER_CTX_set_padding(&ctx, 0); |
|
|
OP_CHECK( EVP_CipherUpdate(&ctx, &out[ret], &ret, attribs->input, attribs->input_len) ); |
|
|
OP_CHECK( EVP_CipherUpdate(&ctx, &out[ret], &ret, attribs->input, attribs->input_len) ); |
|
@@ -83,7 +82,8 @@ Result_t cbc_decrypt( |
|
|
ecb_attribs.input_len = key->len; |
|
|
ecb_attribs.input_len = key->len; |
|
|
|
|
|
|
|
|
// 1. Decrypt |
|
|
// 1. Decrypt |
|
|
if( crypt(&ecb_attribs, key, kDecrypt) != Result_OK ) |
|
|
|
|
|
|
|
|
ecb_attribs.operation = kDecrypt; |
|
|
|
|
|
if( crypt(&ecb_attribs, key) != Result_OK ) |
|
|
return Result_Error; |
|
|
return Result_Error; |
|
|
|
|
|
|
|
|
// 2. Xor IV |
|
|
// 2. Xor IV |
|
@@ -145,7 +145,8 @@ Result_t cbc_encrypt( |
|
|
ecb_attribs.input_len = bs; |
|
|
ecb_attribs.input_len = bs; |
|
|
|
|
|
|
|
|
// 2. Encrypt |
|
|
// 2. Encrypt |
|
|
if( crypt(&ecb_attribs, key, kEncrypt) != Result_OK ) |
|
|
|
|
|
|
|
|
ecb_attribs.operation = kEncrypt; |
|
|
|
|
|
if( crypt(&ecb_attribs, key) != Result_OK ) |
|
|
return Result_Error; |
|
|
return Result_Error; |
|
|
|
|
|
|
|
|
// Encrypted block is my new IV |
|
|
// Encrypted block is my new IV |
|
@@ -162,7 +163,13 @@ Result_t cbc_encrypt( |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Result_t ecb_encrypt( CryptoAttribs_t* attribs, const Key_t* const key ) |
|
|
Result_t ecb_encrypt( CryptoAttribs_t* attribs, const Key_t* const key ) |
|
|
{ return crypt(attribs, key, kEncrypt); } |
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
attribs->operation = kEncrypt; |
|
|
|
|
|
return crypt(attribs, key); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Result_t ecb_decrypt( CryptoAttribs_t* attribs, const Key_t* const key ) |
|
|
Result_t ecb_decrypt( CryptoAttribs_t* attribs, const Key_t* const key ) |
|
|
{ return crypt(attribs, key, kDecrypt); } |
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
attribs->operation = kDecrypt; |
|
|
|
|
|
return crypt(attribs, key); |
|
|
|
|
|
} |