src/java.base/share/classes/javax/crypto/spec/OAEPParameterSpec.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.crypto.spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.spec.MGF1ParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class specifies the set of parameters used with OAEP Padding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * as defined in the
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    35
 * <a href="https://tools.ietf.org/rfc/rfc8017.txt">PKCS#1 v2.2</a> standard.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Its ASN.1 definition in PKCS#1 standard is described below:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * RSAES-OAEP-params ::= SEQUENCE {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    40
 *   hashAlgorithm      [0] HashAlgorithm     DEFAULT sha1,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    41
 *   maskGenAlgorithm   [1] MaskGenAlgorithm  DEFAULT mgf1SHA1,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    42
 *   pSourceAlgorithm   [2] PSourceAlgorithm  DEFAULT pSpecifiedEmpty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <pre>
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    47
 * HashAlgorithm ::= AlgorithmIdentifier {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    48
 *   {OAEP-PSSDigestAlgorithms}
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    49
 * }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    50
 * MaskGenAlgorithm ::= AlgorithmIdentifier { {PKCS1MGFAlgorithms} }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    51
 * PSourceAlgorithm ::= AlgorithmIdentifier {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    52
 *   {PKCS1PSourceAlgorithms}
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    53
 * }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    54
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * OAEP-PSSDigestAlgorithms    ALGORITHM-IDENTIFIER ::= {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    56
 *   { OID id-sha1       PARAMETERS NULL }|
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    57
 *   { OID id-sha224     PARAMETERS NULL }|
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    58
 *   { OID id-sha256     PARAMETERS NULL }|
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    59
 *   { OID id-sha384     PARAMETERS NULL }|
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    60
 *   { OID id-sha512     PARAMETERS NULL }|
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    61
 *   { OID id-sha512-224 PARAMETERS NULL }|
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    62
 *   { OID id-sha512-256 PARAMETERS NULL },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   ...  -- Allows for future expansion --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * PKCS1MGFAlgorithms    ALGORITHM-IDENTIFIER ::= {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    66
 *   { OID id-mgf1 PARAMETERS HashAlgorithm },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   ...  -- Allows for future expansion --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * PKCS1PSourceAlgorithms    ALGORITHM-IDENTIFIER ::= {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    70
 *   { OID id-pSpecified PARAMETERS EncodingParameters },
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   ...  -- Allows for future expansion --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    73
 * EncodingParameters ::= OCTET STRING(SIZE(0..MAX))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>Note: the OAEPParameterSpec.DEFAULT uses the following:
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    76
 * <pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     message digest  -- "SHA-1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     mask generation function (mgf) -- "MGF1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     parameters for mgf -- MGF1ParameterSpec.SHA1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     source of encoding input -- PSource.PSpecified.DEFAULT
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    81
 * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see java.security.spec.MGF1ParameterSpec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see PSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @author Valerie Peng
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
public class OAEPParameterSpec implements AlgorithmParameterSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private String mdName = "SHA-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private String mgfName = "MGF1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private PSource pSrc = PSource.PSpecified.DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The OAEP parameter set with all default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final OAEPParameterSpec DEFAULT = new OAEPParameterSpec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Constructs a parameter set for OAEP padding as defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * the PKCS #1 standard using the default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private OAEPParameterSpec() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Constructs a parameter set for OAEP padding as defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * the PKCS #1 standard using the specified message digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * algorithm <code>mdName</code>, mask generation function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * algorithm <code>mgfName</code>, parameters for the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * generation function <code>mgfSpec</code>, and source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * the encoding input P <code>pSrc</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param mdName the algorithm name for the message digest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param mgfName the algorithm name for the mask generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param mgfSpec the parameters for the mask generation function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * If null is specified, null will be returned by getMGFParameters().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param pSrc the source of the encoding input P.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @exception NullPointerException if <code>mdName</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <code>mgfName</code>, or <code>pSrc</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public OAEPParameterSpec(String mdName, String mgfName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                             AlgorithmParameterSpec mgfSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                             PSource pSrc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (mdName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new NullPointerException("digest algorithm is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (mgfName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new NullPointerException("mask generation function " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                           "algorithm is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (pSrc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new NullPointerException("source of the encoding input " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                           "is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.mdName =  mdName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        this.mgfName =  mgfName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this.mgfSpec =  mgfSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        this.pSrc =  pSrc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Returns the message digest algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return the message digest algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public String getDigestAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return mdName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns the mask generation function algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @return the mask generation function algorithm name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public String getMGFAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return mgfName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns the parameters for the mask generation function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the parameters for the mask generation function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public AlgorithmParameterSpec getMGFParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return mgfSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns the source of encoding input P.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return the source of encoding input P.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public PSource getPSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return pSrc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}