/************************************************************************\ * boxes.c * * * * produces two sets of tables for shark.c (SHARK) * * (boxes1.dat and boxes2.dat) * * version 1.1 * * * * Vincent Rijmen * * December 1995 * * Copyright (C): KULeuven * * Copyright (C): KULeuven. All rights reserved. * * KULeuven makes no representations concerning either the * * merchantability of this software or the suitability of this * * software for any particular purpose. It is provided "as is" * * without express or implied warranty of any kind. * \************************************************************************/ #include #include #define ROOT 0x1f5 typedef unsigned char byte; typedef unsigned short word; typedef unsigned long dword; byte alog[256], log[256]; byte mul(byte a, byte b) /* multiply two elements of GF(2^m) */ { if (a && b) return alog[(log[a] + log[b])%255]; else return 0; } void init() /* produce logtabel and alogtabel, * needed for multiplying in the field GF(2^m) */ { word i, j; alog[0] = 1; for(i = 1; i < 256; i++) { j = alog[i-1] << 1; if (j & 0x100) j ^= ROOT; alog[i] = j; } log[0] = 0; for(i = 0; i < 255; i++) log[alog[i]] = i; } void main() { FILE *out; byte ibox[256], g[9], c[9]; byte in, u, t, pivot, tmp; byte box[256], G[8][8], iG[8][8], A[8][16]; byte trans[9] = { 0xd6, 0x7b, 0x3d, 0x1f, 0x0f, 0x05, 0x03, 0x01, 0xb1}; word i, j, k; init(); /* the substitution box based on F^{-1}(x) * + affine transform of the output */ box[0] = 0; box[1] = 1; for(i = 2; i < 256; i++) box[i] = alog[255 - log[i]]; for(i = 0; i < 256; i++) { in = box[i]; box[i] = 0; for(t = 0; t < 8; t++) { u = in & trans[t]; box[i] ^= ((1 & (u ^ (u >> 1) ^ (u >> 2) ^ (u >> 3) ^ (u >> 4) ^ (u >> 5) ^ (u >> 6) ^ (u >> 7))) << (7 - t)); } box[i] ^= trans[8]; } /* diffusion box G * g(x) = (x - 2)*(x - 4)*...*(x - 2^{8}) * the rows of G are the coefficients of * x^{i} mod g(x), i = 2n - 1, 2n - 2, ..., n * iG = inv(G) * A = [G,I] * Gauss elemination on A to produce [I,iG] */ g[0] = 2; g[1] = 1; for(i = 2; i < 9; i++) g[i] = 0; for(i = 2; i <= 8; i++) { for(j = 8; j > 0; j--) { if (g[j]) g[j] = g[j - 1] ^ alog[(i + log[g[j]])%255]; else g[j] = g[j - 1]; } g[0] = alog[(i + log[g[0]])%255]; } for(j = 0; j < 8; j++) c[j] = 0; c[8] = 1; for(i = 0; i < 8; i++) { if (c[8]) for(u = 1; u <= 8; u++) c[8 - u] ^= mul(c[8],g[8 - u]); c[8] = 0; for(j = 0; j < 8; j++) G[7 - i][7 - j] = c[j]; for(j = 8; j > 0; j--) c[j] = c[j - 1]; c[0] = 0; } for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) A[i][j] = G[i][j]; for(j = 8; j < 16; j++) A[i][j] = 0; A[i][i+8] = 1; } for(i = 0; i < 8; i++) { pivot = A[i][i]; if (pivot == 0) { t = i + 1; while ((A[t][i] == 0) && (t < 8)) t++; if (t == 8) fprintf(stderr,"noninvertible matrix G\n"); else { for(j = 0; j < 16; j++) { tmp = A[i][j]; A[i][j] = A[t][j]; A[t][j] = tmp; } pivot = A[i][i]; } } for(j = 0; j < 16; j++) if (A[i][j]) A[i][j] = alog[(255 + log[A[i][j]] - log[pivot])%255]; for(t = 0; t < 8; t++) if (i != t) { for(j = i+1; j < 16; j++) A[t][j] ^= mul(A[i][j],A[t][i]); A[t][i] = 0; } } for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) iG[i][j] = A[i][j+8]; /* output */ out = fopen("boxes1.dat","w"); for(i = 0; i < 256; i++) ibox[box[i]] = i; fprintf(out,"byte sbox_enc[256] = {\n"); for(i = 0; i < 16; i++) { for(j = 0; j < 16; j++) fprintf(out,"%3d, ",box[i*16+j]); fprintf(out,"\n"); } fprintf(out,"};\n\n"); fprintf(out,"byte sbox_dec[256] = {\n"); for(i = 0; i < 16; i++) { for(j = 0; j < 16; j++) fprintf(out,"%3d, ",ibox[i*16+j]); fprintf(out,"\n"); } fprintf(out,"};\n\n"); fprintf(out,"byte G[8][8] = {\n"); for(i = 0; i < 8; i++) { for(k = 0; k < 8; k++) fprintf(out,"0x%02x, ",G[i][k]); fprintf(out,"\n"); } fprintf(out,"};\n\n"); fprintf(out,"byte iG[8][8] = {\n"); for(i = 0; i < 8; i++) { for(k = 0; k < 8; k++) fprintf(out,"0x%02x, ",iG[i][k]); fprintf(out,"\n"); } fprintf(out,"};\n\n"); fclose(out); out = fopen("boxes2.dat","w"); fprintf(out,"ddword cbox_enc[8][256] = {\n"); for(j = 0; j < 8; j++) { fprintf(out,"/* box %d */\n",j); for(k = 0; k < 256; k++) { fprintf(out,"0x"); if (box[k]) for(i = 0; i < 8; i++) fprintf(out,"%02x", mul(box[k],G[i][j])); else fprintf(out,"0000000000000000"); fprintf(out,"LL,\n"); } } fprintf(out,"};\n\n"); fprintf(out,"ddword cbox_dec[8][256] = {\n"); for(j = 0; j < 8; j++) { fprintf(out,"/* box %d */\n",j); for(k = 0; k < 256; k++) { fprintf(out,"0x"); if (ibox[k]) for(i = 0; i < 8; i++) fprintf(out,"%02x", mul(ibox[k],iG[i][j])); else fprintf(out,"0000000000000000"); fprintf(out,"LL,\n"); } } fprintf(out,"};\n\n"); fclose(out); }