Java Code Examples for javacard.security.KeyPair#ALG_EC_FP

The following examples show how to use javacard.security.KeyPair#ALG_EC_FP . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Command.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * @param keyPair   which keyPair/s (local/remote) to set curve domain parameters on
 * @param keyLength key length to choose
 * @param keyClass  key class to choose
 * @return a Command to send in order to prepare the curve on the keypairs.
 * @throws IOException if curve file cannot be found/opened
 */
public static Command prepareCurve(CardMngr cardManager, ECTesterReader.Config cfg, byte keyPair, short keyLength, byte keyClass) throws IOException {
    if (cfg.customCurve) {
        // Set custom curve (one of the SECG curves embedded applet-side)
        short domainParams = keyClass == KeyPair.ALG_EC_FP ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M;
        return new Command.Set(cardManager, keyPair, EC_Consts.getCurve(keyLength, keyClass), domainParams, null);
    }

    EC_Curve curve = findCurve(cfg, keyLength, keyClass);
    if ((curve == null || curve.flatten() == null) && (cfg.namedCurve != null || cfg.curveFile != null)) {
        if (cfg.namedCurve != null) {
            throw new IOException("Couldn't read named curve data.");
        }
        throw new IOException("Couldn't read the curve file correctly.");
    } else if (curve == null) {
        return null;
    }
    return new Command.Set(cardManager, keyPair, EC_Consts.CURVE_external, curve.getParams(), curve.flatten());
}
 
Example 2
Source File: EC_Curve.java    From ECTester with MIT License 6 votes vote down vote up
public EllipticCurve toCurve() {
    ECField field;
    if (this.field == KeyPair.ALG_EC_FP) {
        field = new ECFieldFp(new BigInteger(1, getData(0)));
    } else {
        byte[][] fieldData = getParam(EC_Consts.PARAMETER_F2M);
        int m = ByteUtil.getShort(fieldData[0], 0);
        int e1 = ByteUtil.getShort(fieldData[1], 0);
        int e2 = ByteUtil.getShort(fieldData[2], 0);
        int e3 = ByteUtil.getShort(fieldData[3], 0);
        int[] powers;
        if (e2 == 0 && e3 == 0) {
            powers = new int[]{e1};
        } else {
            powers = new int[]{e1, e2, e3};
        }
        field = new ECFieldF2m(m, powers);
    }

    BigInteger a = new BigInteger(1, getParam(EC_Consts.PARAMETER_A)[0]);
    BigInteger b = new BigInteger(1, getParam(EC_Consts.PARAMETER_B)[0]);

    return new EllipticCurve(field, a, b);
}
 
Example 3
Source File: Command.java    From ECTester with MIT License 5 votes vote down vote up
@Override
public String getDescription() {
    String field = keyClass == KeyPair.ALG_EC_FP ? "ALG_EC_FP" : "ALG_EC_F2M";
    String key;
    if (keyPair == ECTesterApplet.KEYPAIR_BOTH) {
        key = "both keypairs";
    } else {
        key = ((keyPair == ECTesterApplet.KEYPAIR_LOCAL) ? "local" : "remote") + " keypair";
    }
    return String.format("Allocate %s %db %s", key, keyLength, field);
}
 
Example 4
Source File: CardUtil.java    From ECTester with MIT License 5 votes vote down vote up
public static String getKeyTypeString(byte keyClass) {
    switch (keyClass) {
        case KeyPair.ALG_EC_FP:
            return "ALG_EC_FP";
        case KeyPair.ALG_EC_F2M:
            return "ALG_EC_F2M";
        default:
            return "";
    }
}
 
