Path: blob/master/src/java.base/share/classes/sun/security/rsa/MGF1.java
41159 views
/*1* Copyright (c) 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*/24package sun.security.rsa;2526import java.security.*;2728/**29* This class implements the MGF1 mask generation function defined in PKCS#130* v2.2 B.2.1 (https://tools.ietf.org/html/rfc8017#appendix-B.2.1). A mask31* generation function takes an octet string of variable length and a32* desired output length as input and outputs an octet string of the33* desired length. MGF1 is a mask generation function based on a hash34* function, i.e. message digest algorithm.35*36* @since 1137*/38public final class MGF1 {3940private final MessageDigest md;4142/**43* Construct an instance of MGF1 based on the specified digest algorithm.44*/45MGF1(String mdAlgo) throws NoSuchAlgorithmException {46this.md = MessageDigest.getInstance(mdAlgo);47}4849/**50* Using the specified seed bytes, generate the mask, xor the mask51* with the specified output buffer and store the result into the52* output buffer (essentially replaced in place).53*54* @param seed the buffer holding the seed bytes55* @param seedOfs the index of the seed bytes56* @param seedLen the length of the seed bytes to be used by MGF157* @param maskLen the intended length of the generated mask58* @param out the output buffer holding the mask59* @param outOfs the index of the output buffer for the mask60*/61void generateAndXor(byte[] seed, int seedOfs, int seedLen, int maskLen,62byte[] out, int outOfs) throws RuntimeException {63byte[] C = new byte[4]; // 32 bit counter64byte[] digest = new byte[md.getDigestLength()];65while (maskLen > 0) {66md.update(seed, seedOfs, seedLen);67md.update(C);68try {69md.digest(digest, 0, digest.length);70} catch (DigestException e) {71// should never happen72throw new RuntimeException(e.toString());73}74for (int i = 0; (i < digest.length) && (maskLen > 0); maskLen--) {75out[outOfs++] ^= digest[i++];76}77if (maskLen > 0) {78// increment counter79for (int i = C.length - 1; (++C[i] == 0) && (i > 0); i--) {80// empty81}82}83}84}8586/**87* Returns the name of this MGF1 instance, i.e. "MGF1" followed by the88* digest algorithm it based on.89*/90String getName() {91return "MGF1" + md.getAlgorithm();92}93}949596