Search in sources :

Example 51 with DSAParams

use of java.security.interfaces.DSAParams in project Zom-Android by zom.

the class OtrInputStream method readSignature.

public byte[] readSignature(PublicKey pubKey) throws IOException {
    if (!pubKey.getAlgorithm().equals("DSA"))
        throw new UnsupportedOperationException();
    DSAPublicKey dsaPubKey = (DSAPublicKey) pubKey;
    DSAParams dsaParams = dsaPubKey.getParams();
    byte[] sig = new byte[dsaParams.getQ().bitLength() / 4];
    read(sig);
    return sig;
}
Also used : DSAParams(java.security.interfaces.DSAParams) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 52 with DSAParams

use of java.security.interfaces.DSAParams in project Zom-Android by zom.

the class OtrCryptoEngineImpl method verify.

private Boolean verify(byte[] b, PublicKey pubKey, BigInteger r, BigInteger s) throws OtrCryptoException {
    if (!(pubKey instanceof DSAPublicKey))
        throw new IllegalArgumentException();
    DSAParams dsaParams = ((DSAPublicKey) pubKey).getParams();
    BigInteger q = dsaParams.getQ();
    DSAParameters bcDSAParams = new DSAParameters(dsaParams.getP(), q, dsaParams.getG());
    DSAPublicKey dsaPrivateKey = (DSAPublicKey) pubKey;
    DSAPublicKeyParameters dsaPrivParms = new DSAPublicKeyParameters(dsaPrivateKey.getY(), bcDSAParams);
    // Ian: Note that if you can get the standard DSA implementation you're
    // using to not hash its input, you should be able to pass it ((256-bit
    // value) mod q), (rather than truncating the 256-bit value) and all
    // should be well.
    // ref: Interop problems with libotr - DSA signature
    DSASigner dsaSigner = new DSASigner();
    dsaSigner.init(false, dsaPrivParms);
    BigInteger bmpi = new BigInteger(1, b);
    Boolean result = dsaSigner.verifySignature(BigIntegers.asUnsignedByteArray(bmpi.mod(q)), r, s);
    return result;
}
Also used : DSAPublicKeyParameters(org.spongycastle.crypto.params.DSAPublicKeyParameters) DSASigner(org.spongycastle.crypto.signers.DSASigner) BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) DSAParameters(org.spongycastle.crypto.params.DSAParameters) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 53 with DSAParams

use of java.security.interfaces.DSAParams in project Zom-Android by zom.

the class OtrOutputStream method writePublicKey.

public void writePublicKey(PublicKey pubKey) throws IOException {
    if (!(pubKey instanceof DSAPublicKey))
        throw new UnsupportedOperationException("Key types other than DSA are not supported at the moment.");
    DSAPublicKey dsaKey = (DSAPublicKey) pubKey;
    writeShort(0);
    DSAParams dsaParams = dsaKey.getParams();
    writeBigInt(dsaParams.getP());
    writeBigInt(dsaParams.getQ());
    writeBigInt(dsaParams.getG());
    writeBigInt(dsaKey.getY());
}
Also used : DSAParams(java.security.interfaces.DSAParams) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 54 with DSAParams

use of java.security.interfaces.DSAParams in project Zom-Android by zom.

the class OtrAndroidKeyManagerImpl method regenerateLocalPublicKey.

public void regenerateLocalPublicKey(KeyFactory factory, String fullUserId, DSAPrivateKey privKey) {
    String userId = Address.stripResource(fullUserId);
    BigInteger x = privKey.getX();
    DSAParams params = privKey.getParams();
    BigInteger y = params.getG().modPow(x, params.getP());
    DSAPublicKeySpec keySpec = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG());
    PublicKey pubKey;
    try {
        pubKey = factory.generatePublic(keySpec);
        storeLocalPublicKey(userId, pubKey);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PublicKey(java.security.PublicKey) BigInteger(java.math.BigInteger) DSAParams(java.security.interfaces.DSAParams) OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 55 with DSAParams

use of java.security.interfaces.DSAParams in project Bytecoder by mirkosertic.

the class DSAKeyFactory method engineGetKeySpec.

/**
 * Returns a specification (key material) of the given key object
 * in the requested format.
 *
 * @param key the key
 *
 * @param keySpec the requested format in which the key material shall be
 * returned
 *
 * @return the underlying key specification (key material) in the
 * requested format
 *
 * @exception InvalidKeySpecException if the requested key specification is
 * inappropriate for the given key, or the given key cannot be processed
 * (e.g., the given key has an unrecognized algorithm or format).
 */
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
    DSAParams params;
    try {
        if (key instanceof java.security.interfaces.DSAPublicKey) {
            // Determine valid key specs
            Class<?> dsaPubKeySpec = Class.forName("java.security.spec.DSAPublicKeySpec");
            Class<?> x509KeySpec = Class.forName("java.security.spec.X509EncodedKeySpec");
            if (dsaPubKeySpec.isAssignableFrom(keySpec)) {
                java.security.interfaces.DSAPublicKey dsaPubKey = (java.security.interfaces.DSAPublicKey) key;
                params = dsaPubKey.getParams();
                return keySpec.cast(new DSAPublicKeySpec(dsaPubKey.getY(), params.getP(), params.getQ(), params.getG()));
            } else if (x509KeySpec.isAssignableFrom(keySpec)) {
                return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
            } else {
                throw new InvalidKeySpecException("Inappropriate key specification");
            }
        } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
            // Determine valid key specs
            Class<?> dsaPrivKeySpec = Class.forName("java.security.spec.DSAPrivateKeySpec");
            Class<?> pkcs8KeySpec = Class.forName("java.security.spec.PKCS8EncodedKeySpec");
            if (dsaPrivKeySpec.isAssignableFrom(keySpec)) {
                java.security.interfaces.DSAPrivateKey dsaPrivKey = (java.security.interfaces.DSAPrivateKey) key;
                params = dsaPrivKey.getParams();
                return keySpec.cast(new DSAPrivateKeySpec(dsaPrivKey.getX(), params.getP(), params.getQ(), params.getG()));
            } else if (pkcs8KeySpec.isAssignableFrom(keySpec)) {
                return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
            } else {
                throw new InvalidKeySpecException("Inappropriate key specification");
            }
        } else {
            throw new InvalidKeySpecException("Inappropriate key type");
        }
    } catch (ClassNotFoundException e) {
        throw new InvalidKeySpecException("Unsupported key specification: " + e.getMessage());
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAParams(java.security.interfaces.DSAParams) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

DSAParams (java.security.interfaces.DSAParams)66 DSAPublicKey (java.security.interfaces.DSAPublicKey)30 BigInteger (java.math.BigInteger)29 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)18 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)17 DSAParameterSpec (java.security.spec.DSAParameterSpec)16 PublicKey (java.security.PublicKey)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9 InvalidKeyException (java.security.InvalidKeyException)8 KeyFactory (java.security.KeyFactory)8 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)8 IOException (java.io.IOException)7 GeneralSecurityException (java.security.GeneralSecurityException)7 CertPathValidatorException (java.security.cert.CertPathValidatorException)7 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)7 RSAPublicKey (java.security.interfaces.RSAPublicKey)7 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)7 AlgorithmParameters (java.security.AlgorithmParameters)6 X509Certificate (java.security.cert.X509Certificate)6