Class EllipticCurves
- java.lang.Object
-
- com.google.crypto.tink.subtle.EllipticCurves
-
public final class EllipticCurves extends java.lang.ObjectUtility functions and enums for elliptic curve crypto, used in ECDSA and ECDH.- Since:
- 1.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classEllipticCurves.CurveTypeElliptic curve types.static classEllipticCurves.EcdsaEncodingEcdsa signature encoding.static classEllipticCurves.PointFormatTypePoint format types UNCOMPRESSED and COMPRESSED are defined in https://www.secg.org/sec1-v2.pdf, Sections 2.3.3 and 2.3.4.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static byte[]computeSharedSecret(java.security.interfaces.ECPrivateKey myPrivateKey, java.security.interfaces.ECPublicKey peerPublicKey)Generates the DH shared secret usingmyPrivateKeyandpeerPublicKeystatic byte[]computeSharedSecret(java.security.interfaces.ECPrivateKey myPrivateKey, java.security.spec.ECPoint publicPoint)Generates the DH shared secret usingmyPrivateKeyandpublicPointstatic 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 java.security.spec.ECPointecPointDecode(java.security.spec.EllipticCurve curve, EllipticCurves.PointFormatType format, byte[] encoded)Decodes an encoded point on an elliptic curve.static intencodingSizeInBytes(java.security.spec.EllipticCurve curve, EllipticCurves.PointFormatType format)Returns the encoding size of a point on an elliptic curve.static intfieldSizeInBits(java.security.spec.EllipticCurve curve)Returns the size of an element of the field over which the curve is defined.static intfieldSizeInBytes(java.security.spec.EllipticCurve curve)Returns the size of an element of the field over which the curve is defined.static java.security.KeyPairgenerateKeyPair(EllipticCurves.CurveType curve)Generates a new key pair forcurve.static java.security.KeyPairgenerateKeyPair(java.security.spec.ECParameterSpec spec)Generates a new key pair forspec.static java.security.spec.ECParameterSpecgetCurveSpec(EllipticCurves.CurveType curve)Returns the ECParameterSpec for a named curve.static java.security.interfaces.ECPrivateKeygetEcPrivateKey(byte[] pkcs8PrivateKey)Returns anECPrivateKeyfrompkcs8PrivateKeywhich is an encoding of a private key, encoded according to the ASN.1 type SubjectPublicKeyInfo.static java.security.interfaces.ECPrivateKeygetEcPrivateKey(EllipticCurves.CurveType curve, byte[] keyValue)Returns anECPrivateKeyfromcurvetype andkeyValue.static java.security.interfaces.ECPublicKeygetEcPublicKey(byte[] x509PublicKey)Returns anECPublicKeyfromx509PublicKeywhich is an encoding of a public key, encoded according to the ASN.1 type SubjectPublicKeyInfo.static java.security.interfaces.ECPublicKeygetEcPublicKey(EllipticCurves.CurveType curve, byte[] x, byte[] y)Returns anECPublicKeyfromcurvetype andxandycoordinates.static java.security.interfaces.ECPublicKeygetEcPublicKey(EllipticCurves.CurveType curve, EllipticCurves.PointFormatType pointFormat, byte[] publicKey)Returns anECPublicKeyfrompublicKeythat is a public key in point formatpointFormatoncurve.static java.security.interfaces.ECPublicKeygetEcPublicKey(java.security.spec.ECParameterSpec spec, EllipticCurves.PointFormatType pointFormat, byte[] publicKey)Returns anECPublicKeyfrompublicKeythat is a public key in point formatpointFormatoncurve.static java.math.BigIntegergetModulus(java.security.spec.EllipticCurve curve)Returns the modulus of the field used by the curve specified in ecParams.static java.security.spec.ECParameterSpecgetNistP256Params()static java.security.spec.ECParameterSpecgetNistP384Params()static java.security.spec.ECParameterSpecgetNistP521Params()static java.math.BigIntegergetY(java.math.BigInteger x, boolean lsb, java.security.spec.EllipticCurve curve)Deprecated.This shouldn't be used directly, usepointDecode(com.google.crypto.tink.subtle.EllipticCurves.CurveType,com.google.crypto.tink.subtle.EllipticCurves.PointFormatType,byte[])to decompress points.static booleanisNistEcParameterSpec(java.security.spec.ECParameterSpec spec)Returns whetherspecis aECParameterSpecof one of the NIST curves.static booleanisSameEcParameterSpec(java.security.spec.ECParameterSpec one, java.security.spec.ECParameterSpec two)Returns whetheroneis the sameECParameterSpecastwo.static booleanisValidDerEncoding(byte[] sig)static java.security.spec.ECPointpointDecode(EllipticCurves.CurveType curveType, EllipticCurves.PointFormatType format, byte[] encoded)Decodes an encoded point on an elliptic curve.static java.security.spec.ECPointpointDecode(java.security.spec.EllipticCurve curve, EllipticCurves.PointFormatType format, byte[] encoded)Decodes an encoded point on an elliptic curve.static byte[]pointEncode(EllipticCurves.CurveType curveType, EllipticCurves.PointFormatType format, java.security.spec.ECPoint point)Encodes a point on an elliptic curve.static byte[]pointEncode(java.security.spec.EllipticCurve curve, EllipticCurves.PointFormatType format, java.security.spec.ECPoint point)Encodes a point on an elliptic curve.static voidvalidatePublicKey(java.security.interfaces.ECPublicKey publicKey, java.security.interfaces.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.
-
-
-
Method Detail
-
getNistP256Params
public static java.security.spec.ECParameterSpec getNistP256Params()
-
getNistP384Params
public static java.security.spec.ECParameterSpec getNistP384Params()
-
getNistP521Params
public static java.security.spec.ECParameterSpec getNistP521Params()
-
isNistEcParameterSpec
public static boolean isNistEcParameterSpec(java.security.spec.ECParameterSpec spec)
Returns whetherspecis aECParameterSpecof one of the NIST curves.
-
isSameEcParameterSpec
public static boolean isSameEcParameterSpec(java.security.spec.ECParameterSpec one, java.security.spec.ECParameterSpec two)Returns whetheroneis the sameECParameterSpecastwo.
-
validatePublicKey
public static void validatePublicKey(java.security.interfaces.ECPublicKey publicKey, java.security.interfaces.ECPrivateKey privateKey) throws java.security.GeneralSecurityExceptionChecks 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.- Throws:
java.security.GeneralSecurityException- Since:
- 1.1.0
-
getModulus
public static java.math.BigInteger getModulus(java.security.spec.EllipticCurve curve) throws java.security.GeneralSecurityExceptionReturns the modulus of the field used by the curve specified in ecParams.- Parameters:
curve- must be a prime order elliptic curve- Returns:
- the order of the finite field over which curve is defined.
- Throws:
java.security.GeneralSecurityException
-
fieldSizeInBits
public static int fieldSizeInBits(java.security.spec.EllipticCurve curve) throws java.security.GeneralSecurityExceptionReturns the size of an element of the field over which the curve is defined.- Parameters:
curve- must be a prime order elliptic curve- Returns:
- the size of an element in bits
- Throws:
java.security.GeneralSecurityException
-
fieldSizeInBytes
public static int fieldSizeInBytes(java.security.spec.EllipticCurve curve) throws java.security.GeneralSecurityExceptionReturns the size of an element of the field over which the curve is defined.- Parameters:
curve- must be a prime order elliptic curve- Returns:
- the size of an element in bytes.
- Throws:
java.security.GeneralSecurityException
-
getY
@Deprecated public static java.math.BigInteger getY(java.math.BigInteger x, boolean lsb, java.security.spec.EllipticCurve curve) throws java.security.GeneralSecurityExceptionDeprecated.This shouldn't be used directly, usepointDecode(com.google.crypto.tink.subtle.EllipticCurves.CurveType,com.google.crypto.tink.subtle.EllipticCurves.PointFormatType,byte[])to decompress points.Computes the y coordinate of a point on an elliptic curve.- Throws:
java.security.GeneralSecurityException
-
ecdsaIeee2Der
public static byte[] ecdsaIeee2Der(byte[] ieee) throws java.security.GeneralSecurityExceptionTransforms ECDSA IEEE_P1363 signature encoding to DER encoding.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.
- Parameters:
ieee- ECDSA's signature in IEEE_P1363 format.- Returns:
- ECDSA's signature in DER format.
- Throws:
java.security.GeneralSecurityException- if ieee's length is zero, greater than 132-byte (corresponding to NIST P521) or not divisible by 2.
-
ecdsaDer2Ieee
public static byte[] ecdsaDer2Ieee(byte[] der, int ieeeLength) throws java.security.GeneralSecurityExceptionTransforms ECDSA DER signature encoding to IEEE_P1363 encoding.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.
- Parameters:
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).- Returns:
- ECDSA's signature in IEEE_P1363 format.
- Throws:
java.security.GeneralSecurityException- if the signature is not valid DER encoding.
-
isValidDerEncoding
public static boolean isValidDerEncoding(byte[] sig)
-
encodingSizeInBytes
public static int encodingSizeInBytes(java.security.spec.EllipticCurve curve, EllipticCurves.PointFormatType format) throws java.security.GeneralSecurityExceptionReturns the encoding size of a point on an elliptic curve.- Parameters:
curve- the elliptic curveformat- the format used to encode the point- Returns:
- the size of an encoded point in bytes
- Throws:
java.security.GeneralSecurityException- if the point format is unknown or if the elliptic curve is not supported
-
ecPointDecode
public static java.security.spec.ECPoint ecPointDecode(java.security.spec.EllipticCurve curve, EllipticCurves.PointFormatType format, byte[] encoded) throws java.security.GeneralSecurityExceptionDecodes an encoded point on an elliptic curve. This method checks that the encoded point is on the curve.- Parameters:
curve- the elliptic curveformat- the format used to enocde the pointencoded- the encoded point- Returns:
- the point
- Throws:
java.security.GeneralSecurityException- if the encoded point is invalid or if the curve or format are not supported.
-
pointDecode
public static java.security.spec.ECPoint pointDecode(EllipticCurves.CurveType curveType, EllipticCurves.PointFormatType format, byte[] encoded) throws java.security.GeneralSecurityException
Decodes an encoded point on an elliptic curve. This method checks that the encoded point is on the curve.- Parameters:
curveType- the elliptic curveformat- the format used to enocde the pointencoded- the encoded point- Returns:
- the point
- Throws:
java.security.GeneralSecurityException- if the encoded point is invalid or if the curve or format are not supported.- Since:
- 1.1.0
-
pointDecode
public static java.security.spec.ECPoint pointDecode(java.security.spec.EllipticCurve curve, EllipticCurves.PointFormatType format, byte[] encoded) throws java.security.GeneralSecurityExceptionDecodes an encoded point on an elliptic curve. This method checks that the encoded point is on the curve.- Parameters:
curve- the elliptic curveformat- the format used to enocde the pointencoded- the encoded point- Returns:
- the point
- Throws:
java.security.GeneralSecurityException- if the encoded point is invalid or if the curve or format are not supported.- Since:
- 1.1.0
-
pointEncode
public static byte[] pointEncode(EllipticCurves.CurveType curveType, EllipticCurves.PointFormatType format, java.security.spec.ECPoint point) throws java.security.GeneralSecurityException
Encodes a point on an elliptic curve.- Parameters:
curveType- the elliptic curveformat- the format for the encodingpoint- the point to encode- Returns:
- the encoded key exchange
- Throws:
java.security.GeneralSecurityException- if the point is not on the curve or if the format is not supported.- Since:
- 1.1.0
-
pointEncode
public static byte[] pointEncode(java.security.spec.EllipticCurve curve, EllipticCurves.PointFormatType format, java.security.spec.ECPoint point) throws java.security.GeneralSecurityExceptionEncodes a point on an elliptic curve.- Parameters:
curve- the elliptic curveformat- the format for the encodingpoint- the point to encode- Returns:
- the encoded key exchange
- Throws:
java.security.GeneralSecurityException- if the point is not on the curve or if the format is not supported.- Since:
- 1.1.0
-
getCurveSpec
public static java.security.spec.ECParameterSpec getCurveSpec(EllipticCurves.CurveType curve) throws java.security.NoSuchAlgorithmException
Returns the ECParameterSpec for a named curve.- Parameters:
curve- the curve type- Returns:
- the ECParameterSpec for the curve.
- Throws:
java.security.NoSuchAlgorithmException
-
getEcPublicKey
public static java.security.interfaces.ECPublicKey getEcPublicKey(byte[] x509PublicKey) throws java.security.GeneralSecurityExceptionReturns anECPublicKeyfromx509PublicKeywhich 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
Key.getEncoded(), regardless of the provider.- Throws:
java.security.GeneralSecurityException
-
getEcPublicKey
public static java.security.interfaces.ECPublicKey getEcPublicKey(EllipticCurves.CurveType curve, EllipticCurves.PointFormatType pointFormat, byte[] publicKey) throws java.security.GeneralSecurityException
Returns anECPublicKeyfrompublicKeythat is a public key in point formatpointFormatoncurve.- Throws:
java.security.GeneralSecurityException
-
getEcPublicKey
public static java.security.interfaces.ECPublicKey getEcPublicKey(java.security.spec.ECParameterSpec spec, EllipticCurves.PointFormatType pointFormat, byte[] publicKey) throws java.security.GeneralSecurityExceptionReturns anECPublicKeyfrompublicKeythat is a public key in point formatpointFormatoncurve.- Throws:
java.security.GeneralSecurityException
-
getEcPublicKey
public static java.security.interfaces.ECPublicKey getEcPublicKey(EllipticCurves.CurveType curve, byte[] x, byte[] y) throws java.security.GeneralSecurityException
Returns anECPublicKeyfromcurvetype andxandycoordinates.- Throws:
java.security.GeneralSecurityException
-
getEcPrivateKey
public static java.security.interfaces.ECPrivateKey getEcPrivateKey(byte[] pkcs8PrivateKey) throws java.security.GeneralSecurityExceptionReturns anECPrivateKeyfrompkcs8PrivateKeywhich 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
Key.getEncoded(), regardless of the provider.- Throws:
java.security.GeneralSecurityException
-
getEcPrivateKey
public static java.security.interfaces.ECPrivateKey getEcPrivateKey(EllipticCurves.CurveType curve, byte[] keyValue) throws java.security.GeneralSecurityException
Returns anECPrivateKeyfromcurvetype andkeyValue.- Throws:
java.security.GeneralSecurityException
-
generateKeyPair
public static java.security.KeyPair generateKeyPair(EllipticCurves.CurveType curve) throws java.security.GeneralSecurityException
Generates a new key pair forcurve.- Throws:
java.security.GeneralSecurityException
-
generateKeyPair
public static java.security.KeyPair generateKeyPair(java.security.spec.ECParameterSpec spec) throws java.security.GeneralSecurityExceptionGenerates a new key pair forspec.- Throws:
java.security.GeneralSecurityException
-
computeSharedSecret
public static byte[] computeSharedSecret(java.security.interfaces.ECPrivateKey myPrivateKey, java.security.interfaces.ECPublicKey peerPublicKey) throws java.security.GeneralSecurityExceptionGenerates the DH shared secret usingmyPrivateKeyandpeerPublicKey- Throws:
java.security.GeneralSecurityException
-
computeSharedSecret
public static byte[] computeSharedSecret(java.security.interfaces.ECPrivateKey myPrivateKey, java.security.spec.ECPoint publicPoint) throws java.security.GeneralSecurityExceptionGenerates the DH shared secret usingmyPrivateKeyandpublicPoint- Throws:
java.security.GeneralSecurityException- Since:
- 1.1.0
-
-