src/java.base/share/classes/com/sun/crypto/provider/ChaCha20Poly1305Parameters.java
author mbalao
Tue, 12 Nov 2019 00:30:55 -0300
changeset 59158 438337c846fb
parent 50323 25d711fca885
permissions -rw-r--r--
8233404: System property to set the number of PBE iterations in JCEKS keystores Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50323
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     1
/*
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
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.IOException;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    29
import java.security.AlgorithmParametersSpi;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    30
import java.security.spec.AlgorithmParameterSpec;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    31
import java.security.spec.InvalidParameterSpecException;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    32
import javax.crypto.spec.IvParameterSpec;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    33
import sun.security.util.*;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    34
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    35
/**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    36
 * This class implements the parameter set used with the ChaCha20-Poly1305
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    37
 * algorithm.  The parameter definition comes from
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    38
 * <a href="https://tools.ietf.org/html/rfc8103"><i>RFC 8103</i></a>
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    39
 * and is defined according to the following ASN.1:
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    40
 *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    41
 * <pre>
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    42
 * id-alg-AEADChaCha20Poly1305 OBJECT IDENTIFIER ::=
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    43
          { iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    44
            pkcs9(9) smime(16) alg(3) 18 }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    45
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    46
 * AEADChaCha20Poly1305Nonce ::= OCTET STRING (SIZE(12))
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    47
 * </pre>
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    48
 *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    49
 * The AlgorithmParameters may be instantiated either by its name
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    50
 * ("ChaCha20-Poly1305") or via its OID (1.2.840.113549.1.9.16.3.18)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    51
 */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    52
