public class Util extends Object
Modifier and Type | Field and Description |
---|---|
static int |
ENC_MODE
Mode for KDF.
|
static int |
MAC_MODE |
static int |
PACE_MODE |
Modifier and Type | Method and Description |
---|---|
static ECPoint |
add(ECPoint x,
ECPoint y,
ECParameterSpec params)
EC point addition.
|
static byte[] |
alignKeyDataToSize(byte[] keyData,
int size) |
static BigInteger |
computeAffineY(BigInteger affineX,
ECParameterSpec params)
This just solves the curve equation for y.
|
static byte[] |
computeKeySeed(String cardAccessNumber,
String digestAlg,
boolean doTruncate) |
static byte[] |
computeKeySeed(String documentNumber,
String dateOfBirth,
String dateOfExpiry,
String digestAlg,
boolean doTruncate)
Computes the static key seed, based on information from the MRZ.
|
static SecretKey |
deriveKey(byte[] keySeed,
int mode)
Derives the ENC or MAC key for BAC from the keySeed.
|
static SecretKey |
deriveKey(byte[] keySeed,
String cipherAlg,
int keyLength,
byte[] nonce,
int mode)
Derives a shared key.
|
static SecretKey |
deriveKey(byte[] keySeed,
String cipherAlg,
int keyLength,
byte[] nonce,
int mode,
byte paceKeyReference)
Derives a shared key.
|
static SecretKey |
deriveKey(byte[] keySeed,
String cipherAlgName,
int keyLength,
int mode)
Derives the ENC or MAC key for BAC or PACE
|
static byte[] |
ecPoint2OS(ECPoint point)
Encode an EC point (for use as public key value).
|
static ECPoint |
fromBouncyCastleECPoint(ECPoint point) |
static Provider |
getBouncyCastleProvider()
Gets the BC provider, if present.
|
static byte[] |
getBytes(String str) |
static CertificateFactory |
getCertificateFactory(String algorithm) |
static Cipher |
getCipher(String algorithm) |
static Cipher |
getCipher(String algorithm,
int mode,
Key keySpec) |
static String |
getCurveName(ECParameterSpec params)
Gets the curve name if known (or
null ). |
static String |
getDetailedPrivateKeyAlgorithm(PrivateKey privateKey)
Returns detailed algorithm information (including key length) about the given private key.
|
static String |
getDetailedPublicKeyAlgorithm(PublicKey publicKey)
Return detailed information about the given public key (like RSA or) with some extra
information (like 1024 bits).
|
static KeyAgreement |
getKeyAgreement(String algorithm) |
static byte[] |
getKeyData(String agreementAlg,
PublicKey pcdPublicKey) |
static byte[] |
getKeyHash(String agreementAlg,
PublicKey pcdPublicKey) |
static KeyPairGenerator |
getKeyPairGenerator(String algorithm) |
static Mac |
getMac(String algorithm) |
static Mac |
getMac(String algorithm,
Key key) |
static MessageDigest |
getMessageDigest(String algorithm) |
static BigInteger |
getPrime(AlgorithmParameterSpec params) |
static PublicKey |
getPublicKey(String algorithm,
KeySpec keySpec) |
static byte[] |
getRawECDSASignature(byte[] signedData,
int keySize)
For ECDSA the EAC 1.11 specification requires the signature to be stripped down from any ASN.1 wrappers, as so.
|
static Signature |
getSignature(String algorithm) |
static byte[] |
i2os(BigInteger val)
Converts an integer to an octet string.
|
static byte[] |
i2os(BigInteger val,
int length)
Converts an integer to an octet string.
|
static String |
inferDigestAlgorithmFromCipherAlgorithmForKeyDerivation(String cipherAlg,
int keyLength)
Infers a digest algorithm mnemonic from a signature algorithm mnemonic for
use in key derivation.
|
static String |
inferDigestAlgorithmFromSignatureAlgorithm(String signatureAlgorithm)
Infers a digest algorithm mnemonic from a signature algorithm mnemonic.
|
static String |
inferKeyAgreementAlgorithm(PublicKey publicKey) |
static String |
inferProtocolIdentifier(PublicKey publicKey)
Infer an EAC object identifier for an EC or DH public key.
|
static boolean |
isValid(ECPoint ecPoint,
ECParameterSpec params) |
static ECPoint |
multiply(BigInteger s,
ECPoint point,
ECParameterSpec params)
EC point scalar multiplication.
|
static ECPoint |
normalize(ECPoint ecPoint,
ECParameterSpec params) |
static ECPoint |
os2ECPoint(byte[] encodedECPoint) |
static BigInteger |
os2fe(byte[] bytes,
BigInteger p)
Convert an octet string to field element via OS2FE as specified in BSI TR-03111.
|
static BigInteger |
os2i(byte[] bytes)
Converts an octet string to an integer.
|
static BigInteger |
os2i(byte[] bytes,
int offset,
int length)
Converts an octet string to an integer.
|
static byte[] |
pad(byte[] in,
int blockSize)
Pads the input
in according to ISO9797-1 padding method 2,
using the given block size. |
static byte[] |
pad(byte[] bytes,
int offset,
int length,
int blockSize)
Pads the input
bytes indicated by offset and length
according to ISO9797-1 padding method 2, using the given block size in blockSize . |
static PublicKey |
reconstructPublicKey(PublicKey publicKey)
Reconstructs the public key to use explicit domain params for EC public keys
|
static byte[] |
recoverMessage(int digestLength,
byte[] decryptedResponse)
Recovers the M1 part of the message sent back by the AA protocol
(INTERNAL AUTHENTICATE command).
|
static ECPoint |
toBouncyCastleECPoint(ECPoint point,
ECParameterSpec params) |
static ECDomainParameters |
toBouncyECDomainParameters(ECParameterSpec params) |
static ECPrivateKeyParameters |
toBouncyECPrivateKeyParameters(ECPrivateKey privateKey) |
static ECPublicKeyParameters |
toBouncyECPublicKeyParameters(ECPublicKey publicKey) |
static ECNamedCurveSpec |
toECNamedCurveSpec(ECNamedCurveParameterSpec namedParamSpec)
Translates internal BC named curve spec to BC provided JCA compliant named curve spec.
|
static DHParameterSpec |
toExplicitDHParameterSpec(DHParameters params)
Returns a Difie-Hellman parameter specification which includes
the prime order of the subgroup generated by the generator if this
information is available in the given (Bouncy Castle) parameters.
|
static ECParameterSpec |
toExplicitECParameterSpec(ECNamedCurveParameterSpec parameterSpec) |
static ECParameterSpec |
toExplicitECParameterSpec(ECParameterSpec params)
Translates (named) curve spec to JCA compliant explicit param spec.
|
static PublicKey |
toPublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo) |
static SubjectPublicKeyInfo |
toSubjectPublicKeyInfo(PublicKey publicKey) |
static byte[] |
unpad(byte[] bytes)
Unpads the input
bytes according to ISO9797-1 padding method 2. |
public static final int ENC_MODE
public static final int MAC_MODE
public static final int PACE_MODE
public static Provider getBouncyCastleProvider()
null
public static SecretKey deriveKey(byte[] keySeed, int mode) throws GeneralSecurityException
keySeed
- the key seed.mode
- either ENC_MODE
or MAC_MODE
GeneralSecurityException
- on security errorpublic static SecretKey deriveKey(byte[] keySeed, String cipherAlgName, int keyLength, int mode) throws GeneralSecurityException
keySeed
- the key seed.cipherAlgName
- either AES or DESedekeyLength
- key length in bitsmode
- either ENC_MODE
, MAC_MODE
, or PACE_MODE
GeneralSecurityException
- on security errorpublic static SecretKey deriveKey(byte[] keySeed, String cipherAlg, int keyLength, byte[] nonce, int mode) throws GeneralSecurityException
keySeed
- the shared secret, as octetscipherAlg
- in Java mnemonic notation (for example "DESede", "AES")keyLength
- length in bitsnonce
- optional nonce or null
mode
- the mode either ENC
, MAC
, or PACE
modeGeneralSecurityException
- if something went wrongpublic static SecretKey deriveKey(byte[] keySeed, String cipherAlg, int keyLength, byte[] nonce, int mode, byte paceKeyReference) throws GeneralSecurityException
keySeed
- the shared secret, as octetscipherAlg
- in Java mnemonic notation (for example "DESede", "AES")keyLength
- length in bitsnonce
- optional nonce or null
mode
- the mode either ENC
, MAC
, or PACE
modepaceKeyReference
- Key Reference For Pace ProtocolGeneralSecurityException
- if something went wrongpublic static byte[] computeKeySeed(String documentNumber, String dateOfBirth, String dateOfExpiry, String digestAlg, boolean doTruncate) throws GeneralSecurityException
documentNumber
- a string containing the document numberdateOfBirth
- a string containing the date of birth (YYMMDD)dateOfExpiry
- a string containing the date of expiry (YYMMDD)digestAlg
- a Java mnemonic algorithm string to indicate the digest algorithm (typically SHA-1)doTruncate
- whether to truncate the resulting output to 16 bytesGeneralSecurityException
- on security errorpublic static byte[] computeKeySeed(String cardAccessNumber, String digestAlg, boolean doTruncate) throws GeneralSecurityException
GeneralSecurityException
public static byte[] pad(byte[] in, int blockSize)
in
according to ISO9797-1 padding method 2,
using the given block size.in
- inputblockSize
- the block sizepublic static byte[] pad(byte[] bytes, int offset, int length, int blockSize)
bytes
indicated by offset
and length
according to ISO9797-1 padding method 2, using the given block size in blockSize
.bytes
- inputoffset
- the offsetlength
- the lengthblockSize
- the block sizepublic static byte[] unpad(byte[] bytes) throws BadPaddingException
bytes
according to ISO9797-1 padding method 2.bytes
- the inputBadPaddingException
- on padding exceptionpublic static byte[] recoverMessage(int digestLength, byte[] decryptedResponse)
digestLength
- should be 20decryptedResponse
- response from card, already 'decrypted' (using the AA public key)public static byte[] getRawECDSASignature(byte[] signedData, int keySize) throws IOException
signedData
- signed datakeySize
- key sizeIOException
- on errorpublic static byte[] alignKeyDataToSize(byte[] keyData, int size)
public static byte[] i2os(BigInteger val, int length)
val
- positive integerlength
- lengthpublic static byte[] i2os(BigInteger val)
val
- positive integerpublic static BigInteger os2i(byte[] bytes)
bytes
- octet stringpublic static BigInteger os2i(byte[] bytes, int offset, int length)
bytes
- octet stringoffset
- offset of octet stringlength
- length of octet stringpublic static BigInteger os2fe(byte[] bytes, BigInteger p)
bytes
- octet stringp
- moduluspublic static String inferDigestAlgorithmFromSignatureAlgorithm(String signatureAlgorithm)
signatureAlgorithm
- a signature algorithmnull
if inference failedpublic static String inferDigestAlgorithmFromCipherAlgorithmForKeyDerivation(String cipherAlg, int keyLength)
cipherAlg
- a cipher algorithmkeyLength
- the key lengthnull
if inference failedpublic static DHParameterSpec toExplicitDHParameterSpec(DHParameters params)
params
- parameters for Diffie-Hellman as a Bouncy Castle specific object.public static String getDetailedPublicKeyAlgorithm(PublicKey publicKey)
publicKey
- a public keypublic static String getDetailedPrivateKeyAlgorithm(PrivateKey privateKey)
privateKey
- a private keypublic static String getCurveName(ECParameterSpec params)
null
).params
- an specification of the curvepublic static ECParameterSpec toExplicitECParameterSpec(ECNamedCurveParameterSpec parameterSpec)
public static ECParameterSpec toExplicitECParameterSpec(ECParameterSpec params)
params
- an EC parameter spec, possibly namedpublic static ECNamedCurveSpec toECNamedCurveSpec(ECNamedCurveParameterSpec namedParamSpec)
namedParamSpec
- a named EC parameter specpublic static SubjectPublicKeyInfo toSubjectPublicKeyInfo(PublicKey publicKey)
public static PublicKey toPublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo)
public static PublicKey reconstructPublicKey(PublicKey publicKey)
publicKey
- the public keypublic static ECPoint os2ECPoint(byte[] encodedECPoint)
public static byte[] ecPoint2OS(ECPoint point)
0x04
(without a length).point
- an EC Pointpublic static String inferProtocolIdentifier(PublicKey publicKey)
publicKey
- a public keypublic static ECPoint add(ECPoint x, ECPoint y, ECParameterSpec params)
x
- an EC pointy
- another EC pointparams
- the domain parameterspublic static ECPoint multiply(BigInteger s, ECPoint point, ECParameterSpec params)
s
- the scalarpoint
- an EC pointparams
- the domain parameterspublic static byte[] getBytes(String str)
public static BigInteger getPrime(AlgorithmParameterSpec params)
public static BigInteger computeAffineY(BigInteger affineX, ECParameterSpec params)
affineX
- the x coord of a point on the curveparams
- EC parameters for curve over Fppublic static ECPoint toBouncyCastleECPoint(ECPoint point, ECParameterSpec params)
public static boolean isValid(ECPoint ecPoint, ECParameterSpec params)
public static ECPoint normalize(ECPoint ecPoint, ECParameterSpec params)
public static ECPublicKeyParameters toBouncyECPublicKeyParameters(ECPublicKey publicKey)
public static ECPrivateKeyParameters toBouncyECPrivateKeyParameters(ECPrivateKey privateKey)
public static ECDomainParameters toBouncyECDomainParameters(ECParameterSpec params)
public static byte[] getKeyHash(String agreementAlg, PublicKey pcdPublicKey) throws NoSuchAlgorithmException
NoSuchAlgorithmException
public static Cipher getCipher(String algorithm) throws GeneralSecurityException
GeneralSecurityException
public static Cipher getCipher(String algorithm, int mode, Key keySpec) throws GeneralSecurityException
GeneralSecurityException
public static KeyAgreement getKeyAgreement(String algorithm) throws GeneralSecurityException
GeneralSecurityException
public static KeyPairGenerator getKeyPairGenerator(String algorithm) throws GeneralSecurityException
GeneralSecurityException
public static Mac getMac(String algorithm) throws GeneralSecurityException
GeneralSecurityException
public static Mac getMac(String algorithm, Key key) throws GeneralSecurityException
GeneralSecurityException
public static MessageDigest getMessageDigest(String algorithm) throws GeneralSecurityException
GeneralSecurityException
public static PublicKey getPublicKey(String algorithm, KeySpec keySpec) throws GeneralSecurityException
GeneralSecurityException
public static Signature getSignature(String algorithm) throws GeneralSecurityException
GeneralSecurityException
public static CertificateFactory getCertificateFactory(String algorithm) throws GeneralSecurityException
GeneralSecurityException
Copyright © 2018. All rights reserved.