1 package iaik.pkcs.pkcs11.parameters; 2 3 import iaik.pkcs.pkcs11.TokenRuntimeException; 4 import iaik.pkcs.pkcs11.wrapper.Constants; 5 import iaik.pkcs.pkcs11.wrapper.Functions; 6 7 /** 8 * This class encapsulates parameters for general block ciphers in CBC mode. 9 * Those are all Mechanism.*_CBC and Mechanism.*_CBC_PAD mechanisms. This class 10 * also applies to other mechanisms which require just an initialization vector 11 * as parameter. 12 * 13 * @author Karl Scheibelhofer 14 * @version 1.0 15 * @invariants (initializationVector_ <> null) 16 */ 17 public class InitializationVectorParameters implements Parameters { 18 19 /** 20 * The initialization vector. 21 */ 22 protected byte[] initializationVector_; 23 24 /** 25 * Create a new InitializationVectorParameters object with the given 26 * initialization vector. 27 * 28 * @param initializationVector The initialization vector. 29 * @preconditions (initializationVector <> null) 30 * @postconditions 31 */ InitializationVectorParameters(byte[] initializationVector)32 public InitializationVectorParameters(byte[] initializationVector) { 33 if (initializationVector == null) { 34 throw new NullPointerException( 35 "Argument \"initializationVector\" must not be null."); 36 } 37 initializationVector_ = initializationVector; 38 } 39 40 /** 41 * Create a (deep) clone of this object. 42 * 43 * @return A clone of this object. 44 * @preconditions 45 * @postconditions (result <> null) 46 * and (result instanceof InitializationVectorParameters) 47 * and (result.equals(this)) 48 */ clone()49 public java.lang.Object clone() { 50 InitializationVectorParameters clone; 51 52 try { 53 clone = (InitializationVectorParameters) super.clone(); 54 55 clone.initializationVector_ = (byte[]) this.initializationVector_.clone(); 56 } catch (CloneNotSupportedException ex) { 57 // this must not happen, because this class is cloneable 58 throw new TokenRuntimeException("An unexpected clone exception occurred.", ex); 59 } 60 61 return clone; 62 } 63 64 /** 65 * Get this parameters object as a byte array. 66 * 67 * @return This object as a byte array. 68 * @preconditions 69 * @postconditions (result <> null) 70 */ getPKCS11ParamsObject()71 public Object getPKCS11ParamsObject() { 72 return initializationVector_; 73 } 74 75 /** 76 * Get the initialization vector. 77 * 78 * @return The initialization vector. 79 * @preconditions 80 * @postconditions (result <> null) 81 */ getInitializationVector()82 public byte[] getInitializationVector() { 83 return initializationVector_; 84 } 85 86 /** 87 * Set the initialization vector. 88 * 89 * @param initializationVector The initialization vector. 90 * @preconditions (initializationVector <> null) 91 * @postconditions 92 */ setInitializationVector(byte[] initializationVector)93 public void setInitializationVector(byte[] initializationVector) { 94 if (initializationVector == null) { 95 throw new NullPointerException( 96 "Argument \"initializationVector\" must not be null."); 97 } 98 initializationVector_ = initializationVector; 99 } 100 101 /** 102 * Returns the string representation of this object. Do not parse data from 103 * this string, it is for debugging only. 104 * 105 * @return A string representation of this object. 106 */ toString()107 public String toString() { 108 StringBuffer buffer = new StringBuffer(); 109 110 buffer.append(Constants.INDENT); 111 buffer.append("Initialization Vector (hex): "); 112 buffer.append(Functions.toHexString(initializationVector_)); 113 // buffer.append(Constants.NEWLINE); 114 115 return buffer.toString(); 116 } 117 118 /** 119 * Compares all member variables of this object with the other object. 120 * Returns only true, if all are equal in both objects. 121 * 122 * @param otherObject The other object to compare to. 123 * @return True, if other is an instance of this class and all member 124 * variables of both objects are equal. False, otherwise. 125 * @preconditions 126 * @postconditions 127 */ equals(java.lang.Object otherObject)128 public boolean equals(java.lang.Object otherObject) { 129 boolean equal = false; 130 131 if (otherObject instanceof InitializationVectorParameters) { 132 InitializationVectorParameters other = (InitializationVectorParameters) otherObject; 133 equal = (this == other) 134 || Functions.equals(this.initializationVector_, other.initializationVector_); 135 } 136 137 return equal; 138 } 139 140 /** 141 * The overriding of this method should ensure that the objects of this class 142 * work correctly in a hashtable. 143 * 144 * @return The hash code of this object. 145 * @preconditions 146 * @postconditions 147 */ hashCode()148 public int hashCode() { 149 return Functions.hashCode(initializationVector_); 150 } 151 152 } 153