How to use sha256_update if buf is larger than 64 bytes?
#1
Question 
I'm trying to change the logic of one of the modules, adding sha256 instead of other hashing. The problem is that the data for the hash exceeds the buffer limit of 64 bytes.

I think there should be approximately such logic:

Code:
sha256_ctx_t ctx;
sha256_init(&ctx);

u32 st[21]

st[0] = k[0]
st[1] = k[1]
st[2] = k[2]
st[3] = k[3]
st[4] = ct[0]
st[5] = ct[1]
...
st[15] = ct[10]

sha256_update(&ctx, st, 64); // up to this point there will still be a correct result,
                            // because the 'st' buf contains 64 bytes;

st[16] = ct[11]
st[17] = ct[12]
st[18] = ct[13]
st[19] = ct[15]
st[20] = ct[16]

sha256_update(&ctx, st, 64); // I'm not sure what size I need here, tried different options;

sha256_final(&ctx);

for (int i = 0; i < 8; i++) {
  printf("\n%x\n", ctx.h[i])
}

I expect the correct result sha256(k, ct), but the result is wrong.
Reply
#2
Firstly, you need to scale that size based on the amount of bytes so if your "ct" value is 71 bytes long, the length value would be 71-64 = 5
Secondly, you can't reuse the buffer like that, you have to start from the beginning again and you may have to 0-out the rest of the buffer that you're not using for safety, so instead of "st[16] = ct[11]", you'd do "st[0] = ct[11]" and likely have to do: st[6-15] = 0 or you can just have a new array called "st2" or whatever if you want

Unrelated but you should use "%08x" in your printf so that 0s get padded correctly
Reply
#3
Thanks! I made a new (second) array and it worked
Reply