jdk/src/jdk.crypto.ucrypto/solaris/classes/com/oracle/security/ucrypto/NativeGCMCipher.java
author valeriep
Mon, 06 Apr 2015 20:24:27 +0000
changeset 31695 4d10942c9a7b
parent 31270 e6470b24700d
child 32646 db7c5592a47f
permissions -rw-r--r--
8074865: General crypto resilience changes Summary: Replaced Arrays.equals() with MessageDigest.isEqual() when comparing sensitive values Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     1
/*
30915
504f95d17f58 8081792: buffer size calculation issue in NativeGCMCipher
xuelei
parents: 27182
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     4
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    10
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    15
 * accompanied this code).
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    16
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    20
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    23
 * questions.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    24
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    25
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    26
package com.oracle.security.ucrypto;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    27
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    29
import java.nio.ByteBuffer;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    30
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    31
import java.util.Set;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    32
import java.util.Arrays;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    33
import java.security.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    34
import java.security.spec.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    35
import javax.crypto.*;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    36
import javax.crypto.spec.SecretKeySpec;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    37
import javax.crypto.spec.GCMParameterSpec;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    38
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    39
/**
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    40
 * Cipher wrapper class utilizing ucrypto APIs. This class currently supports
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    41
 * - AES/GCM/NoPADDING
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    42
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    43
 * @since 1.9
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    44
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    45
class NativeGCMCipher extends NativeCipher {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    46
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    47
    public static final class AesGcmNoPadding extends NativeGCMCipher {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    48
        public AesGcmNoPadding() throws NoSuchAlgorithmException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    49
            super(-1);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    50
        }
31270
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30915
diff changeset
    51
        public AesGcmNoPadding(int keySize) throws NoSuchAlgorithmException {
e6470b24700d 7191662: JCE providers should be located via ServiceLoader
valeriep
parents: 30915
diff changeset
    52
            super(keySize);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    53
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    54
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    55
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    56
    private static final int DEFAULT_TAG_LEN = 128; // same as SunJCE provider
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    57
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    58
    // buffer for storing AAD data; if null, meaning buffer content has been
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    59
    // supplied to native context
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    60
    private ByteArrayOutputStream aadBuffer = new ByteArrayOutputStream();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    61
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    62
    // buffer for storing input in decryption, not used for encryption
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    63
    private ByteArrayOutputStream ibuffer = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    64
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    65
    private int tagLen = DEFAULT_TAG_LEN;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    66
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    67
    /*
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    68
     * variables used for performing the GCM (key+iv) uniqueness check.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    69
     * To use GCM mode safely, the cipher object must be re-initialized
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    70
     * with a different combination of key + iv values for each
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    71
     * ENCRYPTION operation. However, checking all past key + iv values
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    72
     * isn't feasible. Thus, we only do a per-instance check of the
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    73
     * key + iv values used in previous encryption.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    74
     * For decryption operations, no checking is necessary.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    75
     */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    76
    private boolean requireReinit = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    77
    private byte[] lastEncKey = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    78
    private byte[] lastEncIv = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    79
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    80
    NativeGCMCipher(int fixedKeySize) throws NoSuchAlgorithmException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    81
        super(UcryptoMech.CRYPTO_AES_GCM, fixedKeySize);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    82
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    83
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    84
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    85
    protected void ensureInitialized() {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    86
        if (!initialized) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    87
            if (aadBuffer != null && aadBuffer.size() > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    88
                init(encrypt, keyValue, iv, tagLen, aadBuffer.toByteArray());
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    89
                aadBuffer = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    90
            } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    91
                init(encrypt, keyValue, iv, tagLen, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    92
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    93
            if (!initialized) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    94
                throw new UcryptoException("Cannot initialize Cipher");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    95
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    96
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    97
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    98
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    99
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   100
    protected int getOutputSizeByOperation(int inLen, boolean isDoFinal) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   101
        if (inLen < 0) return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   102
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   103
        if (!isDoFinal && (inLen == 0)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   104
            return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   105
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   106
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   107
        int result = inLen + bytesBuffered;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   108
        if (encrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   109
            if (isDoFinal) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   110
                result += tagLen/8;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   111
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   112
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   113
            if (ibuffer != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   114
                result += ibuffer.size();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   115
            }
30915
504f95d17f58 8081792: buffer size calculation issue in NativeGCMCipher
xuelei
parents: 27182
diff changeset
   116
            result -= tagLen/8;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   117
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   118
        if (result < 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   119
            result = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   120
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   121
        return result;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   122
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   123
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   124
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   125
    protected void reset(boolean doCancel) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   126
        super.reset(doCancel);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   127
        if (aadBuffer == null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   128
            aadBuffer = new ByteArrayOutputStream();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   129
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   130
            aadBuffer.reset();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   131
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   132
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   133
        if (ibuffer != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   134
            ibuffer.reset();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   135
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   136
        if (!encrypt) requireReinit = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   137
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   138
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   139
    // actual init() implementation - caller should clone key and iv if needed
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   140
    protected void init(boolean encrypt, byte[] keyVal, byte[] ivVal, int tLen, byte[] aad) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   141
        reset(true);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   142
        this.encrypt = encrypt;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   143
        this.keyValue = keyVal;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   144
        this.iv = ivVal;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   145
        long pCtxtVal = NativeCipher.nativeInit(mech.value(), encrypt, keyValue, iv,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   146
            tLen, aad);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   147
        initialized = (pCtxtVal != 0L);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   148
        if (initialized) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   149
            pCtxt = new CipherContextRef(this, pCtxtVal, encrypt);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   150
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   151
            throw new UcryptoException("Cannot initialize Cipher");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   152
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   153
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   154
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   155
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   156
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   157
    protected synchronized AlgorithmParameters engineGetParameters() {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   158
        AlgorithmParameters params = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   159
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   160
            if (iv != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   161
                GCMParameterSpec gcmSpec = new GCMParameterSpec(tagLen, iv.clone());
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   162
                params = AlgorithmParameters.getInstance("GCM");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   163
                params.init(gcmSpec);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   164
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   165
        } catch (GeneralSecurityException e) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   166
            // NoSuchAlgorithmException, NoSuchProviderException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   167
            // InvalidParameterSpecException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   168
            throw new UcryptoException("Could not encode parameters", e);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   169
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   170
        return params;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   171
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   172
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   173
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   174
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   175
    protected synchronized void engineInit(int opmode, Key key,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   176
            AlgorithmParameterSpec params, SecureRandom random)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   177
            throws InvalidKeyException, InvalidAlgorithmParameterException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   178
        checkKey(key);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   179
        if (opmode != Cipher.ENCRYPT_MODE &&
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   180
            opmode != Cipher.DECRYPT_MODE &&
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   181
            opmode != Cipher.WRAP_MODE &&
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   182
            opmode != Cipher.UNWRAP_MODE) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   183
            throw new InvalidAlgorithmParameterException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   184
                ("Unsupported mode: " + opmode);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   185
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   186
        boolean doEncrypt = (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   187
        byte[] keyBytes = key.getEncoded().clone();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   188
        byte[] ivBytes = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   189
        if (params != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   190
            if (!(params instanceof GCMParameterSpec)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   191
                throw new InvalidAlgorithmParameterException("GCMParameterSpec required");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   192
            } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   193
                tagLen = ((GCMParameterSpec) params).getTLen();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   194
                ivBytes = ((GCMParameterSpec) params).getIV();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   195
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   196
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   197
            if (doEncrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   198
                tagLen = DEFAULT_TAG_LEN;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   199
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   200
                // generate IV if none supplied for encryption
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   201
                ivBytes = new byte[blockSize];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   202
                new SecureRandom().nextBytes(ivBytes);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   203
            } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   204
                throw new InvalidAlgorithmParameterException("Parameters required for decryption");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   205
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   206
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   207
        if (doEncrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   208
            requireReinit = Arrays.equals(ivBytes, lastEncIv) &&
31695
4d10942c9a7b 8074865: General crypto resilience changes
valeriep
parents: 31270
diff changeset
   209
                MessageDigest.isEqual(keyBytes, lastEncKey);
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   210
            if (requireReinit) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   211
                throw new InvalidAlgorithmParameterException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   212
                    ("Cannot reuse iv for GCM encryption");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   213
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   214
            lastEncIv = ivBytes;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   215
            lastEncKey = keyBytes;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   216
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   217
            requireReinit = false;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   218
            ibuffer = new ByteArrayOutputStream();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   219
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   220
        init(doEncrypt, keyBytes, ivBytes, tagLen, null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   221
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   222
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   223
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   224
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   225
    protected synchronized void engineInit(int opmode, Key key, AlgorithmParameters params,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   226
            SecureRandom random)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   227
            throws InvalidKeyException, InvalidAlgorithmParameterException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   228
        AlgorithmParameterSpec spec = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   229
        if (params != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   230
            try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   231
                // mech must be UcryptoMech.CRYPTO_AES_GCM
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   232
                spec = params.getParameterSpec(GCMParameterSpec.class);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   233
            } catch (InvalidParameterSpecException iaps) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   234
                throw new InvalidAlgorithmParameterException(iaps);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   235
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   236
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   237
        engineInit(opmode, key, spec, random);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   238
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   239
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   240
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   241
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   242
    protected synchronized byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   243
        if (aadBuffer != null && aadBuffer.size() > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   244
            // init again with AAD data
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   245
            init(encrypt, keyValue, iv, tagLen, aadBuffer.toByteArray());
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   246
            aadBuffer = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   247
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   248
        if (requireReinit) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   249
            throw new IllegalStateException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   250
                ("Must use either different key or iv for GCM encryption");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   251
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   252
        if (inLen > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   253
            if (!encrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   254
                ibuffer.write(in, inOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   255
                return null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   256
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   257
            return super.engineUpdate(in, inOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   258
        } else return null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   259
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   260
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   261
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   262
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   263
    protected synchronized int engineUpdate(byte[] in, int inOfs, int inLen, byte[] out,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   264
            int outOfs) throws ShortBufferException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   265
        int len = getOutputSizeByOperation(inLen, false);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   266
        if (out.length - outOfs < len) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   267
            throw new ShortBufferException("Output buffer must be "
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   268
                                           + "(at least) " + len
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   269
                                           + " bytes long");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   270
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   271
        if (aadBuffer != null && aadBuffer.size() > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   272
            // init again with AAD data
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   273
            init(encrypt, keyValue, iv, tagLen, aadBuffer.toByteArray());
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   274
            aadBuffer = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   275
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   276
        if (requireReinit) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   277
            throw new IllegalStateException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   278
                ("Must use either different key or iv for GCM encryption");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   279
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   280
        if (inLen > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   281
            if (!encrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   282
                ibuffer.write(in, inOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   283
                return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   284
            } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   285
                return super.engineUpdate(in, inOfs, inLen, out, outOfs);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   286
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   287
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   288
        return 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   289
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   290
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   291
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   292
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   293
    protected synchronized void engineUpdateAAD(byte[] src, int srcOfs, int srcLen)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   294
            throws IllegalStateException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   295
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   296
        if ((src == null) || (srcOfs < 0) || (srcOfs + srcLen > src.length)) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   297
            throw new IllegalArgumentException("Invalid AAD");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   298
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   299
        if (keyValue == null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   300
            throw new IllegalStateException("Need to initialize Cipher first");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   301
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   302
        if (requireReinit) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   303
            throw new IllegalStateException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   304
                ("Must use either different key or iv for GCM encryption");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   305
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   306
        if (aadBuffer != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   307
            aadBuffer.write(src, srcOfs, srcLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   308
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   309
            // update has already been called
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   310
            throw new IllegalStateException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   311
                ("Update has been called; no more AAD data");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   312
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   313
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   314
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   315
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   316
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   317
    protected void engineUpdateAAD(ByteBuffer src)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   318
            throws IllegalStateException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   319
        if (src == null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   320
            throw new IllegalArgumentException("Invalid AAD");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   321
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   322
        if (keyValue == null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   323
            throw new IllegalStateException("Need to initialize Cipher first");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   324
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   325
        if (requireReinit) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   326
            throw new IllegalStateException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   327
                ("Must use either different key or iv for GCM encryption");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   328
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   329
        if (aadBuffer != null) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   330
            if (src.hasRemaining()) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   331
                byte[] srcBytes = new byte[src.remaining()];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   332
                src.get(srcBytes);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   333
                aadBuffer.write(srcBytes, 0, srcBytes.length);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   334
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   335
        } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   336
            // update has already been called
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   337
            throw new IllegalStateException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   338
                ("Update has been called; no more AAD data");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   339
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   340
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   341
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   342
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   343
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   344
    protected synchronized byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   345
            throws IllegalBlockSizeException, BadPaddingException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   346
        byte[] out = new byte[getOutputSizeByOperation(inLen, true)];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   347
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   348
            // delegate to the other engineDoFinal(...) method
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   349
            int k = engineDoFinal(in, inOfs, inLen, out, 0);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   350
            if (out.length != k) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   351
                out = Arrays.copyOf(out, k);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   352
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   353
            return out;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   354
        } catch (ShortBufferException e) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   355
            throw new UcryptoException("Internal Error", e);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   356
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   357
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   358
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   359
    // see JCE spec
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   360
    @Override
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   361
    protected synchronized int engineDoFinal(byte[] in, int inOfs, int inLen,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   362
                                             byte[] out, int outOfs)
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   363
        throws ShortBufferException, IllegalBlockSizeException,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   364
               BadPaddingException {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   365
        int len = getOutputSizeByOperation(inLen, true);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   366
        if (out.length - outOfs < len) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   367
            throw new ShortBufferException("Output buffer must be "
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   368
                                           + "(at least) " + len
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   369
                                           + " bytes long");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   370
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   371
        if (aadBuffer != null && aadBuffer.size() > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   372
            // init again with AAD data
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   373
            init(encrypt, keyValue, iv, tagLen, aadBuffer.toByteArray());
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   374
            aadBuffer = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   375
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   376
        if (requireReinit) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   377
            throw new IllegalStateException
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   378
                ("Must use either different key or iv for GCM encryption");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   379
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   380
        if (!encrypt) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   381
            if (inLen > 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   382
                ibuffer.write(in, inOfs, inLen);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   383
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   384
            inLen = ibuffer.size();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   385
            if (inLen < tagLen/8) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   386
                // Otherwise, Solaris lib will error out w/ CRYPTO_BUFFER_TOO_SMALL
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   387
                // when ucrypto_decrypt_final() is called
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   388
                throw new AEADBadTagException("Input too short - need tag");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   389
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   390
            // refresh 'in' to all buffered-up bytes
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   391
            in = ibuffer.toByteArray();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   392
            inOfs = 0;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   393
            ibuffer.reset();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   394
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   395
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   396
            return super.engineDoFinal(in, inOfs, inLen, out, outOfs);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   397
        } catch (UcryptoException ue) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   398
            if (ue.getMessage().equals("CRYPTO_INVALID_MAC")) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   399
                throw new AEADBadTagException("Tag does not match");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   400
            } else {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   401
                // pass it up
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   402
                throw ue;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   403
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   404
        } finally {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   405
            requireReinit = encrypt;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   406
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   407
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   408
}