【问题标题】:AES CTR 256 Encryption Mode of operation on OpenSSLOpenSSL 上的 AES CTR 256 加密操作模式
【发布时间】:2011-03-09 16:24:54
【问题描述】:

我是 OpenSSL 的新手,谁能告诉我如何从 C 文件初始化 AES CTR 模式。我知道这是方法的签名,但我的参数有问题,文档不多,也没有一个清晰的例子,如何进行简单的加密。如果有人可以举例说明对此方法的调用,我将不胜感激。提前致谢!

void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out,
    const unsigned long length, const AES_KEY *key,
    unsigned char ivec[AES_BLOCK_SIZE],
    unsigned char ecount_buf[AES_BLOCK_SIZE],
    unsigned int *num);

您好 Caf,我非常感谢您的快速回答,它非常有用,并且无疑是我在网络上找到的最佳示例。我正在尝试打开一个长度不确定的文件,对其进行加密并使用生成的密文写入另一个文件,然后打开加密文件并恢复明文。我需要使用相当大的 MB 文件,因为我想对 CPU 的性能进行基准测试。但是我在解密时仍然遇到问题。不知何故,当解密相当大的 txt 文件(1504KB)时,它不会完全解密,我得到一半是明文,另一半仍然是加密的。我认为这可能与 iv 的大小或我调用柜台的方式有关。这是我到目前为止所拥有的:

#include <openssl/aes.h>
#include <stdio.h>
#include <string.h>

struct ctr_state { 
    unsigned char ivec[16];   
    unsigned int num; 
    unsigned char ecount[16]; 
}; 

FILE *fp;
FILE *rp;
FILE *op;
size_t count;   
char * buffer; 
AES_KEY key; 

int bytes_read, bytes_written;   
unsigned char indata[AES_BLOCK_SIZE]; 
unsigned char outdata[AES_BLOCK_SIZE];  
unsigned char ckey[] =  "thiskeyisverybad"; // It is 128bits though..
unsigned char iv[8] = {0};//This should be generated by RAND_Bytes I will take into    consideration your previous post
struct ctr_state state;   

int init_ctr(struct ctr_state *state, const unsigned char iv[8]){     
    state->num = 0; 
    memset(state->ecount, 0, 16);      
    memset(state->ivec + 8, 0, 8);  
    memcpy(state->ivec, iv, 8); 
} 

void encrypt(){ 
  //Opening files where text plain text is read and ciphertext stored      
  fp=fopen("input.txt","a+b");
  op=fopen("output.txt","w");
  if (fp==NULL) {fputs ("File error",stderr); exit (1);}   
  if (op==NULL) {fputs ("File error",stderr); exit (1);}      

  //Initializing the encryption KEY
  AES_set_encrypt_key(ckey, 128, &key); 

  //Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext  
 while (1) {     
    init_ctr(&state, iv); //Counter call
    bytes_read = fread(indata, 1, AES_BLOCK_SIZE, fp); 
    AES_ctr128_encrypt(indata, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num);    
    bytes_written = fwrite(outdata, 1, bytes_read, op); 
    if (bytes_read < AES_BLOCK_SIZE) 
    break; 
  }   

  fclose (fp); 
  fclose (op);
  free (buffer); 
}

void decrypt(){
  //Opening files where text cipher text is read and the plaintext recovered         
  rp=fopen("recovered.txt","w");
  op=fopen("output.txt","a+b");
  if (rp==NULL) {fputs ("File error",stderr); exit (1);}   
  if (op==NULL) {fputs ("File error",stderr); exit (1);} 

  //Initializing the encryption KEY
  AES_set_encrypt_key(ckey, 128, &key); 

  //Encrypting Blocks of 16 bytes and writing the output.txt with ciphertext   
  while (1) {     
    init_ctr(&state, iv);//Counter call
    bytes_read = fread(indata, 1, AES_BLOCK_SIZE, op);  
    AES_ctr128_encrypt(indata, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num); 
    bytes_written = fwrite(outdata, 1, bytes_read, rp); 
    if (bytes_read < AES_BLOCK_SIZE) 
    break; 
    }   
  fclose (rp); 
  fclose (op);
  free (buffer); 
}

int main(int argc, char *argv[]){  
  encrypt();  
  //decrypt(); 
  system("PAUSE");  
  return 0;
}

每个加密和解密函数在不同的运行中被调用,所以所有的东西总是用相同的值初始化。再次感谢您可以提前向我提供的提示和问候!!!

【问题讨论】:

  • 您的问题是您在每个块之后重新初始化计数器。这是错误的 - 在加密和解密中将 init_ctr() 调用移到 while() 循环之外。 indataoutdata 也不必是 AES_BLOCK_SIZE 长度 - 它们可以大得多。
  • 你应该使用AES_encrypt和朋友。这是一个纯软件实现,因此您不会享受硬件支持,如 AES-NI。您应该使用EVP_* 函数。请参阅 OpenSSL wiki 上的 EVP Symmetric Encryption and Decryption。事实上,您可能应该使用经过身份验证的加密,因为它提供 机密性和真实性。请参阅 OpenSSL wiki 上的 EVP Authenticated Encryption and Decryption
  • 如果你使用EVP_*函数,那么感兴趣的密码是EVP_aes_128_ctrEVP_aes_192_ctrEVP_aes_256_ctr

