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 /**
  35  * This class represents the GHASH function defined in NIST 800-38D
  36  * under section 6.4. It needs to be constructed w/ a hash subkey, i.e.
  37  * block H. Given input of 128-bit blocks, it will process and output
  38  * a 128-bit block.
  39  *
  40  * <p>This function is used in the implementation of GCM mode.
  41  *
  42  * @since 1.8
  43  */
  44 final class GHASH {
  45     
  46     private static long getLong(byte[] buffer, int offset) {
  47         long result = 0;
  48         int end = offset + 8;
  49         for (int i = offset; i < end; ++i) {
  50             result = (result << 8) + (buffer[i] & 0xFF);
  51         }
  52         return result;
  53     }
  54     
  55     private static void putLong(byte[] buffer, int offset, long value) {
  56         int end = offset + 8;
  57         for (int i = end - 1; i >= offset; --i) {
  58             buffer[i] = (byte) value;
  59             value >>= 8;
  60         }
  61     }
  62     
  63     private static final int AES_BLOCK_SIZE = 16;
  64     
  65     // Multiplies state[0], state[1] by subkeyH[0], subkeyH[1].
  66     private void blockMult(long[] subH) {
  67         long Z0 = 0;
  68         long Z1 = 0;
  69         long V0 = subH[0];
  70         long V1 = subH[1];
  71         long X; 
  72         
  73         // Separate loops for processing state[0] and state[1].
  74         X = state[0];
  75         for (int i = 0; i < 64; i++) {
  76             // Zi+1 = Zi if bit i of x is 0
  77             long mask = X >> 63;
  78             Z0 ^= V0 & mask;
  79             Z1 ^= V1 & mask;
  80             
  81             // Save mask for conditional reduction below.
  82             mask = (V1 << 63) >> 63;
  83             
  84             // V = rightshift(V)
  85             long carry = V0 & 1;
  86             V0 = V0 >>> 1;
  87             V1 = (V1 >>> 1) | (carry << 63);
  88 
  89             // Conditional reduction modulo P128. 
  90             V0 ^= 0xe100000000000000L & mask;
  91             X <<= 1;
  92         }
  93         
  94         X = state[1];
  95         for (int i = 64; i < 127; i++) {
  96             // Zi+1 = Zi if bit i of x is 0
  97             long mask = X >> 63;
  98             Z0 ^= V0 & mask;
  99             Z1 ^= V1 & mask;
 100             
 101             // Save mask for conditional reduction below.
 102             mask = (V1 << 63) >> 63;
 103             
 104             // V = rightshift(V)
 105             long carry = V0 & 1;
 106             V0 = V0 >>> 1;
 107             V1 = (V1 >>> 1) | (carry << 63);
 108 
 109             // Conditional reduction.
 110             V0 ^= 0xe100000000000000L & mask;
 111             X <<= 1;
 112         }
 113         
 114         // calculate Z128
 115         long mask = X >> 63;
 116         Z0 ^= V0 & mask;
 117         Z1 ^= V1 & mask;
 118         
 119         // Save result.
 120         state[0] = Z0;
 121         state[1] = Z1;
 122         
 123     }
 124 
 125     /* subkeyH and state are stored in long[] for GHASH intrinsic use */
 126 
 127     // hash subkey H; should not change after the object has been constructed
 128     private final long[] subkeyH;
 129 
 130     // buffer for storing hash
 131     private final long[] state;
 132 
 133     // variables for save/restore calls
 134     private long stateSave0, stateSave1;
 135 
 136     /**
 137      * Initializes the cipher in the specified mode with the given key
 138      * and iv.
 139      *
 140      * @param subkeyH the hash subkey
 141      *
 142      * @exception ProviderException if the given key is inappropriate for
 143      * initializing this digest
 144      */
 145     GHASH(byte[] subkeyH) throws ProviderException {
 146         if ((subkeyH == null) || subkeyH.length != AES_BLOCK_SIZE) {
 147             throw new ProviderException("Internal error");
 148         }
 149         state = new long[2];
 150         this.subkeyH = new long[2];
 151         this.subkeyH[0] = getLong(subkeyH, 0);
 152         this.subkeyH[1] = getLong(subkeyH, 8);
 153     }
 154 
 155     /**
 156      * Resets the GHASH object to its original state, i.e. blank w/
 157      * the same subkey H. Used after digest() is called and to re-use
 158      * this object for different data w/ the same H.
 159      */
 160     void reset() {
 161         state[0] = 0;
 162         state[1] = 0;
 163     }
 164 
 165     /**
 166      * Save the current snapshot of this GHASH object.
 167      */
 168     void save() {
 169         stateSave0 = state[0];
 170         stateSave1 = state[1];
 171     }
 172 
 173     /**
 174      * Restores this object using the saved snapshot.
 175      */
 176     void restore() {
 177         state[0] = stateSave0;
 178         state[1] = stateSave1;
 179     }
 180 
 181     private void processBlock(byte[] data, int ofs) {
 182         state[0] ^= getLong(data, ofs);
 183         state[1] ^= getLong(data, ofs + 8);
 184         blockMult(subkeyH);
 185     }
 186 
 187     void update(byte[] in) {
 188         update(in, 0, in.length);
 189     }
 190 
 191     void update(byte[] in, int inOfs, int inLen) {
 192         if (inLen == 0) {
 193             return;
 194         }
 195         if (inLen < 0 || inOfs < 0 || inLen > in.length - inOfs) {
 196             throw new RuntimeException("input length out of bound");
 197         }
 198         if (inLen % AES_BLOCK_SIZE != 0) {
 199             throw new RuntimeException("input length unsupported");
 200         }
 201 
 202         // These two checks are for C2 checking
 203         if (state.length != 2) {
 204             throw new RuntimeException("internal state has invalid length " +
 205                                        state.length);
 206         }
 207         if (subkeyH.length != 2) {
 208             throw new RuntimeException("internal subkeyH has invalid length" +
 209                                        subkeyH.length);
 210         }
 211 
 212         processBlocks(in, inOfs, inLen/AES_BLOCK_SIZE);
 213     }
 214 
 215     /*
 216      * This is an intrinsified method.  The method's argument list must match
 217      * the hotspot signature.  This method and methods called by it, cannot
 218      * throw exceptions or allocate arrays as it will breaking intrinsics
 219      */
 220     private void processBlocks(byte[] data, int inOfs, int blocks) {
 221         int offset = inOfs;
 222         while (blocks > 0) {
 223             processBlock(data, offset);
 224             blocks--;
 225             offset += AES_BLOCK_SIZE;
 226         }
 227     }
 228 
 229     byte[] digest() {
 230         byte[] result = new byte[AES_BLOCK_SIZE];
 231         putLong(result, 0, state[0]);
 232         putLong(result, 8, state[1]);
 233         reset();
 234         return result;
 235     }
 236 }