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.IntBuffer;
045:
046: import com.quadcap.util.text.Text;
047: import com.quadcap.util.Util;
048:
049: /**
050: * Implementation of Tiny Encryption Algorithm
051: *
052: * @author Stan Bailes
053: */
054: public class Tea extends AbstractSymmetricKey implements SymmetricKey {
055: static final int delta = 0x9E3779B9;
056: int a, b, c, d;
057: int[] v = new int[2];
058:
059: /**
060: * Initialize key from serialized representation
061: */
062: public void init(String s) {
063: String[] vx = Text.extractN(s, "*:*:*:*:*");
064: a = Integer.parseInt(vx[1]);
065: b = Integer.parseInt(vx[2]);
066: c = Integer.parseInt(vx[3]);
067: d = Integer.parseInt(vx[4]);
068: }
069:
070: public void init(byte[] k) {
071: a = Util.integer(k, 0);
072: b = Util.integer(k, 4);
073: c = Util.integer(k, 8);
074: d = Util.integer(k, 12);
075: }
076:
077: /**
078: * Initialize: Create a random key
079: */
080: public void init(Random r) {
081: a = r.nextInt();
082: b = r.nextInt();
083: c = r.nextInt();
084: d = r.nextInt();
085: }
086:
087: /**
088: * Return the serialized form of the key
089: */
090: public String toString() {
091: return "TEA:" + a + ":" + b + ":" + c + ":" + d;
092: }
093:
094: /**
095: * Encrypt a buffer (must be multiple of 8 bytes)
096: */
097: public void encrypt(ByteBuffer m, ByteBuffer c) {
098: while (m.position() < m.limit()) {
099: v[0] = m.getInt();
100: v[1] = m.getInt();
101: encrypt(v);
102: c.putInt(v[0]);
103: c.putInt(v[1]);
104: }
105: }
106:
107: /**
108: * Decrypt a buffer (must be a multiple of 8 bytes)
109: */
110: public void decrypt(ByteBuffer c, ByteBuffer m) {
111: while (c.position() < c.limit()) {
112: v[0] = c.getInt();
113: v[1] = c.getInt();
114: decrypt(v);
115: m.putInt(v[0]);
116: m.putInt(v[1]);
117: }
118: }
119:
120: public int getBlockSize() {
121: return 8;
122: }
123:
124: final void encrypt(int[] v) {
125: int y = v[0];
126: int z = v[1];
127: int sum = 0;
128:
129: for (int n = 32; n-- > 0;) {
130: sum += delta;
131: y += (z << 4) + a ^ z + sum ^ (z >>> 5) + b;
132: z += (y << 4) + c ^ y + sum ^ (y >>> 5) + d;
133: }
134: v[0] = y;
135: v[1] = z;
136: }
137:
138: final void decrypt(int[] v) {
139: int y = v[0];
140: int z = v[1];
141: int sum = 0xC6EF3720;
142: for (int n = 32; n-- > 0;) {
143: z -= (y << 4) + c ^ y + sum ^ (y >>> 5) + d;
144: y -= (z << 4) + a ^ z + sum ^ (z >>> 5) + b;
145: sum -= delta;
146: }
147: v[0] = y;
148: v[1] = z;
149: }
150: }
|