ページ 11

Key-Expansionを作りたい。

Posted: 2011年10月21日(金) 14:36
by ななみ
AES暗号候補のMARSをC言語で制作中です。
この擬似コードを元にC言語でプログラムを作ってみました。
<擬似コード>
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

以下が私が作ったプログラミングです。
1 #include<stdio.h>
2 int Sbox(int k);
3 int main(void){
4 unsigned int B[4]={0xa4a8b57b,0x5b5d193b,0xc8a8309b,0x73f9a978}; //Initialize B[]
5 unsigned int Key[40]={};
6 int T[]={0};
7 int M,a,b,c,d,e,f,i,j,l,w,r,p;
8 for(j=0;j<=3;j++){
9 for(i=0;i<=14;i++){
10 a=T[(i-7)%15];
11 b=T[(i-2)%15];
12 c=((a^b)<<3|(a^b)>>29);
13 d=c^(4*i+j);
14 T=T^d;
15 }
16 for(l=0;l<=3;l++){ //repeat four times
17 for(i=0;i<=14;i++){
18 e=T[(i-1)%15];
19 e=e&0x01FF; //low 9 bits
20 f=Sbox(e);
21 T[i]=((T[i]+f)<<9|(T[i]+f)>>23);
22 }
23 }
24
25 for(i=0;i<=9;i++){
26 Key[10*j+i]=T[4*i%15];
27 }
28 }
29 for(i=5;i<=35;i+=2){
30 j=Key[i]&0x03;
31 w=Key[i]|0x03;
32 //Generate a bit-mask M
33 M=1;
34
35 //Select apattern from the fixed table and rotate it
36 r=Key[i]&0x1F;
37 p=(B[j]<<r|B[j]>>(32-r));
38
39 //Modify K[i] with p under the control of the mask M
40 Key[i]=w^(p&M);
41 printf("resart=%4x\n",Key[i]);
42 }
43 return (0);
44 }
45

どうでしょうか??
分かりやすく教えていただけると助かります。

Re: Key-Expansionを作りたい。

Posted: 2011年10月21日(金) 15:11
by みけCAT
とりあえずコードが見にくいです。
コードはcodeタグで囲みましょう。