Example 5
Source File: CardCompressionSuite.java    From ECTester with MIT License 4 votes vote down vote up
private void runCompression(byte field) throws Exception {
    short[] keySizes = field == KeyPair.ALG_EC_FP ? EC_Consts.FP_SIZES : EC_Consts.F2M_SIZES;
    short domain = field == KeyPair.ALG_EC_FP ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M;

    for (short keyLength : keySizes) {
        String spec = keyLength + "b " + CardUtil.getKeyTypeString(field);
        byte curveId = EC_Consts.getCurve(keyLength, field);

        Test allocateFirst = runTest(CommandTest.expect(new Command.Allocate(this.card, ECTesterApplet.KEYPAIR_BOTH, keyLength, field), Result.ExpectedValue.SUCCESS));
        if (!allocateFirst.ok()) {
            doTest(CompoundTest.all(Result.ExpectedValue.SUCCESS, "No support for compression test on " + spec + ".", allocateFirst));
            continue;
        }

        List<Test> compressionTests = new LinkedList<>();
        compressionTests.add(allocateFirst);
        Test setCustom = runTest(CommandTest.expect(new Command.Set(this.card, ECTesterApplet.KEYPAIR_BOTH, curveId, domain, null), Result.ExpectedValue.SUCCESS));
        Test genCustom = runTest(CommandTest.expect(new Command.Generate(this.card, ECTesterApplet.KEYPAIR_BOTH), Result.ExpectedValue.SUCCESS));
        compressionTests.add(setCustom);
        compressionTests.add(genCustom);

        Response.Export key = new Command.Export(this.card, ECTesterApplet.KEYPAIR_REMOTE, EC_Consts.KEY_PUBLIC, EC_Consts.PARAMETER_W).send();
        byte[] pubkey = key.getParameter(ECTesterApplet.KEYPAIR_REMOTE, EC_Consts.KEY_PUBLIC);
        EC_Curve secgCurve = EC_Store.getInstance().getObject(EC_Curve.class, "secg", CardUtil.getCurveName(curveId));
        ECPoint pub;
        try {
            pub = ECUtil.fromX962(pubkey, secgCurve.toCurve());
        } catch (IllegalArgumentException iae) {
            doTest(CompoundTest.all(Result.ExpectedValue.SUCCESS, "", compressionTests.toArray(new Test[0])));
            continue;
        }

        List<Test> kaTests = new LinkedList<>();
        for (byte kaType : EC_Consts.KA_TYPES) {
            List<Test> thisTests = new LinkedList<>();
            Test allocate = runTest(CommandTest.expect(new Command.AllocateKeyAgreement(this.card, kaType), Result.ExpectedValue.SUCCESS));
            if (allocate.ok()) {
                Test ka = runTest(CommandTest.expect(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_NONE, kaType), Result.ExpectedValue.SUCCESS));

                thisTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "KeyAgreement setup and basic test.", allocate, ka));
                if (ka.ok()) {
                    // tests of the good stuff
                    Test kaCompressed = CommandTest.expect(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_COMPRESS, kaType), Result.ExpectedValue.SUCCESS);
                    Test kaHybrid = CommandTest.expect(new Command.ECDH(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.KEYPAIR_REMOTE, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_COMPRESS_HYBRID, kaType), Result.ExpectedValue.SUCCESS);
                    thisTests.add(CompoundTest.any(Result.ExpectedValue.SUCCESS, "Tests of compressed and hybrid form.", kaCompressed, kaHybrid));

                    // tests the bad stuff here
                    byte[] pubHybrid = ECUtil.toX962Hybrid(pub, keyLength);
                    pubHybrid[pubHybrid.length - 1] ^= 1;
                    byte[] pubHybridEncoded = ByteUtil.prependLength(pubHybrid);
                    Test kaBadHybrid = CommandTest.expect(new Command.ECDH_direct(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_NONE, kaType, pubHybridEncoded), Result.ExpectedValue.FAILURE);

                    byte[] pubInfinityEncoded = {0x01, 0x00};
                    Test kaBadInfinity = CommandTest.expect(new Command.ECDH_direct(this.card, ECTesterApplet.KEYPAIR_LOCAL, ECTesterApplet.EXPORT_FALSE, EC_Consts.TRANSFORMATION_NONE, kaType, pubInfinityEncoded), Result.ExpectedValue.FAILURE);
                    thisTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Tests of corrupted hybrid form and infinity.", kaBadHybrid, kaBadInfinity));
                }
                kaTests.add(CompoundTest.all(Result.ExpectedValue.SUCCESS, "KeyAgreement tests of " + CardUtil.getKATypeString(kaType) + ".", thisTests.toArray(new Test[0])));
            }
        }
        compressionTests.addAll(kaTests);
        if (cfg.cleanup) {
            compressionTests.add(CommandTest.expect(new Command.Cleanup(this.card), Result.ExpectedValue.ANY));
        }

        doTest(CompoundTest.all(Result.ExpectedValue.SUCCESS, "Compression test of " + spec + ".", compressionTests.toArray(new Test[0])));
    }
}
 