标签: c openssl aes


【解决方案1】:

通常,您会打算反复调用AES_ctr128_encrypt() 以发送具有相同密钥和IV 以及递增计数器的多条消息。这意味着您需要在调用之间跟踪 'ivec'、'num' 和 'ecount' 值 - 所以创建一个 struct 来保存这些值和一个初始化函数:

struct ctr_state {
    unsigned char ivec[16];  /* ivec[0..7] is the IV, ivec[8..15] is the big-endian counter */
    unsigned int num;
    unsigned char ecount[16];
};

int init_ctr(struct ctr_state *state, const unsigned char iv[8])
{
    /* aes_ctr128_encrypt requires 'num' and 'ecount' set to zero on the
     * first call. */
    state->num = 0;
    memset(state->ecount, 0, 16);

    /* Initialise counter in 'ivec' to 0 */
    memset(state->ivec + 8, 0, 8);

    /* Copy IV into 'ivec' */
    memcpy(state->ivec, iv, 8);
}

现在,当您开始与目的地通信时,您需要生成一个 IV 以使用和初始化计数器:

unsigned char iv[8];
struct ctr_state state;

if (!RAND_bytes(iv, 8))
    /* Handle the error */;

init_ctr(&state, iv);

然后您需要将 8 字节 IV 发送到目的地。您还需要从原始密钥字节初始化 AES_KEY

AES_KEY aes_key;

if (AES_set_encrypt_key(key, 128, &aes_key))
    /* Handle the error */;

您现在可以开始加密数据并将其发送到目的地,重复调用AES_ctr128_encrypt(),如下所示:

AES_ctr128_encrypt(msg_in, msg_out, msg_len, &aes_key, state->ivec, state->ecount, &state->num);

msg_in 是指向包含明文消息的缓冲区的指针,msg_out 是指向加密消息应该去的缓冲区的指针,msg_len 是消息长度。

解密是完全一样的,只是你不用RAND_bytes()生成IV——而是取另一方给你的值。

重要:

  1. 在加密过程中不要多次调用init_ctr()。计数器和 IV 必须在开始加密之前仅初始化一次

  2. 在任何情况下都不要试图在加密方面从RAND_bytes() 以外的任何地方获取 IV。不要将其设置为固定值;不要使用散列函数;不要使用收件人的姓名;不要从磁盘读取它。使用RAND_bytes() 生成它并将其发送到目的地。每当您从零计数器开始时,您必须从您以前从未使用过的全新 IV 开始。

  3. 如果您有可能在不更改 IV 和/或密钥的情况下发送 2**64 字节,则需要测试计数器是否溢出。

  4. 不要忽略错误检查。如果某个功能失败而您忽略了它,那么您的系统很可能(甚至很可能)看起来运行正常,但实际上运行完全不安全。

【讨论】:

  • 让我添加一个细节,当我使用这个时我逃脱了:num 参数是你在一个块中的字节数,而不是计数器。如果您正在加密数据包(例如),请始终将 state->num 设置为零并将您的计数器放入 iv 的高字节。
  • @Mike Elkins:确实 - 您可以将 numecount 视为 OpenSSL CTR 实现的不透明内部状态。在大多数情况下,不需要直接更改它们。
  • if (!AES_set_encrypt_key(key, 128, &amp;aes_key)) /* Handle the error */ 应该改为 if (AES_set_encrypt_key(key, 128, &amp;aes_key)) /* Handle the error */,因为 AES_set_encrypt_key 在成功时返回 0,请参阅 man.openbsd.org/AES_encrypt.3#RETURN_VALUES
【解决方案2】:

您的测试程序的基本问题似乎是fopen 调用的模式值不正确。我认为您需要将加密中的 fopen 调用更改为:

fp=fopen("input.txt","rb");
op=fopen("output.txt","wb");

还有那些解密到:

rp=fopen("recovered.txt","wb");
op=fopen("output.txt","rb");

另一件值得指出的事情是ckey 可能应该被声明为一个 32 字节(256 位)的缓冲区。确实,128 位加密仅使用密钥中的 16 字节数据。但是 OpenSSL 函数 AES_set_encrypt_key(至少在我使用的版本中)从该缓冲区读取 32 个字节。它只使用适当数量的字节,但确实会发生读取。这意味着如果缓冲区只有 16 字节,并且发生在与内存中不可读页面相邻的页面末尾,则会导致访问冲突。

哦 - 我刚刚注意到那里有一个对 free 的无关调用。 free(buffer); 调用无效,因为缓冲区从未分配过。我知道你的代码只是一个简单的测试,但是......好吧,我们是程序员,不能帮助自己。

【讨论】:

    猜你喜欢
    • 2017-08-08
    • 2020-01-20
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 2020-09-26
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多