diff options
| author | mark <mark@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-08-14 23:12:35 +0000 |
|---|---|---|
| committer | mark <mark@138bc75d-0d04-0410-961f-82ee72b054a4> | 2006-08-14 23:12:35 +0000 |
| commit | ffde862e033a0825e1e9972a89c0f1f80b261a8e (patch) | |
| tree | 97037d2c09c8384d80531f67ec36a01205df6bdb /libjava/classpath/gnu/javax/crypto/prng/ICMGenerator.java | |
| parent | b415ff10527e977c3758234fd930e2c027bfa17d (diff) | |
| download | ppe42-gcc-ffde862e033a0825e1e9972a89c0f1f80b261a8e.tar.gz ppe42-gcc-ffde862e033a0825e1e9972a89c0f1f80b261a8e.zip | |
2006-08-14 Mark Wielaard <mark@klomp.org>
Imported GNU Classpath 0.92
* HACKING: Add more importing hints. Update automake version
requirement.
* configure.ac (gconf-peer): New enable AC argument.
Add --disable-gconf-peer and --enable-default-preferences-peer
to classpath configure when gconf is disabled.
* scripts/makemake.tcl: Set gnu/java/util/prefs/gconf and
gnu/java/awt/dnd/peer/gtk to bc. Classify
gnu/java/security/Configuration.java as generated source file.
* gnu/java/lang/management/VMGarbageCollectorMXBeanImpl.java,
gnu/java/lang/management/VMMemoryPoolMXBeanImpl.java,
gnu/java/lang/management/VMClassLoadingMXBeanImpl.java,
gnu/java/lang/management/VMRuntimeMXBeanImpl.java,
gnu/java/lang/management/VMMemoryManagerMXBeanImpl.java,
gnu/java/lang/management/VMThreadMXBeanImpl.java,
gnu/java/lang/management/VMMemoryMXBeanImpl.java,
gnu/java/lang/management/VMCompilationMXBeanImpl.java: New VM stub
classes.
* java/lang/management/VMManagementFactory.java: Likewise.
* java/net/VMURLConnection.java: Likewise.
* gnu/java/nio/VMChannel.java: Likewise.
* java/lang/Thread.java (getState): Add stub implementation.
* java/lang/Class.java (isEnum): Likewise.
* java/lang/Class.h (isEnum): Likewise.
* gnu/awt/xlib/XToolkit.java (getClasspathTextLayoutPeer): Removed.
* javax/naming/spi/NamingManager.java: New override for StackWalker
functionality.
* configure, sources.am, Makefile.in, gcj/Makefile.in,
include/Makefile.in, testsuite/Makefile.in: Regenerated.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@116139 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/classpath/gnu/javax/crypto/prng/ICMGenerator.java')
| -rw-r--r-- | libjava/classpath/gnu/javax/crypto/prng/ICMGenerator.java | 252 |
1 files changed, 86 insertions, 166 deletions
diff --git a/libjava/classpath/gnu/javax/crypto/prng/ICMGenerator.java b/libjava/classpath/gnu/javax/crypto/prng/ICMGenerator.java index 7d4f4c9a32c..5b0bd4f8b1e 100644 --- a/libjava/classpath/gnu/javax/crypto/prng/ICMGenerator.java +++ b/libjava/classpath/gnu/javax/crypto/prng/ICMGenerator.java @@ -41,9 +41,8 @@ package gnu.javax.crypto.prng; import gnu.java.security.Registry; import gnu.java.security.prng.BasePRNG; import gnu.java.security.prng.LimitReachedException; - -import gnu.javax.crypto.cipher.IBlockCipher; import gnu.javax.crypto.cipher.CipherFactory; +import gnu.javax.crypto.cipher.IBlockCipher; import java.math.BigInteger; import java.security.InvalidKeyException; @@ -51,171 +50,132 @@ import java.util.HashMap; import java.util.Map; /** - * <p>Counter Mode is a way to define a pseudorandom keystream generator using - * a block cipher. The keystream can be used for additive encryption, key - * derivation, or any other application requiring pseudorandom data.</p> - * - * <p>In ICM, the keystream is logically broken into segments. Each segment is + * Counter Mode is a way to define a pseudorandom keystream generator using a + * block cipher. The keystream can be used for additive encryption, key + * derivation, or any other application requiring pseudorandom data. + * <p> + * In ICM, the keystream is logically broken into segments. Each segment is * identified with a segment index, and the segments have equal lengths. This * segmentation makes ICM especially appropriate for securing packet-based - * protocols.</p> - * - * <p>This implementation adheres to the definition of the ICM keystream - * generation function that allows for any symetric key block cipher algorithm - * (initialisation parameter <code>gnu.crypto.prng.icm.cipher.name</code> taken - * to be an instance of {@link java.lang.String}) to be used. If such a + * protocols. + * <p> + * This implementation adheres to the definition of the ICM keystream generation + * function that allows for any symetric key block cipher algorithm + * (initialisation parameter <code>gnu.crypto.prng.icm.cipher.name</code> + * taken to be an instance of {@link java.lang.String}) to be used. If such a * parameter is not defined/included in the initialisation <code>Map</code>, * then the "Rijndael" algorithm is used. Furthermore, if the initialisation * parameter <code>gnu.crypto.cipher.block.size</code> (taken to be a instance * of {@link java.lang.Integer}) is missing or undefined in the initialisation - * <code>Map</code>, then the cipher's <em>default</em> block size is used.</p> - * - * <p>The practical limits and constraints of such generator are:</p> + * <code>Map</code>, then the cipher's <em>default</em> block size is used. + * <p> + * The practical limits and constraints of such generator are: * <ul> - * <li>The number of blocks in any segment <b>MUST NOT</b> exceed <code> - * 256 ** BLOCK_INDEX_LENGTH</code>. The number of segments <b>MUST NOT</b> - * exceed <code>256 ** SEGMENT_INDEX_LENGTH</code>. These restrictions ensure - * the uniqueness of each block cipher input.</li> - * - * <li>Each segment contains <code>SEGMENT_LENGTH</code> octets; this value - * <b>MUST NOT</b> exceed the value <code>(256 ** BLOCK_INDEX_LENGTH) * + * <li>The number of blocks in any segment <b>MUST NOT</b> exceed <code> + * 256 ** BLOCK_INDEX_LENGTH</code>. + * The number of segments <b>MUST NOT</b> exceed + * <code>256 ** SEGMENT_INDEX_LENGTH</code>. These restrictions ensure the + * uniqueness of each block cipher input.</li> + * <li>Each segment contains <code>SEGMENT_LENGTH</code> octets; this value + * <b>MUST NOT</b> exceed the value <code>(256 ** BLOCK_INDEX_LENGTH) * * BLOCK_LENGTH</code>.</li> - * - * <li>The sum of <code>SEGMENT_INDEX_LENGTH</code> and - * <code>BLOCK_INDEX_LENGTH</code> <b>MUST NOT</b> exceed <code>BLOCK_LENGTH - * / 2</code>. This requirement protects the ICM keystream generator from - * potentially failing to be pseudorandom.</li> + * <li>The sum of <code>SEGMENT_INDEX_LENGTH</code> and + * <code>BLOCK_INDEX_LENGTH</code> <b>MUST NOT</b> exceed <code>BLOCK_LENGTH + * / 2</code>. + * This requirement protects the ICM keystream generator from potentially + * failing to be pseudorandom.</li> * </ul> - * - * <p><b>NOTE</b>: Rijndael is used as the default symmetric key block cipher + * <p> + * <b>NOTE</b>: Rijndael is used as the default symmetric key block cipher * algorithm because, with its default block and key sizes, it is the AES. Yet * being Rijndael, the algorithm offers more versatile block and key sizes which - * may prove to be useful for generating <em>longer</em> key streams.</p> - * - * <p>References:</p> - * + * may prove to be useful for generating <em>longer</em> key streams. + * <p> + * References: * <ol> - * <li><a href="http://www.ietf.org/internet-drafts/draft-mcgrew-saag-icm-00.txt"> - * Integer Counter Mode</a>, David A. McGrew.</li> + * <li><a + * href="http://www.ietf.org/internet-drafts/draft-mcgrew-saag-icm-00.txt"> + * Integer Counter Mode</a>, David A. McGrew.</li> * </ol> */ -public class ICMGenerator extends BasePRNG implements Cloneable +public class ICMGenerator + extends BasePRNG + implements Cloneable { - - // Constants and variables - // ------------------------------------------------------------------------- - /** Property name of underlying block cipher for this ICM generator. */ public static final String CIPHER = "gnu.crypto.prng.icm.cipher.name"; - /** Property name of ICM's block index length. */ - public static final String BLOCK_INDEX_LENGTH = "gnu.crypto.prng.icm.block.index.length"; - + public static final String BLOCK_INDEX_LENGTH = + "gnu.crypto.prng.icm.block.index.length"; /** Property name of ICM's segment index length. */ - public static final String SEGMENT_INDEX_LENGTH = "gnu.crypto.prng.icm.segment.index.length"; - + public static final String SEGMENT_INDEX_LENGTH = + "gnu.crypto.prng.icm.segment.index.length"; /** Property name of ICM's offset. */ public static final String OFFSET = "gnu.crypto.prng.icm.offset"; - /** Property name of ICM's segment index. */ public static final String SEGMENT_INDEX = "gnu.crypto.prng.icm.segment.index"; - /** The integer value 256 as a BigInteger. */ private static final BigInteger TWO_FIFTY_SIX = new BigInteger("256"); - /** The underlying cipher implementation. */ private IBlockCipher cipher; - /** This keystream block index length in bytes. */ private int blockNdxLength = -1; - /** This keystream segment index length in bytes. */ private int segmentNdxLength = -1; - /** The index of the next block for a given keystream segment. */ private BigInteger blockNdx = BigInteger.ZERO; - /** The segment index for this keystream. */ private BigInteger segmentNdx; - /** The initial counter for a given keystream segment. */ private BigInteger C0; - // Constructor(s) - // ------------------------------------------------------------------------- - /** Trivial 0-arguments constructor. */ public ICMGenerator() { super(Registry.ICM_PRNG); } - // Class methods - // ------------------------------------------------------------------------- - - // Instance methods - // ------------------------------------------------------------------------- - - // Implementation of abstract methods in BasePRNG -------------------------- - - // Conceptually, ICM is a keystream generator that takes a secret key - // and a segment index as an input and then outputs a keystream - // segment. The segmentation lends itself to packet encryption, as - // each keystream segment can be used to encrypt a distinct packet. - // - // An ICM key consists of the block cipher key and an Offset. The - // Offset is an integer with BLOCK_LENGTH octets... + // Conceptually, ICM is a keystream generator that takes a secret key and a + // segment index as an input and then outputs a keystream segment. The + // segmentation lends itself to packet encryption, as each keystream segment + // can be used to encrypt a distinct packet. // + // An ICM key consists of the block cipher key and an Offset. The Offset is + // an integer with BLOCK_LENGTH octets... public void setup(Map attributes) { // find out which cipher algorithm to use boolean newCipher = true; String underlyingCipher = (String) attributes.get(CIPHER); if (underlyingCipher == null) - { - if (cipher == null) - { // happy birthday - // ensure we have a reliable implementation of this cipher - cipher = CipherFactory.getInstance(Registry.RIJNDAEL_CIPHER); - } - else - { // we already have one. use it as is - newCipher = false; - } - } - else - { // ensure we have a reliable implementation of this cipher - cipher = CipherFactory.getInstance(underlyingCipher); - } + if (cipher == null) // happy birthday + // ensure we have a reliable implementation of this cipher + cipher = CipherFactory.getInstance(Registry.RIJNDAEL_CIPHER); + else + // we already have one. use it as is + newCipher = false; + else // ensure we have a reliable implementation of this cipher + cipher = CipherFactory.getInstance(underlyingCipher); // find out what block size we should use it in int cipherBlockSize = 0; Integer bs = (Integer) attributes.get(IBlockCipher.CIPHER_BLOCK_SIZE); if (bs != null) - { - cipherBlockSize = bs.intValue(); - } + cipherBlockSize = bs.intValue(); else { - if (newCipher) - { // assume we'll use its default block size - cipherBlockSize = cipher.defaultBlockSize(); - } // else use as is + if (newCipher) // assume we'll use its default block size + cipherBlockSize = cipher.defaultBlockSize(); + // else use as is } - // get the key material byte[] key = (byte[]) attributes.get(IBlockCipher.KEY_MATERIAL); if (key == null) - { - throw new IllegalArgumentException(IBlockCipher.KEY_MATERIAL); - } - + throw new IllegalArgumentException(IBlockCipher.KEY_MATERIAL); // now initialise the cipher HashMap map = new HashMap(); - if (cipherBlockSize != 0) - { // only needed if new or changed - map.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(cipherBlockSize)); - } + if (cipherBlockSize != 0) // only needed if new or changed + map.put(IBlockCipher.CIPHER_BLOCK_SIZE, Integer.valueOf(cipherBlockSize)); map.put(IBlockCipher.KEY_MATERIAL, key); try { @@ -225,135 +185,96 @@ public class ICMGenerator extends BasePRNG implements Cloneable { throw new IllegalArgumentException(IBlockCipher.KEY_MATERIAL); } - // at this point we have an initialised (new or otherwise) cipher // ensure that remaining params make sense - cipherBlockSize = cipher.currentBlockSize(); BigInteger counterRange = TWO_FIFTY_SIX.pow(cipherBlockSize); - // offset, like the underlying cipher key is not cloneable // always look for it and throw an exception if it's not there Object obj = attributes.get(OFFSET); // allow either a byte[] or a BigInteger BigInteger r; if (obj instanceof BigInteger) + r = (BigInteger) obj; + else // assume byte[]. should be same length as cipher block size { - r = (BigInteger) obj; - } - else - { // assume byte[]. should be same length as cipher block size byte[] offset = (byte[]) obj; if (offset.length != cipherBlockSize) - { - throw new IllegalArgumentException(OFFSET); - } - + throw new IllegalArgumentException(OFFSET); r = new BigInteger(1, offset); } - int wantBlockNdxLength = -1; // number of octets in the block index Integer i = (Integer) attributes.get(BLOCK_INDEX_LENGTH); if (i != null) { wantBlockNdxLength = i.intValue(); if (wantBlockNdxLength < 1) - { - throw new IllegalArgumentException(BLOCK_INDEX_LENGTH); - } + throw new IllegalArgumentException(BLOCK_INDEX_LENGTH); } - int wantSegmentNdxLength = -1; // number of octets in the segment index i = (Integer) attributes.get(SEGMENT_INDEX_LENGTH); if (i != null) { wantSegmentNdxLength = i.intValue(); if (wantSegmentNdxLength < 1) - { - throw new IllegalArgumentException(SEGMENT_INDEX_LENGTH); - } + throw new IllegalArgumentException(SEGMENT_INDEX_LENGTH); } - // if both are undefined check if it's a reuse if ((wantBlockNdxLength == -1) && (wantSegmentNdxLength == -1)) { - if (blockNdxLength == -1) - { // new instance - throw new IllegalArgumentException(BLOCK_INDEX_LENGTH + ", " - + SEGMENT_INDEX_LENGTH); - } // else reuse old values + if (blockNdxLength == -1) // new instance + throw new IllegalArgumentException(BLOCK_INDEX_LENGTH + ", " + + SEGMENT_INDEX_LENGTH); + // else reuse old values } - else - { // only one is undefined, set it to BLOCK_LENGTH/2 minus the other + else // only one is undefined, set it to BLOCK_LENGTH/2 minus the other + { int limit = cipherBlockSize / 2; if (wantBlockNdxLength == -1) - { - wantBlockNdxLength = limit - wantSegmentNdxLength; - } + wantBlockNdxLength = limit - wantSegmentNdxLength; else if (wantSegmentNdxLength == -1) - { - wantSegmentNdxLength = limit - wantBlockNdxLength; - } + wantSegmentNdxLength = limit - wantBlockNdxLength; else if ((wantSegmentNdxLength + wantBlockNdxLength) > limit) - { - throw new IllegalArgumentException(BLOCK_INDEX_LENGTH + ", " - + SEGMENT_INDEX_LENGTH); - } + throw new IllegalArgumentException(BLOCK_INDEX_LENGTH + ", " + + SEGMENT_INDEX_LENGTH); // save new values blockNdxLength = wantBlockNdxLength; segmentNdxLength = wantSegmentNdxLength; } - // get the segment index as a BigInteger BigInteger s = (BigInteger) attributes.get(SEGMENT_INDEX); if (s == null) { - if (segmentNdx == null) - { // segment index was never set - throw new IllegalArgumentException(SEGMENT_INDEX); - } + if (segmentNdx == null) // segment index was never set + throw new IllegalArgumentException(SEGMENT_INDEX); // reuse; check if still valid if (segmentNdx.compareTo(TWO_FIFTY_SIX.pow(segmentNdxLength)) > 0) - { - throw new IllegalArgumentException(SEGMENT_INDEX); - } + throw new IllegalArgumentException(SEGMENT_INDEX); } else { if (s.compareTo(TWO_FIFTY_SIX.pow(segmentNdxLength)) > 0) - { - throw new IllegalArgumentException(SEGMENT_INDEX); - } + throw new IllegalArgumentException(SEGMENT_INDEX); segmentNdx = s; } - // The initial counter of the keystream segment with segment index s is // defined as follows, where r denotes the Offset: // // C[0] = (s * (256^BLOCK_INDEX_LENGTH) + r) modulo (256^BLOCK_LENGTH) - // - C0 = segmentNdx.multiply(TWO_FIFTY_SIX.pow(blockNdxLength)).add(r).modPow( - BigInteger.ONE, - counterRange); + C0 = segmentNdx.multiply(TWO_FIFTY_SIX.pow(blockNdxLength)) + .add(r).modPow(BigInteger.ONE, counterRange); } public void fillBlock() throws LimitReachedException { if (C0 == null) - { - throw new IllegalStateException(); - } + throw new IllegalStateException(); if (blockNdx.compareTo(TWO_FIFTY_SIX.pow(blockNdxLength)) >= 0) - { - throw new LimitReachedException(); - } - + throw new LimitReachedException(); int cipherBlockSize = cipher.currentBlockSize(); BigInteger counterRange = TWO_FIFTY_SIX.pow(cipherBlockSize); - // encrypt the counter for the current blockNdx // C[i] = (C[0] + i) modulo (256^BLOCK_LENGTH). - BigInteger Ci = C0.add(blockNdx).modPow(BigInteger.ONE, counterRange); buffer = Ci.toByteArray(); int limit = buffer.length; @@ -370,7 +291,6 @@ public class ICMGenerator extends BasePRNG implements Cloneable cipherBlockSize); buffer = data; } - cipher.encryptBlock(buffer, 0, buffer, 0); blockNdx = blockNdx.add(BigInteger.ONE); // increment blockNdx } |

