OK, așa cum era de așteptat, a fost o „problema de endianness”. Utilizând următoarele SHIFT(x)
macro atât la intrare, cât și la ieșire, recuperez vectorii de testare:
#define SHIFT(x) ((x) << 24 | ((x) & 0x0000ff00) << 8 | ((x) & 0x00ff0000) >> 8 | ((x) & 0xff000000) >> 24)
Iată codul complet:
#include <stdio.h>
int main() {
unsigned int în[16];
unsigned int x[16];
unsigned int out[16];
/*
<https://datatracker.ietf.org/doc/html/rfc7914#section-8>
INTRARE:
7e 87 9a 21 4f 3e c9 86 7c a9 40 e6 41 71 8f 26
ba ee 55 5b 8c 61 c1 b5 0d f8 46 11 6d cd 3b 1d
ee 24 f3 19 df 9b 3d 85 14 12 1e 4b 5a c5 aa 32
76 02 1d 29 09 c7 48 29 ed eb c6 8d b8 b8 c2 5e
*/
#define SHIFT(x) ((x) << 24 | ((x) & 0x0000ff00) << 8 | ((x) & 0x00ff0000) >> 8 | ((x) & 0xff000000) >> 24)
in[ 0] = SHIFT(0x7e879a21);
in[ 1] = SHIFT(0x4f3ec986);
in[ 2] = SHIFT(0x7ca940e6);
in[ 3] = SHIFT(0x41718f26);
in[ 4] = SHIFT(0xbaee555b);
in[ 5] = SHIFT(0x8c61c1b5);
in[ 6] = SHIFT(0x0df84611);
in[ 7] = SHIFT(0x6dcd3b1d);
in[ 8] = SHIFT(0xee24f319);
in[ 9] = SHIFT(0xdf9b3d85);
in[10] = SHIFT(0x14121e4b);
in[11] = SHIFT(0x5ac5aa32);
in[12] = SHIFT(0x76021d29);
in[13] = SHIFT(0x09c74829);
in[14] = SHIFT(0xedebc68d);
in[15] = SHIFT(0xb8b8c25e);
/* <https://datatracker.ietf.org/doc/html/rfc7914#section-3> */
#definiți R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
int i;
pentru (i = 0;i < 16;++i) x[i] = in[i];
pentru (i = 8;i > 0;i -= 2) {
x[4] ^= R(x[0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
x[12] ^= R(x[8]+x[4],13); x[0] ^= R(x[12]+x[8],18);
x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[9]+x[5], 9);
x[1] ^= R(x[13]+x[9],13); x[5] ^= R(x[1]+x[13],18);
x[14] ^= R(x[10]+x[6], 7); x[2] ^= R(x[14]+x[10], 9);
x[6] ^= R(x[2]+x[14],13); x[10] ^= R(x[6]+x[2],18);
x[3] ^= R(x[15]+x[11], 7); x[7] ^= R(x[3]+x[15], 9);
x[11] ^= R(x[7]+x[3],13); x[15] ^= R(x[11]+x[7],18);
x[ 1] ^= R(x[ 0]+x[ 3], 7); x[2] ^= R(x[1]+x[0], 9);
x[3] ^= R(x[2]+x[1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
x[4] ^= R(x[7]+x[6],13); x[5] ^= R(x[4]+x[7],18);
x[11] ^= R(x[10]+x[9], 7); x[8] ^= R(x[11]+x[10], 9);
x[9] ^= R(x[8]+x[11],13); x[10] ^= R(x[9]+x[8],18);
x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
}
pentru (i = 0;i < 16;++i) out[i] = x[i] + in[i];
printf("in[ 0] = 0x%08x | out[ 0] = 0x%08x\n", in[ 0], SHIFT(out[ 0]));
printf("in[ 1] = 0x%08x | out[ 1] = 0x%08x\n", in[ 1], SHIFT(out[ 1]));
printf("in[ 2] = 0x%08x | out[ 2] = 0x%08x\n", in[ 2], SHIFT(out[ 2]));
printf("in[ 3] = 0x%08x | out[ 3] = 0x%08x\n", in[ 3], SHIFT(out[ 3]));
printf("in[ 4] = 0x%08x | out[ 4] = 0x%08x\n", in[ 4], SHIFT(out[ 4]));
printf("in[ 5] = 0x%08x | out[ 5] = 0x%08x\n", in[ 5], SHIFT(out[ 5]));
printf("in[ 6] = 0x%08x | out[ 6] = 0x%08x\n", in[ 6], SHIFT(out[ 6]));
printf("in[ 7] = 0x%08x | out[ 7] = 0x%08x\n", in[ 7], SHIFT(out[ 7]));
printf("in[ 8] = 0x%08x | out[ 8] = 0x%08x\n", in[ 8], SHIFT(out[ 8]));
printf("in[ 9] = 0x%08x | out[ 9] = 0x%08x\n", in[ 9], SHIFT(out[ 9]));
printf("in[10] = 0x%08x | out[10] = 0x%08x\n", in[10], SHIFT(out[10]));
printf("in[11] = 0x%08x | out[11] = 0x%08x\n", in[11], SHIFT(out[11]));
printf("in[12] = 0x%08x | out[12] = 0x%08x\n", in[12], SHIFT(out[12]));
printf("in[13] = 0x%08x | out[13] = 0x%08x\n", in[13], SHIFT(out[13]));
printf("in[14] = 0x%08x | out[14] = 0x%08x\n", in[14], SHIFT(out[14]));
printf("in[15] = 0x%08x | out[15] = 0x%08x\n", in[15], SHIFT(out[15]));
returnează 0;
}
/*
IEȘIRE:
în[ 0] = 0x219a877e | out[ 0] = 0xa41f859c
în[ 1] = 0x86c93e4f | out[ 1] = 0x6608cc99
în[ 2] = 0xe640a97c | out[ 2] = 0x3b81cacb
în[ 3] = 0x268f7141 | out[ 3] = 0x020cef05
în[ 4] = 0x5b55eeba | afară[ 4] = 0x044b2181
în[ 5] = 0xb5c1618c | afară[ 5] = 0xa2fd337d
în[ 6] = 0x1146f80d | afară[ 6] = 0xfd7b1c63
în[ 7] = 0x1d3bcd6d | out[ 7] = 0x96682f29
în[ 8] = 0x19f324ee | afară[ 8] = 0xb4393168
în[ 9] = 0x853d9bdf | out[ 9] = 0xe3c9e6bc
în[10] = 0x4b1e1214 | out[10] = 0xfe6bc5b7
in[11] = 0x32aac55a | out[11] = 0xa06d96ba
in[12] = 0x291d0276 | out[12] = 0xe424cc10
in[13] = 0x2948c709 | out[13] = 0x2c91745c
in[14] = 0x8dc6ebed | out[14] = 0x24ad673d
in[15] = 0x5ec2b8b8 | out[15] = 0xc7618f81
<https://datatracker.ietf.org/doc/html/rfc7914#section-8>
INTRARE:
7e 87 9a 21 4f 3e c9 86 7c a9 40 e6 41 71 8f 26
ba ee 55 5b 8c 61 c1 b5 0d f8 46 11 6d cd 3b 1d
ee 24 f3 19 df 9b 3d 85 14 12 1e 4b 5a c5 aa 32
76 02 1d 29 09 c7 48 29 ed eb c6 8d b8 b8 c2 5e
IEȘIRE:
a4 1f 85 9c 66 08 cc 99 3b 81 ca cb 02 0c ef 05
04 4b 21 81 a2 fd 33 7d fd 7b 1c 63 96 68 2f 29
b4 39 31 68 e3 c9 e6 bc fe 6b c5 b7 a0 6d 96 ba
e4 24 cc 10 2c 91 74 5c 24 ad 67 3d c7 61 8f 81
*/
pe care se poate rula https://www.programiz.com/c-programming/online-compiler/.