src/java.base/share/classes/com/sun/crypto/provider/ChaCha20Cipher.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57791 34bbd91b1522
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     1
/*
55710
18130ed28231 8221345: Better Poly1305 support
jnimeh
parents: 50323
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     4
 *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    10
 *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    15
 * accompanied this code).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    16
 *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    20
 *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    23
 * questions.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    24
 */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    25
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    26
package com.sun.crypto.provider;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    27
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    29
import java.io.IOException;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    30
import java.lang.invoke.MethodHandles;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    31
import java.lang.invoke.VarHandle;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    32
import java.nio.ByteBuffer;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    33
import java.nio.ByteOrder;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    34
import java.security.*;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    35
import java.security.spec.AlgorithmParameterSpec;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    36
import java.util.Objects;
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
    37
import javax.crypto.*;
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    38
import javax.crypto.spec.ChaCha20ParameterSpec;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    39
import javax.crypto.spec.IvParameterSpec;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    40
import javax.crypto.spec.SecretKeySpec;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    41
import sun.security.util.DerValue;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    42
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    43
/**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    44
 * Implementation of the ChaCha20 cipher, as described in RFC 7539.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    45
 *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    46
 * @since 11
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    47
 */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    48
abstract class ChaCha20Cipher extends CipherSpi {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    49
    // Mode constants
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    50
    private static final int MODE_NONE = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    51
    private static final int MODE_AEAD = 1;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    52
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    53
    // Constants used in setting up the initial state
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    54
    private static final int STATE_CONST_0 = 0x61707865;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    55
    private static final int STATE_CONST_1 = 0x3320646e;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    56
    private static final int STATE_CONST_2 = 0x79622d32;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    57
    private static final int STATE_CONST_3 = 0x6b206574;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    58
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    59
    // The keystream block size in bytes and as integers
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    60
    private static final int KEYSTREAM_SIZE = 64;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    61
    private static final int KS_SIZE_INTS = KEYSTREAM_SIZE / Integer.BYTES;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    62
    private static final int CIPHERBUF_BASE = 1024;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    63
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    64
    // The initialization state of the cipher
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    65
    private boolean initialized;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    66
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    67
    // The mode of operation for this object
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    68
    protected int mode;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    69
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    70
    // The direction (encrypt vs. decrypt) for the data flow
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    71
    private int direction;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    72
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    73
    // Has all AAD data been provided (i.e. have we called our first update)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    74
    private boolean aadDone = false;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    75
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    76
    // The key's encoding in bytes for this object
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    77
    private byte[] keyBytes;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    78
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    79
    // The nonce used for this object
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    80
    private byte[] nonce;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    81
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    82
    // The counter
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    83
    private static final long MAX_UINT32 = 0x00000000FFFFFFFFL;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    84
    private long finalCounterValue;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    85
    private long counter;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    86
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    87
    // Two arrays, both implemented as 16-element integer arrays:
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    88
    // The base state, created at initialization time, and a working
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    89
    // state which is a clone of the start state, and is then modified
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    90
    // with the counter and the ChaCha20 block function.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    91
    private final int[] startState = new int[KS_SIZE_INTS];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    92
    private final byte[] keyStream = new byte[KEYSTREAM_SIZE];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    93
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    94
    // The offset into the current keystream
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    95
    private int keyStrOffset;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    96
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    97
    // AEAD-related fields and constants
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    98
    private static final int TAG_LENGTH = 16;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    99
    private long aadLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   100
    private long dataLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   101
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   102
    // Have a buffer of zero padding that can be read all or in part
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   103
    // by the authenticator.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   104
    private static final byte[] padBuf = new byte[TAG_LENGTH];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   105
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   106
    // Create a buffer for holding the AAD and Ciphertext lengths
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   107
    private final byte[] lenBuf = new byte[TAG_LENGTH];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   108
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   109
    // The authenticator (Poly1305) when running in AEAD mode
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   110
    protected String authAlgName;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   111
    private Poly1305 authenticator;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   112
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   113
    // The underlying engine for doing the ChaCha20/Poly1305 work
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   114
    private ChaChaEngine engine;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   115
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   116
    // Use this VarHandle for converting the state elements into little-endian
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   117
    // integer values for the ChaCha20 block function.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   118
    private static final VarHandle asIntLittleEndian =
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   119
            MethodHandles.byteArrayViewVarHandle(int[].class,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   120
                    ByteOrder.LITTLE_ENDIAN);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   121
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   122
    // Use this VarHandle for converting the AAD and data lengths into
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   123
    // little-endian long values for AEAD tag computations.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   124
    private static final VarHandle asLongLittleEndian =
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   125
            MethodHandles.byteArrayViewVarHandle(long[].class,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   126
                    ByteOrder.LITTLE_ENDIAN);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   127
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   128
    // Use this for pulling in 8 bytes at a time as longs for XOR operations
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   129
    private static final VarHandle asLongView =
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   130
            MethodHandles.byteArrayViewVarHandle(long[].class,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   131
                    ByteOrder.nativeOrder());
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   132
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   133
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   134
     * Default constructor.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   135
     */
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   136
    protected ChaCha20Cipher() { }
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   137
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   138
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   139
     * Set the mode of operation.  Since this is a stream cipher, there
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   140
     * is no mode of operation in the block-cipher sense of things.  The
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   141
     * protected {@code mode} field will only accept a value of {@code None}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   142
     * (case-insensitive).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   143
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   144
     * @param mode The mode value
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   145
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   146
     * @throws NoSuchAlgorithmException if a mode of operation besides
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   147
     *      {@code None} is provided.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   148
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   149
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   150
    protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   151
        if (mode.equalsIgnoreCase("None") == false) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   152
            throw new NoSuchAlgorithmException("Mode must be None");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   153
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   154
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   155
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   156
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   157
     * Set the padding scheme.  Padding schemes do not make sense with stream
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   158
     * ciphers, but allow {@code NoPadding}.  See JCE spec.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   159
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   160
     * @param padding The padding type.  The only allowed value is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   161
     *      {@code NoPadding} case insensitive).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   162
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   163
     * @throws NoSuchPaddingException if a padding scheme besides
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   164
     *      {@code NoPadding} is provided.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   165
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   166
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   167
    protected void engineSetPadding(String padding)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   168
            throws NoSuchPaddingException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   169
        if (padding.equalsIgnoreCase("NoPadding") == false) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   170
            throw new NoSuchPaddingException("Padding must be NoPadding");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   171
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   172
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   173
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   174
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   175
     * Returns the block size.  For a stream cipher like ChaCha20, this
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   176
     * value will always be zero.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   177
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   178
     * @return This method always returns 0.  See the JCE Specification.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   179
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   180
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   181
    protected int engineGetBlockSize() {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   182
        return 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   183
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   184
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   185
    /**
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   186
     * Get the output size required to hold the result of the next update or
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   187
     * doFinal operation.  In simple stream-cipher
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   188
     * mode, the output size will equal the input size.  For ChaCha20-Poly1305
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   189
     * for encryption the output size will be the sum of the input length
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   190
     * and tag length.  For decryption, the output size will be  the input
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   191
     * length plus any previously unprocessed data minus the tag
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   192
     * length, minimum zero.
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   193
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   194
     * @param inputLen the length in bytes of the input
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   195
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   196
     * @return the output length in bytes.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   197
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   198
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   199
    protected int engineGetOutputSize(int inputLen) {
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   200
        return engine.getOutputSize(inputLen, true);
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   201
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   202
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   203
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   204
     * Get the nonce value used.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   205
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   206
     * @return the nonce bytes.  For ChaCha20 this will be a 12-byte value.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   207
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   208
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   209
    protected byte[] engineGetIV() {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   210
        return nonce.clone();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   211
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   212
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   213
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   214
     * Get the algorithm parameters for this cipher.  For the ChaCha20
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   215
     * cipher, this will always return {@code null} as there currently is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   216
     * no {@code AlgorithmParameters} implementation for ChaCha20.  For
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   217
     * ChaCha20-Poly1305, a {@code ChaCha20Poly1305Parameters} object will be
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   218
     * created and initialized with the configured nonce value and returned
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   219
     * to the caller.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   220
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   221
     * @return a {@code null} value if the ChaCha20 cipher is used (mode is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   222
     * MODE_NONE), or a {@code ChaCha20Poly1305Parameters} object containing
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   223
     * the nonce if the mode is MODE_AEAD.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   224
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   225
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   226
    protected AlgorithmParameters engineGetParameters() {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   227
        AlgorithmParameters params = null;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   228
        if (mode == MODE_AEAD) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   229
            try {
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   230
                // Place the 12-byte nonce into a DER-encoded OCTET_STRING
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   231
                params = AlgorithmParameters.getInstance("ChaCha20-Poly1305");
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   232
                params.init((new DerValue(
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   233
                        DerValue.tag_OctetString, nonce).toByteArray()));
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   234
            } catch (NoSuchAlgorithmException | IOException exc) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   235
                throw new RuntimeException(exc);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   236
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   237
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   238
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   239
        return params;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   240
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   241
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   242
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   243
     * Initialize the engine using a key and secure random implementation.  If
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   244
     * a SecureRandom object is provided it will be used to create a random
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   245
     * nonce value.  If the {@code random} parameter is null an internal
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   246
     * secure random source will be used to create the random nonce.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   247
     * The counter value will be set to 1.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   248
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   249
     * @param opmode the type of operation to do.  This value may not be
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   250
     *      {@code Cipher.DECRYPT_MODE} or {@code Cipher.UNWRAP_MODE} mode
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   251
     *      because it must generate random parameters like the nonce.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   252
     * @param key a 256-bit key suitable for ChaCha20
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   253
     * @param random a {@code SecureRandom} implementation used to create the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   254
     *      random nonce.  If {@code null} is used for the random object,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   255
     *      then an internal secure random source will be used to create the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   256
     *      nonce.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   257
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   258
     * @throws UnsupportedOperationException if the mode of operation
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   259
     *      is {@code Cipher.WRAP_MODE} or {@code Cipher.UNWRAP_MODE}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   260
     *      (currently unsupported).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   261
     * @throws InvalidKeyException if the key is of the wrong type or is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   262
     *      not 256-bits in length.  This will also be thrown if the opmode
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   263
     *      parameter is {@code Cipher.DECRYPT_MODE}.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   264
     *      {@code Cipher.UNWRAP_MODE} would normally be disallowed in this
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   265
     *      context but it is preempted by the UOE case above.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   266
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   267
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   268
    protected void engineInit(int opmode, Key key, SecureRandom random)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   269
            throws InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   270
        if (opmode != Cipher.DECRYPT_MODE) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   271
            byte[] newNonce = createRandomNonce(random);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   272
            counter = 1;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   273
            init(opmode, key, newNonce);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   274
        } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   275
            throw new InvalidKeyException("Default parameter generation " +
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   276
                "disallowed in DECRYPT and UNWRAP modes");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   277
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   278
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   279
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   280
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   281
     * Initialize the engine using a key and secure random implementation.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   282
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   283
     * @param opmode the type of operation to do.  This value must be either
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   284
     *      {@code Cipher.ENCRYPT_MODE} or {@code Cipher.DECRYPT_MODE}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   285
     * @param key a 256-bit key suitable for ChaCha20
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   286
     * @param params a {@code ChaCha20ParameterSpec} that will provide
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   287
     *      the nonce and initial block counter value.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   288
     * @param random a {@code SecureRandom} implementation, this parameter
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   289
     *      is not used in this form of the initializer.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   290
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   291
     * @throws UnsupportedOperationException if the mode of operation
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   292
     *      is {@code Cipher.WRAP_MODE} or {@code Cipher.UNWRAP_MODE}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   293
     *      (currently unsupported).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   294
     * @throws InvalidKeyException if the key is of the wrong type or is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   295
     *      not 256-bits in length.  This will also be thrown if the opmode
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   296
     *      parameter is not {@code Cipher.ENCRYPT_MODE} or
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   297
     *      {@code Cipher.DECRYPT_MODE} (excepting the UOE case above).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   298
     * @throws InvalidAlgorithmParameterException if {@code params} is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   299
     *      not a {@code ChaCha20ParameterSpec}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   300
     * @throws NullPointerException if {@code params} is {@code null}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   301
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   302
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   303
    protected void engineInit(int opmode, Key key,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   304
            AlgorithmParameterSpec params, SecureRandom random)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   305
            throws InvalidKeyException, InvalidAlgorithmParameterException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   306
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   307
        // If AlgorithmParameterSpec is null, then treat this like an init
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   308
        // of the form (int, Key, SecureRandom)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   309
        if (params == null) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   310
            engineInit(opmode, key, random);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   311
            return;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   312
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   313
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   314
        // We will ignore the secure random implementation and use the nonce
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   315
        // from the AlgorithmParameterSpec instead.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   316
        byte[] newNonce = null;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   317
        switch (mode) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   318
            case MODE_NONE:
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   319
                if (!(params instanceof ChaCha20ParameterSpec)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   320
                    throw new InvalidAlgorithmParameterException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   321
                        "ChaCha20 algorithm requires ChaCha20ParameterSpec");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   322
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   323
                ChaCha20ParameterSpec chaParams = (ChaCha20ParameterSpec)params;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   324
                newNonce = chaParams.getNonce();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   325
                counter = ((long)chaParams.getCounter()) & 0x00000000FFFFFFFFL;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   326
                break;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   327
            case MODE_AEAD:
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   328
                if (!(params instanceof IvParameterSpec)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   329
                    throw new InvalidAlgorithmParameterException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   330
                        "ChaCha20-Poly1305 requires IvParameterSpec");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   331
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   332
                IvParameterSpec ivParams = (IvParameterSpec)params;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   333
                newNonce = ivParams.getIV();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   334
                if (newNonce.length != 12) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   335
                    throw new InvalidAlgorithmParameterException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   336
                        "ChaCha20-Poly1305 nonce must be 12 bytes in length");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   337
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   338
                break;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   339
            default:
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   340
                // Should never happen
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   341
                throw new RuntimeException("ChaCha20 in unsupported mode");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   342
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   343
        init(opmode, key, newNonce);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   344
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   345
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   346
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   347
     * Initialize the engine using the {@code AlgorithmParameter} initialization
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   348
     * format.  This cipher does supports initialization with
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   349
     * {@code AlgorithmParameter} objects for ChaCha20-Poly1305 but not for
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   350
     * ChaCha20 as a simple stream cipher.  In the latter case, it will throw
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   351
     * an {@code InvalidAlgorithmParameterException} if the value is non-null.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   352
     * If a null value is supplied for the {@code params} field
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   353
     * the cipher will be initialized with the counter value set to 1 and
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   354
     * a random nonce.  If {@code null} is used for the random object,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   355
     * then an internal secure random source will be used to create the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   356
     * nonce.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   357
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   358
     * @param opmode the type of operation to do.  This value must be either
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   359
     *      {@code Cipher.ENCRYPT_MODE} or {@code Cipher.DECRYPT_MODE}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   360
     * @param key a 256-bit key suitable for ChaCha20
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   361
     * @param params a {@code null} value if the algorithm is ChaCha20, or
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   362
     *      the appropriate {@code AlgorithmParameters} object containing the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   363
     *      nonce information if the algorithm is ChaCha20-Poly1305.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   364
     * @param random a {@code SecureRandom} implementation, may be {@code null}.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   365
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   366
     * @throws UnsupportedOperationException if the mode of operation
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   367
     *      is {@code Cipher.WRAP_MODE} or {@code Cipher.UNWRAP_MODE}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   368
     *      (currently unsupported).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   369
     * @throws InvalidKeyException if the key is of the wrong type or is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   370
     *      not 256-bits in length.  This will also be thrown if the opmode
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   371
     *      parameter is not {@code Cipher.ENCRYPT_MODE} or
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   372
     *      {@code Cipher.DECRYPT_MODE} (excepting the UOE case above).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   373
     * @throws InvalidAlgorithmParameterException if {@code params} is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   374
     *      non-null and the algorithm is ChaCha20.  This exception will be
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   375
     *      also thrown if the algorithm is ChaCha20-Poly1305 and an incorrect
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   376
     *      {@code AlgorithmParameters} object is supplied.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   377
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   378
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   379
    protected void engineInit(int opmode, Key key,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   380
            AlgorithmParameters params, SecureRandom random)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   381
            throws InvalidKeyException, InvalidAlgorithmParameterException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   382
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   383
        // If AlgorithmParameters is null, then treat this like an init
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   384
        // of the form (int, Key, SecureRandom)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   385
        if (params == null) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   386
            engineInit(opmode, key, random);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   387
            return;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   388
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   389
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   390
        byte[] newNonce = null;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   391
        switch (mode) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   392
            case MODE_NONE:
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   393
                throw new InvalidAlgorithmParameterException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   394
                        "AlgorithmParameters not supported");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   395
            case MODE_AEAD:
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   396
                String paramAlg = params.getAlgorithm();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   397
                if (!paramAlg.equalsIgnoreCase("ChaCha20-Poly1305")) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   398
                    throw new InvalidAlgorithmParameterException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   399
                            "Invalid parameter type: " + paramAlg);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   400
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   401
                try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   402
                    DerValue dv = new DerValue(params.getEncoded());
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   403
                    newNonce = dv.getOctetString();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   404
                    if (newNonce.length != 12) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   405
                        throw new InvalidAlgorithmParameterException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   406
                                "ChaCha20-Poly1305 nonce must be " +
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   407
                                "12 bytes in length");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   408
                    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   409
                } catch (IOException ioe) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   410
                    throw new InvalidAlgorithmParameterException(ioe);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   411
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   412
                break;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   413
            default:
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   414
                throw new RuntimeException("Invalid mode: " + mode);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   415
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   416
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   417
        // If after all the above processing we still don't have a nonce value
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   418
        // then supply a random one provided a random source has been given.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   419
        if (newNonce == null) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   420
            newNonce = createRandomNonce(random);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   421
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   422
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   423
        // Continue with initialization
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   424
        init(opmode, key, newNonce);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   425
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   426
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   427
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   428
     * Update additional authenticated data (AAD).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   429
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   430
     * @param src the byte array containing the authentication data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   431
     * @param offset the starting offset in the buffer to update.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   432
     * @param len the amount of authentication data to update.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   433
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   434
     * @throws IllegalStateException if the cipher has not been initialized,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   435
     *      {@code engineUpdate} has been called, or the cipher is running
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   436
     *      in a non-AEAD mode of operation.  It will also throw this
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   437
     *      exception if the submitted AAD would overflow a 64-bit length
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   438
     *      counter.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   439
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   440
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   441
    protected void engineUpdateAAD(byte[] src, int offset, int len) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   442
        if (!initialized) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   443
            // We know that the cipher has not been initialized if the key
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   444
            // is still null.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   445
            throw new IllegalStateException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   446
                    "Attempted to update AAD on uninitialized Cipher");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   447
        } else if (aadDone) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   448
            // No AAD updates allowed after the PT/CT update method is called
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   449
            throw new IllegalStateException("Attempted to update AAD on " +
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   450
                    "Cipher after plaintext/ciphertext update");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   451
        } else if (mode != MODE_AEAD) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   452
            throw new IllegalStateException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   453
                    "Cipher is running in non-AEAD mode");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   454
        } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   455
            try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   456
                aadLen = Math.addExact(aadLen, len);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   457
                authUpdate(src, offset, len);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   458
            } catch (ArithmeticException ae) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   459
                throw new IllegalStateException("AAD overflow", ae);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   460
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   461
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   462
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   463
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   464
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   465
     * Update additional authenticated data (AAD).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   466
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   467
     * @param src the ByteBuffer containing the authentication data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   468
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   469
     * @throws IllegalStateException if the cipher has not been initialized,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   470
     *      {@code engineUpdate} has been called, or the cipher is running
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   471
     *      in a non-AEAD mode of operation.  It will also throw this
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   472
     *      exception if the submitted AAD would overflow a 64-bit length
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   473
     *      counter.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   474
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   475
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   476
    protected void engineUpdateAAD(ByteBuffer src) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   477
        if (!initialized) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   478
            // We know that the cipher has not been initialized if the key
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   479
            // is still null.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   480
            throw new IllegalStateException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   481
                    "Attempted to update AAD on uninitialized Cipher");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   482
        } else if (aadDone) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   483
            // No AAD updates allowed after the PT/CT update method  is called
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   484
            throw new IllegalStateException("Attempted to update AAD on " +
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   485
                    "Cipher after plaintext/ciphertext update");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   486
        } else if (mode != MODE_AEAD) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   487
            throw new IllegalStateException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   488
                    "Cipher is running in non-AEAD mode");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   489
        } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   490
            try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   491
                aadLen = Math.addExact(aadLen, (src.limit() - src.position()));
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   492
                authenticator.engineUpdate(src);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   493
            } catch (ArithmeticException ae) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   494
                throw new IllegalStateException("AAD overflow", ae);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   495
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   496
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   497
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   498
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   499
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   500
     * Create a random 12-byte nonce.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   501
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   502
     * @param random a {@code SecureRandom} object.  If {@code null} is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   503
     * provided a new {@code SecureRandom} object will be instantiated.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   504
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   505
     * @return a 12-byte array containing the random nonce.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   506
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   507
    private byte[] createRandomNonce(SecureRandom random) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   508
        byte[] newNonce = new byte[12];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   509
        SecureRandom rand = (random != null) ? random : new SecureRandom();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   510
        rand.nextBytes(newNonce);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   511
        return newNonce;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   512
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   513
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   514
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   515
     * Perform additional initialization actions based on the key and operation
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   516
     * type.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   517
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   518
     * @param opmode the type of operation to do.  This value must be either
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   519
     *      {@code Cipher.ENCRYPT_MODE} or {@code Cipher.DECRYPT_MODE}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   520
     * @param key a 256-bit key suitable for ChaCha20
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   521
     * @param newNonce the new nonce value for this initialization.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   522
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   523
     * @throws UnsupportedOperationException if the {@code opmode} parameter
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   524
     *      is {@code Cipher.WRAP_MODE} or {@code Cipher.UNWRAP_MODE}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   525
     *      (currently unsupported).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   526
     * @throws InvalidKeyException if the {@code opmode} parameter is not
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   527
     *      {@code Cipher.ENCRYPT_MODE} or {@code Cipher.DECRYPT_MODE}, or
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   528
     *      if the key format is not {@code RAW}.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   529
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   530
    private void init(int opmode, Key key, byte[] newNonce)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   531
            throws InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   532
        if ((opmode == Cipher.WRAP_MODE) || (opmode == Cipher.UNWRAP_MODE)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   533
            throw new UnsupportedOperationException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   534
                    "WRAP_MODE and UNWRAP_MODE are not currently supported");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   535
        } else if ((opmode != Cipher.ENCRYPT_MODE) &&
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   536
                (opmode != Cipher.DECRYPT_MODE)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   537
            throw new InvalidKeyException("Unknown opmode: " + opmode);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   538
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   539
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   540
        // Make sure that the provided key and nonce are unique before
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   541
        // assigning them to the object.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   542
        byte[] newKeyBytes = getEncodedKey(key);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   543
        checkKeyAndNonce(newKeyBytes, newNonce);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   544
        this.keyBytes = newKeyBytes;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   545
        nonce = newNonce;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   546
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   547
        // Now that we have the key and nonce, we can build the initial state
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   548
        setInitialState();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   549
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   550
        if (mode == MODE_NONE) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   551
            engine = new EngineStreamOnly();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   552
        } else if (mode == MODE_AEAD) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   553
            if (opmode == Cipher.ENCRYPT_MODE) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   554
                engine = new EngineAEADEnc();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   555
            } else if (opmode == Cipher.DECRYPT_MODE) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   556
                engine = new EngineAEADDec();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   557
            } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   558
                throw new InvalidKeyException("Not encrypt or decrypt mode");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   559
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   560
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   561
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   562
        // We can also get one block's worth of keystream created
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   563
        finalCounterValue = counter + MAX_UINT32;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   564
        generateKeystream();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   565
        direction = opmode;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   566
        aadDone = false;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   567
        this.keyStrOffset = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   568
        initialized = true;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   569
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   570
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   571
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   572
     * Check the key and nonce bytes to make sure that they do not repeat
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   573
     * across reinitialization.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   574
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   575
     * @param newKeyBytes the byte encoding for the newly provided key
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   576
     * @param newNonce the new nonce to be used with this initialization
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   577
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   578
     * @throws InvalidKeyException if both the key and nonce match the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   579
     *      previous initialization.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   580
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   581
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   582
    private void checkKeyAndNonce(byte[] newKeyBytes, byte[] newNonce)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   583
            throws InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   584
        // A new initialization must have either a different key or nonce
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   585
        // so the starting state for each block is not the same as the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   586
        // previous initialization.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   587
        if (MessageDigest.isEqual(newKeyBytes, keyBytes) &&
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   588
                MessageDigest.isEqual(newNonce, nonce)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   589
            throw new InvalidKeyException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   590
                    "Matching key and nonce from previous initialization");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   591
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   592
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   593
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   594
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   595
     * Return the encoded key as a byte array
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   596
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   597
     * @param key the {@code Key} object used for this {@code Cipher}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   598
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   599
     * @return the key bytes
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   600
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   601
     * @throws InvalidKeyException if the key is of the wrong type or length,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   602
     *      or if the key encoding format is not {@code RAW}.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   603
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   604
    private static byte[] getEncodedKey(Key key) throws InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   605
        if ("RAW".equals(key.getFormat()) == false) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   606
            throw new InvalidKeyException("Key encoding format must be RAW");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   607
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   608
        byte[] encodedKey = key.getEncoded();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   609
        if (encodedKey == null || encodedKey.length != 32) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   610
            throw new InvalidKeyException("Key length must be 256 bits");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   611
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   612
        return encodedKey;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   613
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   614
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   615
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   616
     * Update the currently running operation with additional data
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   617
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   618
     * @param in the plaintext or ciphertext input bytes (depending on the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   619
     *      operation type).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   620
     * @param inOfs the offset into the input array
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   621
     * @param inLen the length of the data to use for the update operation.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   622
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   623
     * @return the resulting plaintext or ciphertext bytes (depending on
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   624
     *      the operation type)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   625
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   626
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   627
    protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   628
        byte[] out = new byte[engine.getOutputSize(inLen, false)];
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   629
        try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   630
            engine.doUpdate(in, inOfs, inLen, out, 0);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   631
        } catch (ShortBufferException | KeyException exc) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   632
            throw new RuntimeException(exc);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   633
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   634
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   635
        return out;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   636
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   637
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   638
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   639
     * Update the currently running operation with additional data
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   640
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   641
     * @param in the plaintext or ciphertext input bytes (depending on the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   642
     *      operation type).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   643
     * @param inOfs the offset into the input array
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   644
     * @param inLen the length of the data to use for the update operation.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   645
     * @param out the byte array that will hold the resulting data.  The array
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   646
     *      must be large enough to hold the resulting data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   647
     * @param outOfs the offset for the {@code out} buffer to begin writing
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   648
     *      the resulting data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   649
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   650
     * @return the length in bytes of the data written into the {@code out}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   651
     *      buffer.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   652
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   653
     * @throws ShortBufferException if the buffer {@code out} does not have
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   654
     *      enough space to hold the resulting data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   655
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   656
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   657
    protected int engineUpdate(byte[] in, int inOfs, int inLen,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   658
            byte[] out, int outOfs) throws ShortBufferException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   659
        int bytesUpdated = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   660
        try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   661
            bytesUpdated = engine.doUpdate(in, inOfs, inLen, out, outOfs);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   662
        } catch (KeyException ke) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   663
            throw new RuntimeException(ke);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   664
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   665
        return bytesUpdated;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   666
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   667
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   668
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   669
     * Complete the currently running operation using any final
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   670
     * data provided by the caller.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   671
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   672
     * @param in the plaintext or ciphertext input bytes (depending on the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   673
     *      operation type).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   674
     * @param inOfs the offset into the input array
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   675
     * @param inLen the length of the data to use for the update operation.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   676
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   677
     * @return the resulting plaintext or ciphertext bytes (depending on
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   678
     *      the operation type)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   679
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   680
     * @throws AEADBadTagException if, during decryption, the provided tag
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   681
     *      does not match the calculated tag.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   682
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   683
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   684
    protected byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   685
            throws AEADBadTagException {
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
   686
        byte[] output = new byte[engine.getOutputSize(inLen, true)];
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   687
        try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   688
            engine.doFinal(in, inOfs, inLen, output, 0);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   689
        } catch (ShortBufferException | KeyException exc) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   690
            throw new RuntimeException(exc);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   691
        } finally {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   692
            // Regardless of what happens, the cipher cannot be used for
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   693
            // further processing until it has been freshly initialized.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   694
            initialized = false;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   695
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   696
        return output;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   697
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   698
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   699
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   700
     * Complete the currently running operation using any final
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   701
     * data provided by the caller.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   702
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   703
     * @param in the plaintext or ciphertext input bytes (depending on the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   704
     *      operation type).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   705
     * @param inOfs the offset into the input array
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   706
     * @param inLen the length of the data to use for the update operation.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   707
     * @param out the byte array that will hold the resulting data.  The array
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   708
     *      must be large enough to hold the resulting data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   709
     * @param outOfs the offset for the {@code out} buffer to begin writing
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   710
     *      the resulting data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   711
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   712
     * @return the length in bytes of the data written into the {@code out}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   713
     *      buffer.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   714
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   715
     * @throws ShortBufferException if the buffer {@code out} does not have
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   716
     *      enough space to hold the resulting data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   717
     * @throws AEADBadTagException if, during decryption, the provided tag
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   718
     *      does not match the calculated tag.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   719
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   720
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   721
    protected int engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   722
            int outOfs) throws ShortBufferException, AEADBadTagException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   723
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   724
        int bytesUpdated = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   725
        try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   726
            bytesUpdated = engine.doFinal(in, inOfs, inLen, out, outOfs);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   727
        } catch (KeyException ke) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   728
            throw new RuntimeException(ke);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   729
        } finally {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   730
            // Regardless of what happens, the cipher cannot be used for
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   731
            // further processing until it has been freshly initialized.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   732
            initialized = false;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   733
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   734
        return bytesUpdated;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   735
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   736
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   737
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   738
     * Wrap a {@code Key} using this Cipher's current encryption parameters.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   739
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   740
     * @param key the key to wrap.  The data that will be encrypted will
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   741
     *      be the provided {@code Key} in its encoded form.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   742
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   743
     * @return a byte array consisting of the wrapped key.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   744
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   745
     * @throws UnsupportedOperationException this will (currently) always
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   746
     *      be thrown, as this method is not currently supported.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   747
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   748
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   749
    protected byte[] engineWrap(Key key) throws IllegalBlockSizeException,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   750
            InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   751
        throw new UnsupportedOperationException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   752
                "Wrap operations are not supported");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   753
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   754
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   755
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   756
     * Unwrap a {@code Key} using this Cipher's current encryption parameters.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   757
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   758
     * @param wrappedKey the key to unwrap.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   759
     * @param algorithm the algorithm associated with the wrapped key
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   760
     * @param type the type of the wrapped key. This is one of
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   761
     *      {@code SECRET_KEY}, {@code PRIVATE_KEY}, or {@code PUBLIC_KEY}.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   762
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   763
     * @return the unwrapped key as a {@code Key} object.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   764
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   765
     * @throws UnsupportedOperationException this will (currently) always
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   766
     *      be thrown, as this method is not currently supported.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   767
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   768
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   769
    protected Key engineUnwrap(byte[] wrappedKey, String algorithm,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   770
            int type) throws InvalidKeyException, NoSuchAlgorithmException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   771
        throw new UnsupportedOperationException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   772
                "Unwrap operations are not supported");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   773
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   774
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   775
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   776
     * Get the length of a provided key in bits.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   777
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   778
     * @param key the key to be evaluated
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   779
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   780
     * @return the length of the key in bits
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   781
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   782
     * @throws InvalidKeyException if the key is invalid or does not
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   783
     *      have an encoded form.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   784
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   785
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   786
    protected int engineGetKeySize(Key key) throws InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   787
        byte[] encodedKey = getEncodedKey(key);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   788
        return encodedKey.length << 3;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   789
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   790
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   791
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   792
     * Set the initial state.  This will populate the state array and put the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   793
     * key and nonce into their proper locations.  The counter field is not
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   794
     * set here.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   795
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   796
     * @throws IllegalArgumentException if the key or nonce are not in
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   797
     *      their proper lengths (32 bytes for the key, 12 bytes for the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   798
     *      nonce).
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   799
     * @throws InvalidKeyException if the key does not support an encoded form.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   800
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   801
    private void setInitialState() throws InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   802
        // Apply constants to first 4 words
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   803
        startState[0] = STATE_CONST_0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   804
        startState[1] = STATE_CONST_1;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   805
        startState[2] = STATE_CONST_2;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   806
        startState[3] = STATE_CONST_3;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   807
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   808
        // Apply the key bytes as 8 32-bit little endian ints (4 through 11)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   809
        for (int i = 0; i < 32; i += 4) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   810
            startState[(i / 4) + 4] = (keyBytes[i] & 0x000000FF) |
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   811
                ((keyBytes[i + 1] << 8) & 0x0000FF00) |
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   812
                ((keyBytes[i + 2] << 16) & 0x00FF0000) |
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   813
                ((keyBytes[i + 3] << 24) & 0xFF000000);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   814
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   815
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   816
        startState[12] = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   817
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   818
        // The final integers for the state are from the nonce
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   819
        // interpreted as 3 little endian integers
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   820
        for (int i = 0; i < 12; i += 4) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   821
            startState[(i / 4) + 13] = (nonce[i] & 0x000000FF) |
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   822
                ((nonce[i + 1] << 8) & 0x0000FF00) |
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   823
                ((nonce[i + 2] << 16) & 0x00FF0000) |
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   824
                ((nonce[i + 3] << 24) & 0xFF000000);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   825
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   826
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   827
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   828
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   829
     * Using the current state and counter create the next set of keystream
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   830
     * bytes.  This method will generate the next 512 bits of keystream and
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   831
     * return it in the {@code keyStream} parameter.  Following the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   832
     * block function the counter will be incremented.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   833
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   834
    private void generateKeystream() {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   835
        chaCha20Block(startState, counter, keyStream);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   836
        counter++;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   837
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   838
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   839
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   840
     * Perform a full 20-round ChaCha20 transform on the initial state.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   841
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   842
     * @param initState the starting state, not including the counter
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   843
     *      value.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   844
     * @param counter the counter value to apply
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   845
     * @param result  the array that will hold the result of the ChaCha20
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   846
     *      block function.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   847
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   848
     * @note it is the caller's responsibility to ensure that the workState
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   849
     * is sized the same as the initState, no checking is performed internally.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   850
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   851
    private static void chaCha20Block(int[] initState, long counter,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   852
                                      byte[] result) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   853
        // Create an initial state and clone a working copy
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   854
        int ws00 = STATE_CONST_0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   855
        int ws01 = STATE_CONST_1;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   856
        int ws02 = STATE_CONST_2;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   857
        int ws03 = STATE_CONST_3;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   858
        int ws04 = initState[4];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   859
        int ws05 = initState[5];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   860
        int ws06 = initState[6];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   861
        int ws07 = initState[7];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   862
        int ws08 = initState[8];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   863
        int ws09 = initState[9];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   864
        int ws10 = initState[10];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   865
        int ws11 = initState[11];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   866
        int ws12 = (int)counter;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   867
        int ws13 = initState[13];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   868
        int ws14 = initState[14];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   869
        int ws15 = initState[15];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   870
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   871
        // Peform 10 iterations of the 8 quarter round set
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   872
        for (int round = 0; round < 10; round++) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   873
            ws00 += ws04;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   874
            ws12 = Integer.rotateLeft(ws12 ^ ws00, 16);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   875
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   876
            ws08 += ws12;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   877
            ws04 = Integer.rotateLeft(ws04 ^ ws08, 12);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   878
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   879
            ws00 += ws04;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   880
            ws12 = Integer.rotateLeft(ws12 ^ ws00, 8);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   881
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   882
            ws08 += ws12;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   883
            ws04 = Integer.rotateLeft(ws04 ^ ws08, 7);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   884
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   885
            ws01 += ws05;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   886
            ws13 = Integer.rotateLeft(ws13 ^ ws01, 16);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   887
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   888
            ws09 += ws13;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   889
            ws05 = Integer.rotateLeft(ws05 ^ ws09, 12);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   890
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   891
            ws01 += ws05;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   892
            ws13 = Integer.rotateLeft(ws13 ^ ws01, 8);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   893
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   894
            ws09 += ws13;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   895
            ws05 = Integer.rotateLeft(ws05 ^ ws09, 7);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   896
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   897
            ws02 += ws06;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   898
            ws14 = Integer.rotateLeft(ws14 ^ ws02, 16);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   899
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   900
            ws10 += ws14;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   901
            ws06 = Integer.rotateLeft(ws06 ^ ws10, 12);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   902
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   903
            ws02 += ws06;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   904
            ws14 = Integer.rotateLeft(ws14 ^ ws02, 8);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   905
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   906
            ws10 += ws14;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   907
            ws06 = Integer.rotateLeft(ws06 ^ ws10, 7);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   908
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   909
            ws03 += ws07;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   910
            ws15 = Integer.rotateLeft(ws15 ^ ws03, 16);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   911
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   912
            ws11 += ws15;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   913
            ws07 = Integer.rotateLeft(ws07 ^ ws11, 12);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   914
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   915
            ws03 += ws07;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   916
            ws15 = Integer.rotateLeft(ws15 ^ ws03, 8);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   917
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   918
            ws11 += ws15;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   919
            ws07 = Integer.rotateLeft(ws07 ^ ws11, 7);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   920
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   921
            ws00 += ws05;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   922
            ws15 = Integer.rotateLeft(ws15 ^ ws00, 16);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   923
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   924
            ws10 += ws15;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   925
            ws05 = Integer.rotateLeft(ws05 ^ ws10, 12);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   926
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   927
            ws00 += ws05;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   928
            ws15 = Integer.rotateLeft(ws15 ^ ws00, 8);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   929
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   930
            ws10 += ws15;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   931
            ws05 = Integer.rotateLeft(ws05 ^ ws10, 7);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   932
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   933
            ws01 += ws06;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   934
            ws12 = Integer.rotateLeft(ws12 ^ ws01, 16);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   935
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   936
            ws11 += ws12;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   937
            ws06 = Integer.rotateLeft(ws06 ^ ws11, 12);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   938
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   939
            ws01 += ws06;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   940
            ws12 = Integer.rotateLeft(ws12 ^ ws01, 8);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   941
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   942
            ws11 += ws12;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   943
            ws06 = Integer.rotateLeft(ws06 ^ ws11, 7);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   944
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   945
            ws02 += ws07;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   946
            ws13 = Integer.rotateLeft(ws13 ^ ws02, 16);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   947
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   948
            ws08 += ws13;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   949
            ws07 = Integer.rotateLeft(ws07 ^ ws08, 12);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   950
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   951
            ws02 += ws07;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   952
            ws13 = Integer.rotateLeft(ws13 ^ ws02, 8);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   953
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   954
            ws08 += ws13;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   955
            ws07 = Integer.rotateLeft(ws07 ^ ws08, 7);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   956
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   957
            ws03 += ws04;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   958
            ws14 = Integer.rotateLeft(ws14 ^ ws03, 16);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   959
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   960
            ws09 += ws14;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   961
            ws04 = Integer.rotateLeft(ws04 ^ ws09, 12);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   962
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   963
            ws03 += ws04;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   964
            ws14 = Integer.rotateLeft(ws14 ^ ws03, 8);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   965
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   966
            ws09 += ws14;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   967
            ws04 = Integer.rotateLeft(ws04 ^ ws09, 7);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   968
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   969
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   970
        // Add the end working state back into the original state
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   971
        asIntLittleEndian.set(result, 0, ws00 + STATE_CONST_0);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   972
        asIntLittleEndian.set(result, 4, ws01 + STATE_CONST_1);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   973
        asIntLittleEndian.set(result, 8, ws02 + STATE_CONST_2);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   974
        asIntLittleEndian.set(result, 12, ws03 + STATE_CONST_3);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   975
        asIntLittleEndian.set(result, 16, ws04 + initState[4]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   976
        asIntLittleEndian.set(result, 20, ws05 + initState[5]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   977
        asIntLittleEndian.set(result, 24, ws06 + initState[6]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   978
        asIntLittleEndian.set(result, 28, ws07 + initState[7]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   979
        asIntLittleEndian.set(result, 32, ws08 + initState[8]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   980
        asIntLittleEndian.set(result, 36, ws09 + initState[9]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   981
        asIntLittleEndian.set(result, 40, ws10 + initState[10]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   982
        asIntLittleEndian.set(result, 44, ws11 + initState[11]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   983
        // Add the counter back into workState[12]
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   984
        asIntLittleEndian.set(result, 48, ws12 + (int)counter);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   985
        asIntLittleEndian.set(result, 52, ws13 + initState[13]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   986
        asIntLittleEndian.set(result, 56, ws14 + initState[14]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   987
        asIntLittleEndian.set(result, 60, ws15 + initState[15]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   988
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   989
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   990
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   991
     * Perform the ChaCha20 transform.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   992
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   993
     * @param in the array of bytes for the input
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   994
     * @param inOff the offset into the input array to start the transform
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   995
     * @param inLen the length of the data to perform the transform on.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   996
     * @param out the output array.  It must be large enough to hold the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   997
     *      resulting data
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   998
     * @param outOff the offset into the output array to place the resulting
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   999
     *      data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1000
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1001
    private void chaCha20Transform(byte[] in, int inOff, int inLen,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1002
            byte[] out, int outOff) throws KeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1003
        int remainingData = inLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1004
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1005
        while (remainingData > 0) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1006
            int ksRemain = keyStream.length - keyStrOffset;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1007
            if (ksRemain <= 0) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1008
                if (counter <= finalCounterValue) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1009
                    generateKeystream();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1010
                    keyStrOffset = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1011
                    ksRemain = keyStream.length;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1012
                } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1013
                    throw new KeyException("Counter exhausted.  " +
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1014
                            "Reinitialize with new key and/or nonce");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1015
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1016
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1017
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1018
            // XOR each byte in the keystream against the input
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1019
            int xformLen = Math.min(remainingData, ksRemain);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1020
            xor(keyStream, keyStrOffset, in, inOff, out, outOff, xformLen);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1021
            outOff += xformLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1022
            inOff += xformLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1023
            keyStrOffset += xformLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1024
            remainingData -= xformLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1025
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1026
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1027
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1028
    private static void xor(byte[] in1, int off1, byte[] in2, int off2,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1029
            byte[] out, int outOff, int len) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1030
        while (len >= 8) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1031
            long v1 = (long) asLongView.get(in1, off1);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1032
            long v2 = (long) asLongView.get(in2, off2);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1033
            asLongView.set(out, outOff, v1 ^ v2);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1034
            off1 += 8;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1035
            off2 += 8;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1036
            outOff += 8;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1037
            len -= 8;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1038
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1039
        while (len > 0) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1040
            out[outOff] = (byte) (in1[off1] ^ in2[off2]);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1041
            off1++;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1042
            off2++;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1043
            outOff++;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1044
            len--;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1045
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1046
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1047
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1048
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1049
     * Perform initialization steps for the authenticator
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1050
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1051
     * @throws InvalidKeyException if the key is unusable for some reason
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1052
     *      (invalid length, etc.)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1053
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1054
    private void initAuthenticator() throws InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1055
        authenticator = new Poly1305();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1056
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1057
        // Derive the Poly1305 key from the starting state
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1058
        byte[] serializedKey = new byte[KEYSTREAM_SIZE];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1059
        chaCha20Block(startState, 0, serializedKey);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1060
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1061
        authenticator.engineInit(new SecretKeySpec(serializedKey, 0, 32,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1062
                authAlgName), null);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1063
        aadLen = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1064
        dataLen = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1065
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1066
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1067
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1068
     * Update the authenticator state with data.  This routine can be used
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1069
     * to add data to the authenticator, whether AAD or application data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1070
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1071
     * @param data the data to stir into the authenticator.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1072
     * @param offset the offset into the data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1073
     * @param length the length of data to add to the authenticator.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1074
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1075
     * @return the number of bytes processed by this method.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1076
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1077
    private int authUpdate(byte[] data, int offset, int length) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1078
        Objects.checkFromIndexSize(offset, length, data.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1079
        authenticator.engineUpdate(data, offset, length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1080
        return length;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1081
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1082
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1083
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1084
     * Finalize the data and return the tag.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1085
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1086
     * @param data an array containing any remaining data to process.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1087
     * @param dataOff the offset into the data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1088
     * @param length the length of the data to process.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1089
     * @param out the array to write the resulting tag into
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1090
     * @param outOff the offset to begin writing the data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1091
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1092
     * @throws ShortBufferException if there is insufficient room to
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1093
     *      write the tag.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1094
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1095
    private void authFinalizeData(byte[] data, int dataOff, int length,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1096
            byte[] out, int outOff) throws ShortBufferException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1097
        // Update with the final chunk of ciphertext, then pad to a
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1098
        // multiple of 16.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1099
        if (data != null) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1100
            dataLen += authUpdate(data, dataOff, length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1101
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1102
        authPad16(dataLen);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1103
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1104
        // Also write the AAD and ciphertext data lengths as little-endian
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1105
        // 64-bit values.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1106
        authWriteLengths(aadLen, dataLen, lenBuf);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1107
        authenticator.engineUpdate(lenBuf, 0, lenBuf.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1108
        byte[] tag = authenticator.engineDoFinal();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1109
        Objects.checkFromIndexSize(outOff, tag.length, out.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1110
        System.arraycopy(tag, 0, out, outOff, tag.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1111
        aadLen = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1112
        dataLen = 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1113
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1114
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1115
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1116
     * Based on a given length of data, make the authenticator process
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1117
     * zero bytes that will pad the length out to a multiple of 16.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1118
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1119
     * @param dataLen the starting length to be padded.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1120
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1121
    private void authPad16(long dataLen) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1122
        // Pad out the AAD or data to a multiple of 16 bytes
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1123
        authenticator.engineUpdate(padBuf, 0,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1124
                (TAG_LENGTH - ((int)dataLen & 15)) & 15);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1125
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1126
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1127
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1128
     * Write the two 64-bit little-endian length fields into an array
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1129
     * for processing by the poly1305 authenticator.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1130
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1131
     * @param aLen the length of the AAD.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1132
     * @param dLen the length of the application data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1133
     * @param buf the buffer to write the two lengths into.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1134
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1135
     * @note it is the caller's responsibility to provide an array large
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1136
     *      enough to hold the two longs.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1137
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1138
    private void authWriteLengths(long aLen, long dLen, byte[] buf) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1139
        asLongLittleEndian.set(buf, 0, aLen);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1140
        asLongLittleEndian.set(buf, Long.BYTES, dLen);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1141
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1142
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1143
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1144
     * Interface for the underlying processing engines for ChaCha20
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1145
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1146
    interface ChaChaEngine {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1147
        /**
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1148
         * Size an output buffer based on the input and where applicable
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1149
         * the current state of the engine in a multipart operation.
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1150
         *
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1151
         * @param inLength the input length.
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1152
         * @param isFinal true if this is invoked from a doFinal call.
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1153
         *
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1154
         * @return the recommended size for the output buffer.
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1155
         */
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1156
        int getOutputSize(int inLength, boolean isFinal);
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1157
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1158
        /**
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1159
         * Perform a multi-part update for ChaCha20.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1160
         *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1161
         * @param in the input data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1162
         * @param inOff the offset into the input.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1163
         * @param inLen the length of the data to process.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1164
         * @param out the output buffer.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1165
         * @param outOff the offset at which to write the output data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1166
         *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1167
         * @return the number of output bytes written.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1168
         *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1169
         * @throws ShortBufferException if the output buffer does not
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1170
         *      provide enough space.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1171
         * @throws KeyException if the counter value has been exhausted.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1172
         */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1173
        int doUpdate(byte[] in, int inOff, int inLen, byte[] out, int outOff)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1174
                throws ShortBufferException, KeyException;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1175
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1176
        /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1177
         * Finalize a multi-part or single-part ChaCha20 operation.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1178
         *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1179
         * @param in the input data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1180
         * @param inOff the offset into the input.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1181
         * @param inLen the length of the data to process.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1182
         * @param out the output buffer.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1183
         * @param outOff the offset at which to write the output data.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1184
         *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1185
         * @return the number of output bytes written.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1186
         *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1187
         * @throws ShortBufferException if the output buffer does not
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1188
         *      provide enough space.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1189
         * @throws AEADBadTagException if in decryption mode the provided
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1190
         *      tag and calculated tag do not match.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1191
         * @throws KeyException if the counter value has been exhausted.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1192
         */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1193
        int doFinal(byte[] in, int inOff, int inLen, byte[] out, int outOff)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1194
                throws ShortBufferException, AEADBadTagException, KeyException;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1195
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1196
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1197
    private final class EngineStreamOnly implements ChaChaEngine {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1198
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1199
        private EngineStreamOnly () { }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1200
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1201
        @Override
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1202
        public int getOutputSize(int inLength, boolean isFinal) {
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1203
            // The isFinal parameter is not relevant in this kind of engine
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1204
            return inLength;
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1205
        }
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1206
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1207
        @Override
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1208
        public int doUpdate(byte[] in, int inOff, int inLen, byte[] out,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1209
                int outOff) throws ShortBufferException, KeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1210
            if (initialized) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1211
               try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1212
                    if (out != null) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1213
                        Objects.checkFromIndexSize(outOff, inLen, out.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1214
                    } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1215
                        throw new ShortBufferException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1216
                                "Output buffer too small");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1217
                    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1218
                } catch (IndexOutOfBoundsException iobe) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1219
                    throw new ShortBufferException("Output buffer too small");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1220
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1221
                if (in != null) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1222
                    Objects.checkFromIndexSize(inOff, inLen, in.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1223
                    chaCha20Transform(in, inOff, inLen, out, outOff);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1224
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1225
                return inLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1226
            } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1227
                throw new IllegalStateException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1228
                        "Must use either a different key or iv.");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1229
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1230
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1231
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1232
        @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1233
        public int doFinal(byte[] in, int inOff, int inLen, byte[] out,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1234
                int outOff) throws ShortBufferException, KeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1235
            return doUpdate(in, inOff, inLen, out, outOff);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1236
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1237
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1238
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1239
    private final class EngineAEADEnc implements ChaChaEngine {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1240
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1241
        @Override
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1242
        public int getOutputSize(int inLength, boolean isFinal) {
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1243
            return (isFinal ? Math.addExact(inLength, TAG_LENGTH) : inLength);
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1244
        }
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1245
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1246
        private EngineAEADEnc() throws InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1247
            initAuthenticator();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1248
            counter = 1;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1249
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1250
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1251
        @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1252
        public int doUpdate(byte[] in, int inOff, int inLen, byte[] out,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1253
                int outOff) throws ShortBufferException, KeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1254
            if (initialized) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1255
                // If this is the first update since AAD updates, signal that
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1256
                // we're done processing AAD info and pad the AAD to a multiple
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1257
                // of 16 bytes.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1258
                if (!aadDone) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1259
                    authPad16(aadLen);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1260
                    aadDone = true;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1261
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1262
                try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1263
                    if (out != null) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1264
                        Objects.checkFromIndexSize(outOff, inLen, out.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1265
                    } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1266
                        throw new ShortBufferException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1267
                                "Output buffer too small");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1268
                    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1269
                } catch (IndexOutOfBoundsException iobe) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1270
                    throw new ShortBufferException("Output buffer too small");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1271
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1272
                if (in != null) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1273
                    Objects.checkFromIndexSize(inOff, inLen, in.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1274
                    chaCha20Transform(in, inOff, inLen, out, outOff);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1275
                    dataLen += authUpdate(out, outOff, inLen);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1276
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1277
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1278
                return inLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1279
            } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1280
                throw new IllegalStateException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1281
                        "Must use either a different key or iv.");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1282
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1283
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1284
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1285
        @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1286
        public int doFinal(byte[] in, int inOff, int inLen, byte[] out,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1287
                int outOff) throws ShortBufferException, KeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1288
            // Make sure we have enough room for the remaining data (if any)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1289
            // and the tag.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1290
            if ((inLen + TAG_LENGTH) > (out.length - outOff)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1291
                throw new ShortBufferException("Output buffer too small");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1292
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1293
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1294
            doUpdate(in, inOff, inLen, out, outOff);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1295
            authFinalizeData(null, 0, 0, out, outOff + inLen);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1296
            aadDone = false;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1297
            return inLen + TAG_LENGTH;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1298
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1299
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1300
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1301
    private final class EngineAEADDec implements ChaChaEngine {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1302
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1303
        private final ByteArrayOutputStream cipherBuf;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1304
        private final byte[] tag;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1305
57791
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1306
        @Override
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1307
        public int getOutputSize(int inLen, boolean isFinal) {
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1308
            // If we are performing a decrypt-update we should always return
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1309
            // zero length since we cannot return any data until the tag has
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1310
            // been consumed and verified.  CipherSpi.engineGetOutputSize will
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1311
            // always set isFinal to true to get the required output buffer
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1312
            // size.
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1313
            return (isFinal ?
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1314
                    Integer.max(Math.addExact((inLen - TAG_LENGTH),
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1315
                            cipherBuf.size()), 0) : 0);
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1316
        }
34bbd91b1522 8224997: ChaCha20-Poly1305 TLS cipher suite decryption throws ShortBufferException
jnimeh
parents: 55710
diff changeset
  1317
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1318
        private EngineAEADDec() throws InvalidKeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1319
            initAuthenticator();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1320
            counter = 1;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1321
            cipherBuf = new ByteArrayOutputStream(CIPHERBUF_BASE);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1322
            tag = new byte[TAG_LENGTH];
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1323
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1324
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1325
        @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1326
        public int doUpdate(byte[] in, int inOff, int inLen, byte[] out,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1327
                int outOff) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1328
            if (initialized) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1329
                // If this is the first update since AAD updates, signal that
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1330
                // we're done processing AAD info and pad the AAD to a multiple
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1331
                // of 16 bytes.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1332
                if (!aadDone) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1333
                    authPad16(aadLen);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1334
                    aadDone = true;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1335
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1336
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1337
                if (in != null) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1338
                    Objects.checkFromIndexSize(inOff, inLen, in.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1339
                    cipherBuf.write(in, inOff, inLen);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1340
                }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1341
            } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1342
                throw new IllegalStateException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1343
                        "Must use either a different key or iv.");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1344
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1345
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1346
            return 0;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1347
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1348
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1349
        @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1350
        public int doFinal(byte[] in, int inOff, int inLen, byte[] out,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1351
                int outOff) throws ShortBufferException, AEADBadTagException,
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1352
                KeyException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1353
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1354
            byte[] ctPlusTag;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1355
            int ctPlusTagLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1356
            if (cipherBuf.size() == 0 && inOff == 0) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1357
                // No previous data has been seen before doFinal, so we do
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1358
                // not need to hold any ciphertext in a buffer.  We can
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1359
                // process it directly from the "in" parameter.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1360
                doUpdate(null, inOff, inLen, out, outOff);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1361
                ctPlusTag = in;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1362
                ctPlusTagLen = inLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1363
            } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1364
                doUpdate(in, inOff, inLen, out, outOff);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1365
                ctPlusTag = cipherBuf.toByteArray();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1366
                ctPlusTagLen = ctPlusTag.length;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1367
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1368
            cipherBuf.reset();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1369
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1370
            // There must at least be a tag length's worth of ciphertext
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1371
            // data in the buffered input.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1372
            if (ctPlusTagLen < TAG_LENGTH) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1373
                throw new AEADBadTagException("Input too short - need tag");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1374
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1375
            int ctLen = ctPlusTagLen - TAG_LENGTH;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1376
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1377
            // Make sure we will have enough room for the output buffer
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1378
            try {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1379
                Objects.checkFromIndexSize(outOff, ctLen, out.length);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1380
            } catch (IndexOutOfBoundsException ioobe) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1381
                throw new ShortBufferException("Output buffer too small");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1382
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1383
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1384
            // Calculate and compare the tag.  Only do the decryption
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1385
            // if and only if the tag matches.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1386
            authFinalizeData(ctPlusTag, 0, ctLen, tag, 0);
