Quadcap Embeddable Database

com/quadcap/crypto/Tea256.java

Go to the documentation of this file.
00001 package com.quadcap.crypto; 00002 00003 /* Copyright 2002 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.Random; 00042 00043 import java.nio.ByteBuffer; 00044 import java.nio.LongBuffer; 00045 00046 import com.quadcap.util.text.Text; 00047 import com.quadcap.util.Util; 00048 00049 /** 00050 * Implementation of Tiny Encryption Algorithm, modified to use longs 00051 * instead of ints. SO the key is now four longs, or 256 bits, and the 00052 * encryption/decryption proceeds 16 bytes at a time instead of 8. 00053 * 00054 * The number of rounds has been retained at 32. 00055 * 00056 * @author Stan Bailes 00057 */ 00058 public class Tea256 extends AbstractSymmetricKey implements SymmetricKey { 00059 /** Computed as (sqrt(5)-1) * 2**63, following original Tea choice of 00060 delta. */ 00061 static final long delta = 0x9E3779B97F4A7C15L; 00062 static final int rounds = 32; 00063 00064 long a, b, c, d; 00065 long[] v = new long[2]; 00066 00067 /** 00068 * Initialize key from serialized representation 00069 */ 00070 public void init(String s) { 00071 String[] vx = Text.extractN(s, "*:*:*:*:*"); 00072 a = Long.parseLong(vx[1]); 00073 b = Long.parseLong(vx[2]); 00074 c = Long.parseLong(vx[3]); 00075 d = Long.parseLong(vx[4]); 00076 } 00077 00078 public void init(byte[] k) { 00079 a = Util.bytesToLong(k, 0); 00080 b = Util.bytesToLong(k, 8); 00081 c = Util.bytesToLong(k, 16); 00082 d = Util.bytesToLong(k, 24); 00083 } 00084 00085 /** 00086 * Initialize: Create a random key 00087 */ 00088 public void init(Random r) { 00089 a = r.nextLong(); 00090 b = r.nextLong(); 00091 c = r.nextLong(); 00092 d = r.nextLong(); 00093 } 00094 00095 /** 00096 * Return the serialized form of the key 00097 */ 00098 public String toString() { 00099 return "TEA256:" + a + ":" + b + ":" + c + ":" + d; 00100 } 00101 00102 /** 00103 * Encrypt a buffer (must be multiple of 16 bytes) 00104 */ 00105 public void encrypt(ByteBuffer plain, ByteBuffer code) { 00106 LongBuffer p = plain.asLongBuffer(); 00107 LongBuffer cb = code.asLongBuffer(); 00108 for (int i = 0; i < p.limit(); i += 2) { 00109 v[0] = p.get(i); 00110 v[1] = p.get(i+1); 00111 encrypt(v); 00112 cb.put(i, v[0]); 00113 cb.put(i+1, v[1]); 00114 } 00115 } 00116 00117 /** 00118 * Decrypt a buffer (must be a multiple of 16 bytes) 00119 */ 00120 public void decrypt(ByteBuffer code, ByteBuffer plain) { 00121 LongBuffer p = plain.asLongBuffer(); 00122 LongBuffer cb = code.asLongBuffer(); 00123 for (int i = 0; i < cb.limit(); i += 2) { 00124 v[0] = cb.get(i); 00125 v[1] = cb.get(i+1); 00126 decrypt(v); 00127 p.put(i, v[0]); 00128 p.put(i+1, v[1]); 00129 } 00130 } 00131 00132 public int getBlockSize() { return 16; } 00133 00134 final void encrypt(long[] v) { 00135 long y = v[0]; 00136 long z = v[1]; 00137 long sum = 0; 00138 00139 for (int n = rounds; n-- > 0; ) { 00140 sum += delta; 00141 y += (z << 4) + a ^ z + sum ^ (z >>> 5) + b; 00142 z += (y << 4) + c ^ y + sum ^ (y >>> 5) + d; 00143 } 00144 v[0] = y; 00145 v[1] = z; 00146 } 00147 00148 final void decrypt(long[] v) { 00149 long y = v[0]; 00150 long z = v[1]; 00151 long sum = delta * rounds; 00152 for (int n = rounds; n-- > 0; ) { 00153 z -= (y << 4) + c ^ y + sum ^ (y >>> 5) + d; 00154 y -= (z << 4) + a ^ z + sum ^ (z >>> 5) + b; 00155 sum -= delta; 00156 } 00157 v[0] = y; 00158 v[1] = z; 00159 } 00160 }