AES CTR原理

AES CTR原理

AES CTR加解密相关描述:(摘自wiki,下面的原理图比较直观)

Counter (CTR)

CTR
Counter
Encryption parallelizable:Yes
Decryption parallelizable:Yes
Note: CTR mode (CM) is also known as integer counter mode  (ICM) and segmented integer counter  (SIC) mode

Like OFB, counter mode turns a block cipher into a stream cipher. It generates the next keystream block by encrypting successive values of a "counter". The counter can be any function which produces a sequence which is guaranteed not to repeat for a long time, although an actual increment-by-one counter is the simplest and most popular. The usage of a simple deterministic input function used to be controversial; critics argued that "deliberately exposing a cryptosystem to a known systematic input represents an unnecessary risk." By now, CTR mode is widely accepted, and problems resulting from the input function are recognized as a weakness of the underlying block cipher instead of the CTR mode. Along with CBC, CTR mode is one of two block cipher modes recommended by Niels Ferguson and Bruce Schneier.

CTR mode has similar characteristics to OFB, but also allows a random access property during decryption. CTR mode is well suited to operate on a multi-processor machine where blocks can be encrypted in parallel. Furthermore, it does not suffer from the short-cycle problem that can affect OFB.

Note that the nonce in this diagram is the same thing as the initialization vector (IV) in the other diagrams. The IV/nonce and the counter can be combined together using any lossless operation (concatenation, addition, or XOR) to produce the actual unique counter block for encryption.

  中文:(简单解释一下)

  计数模式也被称为整数计数模式以及分段块模式。

   计数模式和OFB有点相似,它将分块密文转行为流密码。计数模式通过对连续递增的counter加密产生keystream,counter可以被任意函数产生,但是必须保证它在相当长的一段时间内不重复,通常情况下,使用的是加1的方式(即操作一个block后counter加1)。

   下图中的nonce和IV具有相同的语义,即nonce=IV,

   计数模式可以用下图描述:

  用key将counter加密后得到ecounter,然后counter=counter+1,将明文与ecounter做异或运算.

 将递增后的counter作为下一次的counter,用key将counter加密后得到ecounter,然后counter=counter+1,将明文与ecounter做异或运算.

重复以上操作。


CTR encryption 2.svg


实现code:

AES_encrypt是一次ecb encrypt过程。
GETU32(p)已大端方式将一个4字节的char buffer的内容转为int

#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
# define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
# define GETU32(p) SWAP(*((u32 *)(p)))
# define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
#else
# define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] <<  8) ^ ((u32)(pt)[3]))
# define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >>  8); (ct)[3] = (u8)(st); }
#endif


/* NOTE: CTR mode is big-endian.  The rest of the AES code
 * is endian-neutral. */

/* increment counter (128-bit int) by 2^64 */
static void AES_ctr128_inc(unsigned char *counter) {
	unsigned long c;

	/* Grab 3rd dword of counter and increment */
#ifdef L_ENDIAN
	c = GETU32(counter + 8);
	c++;
	PUTU32(counter + 8, c);
#else
	c = GETU32(counter + 4);
	c++;
	PUTU32(counter + 4, c);
#endif

	/* if no overflow, we're done */
	if (c)
		return;

	/* Grab top dword of counter and increment */
#ifdef L_ENDIAN
	c = GETU32(counter + 12);
	c++;
	PUTU32(counter + 12, c);
#else
	c = GETU32(counter +  0);
	c++;
	PUTU32(counter +  0, c);
#endif

}

/* The input encrypted as though 128bit counter mode is being
 * used.  The extra state information to record how much of the
 * 128bit block we have used is contained in *num, and the
 * encrypted counter is kept in ecount_buf.  Both *num and
 * ecount_buf must be initialised with zeros before the first
 * call to AES_ctr128_encrypt().
 */
void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out,
	const unsigned long length, const AES_KEY *key,
	unsigned char counter[AES_BLOCK_SIZE],
	unsigned char ecount_buf[AES_BLOCK_SIZE],
	unsigned int *num) {

	unsigned int n;
	unsigned long l=length;

	assert(in && out && key && counter && num);
	assert(*num < AES_BLOCK_SIZE);

	n = *num;

	while (l--) {
		if (n == 0) {
			AES_encrypt(counter, ecount_buf, key);
			AES_ctr128_inc(counter);
		}
		*(out++) = *(in++) ^ ecount_buf[n];
		n = (n+1) % AES_BLOCK_SIZE;
	}

	*num=n;
}


如果你的程序需要支持decypt的第一个block不是从0开始,即void AES_ctr128_encrypt(const unsigned char *in, unsigned char *out,
const unsigned long length, const AES_KEY *key,
unsigned char counter[AES_BLOCK_SIZE],
unsigned char ecount_buf[AES_BLOCK_SIZE],
unsigned int *num)中的num>0,  num<AES_BLOCK_SIZE,

此时就需要对AES_ctr128_encrypt做一次封装


