javacard.security.ECKey Java Examples

The following examples show how to use javacard.security.ECKey. 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: Secp256k1.java    From SatochipApplet with GNU Affero General Public License v3.0 6 votes vote down vote up
public static boolean checkCurveParameters(ECKey eckey, byte[] tmpbuffer, short tmpoffset){
	
	eckey.getA(tmpbuffer, tmpoffset);
	if (0!=Util.arrayCompare(tmpbuffer, tmpoffset, SECP256K1, OFFSET_SECP256K1_a, (short)32))
		return false;
	eckey.getB(tmpbuffer, tmpoffset);
	if (0!=Util.arrayCompare(tmpbuffer, tmpoffset, SECP256K1, OFFSET_SECP256K1_b, (short)32))
		return false;
	eckey.getG(tmpbuffer, tmpoffset);
	if (0!=Util.arrayCompare(tmpbuffer, tmpoffset, SECP256K1, OFFSET_SECP256K1_G, (short)65))
		return false;
	eckey.getR(tmpbuffer, tmpoffset);
	if (0!=Util.arrayCompare(tmpbuffer, tmpoffset, SECP256K1, OFFSET_SECP256K1_R, (short)32))
		return false;
	eckey.getField(tmpbuffer, tmpoffset);
	if (0!=Util.arrayCompare(tmpbuffer, tmpoffset, SECP256K1, OFFSET_SECP256K1_P, (short)32))
		return false;
	if (eckey.getK()!= SECP256K1_K)
		return false;
	
	return true;
}
 
Example #2
Source File: ECP256.java    From gauss-key-card with Apache License 2.0 5 votes vote down vote up
public static void setCurveParameters(ECKey eckey) {
	eckey.setFieldFP(p, (short)0, (short)(p.length));
	eckey.setA(a, (short)0, (short)(a.length));
	eckey.setB(b, (short)0, (short)(b.length));
	eckey.setG(g, (short)0, (short)(g.length));
	eckey.setR(r, (short)0, (short)(r.length));
}
 
Example #3
Source File: Secp256r1.java    From CCU2F with Apache License 2.0 5 votes vote down vote up
protected static boolean setCommonCurveParameters(ECKey key) {
    try {
        key.setA(SECP256R1_A, (short)0, (short)SECP256R1_A.length);
        key.setB(SECP256R1_B, (short)0, (short)SECP256R1_B.length);
        key.setFieldFP(SECP256R1_FP, (short)0, (short)SECP256R1_FP.length);
        key.setG(SECP256R1_G, (short)0, (short)SECP256R1_G.length);
        key.setR(SECP256R1_R, (short)0, (short)SECP256R1_R.length);
        key.setK(SECP256R1_K);
        return true;
    }
    catch(Exception e) {
        return false;
    }

}
 
Example #4
Source File: FIDOCCImplementation.java    From CCU2F with Apache License 2.0 5 votes vote down vote up
public FIDOCCImplementation() {
	
	random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
	
    scratch = JCSystem.makeTransientByteArray((short)128, JCSystem.CLEAR_ON_DESELECT);
    //seed = new byte[64];
    
    keyPair = new KeyPair(
        (ECPublicKey)KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PUBLIC, KeyBuilder.LENGTH_EC_FP_256, false),
        (ECPrivateKey)KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PRIVATE, KeyBuilder.LENGTH_EC_FP_256, false));
    Secp256r1.setCommonCurveParameters((ECKey)keyPair.getPrivate());
    Secp256r1.setCommonCurveParameters((ECKey)keyPair.getPublic());
            
    // Initialize the unique seed for DRNG function 
    //random.generateData(seed, (short)0, (short)64);
    
    // Initialize the unique seed for DRNG function       
    drngSeed1 = (AESKey)KeyBuilderX.buildKey(KeyBuilderX.TYPE_AES_STATIC, KeyBuilder.LENGTH_AES_256, false);
    drngSeed2 = (AESKey)KeyBuilderX.buildKey(KeyBuilderX.TYPE_AES_STATIC, KeyBuilder.LENGTH_AES_256, false);
    random.generateData(scratch, (short)0, (short)32);
    drngSeed1.setKey(scratch, (short)0);
    random.generateData(scratch, (short)0, (short)32);
    drngSeed2.setKey(scratch, (short)0);
 
    sha256 = MessageDigest.getInstance(MessageDigest.ALG_SHA_256, false);
            
    // Initialize the unique keys for MAC function
    macKey1 = (AESKey)KeyBuilderX.buildKey(KeyBuilderX.TYPE_AES_STATIC, KeyBuilder.LENGTH_AES_128, false);
    macKey2 = (AESKey)KeyBuilderX.buildKey(KeyBuilderX.TYPE_AES_STATIC, KeyBuilder.LENGTH_AES_128, false);
    random.generateData(scratch, (short)0, (short)16);
    macKey1.setKey(scratch, (short)0);
    random.generateData(scratch, (short)0, (short)16);
    macKey2.setKey(scratch, (short)0);
    
    // Initialize ecMultiplier 
    ecMultiplyHelper = KeyAgreementX.getInstance(KeyAgreementX.ALG_EC_SVDP_DH_PLAIN_XY, false);
}
 
Example #5
Source File: SECP256k1.java    From status-keycard with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the SECP256k1 curve parameters to the given ECKey (public or private).
 *
 * @param key the key where the curve parameters must be set
 */
