1 /*
   2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2015 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.  Oracle designates this
   9  * particular file as subject to the "Classpath" exception as provided
  10  * by Oracle in the LICENSE file that accompanied this code.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  */
  26 /*
  27  * (C) Copyright IBM Corp. 2013
  28  */
  29 
  30 package com.sun.crypto.provider;
  31 
  32 import java.security.ProviderException;
  33 
  34 import jdk.internal.HotSpotIntrinsicCandidate;
  35 
  36 /**
  37  * This class represents the GHASH function defined in NIST 800-38D
  38  * 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 output
  40  * a 128-bit block.
  41  *
  42  * <p>This function is used in the implementation of GCM mode.
  43  *
  44  * @since 1.8
  45  */
  46 final class GHASH {
  47 
  48     private static long getLong(byte[] buffer, int offset) {
  49         long result = 0;
  50         int end = offset + 8;
  51         for (int i = offset; i < end; ++i) {
  52             result = (result << 8) + (buffer[i] & 0xFF);
  53         }
  54         return result;
  55     }
  56 
  57     private static void putLong(byte[] buffer, int offset, long value) {
  58         int end = offset + 8;
  59         for (int i = end - 1; i >= offset; --i) {
  60             buffer[i] = (byte) value;
  61             value >>= 8;
  62         }
  63     }
  64 
  65     private static final int AES_BLOCK_SIZE = 16;
  66 
  67     // Multiplies state[0], state[1] by subkeyH[0], subkeyH[1].
  68     private static void blockMult(long[] st, long[] subH) {
  69         long Z0 = 0;
  70         long Z1 = 0;
  71         long V0 = subH[0];
  72         long V1 = subH[1];
  73         long X;
  74 
  75         // Separate loops for processing state[0] and state[1].
  76         X = st[0];
  77         for (int i = 0; i < 64; i++) {
  78             // Zi+1 = Zi if bit i of x is 0
  79             long mask = X >> 63;
  80             Z0 ^= V0 & mask;
  81             Z1 ^= V1 & mask;
  82 
  83             // Save mask for conditional reduction below.
  84             mask = (V1 << 63) >> 63;
  85 
  86             // V = rightshift(V)
  87             long carry = V0 & 1;
  88             V0 = V0 >>> 1;
  89             V1 = (V1 >>> 1) | (carry << 63);
  90 
  91             // Conditional reduction modulo P128.
  92             V0 ^= 0xe100000000000000L & mask;
  93             X <<= 1;
  94         }
  95 
  96         X = st[1];
  97         for (int i = 64; i < 127; i++) {
  98             // Zi+1 = Zi if bit i of x is 0
  99             long mask = X >> 63;
 100             Z0 ^= V0 & mask;
 101             Z1 ^= V1 & mask;
 102 
 103             // Save mask for conditional reduction below.
 104             mask = (V1 << 63) >> 63;
 105 
 106             // V = rightshift(V)
 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 
 175     /**
 176      * Restores this object using the saved snapshot.
 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]);
 245         putLong(result, 8, state[1]);
 246         reset();
 247         return result;
 248     }
 249 }