55710
18130ed28231 8221345: Better Poly1305 support
jnimeh
parents: 50323
diff changeset
  1387
            long tagCompare = ((long)asLongView.get(ctPlusTag, ctLen) ^
18130ed28231 8221345: Better Poly1305 support
jnimeh
parents: 50323
diff changeset
  1388
                    (long)asLongView.get(tag, 0)) |
18130ed28231 8221345: Better Poly1305 support
jnimeh
parents: 50323
diff changeset
  1389
                    ((long)asLongView.get(ctPlusTag, ctLen + Long.BYTES) ^
18130ed28231 8221345: Better Poly1305 support
jnimeh
parents: 50323
diff changeset
  1390
                    (long)asLongView.get(tag, Long.BYTES));
18130ed28231 8221345: Better Poly1305 support
jnimeh
parents: 50323
diff changeset
  1391
            if (tagCompare != 0) {
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1392
                throw new AEADBadTagException("Tag mismatch");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1393
            }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1394
            chaCha20Transform(ctPlusTag, 0, ctLen, out, outOff);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1395
            aadDone = false;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1396
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1397
            return ctLen;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1398
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1399
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1400
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1401
    public static final class ChaCha20Only extends ChaCha20Cipher {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1402
        public ChaCha20Only() {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1403
            mode = MODE_NONE;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1404
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1405
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1406
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1407
    public static final class ChaCha20Poly1305 extends ChaCha20Cipher {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1408
        public ChaCha20Poly1305() {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1409
            mode = MODE_AEAD;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1410
            authAlgName = "Poly1305";
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1411
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1412
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
  1413
}