@@ -106,12 +106,14 @@ public enum PRF_ALGORITHMS {
106106 // Check if versions of KeyDerivationFunction, CipherText, and
107107 // CipherTextSerializer are all the same.
108108 {
109- // Ignore error about comparing identical versions and dead code.
110- // We expect them to be, but the point is to catch us if they aren't.
111- assert CipherTextSerializer .cipherTextSerializerVersion == CipherText .cipherTextVersion :
112- "Versions of CipherTextSerializer and CipherText are not compatible." ;
113- assert CipherTextSerializer .cipherTextSerializerVersion == KeyDerivationFunction .kdfVersion :
114- "Versions of CipherTextSerializer and KeyDerivationFunction are not compatible." ;
109+ // Ignore error about comparing identical versions and dead code.
110+ // We expect them to be, but the point is to catch us if they aren't.
111+ if ( CipherTextSerializer .cipherTextSerializerVersion != CipherText .cipherTextVersion ) {
112+ throw new ExceptionInInitializerError ("Versions of CipherTextSerializer and CipherText are not compatible." );
113+ }
114+ if ( CipherTextSerializer .cipherTextSerializerVersion != KeyDerivationFunction .kdfVersion ) {
115+ throw new ExceptionInInitializerError ("Versions of CipherTextSerializer and KeyDerivationFunction are not compatible." );
116+ }
115117 }
116118
117119 /**
@@ -295,14 +297,30 @@ public SecretKey computeDerivedKey(SecretKey keyDerivationKey, int keySize, Stri
295297 // to section 5.1 of NIST SP 800-108 based on feedback from
296298 // Jeffrey Walton.
297299 //
298- // These probably should be turned into actual runtime checks and an
299- // IllegalArgumentException should be thrown if they are violated.
300- assert keyDerivationKey != null : "Key derivation key cannot be null." ;
301- // We would choose a larger minimum key size, but we want to be
302- // able to accept DES for legacy encryption needs.
303- assert keySize >= 56 : "Key has size of " + keySize + ", which is less than minimum of 56-bits." ;
304- assert (keySize % 8 ) == 0 : "Key size (" + keySize + ") must be a even multiple of 8-bits." ;
305- assert purpose != null && !purpose .equals ("" ) : "Purpose may not be null or empty." ;
300+
301+ // These checks used to be assertions prior to ESAPI 2.1.0.1
302+ if ( keyDerivationKey == null ) {
303+ throw new IllegalArgumentException ("Key derivation key cannot be null." );
304+ }
305+ // We would choose a larger minimum key size, but we want to allow
306+ // this KDF to be able to accept DES for legacy encryption needs. (Note that
307+ // elsewhere there are checks that disallow *encryption* for key size
308+ // less than Encryptor.EncryptionKeyLength bits, so if they want
309+ // ESAPI to encrypt stuff for DES, they would have to set that up to
310+ // be 56 bits. But I can't think of any valid symmetric encryption
311+ // algorithm whose key size is less than 56 bits that we would ever
312+ // want to allow.
313+ if ( keySize < 56 ) {
314+ throw new IllegalArgumentException ("Key has size of " + keySize +
315+ ", which is less than minimum of 56-bits." );
316+ }
317+ if ( (keySize % 8 ) != 0 ) {
318+ throw new IllegalArgumentException ("Key size (" + keySize +
319+ ") must be a even multiple of 8-bits." );
320+ }
321+ if ( purpose == null || "" .equals (purpose ) ) {
322+ throw new IllegalArgumentException ("Purpose may not be null or empty." );
323+ }
306324
307325 keySize = calcKeySize ( keySize ); // Safely convert to whole # of bytes.
308326 byte [] derivedKey = new byte [ keySize ];
@@ -451,7 +469,9 @@ public static PRF_ALGORITHMS convertIntToPRF(int selection) {
451469 * {@code ks} bits.
452470 */
453471 private static int calcKeySize (int ks ) {
454- assert ks > 0 : "Key size must be > 0 bits." ;
472+ if ( ks <= 0 ) {
473+ throw new IllegalArgumentException ("Key size must be > 0 bits." );
474+ }
455475 int numBytes = 0 ;
456476 int n = ks /8 ;
457477 int rem = ks % 8 ;
0 commit comments