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: import java.math.BigInteger;
043: import java.nio.ByteBuffer;
044:
045: import com.quadcap.util.Debug;
046: import com.quadcap.util.Util;
047:
048: /**
049: * Implementation detail; factoring operations common to public and
050: * private RSA keys.
051: *
052: * @author Stan Bailes
053: */
054: public abstract class RSAKey implements Key {
055: static final BigInteger e = new BigInteger("3");
056: protected BigInteger n;
057: String text;
058: Random random;
059: int size;
060: byte[] buf;
061:
062: protected void init(String text, int size, BigInteger n) {
063: this .text = text;
064: this .size = size;
065: this .n = n;
066: this .buf = new byte[blockSize()];
067: }
068:
069: /**
070: * Run the algorithm (encryption or decyption) on the specified
071: * buffers
072: */
073: public void engine(ByteBuffer in, ByteBuffer out) {
074: int byteCnt = blockSize();
075: Debug.println("BLOCK SIZE: " + byteCnt + " bytes");
076: Debug.println("in: [" + in.position() + "/" + in.limit() + "-"
077: + in.capacity() + "]");
078: Debug.println("out: [" + out.position() + "/" + out.limit()
079: + "-" + out.capacity() + "]");
080: while (in.remaining() > 0) {
081: int len = Math.min(byteCnt, in.remaining());
082: in.get(buf, 0, len);
083: BigInteger pi = new BigInteger(1, buf);
084: BigInteger xi = engine(pi);
085: byte[] t = xi.toByteArray();
086: if (t.length > byteCnt) {
087: if (t.length > byteCnt + 1) {
088: throw new RuntimeException("---- t.length = "
089: + t.length);
090: } else {
091: out.put(t, 0, byteCnt);
092: }
093: } else {
094: if (t.length < byteCnt) {
095: int lim = byteCnt - t.length;
096: while (lim-- > 0)
097: out.put((byte) 0);
098: }
099: out.put(t);
100: }
101: for (int i = 0; i < buf.length; i++)
102: buf[i] = 0;
103: }
104: }
105:
106: /**
107: * The derived class actually implements this
108: */
109: public abstract void f(ByteBuffer src, ByteBuffer dst);
110:
111: /**
112: * The derived class (public/private) implements this as appropriate
113: */
114: protected abstract BigInteger engine(BigInteger x);
115:
116: final Random getRandom() {
117: if (random == null) {
118: random = new Random();
119: }
120: return random;
121: }
122:
123: final void randomBytes(byte[] buf, int pos, int cnt) {
124: byte[] tbuf = new byte[cnt];
125: getRandom().nextBytes(tbuf);
126: System.arraycopy(tbuf, 0, buf, pos, cnt);
127: }
128:
129: public int blockSize() {
130: return size >> 3;
131: }
132:
133: }
|