void AES_ctr128_encrypt_with_offset(const unsigned char *in, unsigned char *out,
const unsigned long length, const AES_KEY *key,
unsigned char iv[AES_BLOCK_SIZE],
unsigned int  block_offset)

{

       if(block_offset <0 || block_offset >= AES_BLOCK_SIZE)

      {

return;

       }

       unsigned char ecount_buf[AES_BLOCK_SIZE];

      unsigned char counter[AES_BLOCK_SIZE];

       memset(ecount_buf,0,AES_BLOCK_SIZE);

      memcpy(counter,iv,AES_BLOCK_SIZE)

        if(block_offset >0 && block_offset < AES_BLOCK_SIZE )

       {

AES_encrypt(counter, ecount_buf, key);
AES_ctr128_inc(counter);

      }

      int block_offset_cur = block_offset ;

     AES_ctr128_encrypt(in,out,length,key,&block_offset );

}

参考文献:http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation

http://www.opensource.apple.com/source/OpenSSL/OpenSSL-22/openssl/crypto/aes/aes_ctr.c


  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 纯C语言实现AES/CTR是一种使用纯C语言编写的方式来实现AES(Advanced Encryption Standard)的CTR(Counter)模式加密算法。 在C语言中实现AES/CTR算法需要对AES算法和CTR模式进行分别实现。 首先,需要实现AES算法,它是一种对称加密算法,要实现加密和解密功能。AES算法主要包括密钥扩展、轮密钥加、字节替换、行移位、列混淆等步骤。 其次,CTR模式是一种分块加密模式,它将明文分成若干块,并使用计数器加密每一块明文。CTR模式还需要实现一个计数器,用于生成每一块的加密密钥。 实现AES/CTR算法的步骤如下: 1. 实现AES算法的各个步骤,包括密钥扩展、轮密钥加、字节替换、行移位、列混淆等操作。这些操作可以用C语言中的位操作和数组操作实现。 2. 实现CTR模式的加密和解密过程。CTR模式需要实现一个计数器和密钥生成器。计数器用于生成每一块的加密密钥,密钥生成器用于生成密钥序列。 3. 将明文按照每一块的长度进行分割,并使用计数器生成每一块的加密密钥。然后,使用该密钥对每一块进行加密,可以用AES算法进行加密。 4. 对加密后的每一块密文进行拼接得到最终的密文。 5. 解密时,将密文按照每一块的长度进行分割,并使用计数器生成每一块的解密密钥。然后,使用该密钥对每一块进行解密,可以用AES算法进行解密。 6. 对解密后的每一块明文进行拼接得到最终的明文。 总之,纯C语言实现AES/CTR算法需要实现AES的各个步骤和CTR模式的加密和解密过程。通过理解和实现这些算法的原理,可以使用纯C语言编写出一个完整的AES/CTR加密算法。 ### 回答2: AES(Advanced Encryption Standard)是一种常用的对称加密算法,而CTR(Counter)是一种加密模式。要纯C语言实现AES/CTR,可以按照以下步骤操作: 1. 首先,需要实现AES算法中的加密和解密函数。可参考AES算法的标准实现。 2. 接着,需要实现CTR模式的加密和解密函数。CTR模式将加密算法作用于一个计数器产生的密钥流,然后与明文进行异或运算得到密文,解密时同理。可参考CTR模式的标准实现。 3. 在实现AESCTR的函数时,需要使用C语言提供的位运算等操作来操作字节和位。 要注意的是,纯C语言实现AES/CTR可能相对较慢,因为C语言不擅长处理位级别的操作。如果对性能有较高要求的话,可以考虑使用汇编语言或者专门的密码库来实现加密算法。 总结来说,纯C语言实现AES/CTR需要依次实现AES算法的加密和解密函数,以及CTR模式的加密和解密函数,以及使用位运算等操作来操作字节和位。 ### 回答3: AES(Advanced Encryption Standard)是一种常用的对称加密算法,CTR(Counter)是一种加密模式。在纯C语言中实现AES/CTR加密可以通过以下步骤完成: 首先,需要实现AES算法。AES算法基于分组密码,包括一系列轮函数和密钥扩展。可以使用C语言中的位操作来实现AES算法的各个步骤。 其次,需要实现CTR模式。CTR模式使用计数器作为输入,每次加密分组都会递增计数器的值。首先,需要定义一个计数器,然后将其与AES加密算法的输出进行异或操作,得到密文。 最后,编写一个主函数,用于读取明文和密钥,并调用上述的AESCTR函数来进行加密。加密过程中,主函数会将明文分组按顺序送入CTR函数,得到对应的密文。 需要注意的是,纯C语言实现AES/CTR加密需要注意算法的效率和安全性。为了提高性能,可以使用C语言中的位操作进行优化。而为了保证安全性,需要对密钥进行适当的处理,如进行密钥扩展和密钥安全性检查。 总结起来,实现纯C语言中AES/CTR加密需要实现AES算法和CTR模式,并编写一个主函数来调用这些函数完成加密过程。这样可以在C语言环境下实现对称加密,保证数据的机密性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值