1 /**************************************************************************
3 * Java Grande Forum Benchmark Suite - Thread Version 1.0 *
7 * Java Grande Benchmarking Project *
11 * Edinburgh Parallel Computing Centre *
13 * email: epcc-javagrande@epcc.ed.ac.uk *
15 * Original version of this code by *
16 * Gabriel Zachmann (zach@igd.fhg.de) *
18 * This version copyright (c) The University of Edinburgh, 2001. *
19 * All rights reserved. *
21 **************************************************************************/
22 /**************************************************************************
23 * Ported for DSTM Benchmark *
24 **************************************************************************/
30 * This test performs IDEA encryption then decryption. IDEA stands
31 * for International Data Encryption Algorithm. The test is based
32 * on code presented in Applied Cryptography by Bruce Schnier,
33 * which was based on code developed by Xuejia Lai and James L.
46 // Declare class data. Byte buffer plain1 holds the original
47 // data for encryption, crypt1 holds the encrypted data, and
48 // plain2 holds the decrypted data, which should match plain1
53 byte [] plain1; // Buffer for plaintext data.
54 byte [] crypt1; // Buffer for encrypted data.
55 byte [] plain2; // Buffer for decrypted data.
57 short [] userkey; // Key for encryption/decryption.
58 int [] Z; // Encryption subkey (userkey derived).
59 int [] DK; // Decryption subkey (userkey derived).
66 IDEARunner th[] = new IDEARunner [JGFCryptBench.nthreads];
68 // Start the stopwatch.
69 JGFInstrumentor.startTimer("Section2:Crypt:Kernel");
72 for(int i=1;i<JGFCryptBench.nthreads;i++) {
73 th[i] = new IDEARunner(i,plain1,crypt1,Z);
77 th[0] = new IDEARunner(0,plain1,crypt1,Z);
81 for(int i=1;i<JGFCryptBench.nthreads;i++) {
86 for(int i=1;i<JGFCryptBench.nthreads;i++) {
87 th[i] = new IDEARunner(i,crypt1,plain2,DK);
91 th[0] = new IDEARunner(0,crypt1,plain2,DK);
95 for(int i=1;i<JGFCryptBench.nthreads;i++) {
100 // Stop the stopwatch.
101 JGFInstrumentor.stopTimer("Section2:Crypt:Kernel");
108 * Builds the data used for the test -- each time the test is run.
115 // Create three byte arrays that will be used (and reused) for
116 // encryption/decryption operations.
118 plain1 = new byte [array_rows];
119 crypt1 = new byte [array_rows];
120 plain2 = new byte [array_rows];
123 Random rndnum = new Random(136506717L); // Create random number generator.
126 // Allocate three arrays to hold keys: userkey is the 128-bit key.
127 // Z is the set of 16-bit encryption subkeys derived from userkey,
128 // while DK is the set of 16-bit decryption subkeys also derived
129 // from userkey. NOTE: The 16-bit values are stored here in
130 // 32-bit int arrays so that the values may be used in calculations
131 // as if they are unsigned. Each 64-bit block of plaintext goes
132 // through eight processing rounds involving six of the subkeys
133 // then a final output transform with four of the keys; (8 * 6)
136 userkey = new short [8]; // User key has 8 16-bit shorts.
137 Z = new int [52]; // Encryption subkey (user key derived).
138 DK = new int [52]; // Decryption subkey (user key derived).
140 // Generate user key randomly; eight 16-bit values in an array.
142 for (int i = 0; i < 8; i++)
144 // Again, the random number function returns int. Converting
145 // to a short type preserves the bit pattern in the lower 16
146 // bits of the int and discards the rest.
148 userkey[i] = (short) rndnum.nextInt();
151 // Compute encryption and decryption subkeys.
156 // Fill plain1 with "text."
157 for (int i = 0; i < array_rows; i++)
159 plain1[i] = (byte) i;
161 // Converting to a byte
162 // type preserves the bit pattern in the lower 8 bits of the
163 // int and discards the rest.
170 * Builds the 52 16-bit encryption subkeys Z[] from the user key and
171 * stores in 32-bit int array. The routing corrects an error in the
172 * source code in the Schnier book. Basically, the sense of the 7-
173 * and 9-bit shifts are reversed. It still works reversed, but would
174 * encrypted code would not decrypt with someone else's IDEA code.
177 private void calcEncryptKey()
179 int j; // Utility variable.
181 for (int i = 0; i < 52; i++) // Zero out the 52-int Z array.
184 for (int i = 0; i < 8; i++) // First 8 subkeys are userkey itself.
186 Z[i] = userkey[i] & 0xffff; // Convert "unsigned"
190 // Each set of 8 subkeys thereafter is derived from left rotating
191 // the whole 128-bit key 25 bits to left (once between each set of
192 // eight keys and then before the last four). Instead of actually
193 // rotating the whole key, this routine just grabs the 16 bits
194 // that are 25 bits to the right of the corresponding subkey
195 // eight positions below the current subkey. That 16-bit extent
196 // straddles two array members, so bits are shifted left in one
197 // member and right (with zero fill) in the other. For the last
198 // two subkeys in any group of eight, those 16 bits start to
199 // wrap around to the first two members of the previous eight.
201 for (int i = 8; i < 52; i++)
206 Z[i] = ((Z[i -7]>>>9) | (Z[i-6]<<7)) // Shift and combine.
207 & 0xFFFF; // Just 16 bits.
208 continue; // Next iteration.
211 if (j == 6) // Wrap to beginning for second chunk.
213 Z[i] = ((Z[i -7]>>>9) | (Z[i-14]<<7))
218 // j == 7 so wrap to beginning for both chunks.
220 Z[i] = ((Z[i -15]>>>9) | (Z[i-14]<<7))
228 * Builds the 52 16-bit encryption subkeys DK[] from the encryption-
229 * subkeys Z[]. DK[] is a 32-bit int array holding 16-bit values as
233 private void calcDecryptKey()
235 int j, k; // Index counters.
236 int t1, t2, t3; // Temps to hold decrypt subkeys.
238 t1 = inv(Z[0]); // Multiplicative inverse (mod x10001).
239 t2 = - Z[1] & 0xffff; // Additive inverse, 2nd encrypt subkey.
240 t3 = - Z[2] & 0xffff; // Additive inverse, 3rd encrypt subkey.
242 DK[51] = inv(Z[3]); // Multiplicative inverse (mod x10001).
247 j = 47; // Indices into temp and encrypt arrays.
249 for (int i = 0; i < 7; i++)
255 t2 = -Z[k++] & 0xffff;
256 t3 = -Z[k++] & 0xffff;
257 DK[j--] = inv(Z[k++]);
267 t2 = -Z[k++] & 0xffff;
268 t3 = -Z[k++] & 0xffff;
269 DK[j--] = inv(Z[k++]);
282 * Performs multiplication, modulo (2**16)+1. This code is structured
283 * on the assumption that untaken branches are cheaper than taken
284 * branches, and that the compiler doesn't schedule branches.
285 * Java: Must work with 32-bit int and one 64-bit long to keep
286 * 16-bit values and their products "unsigned." The routine assumes
287 * that both a and b could fit in 16 bits even though they come in
288 * as 32-bit ints. Lots of "& 0xFFFF" masks here to keep things 16-bit.
289 * Also, because the routine stores mod (2**16)+1 results in a 2**16
290 * space, the result is truncated to zero whenever the result would
291 * zero, be 2**16. And if one of the multiplicands is 0, the result
292 * is not zero, but (2**16) + 1 minus the other multiplicand (sort
293 * of an additive inverse mod 0x10001).
295 * NOTE: The java conversion of this routine works correctly, but
296 * is half the speed of using Java's modulus division function (%)
297 * on the multiplication with a 16-bit masking of the result--running
298 * in the Symantec Caje IDE. So it's not called for now; the test
299 * uses Java % instead.
302 private int mul(int a, int b)
305 long p; // Large enough to catch 16-bit multiply
306 // without hitting sign bit.
312 b = (int) p & 0xFFFF; // Lower 16 bits.
313 a = (int) p >>> 16; // Upper 16 bits.
315 return (b - a + 1) & 0xFFFF;
317 return (b - a) & 0xFFFF;
320 return ((1 - a) & 0xFFFF); // If b = 0, then same as
323 else // If a = 0, then return
324 return((1 - b) & 0xFFFF); // same as 0x10001 - b.
330 * Compute multiplicative inverse of x, modulo (2**16)+1 using
331 * extended Euclid's GCD (greatest common divisor) algorithm.
332 * It is unrolled twice to avoid swapping the meaning of
333 * the registers. And some subtracts are changed to adds.
334 * Java: Though it uses signed 32-bit ints, the interpretation
335 * of the bits within is strictly unsigned 16-bit.
338 private int inv(int x)
343 if (x <= 1) // Assumes positive x.
344 return(x); // 0 and 1 are self-inverse.
346 t1 = 0x10001 / x; // (2**16+1)/x; x is >= 2, so fits 16 bits.
349 return((1 - t1) & 0xFFFF);
356 if (x == 1) return(t0);
362 return((1 - t1) & 0xFFFF);
368 * Nulls arrays and forces garbage collection to free up memory.
380 //System.gc(); // Force garbage collection.
388 class IDEARunner extends Thread {
391 byte text1[],text2[];
393 public IDEARunner(int id, byte [] text1, byte [] text2, int [] key) {
402 * IDEA encryption/decryption algorithm. It processes plaintext in
403 * 64-bit blocks, one at a time, breaking the block into four 16-bit
404 * unsigned subblocks. It goes through eight rounds of processing
405 * using 6 new subkeys each time, plus four for last step. The source
406 * text is in array text1, the destination text goes into array text2
407 * The routine represents 16-bit subblocks and subkeys as type int so
408 * that they can be treated more easily as unsigned. Multiplication
409 * modulo 0x10001 interprets a zero sub-block as 0x10000; it must to
414 int ilow, iupper, slice, tslice, ttslice;
416 tslice = text1.length / 8;
417 ttslice = (tslice + JGFCryptBench.nthreads-1) / JGFCryptBench.nthreads;
421 iupper = (id+1)*slice;
422 if(iupper > text1.length) iupper = text1.length;
424 int i1 = ilow; // Index into first text array.
425 int i2 = ilow; // Index into second text array.
426 int ik; // Index into key array.
427 int x1, x2, x3, x4, t1, t2; // Four "16-bit" blocks, two temps.
428 int r; // Eight rounds of processing.
430 for (int i =ilow ; i <iupper ; i +=8)
433 ik = 0; // Restart key index.
434 r = 8; // Eight rounds of processing.
436 // Load eight plain1 bytes as four 16-bit "unsigned" integers.
437 // Masking with 0xff prevents sign extension with cast to int.
439 x1 = text1[i1++] & 0xff; // Build 16-bit x1 from 2 bytes,
440 x1 |= (text1[i1++] & 0xff) << 8; // assuming low-order byte first.
441 x2 = text1[i1++] & 0xff;
442 x2 |= (text1[i1++] & 0xff) << 8;
443 x3 = text1[i1++] & 0xff;
444 x3 |= (text1[i1++] & 0xff) << 8;
445 x4 = text1[i1++] & 0xff;
446 x4 |= (text1[i1++] & 0xff) << 8;
449 // 1) Multiply (modulo 0x10001), 1st text sub-block
450 // with 1st key sub-block.
452 x1 = (int) ((long) x1 * key[ik++] % 0x10001L & 0xffff);
454 // 2) Add (modulo 0x10000), 2nd text sub-block
455 // with 2nd key sub-block.
457 x2 = x2 + key[ik++] & 0xffff;
459 // 3) Add (modulo 0x10000), 3rd text sub-block
460 // with 3rd key sub-block.
462 x3 = x3 + key[ik++] & 0xffff;
464 // 4) Multiply (modulo 0x10001), 4th text sub-block
465 // with 4th key sub-block.
467 x4 = (int) ((long) x4 * key[ik++] % 0x10001L & 0xffff);
469 // 5) XOR results from steps 1 and 3.
473 // 6) XOR results from steps 2 and 4.
474 // Included in step 8.
476 // 7) Multiply (modulo 0x10001), result of step 5
477 // with 5th key sub-block.
479 t2 = (int) ((long) t2 * key[ik++] % 0x10001L & 0xffff);
481 // 8) Add (modulo 0x10000), results of steps 6 and 7.
483 t1 = t2 + (x2 ^ x4) & 0xffff;
485 // 9) Multiply (modulo 0x10001), result of step 8
486 // with 6th key sub-block.
488 t1 = (int) ((long) t1 * key[ik++] % 0x10001L & 0xffff);
490 // 10) Add (modulo 0x10000), results of steps 7 and 9.
492 t2 = t1 + t2 & 0xffff;
494 // 11) XOR results from steps 1 and 9.
498 // 14) XOR results from steps 4 and 10. (Out of order).
502 // 13) XOR results from steps 2 and 10. (Out of order).
506 // 12) XOR results from steps 3 and 9. (Out of order).
510 x3 = t2; // Results of x2 and x3 now swapped.
512 } while(--r != 0); // Repeats seven more rounds.
514 // Final output transform (4 steps).
516 // 1) Multiply (modulo 0x10001), 1st text-block
517 // with 1st key sub-block.
519 x1 = (int) ((long) x1 * key[ik++] % 0x10001L & 0xffff);
521 // 2) Add (modulo 0x10000), 2nd text sub-block
522 // with 2nd key sub-block. It says x3, but that is to undo swap
523 // of subblocks 2 and 3 in 8th processing round.
525 x3 = x3 + key[ik++] & 0xffff;
527 // 3) Add (modulo 0x10000), 3rd text sub-block
528 // with 3rd key sub-block. It says x2, but that is to undo swap
529 // of subblocks 2 and 3 in 8th processing round.
531 x2 = x2 + key[ik++] & 0xffff;
533 // 4) Multiply (modulo 0x10001), 4th text-block
534 // with 4th key sub-block.
536 x4 = (int) ((long) x4 * key[ik++] % 0x10001L & 0xffff);
538 // Repackage from 16-bit sub-blocks to 8-bit byte array text2.
540 text2[i2++] = (byte) x1;
541 text2[i2++] = (byte) (x1 >>> 8);
542 text2[i2++] = (byte) x3; // x3 and x2 are switched
543 text2[i2++] = (byte) (x3 >>> 8); // only in name.
544 text2[i2++] = (byte) x2;
545 text2[i2++] = (byte) (x2 >>> 8);
546 text2[i2++] = (byte) x4;
547 text2[i2++] = (byte) (x4 >>> 8);