Top Description Inners Fields Constructors Methods
sun.security.rsa

public final Class RSACore

extends Object
Class Inheritance
Imports
sun.security.jca.JCAUtil, javax.crypto.BadPaddingException, java.math.BigInteger, java.security.SecureRandom, java.security.interfaces.RSAKey, .RSAPrivateCrtKey, .RSAPrivateKey, .RSAPublicKey, java.util.Arrays, .Map, .WeakHashMap, java.util.concurrent.ConcurrentLinkedQueue, java.util.concurrent.locks.ReentrantLock

Core of the RSA implementation. Has code to perform public and private key RSA operations (with and without CRT for private key ops). Private CRT ops also support blinding to thwart timing attacks. The code in this class only does the core RSA operation. Padding and unpadding must be done externally.

Note

RSA keys should be at least 512 bits long

Author
Andreas Sterbenz
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
RSACore.BlindingParameters

Set of blinding parameters for a given RSA key.

private static class
RSACore.BlindingRandomPair

Parameters (u,v) for RSA Blinding.

Field Summary

Modifier and TypeField and Description
private static final Map<BigInteger, ConcurrentLinkedQueue<RSACore.BlindingParameters>>
private static final boolean
private static final ReentrantLock

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static byte[]
convert(byte[] b, int ofs, int len)

private static byte[]
crtCrypt(byte[] msg, RSAPrivateCrtKey key, boolean verify)

RSA private key operations with CRT.

private static byte[]
crypt(byte[] msg, BigInteger n, BigInteger exp)

RSA public key ops.

private static RSACore.BlindingRandomPair
public static int
getByteLength(BigInteger b)

Return the number of bytes required to store the magnitude byte[] of this BigInteger.

public static int
getByteLength(RSAKey key)

Return the number of bytes required to store the modulus of this RSA key.

private static BigInteger
parseMsg(byte[] msg, BigInteger n)

Parse the msg into a BigInteger and check against the modulus n.

private static byte[]
priCrypt(byte[] msg, BigInteger n, BigInteger exp)

RSA non-CRT private key operations.

public static byte[]
rsa(byte[] msg, RSAPublicKey key)

Perform an RSA public key operation.

public static byte[]
rsa(byte[] msg, RSAPrivateKey key)

Deprecated
Perform an RSA private key operation.
public static byte[]
rsa(byte[] msg, RSAPrivateKey key, boolean verify)

Perform an RSA private key operation.

private static byte[]
toByteArray(BigInteger bi, int len)

Return the encoding of this BigInteger that is exactly len bytes long.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blindingCacheback to summary
private static final Map<BigInteger, ConcurrentLinkedQueue<RSACore.BlindingParameters>> blindingCache
ENABLE_BLINDINGback to summary
private static final boolean ENABLE_BLINDING
lockback to summary
private static final ReentrantLock lock

Constructor Detail

RSACoreback to summary
private RSACore()

Method Detail

convertback to summary
public static byte[] convert(byte[] b, int ofs, int len)
crtCryptback to summary
private static byte[] crtCrypt(byte[] msg, RSAPrivateCrtKey key, boolean verify) throws BadPaddingException

RSA private key operations with CRT. Algorithm and variable naming are taken from PKCS#1 v2.1, section 5.1.2.

cryptback to summary
private static byte[] crypt(byte[] msg, BigInteger n, BigInteger exp) throws BadPaddingException

RSA public key ops. Simple modPow().

getBlindingRandomPairback to summary
private static RSACore.BlindingRandomPair getBlindingRandomPair(BigInteger e, BigInteger d, BigInteger n)
getByteLengthback to summary
public static int getByteLength(BigInteger b)

Return the number of bytes required to store the magnitude byte[] of this BigInteger. Do not count a 0x00 byte toByteArray() would prefix for 2's complement form.

getByteLengthback to summary
public static int getByteLength(RSAKey key)

Return the number of bytes required to store the modulus of this RSA key.

parseMsgback to summary
private static BigInteger parseMsg(byte[] msg, BigInteger n) throws BadPaddingException

Parse the msg into a BigInteger and check against the modulus n.

priCryptback to summary
private static byte[] priCrypt(byte[] msg, BigInteger n, BigInteger exp) throws BadPaddingException

RSA non-CRT private key operations.

rsaback to summary
public static byte[] rsa(byte[] msg, RSAPublicKey key) throws BadPaddingException

Perform an RSA public key operation.

rsaback to summary
public static byte[] rsa(byte[] msg, RSAPrivateKey key) throws BadPaddingException

Deprecated

Perform an RSA private key operation. Uses CRT if the key is a CRT key with additional verification check after the signature is computed.