void setCurveParameters(ECKey key) {
  key.setA(SECP256K1_A, (short) 0x00, (short) SECP256K1_A.length);
  key.setB(SECP256K1_B, (short) 0x00, (short) SECP256K1_B.length);
  key.setFieldFP(SECP256K1_FP, (short) 0x00, (short) SECP256K1_FP.length);
  key.setG(SECP256K1_G, (short) 0x00, (short) SECP256K1_G.length);
  key.setR(SECP256K1_R, (short) 0x00, (short) SECP256K1_R.length);
  key.setK(SECP256K1_K);
}
 
Example #6
Source File: Secp256k1.java    From SatochipApplet with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void setCommonCurveParameters(ECKey eckey){
	eckey.setFieldFP( SECP256K1, OFFSET_SECP256K1_P, (short)32);
	eckey.setA( SECP256K1, OFFSET_SECP256K1_a, (short)32);
	eckey.setB( SECP256K1, OFFSET_SECP256K1_b, (short)32);
	eckey.setR( SECP256K1, OFFSET_SECP256K1_R, (short)32);
	eckey.setG( SECP256K1, OFFSET_SECP256K1_G, (short)65);
	eckey.setK( SECP256K1_K);
}
 
Example #7
Source File: Secp256r1.java    From ledger-u2f-javacard with Apache License 2.0 5 votes vote down vote up
protected static boolean setCommonCurveParameters(ECKey key) {
    try {
        key.setA(SECP256R1_A, (short) 0, (short) SECP256R1_A.length);
        key.setB(SECP256R1_B, (short) 0, (short) SECP256R1_B.length);
        key.setFieldFP(SECP256R1_FP, (short) 0, (short) SECP256R1_FP.length);
        key.setG(SECP256R1_G, (short) 0, (short) SECP256R1_G.length);
        key.setR(SECP256R1_R, (short) 0, (short) SECP256R1_R.length);
        key.setK(SECP256R1_K);
        return true;
    } catch (Exception e) {
        return false;
    }

}
 
Example #8
Source File: Secp256k1.java    From ledger-javacard with GNU Affero General Public License v3.0 5 votes vote down vote up
protected static boolean setCommonCurveParameters(ECKey key) {
	try {
		key.setA(SECP256K1_A, (short)0, (short)SECP256K1_A.length);
		key.setB(SECP256K1_B, (short)0, (short)SECP256K1_B.length);
		key.setFieldFP(SECP256K1_FP, (short)0, (short)SECP256K1_FP.length);
		key.setG(SECP256K1_G, (short)0, (short)SECP256K1_G.length);
		key.setR(SECP256K1_R, (short)0, (short)SECP256K1_R.length);
		key.setK(SECP256K1_K);
		return true;
	}
	catch(Exception e) {
		return false;
	}
	
}
 
Example #9
Source File: IsoApplet.java    From IsoApplet with GNU General Public License v3.0 4 votes vote down vote up
/**
 * \brief Initialize an EC key with the curve parameters from buf.
 *
 * \param buf The buffer containing the EC curve parameters. It must be TLV with the following format:
 * 				81 - prime
 * 				82 - coefficient A
 * 				83 - coefficient B
 * 				84 - base point G
 * 				85 - order
 * 				87 - cofactor
 *
 * \param bOff The offset at where the first entry is located.
 *
 * \param bLen The remaining length of buf.
 *
 * \param key The EC key to initialize.
 *
 * \throw NotFoundException Parts of the data needed to fully initialize
 *                          the key were missing.
 *
 * \throw InvalidArgumentsException The ASN.1 sequence was malformatted.
 */
private void initEcParams(byte[] buf, short bOff, short bLen, ECKey key) throws NotFoundException, InvalidArgumentsException {
    short pos = bOff;
    short len;

    /* Search for the prime */
    pos = UtilTLV.findTag(buf, bOff, bLen, (byte) 0x81);
    pos++;
    len = UtilTLV.decodeLengthField(buf, pos);
    pos += UtilTLV.getLengthFieldLength(len);
    key.setFieldFP(buf, pos, len); // "p"

    /* Search for coefficient A */
    pos = UtilTLV.findTag(buf, bOff, bLen, (byte) 0x82);
    pos++;
    len = UtilTLV.decodeLengthField(buf, pos);
    pos += UtilTLV.getLengthFieldLength(len);
    key.setA(buf, pos, len);

    /* Search for coefficient B */
    pos = UtilTLV.findTag(buf, bOff, bLen, (byte) 0x83);
    pos++;
    len = UtilTLV.decodeLengthField(buf, pos);
    pos += UtilTLV.getLengthFieldLength(len);
    key.setB(buf, pos, len);

    /* Search for base point G */
    pos = UtilTLV.findTag(buf, bOff, bLen, (byte) 0x84);
    pos++;
    len = UtilTLV.decodeLengthField(buf, pos);
    pos += UtilTLV.getLengthFieldLength(len);
    key.setG(buf, pos, len); // G(x,y)

    /* Search for order */
    pos = UtilTLV.findTag(buf, bOff, bLen, (byte) 0x85);
    pos++;
    len = UtilTLV.decodeLengthField(buf, pos);
    pos += UtilTLV.getLengthFieldLength(len);
    key.setR(buf, pos, len); // Order of G - "q"

    /* Search for cofactor */
    pos = UtilTLV.findTag(buf, bOff, bLen, (byte) 0x87);
    pos++;
    len = UtilTLV.decodeLengthField(buf, pos);
    pos += UtilTLV.getLengthFieldLength(len);
    if(len == 2) {
        key.setK(Util.getShort(buf, pos));
    } else if(len == 1) {
        key.setK(buf[pos]);
    } else {
        throw InvalidArgumentsException.getInstance();
    }
}