2016-04-30 239 views
1

我有個密碼學作業,詢問爲LAT表計算爲衆多4×4的S-box。爲此我寫了一個簡單的C++程序。我將問題文本附加爲圖像。我不知道是否爲教練給了公式是LAT表計算的通用公式或他做了它自己。我的問題是我準備的軟件給出了全零的LAT表。我沒有這個公式的測試向量。我將附上下面的代碼。我將非常高興,如果有人誰知道線性近似表時會檢查程序,並告訴我是什麼問題。 (我查了一下它的工作原理確定比特轉換部分!)提前 謝謝..線性近似表計算SBOX

Ferda

enter image description here

回答

2

我想,這個問題可以在applyDotFunc()

你永遠不會使用第二種參數。我想,在value2ptr創作,你的目的是使用value2,而不是value1

int applyDotFunc(int value1, int value2) 
{ 
    int value1arr[4] = { 0 }; 
    int* value1ptr = get_bits(value1, 4); 
    value1arr[0] = value1ptr[0]; 
    value1arr[1] = value1ptr[1]; 
    value1arr[2] = value1ptr[2]; 
    value1arr[3] = value1ptr[3]; 

    int value2arr[4] = { 0 }; 
    int* value2ptr = get_bits(value1, 4); // <-- should be value2 ? 
    value2arr[0] = value2ptr[0]; 
    value2arr[1] = value2ptr[1]; 
    value2arr[2] = value2ptr[2]; 
    value2arr[3] = value2ptr[3]; 

---- 編輯 ----

請不要見怪,但我給你關於你的代碼的一些建議。

在開放爲了

1)你O內存小塊的分配很多(在gets_bits()),你永遠不會釋放它;如果你真的想用直接分配的內存(但你的情況是沒有必要的),記得要釋放它

2)儘量避免直接分配的內存和C風格的數組; 3)如果您真的需要直接在C++中分配內存,請使用new/delete並避免malloc()/free()。在C型陣列的情況下,不存在差別,但,分配對象,malloc()不能構造(和free()不能破壞)它們

4)你的get_Bits()/applyDotFunc()是過於複雜。給看看吧,你會看到applyDotFunc()回報1時value1 & value2是奇數高位,並返回0時value1 & value2是即使高位。這樣就可以避免陣列(假設通過value1 & value2),你可以在這個簡單的方式

int applyDotFunc (int valAnd) 
{ 
    int sumBits = 0; 

    for (int k = 0 ; k < 4 ; ++k) 
     sumBits += (0 != (valAnd & (1 << k))); 

    return sumBits & 1 ; 
} 

5)你用三個週期在findApprox時,你可以只用一個寫;它可以是

void findApprox() 
{ 
    int c, d, e; 

    for (c = 1 ; c < 16 ; ++c)  //output mask 
     for (d = 1 ; d < 16 ; ++d) //input mask 
     { 
     approxTable[d][c] = -8; // initialize to -8; so there is no need 
           // to subtract at the end 

     for(e = 0 ; e < 16 ; ++e)   
      approxTable[d][c] += applyDotFunc(e & d)^applyDotFunc(sBox[e] & c); // a += x is simpler than a = a + x 
     } 
} 

6)你確定approxTable指數是從1到15嗎?而不是從0到15?

7)showApprox()e是未使用

8)使用全局變量;這是不必要的,(恕我直言)危險。避免他們喜歡瘟疫。

p.s.:對不起我的英文不好

+0

嗨maxx66,我不寫C++代碼多年,這個代碼是爲一門功課,我會運行一次。所以我沒有盡力寫出完美的代碼。有人(我的導師)檢查了產生的LAT表,這是真的。這對我來說很重要。感謝您的時間與興趣 :) –

4

這裏是一個計算DES s盒的線性近似表的僞代碼。

int x_i =0, y_i = 0, y_j=0,x_j =0, parity_11=0; 
int temp=0; 
for(x_i=0;x_i<64;x_i++){ 
    for(y_i=0;y_i<16;y_i++){ 
     for(x_j=0;x_j<64;x_j++){ 
      y_j=sbox_1[x_j]; 
      y_j=y_j&y_i; 
      temp=x_j&x_i; 
      parity_11=(parity(temp)+parity(y_j))%2; 
      parity_x_y[x_i][y_i]+=parity_11; 
      } 
     } 
    } 

完整代碼,可以發現如下:

static const char sbox_1[] = { 
    14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 
    0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 
    4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 
    15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 
}; 
int parity(unsigned int x){ 
unsigned int count = 0, i, b = 1; 

for(i = 0; i < 8; i++){ 
    if(x & (b << i)){count++;} 
} 

if((count % 2)){return 1;} 

return 0; 
} 
int parity_x_y[64][16] ={}; 
void print_xor(){ 
int i=0,j=0; 
for(i=0;i<64;i++){ 
    j=0; 
    for(j=0;j<16;j++){ 
     printf("%d ",parity_x_y[i][j]); 
    } 
    printf("\n"); 
    } 

} 

void print_LAT(){ 
int x_i =0, y_i = 0, y_j=0,x_j =0, parity_11=0; 

int temp=0; 
print_xor(); 
for(x_i=0;x_i<64;x_i++){ 
    for(y_i=0;y_i<16;y_i++){ 
     for(x_j=0;x_j<64;x_j++){ 
      y_j=sbox_1[x_j]; 
      y_j=y_j&y_i; 
      temp=x_j&x_i; 
      parity_11=(parity(temp)+parity(y_j))%2; 
      parity_x_y[x_i][y_i]+=parity_11; 
      } 
     } 
    } 
    int j=0; 
print_xor(); 
} 

int main(){ 
    print_LAT(); 
    return 0; 
} 

希望這有助於