use of java.security.interfaces.DSAParams in project robovm by robovm.
the class DSAKeyFactoryImpl method engineGetKeySpec.
/**
* This method returns a specification for the supplied key.
*
* The specification will be returned in the form of an object of the type
* specified by keySpec.
*
* @param key -
* either DSAPrivateKey or DSAPublicKey
* @param keySpec -
* either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
*
* @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
*
* @throws InvalidKeySpecException
* if "keySpec" is not a specification for DSAPublicKey or
* DSAPrivateKey
*/
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
BigInteger p, q, g, x, y;
if (key != null) {
if (keySpec == null) {
throw new NullPointerException("keySpec == null");
}
if (key instanceof DSAPrivateKey) {
DSAPrivateKey privateKey = (DSAPrivateKey) key;
if (keySpec.equals(DSAPrivateKeySpec.class)) {
x = privateKey.getX();
DSAParams params = privateKey.getParams();
p = params.getP();
q = params.getQ();
g = params.getG();
return (T) (new DSAPrivateKeySpec(x, p, q, g));
}
if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
return (T) (new PKCS8EncodedKeySpec(key.getEncoded()));
}
throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec");
}
if (key instanceof DSAPublicKey) {
DSAPublicKey publicKey = (DSAPublicKey) key;
if (keySpec.equals(DSAPublicKeySpec.class)) {
y = publicKey.getY();
DSAParams params = publicKey.getParams();
p = params.getP();
q = params.getQ();
g = params.getG();
return (T) (new DSAPublicKeySpec(y, p, q, g));
}
if (keySpec.equals(X509EncodedKeySpec.class)) {
return (T) (new X509EncodedKeySpec(key.getEncoded()));
}
throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec");
}
}
throw new InvalidKeySpecException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
use of java.security.interfaces.DSAParams in project robovm by robovm.
the class DSAKeyFactoryImpl method engineTranslateKey.
/**
* The method generates a DSAPublicKey object from the provided key.
*
* @param
* key - a DSAPublicKey object or DSAPrivateKey object.
*
* @return
* object of the same type as the "key" argument
*
* @throws InvalidKeyException
* if "key" is neither DSAPublicKey nor DSAPrivateKey
*/
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
if (key != null) {
if (key instanceof DSAPrivateKey) {
DSAPrivateKey privateKey = (DSAPrivateKey) key;
DSAParams params = privateKey.getParams();
try {
return engineGeneratePrivate(new DSAPrivateKeySpec(privateKey.getX(), params.getP(), params.getQ(), params.getG()));
} catch (InvalidKeySpecException e) {
// Actually this exception shouldn't be thrown
throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
}
}
if (key instanceof DSAPublicKey) {
DSAPublicKey publicKey = (DSAPublicKey) key;
DSAParams params = publicKey.getParams();
try {
return engineGeneratePublic(new DSAPublicKeySpec(publicKey.getY(), params.getP(), params.getQ(), params.getG()));
} catch (InvalidKeySpecException e) {
// Actually this exception shouldn't be thrown
throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
}
}
}
throw new InvalidKeyException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
use of java.security.interfaces.DSAParams in project robovm by robovm.
the class SHA1withDSA_SignatureImpl method engineInitSign.
/**
* Initializes this signature object with PrivateKey object
* passed as argument to the method.
*
* @params
* privateKey DSAPrivateKey object
* @throws
* InvalidKeyException if privateKey is not DSAPrivateKey object
*/
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
DSAParams params;
// parameters and private key
BigInteger p, q, x;
int n;
if (privateKey == null || !(privateKey instanceof DSAPrivateKey)) {
throw new InvalidKeyException();
}
params = ((DSAPrivateKey) privateKey).getParams();
p = params.getP();
q = params.getQ();
x = ((DSAPrivateKey) privateKey).getX();
// checks described in DSA standard
n = p.bitLength();
if (p.compareTo(BigInteger.valueOf(1)) != 1 || n < 512 || n > 1024 || (n & 077) != 0) {
throw new InvalidKeyException("bad p");
}
if (q.signum() != 1 && q.bitLength() != 160) {
throw new InvalidKeyException("bad q");
}
if (x.signum() != 1 || x.compareTo(q) != -1) {
throw new InvalidKeyException("x <= 0 || x >= q");
}
dsaKey = (DSAKey) privateKey;
msgDigest.reset();
}
use of java.security.interfaces.DSAParams in project robovm by robovm.
the class SHA1withDSA_SignatureImpl method engineSign.
/**
* Returns signature bytes as byte array containing
* ASN1 representation for two BigInteger objects
* which is SEQUENCE of two INTEGERS.
* Length of sequence varies from less than 46 to 48.
*
* Resets object to the state it was in
* when previous call to either "initSign" method was called.
*
* @return
* byte array containing signature in ASN1 representation
* @throws
* SignatureException if object's state is not SIGN or
* signature algorithm cannot process data
*/
protected byte[] engineSign() throws SignatureException {
// names of below BigIntegers are the same as they are defined in DSA standard
BigInteger r = null;
BigInteger s = null;
BigInteger k = null;
// parameters and private key
BigInteger p, q, g, x;
// BigInteger for message digest
BigInteger digestBI;
// various byte array being used in computing signature
byte[] randomBytes;
byte[] rBytes;
byte[] sBytes;
byte[] signature;
int n, n1, n2;
DSAParams params;
if (appRandom == null) {
appRandom = new SecureRandom();
}
params = dsaKey.getParams();
p = params.getP();
q = params.getQ();
g = params.getG();
x = ((DSAPrivateKey) dsaKey).getX();
// forming signature according algorithm described in chapter 5 of DSA standard
digestBI = new BigInteger(1, msgDigest.digest());
randomBytes = new byte[20];
for (; ; ) {
appRandom.nextBytes(randomBytes);
k = new BigInteger(1, randomBytes);
if (k.compareTo(q) != -1) {
continue;
}
r = g.modPow(k, p).mod(q);
if (r.signum() == 0) {
continue;
}
s = k.modInverse(q).multiply(digestBI.add(x.multiply(r)).mod(q)).mod(q);
if (s.signum() != 0) {
break;
}
}
// forming signature's ASN1 representation which is SEQUENCE of two INTEGERs
//
rBytes = r.toByteArray();
n1 = rBytes.length;
if ((rBytes[0] & 0x80) != 0) {
n1++;
}
sBytes = s.toByteArray();
n2 = sBytes.length;
if ((sBytes[0] & 0x80) != 0) {
n2++;
}
// 48 is max. possible length of signature
signature = new byte[6 + n1 + n2];
// ASN1 SEQUENCE tag
signature[0] = (byte) 0x30;
// total length of two INTEGERs
signature[1] = (byte) (4 + n1 + n2);
// ASN1 INTEGER tag
signature[2] = (byte) 0x02;
// length of r
signature[3] = (byte) n1;
// ASN1 INTEGER tag
signature[4 + n1] = (byte) 0x02;
// length of s
signature[5 + n1] = (byte) n2;
if (n1 == rBytes.length) {
n = 4;
} else {
n = 5;
}
System.arraycopy(rBytes, 0, signature, n, rBytes.length);
if (n2 == sBytes.length) {
n = 6 + n1;
} else {
n = 7 + n1;
}
System.arraycopy(sBytes, 0, signature, n, sBytes.length);
return signature;
}
use of java.security.interfaces.DSAParams in project robovm by robovm.
the class KeyPairGeneratorTest method testDSAGeneratorWithParams.
public void testDSAGeneratorWithParams() throws Exception {
final DSAParameterSpec dsaSpec = new DSAParameterSpec(DSA_P, DSA_Q, DSA_G);
boolean failure = false;
final Provider[] providers = Security.getProviders();
for (final Provider p : providers) {
Service s = p.getService("KeyPairGenerator", "DSA");
if (s == null) {
continue;
}
final KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", p);
kpg.initialize(dsaSpec);
KeyPair pair = kpg.generateKeyPair();
DSAPrivateKey privKey = (DSAPrivateKey) pair.getPrivate();
DSAPublicKey pubKey = (DSAPublicKey) pair.getPublic();
DSAParams actualParams = privKey.getParams();
assertNotNull("DSA params should not be null", actualParams);
assertEquals("DSA P should be the same as supplied with provider " + p.getName(), DSA_P, actualParams.getP());
assertEquals("DSA Q should be the same as supplied with provider " + p.getName(), DSA_Q, actualParams.getQ());
assertEquals("DSA G should be the same as supplied with provider " + p.getName(), DSA_G, actualParams.getG());
actualParams = pubKey.getParams();
assertNotNull("DSA params should not be null", actualParams);
assertEquals("DSA P should be the same as supplied with provider " + p.getName(), DSA_P, actualParams.getP());
assertEquals("DSA Q should be the same as supplied with provider " + p.getName(), DSA_Q, actualParams.getQ());
assertEquals("DSA G should be the same as supplied with provider " + p.getName(), DSA_G, actualParams.getG());
}
}
Aggregations