1

我正在使用 c 开发 AES 加密程序,同时在混合列块中进行 galois 域乘法,

前任。[ https://crypto.stackexchange.com/questions/2402/how-to-solve-mixcolumns][1]

代码

for galois field multiplication
    int galois_multiply( int a,int b){
        int flag,res;
        switch (a){
            case 1:
                return b;
                break;
            case 2: flag= b * 0x80;
                b= b << 1;      //left shift
                if (flag)
                    res= b ^ 0x1b;
                else
                    res= b ^0x00;
                printf("\nnumber  %d returned by galois_multiply function\n",res);
                return res;

            case 3: res= b ^ galois_multiply(2,b);
                printf("\nnumber  %d returned by galois_multiply function\n",res);
                return res;

            default:
                printf("Invalid number  %d passed to galois_multiply function\n",a);
                exit(EXIT_FAILURE);
        }
                         return 0;
    }

假设为

  • d4×02为d4<<1,与1b异或(因为d4的高位被置位),正确答案为b3;而使用此代码我得到 1b3
  • bf×03 是 bf<<1 与 1b(因为设置了 bf 的高位)和 bf(因为我们乘以 3)异或,应该给 da;但使用代码结果是 1da

即使通过屏蔽 msb 解决了上述问题,但在以下代码中的 mixcolumn 中使用时,答案似乎是不正确的,它的一般矩阵运算仅在乘法被 galois 乘法和加法替换为 XOR 操作的情况下

void mixColumn(unsigned char **state){
    int mc[4][4]={{2,3,1,1},{1,2,3,1},{1,1,2,3},{3,1,1,2}};
    int res[4][4]={{0}};
    int i,j,k;

    for(i=0;i<4;i++){
            for(j=0;j<4;j++){
                    res[i][j]=0;
                    for(k=0;k<4;k++)
                        res[i][j]= res[i][j] ^ galois_multiply(mc[i][k],state[k][j]);
                    state[i][j]=res[i][j];
            }
    }

}

你能找到任何可能导致错误的错误吗...

4

2 回答 2

0

对于一个 int
如果你想清除一个int x;使用x &=7fffffff; (只删除第一位)
对于一个 short int
如果你想清除一个short x;使用x &=7fff; (只删除第一位)

如果 short x == 1001011111001010然后(或 0x18ca
short mask == 0111111111111111;
x &= mask; ==0001011111001010

但是,假设您有:0x1b3,(或 0x1da),但如果您想要屏蔽前两个字节,则需要 0xb3(或 0xda)。

在这种情况下,请将您的掩码设置为0x00ff

看来您正在工作short int,然后将代码从 更改intshort,或(如@wildplasser 评论)更改为unsigned short

您发布的代码示例在几个地方有无法访问的代码,并且不包含 return 语句:

int galois_multiply( int a,int b){
    int flag,res;
    switch (a){
        case 1:
            return b;
            break;//unreachable code
        case 2: flag= b * 0x80;
            b= b << 1;      //left shift
            if (flag)
                res= b ^ 0x1b;
            else
                res= b ^0x00;
            printf("\nnumber  %d returned by galois_multiply function\n",res);
            return res;
            break;//unreachable code
        case 3: res= b ^ galois_multiply(2,b);
            printf("\nnumber  %d returned by galois_multiply function\n",res);
            return res;
            break;//unreachable code
        default:
            printf("Invalid number  %d passed to galois_multiply function\n",a);
            exit(EXIT_FAILURE);
    }
    //no return statement (prototype specifies one)
}

当前面有一些其他退出方法时,该switch()语句不需要break;语句,例如return x;

于 2014-04-26T16:40:35.567 回答
0

终于发现了我在案例2中犯的错误,我用过

flag= b * 0x80;

但我应该使用的是

flag= b & 0x80;

在二进制中思考让我认为这两个都是相同的运算符,但在字节级别的情况完全不同,* 会将内容乘以 80h,而 & 将按位与(乘)两个操作数,这是我需要的。

于 2014-04-27T04:17:26.737 回答