001: package org.bouncycastle.crypto.engines;
002:
003: import org.bouncycastle.crypto.CipherParameters;
004: import org.bouncycastle.crypto.DataLengthException;
005: import org.bouncycastle.crypto.StreamCipher;
006: import org.bouncycastle.crypto.params.KeyParameter;
007:
008: /**
009: * Implementation of Bob Jenkin's ISAAC (Indirection Shift Accumulate Add and Count).
010: * see: http://www.burtleburtle.net/bob/rand/isaacafa.html
011: */
012: public class ISAACEngine implements StreamCipher {
013: // Constants
014: private final int sizeL = 8, stateArraySize = sizeL << 5; // 256
015:
016: // Cipher's internal state
017: private int[] engineState = null, // mm
018: results = null; // randrsl
019: private int a = 0, b = 0, c = 0;
020:
021: // Engine state
022: private int index = 0;
023: private byte[] keyStream = new byte[stateArraySize << 2], // results expanded into bytes
024: workingKey = null;
025: private boolean initialised = false;
026:
027: /**
028: * initialise an ISAAC cipher.
029: *
030: * @param forEncryption whether or not we are for encryption.
031: * @param params the parameters required to set up the cipher.
032: * @exception IllegalArgumentException if the params argument is
033: * inappropriate.
034: */
035: public void init(boolean forEncryption, CipherParameters params) {
036: if (!(params instanceof KeyParameter)) {
037: throw new IllegalArgumentException(
038: "invalid parameter passed to ISAAC init - "
039: + params.getClass().getName());
040: }
041: /*
042: * ISAAC encryption and decryption is completely
043: * symmetrical, so the 'forEncryption' is
044: * irrelevant.
045: */
046: KeyParameter p = (KeyParameter) params;
047: setKey(p.getKey());
048:
049: return;
050: }
051:
052: public byte returnByte(byte in) {
053: if (index == 0) {
054: isaac();
055: keyStream = intToByteLittle(results);
056: }
057: byte out = (byte) (keyStream[index] ^ in);
058: index = (index + 1) & 1023;
059:
060: return out;
061: }
062:
063: public void processBytes(byte[] in, int inOff, int len, byte[] out,
064: int outOff) {
065: if (!initialised) {
066: throw new IllegalStateException(getAlgorithmName()
067: + " not initialised");
068: }
069:
070: if ((inOff + len) > in.length) {
071: throw new DataLengthException("input buffer too short");
072: }
073:
074: if ((outOff + len) > out.length) {
075: throw new DataLengthException("output buffer too short");
076: }
077:
078: for (int i = 0; i < len; i++) {
079: if (index == 0) {
080: isaac();
081: keyStream = intToByteLittle(results);
082: }
083: out[i + outOff] = (byte) (keyStream[index] ^ in[i + inOff]);
084: index = (index + 1) & 1023;
085: }
086: }
087:
088: public String getAlgorithmName() {
089: return "ISAAC";
090: }
091:
092: public void reset() {
093: setKey(workingKey);
094: }
095:
096: // Private implementation
097: private void setKey(byte[] keyBytes) {
098: workingKey = keyBytes;
099:
100: if (engineState == null) {
101: engineState = new int[stateArraySize];
102: }
103:
104: if (results == null) {
105: results = new int[stateArraySize];
106: }
107:
108: int i, j, k;
109:
110: // Reset state
111: for (i = 0; i < stateArraySize; i++) {
112: engineState[i] = results[i] = 0;
113: }
114: a = b = c = 0;
115:
116: // Reset index counter for output
117: index = 0;
118:
119: // Convert the key bytes to ints and put them into results[] for initialization
120: byte[] t = new byte[keyBytes.length + (keyBytes.length & 3)];
121: System.arraycopy(keyBytes, 0, t, 0, keyBytes.length);
122: for (i = 0; i < t.length; i += 4) {
123: results[i >> 2] = byteToIntLittle(t, i);
124: }
125:
126: // It has begun?
127: int[] abcdefgh = new int[sizeL];
128:
129: for (i = 0; i < sizeL; i++) {
130: abcdefgh[i] = 0x9e3779b9; // Phi (golden ratio)
131: }
132:
133: for (i = 0; i < 4; i++) {
134: mix(abcdefgh);
135: }
136:
137: for (i = 0; i < 2; i++) {
138: for (j = 0; j < stateArraySize; j += sizeL) {
139: for (k = 0; k < sizeL; k++) {
140: abcdefgh[k] += (i < 1) ? results[j + k]
141: : engineState[j + k];
142: }
143:
144: mix(abcdefgh);
145:
146: for (k = 0; k < sizeL; k++) {
147: engineState[j + k] = abcdefgh[k];
148: }
149: }
150: }
151:
152: isaac();
153:
154: initialised = true;
155: }
156:
157: private void isaac() {
158: int i, x, y;
159:
160: b += ++c;
161: for (i = 0; i < stateArraySize; i++) {
162: x = engineState[i];
163: switch (i & 3) {
164: case 0:
165: a ^= (a << 13);
166: break;
167: case 1:
168: a ^= (a >>> 6);
169: break;
170: case 2:
171: a ^= (a << 2);
172: break;
173: case 3:
174: a ^= (a >>> 16);
175: break;
176: }
177: a += engineState[(i + 128) & 0xFF];
178: engineState[i] = y = engineState[(x >>> 2) & 0xFF] + a + b;
179: results[i] = b = engineState[(y >>> 10) & 0xFF] + x;
180: }
181: }
182:
183: private void mix(int[] x) {
184: x[0] ^= x[1] << 11;
185: x[3] += x[0];
186: x[1] += x[2];
187: x[1] ^= x[2] >>> 2;
188: x[4] += x[1];
189: x[2] += x[3];
190: x[2] ^= x[3] << 8;
191: x[5] += x[2];
192: x[3] += x[4];
193: x[3] ^= x[4] >>> 16;
194: x[6] += x[3];
195: x[4] += x[5];
196: x[4] ^= x[5] << 10;
197: x[7] += x[4];
198: x[5] += x[6];
199: x[5] ^= x[6] >>> 4;
200: x[0] += x[5];
201: x[6] += x[7];
202: x[6] ^= x[7] << 8;
203: x[1] += x[6];
204: x[7] += x[0];
205: x[7] ^= x[0] >>> 9;
206: x[2] += x[7];
207: x[0] += x[1];
208: }
209:
210: private int byteToIntLittle(byte[] x, int offset) {
211: return (int) (x[offset++] & 0xFF) | ((x[offset++] & 0xFF) << 8)
212: | ((x[offset++] & 0xFF) << 16) | (x[offset++] << 24);
213: }
214:
215: private byte[] intToByteLittle(int x) {
216: byte[] out = new byte[4];
217: out[3] = (byte) x;
218: out[2] = (byte) (x >>> 8);
219: out[1] = (byte) (x >>> 16);
220: out[0] = (byte) (x >>> 24);
221: return out;
222: }
223:
224: private byte[] intToByteLittle(int[] x) {
225: byte[] out = new byte[4 * x.length];
226: for (int i = 0, j = 0; i < x.length; i++, j += 4) {
227: System.arraycopy(intToByteLittle(x[i]), 0, out, j, 4);
228: }
229: return out;
230: }
231: }
|