Example 6
Source File: EC_Curve.java    From ECTester with MIT License 4 votes vote down vote up
/**
 * @param bits
 * @param field KeyPair.ALG_EC_FP or KeyPair.ALG_EC_F2M
 */
public EC_Curve(short bits, byte field) {
    super(field == KeyPair.ALG_EC_FP ? EC_Consts.PARAMETERS_DOMAIN_FP : EC_Consts.PARAMETERS_DOMAIN_F2M);
    this.bits = bits;
    this.field = field;
}
 
Example 7
Source File: EC_Curve.java    From ECTester with MIT License 4 votes vote down vote up
@Override
public String toString() {
    return "<" + getId() + "> " + (field == KeyPair.ALG_EC_FP ? "Prime" : "Binary") + " field Elliptic curve (" + String.valueOf(bits) + "b)" + (desc == null ? "" : ": " + desc) + System.lineSeparator() + super.toString();
}
 
Example 8
Source File: EC_Curve.java    From ECTester with MIT License 4 votes vote down vote up
public static EC_Curve fromSpec(ECParameterSpec spec) {
    EllipticCurve curve = spec.getCurve();
    ECField field = curve.getField();

    short bits = (short) field.getFieldSize();
    byte[][] params;
    int paramIndex = 0;
    byte fieldType;
    if (field instanceof ECFieldFp) {
        ECFieldFp primeField = (ECFieldFp) field;
        params = new byte[7][];
        params[paramIndex++] = primeField.getP().toByteArray();
        fieldType = KeyPair.ALG_EC_FP;
    } else if (field instanceof ECFieldF2m) {
        ECFieldF2m binaryField = (ECFieldF2m) field;
        params = new byte[10][];
        params[paramIndex] = new byte[2];
        ByteUtil.setShort(params[paramIndex++], 0, (short) binaryField.getM());
        int[] powers = binaryField.getMidTermsOfReductionPolynomial();
        for (int i = 0; i < 3; ++i) {
            params[paramIndex] = new byte[2];
            short power = (i < powers.length) ? (short) powers[i] : 0;
            ByteUtil.setShort(params[paramIndex++], 0, power);
        }
        fieldType = KeyPair.ALG_EC_F2M;
    } else {
        throw new IllegalArgumentException("ECParameterSpec with an unknown field.");
    }

    ECPoint generator = spec.getGenerator();

    params[paramIndex++] = curve.getA().toByteArray();
    params[paramIndex++] = curve.getB().toByteArray();

    params[paramIndex++] = generator.getAffineX().toByteArray();
    params[paramIndex++] = generator.getAffineY().toByteArray();

    params[paramIndex++] = spec.getOrder().toByteArray();
    params[paramIndex] = new byte[2];
    ByteUtil.setShort(params[paramIndex], 0, (short) spec.getCofactor());

    EC_Curve result = new EC_Curve(bits, fieldType);
    result.readByteArray(params);
    return result;
}
 
Example 9
Source File: EC_Consts.java    From ECTester with MIT License 4 votes vote down vote up
public static byte getCurve(short keyLength, byte keyClass) {
    if (keyClass == KeyPair.ALG_EC_FP) {
        switch (keyLength) {
            case (short) 112:
                return CURVE_secp112r1;
            case (short) 128:
                return CURVE_secp128r1;
            case (short) 160:
                return CURVE_secp160r1;
            case (short) 192:
                return CURVE_secp192r1;
            case (short) 224:
                return CURVE_secp224r1;
            case (short) 256:
                return CURVE_secp256r1;
            case (short) 384:
                return CURVE_secp384r1;
            case (short) 521:
                return CURVE_secp521r1;
            default:
                ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
        }
    } else if (keyClass == KeyPair.ALG_EC_F2M) {
        switch (keyLength) {
            case (short) 163:
                return CURVE_sect163r1;
            case (short) 233:
                return CURVE_sect233r1;
            case (short) 283:
                return CURVE_sect283r1;
            case (short) 409:
                return CURVE_sect409r1;
            case (short) 571:
                return CURVE_sect571r1;
            default:
                ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
        }
    } else {
        ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
    }
    return 0;
}
 
Example 10
Source File: EC_Consts.java    From ECTester with MIT License 4 votes vote down vote up
public static byte getCurveType(byte curve) {
    return curve <= FP_CURVES ? KeyPair.ALG_EC_FP : KeyPair.ALG_EC_F2M;
}