Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/GHASH.java
41161 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2015 Red Hat, Inc.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation. Oracle designates this8* particular file as subject to the "Classpath" exception as provided9* by Oracle in the LICENSE file that accompanied this code.10*11* This code is distributed in the hope that it will be useful, but WITHOUT12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14* version 2 for more details (a copy is included in the LICENSE file that15* accompanied this code).16*17* You should have received a copy of the GNU General Public License version18* 2 along with this work; if not, write to the Free Software Foundation,19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.20*21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA22* or visit www.oracle.com if you need additional information or have any23* questions.24*/25/*26* (C) Copyright IBM Corp. 201327*/2829package com.sun.crypto.provider;3031import java.nio.ByteBuffer;32import java.security.ProviderException;3334import jdk.internal.vm.annotation.IntrinsicCandidate;3536/**37* This class represents the GHASH function defined in NIST 800-38D38* under section 6.4. It needs to be constructed w/ a hash subkey, i.e.39* block H. Given input of 128-bit blocks, it will process and output40* a 128-bit block.41*42* <p>This function is used in the implementation of GCM mode.43*44* @since 1.845*/46final class GHASH {4748private static long getLong(byte[] buffer, int offset) {49long result = 0;50int end = offset + 8;51for (int i = offset; i < end; ++i) {52result = (result << 8) + (buffer[i] & 0xFF);53}54return result;55}5657private static void putLong(byte[] buffer, int offset, long value) {58int end = offset + 8;59for (int i = end - 1; i >= offset; --i) {60buffer[i] = (byte) value;61value >>= 8;62}63}6465private static final int AES_BLOCK_SIZE = 16;6667// Multiplies state[0], state[1] by subkeyH[0], subkeyH[1].68private static void blockMult(long[] st, long[] subH) {69long Z0 = 0;70long Z1 = 0;71long V0 = subH[0];72long V1 = subH[1];73long X;7475// Separate loops for processing state[0] and state[1].76X = st[0];77for (int i = 0; i < 64; i++) {78// Zi+1 = Zi if bit i of x is 079long mask = X >> 63;80Z0 ^= V0 & mask;81Z1 ^= V1 & mask;8283// Save mask for conditional reduction below.84mask = (V1 << 63) >> 63;8586// V = rightshift(V)87long carry = V0 & 1;88V0 = V0 >>> 1;89V1 = (V1 >>> 1) | (carry << 63);9091// Conditional reduction modulo P128.92V0 ^= 0xe100000000000000L & mask;93X <<= 1;94}9596X = st[1];97for (int i = 64; i < 127; i++) {98// Zi+1 = Zi if bit i of x is 099long mask = X >> 63;100Z0 ^= V0 & mask;101Z1 ^= V1 & mask;102103// Save mask for conditional reduction below.104mask = (V1 << 63) >> 63;105106// V = rightshift(V)107long carry = V0 & 1;108V0 = V0 >>> 1;109V1 = (V1 >>> 1) | (carry << 63);110111// Conditional reduction.112V0 ^= 0xe100000000000000L & mask;113X <<= 1;114}115116// calculate Z128117long mask = X >> 63;118Z0 ^= V0 & mask;119Z1 ^= V1 & mask;120121// Save result.122st[0] = Z0;123st[1] = Z1;124125}126127/* subkeyHtbl and state are stored in long[] for GHASH intrinsic use */128129// hashtable subkeyHtbl; holds 2*9 powers of subkeyH computed using carry-less multiplication130private long[] subkeyHtbl;131132// buffer for storing hash133private final long[] state;134135// variables for save/restore calls136private long stateSave0, stateSave1;137138/**139* Initializes the cipher in the specified mode with the given key140* and iv.141*142* @param subkeyH the hash subkey143*144* @exception ProviderException if the given key is inappropriate for145* initializing this digest146*/147GHASH(byte[] subkeyH) throws ProviderException {148if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) {149throw new ProviderException("Internal error");150}151state = new long[2];152subkeyHtbl = new long[2*9];153subkeyHtbl[0] = getLong(subkeyH, 0);154subkeyHtbl[1] = getLong(subkeyH, 8);155}156157/**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-use160* this object for different data w/ the same H.161*/162void reset() {163state[0] = 0;164state[1] = 0;165}166167/**168* Save the current snapshot of this GHASH object.169*/170void save() {171stateSave0 = state[0];172stateSave1 = state[1];173}174175/**176* Restores this object using the saved snapshot.177*/178void restore() {179state[0] = stateSave0;180state[1] = stateSave1;181}182183private static void processBlock(byte[] data, int ofs, long[] st, long[] subH) {184st[0] ^= getLong(data, ofs);185st[1] ^= getLong(data, ofs + 8);186blockMult(st, subH);187}188189void update(byte[] in) {190update(in, 0, in.length);191}192193void update(byte[] in, int inOfs, int inLen) {194if (inLen == 0) {195return;196}197ghashRangeCheck(in, inOfs, inLen, state, subkeyHtbl);198processBlocks(in, inOfs, inLen/AES_BLOCK_SIZE, state, subkeyHtbl);199}200201// Maximum buffer size rotating ByteBuffer->byte[] intrinsic copy202private static final int MAX_LEN = 1024;203204// Will process as many blocks it can and will leave the remaining.205int update(ByteBuffer src, int inLen) {206inLen -= (inLen % AES_BLOCK_SIZE);207if (inLen == 0) {208return 0;209}210211int processed = inLen;212byte[] in = new byte[Math.min(MAX_LEN, inLen)];213while (processed > MAX_LEN ) {214src.get(in, 0, MAX_LEN);215update(in, 0 , MAX_LEN);216processed -= MAX_LEN;217}218src.get(in, 0, processed);219update(in, 0, processed);220return inLen;221}222223void doLastBlock(ByteBuffer src, int inLen) {224int processed = update(src, inLen);225if (inLen == processed) {226return;227}228byte[] block = new byte[AES_BLOCK_SIZE];229src.get(block, 0, inLen - processed);230update(block, 0, AES_BLOCK_SIZE);231}232233private static void ghashRangeCheck(byte[] in, int inOfs, int inLen, long[] st, long[] subH) {234if (inLen < 0) {235throw new RuntimeException("invalid input length: " + inLen);236}237if (inOfs < 0) {238throw new RuntimeException("invalid offset: " + inOfs);239}240if (inLen > in.length - inOfs) {241throw new RuntimeException("input length out of bound: " +242inLen + " > " + (in.length - inOfs));243}244if (inLen % AES_BLOCK_SIZE != 0) {245throw new RuntimeException("input length/block size mismatch: " +246inLen);247}248249// These two checks are for C2 checking250if (st.length != 2) {251throw new RuntimeException("internal state has invalid length: " +252st.length);253}254if (subH.length != 18) {255throw new RuntimeException("internal subkeyHtbl has invalid length: " +256subH.length);257}258}259/*260* This is an intrinsified method. The method's argument list must match261* the hotspot signature. This method and methods called by it, cannot262* throw exceptions or allocate arrays as it will breaking intrinsics263*/264@IntrinsicCandidate265private static void processBlocks(byte[] data, int inOfs, int blocks, long[] st, long[] subH) {266int offset = inOfs;267while (blocks > 0) {268processBlock(data, offset, st, subH);269blocks--;270offset += AES_BLOCK_SIZE;271}272}273274byte[] digest() {275byte[] result = new byte[AES_BLOCK_SIZE];276putLong(result, 0, state[0]);277putLong(result, 8, state[1]);278reset();279return result;280}281}282283284