diff options
author | Tom Tromey <tromey@gcc.gnu.org> | 2007-01-09 19:58:05 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2007-01-09 19:58:05 +0000 |
commit | 97b8365cafc3a344a22d3980b8ed885f5c6d8357 (patch) | |
tree | 996a5f57d4a68c53473382e45cb22f574cb3e4db /libjava/classpath/gnu/javax/crypto | |
parent | c648dedbde727ca3f883bb5fd773aa4af70d3369 (diff) | |
download | gcc-97b8365cafc3a344a22d3980b8ed885f5c6d8357.zip gcc-97b8365cafc3a344a22d3980b8ed885f5c6d8357.tar.gz gcc-97b8365cafc3a344a22d3980b8ed885f5c6d8357.tar.bz2 |
Merged gcj-eclipse branch to trunk.
From-SVN: r120621
Diffstat (limited to 'libjava/classpath/gnu/javax/crypto')
4 files changed, 159 insertions, 46 deletions
diff --git a/libjava/classpath/gnu/javax/crypto/RSACipherImpl.java b/libjava/classpath/gnu/javax/crypto/RSACipherImpl.java index 60504ec..a4adff0 100644 --- a/libjava/classpath/gnu/javax/crypto/RSACipherImpl.java +++ b/libjava/classpath/gnu/javax/crypto/RSACipherImpl.java @@ -40,6 +40,7 @@ package gnu.javax.crypto; import gnu.classpath.debug.Component; import gnu.classpath.debug.SystemLogger; +import gnu.java.security.sig.rsa.EME_PKCS1_V1_5; import gnu.java.security.util.ByteArray; import java.math.BigInteger; @@ -53,7 +54,6 @@ import java.security.interfaces.RSAPrivateCrtKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.AlgorithmParameterSpec; -import java.util.logging.Logger; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; @@ -65,7 +65,8 @@ import javax.crypto.ShortBufferException; public class RSACipherImpl extends CipherSpi { - private static final Logger logger = SystemLogger.SYSTEM; + private static final SystemLogger logger = SystemLogger.SYSTEM; + private static final byte[] EMPTY = new byte[0]; private int opmode = -1; private RSAPrivateKey decipherKey = null; @@ -199,22 +200,12 @@ public class RSACipherImpl engineUpdate(in, offset, length); if (opmode == Cipher.DECRYPT_MODE) { - if (pos < dataBuffer.length) - throw new IllegalBlockSizeException("expecting exactly " - + dataBuffer.length + " bytes"); - BigInteger enc = new BigInteger(1, dataBuffer); - byte[] dec = rsaDecrypt(enc); - logger.log(Component.CRYPTO, "RSA: decryption produced\n{0}", - new ByteArray(dec)); - if (dec[0] != 0x02) - throw new BadPaddingException("expected padding type 2"); - int i; - for (i = 1; i < dec.length && dec[i] != 0x00; i++) - ; // keep incrementing i - int len = dec.length - i - 1; // skip the 0x00 byte - byte[] result = new byte[len]; - System.arraycopy(dec, i + 1, result, 0, len); - pos = 0; + BigInteger enc = new BigInteger (1, dataBuffer); + byte[] dec = rsaDecrypt (enc); + logger.log (Component.CRYPTO, "RSA: decryption produced\n{0}", + new ByteArray (dec)); + EME_PKCS1_V1_5 pkcs = EME_PKCS1_V1_5.getInstance(decipherKey); + byte[] result = pkcs.decode(dec); return result; } else @@ -222,24 +213,18 @@ public class RSACipherImpl offset = dataBuffer.length - pos; if (offset < 3) throw new IllegalBlockSizeException("input is too large to encrypt"); - byte[] dec = new byte[dataBuffer.length]; - dec[0] = 0x02; + EME_PKCS1_V1_5 pkcs = EME_PKCS1_V1_5.getInstance(encipherKey); if (random == null) random = new SecureRandom(); - byte[] pad = new byte[offset - 2]; - random.nextBytes(pad); - for (int i = 0; i < pad.length; i++) - if (pad[i] == 0) - pad[i] = 1; - System.arraycopy(pad, 0, dec, 1, pad.length); - dec[dec.length - pos] = 0x00; - System.arraycopy(dataBuffer, 0, dec, offset, pos); - logger.log(Component.CRYPTO, "RSA: produced padded plaintext\n{0}", - new ByteArray(dec)); - BigInteger x = new BigInteger(1, dec); - BigInteger y = x.modPow(encipherKey.getPublicExponent(), - encipherKey.getModulus()); - byte[] enc = y.toByteArray(); + byte[] em = new byte[pos]; + System.arraycopy(dataBuffer, 0, em, 0, pos); + byte[] dec = pkcs.encode(em, random); + logger.log (Component.CRYPTO, "RSA: produced padded plaintext\n{0}", + new ByteArray (dec)); + BigInteger x = new BigInteger (1, dec); + BigInteger y = x.modPow (encipherKey.getPublicExponent (), + encipherKey.getModulus ()); + byte[] enc = y.toByteArray (); if (enc[0] == 0x00) { byte[] tmp = new byte[enc.length - 1]; @@ -298,7 +283,17 @@ public class RSACipherImpl } BigInteger dec = enc.modPow(decipherKey.getPrivateExponent(), n); if (pubExp != null) - dec = dec.multiply(r.modInverse(n)).mod(n); - return dec.toByteArray(); + { + dec = dec.multiply (r.modInverse (n)).mod (n); + } + + byte[] decb = dec.toByteArray(); + if (decb[0] != 0x00) + { + byte[] b = new byte[decb.length + 1]; + System.arraycopy(decb, 0, b, 1, decb.length); + decb = b; + } + return decb; } } diff --git a/libjava/classpath/gnu/javax/crypto/jce/cipher/CipherAdapter.java b/libjava/classpath/gnu/javax/crypto/jce/cipher/CipherAdapter.java index b7cd755..0863b1f 100644 --- a/libjava/classpath/gnu/javax/crypto/jce/cipher/CipherAdapter.java +++ b/libjava/classpath/gnu/javax/crypto/jce/cipher/CipherAdapter.java @@ -373,14 +373,24 @@ class CipherAdapter engineInit(opmode, key, spec, random); } - protected byte[] engineUpdate(byte[] input, int off, int len) + protected byte[] engineUpdate(byte[] input, int inOff, int inLen) { + if (inLen == 0) // nothing to process + return new byte[0]; final int blockSize = mode.currentBlockSize(); - final int count = (partLen + len) / blockSize; - final byte[] out = new byte[count * blockSize]; + int blockCount = (partLen + inLen) / blockSize; + + // always keep data for unpadding in padded decryption mode; + // might even be a complete block + if (pad != null + && ((Integer) attributes.get(IMode.STATE)).intValue() == IMode.DECRYPTION + && (partLen + inLen) % blockSize == 0) + blockCount--; + + final byte[] out = new byte[blockCount * blockSize]; try { - engineUpdate(input, off, len, out, 0); + engineUpdate(input, inOff, inLen, out, 0); } catch (ShortBufferException x) // should not happen { @@ -395,7 +405,15 @@ class CipherAdapter if (inLen == 0) // nothing to process return 0; final int blockSize = mode.currentBlockSize(); - final int blockCount = (partLen + inLen) / blockSize; + int blockCount = (partLen + inLen) / blockSize; + + // always keep data for unpadding in padded decryption mode; + // might even be a complete block + if (pad != null + && ((Integer) attributes.get(IMode.STATE)).intValue() == IMode.DECRYPTION + && (partLen + inLen) % blockSize == 0) + blockCount--; + final int result = blockCount * blockSize; if (result > out.length - outOff) throw new ShortBufferException(); @@ -447,16 +465,21 @@ class CipherAdapter break; case IMode.DECRYPTION: int padLen; + byte[] buf3 = new byte[buf.length + partLen]; try { - padLen = pad.unpad(buf, 0, buf.length); + if (partLen != mode.currentBlockSize()) + throw new WrongPaddingException(); + System.arraycopy(buf, 0, buf3, 0, buf.length); + mode.update(partBlock, 0, buf3, buf.length); + padLen = pad.unpad(buf3, 0, buf3.length); } catch (WrongPaddingException wpe) { throw new BadPaddingException(wpe.getMessage()); } - result = new byte[buf.length - padLen]; - System.arraycopy(buf, 0, result, 0, result.length); + result = new byte[buf3.length - padLen]; + System.arraycopy(buf3, 0, result, 0, result.length); break; default: throw new IllegalStateException(); diff --git a/libjava/classpath/gnu/javax/crypto/key/GnuPBEKey.java b/libjava/classpath/gnu/javax/crypto/key/GnuPBEKey.java new file mode 100644 index 0000000..6f4bcb1 --- /dev/null +++ b/libjava/classpath/gnu/javax/crypto/key/GnuPBEKey.java @@ -0,0 +1,95 @@ +/* GnuPBEKey.java -- A password-based encryption key. + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is a part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or (at +your option) any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 +USA + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.javax.crypto.key; + +import javax.crypto.interfaces.PBEKey; +import javax.crypto.spec.PBEKeySpec; + +/** + * An implementation of a password-based encryption key. + * + * @author Casey Marshall (csm@gnu.org) + */ +public class GnuPBEKey + implements PBEKey +{ + private final PBEKeySpec spec; + + public GnuPBEKey (final PBEKeySpec spec) + { + if (spec == null) + throw new NullPointerException (); + this.spec = spec; + } + + public GnuPBEKey (char[] password, byte[] salt, int iterationCount) + { + this (new PBEKeySpec (password, salt, iterationCount)); + } + + public int getIterationCount () + { + return spec.getIterationCount (); + } + + public char[] getPassword () + { + return spec.getPassword (); + } + + public byte[] getSalt () + { + return spec.getSalt (); + } + + public String getAlgorithm () + { + return "PBE"; + } + + public String getFormat () + { + return "NONE"; // FIXME? + } + + public byte[] getEncoded () + { + return null; // FIXME? + } +} diff --git a/libjava/classpath/gnu/javax/crypto/pad/PKCS7.java b/libjava/classpath/gnu/javax/crypto/pad/PKCS7.java index bef1784..8fce5b8 100644 --- a/libjava/classpath/gnu/javax/crypto/pad/PKCS7.java +++ b/libjava/classpath/gnu/javax/crypto/pad/PKCS7.java @@ -100,8 +100,8 @@ public final class PKCS7 throws WrongPaddingException { int limit = offset + length; - int result = in[limit - 1] & 0xFF; - for (int i = 0; i < result; i++) + int result = in[--limit] & 0xFF; + for (int i = 0; i < result - 1; i++) if (result != (in[--limit] & 0xFF)) throw new WrongPaddingException(); if (Configuration.DEBUG) |