public final class ChaCha20Poly1305Parameters extends AlgorithmParametersSpi {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    53
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    54
    private static final String DEFAULT_FMT = "ASN.1";
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    55
    private byte[] nonce;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    56
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    57
    public ChaCha20Poly1305Parameters() {}
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    58
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    59
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    60
     * Initialize the ChaCha20Poly1305Parameters using an IvParameterSpec.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    61
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    62
     * @param paramSpec the {@code IvParameterSpec} used to configure
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    63
     *      this object.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    64
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    65
     * @throws InvalidParameterSpecException if an object of a type other
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    66
     *      than {@code IvParameterSpec} is used.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    67
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    68
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    69
    protected void engineInit(AlgorithmParameterSpec paramSpec)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    70
        throws InvalidParameterSpecException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    71
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    72
        if (!(paramSpec instanceof IvParameterSpec)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    73
            throw new InvalidParameterSpecException
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    74
                ("Inappropriate parameter specification");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    75
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    76
        IvParameterSpec ivps = (IvParameterSpec)paramSpec;
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    77
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    78
        // Obtain the nonce
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    79
        nonce = ivps.getIV();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    80
        if (nonce.length != 12) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    81
            throw new InvalidParameterSpecException("ChaCha20-Poly1305 nonce" +
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    82
                    " must be 12 bytes in length");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    83
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    84
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    85
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    86
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    87
     * Initialize the ChaCha20Poly1305Parameters from a DER encoded
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    88
     * parameter block.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    89
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    90
     * @param encoded the DER encoding of the nonce as an OCTET STRING.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    91
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    92
     * @throws IOException if the encoded nonce is not 12 bytes long or a DER
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    93
     *      decoding error occurs.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    94
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    95
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    96
    protected void engineInit(byte[] encoded) throws IOException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    97
        DerValue val = new DerValue(encoded);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    98
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
    99
        // Get the nonce value
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   100
        nonce = val.getOctetString();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   101
        if (nonce.length != 12) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   102
           throw new IOException(
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   103
                   "ChaCha20-Poly1305 nonce must be 12 bytes in length");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   104
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   105
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   106
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   107
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   108
     * Initialize the ChaCha20Poly1305Parameters from a DER encoded
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   109
     * parameter block.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   110
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   111
     * @param encoded the DER encoding of the nonce and initial block counter.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   112
     * @param decodingMethod the decoding method.  The only currently accepted
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   113
     *      value is "ASN.1"
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   114
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   115
     * @throws IOException if the encoded nonce is not 12 bytes long, a DER
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   116
     *      decoding error occurs, or an unsupported decoding method is
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   117
     *      provided.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   118
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   119
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   120
    protected void engineInit(byte[] encoded, String decodingMethod)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   121
            throws IOException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   122
        if (decodingMethod == null ||
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   123
                decodingMethod.equalsIgnoreCase(DEFAULT_FMT)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   124
            engineInit(encoded);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   125
        } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   126
            throw new IOException("Unsupported parameter format: " +
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   127
                    decodingMethod);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   128
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   129
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   130
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   131
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   132
     * Return an IvParameterSpec with the same parameters as those
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   133
     * held in this object.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   134
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   135
     * @param paramSpec the class name of the spec.  In this case it should
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   136
     *      be {@code IvParameterSpec.class}.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   137
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   138
     * @return a {@code IvParameterSpec} object containing the nonce
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   139
     *      value held in this object.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   140
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   141
     * @throws InvalidParameterSpecException if a class other than
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   142
     *      {@code IvParameterSpec.class} was specified in the paramSpec
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   143
     *      parameter.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   144
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   145
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   146
    protected <T extends AlgorithmParameterSpec>
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   147
            T engineGetParameterSpec(Class<T> paramSpec)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   148
        throws InvalidParameterSpecException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   149
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   150
        if (IvParameterSpec.class.isAssignableFrom(paramSpec)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   151
            return paramSpec.cast(new IvParameterSpec(nonce));
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   152
        } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   153
            throw new InvalidParameterSpecException
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   154
                ("Inappropriate parameter specification");
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
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   158
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   159
     * Return the encoded parameters in ASN.1 form.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   160
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   161
     * @return a byte array containing the DER-encoding for the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   162
     *      ChaCha20-Poly1305 parameters.  This will be the nonce
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   163
     *      encoded as a DER OCTET STRING.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   164
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   165
     * @throws IOException if any DER encoding error occurs.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   166
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   167
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   168
    protected byte[] engineGetEncoded() throws IOException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   169
        DerOutputStream out = new DerOutputStream();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   170
        out.write(DerValue.tag_OctetString, nonce);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   171
        return out.toByteArray();
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
     * Return the encoded parameters in ASN.1 form.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   176
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   177
     * @param encodingMethod the encoding method to be used.  This parameter
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   178
     *      must be "ASN.1" as it is the only currently supported encoding
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   179
     *      format.  If the parameter is {@code null} then the default
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   180
     *      encoding format will be used.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   181
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   182
     * @return a byte array containing the DER-encoding for the
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   183
     *      ChaCha20-Poly1305 parameters.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   184
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   185
     * @throws IOException if any DER encoding error occurs or an unsupported
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   186
     *      encoding method is provided.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   187
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   188
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   189
    protected byte[] engineGetEncoded(String encodingMethod)
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   190
        throws IOException {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   191
        if (encodingMethod == null ||
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   192
                encodingMethod.equalsIgnoreCase(DEFAULT_FMT)) {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   193
            return engineGetEncoded();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   194
        } else {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   195
            throw new IOException("Unsupported encoding format: " +
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   196
                    encodingMethod);
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   197
        }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   198
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   199
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   200
    /**
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   201
     * Creates a formatted string describing the parameters.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   202
     *
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   203
     * @return a string representation of the ChaCha20 parameters.
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   204
     */
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   205
    @Override
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   206
    protected String engineToString() {
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   207
        String LINE_SEP = System.lineSeparator();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   208
        HexDumpEncoder encoder = new HexDumpEncoder();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   209
        StringBuilder sb = new StringBuilder(LINE_SEP + "nonce:" +
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   210
                LINE_SEP + "[" + encoder.encodeBuffer(nonce) + "]");
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   211
        return sb.toString();
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   212
    }
25d711fca885 8153029: ChaCha20 Cipher Implementation
jnimeh
parents:
diff changeset
   213
}