Search in sources :

Example 31 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project benchmark by seelunzi.

the class Wallet method generateKeyPair.

/**
 * 使用ECC(椭圆曲线算法)生成公私钥
 */
public void generateKeyPair() {
    try {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECDSA", "BC");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        ECGenParameterSpec ecSpec = new ECGenParameterSpec("prime192v1");
        keyPairGenerator.initialize(ecSpec, random);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        privateKey = keyPair.getPrivate();
        publicKey = keyPair.getPublic();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ECGenParameterSpec(java.security.spec.ECGenParameterSpec)

Example 32 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project karaf by apache.

the class PublickeyLoginModule method equals.

public static boolean equals(PublicKey key, String storedKey) throws FailedLoginException {
    try {
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(storedKey)));
        String identifier = readString(dis);
        if (key instanceof DSAPublicKey) {
            if (!"ssh-dss".equals(identifier)) {
                return false;
            }
            BigInteger p = readBigInteger(dis);
            BigInteger q = readBigInteger(dis);
            BigInteger g = readBigInteger(dis);
            BigInteger y = readBigInteger(dis);
            KeyFactory keyFactory = KeyFactory.getInstance("DSA");
            KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
            PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
            return key.equals(generatedPublicKey);
        } else if (key instanceof RSAKey) {
            if (!"ssh-rsa".equals(identifier)) {
                return false;
            }
            BigInteger exponent = readBigInteger(dis);
            BigInteger modulus = readBigInteger(dis);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            KeySpec publicKeySpec = new RSAPublicKeySpec(modulus, exponent);
            PublicKey generatedPublicKey = keyFactory.generatePublic(publicKeySpec);
            return key.equals(generatedPublicKey);
        } else if (key instanceof ECPublicKey) {
            String ecIdentifier = readString(dis);
            if (!identifier.equals("ecdsa-sha2-" + ecIdentifier) || !nistSecMap.containsKey(ecIdentifier)) {
                return false;
            }
            // Overall size of the x + y coordinates. We only support uncompressed points here, so
            // to read x + y we ignore the "04" byte using (size - 1) / 2
            int size = dis.readInt();
            byte[] bytes = new byte[(size - 1) / 2];
            dis.skipBytes(1);
            dis.read(bytes, 0, bytes.length);
            BigInteger x = new BigInteger(bytes);
            dis.read(bytes, 0, bytes.length);
            BigInteger y = new BigInteger(bytes);
            KeyFactory keyFactory = KeyFactory.getInstance("EC");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
            parameters.init(new ECGenParameterSpec(nistSecMap.get(ecIdentifier)));
            ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
            ECPoint pubPoint = new ECPoint(x, y);
            KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
            PublicKey generatedPublicKey = keyFactory.generatePublic(keySpec);
            return key.equals(generatedPublicKey);
        } else {
            throw new FailedLoginException("Unsupported key type " + key.getClass().toString());
        }
    } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException e) {
        throw new FailedLoginException("Unable to check public key");
    }
}
Also used : RSAKey(java.security.interfaces.RSAKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) KeyFactory(java.security.KeyFactory) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ECPoint(java.security.spec.ECPoint) ECPoint(java.security.spec.ECPoint) DSAPublicKey(java.security.interfaces.DSAPublicKey) FailedLoginException(javax.security.auth.login.FailedLoginException) ECPublicKey(java.security.interfaces.ECPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 33 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project karaf by apache.

the class PublicKeyLoginModuleTest method testECLogin.

@Test
public void testECLogin() throws Exception {
    Properties options = getLoginModuleOptions();
    PublickeyLoginModule module = new PublickeyLoginModule();
    Subject subject = new Subject();
    String x = "-29742501866672735446035294501787338870744851402037490785638836399245997090445";
    String y = "-47637824304079393691947094099226900728731860400898598466261954347951527449659";
    KeyFactory keyFactory = KeyFactory.getInstance("EC");
    AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
    parameters.init(new ECGenParameterSpec("secp256r1"));
    ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
    ECPoint pubPoint = new ECPoint(new BigInteger(x), new BigInteger(y));
    KeySpec keySpec = new ECPublicKeySpec(pubPoint, ecParameters);
    PublicKey publicKey = keyFactory.generatePublic(keySpec);
    module.initialize(subject, new NamePubkeyCallbackHandler("ec", publicKey), null, options);
    assertEquals("Precondition", 0, subject.getPrincipals().size());
    assertTrue(module.login());
    assertTrue(module.commit());
    assertFalse(subject.getPrincipals().isEmpty());
    assertThat("ec", isIn(names(subject.getPrincipals(UserPrincipal.class))));
    assertThat("ssh", isIn(names(subject.getPrincipals(RolePrincipal.class))));
    assertTrue(module.logout());
    assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
}
Also used : PublicKey(java.security.PublicKey) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) Properties(org.apache.felix.utils.properties.Properties) ECPoint(java.security.spec.ECPoint) Subject(javax.security.auth.Subject) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) NamePubkeyCallbackHandler(org.apache.karaf.jaas.modules.NamePubkeyCallbackHandler) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) KeyFactory(java.security.KeyFactory) AlgorithmParameters(java.security.AlgorithmParameters) Test(org.junit.Test)