Annotations
@Deprecated
rsaback to summary
public static byte[] rsa(byte[] msg, RSAPrivateKey key, boolean verify) throws BadPaddingException

Perform an RSA private key operation. Uses CRT if the key is a CRT key. Set 'verify' to true if this function is used for generating a signature.

toByteArrayback to summary
private static byte[] toByteArray(BigInteger bi, int len)

Return the encoding of this BigInteger that is exactly len bytes long. Prefix/strip off leading 0x00 bytes if necessary.

Precondition

bi must fit into len bytes

sun.security.rsa back to summary

private final Class RSACore.BlindingParameters

extends Object
Class Inheritance

Set of blinding parameters for a given RSA key. The RSA modulus is usually unique, so we index by modulus in blindingCache. However, to protect against the unlikely case of two keys sharing the same modulus, we also store the public or the private exponent. This means we cannot cache blinding parameters for multiple keys that share the same modulus, but since sharing moduli is fundamentally broken and insecure, this does not matter.

Field Summary

Modifier and TypeField and Description
private static final BigInteger
private final BigInteger
d

private final BigInteger
e

private BigInteger
u

private BigInteger
v

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv RSACore.BlindingRandomPair
pack-priv boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BIG_TWOback to summary
private static final BigInteger BIG_TWO
dback to summary
private final BigInteger d
eback to summary
private final BigInteger e
uback to summary
private BigInteger u
vback to summary
private BigInteger v

Constructor Detail

BlindingParametersback to summary
pack-priv BlindingParameters(BigInteger e, BigInteger d, BigInteger n)

Method Detail

getBlindingRandomPairback to summary
pack-priv RSACore.BlindingRandomPair getBlindingRandomPair(BigInteger e, BigInteger d, BigInteger n)
isReusableback to summary
pack-priv boolean isReusable()
sun.security.rsa back to summary

private final Class RSACore.BlindingRandomPair

extends Object
Class Inheritance

Parameters (u,v) for RSA Blinding. This is described in the RSA Bulletin#2 (Jan 96) and other places: ftp://ftp.rsa.com/pub/pdfs/bull-2.pdf The standard RSA Blinding decryption requires the public key exponent (e) and modulus (n), and converts ciphertext (c) to plaintext (p). Before the modular exponentiation operation, the input message should be multiplied by (u (mod n)), and afterward the result is corrected by multiplying with (v (mod n)). The system should reject messages equal to (0 (mod n)). That is: 1. Generate r between 0 and n-1, relatively prime to n. 2. Compute x = (c*u) mod n 3. Compute y = (x^d) mod n 4. Compute p = (y*v) mod n The Java APIs allows for either standard RSAPrivateKey or RSAPrivateCrtKey RSA keys. If the public exponent is available to us (e.g. RSAPrivateCrtKey), choose a random r, then let (u, v): u = r ^ e mod n v = r ^ (-1) mod n The proof follows: p = (((c * u) ^ d mod n) * v) mod n = ((c ^ d) * (u ^ d) * v) mod n = ((c ^ d) * (r ^ e) ^ d) * (r ^ (-1))) mod n = ((c ^ d) * (r ^ (e * d)) * (r ^ (-1))) mod n = ((c ^ d) * (r ^ 1) * (r ^ (-1))) mod n (see below) = (c ^ d) mod n because in RSA cryptosystem, d is the multiplicative inverse of e: (r^(e * d)) mod n = (r ^ 1) mod n = r mod n However, if the public exponent is not available (e.g. RSAPrivateKey), we mitigate the timing issue by using a similar random number blinding approach using the private key: u = r v = ((r ^ (-1)) ^ d) mod n This returns the same plaintext because: p = (((c * u) ^ d mod n) * v) mod n = ((c ^ d) * (u ^ d) * v) mod n = ((c ^ d) * (u ^ d) * ((u ^ (-1)) ^d)) mod n = (c ^ d) mod n Computing inverses mod n and random number generation is slow, so it is often not practical to generate a new random (u, v) pair for each new exponentiation. The calculation of parameters might even be subject to timing attacks. However, (u, v) pairs should not be reused since they themselves might be compromised by timing attacks, leaving the private exponent vulnerable. An efficient solution to this problem is update u and v before each modular exponentiation step by computing: u = u ^ 2 v = v ^ 2 The total performance cost is small.

Field Summary

Modifier and TypeField and Description
pack-priv final BigInteger
u

pack-priv final BigInteger
v

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

uback to summary
pack-priv final BigInteger u
vback to summary
pack-priv final BigInteger v

Constructor Detail

BlindingRandomPairback to summary
pack-priv BlindingRandomPair(BigInteger u, BigInteger v)