/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* RSA padding and unpadding.
*
* Format of PKCS#1 v1.5 padding is:
* 0x00 | BT | PS...PS | 0x00 | data...data
* where BT is the blocktype (1 or 2). The length of the entire string
* must be the same as the size of the modulus (i.e. 128 byte for a 1024 bit
* key). Per spec, the padding string must be at least 8 bytes long. That
* leaves up to (length of key in bytes) - 11 bytes for the data.
*
* OAEP padding is a bit more complicated and has a number of options.
* We support:
* . arbitrary hash functions ('Hash' in the specification), MessageDigest
* implementation must be available
* . MGF1 as the mask generation function
* . the empty string as the default value for label L and whatever
* specified in javax.crypto.spec.OAEPParameterSpec
*
* Note: RSA keys should be at least 512 bits long
*
* @since 1.5
* @author Andreas Sterbenz
*/
public final class RSAPadding {
// NOTE: the constants below are embedded in the JCE RSACipher class
// file. Do not change without coordinating the update
// PKCS#1 v1.5 padding, blocktype 1 (signing)
// PKCS#1 v1.5 padding, blocktype 2 (encryption)
// nopadding. Does not do anything, but allows simpler RSACipher code
// PKCS#1 v2.1 OAEP padding
// type, one of PAD_*
private final int type;
// size of the padded block (i.e. size of the modulus)
private final int paddedSize;
// PRNG used to generate padding bytes (PAD_BLOCKTYPE_2, PAD_OAEP_MGF1)
// maximum size of the data
private final int maxDataSize;
// OAEP: main messagedigest
// OAEP: message digest for MGF1
// OAEP: value of digest of data (user-supplied or zero-length) using md
private byte[] lHash;
/**
* Get a RSAPadding instance of the specified type.
* Keys used with this padding must be paddedSize bytes long.
*/
}
/**
* Get a RSAPadding instance of the specified type.
* Keys used with this padding must be paddedSize bytes long.
*/
}
/**
* Get a RSAPadding instance of the specified type, which must be
* OAEP. Keys used with this padding must be paddedSize bytes long.
*/
}
// internal constructor
this.paddedSize = paddedSize;
if (paddedSize < 64) {
// sanity check, already verified in RSASignature/RSACipher
throw new InvalidKeyException("Padded size must be at least 64");
}
switch (type) {
case PAD_BLOCKTYPE_1:
case PAD_BLOCKTYPE_2:
break;
case PAD_NONE:
break;
case PAD_OAEP_MGF1:
byte[] digestInput = null;
try {
throw new InvalidAlgorithmParameterException
("Unsupported MGF algo: " + mgfName);
}
throw new InvalidAlgorithmParameterException
("Unsupported pSource algo: " + pSrcAlgo);
}
}
} catch (NoSuchAlgorithmException e) {
throw new InvalidKeyException
}
if (maxDataSize <= 0) {
throw new InvalidKeyException
("Key is too short for encryption using OAEPPadding" +
}
break;
default:
}
}
// cache of hashes of zero length data
/**
* Return the value of the digest using the specified message digest
* <code>md</code> and the digest input <code>digestInput</code>.
* if <code>digestInput</code> is null or 0-length, zero length
* is used to generate the initial digest.
* Note: the md object must be in reset state
*/
byte[] digestInput) {
}
} else {
}
return result;
}
/**
* Return the maximum size of the plaintext data that can be processed using
* this object.
*/
public int getMaxDataSize() {
return maxDataSize;
}
/**
* Pad the data and return the padded block.
*/
throws BadPaddingException {
}
/**
* Pad the data and return the padded block.
*/
throw new BadPaddingException("Data must be shorter than "
}
switch (type) {
case PAD_NONE:
return data;
case PAD_BLOCKTYPE_1:
case PAD_BLOCKTYPE_2:
case PAD_OAEP_MGF1:
default:
throw new AssertionError();
}
}
/**
* Unpad the padded block and return the data.
*/
throws BadPaddingException {
}
/**
* Unpad the padded block and return the data.
*/
}
switch (type) {
case PAD_NONE:
return padded;
case PAD_BLOCKTYPE_1:
case PAD_BLOCKTYPE_2:
case PAD_OAEP_MGF1:
default:
throw new AssertionError();
}
}
/**
* PKCS#1 v1.5 padding (blocktype 1 and 2).
*/
byte[] padded = new byte[paddedSize];
int k = 0;
padded[k++] = 0;
if (type == PAD_BLOCKTYPE_1) {
// blocktype 1: all padding bytes are 0xff
while (psSize-- > 0) {
padded[k++] = (byte)0xff;
}
} else {
// blocktype 2: padding bytes are random non-zero bytes
}
// generate non-zero padding bytes
// use a buffer to reduce calls to SecureRandom
byte[] r = new byte[64];
int i = -1;
while (psSize-- > 0) {
int b;
do {
if (i < 0) {
i = r.length - 1;
}
b = r[i--] & 0xff;
} while (b == 0);
padded[k++] = (byte)b;
}
}
return padded;
}
/**
* PKCS#1 v1.5 unpadding (blocktype 1 and 2).
*/
int k = 0;
if (padded[k++] != 0) {
throw new BadPaddingException("Data must start with zero");
}
}
while (true) {
int b = padded[k++] & 0xff;
if (b == 0) {
break;
}
throw new BadPaddingException("Padding string not terminated");
}
throw new BadPaddingException("Padding byte not 0xff: " + b);
}
}
if (n > maxDataSize) {
throw new BadPaddingException("Padding string too short");
}
byte[] data = new byte[n];
return data;
}
/**
* PKCS#1 v2.0 OAEP padding (MGF1).
* Paragraph references refer to PKCS#1 v2.1 (June 14, 2002)
*/
}
// 2.d: generate a random octet string seed of length hLen
// if necessary
// buffer for encoded message EM
byte[] EM = new byte[paddedSize];
// start and length of seed (as index into EM)
int seedStart = 1;
// copy seed into EM
// start and length of data block DB in EM
// we place it inside of EM to reduce copying
// start of message M in EM
// build DB
// 2.b: Concatenate lHash, PS, a single octet with hexadecimal value
// 0x01, and the message M to form a data block DB of length
// k - hLen -1 octets as DB = lHash || PS || 0x01 || M
// (note that PS is all zeros)
// produce maskedDB
// produce maskSeed
return EM;
}
/**
* PKCS#1 v2.1 OAEP unpadding (MGF1).
*/
throw new BadPaddingException("Data must start with zero");
}
int seedStart = 1;
// verify lHash == lHash'
for (int i = 0; i < hLen; i++) {
throw new BadPaddingException("lHash mismatch");
}
}
// skip over padding (0x00 bytes)
while (EM[i] == 0) {
i++;
throw new BadPaddingException("Padding string not terminated");
}
}
if (EM[i++] != 1) {
throw new BadPaddingException
("Padding string not terminated by 0x01 byte");
}
byte[] m = new byte[mLen];
return m;
}
/**
* Compute MGF1 using mgfMD as the message digest.
* Note that we combine MGF1 with the XOR operation to reduce data
* copying.
*
* We generate maskLen bytes of MGF1 from the seed and XOR it into
* out[] starting at outOfs;
*/
byte[] C = new byte[4]; // 32 bit counter
while (maskLen > 0) {
try {
} catch (DigestException e) {
// should never happen
throw new BadPaddingException(e.toString());
}
}
if (maskLen > 0) {
// increment counter
// empty
}
}
}
}
}