Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/GHASH.java
67848 views
/*1* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24/*25* (C) Copyright IBM Corp. 201326* Copyright (c) 2015 Red Hat, Inc.27*/2829package com.sun.crypto.provider;3031import java.lang.invoke.MethodHandles;32import java.lang.invoke.VarHandle;33import java.nio.ByteBuffer;34import java.nio.ByteOrder;35import java.security.ProviderException;3637import jdk.internal.vm.annotation.IntrinsicCandidate;3839/**40* This class represents the GHASH function defined in NIST 800-38D41* under section 6.4. It needs to be constructed w/ a hash subkey, i.e.42* block H. Given input of 128-bit blocks, it will process and output43* a 128-bit block.44*45* <p>This function is used in the implementation of GCM mode.46*47* @since 1.848*/4950final class GHASH implements Cloneable, GCM {5152private static final int AES_BLOCK_SIZE = 16;5354// Handle for converting byte[] <-> long55private static final VarHandle asLongView =56MethodHandles.byteArrayViewVarHandle(long[].class,57ByteOrder.BIG_ENDIAN);5859// Maximum buffer size rotating ByteBuffer->byte[] intrinsic copy60private static final int MAX_LEN = 1024;6162// Multiplies state[0], state[1] by subkeyH[0], subkeyH[1].63private static void blockMult(long[] st, long[] subH) {64long Z0 = 0;65long Z1 = 0;66long V0 = subH[0];67long V1 = subH[1];68long X;6970// Separate loops for processing state[0] and state[1].71X = st[0];72for (int i = 0; i < 64; i++) {73// Zi+1 = Zi if bit i of x is 074long mask = X >> 63;75Z0 ^= V0 & mask;76Z1 ^= V1 & mask;7778// Save mask for conditional reduction below.79mask = (V1 << 63) >> 63;8081// V = rightshift(V)82long carry = V0 & 1;83V0 = V0 >>> 1;84V1 = (V1 >>> 1) | (carry << 63);8586// Conditional reduction modulo P128.87V0 ^= 0xe100000000000000L & mask;88X <<= 1;89}9091X = st[1];92for (int i = 64; i < 127; i++) {93// Zi+1 = Zi if bit i of x is 094long mask = X >> 63;95Z0 ^= V0 & mask;96Z1 ^= V1 & mask;9798// Save mask for conditional reduction below.99mask = (V1 << 63) >> 63;100101// V = rightshift(V)102long carry = V0 & 1;103V0 = V0 >>> 1;104V1 = (V1 >>> 1) | (carry << 63);105106// Conditional reduction.107V0 ^= 0xe100000000000000L & mask;108X <<= 1;109}110111// calculate Z128112long mask = X >> 63;113Z0 ^= V0 & mask;114Z1 ^= V1 & mask;115116// Save result.117st[0] = Z0;118st[1] = Z1;119120}121122/* subkeyHtbl and state are stored in long[] for GHASH intrinsic use */123124// hashtable subkeyHtbl holds 2*9 powers of subkeyH computed using125// carry-less multiplication126private long[] subkeyHtbl;127128// buffer for storing hash129private final long[] state;130131/**132* Initializes the cipher in the specified mode with the given key133* and iv.134*135* @param subkeyH the hash subkey136*137* @exception ProviderException if the given key is inappropriate for138* initializing this digest139*/140GHASH(byte[] subkeyH) throws ProviderException {141if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) {142throw new ProviderException("Internal error");143}144state = new long[2];145subkeyHtbl = new long[2*9];146subkeyHtbl[0] = (long)asLongView.get(subkeyH, 0);147subkeyHtbl[1] = (long)asLongView.get(subkeyH, 8);148}149150// Cloning constructor151private GHASH(GHASH g) {152state = g.state.clone();153subkeyHtbl = g.subkeyHtbl.clone();154}155156@Override157public GHASH clone() {158return new GHASH(this);159}160161private static void processBlock(byte[] data, int ofs, long[] st,162long[] subH) {163st[0] ^= (long)asLongView.get(data, ofs);164st[1] ^= (long)asLongView.get(data, ofs + 8);165blockMult(st, subH);166}167168int update(byte[] in) {169return update(in, 0, in.length);170}171172int update(byte[] in, int inOfs, int inLen) {173if (inLen == 0) {174return 0;175}176int len = inLen - (inLen % AES_BLOCK_SIZE);177ghashRangeCheck(in, inOfs, len, state, subkeyHtbl);178processBlocks(in, inOfs, len / AES_BLOCK_SIZE, state, subkeyHtbl);179return len;180}181182// Will process as many blocks it can and will leave the remaining.183int update(ByteBuffer ct, int inLen) {184inLen -= (inLen % AES_BLOCK_SIZE);185if (inLen == 0) {186return 0;187}188189// If ct is a direct bytebuffer, send it directly to the intrinsic190if (ct.isDirect()) {191int processed = inLen;192processBlocksDirect(ct, inLen);193return processed;194} else if (!ct.isReadOnly()) {195// If a non-read only heap bytebuffer, use the array update method196int processed = update(ct.array(),197ct.arrayOffset() + ct.position(),198inLen);199ct.position(ct.position() + processed);200return processed;201}202203// Read only heap bytebuffers have to be copied and operated on204int to_process = inLen;205byte[] in = new byte[Math.min(MAX_LEN, inLen)];206while (to_process > MAX_LEN ) {207ct.get(in, 0, MAX_LEN);208update(in, 0 , MAX_LEN);209to_process -= MAX_LEN;210}211ct.get(in, 0, to_process);212update(in, 0, to_process);213return inLen;214}215216int doFinal(ByteBuffer src, int inLen) {217int processed = 0;218219if (inLen >= AES_BLOCK_SIZE) {220processed = update(src, inLen);221}222223if (inLen == processed) {224return processed;225}226byte[] block = new byte[AES_BLOCK_SIZE];227src.get(block, 0, inLen - processed);228update(block, 0, AES_BLOCK_SIZE);229return inLen;230}231232int doFinal(byte[] in, int inOfs, int inLen) {233int remainder = inLen % AES_BLOCK_SIZE;234inOfs += update(in, inOfs, inLen - remainder);235if (remainder > 0) {236byte[] block = new byte[AES_BLOCK_SIZE];237System.arraycopy(in, inOfs, block, 0,238remainder);239update(block, 0, AES_BLOCK_SIZE);240}241return inLen;242}243244private static void ghashRangeCheck(byte[] in, int inOfs, int inLen,245long[] st, long[] subH) {246if (inLen < 0) {247throw new RuntimeException("invalid input length: " + inLen);248}249if (inOfs < 0) {250throw new RuntimeException("invalid offset: " + inOfs);251}252if (inLen > in.length - inOfs) {253throw new RuntimeException("input length out of bound: " +254inLen + " > " + (in.length - inOfs));255}256if (inLen % AES_BLOCK_SIZE != 0) {257throw new RuntimeException("input length/block size mismatch: " +258inLen);259}260261// These two checks are for C2 checking262if (st.length != 2) {263throw new RuntimeException("internal state has invalid length: " +264st.length);265}266if (subH.length != 18) {267throw new RuntimeException("internal subkeyHtbl has invalid length: " +268subH.length);269}270}271/*272* This is an intrinsified method. The method's argument list must match273* the hotspot signature. This method and methods called by it, cannot274* throw exceptions or allocate arrays as it will breaking intrinsics275*/276@IntrinsicCandidate277private static void processBlocks(byte[] data, int inOfs, int blocks,278long[] st, long[] subH) {279int offset = inOfs;280while (blocks > 0) {281processBlock(data, offset, st, subH);282blocks--;283offset += AES_BLOCK_SIZE;284}285}286287// ProcessBlock for Direct ByteBuffers288private void processBlocksDirect(ByteBuffer ct, int inLen) {289byte[] data = new byte[Math.min(MAX_LEN, inLen)];290while (inLen > MAX_LEN) {291ct.get(data, 0, MAX_LEN);292processBlocks(data, 0, MAX_LEN / AES_BLOCK_SIZE, state,293subkeyHtbl);294inLen -= MAX_LEN;295}296if (inLen >= AES_BLOCK_SIZE) {297int len = inLen - (inLen % AES_BLOCK_SIZE);298ct.get(data, 0, len);299processBlocks(data, 0, len / AES_BLOCK_SIZE, state,300subkeyHtbl);301}302}303304byte[] digest() {305byte[] result = new byte[AES_BLOCK_SIZE];306asLongView.set(result, 0, state[0]);307asLongView.set(result, 8, state[1]);308// Reset state309state[0] = 0;310state[1] = 0;311return result;312}313314315/**316* None of the out or dst values are necessary, they are to satisfy the317* GCM interface requirement318*/319@Override320public int update(byte[] in, int inOfs, int inLen, byte[] out, int outOfs) {321return update(in, inOfs, inLen);322}323324@Override325public int update(byte[] in, int inOfs, int inLen, ByteBuffer dst) {326return update(in, inOfs, inLen);327}328329@Override330public int update(ByteBuffer src, ByteBuffer dst) {331return update(src, src.remaining());332}333334@Override335public int doFinal(byte[] in, int inOfs, int inLen, byte[] out,336int outOfs) {337return doFinal(in, inOfs, inLen);338}339340@Override341public int doFinal(ByteBuffer src, ByteBuffer dst) {342return doFinal(src, src.remaining());343}344}345346347