0

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.

3
  • Hint: Where are those values stored? Commented Nov 21, 2013 at 23:26
  • First two parameters are sources, the third one stores the result Commented Nov 21, 2013 at 23:28
  • 1
    You are aware that in mulPoly a,b,c all point to the same data, right? Commented Nov 21, 2013 at 23:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.