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     // subkeyHtbl holds 2*9 powers of subkeyH computed using carry-less multiplication
 132     private long[] subkeyHtbl;
 133 
 134     // buffer for storing hash
 135     private final long[] state;
 136 
 137     // variables for save/restore calls
 138     private long stateSave0, stateSave1;
 139 
 140     /**
 141      * Initializes the cipher in the specified mode with the given key
 142      * and iv.
 143      *
 144      * @param subkeyH the hash subkey
 145      *
 146      * @exception ProviderException if the given key is inappropriate for
 147      * initializing this digest
 148      */
 149     GHASH(byte[] subkeyH) throws ProviderException {
 150         if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) {
 151             throw new ProviderException("Internal error");
 152         }
 153         state = new long[2];
 154         subkeyHtbl = new long[2*9];
 155         this.subkeyH = new long[2];
 156         this.subkeyH[0] = getLong(subkeyH, 0);
 157         this.subkeyH[1] = getLong(subkeyH, 8);
 158         subkeyHtbl[0] = this.subkeyH[0];
 159         subkeyHtbl[1] = this.subkeyH[1];
 160         for (int i = 1; i < 9 ; i++) {
 161             subkeyHtbl[2*i] = 0;
 162             subkeyHtbl[2*i+1] = 0;
 163         }
 164     }
 165 
 166     /**
 167      * Resets the GHASH object to its original state, i.e. blank w/
 168      * the same subkey H. Used after digest() is called and to re-use
 169      * this object for different data w/ the same H.
 170      */
 171     void reset() {
 172         state[0] = 0;
 173         state[1] = 0;
 174     }
 175 
 176     /**
 177      * Save the current snapshot of this GHASH object.
 178      */
 179     void save() {
 180         stateSave0 = state[0];
 181         stateSave1 = state[1];
 182     }
 183 
 184     /**
 185      * Restores this object using the saved snapshot.
 186      */
 187     void restore() {
 188         state[0] = stateSave0;
 189         state[1] = stateSave1;
 190     }
 191 
 192     private static void processBlock(byte[] data, int ofs, long[] st, long[] subH) {
 193         st[0] ^= getLong(data, ofs);
 194         st[1] ^= getLong(data, ofs + 8);
 195         blockMult(st, subH);
 196     }
 197 
 198     void update(byte[] in) {
 199         update(in, 0, in.length);
 200     }
 201 
 202     void update(byte[] in, int inOfs, int inLen) {
 203         if (inLen == 0) {
 204             return;
 205         }
 206         ghashRangeCheck(in, inOfs, inLen, state, subkeyHtbl);
 207         processBlocks(in, inOfs, inLen/AES_BLOCK_SIZE, state, subkeyHtbl);
 208     }
 209 
 210     private static void ghashRangeCheck(byte[] in, int inOfs, int inLen, long[] st, long[] subkeyHtbl) {
 211         if (inLen < 0) {
 212             throw new RuntimeException("invalid input length: " + inLen);
 213         }
 214         if (inOfs < 0) {
 215             throw new RuntimeException("invalid offset: " + inOfs);
 216         }
 217         if (inLen > in.length - inOfs) {
 218             throw new RuntimeException("input length out of bound: " +
 219                                        inLen + " > " + (in.length - inOfs));
 220         }
 221         if (inLen % AES_BLOCK_SIZE != 0) {
 222             throw new RuntimeException("input length/block size mismatch: " +
 223                                        inLen);
 224         }
 225 
 226         // These two checks are for C2 checking
 227         if (st.length != 2) {
 228             throw new RuntimeException("internal state has invalid length: " +
 229                                        st.length);
 230         }
 231         if (subkeyHtbl.length != 18) {
 232             throw new RuntimeException("internal subkeyH has invalid length: " +
 233                                        subkeyHtbl.length);
 234         }
 235     }
 236     /*
 237      * This is an intrinsified method.  The method's argument list must match
 238      * the hotspot signature.  This method and methods called by it, cannot
 239      * throw exceptions or allocate arrays as it will breaking intrinsics
 240      */
 241     @HotSpotIntrinsicCandidate
 242     private static void processBlocks(byte[] data, int inOfs, int blocks, long[] st, long[] subH) {
 243         int offset = inOfs;
 244         while (blocks > 0) {
 245             processBlock(data, offset, st, subH);
 246             blocks--;
 247             offset += AES_BLOCK_SIZE;
 248         }
 249     }
 250 
 251     byte[] digest() {
 252         byte[] result = new byte[AES_BLOCK_SIZE];
 253         putLong(result, 0, state[0]);
 254         putLong(result, 8, state[1]);
 255         reset();
 256         return result;
 257     }
 258 }