raw
smg_comms_packing...    1  ------------------------------------------------------------------------------
smg_comms_packing... 2 --
smg_comms_packing... 3 -- Serpent Blockcipher
smg_comms_packing... 4 --
smg_comms_packing... 5 -- Copyright (c) 1998 Markus G. Kuhn <mkuhn@acm.org>. All rights reserved.
smg_comms_packing... 6 --
smg_comms_packing... 7 -- Modified by S.MG, 2018
smg_comms_packing... 8 --
smg_comms_packing... 9 ------------------------------------------------------------------------------
smg_comms_packing... 10 --
smg_comms_packing... 11 -- This implementation is optimized for best execution time by use of
smg_comms_packing... 12 -- function inlining and loop unrolling. It is not intended to be used in
smg_comms_packing... 13 -- applications (such as smartcards) where machine code size matters. Best
smg_comms_packing... 14 -- compiled with highest optimization level activated and all run-time
smg_comms_packing... 15 -- checks supressed.
smg_comms_packing... 16 --
smg_comms_packing... 17 ------------------------------------------------------------------------------
smg_comms_packing... 18
smg_comms_packing... 19 with System, Ada.Unchecked_Conversion;
smg_comms_packing... 20 use System;
smg_comms_packing... 21
smg_comms_packing... 22 package body Serpent is
smg_comms_packing... 23
smg_comms_packing... 24 pragma Optimize( Time );
smg_comms_packing... 25
smg_comms_packing... 26 -- Auxiliary functions for byte array to word array conversion with
smg_comms_packing... 27 -- Bigendian/Littleendian handling.
smg_comms_packing... 28 --
smg_comms_packing... 29 -- The convention followed here is that the input byte array
smg_comms_packing... 30 --
smg_comms_packing... 31 -- 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
smg_comms_packing... 32 --
smg_comms_packing... 33 -- is converted into the register values
smg_comms_packing... 34 --
smg_comms_packing... 35 -- X0 = 03020100, X1 = 07060504, X2 = 0b0a0908, X3 = 0f0e0d0c
smg_comms_packing... 36
smg_comms_packing... 37 subtype Bytes_4 is Bytes (0 .. 3);
smg_comms_packing... 38 function Cast is new Ada.Unchecked_Conversion (Bytes_4, Unsigned_32);
smg_comms_packing... 39 function Cast is new Ada.Unchecked_Conversion (Unsigned_32, Bytes_4);
smg_comms_packing... 40
smg_comms_packing... 41 function Bytes_To_Word (X : Bytes_4) return Unsigned_32 is
smg_comms_packing... 42 begin
smg_comms_packing... 43 if Default_Bit_Order = Low_Order_First then
smg_comms_packing... 44 -- we have a Littleendian processor
smg_comms_packing... 45 return Cast(X);
smg_comms_packing... 46 else
smg_comms_packing... 47 -- word sex change
smg_comms_packing... 48 return Cast((X(3), X(2), X(1), X(0)));
smg_comms_packing... 49 end if;
smg_comms_packing... 50 end Bytes_To_Word;
smg_comms_packing... 51
smg_comms_packing... 52 function Word_To_Bytes (X : Unsigned_32) return Bytes_4 is
smg_comms_packing... 53 begin
smg_comms_packing... 54 if Default_Bit_Order = Low_Order_First then
smg_comms_packing... 55 -- we have a Littleendian processor
smg_comms_packing... 56 return Cast(X);
smg_comms_packing... 57 else
smg_comms_packing... 58 -- word sex change
smg_comms_packing... 59 return (Cast(X)(3), Cast(X)(2), Cast(X)(1), Cast(X)(0));
smg_comms_packing... 60 end if;
smg_comms_packing... 61 end Word_To_Bytes;
smg_comms_packing... 62
smg_comms_packing... 63 pragma Inline(Bytes_To_Word, Word_To_Bytes);
smg_comms_packing... 64 -- inline functions for the Encryption and Decryption procedures
smg_comms_packing... 65
smg_comms_packing... 66 -- Sbox function
smg_comms_packing... 67 procedure S (R : Integer; X0, X1, X2, X3 : in out Unsigned_32) is
smg_comms_packing... 68 T01, T02, T03, T04, T05, T06, T07, T08, T09,
smg_comms_packing... 69 T10, T11, T12, T13, T14, T15, T16, T17, T18 : Unsigned_32;
smg_comms_packing... 70 W, X, Y, Z : Unsigned_32;
smg_comms_packing... 71 begin
smg_comms_packing... 72 if R = 0 then
smg_comms_packing... 73 -- S0: 3 8 15 1 10 6 5 11 14 13 4 2 7 0 9 12
smg_comms_packing... 74 -- depth = 5,7,4,2, Total gates=18
smg_comms_packing... 75 T01 := X1 xor X2;
smg_comms_packing... 76 T02 := X0 or X3;
smg_comms_packing... 77 T03 := X0 xor X1;
smg_comms_packing... 78 Z := T02 xor T01;
smg_comms_packing... 79 T05 := X2 or z;
smg_comms_packing... 80 T06 := X0 xor X3;
smg_comms_packing... 81 T07 := X1 or X2;
smg_comms_packing... 82 T08 := X3 and T05;
smg_comms_packing... 83 T09 := T03 and T07;
smg_comms_packing... 84 Y := T09 xor T08;
smg_comms_packing... 85 T11 := T09 and y;
smg_comms_packing... 86 T12 := X2 xor X3;
smg_comms_packing... 87 T13 := T07 xor T11;
smg_comms_packing... 88 T14 := X1 and T06;
smg_comms_packing... 89 T15 := T06 xor T13;
smg_comms_packing... 90 W := not T15;
smg_comms_packing... 91 T17 := W xor T14;
smg_comms_packing... 92 X := T12 xor T17;
smg_comms_packing... 93 elsif R = 1 then
smg_comms_packing... 94 -- S1: 15 12 2 7 9 0 5 10 1 11 14 8 6 13 3 4
smg_comms_packing... 95 -- depth = 10,7,3,5, Total gates=18
smg_comms_packing... 96 T01 := X0 or X3;
smg_comms_packing... 97 T02 := X2 xor X3;
smg_comms_packing... 98 T03 := not X1;
smg_comms_packing... 99 T04 := X0 xor X2;
smg_comms_packing... 100 T05 := X0 or T03;
smg_comms_packing... 101 T06 := X3 and T04;
smg_comms_packing... 102 T07 := T01 and T02;
smg_comms_packing... 103 T08 := X1 or T06;
smg_comms_packing... 104 Y := T02 xor T05;
smg_comms_packing... 105 T10 := T07 xor T08;
smg_comms_packing... 106 T11 := T01 xor T10;
smg_comms_packing... 107 T12 := Y xor T11;
smg_comms_packing... 108 T13 := X1 and X3;
smg_comms_packing... 109 Z := not T10;
smg_comms_packing... 110 X := T13 xor T12;
smg_comms_packing... 111 T16 := T10 or x;
smg_comms_packing... 112 T17 := T05 and T16;
smg_comms_packing... 113 W := X2 xor T17;
smg_comms_packing... 114 elsif R = 2 then
smg_comms_packing... 115 -- S2: 8 6 7 9 3 12 10 15 13 1 14 4 0 11 5 2
smg_comms_packing... 116 -- depth = 3,8,11,7, Total gates=16
smg_comms_packing... 117 T01 := X0 or X2;
smg_comms_packing... 118 T02 := X0 xor X1;
smg_comms_packing... 119 T03 := X3 xor T01;
smg_comms_packing... 120 W := T02 xor T03;
smg_comms_packing... 121 T05 := X2 xor w;
smg_comms_packing... 122 T06 := X1 xor T05;
smg_comms_packing... 123 T07 := X1 or T05;
smg_comms_packing... 124 T08 := T01 and T06;
smg_comms_packing... 125 T09 := T03 xor T07;
smg_comms_packing... 126 T10 := T02 or T09;
smg_comms_packing... 127 X := T10 xor T08;
smg_comms_packing... 128 T12 := X0 or X3;
smg_comms_packing... 129 T13 := T09 xor x;
smg_comms_packing... 130 T14 := X1 xor T13;
smg_comms_packing... 131 Z := not T09;
smg_comms_packing... 132 Y := T12 xor T14;
smg_comms_packing... 133 elsif R = 3 then
smg_comms_packing... 134 -- S3: 0 15 11 8 12 9 6 3 13 1 2 4 10 7 5 14
smg_comms_packing... 135 -- depth = 8,3,5,5, Total gates=18
smg_comms_packing... 136 T01 := X0 xor X2;
smg_comms_packing... 137 T02 := X0 or X3;
smg_comms_packing... 138 T03 := X0 and X3;
smg_comms_packing... 139 T04 := T01 and T02;
smg_comms_packing... 140 T05 := X1 or T03;
smg_comms_packing... 141 T06 := X0 and X1;
smg_comms_packing... 142 T07 := X3 xor T04;
smg_comms_packing... 143 T08 := X2 or T06;
smg_comms_packing... 144 T09 := X1 xor T07;
smg_comms_packing... 145 T10 := X3 and T05;
smg_comms_packing... 146 T11 := T02 xor T10;
smg_comms_packing... 147 Z := T08 xor T09;
smg_comms_packing... 148 T13 := X3 or z;
smg_comms_packing... 149 T14 := X0 or T07;
smg_comms_packing... 150 T15 := X1 and T13;
smg_comms_packing... 151 Y := T08 xor T11;
smg_comms_packing... 152 W := T14 xor T15;
smg_comms_packing... 153 X := T05 xor T04;
smg_comms_packing... 154 elsif R = 4 then
smg_comms_packing... 155 -- S4: 1 15 8 3 12 0 11 6 2 5 4 10 9 14 7 13
smg_comms_packing... 156 -- depth = 6,7,5,3, Total gates=19
smg_comms_packing... 157 T01 := X0 or X1;
smg_comms_packing... 158 T02 := X1 or X2;
smg_comms_packing... 159 T03 := X0 xor T02;
smg_comms_packing... 160 T04 := X1 xor X3;
smg_comms_packing... 161 T05 := X3 or T03;
smg_comms_packing... 162 T06 := X3 and T01;
smg_comms_packing... 163 Z := T03 xor T06;
smg_comms_packing... 164 T08 := Z and T04;
smg_comms_packing... 165 T09 := T04 and T05;
smg_comms_packing... 166 T10 := X2 xor T06;
smg_comms_packing... 167 T11 := X1 and X2;
smg_comms_packing... 168 T12 := T04 xor T08;
smg_comms_packing... 169 T13 := T11 or T03;
smg_comms_packing... 170 T14 := T10 xor T09;
smg_comms_packing... 171 T15 := X0 and T05;
smg_comms_packing... 172 T16 := T11 or T12;
smg_comms_packing... 173 Y := T13 xor T08;
smg_comms_packing... 174 X := T15 xor T16;
smg_comms_packing... 175 W := not T14;
smg_comms_packing... 176 elsif R = 5 then
smg_comms_packing... 177 -- S5: 15 5 2 11 4 10 9 12 0 3 14 8 13 6 7 1
smg_comms_packing... 178 -- depth = 4,6,8,6, Total gates=17
smg_comms_packing... 179 T01 := X1 xor X3;
smg_comms_packing... 180 T02 := X1 or X3;
smg_comms_packing... 181 T03 := X0 and T01;
smg_comms_packing... 182 T04 := X2 xor T02;
smg_comms_packing... 183 T05 := T03 xor T04;
smg_comms_packing... 184 W := not T05;
smg_comms_packing... 185 T07 := X0 xor T01;
smg_comms_packing... 186 T08 := X3 or w;
smg_comms_packing... 187 T09 := X1 or T05;
smg_comms_packing... 188 T10 := X3 xor T08;
smg_comms_packing... 189 T11 := X1 or T07;
smg_comms_packing... 190 T12 := T03 or w;
smg_comms_packing... 191 T13 := T07 or T10;
smg_comms_packing... 192 T14 := T01 xor T11;
smg_comms_packing... 193 Y := T09 xor T13;
smg_comms_packing... 194 X := T07 xor T08;
smg_comms_packing... 195 Z := T12 xor T14;
smg_comms_packing... 196 elsif R = 6 then
smg_comms_packing... 197 -- S6: 7 2 12 5 8 4 6 11 14 9 1 15 13 3 10 0
smg_comms_packing... 198 -- depth = 8,3,6,3, Total gates=19
smg_comms_packing... 199 T01 := X0 and X3;
smg_comms_packing... 200 T02 := X1 xor X2;
smg_comms_packing... 201 T03 := X0 xor X3;
smg_comms_packing... 202 T04 := T01 xor T02;
smg_comms_packing... 203 T05 := X1 or X2;
smg_comms_packing... 204 X := not T04;
smg_comms_packing... 205 T07 := T03 and T05;
smg_comms_packing... 206 T08 := X1 and x;
smg_comms_packing... 207 T09 := X0 or X2;
smg_comms_packing... 208 T10 := T07 xor T08;
smg_comms_packing... 209 T11 := X1 or X3;
smg_comms_packing... 210 T12 := X2 xor T11;
smg_comms_packing... 211 T13 := T09 xor T10;
smg_comms_packing... 212 Y := not T13;
smg_comms_packing... 213 T15 := X and T03;
smg_comms_packing... 214 Z := T12 xor T07;
smg_comms_packing... 215 T17 := X0 xor X1;
smg_comms_packing... 216 T18 := Y xor T15;
smg_comms_packing... 217 W := T17 xor T18;
smg_comms_packing... 218 elsif R = 7 then
smg_comms_packing... 219 -- S7: 1 13 15 0 14 8 2 11 7 4 12 10 9 3 5 6
smg_comms_packing... 220 -- depth = 10,7,10,4, Total gates=19
smg_comms_packing... 221 T01 := X0 and X2;
smg_comms_packing... 222 T02 := not X3;
smg_comms_packing... 223 T03 := X0 and T02;
smg_comms_packing... 224 T04 := X1 or T01;
smg_comms_packing... 225 T05 := X0 and X1;
smg_comms_packing... 226 T06 := X2 xor T04;
smg_comms_packing... 227 Z := T03 xor T06;
smg_comms_packing... 228 T08 := X2 or z;
smg_comms_packing... 229 T09 := X3 or T05;
smg_comms_packing... 230 T10 := X0 xor T08;
smg_comms_packing... 231 T11 := T04 and z;
smg_comms_packing... 232 X := T09 xor T10;
smg_comms_packing... 233 T13 := X1 xor x;
smg_comms_packing... 234 T14 := T01 xor x;
smg_comms_packing... 235 T15 := X2 xor T05;
smg_comms_packing... 236 T16 := T11 or T13;
smg_comms_packing... 237 T17 := T02 or T14;
smg_comms_packing... 238 W := T15 xor T17;
smg_comms_packing... 239 Y := X0 xor T16;
smg_comms_packing... 240 end if;
smg_comms_packing... 241 X0 := W;
smg_comms_packing... 242 X1 := X;
smg_comms_packing... 243 X2 := Y;
smg_comms_packing... 244 X3 := Z;
smg_comms_packing... 245 end S;
smg_comms_packing... 246
smg_comms_packing... 247
smg_comms_packing... 248 -- Inverse Sbox function
smg_comms_packing... 249
smg_comms_packing... 250 procedure SI (R : Integer; X0, X1, X2, X3 : in out Unsigned_32) is
smg_comms_packing... 251 T01, T02, T03, T04, T05, T06, T07, T08, T09,
smg_comms_packing... 252 T10, T11, T12, T13, T14, T15, T16, T17, T18 : Unsigned_32;
smg_comms_packing... 253 W, X, Y, Z : Unsigned_32;
smg_comms_packing... 254 begin
smg_comms_packing... 255 if R = 0 then
smg_comms_packing... 256 -- InvS0: 13 3 11 0 10 6 5 12 1 14 4 7 15 9 8 2
smg_comms_packing... 257 -- depth = 8,4,3,6, Total gates=19
smg_comms_packing... 258 T01 := X2 xor X3;
smg_comms_packing... 259 T02 := X0 or X1;
smg_comms_packing... 260 T03 := X1 or X2;
smg_comms_packing... 261 T04 := X2 and T01;
smg_comms_packing... 262 T05 := T02 xor T01;
smg_comms_packing... 263 T06 := X0 or T04;
smg_comms_packing... 264 Y := not T05;
smg_comms_packing... 265 T08 := X1 xor X3;
smg_comms_packing... 266 T09 := T03 and T08;
smg_comms_packing... 267 T10 := X3 or y;
smg_comms_packing... 268 X := T09 xor T06;
smg_comms_packing... 269 T12 := X0 or T05;
smg_comms_packing... 270 T13 := X xor T12;
smg_comms_packing... 271 T14 := T03 xor T10;
smg_comms_packing... 272 T15 := X0 xor X2;
smg_comms_packing... 273 Z := T14 xor T13;
smg_comms_packing... 274 T17 := T05 and T13;
smg_comms_packing... 275 T18 := T14 or T17;
smg_comms_packing... 276 W := T15 xor T18;
smg_comms_packing... 277 elsif R = 1 then
smg_comms_packing... 278 -- InvS1: 5 8 2 14 15 6 12 3 11 4 7 9 1 13 10 0
smg_comms_packing... 279 -- depth = 7,4,5,3, Total gates=18
smg_comms_packing... 280 T01 := X0 xor X1;
smg_comms_packing... 281 T02 := X1 or X3;
smg_comms_packing... 282 T03 := X0 and X2;
smg_comms_packing... 283 T04 := X2 xor T02;
smg_comms_packing... 284 T05 := X0 or T04;
smg_comms_packing... 285 T06 := T01 and T05;
smg_comms_packing... 286 T07 := X3 or T03;
smg_comms_packing... 287 T08 := X1 xor T06;
smg_comms_packing... 288 T09 := T07 xor T06;
smg_comms_packing... 289 T10 := T04 or T03;
smg_comms_packing... 290 T11 := X3 and T08;
smg_comms_packing... 291 Y := not T09;
smg_comms_packing... 292 X := T10 xor T11;
smg_comms_packing... 293 T14 := X0 or y;
smg_comms_packing... 294 T15 := T06 xor x;
smg_comms_packing... 295 Z := T01 xor T04;
smg_comms_packing... 296 T17 := X2 xor T15;
smg_comms_packing... 297 W := T14 xor T17;
smg_comms_packing... 298 elsif R = 2 then
smg_comms_packing... 299 -- InvS2: 12 9 15 4 11 14 1 2 0 3 6 13 5 8 10 7
smg_comms_packing... 300 -- depth = 3,6,8,3, Total gates=18
smg_comms_packing... 301 T01 := X0 xor X3;
smg_comms_packing... 302 T02 := X2 xor X3;
smg_comms_packing... 303 T03 := X0 and X2;
smg_comms_packing... 304 T04 := X1 or T02;
smg_comms_packing... 305 W := T01 xor T04;
smg_comms_packing... 306 T06 := X0 or X2;
smg_comms_packing... 307 T07 := X3 or w;
smg_comms_packing... 308 T08 := not X3;
smg_comms_packing... 309 T09 := X1 and T06;
smg_comms_packing... 310 T10 := T08 or T03;
smg_comms_packing... 311 T11 := X1 and T07;
smg_comms_packing... 312 T12 := T06 and T02;
smg_comms_packing... 313 Z := T09 xor T10;
smg_comms_packing... 314 X := T12 xor T11;
smg_comms_packing... 315 T15 := X2 and z;
smg_comms_packing... 316 T16 := W xor x;
smg_comms_packing... 317 T17 := T10 xor T15;
smg_comms_packing... 318 Y := T16 xor T17;
smg_comms_packing... 319 elsif R = 3 then
smg_comms_packing... 320 -- InvS3: 0 9 10 7 11 14 6 13 3 5 12 2 4 8 15 1
smg_comms_packing... 321 -- depth = 3,6,4,4, Total gates=17
smg_comms_packing... 322 T01 := X2 or X3;
smg_comms_packing... 323 T02 := X0 or X3;
smg_comms_packing... 324 T03 := X2 xor T02;
smg_comms_packing... 325 T04 := X1 xor T02;
smg_comms_packing... 326 T05 := X0 xor X3;
smg_comms_packing... 327 T06 := T04 and T03;
smg_comms_packing... 328 T07 := X1 and T01;
smg_comms_packing... 329 Y := T05 xor T06;
smg_comms_packing... 330 T09 := X0 xor T03;
smg_comms_packing... 331 W := T07 xor T03;
smg_comms_packing... 332 T11 := W or T05;
smg_comms_packing... 333 T12 := T09 and T11;
smg_comms_packing... 334 T13 := X0 and y;
smg_comms_packing... 335 T14 := T01 xor T05;
smg_comms_packing... 336 X := X1 xor T12;
smg_comms_packing... 337 T16 := X1 or T13;
smg_comms_packing... 338 Z := T14 xor T16;
smg_comms_packing... 339 elsif R = 4 then
smg_comms_packing... 340 -- InvS4: 5 0 8 3 10 9 7 14 2 12 11 6 4 15 13 1
smg_comms_packing... 341 -- depth = 6,4,7,3, Total gates=17
smg_comms_packing... 342 T01 := X1 or X3;
smg_comms_packing... 343 T02 := X2 or X3;
smg_comms_packing... 344 T03 := X0 and T01;
smg_comms_packing... 345 T04 := X1 xor T02;
smg_comms_packing... 346 T05 := X2 xor X3;
smg_comms_packing... 347 T06 := not T03;
smg_comms_packing... 348 T07 := X0 and T04;
smg_comms_packing... 349 X := T05 xor T07;
smg_comms_packing... 350 T09 := X or T06;
smg_comms_packing... 351 T10 := X0 xor T07;
smg_comms_packing... 352 T11 := T01 xor T09;
smg_comms_packing... 353 T12 := X3 xor T04;
smg_comms_packing... 354 T13 := X2 or T10;
smg_comms_packing... 355 Z := T03 xor T12;
smg_comms_packing... 356 T15 := X0 xor T04;
smg_comms_packing... 357 Y := T11 xor T13;
smg_comms_packing... 358 W := T15 xor T09;
smg_comms_packing... 359 elsif R = 5 then
smg_comms_packing... 360 -- InvS5: 8 15 2 9 4 1 13 14 11 6 5 3 7 12 10 0
smg_comms_packing... 361 -- depth = 4,6,9,7, Total gates=17
smg_comms_packing... 362 T01 := X0 and X3;
smg_comms_packing... 363 T02 := X2 xor T01;
smg_comms_packing... 364 T03 := X0 xor X3;
smg_comms_packing... 365 T04 := X1 and T02;
smg_comms_packing... 366 T05 := X0 and X2;
smg_comms_packing... 367 W := T03 xor T04;
smg_comms_packing... 368 T07 := X0 and w;
smg_comms_packing... 369 T08 := T01 xor w;
smg_comms_packing... 370 T09 := X1 or T05;
smg_comms_packing... 371 T10 := not X1;
smg_comms_packing... 372 X := T08 xor T09;
smg_comms_packing... 373 T12 := T10 or T07;
smg_comms_packing... 374 T13 := W or x;
smg_comms_packing... 375 Z := T02 xor T12;
smg_comms_packing... 376 T15 := T02 xor T13;
smg_comms_packing... 377 T16 := X1 xor X3;
smg_comms_packing... 378 Y := T16 xor T15;
smg_comms_packing... 379 elsif R = 6 then
smg_comms_packing... 380 -- InvS6: 15 10 1 13 5 3 6 0 4 9 14 7 2 12 8 11
smg_comms_packing... 381 -- depth = 5,3,8,6, Total gates=19
smg_comms_packing... 382 T01 := X0 xor X2;
smg_comms_packing... 383 T02 := not X2;
smg_comms_packing... 384 T03 := X1 and T01;
smg_comms_packing... 385 T04 := X1 or T02;
smg_comms_packing... 386 T05 := X3 or T03;
smg_comms_packing... 387 T06 := X1 xor X3;
smg_comms_packing... 388 T07 := X0 and T04;
smg_comms_packing... 389 T08 := X0 or T02;
smg_comms_packing... 390 T09 := T07 xor T05;
smg_comms_packing... 391 X := T06 xor T08;
smg_comms_packing... 392 W := not T09;
smg_comms_packing... 393 T12 := X1 and w;
smg_comms_packing... 394 T13 := T01 and T05;
smg_comms_packing... 395 T14 := T01 xor T12;
smg_comms_packing... 396 T15 := T07 xor T13;
smg_comms_packing... 397 T16 := X3 or T02;
smg_comms_packing... 398 T17 := X0 xor x;
smg_comms_packing... 399 Z := T17 xor T15;
smg_comms_packing... 400 Y := T16 xor T14;
smg_comms_packing... 401 elsif R = 7 then
smg_comms_packing... 402 -- InvS7: 3 0 6 13 9 14 15 8 5 12 11 7 10 1 4 2
smg_comms_packing... 403 -- depth := 9,7,3,3, Total gates:=18
smg_comms_packing... 404 T01 := X0 and X1;
smg_comms_packing... 405 T02 := X0 or X1;
smg_comms_packing... 406 T03 := X2 or T01;
smg_comms_packing... 407 T04 := X3 and T02;
smg_comms_packing... 408 Z := T03 xor T04;
smg_comms_packing... 409 T06 := X1 xor T04;
smg_comms_packing... 410 T07 := X3 xor z;
smg_comms_packing... 411 T08 := not T07;
smg_comms_packing... 412 T09 := T06 or T08;
smg_comms_packing... 413 T10 := X1 xor X3;
smg_comms_packing... 414 T11 := X0 or X3;
smg_comms_packing... 415 X := X0 xor T09;
smg_comms_packing... 416 T13 := X2 xor T06;
smg_comms_packing... 417 T14 := X2 and T11;
smg_comms_packing... 418 T15 := X3 or x;
smg_comms_packing... 419 T16 := T01 or T10;
smg_comms_packing... 420 W := T13 xor T15;
smg_comms_packing... 421 Y := T14 xor T16;
smg_comms_packing... 422 end if;
smg_comms_packing... 423 X0 := W;
smg_comms_packing... 424 X1 := X;
smg_comms_packing... 425 X2 := Y;
smg_comms_packing... 426 X3 := Z;
smg_comms_packing... 427 end SI;
smg_comms_packing... 428
smg_comms_packing... 429
smg_comms_packing... 430 -- Linear Transform
smg_comms_packing... 431
smg_comms_packing... 432 procedure Tr (X0, X1, X2, X3 : in out Unsigned_32) is
smg_comms_packing... 433 begin
smg_comms_packing... 434 X0 := Rotate_Left(X0, 13);
smg_comms_packing... 435 X2 := Rotate_Left(X2, 3);
smg_comms_packing... 436 X1 := X1 xor X0 xor X2;
smg_comms_packing... 437 X3 := X3 xor X2 xor Shift_Left(X0, 3);
smg_comms_packing... 438 X1 := Rotate_Left(X1, 1);
smg_comms_packing... 439 X3 := Rotate_Left(X3, 7);
smg_comms_packing... 440 X0 := X0 xor X1 xor X3;
smg_comms_packing... 441 X2 := X2 xor X3 xor Shift_Left(X1, 7);
smg_comms_packing... 442 X0 := Rotate_Left(X0, 5);
smg_comms_packing... 443 X2 := Rotate_Left(X2, 22);
smg_comms_packing... 444 end Tr;
smg_comms_packing... 445
smg_comms_packing... 446
smg_comms_packing... 447 -- Inverse Linear Transform
smg_comms_packing... 448
smg_comms_packing... 449 procedure TrI (X0, X1, X2, X3 : in out Unsigned_32) is
smg_comms_packing... 450 begin
smg_comms_packing... 451 X2 := Rotate_Right(X2, 22);
smg_comms_packing... 452 X0 := Rotate_Right(X0, 5);
smg_comms_packing... 453 X2 := X2 xor X3 xor Shift_Left(X1, 7);
smg_comms_packing... 454 X0 := X0 xor X1 xor X3;
smg_comms_packing... 455 X3 := Rotate_Right(X3, 7);
smg_comms_packing... 456 X1 := Rotate_Right(X1, 1);
smg_comms_packing... 457 X3 := X3 xor X2 xor Shift_Left(X0, 3);
smg_comms_packing... 458 X1 := X1 xor X0 xor X2;
smg_comms_packing... 459 X2 := Rotate_Right(X2, 3);
smg_comms_packing... 460 X0 := Rotate_Right(X0, 13);
smg_comms_packing... 461 end TrI;
smg_comms_packing... 462
smg_comms_packing... 463
smg_comms_packing... 464 procedure Keying (W : Key_Schedule;
smg_comms_packing... 465 R : Integer;
smg_comms_packing... 466 X0, X1, X2, X3 : in out Unsigned_32) is
smg_comms_packing... 467 begin
smg_comms_packing... 468 X0 := X0 xor W(4*R);
smg_comms_packing... 469 X1 := X1 xor W(4*R+1);
smg_comms_packing... 470 X2 := X2 xor W(4*R+2);
smg_comms_packing... 471 X3 := X3 xor W(4*R+3);
smg_comms_packing... 472 end Keying;
smg_comms_packing... 473
smg_comms_packing... 474
smg_comms_packing... 475 pragma Inline(S, SI, Tr, TrI, Keying);
smg_comms_packing... 476
smg_comms_packing... 477
smg_comms_packing... 478 procedure Prepare_Key (K : in Key; W : out Key_Schedule) is
smg_comms_packing... 479 begin
smg_comms_packing... 480 for I in 0..7 loop
smg_comms_packing... 481 W(-8+I) := Bytes_To_Word(K(4*I .. 4*I+3));
smg_comms_packing... 482 end loop;
smg_comms_packing... 483 for I in 0..131 loop
smg_comms_packing... 484 W(I) := Rotate_Left(W(I-8) xor W(I-5) xor W(I-3) xor W(I-1) xor
smg_comms_packing... 485 16#9e3779b9# xor Unsigned_32(I), 11);
smg_comms_packing... 486 end loop;
smg_comms_packing... 487 S(3, W( 0), W( 1), W( 2), W( 3));
smg_comms_packing... 488 S(2, W( 4), W( 5), W( 6), W( 7));
smg_comms_packing... 489 S(1, W( 8), W( 9), W( 10), W( 11));
smg_comms_packing... 490 S(0, W( 12), W( 13), W( 14), W( 15));
smg_comms_packing... 491 S(7, W( 16), W( 17), W( 18), W( 19));
smg_comms_packing... 492 S(6, W( 20), W( 21), W( 22), W( 23));
smg_comms_packing... 493 S(5, W( 24), W( 25), W( 26), W( 27));
smg_comms_packing... 494 S(4, W( 28), W( 29), W( 30), W( 31));
smg_comms_packing... 495 S(3, W( 32), W( 33), W( 34), W( 35));
smg_comms_packing... 496 S(2, W( 36), W( 37), W( 38), W( 39));
smg_comms_packing... 497 S(1, W( 40), W( 41), W( 42), W( 43));
smg_comms_packing... 498 S(0, W( 44), W( 45), W( 46), W( 47));
smg_comms_packing... 499 S(7, W( 48), W( 49), W( 50), W( 51));
smg_comms_packing... 500 S(6, W( 52), W( 53), W( 54), W( 55));
smg_comms_packing... 501 S(5, W( 56), W( 57), W( 58), W( 59));
smg_comms_packing... 502 S(4, W( 60), W( 61), W( 62), W( 63));
smg_comms_packing... 503 S(3, W( 64), W( 65), W( 66), W( 67));
smg_comms_packing... 504 S(2, W( 68), W( 69), W( 70), W( 71));
smg_comms_packing... 505 S(1, W( 72), W( 73), W( 74), W( 75));
smg_comms_packing... 506 S(0, W( 76), W( 77), W( 78), W( 79));
smg_comms_packing... 507 S(7, W( 80), W( 81), W( 82), W( 83));
smg_comms_packing... 508 S(6, W( 84), W( 85), W( 86), W( 87));
smg_comms_packing... 509 S(5, W( 88), W( 89), W( 90), W( 91));
smg_comms_packing... 510 S(4, W( 92), W( 93), W( 94), W( 95));
smg_comms_packing... 511 S(3, W( 96), W( 97), W( 98), W( 99));
smg_comms_packing... 512 S(2, W(100), W(101), W(102), W(103));
smg_comms_packing... 513 S(1, W(104), W(105), W(106), W(107));
smg_comms_packing... 514 S(0, W(108), W(109), W(110), W(111));
smg_comms_packing... 515 S(7, W(112), W(113), W(114), W(115));
smg_comms_packing... 516 S(6, W(116), W(117), W(118), W(119));
smg_comms_packing... 517 S(5, W(120), W(121), W(122), W(123));
smg_comms_packing... 518 S(4, W(124), W(125), W(126), W(127));
smg_comms_packing... 519 S(3, W(128), W(129), W(130), W(131));
smg_comms_packing... 520 end Prepare_Key;
smg_comms_packing... 521
smg_comms_packing... 522
smg_comms_packing... 523 procedure Encrypt (W : in Key_Schedule; Plaintext : in Block;
smg_comms_packing... 524 Ciphertext : out Block) is
smg_comms_packing... 525 X0, X1, X2, X3 : Unsigned_32;
smg_comms_packing... 526 begin
smg_comms_packing... 527 X0 := Bytes_To_Word(Plaintext( 0 .. 3));
smg_comms_packing... 528 X1 := Bytes_To_Word(Plaintext( 4 .. 7));
smg_comms_packing... 529 X2 := Bytes_To_Word(Plaintext( 8 .. 11));
smg_comms_packing... 530 X3 := Bytes_To_Word(Plaintext(12 .. 15));
smg_comms_packing... 531
smg_comms_packing... 532 Keying(W, 0, X0, X1, X2, X3); S(0, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 533 Keying(W, 1, X0, X1, X2, X3); S(1, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 534 Keying(W, 2, X0, X1, X2, X3); S(2, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 535 Keying(W, 3, X0, X1, X2, X3); S(3, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 536 Keying(W, 4, X0, X1, X2, X3); S(4, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 537 Keying(W, 5, X0, X1, X2, X3); S(5, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 538 Keying(W, 6, X0, X1, X2, X3); S(6, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 539 Keying(W, 7, X0, X1, X2, X3); S(7, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 540 Keying(W, 8, X0, X1, X2, X3); S(0, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 541 Keying(W, 9, X0, X1, X2, X3); S(1, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 542 Keying(W, 10, X0, X1, X2, X3); S(2, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 543 Keying(W, 11, X0, X1, X2, X3); S(3, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 544 Keying(W, 12, X0, X1, X2, X3); S(4, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 545 Keying(W, 13, X0, X1, X2, X3); S(5, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 546 Keying(W, 14, X0, X1, X2, X3); S(6, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 547 Keying(W, 15, X0, X1, X2, X3); S(7, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 548 Keying(W, 16, X0, X1, X2, X3); S(0, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 549 Keying(W, 17, X0, X1, X2, X3); S(1, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 550 Keying(W, 18, X0, X1, X2, X3); S(2, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 551 Keying(W, 19, X0, X1, X2, X3); S(3, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 552 Keying(W, 20, X0, X1, X2, X3); S(4, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 553 Keying(W, 21, X0, X1, X2, X3); S(5, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 554 Keying(W, 22, X0, X1, X2, X3); S(6, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 555 Keying(W, 23, X0, X1, X2, X3); S(7, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 556 Keying(W, 24, X0, X1, X2, X3); S(0, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 557 Keying(W, 25, X0, X1, X2, X3); S(1, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 558 Keying(W, 26, X0, X1, X2, X3); S(2, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 559 Keying(W, 27, X0, X1, X2, X3); S(3, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 560 Keying(W, 28, X0, X1, X2, X3); S(4, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 561 Keying(W, 29, X0, X1, X2, X3); S(5, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 562 Keying(W, 30, X0, X1, X2, X3); S(6, X0, X1, X2, X3); Tr(X0, X1, X2, X3);
smg_comms_packing... 563 Keying(W, 31, X0, X1, X2, X3);
smg_comms_packing... 564 S(7, X0, X1, X2, X3);
smg_comms_packing... 565 Keying(W, 32, X0, X1, X2, X3);
smg_comms_packing... 566
smg_comms_packing... 567 Ciphertext( 0 .. 3) := Word_To_Bytes(X0);
smg_comms_packing... 568 Ciphertext( 4 .. 7) := Word_To_Bytes(X1);
smg_comms_packing... 569 Ciphertext( 8 .. 11) := Word_To_Bytes(X2);
smg_comms_packing... 570 Ciphertext(12 .. 15) := Word_To_Bytes(X3);
smg_comms_packing... 571 end Encrypt;
smg_comms_packing... 572
smg_comms_packing... 573
smg_comms_packing... 574 procedure Decrypt (W : in Key_Schedule; Ciphertext : in Block;
smg_comms_packing... 575 Plaintext : out Block) is
smg_comms_packing... 576 X0, X1, X2, X3 : Unsigned_32;
smg_comms_packing... 577 begin
smg_comms_packing... 578 X0 := Bytes_To_Word(Ciphertext( 0 .. 3));
smg_comms_packing... 579 X1 := Bytes_To_Word(Ciphertext( 4 .. 7));
smg_comms_packing... 580 X2 := Bytes_To_Word(Ciphertext( 8 .. 11));
smg_comms_packing... 581 X3 := Bytes_To_Word(Ciphertext(12 .. 15));
smg_comms_packing... 582
smg_comms_packing... 583 Keying(W, 32, X0, X1, X2, X3);
smg_comms_packing... 584 SI(7, X0, X1, X2, X3);
smg_comms_packing... 585 Keying(W, 31, X0, X1, X2, X3);
smg_comms_packing... 586 TrI(X0, X1, X2, X3); SI(6, X0, X1, X2, X3); Keying(W,30, X0, X1, X2, X3);
smg_comms_packing... 587 TrI(X0, X1, X2, X3); SI(5, X0, X1, X2, X3); Keying(W,29, X0, X1, X2, X3);
smg_comms_packing... 588 TrI(X0, X1, X2, X3); SI(4, X0, X1, X2, X3); Keying(W,28, X0, X1, X2, X3);
smg_comms_packing... 589 TrI(X0, X1, X2, X3); SI(3, X0, X1, X2, X3); Keying(W,27, X0, X1, X2, X3);
smg_comms_packing... 590 TrI(X0, X1, X2, X3); SI(2, X0, X1, X2, X3); Keying(W,26, X0, X1, X2, X3);
smg_comms_packing... 591 TrI(X0, X1, X2, X3); SI(1, X0, X1, X2, X3); Keying(W,25, X0, X1, X2, X3);
smg_comms_packing... 592 TrI(X0, X1, X2, X3); SI(0, X0, X1, X2, X3); Keying(W,24, X0, X1, X2, X3);
smg_comms_packing... 593 TrI(X0, X1, X2, X3); SI(7, X0, X1, X2, X3); Keying(W,23, X0, X1, X2, X3);
smg_comms_packing... 594 TrI(X0, X1, X2, X3); SI(6, X0, X1, X2, X3); Keying(W,22, X0, X1, X2, X3);
smg_comms_packing... 595 TrI(X0, X1, X2, X3); SI(5, X0, X1, X2, X3); Keying(W,21, X0, X1, X2, X3);
smg_comms_packing... 596 TrI(X0, X1, X2, X3); SI(4, X0, X1, X2, X3); Keying(W,20, X0, X1, X2, X3);
smg_comms_packing... 597 TrI(X0, X1, X2, X3); SI(3, X0, X1, X2, X3); Keying(W,19, X0, X1, X2, X3);
smg_comms_packing... 598 TrI(X0, X1, X2, X3); SI(2, X0, X1, X2, X3); Keying(W,18, X0, X1, X2, X3);
smg_comms_packing... 599 TrI(X0, X1, X2, X3); SI(1, X0, X1, X2, X3); Keying(W,17, X0, X1, X2, X3);
smg_comms_packing... 600 TrI(X0, X1, X2, X3); SI(0, X0, X1, X2, X3); Keying(W,16, X0, X1, X2, X3);
smg_comms_packing... 601 TrI(X0, X1, X2, X3); SI(7, X0, X1, X2, X3); Keying(W,15, X0, X1, X2, X3);
smg_comms_packing... 602 TrI(X0, X1, X2, X3); SI(6, X0, X1, X2, X3); Keying(W,14, X0, X1, X2, X3);
smg_comms_packing... 603 TrI(X0, X1, X2, X3); SI(5, X0, X1, X2, X3); Keying(W,13, X0, X1, X2, X3);
smg_comms_packing... 604 TrI(X0, X1, X2, X3); SI(4, X0, X1, X2, X3); Keying(W,12, X0, X1, X2, X3);
smg_comms_packing... 605 TrI(X0, X1, X2, X3); SI(3, X0, X1, X2, X3); Keying(W,11, X0, X1, X2, X3);
smg_comms_packing... 606 TrI(X0, X1, X2, X3); SI(2, X0, X1, X2, X3); Keying(W,10, X0, X1, X2, X3);
smg_comms_packing... 607 TrI(X0, X1, X2, X3); SI(1, X0, X1, X2, X3); Keying(W, 9, X0, X1, X2, X3);
smg_comms_packing... 608 TrI(X0, X1, X2, X3); SI(0, X0, X1, X2, X3); Keying(W, 8, X0, X1, X2, X3);
smg_comms_packing... 609 TrI(X0, X1, X2, X3); SI(7, X0, X1, X2, X3); Keying(W, 7, X0, X1, X2, X3);
smg_comms_packing... 610 TrI(X0, X1, X2, X3); SI(6, X0, X1, X2, X3); Keying(W, 6, X0, X1, X2, X3);
smg_comms_packing... 611 TrI(X0, X1, X2, X3); SI(5, X0, X1, X2, X3); Keying(W, 5, X0, X1, X2, X3);
smg_comms_packing... 612 TrI(X0, X1, X2, X3); SI(4, X0, X1, X2, X3); Keying(W, 4, X0, X1, X2, X3);
smg_comms_packing... 613 TrI(X0, X1, X2, X3); SI(3, X0, X1, X2, X3); Keying(W, 3, X0, X1, X2, X3);
smg_comms_packing... 614 TrI(X0, X1, X2, X3); SI(2, X0, X1, X2, X3); Keying(W, 2, X0, X1, X2, X3);
smg_comms_packing... 615 TrI(X0, X1, X2, X3); SI(1, X0, X1, X2, X3); Keying(W, 1, X0, X1, X2, X3);
smg_comms_packing... 616 TrI(X0, X1, X2, X3); SI(0, X0, X1, X2, X3); Keying(W, 0, X0, X1, X2, X3);
smg_comms_packing... 617
smg_comms_packing... 618 Plaintext( 0 .. 3) := Word_To_Bytes(X0);
smg_comms_packing... 619 Plaintext( 4 .. 7) := Word_To_Bytes(X1);
smg_comms_packing... 620 Plaintext( 8 .. 11) := Word_To_Bytes(X2);
smg_comms_packing... 621 Plaintext(12 .. 15) := Word_To_Bytes(X3);
smg_comms_packing... 622 end Decrypt;
smg_comms_packing... 623
smg_comms_packing... 624 end Serpent;