public final class EllipticCurves extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
EllipticCurves.CurveType
Elliptic curve types.
|
static class |
EllipticCurves.EcdsaEncoding
Ecdsa signature encoding.
|
static class |
EllipticCurves.PointFormatType
Point formats.
|
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
computeSharedSecret(ECPrivateKey myPrivateKey,
ECPoint publicPoint)
Generates the DH shared secret using
myPrivateKey and publicPoint |
static byte[] |
computeSharedSecret(ECPrivateKey myPrivateKey,
ECPublicKey peerPublicKey) |
static byte[] |
ecdsaDer2Ieee(byte[] der,
int ieeeLength)
Transforms ECDSA DER signature encoding to IEEE_P1363 encoding.
|
static byte[] |
ecdsaIeee2Der(byte[] ieee)
Transforms ECDSA IEEE_P1363 signature encoding to DER encoding.
|
static ECPoint |
ecPointDecode(EllipticCurve curve,
EllipticCurves.PointFormatType format,
byte[] encoded)
Decodes an encoded point on an elliptic curve.
|
static int |
encodingSizeInBytes(EllipticCurve curve,
EllipticCurves.PointFormatType format)
Returns the encoding size of a point on an elliptic curve.
|
static int |
fieldSizeInBits(EllipticCurve curve)
Returns the size of an element of the field over which the curve is defined.
|
static int |
fieldSizeInBytes(EllipticCurve curve)
Returns the size of an element of the field over which the curve is defined.
|
static KeyPair |
generateKeyPair(ECParameterSpec spec)
Generates a new key pair for
spec. |
static KeyPair |
generateKeyPair(EllipticCurves.CurveType curve)
Generates a new key pair for
curve. |
static ECParameterSpec |
getCurveSpec(EllipticCurves.CurveType curve)
Returns the ECParameterSpec for a named curve.
|
static ECPrivateKey |
getEcPrivateKey(byte[] pkcs8PrivateKey)
Returns an
ECPrivateKey from pkcs8PrivateKey which is an encoding of a private
key, encoded according to the ASN.1 type SubjectPublicKeyInfo. |
static ECPrivateKey |
getEcPrivateKey(EllipticCurves.CurveType curve,
byte[] keyValue)
Returns an
ECPrivateKey from curve type and keyValue. |
static ECPublicKey |
getEcPublicKey(byte[] x509PublicKey)
Returns an
ECPublicKey from x509PublicKey which is an encoding of a public
key, encoded according to the ASN.1 type SubjectPublicKeyInfo. |
static ECPublicKey |
getEcPublicKey(ECParameterSpec spec,
EllipticCurves.PointFormatType pointFormat,
byte[] publicKey)
|
static ECPublicKey |
getEcPublicKey(EllipticCurves.CurveType curve,
byte[] x,
byte[] y)
Returns an
ECPublicKey from curve type and x and y coordinates. |
static ECPublicKey |
getEcPublicKey(EllipticCurves.CurveType curve,
EllipticCurves.PointFormatType pointFormat,
byte[] publicKey)
|
static BigInteger |
getModulus(EllipticCurve curve)
Returns the modulus of the field used by the curve specified in ecParams.
|
static ECParameterSpec |
getNistP256Params() |
static ECParameterSpec |
getNistP384Params() |
static ECParameterSpec |
getNistP521Params() |
static BigInteger |
getY(BigInteger x,
boolean lsb,
EllipticCurve curve)
Computes the y coordinate of a point on an elliptic curve.
|
static boolean |
isNistEcParameterSpec(ECParameterSpec spec)
Returns whether
spec is a ECParameterSpec of one of the NIST curves. |
static boolean |
isSameEcParameterSpec(ECParameterSpec one,
ECParameterSpec two)
|
static boolean |
isValidDerEncoding(byte[] sig) |
protected static BigInteger |
modSqrt(BigInteger x,
BigInteger p)
Computes a square root modulo an odd prime.
|
static ECPoint |
pointDecode(EllipticCurve curve,
EllipticCurves.PointFormatType format,
byte[] encoded)
Decodes an encoded point on an elliptic curve.
|
static ECPoint |
pointDecode(EllipticCurves.CurveType curveType,
EllipticCurves.PointFormatType format,
byte[] encoded)
Decodes an encoded point on an elliptic curve.
|
static byte[] |
pointEncode(EllipticCurve curve,
EllipticCurves.PointFormatType format,
ECPoint point)
Encodes a point on an elliptic curve.
|
static byte[] |
pointEncode(EllipticCurves.CurveType curveType,
EllipticCurves.PointFormatType format,
ECPoint point)
Encodes a point on an elliptic curve.
|
static void |
validatePublicKey(ECPublicKey publicKey,
ECPrivateKey privateKey)
Checks that the public key's params is the same as the private key's params, and the public key
is a valid point on the private key's curve.
|
public static ECParameterSpec getNistP256Params()
public static ECParameterSpec getNistP384Params()
public static ECParameterSpec getNistP521Params()
public static boolean isNistEcParameterSpec(ECParameterSpec spec)
spec is a ECParameterSpec of one of the NIST curves.public static boolean isSameEcParameterSpec(ECParameterSpec one, ECParameterSpec two)
public static void validatePublicKey(ECPublicKey publicKey, ECPrivateKey privateKey) throws GeneralSecurityException
GeneralSecurityExceptionpublic static BigInteger getModulus(EllipticCurve curve) throws GeneralSecurityException
curve - must be a prime order elliptic curveGeneralSecurityExceptionpublic static int fieldSizeInBits(EllipticCurve curve) throws GeneralSecurityException
curve - must be a prime order elliptic curveGeneralSecurityExceptionpublic static int fieldSizeInBytes(EllipticCurve curve) throws GeneralSecurityException
curve - must be a prime order elliptic curveGeneralSecurityExceptionprotected static BigInteger modSqrt(BigInteger x, BigInteger p) throws GeneralSecurityException
x - the squarep - the prime modulus (the behaviour of the method is undefined if p is not prime).GeneralSecurityException - if the square root could not be found.public static BigInteger getY(BigInteger x, boolean lsb, EllipticCurve curve) throws GeneralSecurityException
x - the x-coordinate of the pointlsb - the least significant bit of the y-coordinate of the point.curve - this must be an elliptic curve over a prime field using Weierstrass
representation.GeneralSecurityException - if there is no point with coordinate x on the curve, or if
curve is not supported.public static byte[] ecdsaIeee2Der(byte[] ieee)
throws GeneralSecurityException
The IEEE_P1363 signature's format is r || s, where r and s are zero-padded and have the same size in bytes as the order of the curve. For example, for NIST P-256 curve, r and s are zero-padded to 32 bytes.
The DER signature is encoded using ASN.1 (https://tools.ietf.org/html/rfc5480#appendix-A): ECDSA-Sig-Value :: = SEQUENCE { r INTEGER, s INTEGER }. In particular, the encoding is: 0x30 || totalLength || 0x02 || r's length || r || 0x02 || s's length || s.
ieee - ECDSA's signature in IEEE_P1363 format.GeneralSecurityException - if ieee's length is zero, greater than 132-byte (corresponding
to NIST P521) or not divisible by 2.public static byte[] ecdsaDer2Ieee(byte[] der,
int ieeeLength)
throws GeneralSecurityException
The IEEE_P1363 signature's format is r || s, where r and s are zero-padded and have the same size in bytes as the order of the curve. For example, for NIST P-256 curve, r and s are zero-padded to 32 bytes.
The DER signature is encoded using ASN.1 (https://tools.ietf.org/html/rfc5480#appendix-A): ECDSA-Sig-Value :: = SEQUENCE { r INTEGER, s INTEGER }. In particular, the encoding is: 0x30 || totalLength || 0x02 || r's length || r || 0x02 || s's length || s.
der - ECDSA's signature in DER encoding.ieeeLength - length of ECDSA signature's in IEEE_P1363's format which equals to 2 * (size
of elliptic curve's field in bytes).GeneralSecurityException - if the signature is not valid DER encoding.public static boolean isValidDerEncoding(byte[] sig)
public static int encodingSizeInBytes(EllipticCurve curve, EllipticCurves.PointFormatType format) throws GeneralSecurityException
curve - the elliptic curveformat - the format used to encode the pointGeneralSecurityException - if the point format is unknown or if the elliptic curve is not
supportedpublic static ECPoint ecPointDecode(EllipticCurve curve, EllipticCurves.PointFormatType format, byte[] encoded) throws GeneralSecurityException
curve - the elliptic curveformat - the format used to enocde the pointencoded - the encoded pointGeneralSecurityException - if the encoded point is invalid or if the curve or format are
not supported.public static ECPoint pointDecode(EllipticCurves.CurveType curveType, EllipticCurves.PointFormatType format, byte[] encoded) throws GeneralSecurityException
curve - the elliptic curveformat - the format used to enocde the pointencoded - the encoded pointGeneralSecurityException - if the encoded point is invalid or if the curve or format are
not supported.public static ECPoint pointDecode(EllipticCurve curve, EllipticCurves.PointFormatType format, byte[] encoded) throws GeneralSecurityException
curve - the elliptic curveformat - the format used to enocde the pointencoded - the encoded pointGeneralSecurityException - if the encoded point is invalid or if the curve or format are
not supported.public static byte[] pointEncode(EllipticCurves.CurveType curveType, EllipticCurves.PointFormatType format, ECPoint point) throws GeneralSecurityException
curve - the elliptic curveformat - the format for the encodingpoint - the point to encodeGeneralSecurityException - if the point is not on the curve or if the format is not
supported.public static byte[] pointEncode(EllipticCurve curve, EllipticCurves.PointFormatType format, ECPoint point) throws GeneralSecurityException
curve - the elliptic curveformat - the format for the encodingpoint - the point to encodeGeneralSecurityException - if the point is not on the curve or if the format is not
supported.public static ECParameterSpec getCurveSpec(EllipticCurves.CurveType curve) throws NoSuchAlgorithmException
curve - the curve typeNoSuchAlgorithmExceptionpublic static ECPublicKey getEcPublicKey(byte[] x509PublicKey) throws GeneralSecurityException
ECPublicKey from x509PublicKey which is an encoding of a public
key, encoded according to the ASN.1 type SubjectPublicKeyInfo.
TODO(b/68672497): test that in Java one can always get this representation by using
{@link ECPublicKey#getEncoded), regardless of the provider.GeneralSecurityExceptionpublic static ECPublicKey getEcPublicKey(EllipticCurves.CurveType curve, EllipticCurves.PointFormatType pointFormat, byte[] publicKey) throws GeneralSecurityException
GeneralSecurityExceptionpublic static ECPublicKey getEcPublicKey(ECParameterSpec spec, EllipticCurves.PointFormatType pointFormat, byte[] publicKey) throws GeneralSecurityException
GeneralSecurityExceptionpublic static ECPublicKey getEcPublicKey(EllipticCurves.CurveType curve, byte[] x, byte[] y) throws GeneralSecurityException
ECPublicKey from curve type and x and y coordinates.GeneralSecurityExceptionpublic static ECPrivateKey getEcPrivateKey(byte[] pkcs8PrivateKey) throws GeneralSecurityException
ECPrivateKey from pkcs8PrivateKey which is an encoding of a private
key, encoded according to the ASN.1 type SubjectPublicKeyInfo.
TODO(b/68672497): test that in Java one can always get this representation by using
{@link ECPrivateKey#getEncoded), regardless of the provider.GeneralSecurityExceptionpublic static ECPrivateKey getEcPrivateKey(EllipticCurves.CurveType curve, byte[] keyValue) throws GeneralSecurityException
ECPrivateKey from curve type and keyValue.GeneralSecurityExceptionpublic static KeyPair generateKeyPair(EllipticCurves.CurveType curve) throws GeneralSecurityException
curve.GeneralSecurityExceptionpublic static KeyPair generateKeyPair(ECParameterSpec spec) throws GeneralSecurityException
spec.GeneralSecurityExceptionpublic static byte[] computeSharedSecret(ECPrivateKey myPrivateKey, ECPublicKey peerPublicKey) throws GeneralSecurityException
GeneralSecurityExceptionpublic static byte[] computeSharedSecret(ECPrivateKey myPrivateKey, ECPoint publicPoint) throws GeneralSecurityException
myPrivateKey and publicPointGeneralSecurityException