我有一个功能:
void sbox(uint8_t* sb1, uint8_t* sb2, uint8_t* sb3, uint8_t* sb4, uint8_t* primitive, size_t size) {
if(!sb1 || !sb2 || !sb3 || !sb4 || !primitive) {
printf("*** error: Polynomials cannot be null in sbox()\n");
return;
}
uint8_t timeSb1, timeSb2, timeSb3, timeSb4;
timeSb1 = 62;
timeSb2 = 5;
timeSb3 = 17;
timeSb4 = 62;
sb1 = sb(sb1, sb1, sb1, size, primitive, timeSb1);
sb2 = sb(sb2, sb2, sb2, size, primitive, timeSb2);
sb3 = sb(sb3, sb3, sb3, size, primitive, timeSb3);
sb4 = sbr4(sb4, sb4, sb4, size, primitive, timeSb4);
}
其中函数sb()和sbr4()的第三个参数是在这些函数中对前两个参数应用的操作的结果。其余的功能是:
uint8_t* sb(uint8_t* a, uint8_t* b, uint8_t* c, size_t size, uint8_t* primitive, size_t times) {
return mul2polyTimes(a, b, c, size, primitive, times);
}
uint8_t* mul2polyTimes(uint8_t* a, uint8_t* b, uint8_t* c, size_t size, uint8_t* primitive, size_t times) {
if(!a || !b || !primitive) {
printf("*** error: Polynomials cannot be null in mul2poly()\n");
return NULL;
}
for(int i = 0; i < times; i++) {
c = mul2poly(a, b, c, size, primitive);
}
return c;
}
uint8_t* mul2poly(uint8_t* a, uint8_t* b, uint8_t* c, size_t size, uint8_t* primitive)
{
if(!a || !b || !primitive) {
printf("*** error: Polynomials cannot be null in mul2poly()\n");
return NULL;
}
uint8_t carry, prodSize;
uint8_t *temp;
carry = 0;
temp = NULL;
prodSize = 2*size;
c = (uint8_t*)allocate1DArray((void*)c, prodSize);
temp = (uint8_t*)allocate1DArray((void*)temp, prodSize);
for(int i = size - 1; i >= 0; i--)
temp[i + size] = a[i];
if(b[size-1]) {
for(int i = size - 1; i >= 0; i--)
c[i + size] = a[i];
}
for(int i = size - 2; i >= 0; i--) {
lShiftPoly(temp, prodSize);
if(b[i]) {
for(int ii = prodSize - 1; ii >= 0; ii--) {
if(carry) {
if(c[ii] && temp[ii]) {
c[ii] = 1;
carry = 1;
} else if(c[ii] || temp[ii]) {
c[ii] = 0;
carry = 1;
} else {
c[ii] = 1;
carry = 0;
}
} else {
if(c[ii] && temp[ii]) {
c[ii] = 0;
carry = 1;
} else if(c[ii] || temp[ii]) {
c[ii] = 1;
carry = 0;
} else {
c[ii] = 0;
carry = 0;
}
}
}
}
}
uint8_t reduce;
reduce = 0;
for(int i = 0; i < size; i++) {
if(c[i]) {
reduce = 1;
break;
}
}
if(reduce) {
for(int i = 0; i < prodSize; i++)
c[i] ^= primitive[i];
}
uint8_t* d;
d = NULL;
d = (uint8_t*)allocate1DArray((void*)d, size);
for(int i = size; i < prodSize; i++) {
d[i-size] = c[i];
}
free(c);
c = d;
return c;
}
我注意到,在调用sbox()之后,引用值的值保持不变,但是在函数内检查时,它们会被相应地设置。我在这里错过了什么?
注意:这些函数占用如此大量参数的原因与编程无关。
答案 0 :(得分:2)
mul2poly()
中的这一行:
c = (uint8_t*)allocate1DArray((void*)c, prodSize);
不会在mul2poly()
的特定调用之外传播新分配。您需要将第三个参数作为uint8_t**
传递并适当地取消引用它以在C中执行您想要的操作。