001: package com.quadcap.crypto;
002:
003: /* Copyright 2002 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Random;
042:
043: import java.nio.ByteBuffer;
044: import java.nio.LongBuffer;
045:
046: import com.quadcap.util.text.Text;
047: import com.quadcap.util.Util;
048:
049: /**
050: * Implementation of Tiny Encryption Algorithm, modified to use longs
051: * instead of ints. SO the key is now four longs, or 256 bits, and the
052: * encryption/decryption proceeds 16 bytes at a time instead of 8.
053: *
054: * The number of rounds has been retained at 32.
055: *
056: * @author Stan Bailes
057: */
058: public class Tea256 extends AbstractSymmetricKey implements
059: SymmetricKey {
060: /** Computed as (sqrt(5)-1) * 2**63, following original Tea choice of
061: delta. */
062: static final long delta = 0x9E3779B97F4A7C15L;
063: static final int rounds = 32;
064:
065: long a, b, c, d;
066: long[] v = new long[2];
067:
068: /**
069: * Initialize key from serialized representation
070: */
071: public void init(String s) {
072: String[] vx = Text.extractN(s, "*:*:*:*:*");
073: a = Long.parseLong(vx[1]);
074: b = Long.parseLong(vx[2]);
075: c = Long.parseLong(vx[3]);
076: d = Long.parseLong(vx[4]);
077: }
078:
079: public void init(byte[] k) {
080: a = Util.bytesToLong(k, 0);
081: b = Util.bytesToLong(k, 8);
082: c = Util.bytesToLong(k, 16);
083: d = Util.bytesToLong(k, 24);
084: }
085:
086: /**
087: * Initialize: Create a random key
088: */
089: public void init(Random r) {
090: a = r.nextLong();
091: b = r.nextLong();
092: c = r.nextLong();
093: d = r.nextLong();
094: }
095:
096: /**
097: * Return the serialized form of the key
098: */
099: public String toString() {
100: return "TEA256:" + a + ":" + b + ":" + c + ":" + d;
101: }
102:
103: /**
104: * Encrypt a buffer (must be multiple of 16 bytes)
105: */
106: public void encrypt(ByteBuffer plain, ByteBuffer code) {
107: LongBuffer p = plain.asLongBuffer();
108: LongBuffer cb = code.asLongBuffer();
109: for (int i = 0; i < p.limit(); i += 2) {
110: v[0] = p.get(i);
111: v[1] = p.get(i + 1);
112: encrypt(v);
113: cb.put(i, v[0]);
114: cb.put(i + 1, v[1]);
115: }
116: }
117:
118: /**
119: * Decrypt a buffer (must be a multiple of 16 bytes)
120: */
121: public void decrypt(ByteBuffer code, ByteBuffer plain) {
122: LongBuffer p = plain.asLongBuffer();
123: LongBuffer cb = code.asLongBuffer();
124: for (int i = 0; i < cb.limit(); i += 2) {
125: v[0] = cb.get(i);
126: v[1] = cb.get(i + 1);
127: decrypt(v);
128: p.put(i, v[0]);
129: p.put(i + 1, v[1]);
130: }
131: }
132:
133: public int getBlockSize() {
134: return 16;
135: }
136:
137: final void encrypt(long[] v) {
138: long y = v[0];
139: long z = v[1];
140: long sum = 0;
141:
142: for (int n = rounds; n-- > 0;) {
143: sum += delta;
144: y += (z << 4) + a ^ z + sum ^ (z >>> 5) + b;
145: z += (y << 4) + c ^ y + sum ^ (y >>> 5) + d;
146: }
147: v[0] = y;
148: v[1] = z;
149: }
150:
151: final void decrypt(long[] v) {
152: long y = v[0];
153: long z = v[1];
154: long sum = delta * rounds;
155: for (int n = rounds; n-- > 0;) {
156: z -= (y << 4) + c ^ y + sum ^ (y >>> 5) + d;
157: y -= (z << 4) + a ^ z + sum ^ (z >>> 5) + b;
158: sum -= delta;
159: }
160: v[0] = y;
161: v[1] = z;
162: }
163: }
|