Path: blob/master/src/java.base/share/classes/javax/crypto/spec/OAEPParameterSpec.java
41159 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto.spec;2627import java.math.BigInteger;28import java.security.spec.AlgorithmParameterSpec;29import java.security.spec.MGF1ParameterSpec;3031/**32* This class specifies the set of parameters used with OAEP Padding,33* as defined in the34* <a href="https://tools.ietf.org/rfc/rfc8017.txt">PKCS#1 v2.2</a> standard.35*36* Its ASN.1 definition in PKCS#1 standard is described below:37* <pre>38* RSAES-OAEP-params ::= SEQUENCE {39* hashAlgorithm [0] HashAlgorithm DEFAULT sha1,40* maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1,41* pSourceAlgorithm [2] PSourceAlgorithm DEFAULT pSpecifiedEmpty42* }43* </pre>44* where45* <pre>46* HashAlgorithm ::= AlgorithmIdentifier {47* {OAEP-PSSDigestAlgorithms}48* }49* MaskGenAlgorithm ::= AlgorithmIdentifier { {PKCS1MGFAlgorithms} }50* PSourceAlgorithm ::= AlgorithmIdentifier {51* {PKCS1PSourceAlgorithms}52* }53*54* OAEP-PSSDigestAlgorithms ALGORITHM-IDENTIFIER ::= {55* { OID id-sha1 PARAMETERS NULL }|56* { OID id-sha224 PARAMETERS NULL }|57* { OID id-sha256 PARAMETERS NULL }|58* { OID id-sha384 PARAMETERS NULL }|59* { OID id-sha512 PARAMETERS NULL }|60* { OID id-sha512-224 PARAMETERS NULL }|61* { OID id-sha512-256 PARAMETERS NULL },62* ... -- Allows for future expansion --63* }64* PKCS1MGFAlgorithms ALGORITHM-IDENTIFIER ::= {65* { OID id-mgf1 PARAMETERS HashAlgorithm },66* ... -- Allows for future expansion --67* }68* PKCS1PSourceAlgorithms ALGORITHM-IDENTIFIER ::= {69* { OID id-pSpecified PARAMETERS EncodingParameters },70* ... -- Allows for future expansion --71* }72* EncodingParameters ::= OCTET STRING(SIZE(0..MAX))73* </pre>74* <p>Note: the OAEPParameterSpec.DEFAULT uses the following:75* <pre>76* message digest -- "SHA-1"77* mask generation function (mgf) -- "MGF1"78* parameters for mgf -- MGF1ParameterSpec.SHA179* source of encoding input -- PSource.PSpecified.DEFAULT80* </pre>81*82* @see java.security.spec.MGF1ParameterSpec83* @see PSource84*85* @author Valerie Peng86*87* @since 1.588*/89public class OAEPParameterSpec implements AlgorithmParameterSpec {9091private String mdName = "SHA-1";92private String mgfName = "MGF1";93private AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;94private PSource pSrc = PSource.PSpecified.DEFAULT;9596/**97* The OAEP parameter set with all default values.98*/99public static final OAEPParameterSpec DEFAULT = new OAEPParameterSpec();100101/**102* Constructs a parameter set for OAEP padding as defined in103* the PKCS #1 standard using the default values.104*/105private OAEPParameterSpec() {106}107108/**109* Constructs a parameter set for OAEP padding as defined in110* the PKCS #1 standard using the specified message digest111* algorithm <code>mdName</code>, mask generation function112* algorithm <code>mgfName</code>, parameters for the mask113* generation function <code>mgfSpec</code>, and source of114* the encoding input P <code>pSrc</code>.115*116* @param mdName the algorithm name for the message digest.117* @param mgfName the algorithm name for the mask generation118* function.119* @param mgfSpec the parameters for the mask generation function.120* If null is specified, null will be returned by getMGFParameters().121* @param pSrc the source of the encoding input P.122* @exception NullPointerException if <code>mdName</code>,123* <code>mgfName</code>, or <code>pSrc</code> is null.124*/125public OAEPParameterSpec(String mdName, String mgfName,126AlgorithmParameterSpec mgfSpec,127PSource pSrc) {128if (mdName == null) {129throw new NullPointerException("digest algorithm is null");130}131if (mgfName == null) {132throw new NullPointerException("mask generation function " +133"algorithm is null");134}135if (pSrc == null) {136throw new NullPointerException("source of the encoding input " +137"is null");138}139this.mdName = mdName;140this.mgfName = mgfName;141this.mgfSpec = mgfSpec;142this.pSrc = pSrc;143}144145/**146* Returns the message digest algorithm name.147*148* @return the message digest algorithm name.149*/150public String getDigestAlgorithm() {151return mdName;152}153154/**155* Returns the mask generation function algorithm name.156*157* @return the mask generation function algorithm name.158*/159public String getMGFAlgorithm() {160return mgfName;161}162163/**164* Returns the parameters for the mask generation function.165*166* @return the parameters for the mask generation function.167*/168public AlgorithmParameterSpec getMGFParameters() {169return mgfSpec;170}171172/**173* Returns the source of encoding input P.174*175* @return the source of encoding input P.176*/177public PSource getPSource() {178return pSrc;179}180}181182183