本文整理汇总了Java中javacard.security.KeyBuilder.TYPE_EC_FP_PRIVATE属性的典型用法代码示例。如果您正苦于以下问题:Java KeyBuilder.TYPE_EC_FP_PRIVATE属性的具体用法?Java KeyBuilder.TYPE_EC_FP_PRIVATE怎么用?Java KeyBuilder.TYPE_EC_FP_PRIVATE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类javacard.security.KeyBuilder
的用法示例。
在下文中一共展示了KeyBuilder.TYPE_EC_FP_PRIVATE属性的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getUncompressedPublicPoint
@Override
public boolean getUncompressedPublicPoint(byte[] privateKey,
short privateKeyOffset, byte[] publicPoint, short publicPointOffset) {
if ((privateKey != null) && (keyAgreement != null)) {
try {
if (ecAlgorithm != KeyBuilder.TYPE_EC_FP_PRIVATE) {
Secp256k1.setCommonCurveParameters(this.privateKey);
}
this.privateKey.setS(privateKey, privateKeyOffset, (short)32);
keyAgreement.init(this.privateKey);
keyAgreement.generateSecret(Secp256k1.SECP256K1_G, (short)0, (short)Secp256k1.SECP256K1_G.length, publicPoint, publicPointOffset);
return true;
}
catch(Exception e) {
return false;
}
}
else {
return false;
}
}
示例2: getPublicKeyFromPrivate
/**
* This function returns the public key associated with a particular private key stored
* in the applet. The exact key blob contents depend on the key�s algorithm and type.
*
* ins: 0x35
* p1: private key number (0x00-0x0F)
* p2: 0x00
* data: none
* return(SECP256K1): [coordx_size(2b) | pubkey_coordx | sig_size(2b) | sig]
*/
private void getPublicKeyFromPrivate(APDU apdu, byte[] buffer) {
if (buffer[ISO7816.OFFSET_P2] != (byte) 0x00)
ISOException.throwIt(SW_INCORRECT_P2);
short bytesLeft = Util.makeShort((byte) 0x00, buffer[ISO7816.OFFSET_LC]);
if (bytesLeft != apdu.setIncomingAndReceive())
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
byte key_nb = buffer[ISO7816.OFFSET_P1];
if ((key_nb < 0) || (key_nb >= MAX_NUM_KEYS))
ISOException.throwIt(SW_INCORRECT_P1);
Key key = keys[key_nb];
if ((key == null) || !key.isInitialized())
ISOException.throwIt(SW_INCORRECT_P1);
// Enforce Access Control
if (!authorizeKeyOp(key_nb, ACL_READ))
ISOException.throwIt(SW_UNAUTHORIZED);
// check type
byte key_type = key.getType();
switch(key_type){
case KeyBuilder.TYPE_EC_FP_PRIVATE:
if (key.getSize()!= LENGTH_EC_FP_256)
ISOException.throwIt(SW_INCORRECT_ALG);
// check the curve param
if(!Secp256k1.checkCurveParameters((ECPrivateKey)key, recvBuffer, (short)0))
ISOException.throwIt(SW_INCORRECT_ALG);
// compute the corresponding partial public key...
keyAgreement.init((ECPrivateKey)key);
short coordx_size = keyAgreement.generateSecret(Secp256k1.SECP256K1, Secp256k1.OFFSET_SECP256K1_G, (short) 65, buffer, (short)2); // compute x coordinate of public key as k*G
Util.setShort(buffer, (short)0, coordx_size);
// sign fixed message
sigECDSA.init(key, Signature.MODE_SIGN);
short sign_size= sigECDSA.sign(buffer, (short)0, (short)(coordx_size+2), buffer, (short)(coordx_size+4));
Util.setShort(buffer, (short)(coordx_size+2), sign_size);
// return x-coordinate of public key+signature
// the client can recover full public-key from the signature or
// by guessing the compression value () and verifying the signature...
apdu.setOutgoingAndSend((short) 0, (short)(2+coordx_size+2+sign_size));
break;
default:
ISOException.throwIt(SW_INCORRECT_ALG);
}// end switch
}
示例3: signShortMessage
/**
* This function signs short Bitcoin message using std or Bip32 extended key in 1 APDU
*
* ins: 0x72
* p1: key number or 0xFF for the last derived Bip32 extended key
* p2: 0x00
* data: [msg_size(2b) | msg_data]
*
* return: [sig]
*
*/
private void signShortMessage(APDU apdu, byte[] buffer){
byte key_nb = buffer[ISO7816.OFFSET_P1];
if ( (key_nb!=(byte)0xFF) && ((key_nb < 0)||(key_nb >= MAX_NUM_KEYS)) ) // debug!!
ISOException.throwIt(SW_INCORRECT_P1);
if (buffer[ISO7816.OFFSET_P2] != (byte) 0x00)
ISOException.throwIt(SW_INCORRECT_P2);
short bytesLeft = Util.makeShort((byte) 0x00, buffer[ISO7816.OFFSET_LC]);
if (bytesLeft != apdu.setIncomingAndReceive())
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
// chek ACL
if (key_nb==(byte)0xFF && !authorizeKeyOp(bip32_extendedACL, ACL_USE))
ISOException.throwIt(SW_UNAUTHORIZED);
if (key_nb!=(byte)0xFF && !authorizeKeyOp(key_nb, ACL_USE))
ISOException.throwIt(SW_UNAUTHORIZED);
// check whether the seed is initialized
if (key_nb==(byte)0xFF && !bip32_seeded)
ISOException.throwIt(SW_BIP32_UNINITIALIZED_SEED);
// copy message header to tmp buffer
Util.arrayCopyNonAtomic(BITCOIN_SIGNED_MESSAGE_HEADER, (short)0, recvBuffer, (short)0, (short)BITCOIN_SIGNED_MESSAGE_HEADER.length);
short recvOffset= (short)BITCOIN_SIGNED_MESSAGE_HEADER.length;
// buffer data = [2-byte size | n-byte message to sign]
short offset= (short)ISO7816.OFFSET_CDATA;
short msgSize= Util.getShort(buffer, offset);
recvOffset+= Biginteger.encodeShortToVarInt(msgSize, recvBuffer, recvOffset);
offset+=2;
Util.arrayCopyNonAtomic(buffer, offset, recvBuffer, recvOffset, msgSize);
offset+= msgSize;
recvOffset+= msgSize;
// hash SHA-256
sha256.reset();
sha256.doFinal(recvBuffer, (short) 0, recvOffset, recvBuffer, (short) 0);
// set key & sign
if (key_nb==(byte)0xFF)
sigECDSA.init(bip32_extendedkey, Signature.MODE_SIGN);
else{
Key key= keys[key_nb];
if (key.getType()!=KeyBuilder.TYPE_EC_FP_PRIVATE)
ISOException.throwIt(SW_INCORRECT_ALG);
sigECDSA.init(key, Signature.MODE_SIGN);
}
short sign_size= sigECDSA.sign(recvBuffer, (short)0, (short)32, buffer, (short)0);
apdu.setOutgoingAndSend((short) 0, sign_size);
}
示例4: SignTransaction
/**
* This function signs the current hash transaction with a std or the last extended key
* The hash provided in the APDU is compared to the version stored inside the chip.
* Depending of the total amount in the transaction and the predefined limit,
* a HMAC must be provided as an additional security layer.
*
* ins: 0x6F
* p1: key number or 0xFF for the last derived Bip32 extended key
* p2: 0x00
* data: [hash(32b) | option:hmac(20b)]
*
* return: [sig ]
*
*/
private void SignTransaction(APDU apdu, byte[] buffer){
byte key_nb = buffer[ISO7816.OFFSET_P1];
if ( (key_nb!=(byte)0xFF) && ((key_nb < 0) || (key_nb >= MAX_NUM_KEYS)) )
ISOException.throwIt(SW_INCORRECT_P1);
short bytesLeft = Util.makeShort((byte) 0x00, buffer[ISO7816.OFFSET_LC]);
if (bytesLeft != apdu.setIncomingAndReceive())
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
if (bytesLeft<MessageDigest.LENGTH_SHA_256)
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
if (key_nb==(byte)0xFF && !authorizeKeyOp(bip32_extendedACL, ACL_USE))
ISOException.throwIt(SW_UNAUTHORIZED);
if (key_nb!=(byte)0xFF && !authorizeKeyOp(key_nb, ACL_USE))
ISOException.throwIt(SW_UNAUTHORIZED);
// check whether the seed is initialized
if (key_nb==(byte)0xFF && !bip32_seeded)
ISOException.throwIt(SW_BIP32_UNINITIALIZED_SEED);
// check doublehash value in buffer with cached singlehash value
sha256.reset();
sha256.doFinal(transactionData, OFFSET_TRANSACTION_HASH, MessageDigest.LENGTH_SHA_256, recvBuffer, (short)0);
if ((byte)0 != Util.arrayCompare(buffer, ISO7816.OFFSET_CDATA, recvBuffer, (short)0, MessageDigest.LENGTH_SHA_256))
ISOException.throwIt(SW_INCORRECT_TXHASH);
// check challenge-response answer if necessary
if( (option_flags & HMAC_CHALRESP_2FA)==HMAC_CHALRESP_2FA){
if( Biginteger.lessThan(transactionData, OFFSET_TRANSACTION_LIMIT, transactionData, OFFSET_TRANSACTION_AMOUNT, (short)8)){
if (bytesLeft<MessageDigest.LENGTH_SHA_256+MessageDigest.LENGTH_SHA+(short)2)
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
// check flag for 2fa_hmac_chalresp
short hmac_flags= Util.getShort(buffer, (short)(ISO7816.OFFSET_CDATA+32+2));
if (hmac_flags!=HMAC_CHALRESP_2FA)
ISOException.throwIt(SW_INCORRECT_ALG);
// hmac of 64-bytes msg: (doublesha256(raw_tx) | 32bytes padding)
Util.arrayFillNonAtomic(recvBuffer, (short)32, (short)32, (byte)0x00);
HmacSha160.computeHmacSha160(transactionData, OFFSET_TRANSACTION_HMACKEY, (short)20, recvBuffer, (short)0, (short)64, recvBuffer, (short)64);
if (Util.arrayCompare(buffer, (short)(ISO7816.OFFSET_CDATA+32+2), recvBuffer, (short)64, (short)20)!=0)
ISOException.throwIt(SW_SIGNATURE_INVALID);
// reset total amount
Util.arrayFillNonAtomic(transactionData, OFFSET_TRANSACTION_TOTAL, (short)8, (byte)0x00);
}
else{
//update total amount
Util.arrayCopyNonAtomic(transactionData, OFFSET_TRANSACTION_AMOUNT, transactionData, OFFSET_TRANSACTION_TOTAL, (short)8);
}
}
// hash+sign singlehash
if (key_nb==(byte)0xFF)
sigECDSA.init(bip32_extendedkey, Signature.MODE_SIGN);
else{
Key key= keys[key_nb];
if (key.getType()!=KeyBuilder.TYPE_EC_FP_PRIVATE)
ISOException.throwIt(SW_INCORRECT_ALG);
sigECDSA.init(key, Signature.MODE_SIGN);
}
short sign_size= sigECDSA.sign(transactionData, OFFSET_TRANSACTION_HASH, (short)32, buffer, (short)0);
apdu.setOutgoingAndSend((short) 0, sign_size);
}