本文整理汇总了Java中javacard.security.KeyBuilder类的典型用法代码示例。如果您正苦于以下问题:Java KeyBuilder类的具体用法?Java KeyBuilder怎么用?Java KeyBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
KeyBuilder类属于javacard.security包,在下文中一共展示了KeyBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: FIDOCCImplementation
import javacard.security.KeyBuilder; //导入依赖的package包/类
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);
}
示例2: OpenPGPSecureMessaging
import javacard.security.KeyBuilder; //导入依赖的package包/类
/**
* Construct a new secure messaging wrapper.
*/
public OpenPGPSecureMessaging() {
ssc = JCSystem.makeTransientByteArray(SSC_SIZE,
JCSystem.CLEAR_ON_DESELECT);
tmp = JCSystem.makeTransientByteArray(TMP_SIZE,
JCSystem.CLEAR_ON_DESELECT);
signer = Signature.getInstance(
Signature.ALG_DES_MAC8_ISO9797_1_M2_ALG3, false);
verifier = Signature.getInstance(
Signature.ALG_DES_MAC8_ISO9797_1_M2_ALG3, false);
cipher = Cipher.getInstance(
Cipher.ALG_DES_CBC_ISO9797_M2, false);
decipher = Cipher.getInstance(
Cipher.ALG_DES_CBC_ISO9797_M2, false);
keyMAC = (DESKey) KeyBuilder.buildKey(
KeyBuilder.TYPE_DES_TRANSIENT_DESELECT,
KeyBuilder.LENGTH_DES3_2KEY, false);
keyENC = (DESKey) KeyBuilder.buildKey(
KeyBuilder.TYPE_DES_TRANSIENT_DESELECT,
KeyBuilder.LENGTH_DES3_2KEY, false);
ssc_set = JCSystem.makeTransientBooleanArray((short)1, JCSystem.CLEAR_ON_DESELECT);
ssc_set[0] = false;
}
示例3: authenticateGeneralReplayAttack
import javacard.security.KeyBuilder; //导入依赖的package包/类
@Test
public void authenticateGeneralReplayAttack() {
byte[] challenge, challengeresponse = new byte[8];
byte[] key = DatatypeConverter.parseHexBinary("010203040506070801020304050607080102030405060708");
Cipher cipherDES = Cipher.getInstance(Cipher.ALG_DES_CBC_NOPAD, false);
DESKey deskey = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_3KEY, false);
deskey.setKey(key, (short) 0);
// select admin key
execute("00 22 81 A4 03 83 01 80");
// get a challenge
ResponseAPDU response = execute("00 87 00 00 04 7C 02 81 00 00");
if (!Arrays.equals(Arrays.copyOfRange(response.getBytes(), 0, 4), new byte[] {0x7C,0x0A,(byte) 0x81,0x08})) {
fail("not a challenge:" + DatatypeConverter.printHexBinary(response.getBytes()));
}
// compute the response
challenge = Arrays.copyOfRange(response.getBytes(), 4, 12);
//solve challenge
cipherDES.init(deskey, Cipher.MODE_ENCRYPT);
cipherDES.doFinal(challenge, (short) 0, (short)8, challengeresponse, (short) 0);
// send the response
execute("00 87 00 00 0C 7C 0A 82 08" + DatatypeConverter.printHexBinary(challengeresponse), 0x9000);
execute("00 87 00 00 0C 7C 0A 82 08" + DatatypeConverter.printHexBinary(challengeresponse), 0x6985);
}
示例4: authenticateGeneral
import javacard.security.KeyBuilder; //导入依赖的package包/类
protected void authenticateGeneral(byte[] key, boolean successexpected) {
byte[] challenge, challengeresponse = new byte[8];
Cipher cipherDES = Cipher.getInstance(Cipher.ALG_DES_CBC_NOPAD, false);
DESKey deskey = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_3KEY, false);
deskey.setKey(key, (short) 0);
// select admin key
execute("00 22 81 A4 03 83 01 80");
// get a challenge
ResponseAPDU response = execute("00 87 00 00 04 7C 02 81 00 00");
if (!Arrays.equals(Arrays.copyOfRange(response.getBytes(), 0, 4), new byte[] {0x7C,0x0A,(byte) 0x81,0x08})) {
fail("not a challenge:" + DatatypeConverter.printHexBinary(response.getBytes()));
}
// compute the response
challenge = Arrays.copyOfRange(response.getBytes(), 4, 12);
//solve challenge
cipherDES.init(deskey, Cipher.MODE_ENCRYPT);
cipherDES.doFinal(challenge, (short) 0, (short)8, challengeresponse, (short) 0);
// send the response
execute("00 87 00 00 0C 7C 0A 82 08" + DatatypeConverter.printHexBinary(challengeresponse), (successexpected?0x9000: 0x6982));
}
示例5: getKey
import javacard.security.KeyBuilder; //导入依赖的package包/类
/**
* Retrieves the Key object to be used w/ the specified key number, key type
* (KEY_XX) and size. If exists, check it has the proper key type If not,
* creates it.
*
* @return Retrieved Key object or throws SW_UNATUTHORIZED,
* SW_OPERATION_NOT_ALLOWED
*/
private Key getKey(byte key_nb, byte key_type, short key_size) {
if (keys[key_nb] == null) {
// We have to create the Key
/* Check that Identity n.0 is logged */
if ((create_key_ACL == (byte) 0xFF)
|| (((logged_ids & create_key_ACL) == (short) 0x0000) && (create_key_ACL != (byte) 0x00)))
ISOException.throwIt(SW_UNAUTHORIZED);
keys[key_nb] = KeyBuilder.buildKey(key_type, key_size, false);
} else {
// Key already exists: check size & type
/*
* TODO: As an option, we could just discard and recreate if not of
* the correct type, but creates trash objects
*/
if ((keys[key_nb].getSize() != key_size) || (keys[key_nb].getType() != key_type))
ISOException.throwIt(SW_OPERATION_NOT_ALLOWED);
}
return keys[key_nb];
}
示例6: testBip32Gen
import javacard.security.KeyBuilder; //导入依赖的package包/类
private static void testBip32Gen(byte[] apduBuffer) {
byte lastStatus = STATUS_FAILED_GENERIC;
byte flags = (byte)0x00;
try {
if ((nativeHmacSha512 != null) && (hmacKey != null)) { hmacKey.setKey(BITCOIN_SEED, (short)0, (short)BITCOIN_SEED.length); if ((proprietaryAPI != null) && (proprietaryAPI.hasHmacSHA512())) { proprietaryAPI.hmacSHA512(hmacKey, SEED, (short)0, (short)SEED.length, buffer, (short)0); } else { nativeHmacSha512.init(hmacKey, Signature.MODE_SIGN); nativeHmacSha512.sign(SEED, (short)0, (short)SEED.length, buffer, (short)0); } } else { HmacSha512.hmac(BITCOIN_SEED, (short)0, (short)BITCOIN_SEED.length, SEED, (short)0, (short)SEED.length, buffer, (short)0, apduBuffer, (short)0); };
if ((Util.arrayCompare(PRIVATE_SEED, (short)0, buffer, (short)0, (short)32) == 0) &&
(Util.arrayCompare(CHAIN_SEED, (short)0, buffer, (short)32, (short)32) == 0)) {
lastStatus = STATUS_PASSED;
}
if ((nativeHmacSha512 != null) && (hmacKey != null)) {
flags |= FLAG_NATIVE_HMAC_SHA512;
}
if (hmacAlgorithm == KeyBuilder.TYPE_HMAC_TRANSIENT_DESELECT) {
flags |= FLAG_HMAC_TRANSIENT_DESELECT;
}
if (hmacAlgorithm == KeyBuilder.TYPE_HMAC_TRANSIENT_RESET) {
flags |= FLAG_HMAC_TRANSIENT_RESET;
}
}
catch(Exception e) {
lastStatus = STATUS_FAILED_INTERNAL;
}
apduBuffer[0] = lastStatus;
apduBuffer[1] = flags;
}
示例7: FIDOStandalone
import javacard.security.KeyBuilder; //导入依赖的package包/类
public FIDOStandalone() {
scratch = JCSystem.makeTransientByteArray((short)64, JCSystem.CLEAR_ON_DESELECT);
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());
random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
// Initialize the unique wrapping key
chipKey = (AESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_AES, KeyBuilder.LENGTH_AES_256, false);
random.generateData(scratch, (short)0, (short)32);
chipKey.setKey(scratch, (short)0);
cipherEncrypt = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false);
cipherEncrypt.init(chipKey, Cipher.MODE_ENCRYPT, IV_ZERO_AES, (short)0, (short)IV_ZERO_AES.length);
cipherDecrypt = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false);
cipherDecrypt.init(chipKey, Cipher.MODE_DECRYPT, IV_ZERO_AES, (short)0, (short)IV_ZERO_AES.length);
}
示例8: getEcFpFieldLength
import javacard.security.KeyBuilder; //导入依赖的package包/类
/**
* \brief Get the field length of an EC FP key using the amount of bytes
* of a parameter (e.g. the prime).
*
* \return The bit length of the field.
*
* \throw ISOException SC_FUNC_NOT_SUPPORTED.
*/
private short getEcFpFieldLength(short bytes) {
switch(bytes) {
case 24:
return KeyBuilder.LENGTH_EC_FP_192;
case 28:
return LENGTH_EC_FP_224;
case 32:
return LENGTH_EC_FP_256;
case 40:
return LENGTH_EC_FP_320;
case 48:
return LENGTH_EC_FP_384;
case 64:
return LENGTH_EC_FP_512;
case 66:
return LENGTH_EC_FP_521;
default:
ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
return 0;
}
}
示例9: LedgerWalletApplet
import javacard.security.KeyBuilder; //导入依赖的package包/类
public LedgerWalletApplet(byte[] parameters, short parametersOffset, byte parametersLength) {
BCDUtils.init();
TC.init();
Crypto.init();
Transaction.init();
Bip32Cache.init();
Keycard.init();
limits = new byte[LIMIT_LAST];
scratch256 = JCSystem.makeTransientByteArray((short)256, JCSystem.CLEAR_ON_DESELECT);
transactionPin = new OwnerPIN(TRANSACTION_PIN_ATTEMPTS, TRANSACTION_PIN_SIZE);
walletPin = new OwnerPIN(WALLET_PIN_ATTEMPTS, WALLET_PIN_SIZE);
secondaryPin = new OwnerPIN(SECONDARY_PIN_ATTEMPTS, SECONDARY_PIN_SIZE);
masterDerived = new byte[64];
chipKey = (DESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_2KEY, false);
trustedInputKey = (DESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_2KEY, false);
developerKey = (DESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_2KEY, false);
try {
pairingKey = (AESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_AES, KeyBuilder.LENGTH_AES_256, false);
}
catch(Exception e) {
}
reset();
if (parametersLength != 0) {
attestationPrivate = (ECPrivateKey)KeyBuilder.buildKey(KeyBuilder.TYPE_EC_FP_PRIVATE, KeyBuilder.LENGTH_EC_FP_256, false);
attestationPublic = new byte[65];
Secp256k1.setCommonCurveParameters(attestationPrivate);
attestationPrivate.setS(parameters, parametersOffset, (short)32);
parametersOffset += (short)32;
attestationSignature = new byte[parameters[(short)(parametersOffset + 1)] + 2];
Util.arrayCopy(parameters, parametersOffset, attestationSignature, (short)0, (short)attestationSignature.length);
}
}
示例10: getUncompressedPublicPoint
import javacard.security.KeyBuilder; //导入依赖的package包/类
@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;
}
}
示例11: PRNGTest
import javacard.security.KeyBuilder; //导入依赖的package包/类
private PRNGTest() {
random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
drngAESKey = (AESKey) KeyBuilderX.buildKey(KeyBuilderX.TYPE_AES_STATIC,
KeyBuilder.LENGTH_AES_128, false);
tmp = JCSystem.makeTransientByteArray((short) 40,
JCSystem.CLEAR_ON_DESELECT);
random.generateData(tmp, (short) 0, (short) 16);
drngAESKey.setKey(tmp, (short) 0);
scratch = JCSystem.makeTransientByteArray((short) 32,
JCSystem.CLEAR_ON_DESELECT);
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 key for DRNG function (AES CMAC)
drngKey1 = (AESKey) KeyBuilderX.buildKey(KeyBuilderX.TYPE_AES_STATIC,
KeyBuilder.LENGTH_AES_128, false);
drngKey2 = (AESKey) KeyBuilderX.buildKey(KeyBuilderX.TYPE_AES_STATIC,
KeyBuilder.LENGTH_AES_128, false);
random.generateData(scratch, (short) 0, (short) 32);
drngKey1.setKey(scratch, (short) 0);
drngKey2.setKey(scratch, (short) 16);
drng1 = SignatureX.getInstance(SignatureX.ALG_AES_CMAC16, false);
drng1.init(drngKey1, Signature.MODE_SIGN);
drng2 = SignatureX.getInstance(SignatureX.ALG_AES_CMAC16, false);
drng2.init(drngKey2, Signature.MODE_SIGN);
// Initialize the unique key for MAC function (AES CMAC)
macKey = (AESKey) KeyBuilderX.buildKey(KeyBuilderX.TYPE_AES_STATIC,
KeyBuilder.LENGTH_AES_128, false);
random.generateData(scratch, (short) 0, (short) 16);
macKey.setKey(scratch, (short) 0);
cmacSign = SignatureX.getInstance(SignatureX.ALG_AES_CMAC16, false);
cmacSign.init(macKey, Signature.MODE_SIGN);
cmacVerify = SignatureX.getInstance(SignatureX.ALG_AES_CMAC16, false);
cmacVerify.init(macKey, Signature.MODE_VERIFY);
// Initialize ecMultiplier
ecMultiplyHelper = KeyAgreementX.getInstance(KeyAgreementX.ALG_EC_SVDP_DH_PLAIN_XY, false);
}
示例12: DH
import javacard.security.KeyBuilder; //导入依赖的package包/类
public DH() {
// Creates a RSA private key instance as template for the DH private key
dhPriv = (RSAPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE_TRANSIENT_RESET, KeyBuilder.LENGTH_RSA_2048, false);
// Creates an RSA cipher instance
dhCipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);
// Set default G to 2
G[(short) (maxLength - 1)] = (byte) 0x02;
}
示例13: DH
import javacard.security.KeyBuilder; //导入依赖的package包/类
public DH() {
// Creates a RSA private key instance as template for the DH private key
dhPriv = (RSAPrivateKey) KeyBuilder.buildKey(KeyBuilder.TYPE_RSA_PRIVATE_TRANSIENT_RESET, KeyBuilder.LENGTH_RSA_2048, false);
// Creates an RSA cipher instance
dhCipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);
}
示例14: authenticateMutualReplayAttack
import javacard.security.KeyBuilder; //导入依赖的package包/类
@Test
public void authenticateMutualReplayAttack() {
byte[] key = DatatypeConverter.parseHexBinary("010203040506070801020304050607080102030405060708");
byte[] myChallenge= new byte [16], globalchallenge = new byte[40], challengeresponse = new byte[40];
byte[] challenge;
Cipher cipherDES = Cipher.getInstance(Cipher.ALG_DES_CBC_NOPAD, false);
DESKey deskey = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_3KEY, false);
deskey.setKey(key, (short) 0);
RandomData randomData = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
randomData.generateData(myChallenge, (short) 0, (short) myChallenge.length);
// select admin key
execute("00 22 81 A4 03 83 01 80");
// get a challenge
ResponseAPDU response = execute("00 87 00 00 14 7C 12 81 10" + DatatypeConverter.printHexBinary(myChallenge) + "00");
if (!Arrays.equals(Arrays.copyOfRange(response.getBytes(), 0, 4), new byte[] {0x7C,0x12,(byte) 0x81,0x10})) {
fail("not a challenge:" + DatatypeConverter.printHexBinary(response.getBytes()));
}
// compute the response
challenge = Arrays.copyOfRange(response.getBytes(), 4, 20);
//solve challenge
//R2
System.arraycopy(challenge, 0, globalchallenge, 0, 16);
//R1
System.arraycopy(myChallenge, 0, globalchallenge, 16, 16);
// keep Z1 random
globalchallenge[(short)39] = (byte) 0x80;
cipherDES.init(deskey, Cipher.MODE_ENCRYPT);
cipherDES.doFinal(globalchallenge, (short) 0, (short)40, challengeresponse, (short) 0);
// send the response
execute("00 87 00 00 2C 7C 2A 82 28" + DatatypeConverter.printHexBinary(challengeresponse), 0x9000);
execute("00 87 00 00 2C 7C 2A 82 28" + DatatypeConverter.printHexBinary(challengeresponse), 0x6985);
}
示例15: CryptonitApplet
import javacard.security.KeyBuilder; //导入依赖的package包/类
protected CryptonitApplet(byte[] bArray, short bOffset, byte bLength) {
mgmt_key = KeyBuilder.buildKey(KeyBuilder.TYPE_DES,
KeyBuilder.LENGTH_DES3_3KEY, false);
((DESKey) mgmt_key).setKey(new byte[]{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
}, (short) 0);
mgmt_counter = new OwnerPIN(MGMT_MAX_TRIES, (byte) 4);
mgmt_counter.update(new byte[]{0x00, 0x00, 0x00, 0x00}, (short) 0, (byte) 4);
challenge = JCSystem.makeTransientByteArray((short) 8,
JCSystem.CLEAR_ON_DESELECT);
pin = new OwnerPIN(PIN_MAX_TRIES, PIN_MAX_LENGTH);
pin.update(new byte[]{
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38
}, (short) 0, (byte) 8);
keys = new Key[(byte) 4];
random = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
authenticated = JCSystem.makeTransientBooleanArray((short) 1, JCSystem.CLEAR_ON_DESELECT);
rsa_cipher = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, false);
try {
ec_signature = Signature.getInstance(Signature.ALG_ECDSA_SHA, false);
} catch (Exception e) {
}
FileIndex index = new FileIndex();
io = new IOBuffer(index);
register();
}