Key-Expansionを作りたい。改訂版
Posted: 2011年10月21日(金) 15:24
AES暗号候補のMARSをC言語で制作中です。
この擬似コードを元にC言語でプログラムを作ってみました。
先ほどのトピックが見にくかったためもう1度建て直しました。
削除方法が分からなかったためそのままですいません。
<擬似コード>
Key-Expansion(input:k[],n; output: K[])
1.//n is number pf words in the key buffer k[],(4≦n≦14)
2.// K []is the expanded key array, consisting of 40 words
3.// T []is a temporary array, consisting of 15 words
4. // B[] is a fixed table of four words
5. // Initialize B
6. B[] = {0xa4a8d57b; 0x5b5d193b; 0xc8a8309b; 0x73f9a978}
7. // Initialize T [] with key data
8.T[0...n-1]= k[0...n-1], T[n] = n, T[n + 1...14]= 0
9. // Four iterations, computing 10 words of K[] in each
10. for j = 0 to 3 do
11. for i = 0 to 14 do // Linear transformation
12. T= T ⊕((T[ i -7 mod 15 ]⊕T[ i -2 mod 15 ]) <<< 3) ⊕(4i + j)
13. repeat four times // Four stirring rounds
14. for i = 0 to 14 do
15. T = (T i + S[low 9 bits of T] )<<< 9
16. end-report
17. for i = 0 to 9 do
18. K [10j + i] = T[4i mod 15]
19. end-for
20. // Modify multiplication keys
21. for i = 5, 7, ... 35 do
22.j =least two bits of K
23.ω= K with both of the least two bits set to 1
24. // Generate a bit-mask M
25. Ml = 1 iff wl belongs to a sequence of ten consecutive 0’s or 1’s in w
26. and also 2 ≦ l ≦ 30 and wl-1 = wl = wl+1
27. // Select a pattern from the fixed table and rotate it
28. r =least five bits of K [i – 1] // Rotation amount
29. p=B[j]<<< r
30. // Modify K with p under the control of the mask M
31. K = w ⊕ (p ^ M)
32. end-for
以下が私が作ったプログラミングです。
どうでしょうか??
分かりやすく教えていただけると助かります。
この擬似コードを元にC言語でプログラムを作ってみました。
先ほどのトピックが見にくかったためもう1度建て直しました。
削除方法が分からなかったためそのままですいません。
<擬似コード>
Key-Expansion(input:k[],n; output: K[])
1.//n is number pf words in the key buffer k[],(4≦n≦14)
2.// K []is the expanded key array, consisting of 40 words
3.// T []is a temporary array, consisting of 15 words
4. // B[] is a fixed table of four words
5. // Initialize B
6. B[] = {0xa4a8d57b; 0x5b5d193b; 0xc8a8309b; 0x73f9a978}
7. // Initialize T [] with key data
8.T[0...n-1]= k[0...n-1], T[n] = n, T[n + 1...14]= 0
9. // Four iterations, computing 10 words of K[] in each
10. for j = 0 to 3 do
11. for i = 0 to 14 do // Linear transformation
12. T= T ⊕((T[ i -7 mod 15 ]⊕T[ i -2 mod 15 ]) <<< 3) ⊕(4i + j)
13. repeat four times // Four stirring rounds
14. for i = 0 to 14 do
15. T = (T i + S[low 9 bits of T] )<<< 9
16. end-report
17. for i = 0 to 9 do
18. K [10j + i] = T[4i mod 15]
19. end-for
20. // Modify multiplication keys
21. for i = 5, 7, ... 35 do
22.j =least two bits of K
23.ω= K with both of the least two bits set to 1
24. // Generate a bit-mask M
25. Ml = 1 iff wl belongs to a sequence of ten consecutive 0’s or 1’s in w
26. and also 2 ≦ l ≦ 30 and wl-1 = wl = wl+1
27. // Select a pattern from the fixed table and rotate it
28. r =least five bits of K [i – 1] // Rotation amount
29. p=B[j]<<< r
30. // Modify K with p under the control of the mask M
31. K = w ⊕ (p ^ M)
32. end-for
以下が私が作ったプログラミングです。
#include<stdio.h>
int Sbox(int k);
int main(void){
unsigned int B[4]={0xa4a8b57b,0x5b5d193b,0xc8a8309b,0x73f9a978}; //Initialize B[]
unsigned int Key[40]={};
int T[]={0};
int M,a,b,c,d,e,f,i,j,l,w,r,p;
for(j=0;j<=3;j++){
for(i=0;i<=14;i++){
a=T[(i-7)%15];
b=T[(i-2)%15];
c=((a^b)<<3|(a^b)>>29);
d=c^(4*i+j);
T[i]=T[i]^d;
}
for(l=0;l<=3;l++){ //repeat four times
for(i=0;i<=14;i++){
e=T[(i-1)%15];
e=e&0x01FF; //low 9 bits
f=Sbox(e);
T[i]=((T[i]+f)<<9|(T[i]+f)>>23);
}
}
for(i=0;i<=9;i++){
Key[10*j+i]=T[4*i%15];
}
}
for(i=5;i<=35;i+=2){
j=Key[i]&0x03;
w=Key[i]|0x03;
//Generate a bit-mask M
M=1;
//Select apattern from the fixed table and rotate it
r=Key[i]&0x1F;
p=(B[j]<<r|B[j]>>(32-r));
//Modify K[i] with p under the control of the mask M
Key[i]=w^(p&M);
printf("resart=%4x\n",Key[i]);
}
return (0);
}
分かりやすく教えていただけると助かります。