0

试图了解 Brian Gladman 博士的 AES-CTR 实施。(http://gladman.plushost.co.uk/oldsite/AES/aes-src-12-09-11.zip aes_modes.c 第 849 行以后)我阅读了 CTR 算法,但这个实现让我很困惑。我看不出它如何与 CTR 模式算法匹配。

if(b_pos) 代码部分是做什么用的?

我也不明白条件 b_pos < AES_BLOCK_SIZE && len

因为 (AES_BLOCK_SIZE && len) 在 len > 0 时始终为 1。AES_BLOCK_SIZE 为 16。

    if(b_pos)
    {
        memcpy(buf, cbuf, AES_BLOCK_SIZE);
        if(aes_ecb_encrypt(buf, buf, AES_BLOCK_SIZE, ctx) != EXIT_SUCCESS)
            return EXIT_FAILURE;

        while(b_pos < AES_BLOCK_SIZE && len)
        {
            *obuf++ = *ibuf++ ^ buf[b_pos++];
            --len;
        }

        if(len)
            ctr_inc(cbuf), b_pos = 0;
    }





AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
            int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx ctx[1])
{   unsigned char   *ip;
    int             i, blen, b_pos = (int)(ctx->inf.b[2]);
    uint_8t buf[BFR_LENGTH];
    if(b_pos)
    {
        memcpy(buf, cbuf, AES_BLOCK_SIZE);
        if(aes_ecb_encrypt(buf, buf, AES_BLOCK_SIZE, ctx) != EXIT_SUCCESS)
            return EXIT_FAILURE;

        while(b_pos < AES_BLOCK_SIZE && len)
        {
            *obuf++ = *ibuf++ ^ buf[b_pos++];
            --len;
        }

        if(len)
            ctr_inc(cbuf), b_pos = 0;
    }
    while(len)
    {
        blen = (len > BFR_LENGTH ? BFR_LENGTH : len), len -= blen;

        for(i = 0, ip = buf; i < (blen >> 4); ++i)
        {
            memcpy(ip, cbuf, AES_BLOCK_SIZE);
            ctr_inc(cbuf);
            ip += AES_BLOCK_SIZE;
        }

        if(blen & (AES_BLOCK_SIZE - 1))
            memcpy(ip, cbuf, AES_BLOCK_SIZE), i++;

        if(aes_ecb_encrypt(buf, buf, i * AES_BLOCK_SIZE, ctx) != EXIT_SUCCESS)
            return EXIT_FAILURE;

        i = 0; ip = buf;
            while(i + AES_BLOCK_SIZE <= blen)
            {
                obuf[ 0] = ibuf[ 0] ^ ip[ 0]; obuf[ 1] = ibuf[ 1] ^ ip[ 1];
                obuf[ 2] = ibuf[ 2] ^ ip[ 2]; obuf[ 3] = ibuf[ 3] ^ ip[ 3];
                obuf[ 4] = ibuf[ 4] ^ ip[ 4]; obuf[ 5] = ibuf[ 5] ^ ip[ 5];
                obuf[ 6] = ibuf[ 6] ^ ip[ 6]; obuf[ 7] = ibuf[ 7] ^ ip[ 7];
                obuf[ 8] = ibuf[ 8] ^ ip[ 8]; obuf[ 9] = ibuf[ 9] ^ ip[ 9];
                obuf[10] = ibuf[10] ^ ip[10]; obuf[11] = ibuf[11] ^ ip[11];
                obuf[12] = ibuf[12] ^ ip[12]; obuf[13] = ibuf[13] ^ ip[13];
                obuf[14] = ibuf[14] ^ ip[14]; obuf[15] = ibuf[15] ^ ip[15];
                i += AES_BLOCK_SIZE;
                ip += AES_BLOCK_SIZE;
                ibuf += AES_BLOCK_SIZE;
                obuf += AES_BLOCK_SIZE;
            }

        while(i++ < blen)
            *obuf++ = *ibuf++ ^ ip[b_pos++];
    }
    ctx->inf.b[2] = (uint_8t)b_pos;
    return EXIT_SUCCESS;
}
4

1 回答 1

1

CTR 模式使 AES 像流密码一样工作。加密和解密时,对数据长度没有限制,尤其是在块对齐方面(就像其他模式一样,如 CBC)。

该函数aes_ctr_crypt()确实可以使用ibuf任意长度的输入数据 ( ) 调用。

问题在于,对于每次调用,函数都需要记住上一次的两件事:

  1. 当前计数器块的值(即参数cbuf
  2. 到目前为止它在当前计数器块中消耗了多少字节(这是保存在参数中ctx并复制到局部变量中的信息b_pos)。

b_pos因此是密钥流中的位置模 16。

换句话说,当函数完成对某条数据的加密而没有消耗最后一个计数器块中的所有 16 个字节时,它会留下b_pos一个值0<b_pos<16,以便函数可以在下一次调用时从那里继续。值表示前一个计数器块0中的所有字节都已用完,是时候调用计数器函数了。

关于b_pos < AES_BLOCK_SIZE && len您应该将其阅读为:

(b_pos < AES_BLOCK_SIZE) && len

因为运算符优先级。循环终止要么是因为您用完了“剩余的”密钥流字节(左手),要么是因为您用完了明文数据(右手)。

于 2012-09-13T20:22:12.603 回答