Path: blob/master/src/java.base/share/classes/com/sun/crypto/provider/GCTR.java
41161 views
/*1* Copyright (c) 2013, 2020, 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*/2425/*26* (C) Copyright IBM Corp. 201327*/2829package com.sun.crypto.provider;3031import java.nio.ByteBuffer;32import java.nio.ByteOrder;33import javax.crypto.IllegalBlockSizeException;34import static com.sun.crypto.provider.AESConstants.AES_BLOCK_SIZE;3536/**37* This class represents the GCTR function defined in NIST 800-38D38* under section 6.5. With a given cipher object and initial counter39* block, a counter mode operation is performed. Blocksize is limited40* to 16 bytes.41*42* If any invariant is broken, failures can occur because the43* AESCrypt.encryptBlock method can be intrinsified on the HotSpot VM44* (see JDK-8067648 for details).45*46* The counter mode operations can be intrinsified and parallelized47* by using CounterMode.implCrypt() if HotSpot VM supports it on the48* architecture.49*50* <p>This function is used in the implementation of GCM mode.51*52* @since 1.853*/54final class GCTR extends CounterMode {5556// Maximum buffer size rotating ByteBuffer->byte[] intrinsic copy57private static final int MAX_LEN = 1024;5859GCTR(SymmetricCipher cipher, byte[] initialCounterBlk) {60super(cipher);61if (initialCounterBlk.length != AES_BLOCK_SIZE) {62throw new RuntimeException("length of initial counter block (" +63initialCounterBlk.length + ") not equal to AES_BLOCK_SIZE (" +64AES_BLOCK_SIZE + ")");65}6667iv = initialCounterBlk;68reset();69}7071@Override72String getFeedback() {73return "GCTR";74}7576// return the number of blocks until the lower 32 bits roll over77private long blocksUntilRollover() {78ByteBuffer buf = ByteBuffer.wrap(counter, counter.length - 4, 4);79buf.order(ByteOrder.BIG_ENDIAN);80long ctr32 = 0xFFFFFFFFL & buf.getInt();81long blocksLeft = (1L << 32) - ctr32;82return blocksLeft;83}8485// input must be multiples of 128-bit blocks when calling update86int update(byte[] in, int inOfs, int inLen, byte[] out, int outOfs) {87if (inLen - inOfs > in.length) {88throw new RuntimeException("input length out of bound");89}90if (inLen < 0 || inLen % AES_BLOCK_SIZE != 0) {91throw new RuntimeException("input length unsupported");92}93if (out.length - outOfs < inLen) {94throw new RuntimeException("output buffer too small");95}9697long blocksLeft = blocksUntilRollover();98int numOfCompleteBlocks = inLen / AES_BLOCK_SIZE;99if (numOfCompleteBlocks >= blocksLeft) {100// Counter Mode encryption cannot be used because counter will101// roll over incorrectly. Use GCM-specific code instead.102byte[] encryptedCntr = new byte[AES_BLOCK_SIZE];103for (int i = 0; i < numOfCompleteBlocks; i++) {104embeddedCipher.encryptBlock(counter, 0, encryptedCntr, 0);105for (int n = 0; n < AES_BLOCK_SIZE; n++) {106int index = (i * AES_BLOCK_SIZE + n);107out[outOfs + index] =108(byte) ((in[inOfs + index] ^ encryptedCntr[n]));109}110GaloisCounterMode.increment32(counter);111}112return inLen;113} else {114return encrypt(in, inOfs, inLen, out, outOfs);115}116}117118// input must be multiples of AES blocks, 128-bit, when calling update119int update(byte[] in, int inOfs, int inLen, ByteBuffer dst) {120if (inLen - inOfs > in.length) {121throw new RuntimeException("input length out of bound");122}123if (inLen < 0 || inLen % AES_BLOCK_SIZE != 0) {124throw new RuntimeException("input length unsupported");125}126// See GaloisCounterMode. decryptFinal(bytebuffer, bytebuffer) for127// details on the check for 'dst' having enough space for the result.128129long blocksLeft = blocksUntilRollover();130int numOfCompleteBlocks = inLen / AES_BLOCK_SIZE;131if (numOfCompleteBlocks >= blocksLeft) {132// Counter Mode encryption cannot be used because counter will133// roll over incorrectly. Use GCM-specific code instead.134byte[] encryptedCntr = new byte[AES_BLOCK_SIZE];135for (int i = 0; i < numOfCompleteBlocks; i++) {136embeddedCipher.encryptBlock(counter, 0, encryptedCntr, 0);137for (int n = 0; n < AES_BLOCK_SIZE; n++) {138int index = (i * AES_BLOCK_SIZE + n);139dst.put((byte) ((in[inOfs + index] ^ encryptedCntr[n])));140}141GaloisCounterMode.increment32(counter);142}143return inLen;144} else {145int len = inLen - inLen % AES_BLOCK_SIZE;146int processed = len;147byte[] out = new byte[Math.min(MAX_LEN, len)];148int offset = inOfs;149while (processed > MAX_LEN) {150encrypt(in, offset, MAX_LEN, out, 0);151dst.put(out, 0, MAX_LEN);152processed -= MAX_LEN;153offset += MAX_LEN;154}155encrypt(in, offset, processed, out, 0);156// If dst is less than blocksize, insert only what it can. Extra157// bytes would cause buffers with enough size to fail with a158// short buffer159dst.put(out, 0, Math.min(dst.remaining(), processed));160return len;161}162}163164// input operates on multiples of AES blocks, 128-bit, when calling update.165// The remainder is left in the src buffer.166int update(ByteBuffer src, ByteBuffer dst) {167long blocksLeft = blocksUntilRollover();168int numOfCompleteBlocks = src.remaining() / AES_BLOCK_SIZE;169if (numOfCompleteBlocks >= blocksLeft) {170// Counter Mode encryption cannot be used because counter will171// roll over incorrectly. Use GCM-specific code instead.172byte[] encryptedCntr = new byte[AES_BLOCK_SIZE];173for (int i = 0; i < numOfCompleteBlocks; i++) {174embeddedCipher.encryptBlock(counter, 0, encryptedCntr, 0);175for (int n = 0; n < AES_BLOCK_SIZE; n++) {176dst.put((byte) (src.get() ^ encryptedCntr[n]));177}178GaloisCounterMode.increment32(counter);179}180return numOfCompleteBlocks * AES_BLOCK_SIZE;181}182183int len = src.remaining() - (src.remaining() % AES_BLOCK_SIZE);184int processed = len;185byte[] in = new byte[Math.min(MAX_LEN, len)];186while (processed > MAX_LEN) {187src.get(in, 0, MAX_LEN);188encrypt(in, 0, MAX_LEN, in, 0);189dst.put(in, 0, MAX_LEN);190processed -= MAX_LEN;191}192src.get(in, 0, processed);193encrypt(in, 0, processed, in, 0);194dst.put(in, 0, processed);195return len;196}197198// input can be arbitrary size when calling doFinal199int doFinal(byte[] in, int inOfs, int inLen, byte[] out,200int outOfs) throws IllegalBlockSizeException {201try {202if (inLen < 0) {203throw new IllegalBlockSizeException("Negative input size!");204} else if (inLen > 0) {205int lastBlockSize = inLen % AES_BLOCK_SIZE;206int completeBlkLen = inLen - lastBlockSize;207// process the complete blocks first208update(in, inOfs, completeBlkLen, out, outOfs);209if (lastBlockSize != 0) {210// do the last partial block211byte[] encryptedCntr = new byte[AES_BLOCK_SIZE];212embeddedCipher.encryptBlock(counter, 0, encryptedCntr, 0);213for (int n = 0; n < lastBlockSize; n++) {214out[outOfs + completeBlkLen + n] =215(byte) ((in[inOfs + completeBlkLen + n] ^216encryptedCntr[n]));217}218}219}220} finally {221reset();222}223return inLen;224}225226// src can be arbitrary size when calling doFinal227int doFinal(ByteBuffer src, ByteBuffer dst) {228int len = src.remaining();229int lastBlockSize = len % AES_BLOCK_SIZE;230try {231update(src, dst);232if (lastBlockSize != 0) {233// do the last partial block234byte[] encryptedCntr = new byte[AES_BLOCK_SIZE];235embeddedCipher.encryptBlock(counter, 0, encryptedCntr, 0);236for (int n = 0; n < lastBlockSize; n++) {237dst.put((byte) (src.get() ^ encryptedCntr[n]));238}239}240} finally {241reset();242}243return len;244}245}246247248