简体   繁体   中英

Unexpected behavior of C function parameters

I have a function:

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);
}

where the third parameter of functions sb() and sbr4() is the result of an operation applied on the first two parameters, within those functions. The remaining functions are:

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;
   }

I've noticed that, after calling sbox(), the values of the referenced values remain unaltered, but when inspected within the function they are being set accordingly. What am I missing here?

Note: the reasons that these functions take such a large number of parameters are not programming related.

This line in mul2poly() :

c = (uint8_t*)allocate1DArray((void*)c, prodSize);

doesn't propagate the new allocation outside the particular invocation of mul2poly() . You'll need to pass the third argument as a uint8_t** and dereference it appropriately to do what you want in C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM