1 /* emits an optimized version of SAFER+ ... only does encrypt so far... */ 2 3 #include <stdio.h> 4 #include <string.h> 5 6 /* This is the "Armenian" Shuffle. It takes the input from b and stores it in b2 */ 7 #define SHUF\ 8 b2[0] = b[8]; b2[1] = b[11]; b2[2] = b[12]; b2[3] = b[15]; \ 9 b2[4] = b[2]; b2[5] = b[1]; b2[6] = b[6]; b2[7] = b[5]; \ 10 b2[8] = b[10]; b2[9] = b[9]; b2[10] = b[14]; b2[11] = b[13]; \ 11 b2[12] = b[0]; b2[13] = b[7]; b2[14] = b[4]; b2[15] = b[3]; memcpy(b, b2, sizeof(b)); 12 13 /* This is the inverse shuffle. It takes from b and gives to b2 */ 14 #define iSHUF(b, b2) \ 15 b2[0] = b[12]; b2[1] = b[5]; b2[2] = b[4]; b2[3] = b[15]; \ 16 b2[4] = b[14]; b2[5] = b[7]; b2[6] = b[6]; b2[7] = b[13]; \ 17 b2[8] = b[0]; b2[9] = b[9]; b2[10] = b[8]; b2[11] = b[1]; \ 18 b2[12] = b[2]; b2[13] = b[11]; b2[14] = b[10]; b2[15] = b[3]; memcpy(b, b2, sizeof(b)); 19 20 #define ROUND(b, i) \ 21 b[0] = (safer_ebox[(b[0] ^ skey->saferp.K[i][0]) & 255] + skey->saferp.K[i+1][0]) & 255; \ 22 b[1] = safer_lbox[(b[1] + skey->saferp.K[i][1]) & 255] ^ skey->saferp.K[i+1][1]; \ 23 b[2] = safer_lbox[(b[2] + skey->saferp.K[i][2]) & 255] ^ skey->saferp.K[i+1][2]; \ 24 b[3] = (safer_ebox[(b[3] ^ skey->saferp.K[i][3]) & 255] + skey->saferp.K[i+1][3]) & 255; \ 25 b[4] = (safer_ebox[(b[4] ^ skey->saferp.K[i][4]) & 255] + skey->saferp.K[i+1][4]) & 255; \ 26 b[5] = safer_lbox[(b[5] + skey->saferp.K[i][5]) & 255] ^ skey->saferp.K[i+1][5]; \ 27 b[6] = safer_lbox[(b[6] + skey->saferp.K[i][6]) & 255] ^ skey->saferp.K[i+1][6]; \ 28 b[7] = (safer_ebox[(b[7] ^ skey->saferp.K[i][7]) & 255] + skey->saferp.K[i+1][7]) & 255; \ 29 b[8] = (safer_ebox[(b[8] ^ skey->saferp.K[i][8]) & 255] + skey->saferp.K[i+1][8]) & 255; \ 30 b[9] = safer_lbox[(b[9] + skey->saferp.K[i][9]) & 255] ^ skey->saferp.K[i+1][9]; \ 31 b[10] = safer_lbox[(b[10] + skey->saferp.K[i][10]) & 255] ^ skey->saferp.K[i+1][10]; \ 32 b[11] = (safer_ebox[(b[11] ^ skey->saferp.K[i][11]) & 255] + skey->saferp.K[i+1][11]) & 255; \ 33 b[12] = (safer_ebox[(b[12] ^ skey->saferp.K[i][12]) & 255] + skey->saferp.K[i+1][12]) & 255; \ 34 b[13] = safer_lbox[(b[13] + skey->saferp.K[i][13]) & 255] ^ skey->saferp.K[i+1][13]; \ 35 b[14] = safer_lbox[(b[14] + skey->saferp.K[i][14]) & 255] ^ skey->saferp.K[i+1][14]; \ 36 b[15] = (safer_ebox[(b[15] ^ skey->saferp.K[i][15]) & 255] + skey->saferp.K[i+1][15]) & 255; 37 38 int main(void) 39 { 40 int b[16], b2[16], x, y, z; 41 42 /* -- ENCRYPT --- */ 43 for (x = 0; x < 16; x++) b[x] = x; 44 /* emit encrypt preabmle */ 45 printf( 46 "void saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)\n" 47 "{\n" 48 " int x;\n" 49 " unsigned char b[16];\n" 50 "\n" 51 " LTC_ARGCHK(pt != NULL);\n" 52 " LTC_ARGCHK(ct != NULL);\n" 53 " LTC_ARGCHK(skey != NULL);\n" 54 "\n" 55 " /* do eight rounds */\n" 56 " for (x = 0; x < 16; x++) {\n" 57 " b[x] = pt[x];\n" 58 " }\n"); 59 60 /* do 8 rounds of ROUND; LT; */ 61 for (x = 0; x < 8; x++) { 62 /* ROUND(..., x*2) */ 63 for (y = 0; y < 16; y++) { 64 printf("b[%d] = (safer_%cbox[(b[%d] %c skey->saferp.K[%d][%d]) & 255] %c skey->saferp.K[%d][%d]) & 255;\n", 65 b[y], "elle"[y&3], b[y], "^++^"[y&3], x*2, y, "+^^+"[y&3], x*2+1, y); 66 } 67 68 /* LT */ 69 for (y = 0; y < 4; y++) { 70 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[0], b[0], b[1], b[0], b[1]); 71 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[2], b[2], b[3], b[3], b[2]); 72 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[4], b[4], b[5], b[5], b[4]); 73 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[6], b[6], b[7], b[7], b[6]); 74 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[8], b[8], b[9], b[9], b[8]); 75 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[10], b[10], b[11], b[11], b[10]); 76 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[12], b[12], b[13], b[13], b[12]); 77 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[14], b[14], b[15], b[15], b[14]); 78 if (y < 3) { 79 SHUF; 80 } 81 } 82 } 83 84 printf( 85 " if (skey->saferp.rounds <= 8) {\n"); 86 /* finish */ 87 for (x = 0; x < 16; x++) { 88 printf( 89 " ct[%d] = (b[%d] %c skey->saferp.K[skey->saferp.rounds*2][%d]) & 255;\n", 90 x, b[x], "^++^"[x&3], x); 91 } 92 printf(" return;\n }\n"); 93 94 /* 192-bit keys */ 95 printf( 96 " /* 192-bit key? */\n" 97 " if (skey->saferp.rounds > 8) {\n"); 98 99 /* do 4 rounds of ROUND; LT; */ 100 for (x = 8; x < 12; x++) { 101 /* ROUND(..., x*2) */ 102 for (y = 0; y < 16; y++) { 103 printf("b[%d] = (safer_%cbox[(b[%d] %c skey->saferp.K[%d][%d]) & 255] %c skey->saferp.K[%d][%d]) & 255;\n", 104 b[y], "elle"[y&3], b[y], "^++^"[y&3], x*2, y, "+^^+"[y&3], x*2+1, y); 105 } 106 107 /* LT */ 108 for (y = 0; y < 4; y++) { 109 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[0], b[0], b[1], b[0], b[1]); 110 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[2], b[2], b[3], b[3], b[2]); 111 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[4], b[4], b[5], b[5], b[4]); 112 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[6], b[6], b[7], b[7], b[6]); 113 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[8], b[8], b[9], b[9], b[8]); 114 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[10], b[10], b[11], b[11], b[10]); 115 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[12], b[12], b[13], b[13], b[12]); 116 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[14], b[14], b[15], b[15], b[14]); 117 if (y < 3) { 118 SHUF; 119 } 120 } 121 } 122 printf("}\n"); 123 124 printf( 125 " if (skey->saferp.rounds <= 12) {\n"); 126 /* finish */ 127 for (x = 0; x < 16; x++) { 128 printf( 129 " ct[%d] = (b[%d] %c skey->saferp.K[skey->saferp.rounds*2][%d]) & 255;\n", 130 x, b[x], "^++^"[x&3], x); 131 } 132 printf(" return;\n }\n"); 133 134 /* 256-bit keys */ 135 printf( 136 " /* 256-bit key? */\n" 137 " if (skey->saferp.rounds > 12) {\n"); 138 139 /* do 4 rounds of ROUND; LT; */ 140 for (x = 12; x < 16; x++) { 141 /* ROUND(..., x*2) */ 142 for (y = 0; y < 16; y++) { 143 printf("b[%d] = (safer_%cbox[(b[%d] %c skey->saferp.K[%d][%d]) & 255] %c skey->saferp.K[%d][%d]) & 255;\n", 144 b[y], "elle"[y&3], b[y], "^++^"[y&3], x*2, y, "+^^+"[y&3], x*2+1, y); 145 } 146 147 /* LT */ 148 for (y = 0; y < 4; y++) { 149 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[0], b[0], b[1], b[0], b[1]); 150 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[2], b[2], b[3], b[3], b[2]); 151 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[4], b[4], b[5], b[5], b[4]); 152 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[6], b[6], b[7], b[7], b[6]); 153 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[8], b[8], b[9], b[9], b[8]); 154 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[10], b[10], b[11], b[11], b[10]); 155 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[12], b[12], b[13], b[13], b[12]); 156 printf(" b[%d] = (b[%d] + (b[%d] = (b[%d] + b[%d]) & 255)) & 255;\n", b[14], b[14], b[15], b[15], b[14]); 157 if (y < 3) { 158 SHUF; 159 } 160 } 161 } 162 /* finish */ 163 for (x = 0; x < 16; x++) { 164 printf( 165 " ct[%d] = (b[%d] %c skey->saferp.K[skey->saferp.rounds*2][%d]) & 255;\n", 166 x, b[x], "^++^"[x&3], x); 167 } 168 printf(" return;\n"); 169 printf(" }\n}\n\n"); 170 171 return 0; 172 } 173 174 175 /* $Source: /cvs/libtom/libtomcrypt/notes/etc/saferp_optimizer.c,v $ */ 176 /* $Revision: 1.2 $ */ 177 /* $Date: 2005/05/05 14:35:58 $ */ 178