流對稱加密算法:TRIVIUM
TRIVIUM是一個面向硬件實現(xiàn)的流式密碼算法,其密鑰和初始化向量(Initialize Vector,IV)長度均為80位,能提供等同于AES128的安全性。所謂流式密碼算法一般通過密鑰生成一個“加密字節(jié)”,然后用這個加密字節(jié)和實際數(shù)據(jù)進行某種處理(如異或)生成密文。TRIVIUM作為一個密碼算法,結(jié)構(gòu)和實現(xiàn)極為簡單,且硬件消耗小,內(nèi)部僅維護36字節(jié)狀態(tài)。TRIVIUM至今未被攻破。注意,TRIVIUM最多加密2^64位數(shù)據(jù),如超出則必須更換密鑰和IV。
1 ?初始化
設TRIVIUM內(nèi)部狀態(tài)為S1...S288,初始化密鑰為K1...K80,初始化向量為IV1...IV80,則初始化過程為
(S1, S2, …, S93) = (K1, K2, … , K80, 0, 0, … 0)
(S94, S95, …, S177) = (IV1, IV2, … IV80, 0, 0,…, 0)
(S178, S179, …, S285, S286, S287, S288) = (0, 0, … 0, 1, 1, 1)
For iter = 1 to 4 * 288 do
? T1 = S66 + S91 * S92 + S93 + S171
?T2 = S162 + S175 * S176 + S177 + S264
?T3 = S243 + S286 * S287 + S288 + S69
?(S1, S2, … S93) = (T3, ?S1, … S92)
?(S94, S95 … S177) = (T1, S94, … S176)
?(S178, S179, … S288) = (T2, S178, … S287)
End For
2 ?生成加密位
設需生成N個加密位,第i個輸出加密位記為Zi,有
For iter = 1 to N do
T1 = S66 + S93
T2 = S162 + S177
T3 = S243 + S288
Zi = T1 + T2 + T3
T1 = S66 + S91 * S92 + S93 + S171
T2 = S162 + S175 * S176 + S177 + S264
T3 = S243 + S286 * S287 + S288 + S69
(S1, S2, … S93) = (T3, ?S1, … S92)
(S94, S95 … S177) = (T1, S94, … S176)
(S178, S179, … S288) = (T2, S178, … S287)
End For
結(jié)構(gòu)如下圖所示:

C++實例如下:
#include "CSTDIO"
#include "CSTRING"
#include "CSTDLIB"
#include "BITSET"
typedef unsigned char byte;
class TriviumCipher {
public:
TriviumCipher();
~TriviumCipher();
byte generateKeyByte();
private:
std::bitset<288> state;
bool tmp1, tmp2, tmp3;
int iter, iter2;
byte output;
};
int main(int argc, char **argv) {
TriviumCipher *cipher = new TriviumCipher();
byte arr[100];
for(int index = 0; index < 100; index++) {
arr[index] = cipher->generateKeyByte();
printf("%02x", arr[index]);
}
printf("\n");
return 0;
}
TriviumCipher::TriviumCipher() {
std::bitset<80> key;
std::bitset<80> iv;
this->state.set(1);
for(iter = 1; iter < 80; iter++) {
key[iter] = rand() % 2;
iv[iter] = rand() % 2;
}
for(iter = 0; iter < 80; iter++) {
this->state[iter] = key[iter];
}
for(iter = 80; iter < 93; iter++) {
this->state[iter] = 0;
}
for(iter = 93; iter < 173; iter++) {
this->state[iter] = iv[iter - 93];
}
for(iter = 173; iter < 177; iter++) {
this->state[iter] = 0;
}
for(iter = 285; iter < 288; iter++) {
this->state[iter] = 1;
}
for(iter = 0; iter < (4 * 288); iter++ ){
tmp1 = (this->state[65] + this->state[90] * this->state[91] + this->state[92] + this->state[170]) % 2;
tmp2 = (this->state[161] + this->state[174] * this->state[175] + this->state[176] + this->state[263]) % 2;
tmp3 = (this->state[242] + this->state[285] * this->state[286] + this->state[287] + this->state[68]) % 2;
for(iter2 = 92; iter2 > 0; iter2--) {
this->state[iter2] = this->state[iter2 - 1];
}
this->state[0] = tmp3;
for(iter2 = 176; iter2 > 93; iter2--) {
this->state[iter2] = this->state[iter2 - 1];
}
this->state[93] = tmp1;
for(iter2 = 287; iter2 > 177; iter2--) {
this->state[iter2] = this->state[iter2 - 1];
}
this->state[177] = tmp2;
}
}
TriviumCipher::~TriviumCipher() {
}
byte TriviumCipher::generateKeyByte() {
output = 0;
for(iter = 0; iter < 8; iter++) {
tmp1 = (this->state[65] + this->state[92]) % 2;
tmp2 = (this->state[161] + this->state[176]) % 2;
tmp3 = (this->state[242] + this->state[287]) % 2;
output = output << 1;
output |= (tmp1 + tmp2 + tmp3) % 2;
tmp1 = (tmp1 + this->state[90] * this->state[91] + this->state[170]) % 2;
tmp2 = (tmp2 + this->state[174] * this->state[175] + this->state[263]) % 2;
tmp3 = (tmp3 + this->state[285] * this->state[286] + this->state[68]) % 2;
for(iter2 = 92; iter2 > 0; iter2--) {
this->state[iter2] = this->state[iter2 - 1];
}
this->state[0] = tmp3;
for(iter2 = 176; iter2 > 93; iter2--) {
this->state[iter2] = this->state[iter2 - 1];
}
this->state[93] = tmp1;
for(iter2 = 287; iter2 > 177; iter2--) {
this->state[iter2] = this->state[iter2 - 1];
}
this->state[177] = tmp2;
}
return output;
}