📄 rc6.c
字号:
/**********************************************************************
* AVR单片机速度优化 RC6 加密算法
* 在有128bytes RAM 的AVR单片机上执行 rc6 16/10/8(16 bit/10 rounds/8 bytes keys)
* 对多数代码进行了 C 语言优化,对数据相关循环移位,模乘等用ASM优化
* 在4MHz无乘法器的AVR上得到平均 1172 Bytes/s的加解密速度。
* 编译器: AVR-GCC
* 执行速度:
* key_setup 8224 时钟周期
* encrypt 2766 时钟周期/分组(64bit)
* decrypt 2697 时钟周期/分组(64bit)
* 平均 2731 时钟周期/分组(64bit)
*
* 2002 孔卫红 netvideo1@sina.com http://netvideo.yeah.net
*
*/
/* rc6 (TM)
* Unoptimized sample implementation of Ron Rivest's submission to the
* AES bakeoff.
*
* Salvo Salasio, 19 June 1998
*
* Intellectual property notes: The name of the algorithm (RC6) is
* trademarked; any property rights to the algorithm or the trademark
* should be discussed with discussed with the authors of the defining
* paper "The RC6(TM) Block Cipher": Ronald L. Rivest (MIT),
* M.J.B. Robshaw (RSA Labs), R. Sidney (RSA Labs), and Y.L. Yin (RSA Labs),
* distributed 18 June 1998 and available from the lead author's web site.
*
* This sample implementation is placed in the public domain by the author,
* Salvo Salasio. The ROTL and ROTR definitions were cribbed from RSA Labs'
* RC5 reference implementation.
*/
//#include <stdio.h>
/* RC6 is parameterized for w-bit words, b bytes of key, and
* r rounds. The AES version of RC6 specifies b=16, 24, or 32;
* w=32; and r=20.
*/
#include "rc6a.h"
#define w 16 /* word size in bits */
#define r 10 /* based on security estimates */
#define b 8
#define P16 0xb7e1 /* Magic constants for key setup */
#define Q16 0x9e37
/* derived constants */
#define bytes (w / 8) /* bytes per word */
#define c ((b + bytes - 1) / bytes) /* key in words, rounded up */
#define R24 (2 * r + 4)
#define lgw 4 /* log2(w) -- wussed out */
/* Rotations */
//#define ROTL(x,y) (((x)<<(y&(w-1))) | ((x)>>(w-(y&(w-1)))))
//#define ROTR(x,y) (((x)>>(y&(w-1))) | ((x)<<(w-(y&(w-1)))))
#define ROTLFIX(x) \
__asm__ __volatile__ ( \
"bst %B0,7" "\n\t" \
"rol %A0" "\n\t" \
"rol %B0" "\n\t" \
"bld %A0,0" "\n\t" \
"bst %B0,7" "\n\t" \
"rol %A0" "\n\t" \
"rol %B0" "\n\t" \
"bld %A0,0" "\n\t" \
"bst %B0,7" "\n\t" \
"rol %A0" "\n\t" \
"rol %B0" "\n\t" \
"bld %A0,0" "\n\t" \
:"=r" (x) \
:"0" (x) \
)
#define ROTLFIX4(x) \
__asm__ __volatile__ ( \
"swap %B0" "\n\t" \
"mov r18,%B0" "\n\t" \
"andi r18,0x0f" "\n\t" \
"andi %B0,0xf0" "\n\t" \
"swap %A0" "\n\t" \
"mov r19,%A0" "\n\t" \
"andi r19,0x0f" "\n\t" \
"andi %A0,0xf0" "\n\t" \
"add %A0,r18" "\n\t" \
"add %B0,r19" "\n\t" \
:"=r" (x) \
:"0" (x) \
:"r18","r19" \
)
unsigned int S[R24]; /* Key schedule */
void rc6_key_setup(unsigned int *L)
{
unsigned char i;
unsigned int A, B;
unsigned int *ps=S;
*ps = P16;
for (i = 0; i < 2 * r + 3; i++){
unsigned int tmp=*ps++;
*ps=tmp+Q16;
}
A = B = i = 0;
ps=S;
{unsigned char j=0,s;
unsigned int *pl=L;
for (s = 0; s < 3*R24; s++)
{
{
unsigned int xx;
xx=*ps+(A+B);
ROTLFIX(xx);
*ps=xx;
}
A=*ps;
B+=A;
B = *pl = rotlw(*pl + B, B);
if(i==(R24-1)){
i=0;
ps=S;
}else{
i++;
ps++;
}
if(j==(c-1)){
j=0;
pl=L;
}else{
j++;
pl++;
}
}
}
}
void rc6_block_encrypt(unsigned int *pt)
{
unsigned int A, B, C, D;
A = pt[0];
B = pt[1];
C = pt[2];
D = pt[3];
unsigned int *ps=S;
B+=*ps++;
D+=*ps++;
for(unsigned char i = 0; i < r; i++)
{
unsigned int t, u;
t = B+(w_square_mod(B)<<1);
ROTLFIX4(t);
u = D+(w_square_mod(D)<<1);
ROTLFIX4(u);
A = rotlw(A ^ t, u) + *ps++;
C = rotlw(C ^ u, t) + *ps++;
t = A;
A = B;
B = C;
C = D;
D = t;
}
A += *ps++;
C += *ps;
pt[0] = A;
pt[1] = B;
pt[2] = C;
pt[3] = D;
}
void rc6_block_decrypt(unsigned int *ct)
{
unsigned int A, B, C, D;
A = ct[0];
B = ct[1];
C = ct[2];
D = ct[3];
unsigned int *ps=S+(2 * r + 3);
C-=*ps--;
A-=*ps--;
for (unsigned char i = 0; i<r; i++)
{
unsigned int t,u;
t = D;
D = C;
C = B;
B = A;
A = t;
u = D+(w_square_mod(D)<<1);
ROTLFIX4(u);
t = B+(w_square_mod(B)<<1);
ROTLFIX4(t);
C = rotrw(C - *ps--, t) ^ u;
A = rotrw(A - *ps--, u) ^ t;
}
D -= *ps--;
B -= *ps;
ct[0] = A;
ct[1] = B;
ct[2] = C;
ct[3] = D;
}
/****************************************************************************
* Test prog
*/
int
main(void)
{
unsigned int ct[4],key[4];
key[0]=ct[0]=0x1234;
key[0]=ct[1]=0x5678;
key[0]=ct[2]=0x9abc;
key[0]=ct[3]=0xdef0;
rc6_key_setup(key);
rc6_block_encrypt(ct);
rc6_block_decrypt(ct);
for(;;){
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -