111 // Conditional reduction. 112 V0 ^= 0xe100000000000000L & mask; 113 X <<= 1; 114 } 115 116 // calculate Z128 117 long mask = X >> 63; 118 Z0 ^= V0 & mask; 119 Z1 ^= V1 & mask; 120 121 // Save result. 122 st[0] = Z0; 123 st[1] = Z1; 124 125 } 126 127 /* subkeyH and state are stored in long[] for GHASH intrinsic use */ 128 129 // hash subkey H; should not change after the object has been constructed 130 private final long[] subkeyH; 131 132 // buffer for storing hash 133 private final long[] state; 134 135 // variables for save/restore calls 136 private long stateSave0, stateSave1; 137 138 /** 139 * Initializes the cipher in the specified mode with the given key 140 * and iv. 141 * 142 * @param subkeyH the hash subkey 143 * 144 * @exception ProviderException if the given key is inappropriate for 145 * initializing this digest 146 */ 147 GHASH(byte[] subkeyH) throws ProviderException { 148 if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) { 149 throw new ProviderException("Internal error"); 150 } 151 state = new long[2]; 152 this.subkeyH = new long[2]; 153 this.subkeyH[0] = getLong(subkeyH, 0); 154 this.subkeyH[1] = getLong(subkeyH, 8); 155 } 156 157 /** 158 * Resets the GHASH object to its original state, i.e. blank w/ 159 * the same subkey H. Used after digest() is called and to re-use 160 * this object for different data w/ the same H. 161 */ 162 void reset() { 163 state[0] = 0; 164 state[1] = 0; 165 } 166 167 /** 168 * Save the current snapshot of this GHASH object. 169 */ 170 void save() { 171 stateSave0 = state[0]; 172 stateSave1 = state[1]; 173 } 174 177 */ 178 void restore() { 179 state[0] = stateSave0; 180 state[1] = stateSave1; 181 } 182 183 private static void processBlock(byte[] data, int ofs, long[] st, long[] subH) { 184 st[0] ^= getLong(data, ofs); 185 st[1] ^= getLong(data, ofs + 8); 186 blockMult(st, subH); 187 } 188 189 void update(byte[] in) { 190 update(in, 0, in.length); 191 } 192 193 void update(byte[] in, int inOfs, int inLen) { 194 if (inLen == 0) { 195 return; 196 } 197 ghashRangeCheck(in, inOfs, inLen, state, subkeyH); 198 processBlocks(in, inOfs, inLen/AES_BLOCK_SIZE, state, subkeyH); 199 } 200 201 private static void ghashRangeCheck(byte[] in, int inOfs, int inLen, long[] st, long[] subH) { 202 if (inLen < 0) { 203 throw new RuntimeException("invalid input length: " + inLen); 204 } 205 if (inOfs < 0) { 206 throw new RuntimeException("invalid offset: " + inOfs); 207 } 208 if (inLen > in.length - inOfs) { 209 throw new RuntimeException("input length out of bound: " + 210 inLen + " > " + (in.length - inOfs)); 211 } 212 if (inLen % AES_BLOCK_SIZE != 0) { 213 throw new RuntimeException("input length/block size mismatch: " + 214 inLen); 215 } 216 217 // These two checks are for C2 checking 218 if (st.length != 2) { 219 throw new RuntimeException("internal state has invalid length: " + 220 st.length); 221 } 222 if (subH.length != 2) { 223 throw new RuntimeException("internal subkeyH has invalid length: " + 224 subH.length); 225 } 226 } 227 /* 228 * This is an intrinsified method. The method's argument list must match 229 * the hotspot signature. This method and methods called by it, cannot 230 * throw exceptions or allocate arrays as it will breaking intrinsics 231 */ 232 @HotSpotIntrinsicCandidate 233 private static void processBlocks(byte[] data, int inOfs, int blocks, long[] st, long[] subH) { 234 int offset = inOfs; 235 while (blocks > 0) { 236 processBlock(data, offset, st, subH); 237 blocks--; 238 offset += AES_BLOCK_SIZE; 239 } 240 } 241 242 byte[] digest() { 243 byte[] result = new byte[AES_BLOCK_SIZE]; 244 putLong(result, 0, state[0]); | 111 // Conditional reduction. 112 V0 ^= 0xe100000000000000L & mask; 113 X <<= 1; 114 } 115 116 // calculate Z128 117 long mask = X >> 63; 118 Z0 ^= V0 & mask; 119 Z1 ^= V1 & mask; 120 121 // Save result. 122 st[0] = Z0; 123 st[1] = Z1; 124 125 } 126 127 /* subkeyH and state are stored in long[] for GHASH intrinsic use */ 128 129 // hash subkey H; should not change after the object has been constructed 130 private final long[] subkeyH; 131 // subkeyHtbl holds 2*9 powers of subkeyH computed using carry-less multiplication 132 private long[] subkeyHtbl; 133 134 // buffer for storing hash 135 private final long[] state; 136 137 // variables for save/restore calls 138 private long stateSave0, stateSave1; 139 140 /** 141 * Initializes the cipher in the specified mode with the given key 142 * and iv. 143 * 144 * @param subkeyH the hash subkey 145 * 146 * @exception ProviderException if the given key is inappropriate for 147 * initializing this digest 148 */ 149 GHASH(byte[] subkeyH) throws ProviderException { 150 if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) { 151 throw new ProviderException("Internal error"); 152 } 153 state = new long[2]; 154 subkeyHtbl = new long[2*9]; 155 this.subkeyH = new long[2]; 156 this.subkeyH[0] = getLong(subkeyH, 0); 157 this.subkeyH[1] = getLong(subkeyH, 8); 158 subkeyHtbl[0] = this.subkeyH[0]; 159 subkeyHtbl[1] = this.subkeyH[1]; 160 for (int i = 1; i < 9 ; i++) { 161 subkeyHtbl[2*i] = 0; 162 subkeyHtbl[2*i+1] = 0; 163 } 164 } 165 166 /** 167 * Resets the GHASH object to its original state, i.e. blank w/ 168 * the same subkey H. Used after digest() is called and to re-use 169 * this object for different data w/ the same H. 170 */ 171 void reset() { 172 state[0] = 0; 173 state[1] = 0; 174 } 175 176 /** 177 * Save the current snapshot of this GHASH object. 178 */ 179 void save() { 180 stateSave0 = state[0]; 181 stateSave1 = state[1]; 182 } 183 186 */ 187 void restore() { 188 state[0] = stateSave0; 189 state[1] = stateSave1; 190 } 191 192 private static void processBlock(byte[] data, int ofs, long[] st, long[] subH) { 193 st[0] ^= getLong(data, ofs); 194 st[1] ^= getLong(data, ofs + 8); 195 blockMult(st, subH); 196 } 197 198 void update(byte[] in) { 199 update(in, 0, in.length); 200 } 201 202 void update(byte[] in, int inOfs, int inLen) { 203 if (inLen == 0) { 204 return; 205 } 206 ghashRangeCheck(in, inOfs, inLen, state, subkeyHtbl); 207 processBlocks(in, inOfs, inLen/AES_BLOCK_SIZE, state, subkeyHtbl); 208 } 209 210 private static void ghashRangeCheck(byte[] in, int inOfs, int inLen, long[] st, long[] subkeyHtbl) { 211 if (inLen < 0) { 212 throw new RuntimeException("invalid input length: " + inLen); 213 } 214 if (inOfs < 0) { 215 throw new RuntimeException("invalid offset: " + inOfs); 216 } 217 if (inLen > in.length - inOfs) { 218 throw new RuntimeException("input length out of bound: " + 219 inLen + " > " + (in.length - inOfs)); 220 } 221 if (inLen % AES_BLOCK_SIZE != 0) { 222 throw new RuntimeException("input length/block size mismatch: " + 223 inLen); 224 } 225 226 // These two checks are for C2 checking 227 if (st.length != 2) { 228 throw new RuntimeException("internal state has invalid length: " + 229 st.length); 230 } 231 if (subkeyHtbl.length != 18) { 232 throw new RuntimeException("internal subkeyH has invalid length: " + 233 subkeyHtbl.length); 234 } 235 } 236 /* 237 * This is an intrinsified method. The method's argument list must match 238 * the hotspot signature. This method and methods called by it, cannot 239 * throw exceptions or allocate arrays as it will breaking intrinsics 240 */ 241 @HotSpotIntrinsicCandidate 242 private static void processBlocks(byte[] data, int inOfs, int blocks, long[] st, long[] subH) { 243 int offset = inOfs; 244 while (blocks > 0) { 245 processBlock(data, offset, st, subH); 246 blocks--; 247 offset += AES_BLOCK_SIZE; 248 } 249 } 250 251 byte[] digest() { 252 byte[] result = new byte[AES_BLOCK_SIZE]; 253 putLong(result, 0, state[0]); |