📄 speed.java
字号:
*/ protected final void finalize() { if (native_lock != null) { synchronized(native_lock) { String error = native_finalize(); // may be called more than once if (error != null) debug(error + " in native_finalize"); } } } /** * Always throws a CloneNotSupportedException (cloning of ciphers is not * supported for security reasons). */ public final Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); }// Implementation of JCE methods//........................................................................... /** * <b>SPI</b>: Return the data block length of this cipher. * Default (8 bytes) is returned before instantiation, * actual length used by object returned after instantiation. * * @return the block length in bytes. */ protected int engineBlockSize() { return block_size; } /** * <b>SPI</b>: Initializes this cipher for encryption, using the * specified key. * * @param key the key to use for encryption. * @exception InvalidKeyException if one of the following occurs: <ul> * <li> key.getEncoded() == null; * <li> The length of the user key array is not ... * </ul> */ public void engineInitEncrypt (Key key) throws InvalidKeyException { makeKey(key); } /** * <b>SPI</b>: Initializes this cipher for decryption, using the * specified key. * * @param key the key to use for decryption. * @exception InvalidKeyException if one of the following occurs: <ul> * <li> key.getEncoded() == null; * <li> The length of the user key array is not ... * </ul> */ public void engineInitDecrypt (Key key) throws InvalidKeyException, CryptixException { makeKey(key); } /** * <b>SPI</b>: This is the main engine method for updating data. * <p> * <i>in</i> and <i>out</i> may be the same array, and the input and output * regions may overlap. * * @param in the input data. * @param inOffset the offset into in specifying where the data starts. * @param inLen the length of the subarray. * @param out the output array. * @param outOffset the offset indicating where to start writing into * the out array. * @return the number of bytes written. * @exception CryptixException if the native library is being used, and it * reports an error. */ protected int engineUpdate(byte[] in, int inOffset, int inLen, byte[] out, int outOffset) { int BLOCK_SIZE = block_size; // use local variable to ensure that // BLOCK_SIZE does not change during this call. if (inLen < 0) throw new IllegalArgumentException("inLen < 0"); int blockCount = inLen / BLOCK_SIZE; inLen = blockCount * BLOCK_SIZE; boolean doEncrypt = (getState() == ENCRYPT); // Avoid overlapping input and output regions. if (in == out && (outOffset >= inOffset && outOffset < (long)inOffset+inLen || inOffset >= outOffset && inOffset < (long)outOffset+inLen)) { byte[] newin = new byte[inLen]; System.arraycopy(in, inOffset, newin, 0, inLen); in = newin; inOffset = 0; } if (native_lock != null) { synchronized(native_lock) { // If in == null || out == 0, evaluating their lengths will throw a // NullPointerException. if (inOffset < 0 || (long)inOffset + inLen > in.length || outOffset < 0 || (long)outOffset + inLen > out.length) throw new ArrayIndexOutOfBoundsException(getAlgorithm() + ": Arguments to native_crypt would cause a buffer overflow"); // In future, we may pass more than one block to native_crypt to reduce // native method overhead. for (int i = 0; i < blockCount; i++) { if (0 == native_crypt(native_cookie, in, inOffset, out, outOffset, doEncrypt, rounds, BLOCK_SIZE)) throw new CryptixException(getAlgorithm() + ": Error in native code"); inOffset += BLOCK_SIZE; outOffset += BLOCK_SIZE; } } } else if (doEncrypt) { // state == ENCRYPT for (int i = 0; i < blockCount; i++) { blockEncrypt(in, inOffset, out, outOffset); inOffset += BLOCK_SIZE; outOffset += BLOCK_SIZE; } } else { // state == DECRYPT for (int i = 0; i < blockCount; i++) { blockDecrypt(in, inOffset, out, outOffset); inOffset += BLOCK_SIZE; outOffset += BLOCK_SIZE; } } return inLen; } /** * <b>SPI</b>: Sets the specified algorithm parameter to the specified * value. * <p> * SPEED has two parameters: * <ul> * <li> "rounds", which specifies the number of rounds for this instance * as a decimal String. * <li> "blockSize", which specifies the block size for this instance, * also as a decimal String. * </ul> * * @param param the string name of the parameter. * @param value the parameter value. * @exception InvalidParameterException if param is an invalid * parameter for this cipher implementation, the * parameter is already set and cannot be set again, a * security exception occurs, and so on. * @exception InvalidParameterTypeException if value is of the wrong * type. */ protected void engineSetParameter(String param, Object value) throws NoSuchParameterException, InvalidParameterException, InvalidParameterTypeException { if (param.equalsIgnoreCase("rounds")) { if (value instanceof Integer) setRounds(((Integer) value).intValue()); else throw new InvalidParameterTypeException("rounds.SPEED"); } else if (param.equalsIgnoreCase("blockSize")) { if (value instanceof Integer) setBlockSize(((Integer) value).intValue()); else throw new InvalidParameterTypeException("blockSize.SPEED"); } else throw new NoSuchParameterException(param + ".SPEED"); } /** * <b>SPI</b>: Gets the value of the specified algorithm parameter. * <p> * SPEED has two parameters: * <ul> * <li> "rounds", which specifies the number of rounds for this instance * as a decimal String. * <li> "blockSize", which specifies the block size for this instance, * also as a decimal String. * </ul> * * @param param the string name of the parameter. * @return the object that represents the parameter value, or null if there * is none. */ protected Object engineGetParameter(String param) throws NoSuchParameterException, InvalidParameterException { if (param.equalsIgnoreCase("rounds")) return new Integer(rounds); else if (param.equalsIgnoreCase("blockSize")) return new Integer(block_size); else throw new NoSuchParameterException(param + ".SPEED"); }// Own methods//............................................................................ /** * Sets the number of rounds for this cipher. Allowed only when this * cipher is in the UNINITIALIZED state; otherwise an exception is * thrown. * <p> * If the specified number is invalid, an IllegalArgumentException is * thrown. * * @param rounds the desired number of rounds: >= 32, multiple of 4 * @exception IllegalStateException if this cipher is not uninitialised. * @exception InvalidParameterException if the given number of rounds is * not supported. */ public void setRounds(int rounds) { if (getState() != UNINITIALIZED) throw new IllegalStateException(getAlgorithm() + ": Cipher not in UNINITIALIZED state"); if (rounds < MIN_NOF_ROUNDS || rounds % 4 != 0) throw new IllegalArgumentException(getAlgorithm() + ": Invalid number of rounds"); this.rounds = rounds; } /** * Returns the currently set number of rounds for this instance. * * @return the number of rounds. */ public int getRounds() { return rounds; } /** * Sets the block size in bytes for this cipher. Allowed only when this * cipher is in the UNINITIALIZED state; otherwise an exception is * thrown. * <p> * If the specified number is invalid, an IllegalArgumentException is * thrown. * * @param blocksize the desired block size in bytes: 8, 16 or 32 * @exception IllegalStateException if this cipher is not uninitialised. * @exception IllegalArgumentException if the given number of rounds is * not supported. */ public void setBlockSize(int blocksize) { if (getState() != UNINITIALIZED) throw new IllegalStateException(getAlgorithm() + ": Cipher not in UNINITIALIZED state"); if (blocksize != 8 && blocksize != 16 && blocksize != 32) throw new IllegalArgumentException(getAlgorithm() + ": Invalid block size"); block_size = blocksize; } /** * Expands a user-key to a working key schedule. * * @param key the user-key object to use. * @exception InvalidKeyException if one of the following occurs: <ul> * <li> key.getEncoded() == null; * <li> The length of the user key array is not ... * </ul> */ private void makeKey (Key key) throws InvalidKeyException { byte[] userkey = key.getEncoded(); if (userkey == null) throw new InvalidKeyException(getAlgorithm() + ": Null user key"); int len = userkey.length; if (len < MIN_USER_KEY_LENGTH || len > MAX_USER_KEY_LENGTH) throw new InvalidKeyException(getAlgorithm() + ": Invalid user key length"); // If native library available then use it. If not or if // native method returned error then revert to 100% Java. if (native_lock != null) { synchronized(native_lock) { try { linkStatus.check(native_ks(native_cookie, userkey)); return; } catch (Error error) { native_finalize(); native_lock = null;if (DEBUG && debuglevel > 0) debug(error + ". Will use 100% Java."); } } } set_constants(userkey.length); kb = new int[kb_bits]; // scheduling temp buffer round_key = new int[rounds]; // real internal key // // Copy the key into the double-byte temporary buffer // for (int i = 0; i < key_len_dbyte; i++) kb[i] = userkey[2*i] | userkey[2*i + 1] << 8; // // Fill out the buffer from earlier parts of the key // for (int i = key_len_dbyte; i < kb_bits; i++) { int t = (s2 & s1) ^ (s1 & s0) ^ (s0 & s2); t = (t << 5) | (t >>> 11); t += s2 + kb[i % key_len_dbyte]; t &= 0xFFFF; s2 = s1; s1 = s0; s0 = kb[i] = t; }if (DEBUG && debuglevel >= 5) debug("kb_bits=" + kb_bits + ", kb.length=" + kb.length + ", round_key.length=" + round_key.length); // // Transfer the double-byte temporary key into the real key // switch (data_bits) { case 256: for (int i = 0; i < kb_bits / 2; i++) round_key[i] = kb[2*i] | (kb[2*i + 1] << 16); break; case 128: for (int i = 0; i < kb_bits; i++) round_key[i] = kb[i]; break; case 64: for (int i = 0; i < kb_bits; i++) { round_key[2*i] = kb[i] & 0xFF; round_key[2*i+1] = (kb[i] >>> 8) & 0xFF; } break; default: throw new CryptixException("SPEED: " + data_bits + " illegal in key_schedule?"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -