java.lang.Object
net.i2p.crypto.DSAEngine
Digital signature algorithm engine supporting DSA, ECDSA, RSA, and EdDSA. Provides signing and verification operations for all supported signature types.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final I2PAppContextThe context supplying the random.private final LogLogger for this class.private static final ThreadLocal<MessageDigest> Thread-local SHA-1 digest forcalculateHash(byte[], int, int)andcalculateHash(InputStream), so that a DSA-SHA1 hash does not allocate a fresh SHA1 (a MessageDigestSpi plus a 64 byte pad) each time.private static final ThreadLocal<MessageDigest> Thread-local SHA-512 digest for the EdDSA sign/verify paths, so that each operation does not clone a fresh MessageDigest off the SigType prototype.private static final booleanWhether to use the JVM's own DSA implementation.private static final BigIntegerTwo less than dsaq, the Fermat exponent for the constant-time DSA nonce inverse.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate SignaturealtSign(byte[] data, int offset, int len, SigningPrivateKey privateKey) Generic sign any type.private SignaturealtSignRaw(String algo, SimpleDataStructure hash, PrivateKey privKey, SigType type) Generic raw sign any type.private SignaturealtSignRaw(SimpleDataStructure hash, SigningPrivateKey privateKey) Generic raw sign any type.private SignaturealtSignSHA1(byte[] data, int offset, int len, SigningPrivateKey privateKey) Alternate tosign(byte[], int, int, SigningPrivateKey)using java.security libraries.private booleanaltVerifySig(Signature signature, byte[] data, int offset, int len, SigningPublicKey verifyingKey) Generic verify any type.private booleanaltVerifySigRaw(Signature signature, SimpleDataStructure hash, PublicKey pubKey) Generic raw verify any type.private booleanaltVerifySigRaw(Signature signature, SimpleDataStructure hash, SigningPublicKey verifyingKey) Generic raw verify any typeprivate booleanaltVerifySigSHA1(Signature signature, byte[] data, int offset, int len, SigningPublicKey verifyingKey) Alternate toverifySignature(Signature, byte[], int, int, SigningPublicKey)using java.security libraries.static SHA1HashcalculateHash(byte[] source, int offset, int len) Calculate a SHA-1 hash of the source data.Reads the stream until EOF.private static EdDSAEngineengineFor(SigType type, MessageDigest md) New EdDSA or RedDSA engine for the given type, sharing the caller's (thread-local) SHA-512 digest.static DSAEngineGet the DSA engine instance from the global context.private static StringgetRawAlgo(Key key) Map a Java key to the raw (prehashed) JCA algorithm name.private static StringgetRawAlgo(SigType type) Map a signature type to the raw (prehashed) JCA algorithm name.private static MessageDigestsha512()Gives a SHA-512 digest, from a pool.sign(byte[] data, int offset, int length, SigningPrivateKey signingKey) Sign using any key type as of 0.9.12 (DSA-SHA1 only prior to that).sign(byte[] data, SigningPrivateKey signingKey) Sign using any key type.sign(InputStream in, SigningPrivateKey signingKey) Sign using DSA-SHA1 ONLY.sign(SHA1Hash hash, SigningPrivateKey signingKey) Sign using DSA-SHA1 ONLY.sign(Hash hash, SigningPrivateKey signingKey) Nonstandard.sign(SimpleDataStructure hash, PrivateKey privKey, SigType type) Generic signature type.sign(SimpleDataStructure hash, SigningPrivateKey signingKey) Generic signature type.private SignaturesignIt(SimpleDataStructure hash, SigningPrivateKey signingKey) Sign using DSA-SHA1 or Syndie DSA-SHA256 ONLY.private booleanverifySig(Signature signature, SimpleDataStructure hash, SigningPublicKey verifyingKey) Verify using DSA-SHA1 or Syndie DSA-SHA256 ONLY.booleanverifySignature(Signature signature, byte[] signedData, int offset, int size, SigningPublicKey verifyingKey) Verify using any sig type as of 0.9.12 (DSA only prior to that).booleanverifySignature(Signature signature, byte[] signedData, SigningPublicKey verifyingKey) Verify a signature using any supported signature type.booleanverifySignature(Signature signature, InputStream in, SigningPublicKey verifyingKey) Verify using DSA-SHA1 ONLY.booleanverifySignature(Signature signature, SHA1Hash hash, SigningPublicKey verifyingKey) Verify using DSA-SHA1 ONLY.booleanverifySignature(Signature signature, Hash hash, SigningPublicKey verifyingKey) Nonstandard.booleanverifySignature(Signature signature, SimpleDataStructure hash, PublicKey pubKey) Generic signature type.booleanverifySignature(Signature signature, SimpleDataStructure hash, SigningPublicKey verifyingKey) Generic signature type.
Field Details
_log
Logger for this class._context
The context supplying the random._useJavaLibs
private static final boolean _useJavaLibsWhether to use the JVM's own DSA implementation.- See Also:
_sha1
Thread-local SHA-1 digest forcalculateHash(byte[], int, int)andcalculateHash(InputStream), so that a DSA-SHA1 hash does not allocate a fresh SHA1 (a MessageDigestSpi plus a 64 byte pad) each time. Reset on every acquire and never handed outside this class, so there is no state that can leak between calls._sha512
Thread-local SHA-512 digest for the EdDSA sign/verify paths, so that each operation does not clone a fresh MessageDigest off the SigType prototype. The engine resets it on every init, and it is never handed outside this class, so no state can leak between calls.DSAQ_MINUS_2
Two less than dsaq, the Fermat exponent for the constant-time DSA nonce inverse.
Constructor Details
DSAEngine
Create a DSA engine for the given context.- Parameters:
context- the application context, supplying the log, clock and random source
Method Details
sha512
Gives a SHA-512 digest, from a pool.- Returns:
- a digest ready for use
engineFor
New EdDSA or RedDSA engine for the given type, sharing the caller's (thread-local) SHA-512 digest.- Parameters:
type- the signature type; RedDSA_SHA512_Ed25519 selects the RedDSA engine, any other type the standard EdDSA enginemd- the SHA-512 digest the new engine writes into- Returns:
- a newly allocated engine, non-null
getInstance
Get the DSA engine instance from the global context.- Returns:
- the DSA engine
verifySignature
public boolean verifySignature(Signature signature, byte[] signedData, SigningPublicKey verifyingKey) Verify a signature using any supported signature type.- Parameters:
signature- the signature to verifysignedData- the data that was signedverifyingKey- the public key to verify with- Returns:
- true if the signature is valid, false otherwise
verifySignature
public boolean verifySignature(Signature signature, byte[] signedData, int offset, int size, SigningPublicKey verifyingKey) Verify using any sig type as of 0.9.12 (DSA only prior to that).- Parameters:
signature- the signature to verifysignedData- the data that was signedoffset- index within signedData of the first signed bytesize- number of signed bytes to verify, starting at offsetverifyingKey- the public key to verify with- Returns:
- true if valid, false otherwise
verifySignature
Verify using DSA-SHA1 ONLY.- Parameters:
signature- the signature to verifyin- the data to hash and verify, read to EOF but not closedverifyingKey- the public key to verify with- Returns:
- true if valid, false otherwise
verifySignature
Verify using DSA-SHA1 ONLY.- Parameters:
signature- the signature to verifyhash- SHA-1 hash, NOT a SHA-256 hashverifyingKey- the public key to verify with- Returns:
- true if valid, false otherwise
verifySignature
Nonstandard. Used by Syndie.- Parameters:
signature- the signature to verifyhash- the pre-computed hash, of the length required by the key's typeverifyingKey- the public key to verify with- Returns:
- true if valid, false otherwise
verifySignature
public boolean verifySignature(Signature signature, SimpleDataStructure hash, SigningPublicKey verifyingKey) Generic signature type.Warning, nonstandard for EdDSA, double-hashes, not recommended.
- Parameters:
signature- the signature to verifyhash- SHA1Hash, Hash, Hash384, or Hash512verifyingKey- the public key to verify with- Returns:
- true if valid, false otherwise
verifySignature
Generic signature type. If you have a Java pubkey, use this, so you don't lose the key parameters, which may be different than the ones defined inSigType.Warning, nonstandard for EdDSA, double-hashes, not recommended.
- Parameters:
signature- the signature to verifyhash- SHA1Hash, Hash, Hash384, or Hash512pubKey- Java key- Returns:
- true if valid, false otherwise
verifySig
private boolean verifySig(Signature signature, SimpleDataStructure hash, SigningPublicKey verifyingKey) Verify using DSA-SHA1 or Syndie DSA-SHA256 ONLY.- Parameters:
signature- a DSA-SHA1 signature, the only type acceptedhash- either a Hash or a SHA1HashverifyingKey- a DSA-SHA1 public key, the only type accepted- Returns:
- true if valid, false otherwise
- Throws:
IllegalArgumentException- if the signature or the key is not DSA-SHA1
sign
Sign using any key type. Uses TheCrypto code unless configured to use the java.security libraries.- Parameters:
data- the data to signsigningKey- the private key matching the signature type- Returns:
- null on error
sign
Sign using any key type as of 0.9.12 (DSA-SHA1 only prior to that).- Parameters:
data- the data to signoffset- index within data of the first byte to signlength- number of bytes to sign, starting at offsetsigningKey- the private key matching the signature type- Returns:
- null on error
sign
Sign using DSA-SHA1 ONLY. Reads the stream until EOF. Does not close the stream.- Parameters:
in- the data to hash and sign, read to EOF but not closedsigningKey- the private key matching the signature type- Returns:
- null on error
sign
Sign using DSA-SHA1 ONLY.- Parameters:
hash- SHA-1 hash, NOT a SHA-256 hashsigningKey- the private key matching the signature type- Returns:
- null on error
sign
Nonstandard. Used by Syndie.- Parameters:
hash- the pre-computed hash, of the length required by the key's typesigningKey- the private key matching the signature type- Returns:
- null on error
sign
Generic signature type.Warning, nonstandard for EdDSA, double-hashes, not recommended.
- Parameters:
hash- SHA1Hash, Hash, Hash384, or Hash512signingKey- the private key matching the signature type- Returns:
- null on error
sign
Generic signature type. If you have a Java privkey, use this, so you don't lose the key parameters, which may be different than the ones defined in SigType.Warning, nonstandard for EdDSA, double-hashes, not recommended.
- Parameters:
hash- SHA1Hash, Hash, Hash384, or Hash512privKey- Java keytype- returns a Signature of this type- Returns:
- null on error
signIt
Sign using DSA-SHA1 or Syndie DSA-SHA256 ONLY.- Parameters:
hash- either a Hash or a SHA1HashsigningKey- a DSA-SHA1 private key, the only type accepted- Returns:
- the 40-byte signature (r then s), or null on error
- Throws:
IllegalArgumentException- if the key is not DSA-SHA1
calculateHash
Reads the stream until EOF. Does not close the stream. Prefer calculateHash(byte[], int, int) for byte array inputs.- Parameters:
in- the stream to read to EOF, not closed here- Returns:
- the SHA-1 hash, NOT a SHA-256 hash, or null on an IO error
calculateHash
Calculate a SHA-1 hash of the source data.- Parameters:
source- the data to hashoffset- the offset in the source datalen- the length of data to hash- Returns:
- the SHA-1 hash
altVerifySig
private boolean altVerifySig(Signature signature, byte[] data, int offset, int len, SigningPublicKey verifyingKey) throws GeneralSecurityException Generic verify any type.- Parameters:
signature- the signature to check, of any signature typedata- the data the signature was made overoffset- where the signed data starts in datalen- how many bytes of data to verifyverifyingKey- the public key to check against, of the same type as the signature- Returns:
- true if valid, false otherwise
- Throws:
GeneralSecurityException- if algorithm unavailable or on other errorsIllegalArgumentException- if the signature and key types differ
altVerifySigRaw
private boolean altVerifySigRaw(Signature signature, SimpleDataStructure hash, SigningPublicKey verifyingKey) throws GeneralSecurityException Generic raw verify any typeWarning, nonstandard for EdDSA, double-hashes, not recommended.
- Parameters:
signature- the signature to check, of any signature typehash- the pre-computed hash, of the signature type's hash lengthverifyingKey- the public key to check against, of the same type as the signature- Returns:
- true if valid, false otherwise
- Throws:
GeneralSecurityException- if algorithm unavailable or on other errorsIllegalArgumentException- if the signature and key types differ
altVerifySigRaw
private boolean altVerifySigRaw(Signature signature, SimpleDataStructure hash, PublicKey pubKey) throws GeneralSecurityException Generic raw verify any type. If you have a Java pubkey, use this, so you don't lose the key parameters, which may be different than the ones defined in SigType.Warning, nonstandard for EdDSA, double-hashes, not recommended.
- Parameters:
signature- the signature to check, of any signature typehash- the pre-computed hash, of the signature type's hash lengthpubKey- the public key as a Java key, with its own parameters intact- Returns:
- true if valid, false otherwise
- Throws:
GeneralSecurityException- if algorithm unavailable or on other errorsIllegalArgumentException- if the hash length does not match the signature type's hash length
altVerifySigSHA1
private boolean altVerifySigSHA1(Signature signature, byte[] data, int offset, int len, SigningPublicKey verifyingKey) throws GeneralSecurityException Alternate toverifySignature(Signature, byte[], int, int, SigningPublicKey)using java.security libraries.- Parameters:
signature- the DSA-SHA1 signature to checkdata- the data the signature was made overoffset- where the signed data starts in datalen- how many bytes of data to verifyverifyingKey- the DSA public key to check against- Returns:
- true if valid, false otherwise
- Throws:
GeneralSecurityException- if algorithm unavailable or on other errors
altSign
private Signature altSign(byte[] data, int offset, int len, SigningPrivateKey privateKey) throws GeneralSecurityException Generic sign any type.- Parameters:
data- the data to signoffset- where the data to sign starts in datalen- how many bytes of data to signprivateKey- the key to sign with, whose type selects the algorithm- Returns:
- the Signature, or null on error
- Throws:
GeneralSecurityException- if algorithm unavailable or on other errors
altSignRaw
private Signature altSignRaw(SimpleDataStructure hash, SigningPrivateKey privateKey) throws GeneralSecurityException Generic raw sign any type.Warning, nonstandard for EdDSA, double-hashes, not recommended.
- Parameters:
hash- SHA1Hash, Hash, Hash384, or Hash512privateKey- the key to sign with, whose type selects the algorithm- Returns:
- the signature over the hash, of the private key's type
- Throws:
GeneralSecurityException- if algorithm unavailable or on other errors
altSignRaw
private Signature altSignRaw(String algo, SimpleDataStructure hash, PrivateKey privKey, SigType type) throws GeneralSecurityException Generic raw sign any type.Warning, nonstandard for EdDSA, double-hashes, not recommended.
- Parameters:
algo- the raw JCA algorithm name, e.g.NONEwithDSA, ignored for EdDSA typeshash- SHA1Hash, Hash, Hash384, or Hash512privKey- the key to sign with, as a Java keytype- returns a Signature of this type- Returns:
- the signature over the hash, of the type named by algo
- Throws:
GeneralSecurityException- if algorithm unavailable or on other errorsIllegalArgumentException- if the hash length does not match the type's hash length
altSignSHA1
private Signature altSignSHA1(byte[] data, int offset, int len, SigningPrivateKey privateKey) throws GeneralSecurityException Alternate tosign(byte[], int, int, SigningPrivateKey)using java.security libraries.- Parameters:
data- the data to signoffset- where the data to sign starts in datalen- how many bytes of data to signprivateKey- the DSA private key to sign with- Returns:
- the DSA-SHA1 signature, non-null
- Throws:
GeneralSecurityException- if algorithm unavailable or on other errors
getRawAlgo
Map a signature type to the raw (prehashed) JCA algorithm name.- Parameters:
type- the signature type whose base algorithm selects the name- Returns:
- the JCA algorithm name, e.g.
NONEwithDSA - Throws:
UnsupportedOperationException- if the type's base algorithm has no raw form
getRawAlgo
Map a Java key to the raw (prehashed) JCA algorithm name.- Parameters:
key- the Java key whose class selects the algorithm name- Returns:
- the JCA algorithm name, e.g.
NONEwithDSA - Throws:
UnsupportedOperationException- if the key implements none of the four supported key interfaces