Example 34 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project wycheproof by google.

the class EcdsaTest method testBasic.

/**
   * This test checks the basic functionality of ECDSA. It can also be used to generate simple test
   * vectors.
   */
public void testBasic() throws Exception {
    String algorithm = "SHA256WithECDSA";
    String hashAlgorithm = "SHA-256";
    String message = "Hello";
    String curve = "secp256r1";
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
    ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
    keyGen.initialize(ecSpec);
    KeyPair keyPair = keyGen.generateKeyPair();
    ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
    ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
    byte[] messageBytes = message.getBytes("UTF-8");
    Signature signer = Signature.getInstance(algorithm);
    Signature verifier = Signature.getInstance(algorithm);
    signer.initSign(priv);
    signer.update(messageBytes);
    byte[] signature = signer.sign();
    verifier.initVerify(pub);
    verifier.update(messageBytes);
    assertTrue(verifier.verify(signature));
    // Extract some parameters.
    byte[] rawHash = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
    ECParameterSpec params = priv.getParams();
    // Print keys and signature, so that it can be used to generate new test vectors.
    System.out.println("Message:" + message);
    System.out.println("Hash:" + TestUtil.bytesToHex(rawHash));
    System.out.println("Curve:" + curve);
    System.out.println("Order:" + params.getOrder().toString());
    System.out.println("Private key:");
    System.out.println("S:" + priv.getS().toString());
    System.out.println("encoded:" + TestUtil.bytesToHex(priv.getEncoded()));
    System.out.println("Public key:");
    ECPoint w = pub.getW();
    System.out.println("X:" + w.getAffineX().toString());
    System.out.println("Y:" + w.getAffineY().toString());
    System.out.println("encoded:" + TestUtil.bytesToHex(pub.getEncoded()));
    System.out.println("Signature:" + TestUtil.bytesToHex(signature));
    System.out.println("r:" + extractR(signature).toString());
    System.out.println("s:" + extractS(signature).toString());
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) KeyPair(java.security.KeyPair) ECPublicKey(java.security.interfaces.ECPublicKey) ECParameterSpec(java.security.spec.ECParameterSpec) Signature(java.security.Signature) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) ECPoint(java.security.spec.ECPoint)

Example 35 with ECGenParameterSpec

use of java.security.spec.ECGenParameterSpec in project wycheproof by google.

the class EciesTest method testIsAlias.

/**
   * Tests whether algorithmA is an alias of algorithmB by encrypting with algorithmA and decrypting
   * with algorithmB.
   */
@SuppressWarnings("InsecureCryptoUsage")
public void testIsAlias(String algorithmA, String algorithmB) throws Exception {
    Cipher eciesA;
    Cipher eciesB;
    // Allowing tests to be skipped, because we don't want to encourage abbreviations.
    try {
        eciesA = Cipher.getInstance(algorithmA);
    } catch (NoSuchAlgorithmException ex) {
        System.out.println("Skipping because of:" + ex.toString());
        return;
    }
    try {
        eciesB = Cipher.getInstance(algorithmB);
    } catch (NoSuchAlgorithmException ex) {
        System.out.println("Skipping because of:" + ex.toString());
        return;
    }
    ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
    KeyPairGenerator kf = KeyPairGenerator.getInstance("EC");
    kf.initialize(ecSpec);
    KeyPair keyPair = kf.generateKeyPair();
    byte[] message = "Hello".getBytes("UTF-8");
    eciesA.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
    byte[] ciphertext = eciesA.doFinal(message);
    eciesB.init(Cipher.DECRYPT_MODE, keyPair.getPrivate(), eciesB.getParameters());
    byte[] decrypted = eciesB.doFinal(ciphertext);
    assertEquals(TestUtil.bytesToHex(message), TestUtil.bytesToHex(decrypted));
}
Also used : KeyPair(java.security.KeyPair) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyPairGenerator(java.security.KeyPairGenerator)

Aggregations

ECGenParameterSpec (java.security.spec.ECGenParameterSpec)67 KeyPairGenerator (java.security.KeyPairGenerator)31 AlgorithmParameters (java.security.AlgorithmParameters)23 KeyPair (java.security.KeyPair)23 BigInteger (java.math.BigInteger)19 PublicKey (java.security.PublicKey)19 ECPublicKey (java.security.interfaces.ECPublicKey)17 ECParameterSpec (java.security.spec.ECParameterSpec)15 ECPoint (java.security.spec.ECPoint)15 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)13 Test (org.junit.Test)13 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 KeySpec (java.security.spec.KeySpec)11 ECPrivateKey (java.security.interfaces.ECPrivateKey)9 PrivateKey (java.security.PrivateKey)8 SecureRandom (java.security.SecureRandom)8 Cipher (javax.crypto.Cipher)8 GeneralSecurityException (java.security.GeneralSecurityException)7 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)7