< prev index next >
src/java.base/share/classes/com/sun/crypto/provider/GHASH.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015 Red Hat, Inc.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
--- 1,7 ----
/*
! * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015 Red Hat, Inc.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
*** 60,77 ****
}
}
private static final int AES_BLOCK_SIZE = 16;
! // Multiplies state0, state1 by V0, V1.
! private void blockMult(long V0, long V1) {
long Z0 = 0;
long Z1 = 0;
long X;
! // Separate loops for processing state0 and state1.
! X = state0;
for (int i = 0; i < 64; i++) {
// Zi+1 = Zi if bit i of x is 0
long mask = X >> 63;
Z0 ^= V0 & mask;
Z1 ^= V1 & mask;
--- 60,79 ----
}
}
private static final int AES_BLOCK_SIZE = 16;
! // Multiplies state[0], state[1] by subkeyH[0], subkeyH[1].
! private void blockMult(long[] subH) {
long Z0 = 0;
long Z1 = 0;
+ long V0 = subH[0];
+ long V1 = subH[1];
long X;
! // Separate loops for processing state[0] and state[1].
! X = state[0];
for (int i = 0; i < 64; i++) {
// Zi+1 = Zi if bit i of x is 0
long mask = X >> 63;
Z0 ^= V0 & mask;
Z1 ^= V1 & mask;
*** 87,97 ****
// Conditional reduction modulo P128.
V0 ^= 0xe100000000000000L & mask;
X <<= 1;
}
! X = state1;
for (int i = 64; i < 127; i++) {
// Zi+1 = Zi if bit i of x is 0
long mask = X >> 63;
Z0 ^= V0 & mask;
Z1 ^= V1 & mask;
--- 89,99 ----
// Conditional reduction modulo P128.
V0 ^= 0xe100000000000000L & mask;
X <<= 1;
}
! X = state[1];
for (int i = 64; i < 127; i++) {
// Zi+1 = Zi if bit i of x is 0
long mask = X >> 63;
Z0 ^= V0 & mask;
Z1 ^= V1 & mask;
*** 113,131 ****
long mask = X >> 63;
Z0 ^= V0 & mask;
Z1 ^= V1 & mask;
// Save result.
! state0 = Z0;
! state1 = Z1;
}
// hash subkey H; should not change after the object has been constructed
! private final long subkeyH0, subkeyH1;
// buffer for storing hash
! private long state0, state1;
// variables for save/restore calls
private long stateSave0, stateSave1;
/**
--- 115,136 ----
long mask = X >> 63;
Z0 ^= V0 & mask;
Z1 ^= V1 & mask;
// Save result.
! state[0] = Z0;
! state[1] = Z1;
!
}
+ /* subkeyH and state are stored in long[] for GHASH intrinsic use */
+
// hash subkey H; should not change after the object has been constructed
! private final long[] subkeyH;
// buffer for storing hash
! private final long[] state;
// variables for save/restore calls
private long stateSave0, stateSave1;
/**
*** 139,207 ****
*/
GHASH(byte[] subkeyH) throws ProviderException {
if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) {
throw new ProviderException("Internal error");
}
! this.subkeyH0 = getLong(subkeyH, 0);
! this.subkeyH1 = getLong(subkeyH, 8);
}
/**
* Resets the GHASH object to its original state, i.e. blank w/
* the same subkey H. Used after digest() is called and to re-use
* this object for different data w/ the same H.
*/
void reset() {
! state0 = 0;
! state1 = 0;
}
/**
* Save the current snapshot of this GHASH object.
*/
void save() {
! stateSave0 = state0;
! stateSave1 = state1;
}
/**
* Restores this object using the saved snapshot.
*/
void restore() {
! state0 = stateSave0;
! state1 = stateSave1;
}
private void processBlock(byte[] data, int ofs) {
! if (data.length - ofs < AES_BLOCK_SIZE) {
! throw new RuntimeException("need complete block");
! }
! state0 ^= getLong(data, ofs);
! state1 ^= getLong(data, ofs + 8);
! blockMult(subkeyH0, subkeyH1);
}
void update(byte[] in) {
update(in, 0, in.length);
}
void update(byte[] in, int inOfs, int inLen) {
! if (inLen - inOfs > in.length) {
! throw new RuntimeException("input length out of bound");
}
if (inLen % AES_BLOCK_SIZE != 0) {
! throw new RuntimeException("input length unsupported");
}
! for (int i = inOfs; i < (inOfs + inLen); i += AES_BLOCK_SIZE) {
! processBlock(in, i);
}
}
byte[] digest() {
byte[] result = new byte[AES_BLOCK_SIZE];
! putLong(result, 0, state0);
! putLong(result, 8, state1);
reset();
return result;
}
}
--- 144,246 ----
*/
GHASH(byte[] subkeyH) throws ProviderException {
if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) {
throw new ProviderException("Internal error");
}
! state = new long[2];
! this.subkeyH = new long[2];
! this.subkeyH[0] = getLong(subkeyH, 0);
! this.subkeyH[1] = getLong(subkeyH, 8);
}
/**
* Resets the GHASH object to its original state, i.e. blank w/
* the same subkey H. Used after digest() is called and to re-use
* this object for different data w/ the same H.
*/
void reset() {
! state[0] = 0;
! state[1] = 0;
}
/**
* Save the current snapshot of this GHASH object.
*/
void save() {
! stateSave0 = state[0];
! stateSave1 = state[1];
}
/**
* Restores this object using the saved snapshot.
*/
void restore() {
! state[0] = stateSave0;
! state[1] = stateSave1;
}
private void processBlock(byte[] data, int ofs) {
! state[0] ^= getLong(data, ofs);
! state[1] ^= getLong(data, ofs + 8);
! blockMult(subkeyH);
}
void update(byte[] in) {
update(in, 0, in.length);
}
void update(byte[] in, int inOfs, int inLen) {
! if (inLen == 0) {
! return;
! }
! ghashRangeCheck(in, inOfs, inLen);
! processBlocks(in, inOfs, inLen/AES_BLOCK_SIZE);
! }
!
! private void ghashRangeCheck(byte[] in, int inOfs, int inLen) {
! if (inLen < 0) {
! throw new RuntimeException("invalid input length: " + inLen);
! }
! if (inOfs < 0) {
! throw new RuntimeException("invalid offset: " + inOfs);
! }
! if (inLen > in.length - inOfs) {
! throw new RuntimeException("input length out of bound: " +
! inLen + " > " + (in.length - inOfs));
}
if (inLen % AES_BLOCK_SIZE != 0) {
! throw new RuntimeException("input length/block size mismatch: " +
! inLen);
}
! // These two checks are for C2 checking
! if (state.length != 2) {
! throw new RuntimeException("internal state has invalid length: " +
! state.length);
! }
! if (subkeyH.length != 2) {
! throw new RuntimeException("internal subkeyH has invalid length: " +
! subkeyH.length);
! }
! }
! /*
! * This is an intrinsified method. The method's argument list must match
! * the hotspot signature. This method and methods called by it, cannot
! * throw exceptions or allocate arrays as it will breaking intrinsics
! */
! private void processBlocks(byte[] data, int inOfs, int blocks) {
! int offset = inOfs;
! while (blocks > 0) {
! processBlock(data, offset);
! blocks--;
! offset += AES_BLOCK_SIZE;
}
}
byte[] digest() {
byte[] result = new byte[AES_BLOCK_SIZE];
! putLong(result, 0, state[0]);
! putLong(result, 8, state[1]);
reset();
return result;
}
}
< prev index next >