1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package sun.security.rsa;
  26 
  27 import java.security.*;
  28 
  29 /**
  30  * This class implements the MGF1 mask generation function defined in PKCS#1
  31  * v2.2 B.2.1. A mask generation function takes an octet string of variable
  32  * length and a desired output length as input and outputs an octet string of
  33  * the desired length. MGF1 is a mask generation function based on a hash
  34  * function, i.e. message digest algorithm.
  35  *
  36  * @since   11
  37  */
  38 public final class MGF1 {
  39 
  40     private final MessageDigest md;
  41 
  42     /**
  43      * Construct an instance of MGF1 based on the specified digest algorithm.
  44      */
  45     MGF1(String mdAlgo) throws NoSuchAlgorithmException {
  46         this.md = MessageDigest.getInstance(mdAlgo);
  47     }
  48 
  49     /**
  50      * Using the specified seed bytes, generate the mask, xor the mask
  51      * with the specified output buffer and store the result into the
  52      * output buffer (essentially replaced in place).
  53      *
  54      * @param seed the buffer holding the seed bytes
  55      * @param seedOfs the index of the seed bytes
  56      * @param seedLen the length of the seed bytes to be used by MGF1
  57      * @param maskLen the intended length of the generated mask
  58      * @param out the output buffer holding the mask
  59      * @param outOfs the index of the output buffer for the mask
  60      */
  61     void generateAndXor(byte[] seed, int seedOfs, int seedLen, int maskLen,
  62             byte[] out, int outOfs) throws RuntimeException {
  63         byte[] C = new byte[4]; // 32 bit counter
  64         byte[] digest = new byte[md.getDigestLength()];
  65         while (maskLen > 0) {
  66             md.update(seed, seedOfs, seedLen);
  67             md.update(C);
  68             try {
  69                 md.digest(digest, 0, digest.length);
  70             } catch (DigestException e) {
  71                 // should never happen
  72                 throw new RuntimeException(e.toString());
  73             }
  74             for (int i = 0; (i < digest.length) && (maskLen > 0); maskLen--) {
  75                 out[outOfs++] ^= digest[i++];
  76             }
  77             if (maskLen > 0) {
  78                 // increment counter
  79                 for (int i = C.length - 1; (++C[i] == 0) && (i > 0); i--) {
  80                     // empty
  81                 }
  82             }
  83         }
  84     }
  85 
  86     /**
  87      * Returns the name of this MGF1 instance, i.e. "MGF1" followed by the
  88      * digest algorithm it based on.
  89      */
  90     String getName() {
  91         return "MGF1" + md.getAlgorithm();
  92     }
  93 }