107 long carry = V0 & 1; 108 V0 = V0 >>> 1; 109 V1 = (V1 >>> 1) | (carry << 63); 110 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]; | 107 long carry = V0 & 1; 108 V0 = V0 >>> 1; 109 V1 = (V1 >>> 1) | (carry << 63); 110 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 /* subkeyHtbl and state are stored in long[] for GHASH intrinsic use */ 128 129 // hashtable subkeyHtbl; holds 2*9 powers of subkeyH computed using carry-less multiplication 130 private long[] subkeyHtbl; 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 subkeyHtbl = new long[2*9]; 153 subkeyHtbl[0] = getLong(subkeyH, 0); 154 subkeyHtbl[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, subkeyHtbl); 198 processBlocks(in, inOfs, inLen/AES_BLOCK_SIZE, state, subkeyHtbl); 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 != 18) { 223 throw new RuntimeException("internal subkeyHtbl 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]; |