javacard.framework.CardRuntimeException Java Examples

The following examples show how to use javacard.framework.CardRuntimeException. 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: ECKeyGenerator.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * @param keyClass
 * @param keyLength
 * @return
 */
public KeyPair allocatePair(byte keyClass, short keyLength) {
    sw = ISO7816.SW_NO_ERROR;
    KeyPair ecKeyPair = null;
    try {
        if (!dryRun) {
            ecKeyPair = new KeyPair(keyClass, keyLength);

            if (ecKeyPair.getPublic() == null || ecKeyPair.getPrivate() == null) {
                try {
                    ecKeyPair.genKeyPair();
                } catch (Exception ignored) {
                }
            }
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return ecKeyPair;
}
 
Example #2
Source File: ECKeyGenerator.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * @param keyClass
 * @param keyLength
 * @return
 */
public KeyPair constructPair(byte keyClass, short keyLength) {
    sw = ISO7816.SW_NO_ERROR;
    KeyPair ecKeyPair = null;
    byte privKeyType;
    byte pubKeyType;
    if (keyClass == KeyPair.ALG_EC_FP) {
        privKeyType = KeyBuilder.TYPE_EC_FP_PRIVATE;
        pubKeyType = KeyBuilder.TYPE_EC_FP_PUBLIC;
    } else {
        privKeyType = KeyBuilder.TYPE_EC_F2M_PRIVATE;
        pubKeyType = KeyBuilder.TYPE_EC_F2M_PUBLIC;
    }
    try {
        if (!dryRun) {
            ECPrivateKey privateKey = (ECPrivateKey) KeyBuilder.buildKey(privKeyType, keyLength, false);
            ECPublicKey publicKey = (ECPublicKey) KeyBuilder.buildKey(pubKeyType, keyLength, false);

            ecKeyPair = new KeyPair(publicKey, privateKey);
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return ecKeyPair;
}
 
Example #3
Source File: ECKeyGenerator.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * Copies this KeyPairs curve parameters to another ECKeyGenerator.
 *
 * @param from   keyPair to copy from
 * @param to     keyPair to copy to
 * @param params parameters to copy
 * @param buffer buffer to use for copying
 * @param offset offset to use in buffer
 * @return sw
 */
public short copyCurve(KeyPair from, KeyPair to, short params, byte[] buffer, short offset) {
    try {
        sw = AppletUtil.keypairCheck(from);
        sw = AppletUtil.keypairCheck(to);

        short param = EC_Consts.PARAMETER_FP;
        while (param <= EC_Consts.PARAMETER_K) {
            short masked = (short) (param & params);
            if (masked != 0) {
                short paramLength = exportParameter(from, EC_Consts.KEY_PUBLIC, masked, buffer, offset);
                setParameter(to, EC_Consts.KEY_BOTH, masked, buffer, offset, paramLength);
            }
            param = (short) (param << 1);
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return sw;
}
 
Example #4
Source File: ECKeyTester.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * Tests ECDH secret generation with keys from given {@code privatePair} and {@code publicPair}.
 * Uses {@code pubkeyBuffer} at {@code pubkeyOffset} for computations.
 * Output should equal with ECDHC output.
 *
 * @param privatePair    KeyPair from which the private key is used
 * @param publicPair     KeyPair from which the public key is used
 * @param pubkeyBuffer   buffer to be used for the public key
 * @param pubkeyOffset   offset into pubkeyBuffer that can be used for the public key
 * @param outputBuffer   buffer to be used for the secret output
 * @param outputOffset   offset into the outputBuffer
 * @param transformation (EC_Consts.TRANSFORMATION_* | ...)
 * @return derived secret length
 **/
public short testKA(KeyPair privatePair, KeyPair publicPair, byte[] pubkeyBuffer, short pubkeyOffset, byte[] outputBuffer, short outputOffset, short transformation) {
    short length = 0;
    try {
        sw = AppletUtil.kaCheck(ecKeyAgreement);
        sw = AppletUtil.keypairCheck(privatePair);
        sw = AppletUtil.keypairCheck(publicPair);
        if (!dryRun) {
            short pubkeyLength = ((ECPublicKey) publicPair.getPublic()).getW(pubkeyBuffer, pubkeyOffset);
            ecKeyAgreement.init(privatePair.getPrivate());

            pubkeyLength = EC_Consts.transformParameter(transformation, pubkeyBuffer, pubkeyOffset, pubkeyLength);
            length = ecKeyAgreement.generateSecret(pubkeyBuffer, pubkeyOffset, pubkeyLength, outputBuffer, outputOffset);
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return length;
}
 
Example #5
Source File: ECKeyTester.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * @param privatePair
 * @param pubkey
 * @param pubkeyOffset
 * @param pubkeyLength
 * @param outpuBuffer
 * @param outputOffset
 * @param transformation
 * @return
 */
public short testKA_direct(KeyPair privatePair, byte[] pubkey, short pubkeyOffset, short pubkeyLength, byte[] outpuBuffer, short outputOffset, short transformation) {
    short length = 0;
    try {
        sw = AppletUtil.kaCheck(ecKeyAgreement);
        sw = AppletUtil.keypairCheck(privatePair);

        if (!dryRun) {
            ecKeyAgreement.init(privatePair.getPrivate());
            pubkeyLength = EC_Consts.transformParameter(transformation, pubkey, pubkeyOffset, pubkeyLength);
            length = ecKeyAgreement.generateSecret(pubkey, pubkeyOffset, pubkeyLength, outpuBuffer, outputOffset);
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return length;
}
 
Example #6
Source File: ECKeyTester.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * Uses {@code signKey} to sign data from {@code inputBuffer} at {@code inputOffset} with {@code inputOffset}.
 * Then checks for correct signature length.
 * Then tries verifying the data with {@code verifyKey}.
 *
 * @param signKey     key to use for signing
 * @param verifyKey   key to use for verifying the signature
 * @param inputBuffer buffer to sign data from
 * @param inputOffset offset into inputBuffer to sign data from
 * @param inputLength length of data to sign
 * @param sigBuffer   buffer to output signature to
 * @param sigOffset   offset into sigBuffer to output to
 * @return signature length
 */
public short testECDSA(ECPrivateKey signKey, ECPublicKey verifyKey, byte[] inputBuffer, short inputOffset, short inputLength, byte[] sigBuffer, short sigOffset) {
    short length = 0;
    try {
        sw = AppletUtil.signCheck(ecdsaSignature);

        if (!dryRun) {
            ecdsaSignature.init(signKey, Signature.MODE_SIGN);
            length = ecdsaSignature.sign(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset);

            ecdsaSignature.init(verifyKey, Signature.MODE_VERIFY);
            if (!ecdsaSignature.verify(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset, length)) {
                sw = AppletBase.SW_SIG_VERIFY_FAIL;
            }
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return length;
}
 
Example #7
Source File: ECKeyTester.java    From ECTester with MIT License 6 votes vote down vote up
/**
 * @param verifyKey
 * @param inputBuffer
 * @param inputOffset
 * @param inputLength
 * @param sigBuffer
 * @param sigOffset
 * @param sigLength
 * @return
 */
public short testECDSA_verify(ECPublicKey verifyKey, byte[] inputBuffer, short inputOffset, short inputLength, byte[] sigBuffer, short sigOffset, short sigLength) {
    short length = 0;
    try {
        sw = AppletUtil.signCheck(ecdsaSignature);

        if (!dryRun) {
            ecdsaSignature.init(verifyKey, Signature.MODE_VERIFY);
            if (!ecdsaSignature.verify(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset, sigLength)) {
                sw = AppletBase.SW_SIG_VERIFY_FAIL;
            }
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return length;
}
 
Example #8
Source File: ECKeyGenerator.java    From ECTester with MIT License 5 votes vote down vote up
/**
 * @param keypair
 * @param key
 * @return
 */
public short clearPair(KeyPair keypair, byte key) {
    try {
        sw = AppletUtil.keypairCheck(keypair);
        if (!dryRun) {
            if ((key & EC_Consts.KEY_PUBLIC) != 0) keypair.getPublic().clearKey();
            if ((key & EC_Consts.KEY_PRIVATE) != 0) keypair.getPrivate().clearKey();
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return sw;
}
 
Example #9
Source File: ECKeyGenerator.java    From ECTester with MIT License 5 votes vote down vote up
/**
 * @param keypair
 * @return
 */
public short generatePair(KeyPair keypair) {
    try {
        sw = AppletUtil.keypairCheck(keypair);
        if (!dryRun) {
            keypair.genKeyPair();
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return sw;
}
 
Example #10
Source File: ECKeyTester.java    From ECTester with MIT License 5 votes vote down vote up
public short allocateKA(byte algorithm) {
    sw = ISO7816.SW_NO_ERROR;
    try {
        if (!dryRun) {
            ecKeyAgreement = KeyAgreement.getInstance(algorithm, false);
            kaType = algorithm;
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return sw;
}
 
Example #11
Source File: ECKeyTester.java    From ECTester with MIT License 5 votes vote down vote up
public short allocateSig(byte algorithm) {
    sw = ISO7816.SW_NO_ERROR;
    try {
        if (!dryRun) {
            ecdsaSignature = Signature.getInstance(algorithm, false);
            sigType = algorithm;
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return sw;
}
 
Example #12
Source File: ECKeyTester.java    From ECTester with MIT License 5 votes vote down vote up
/**
 * @param signKey
 * @param inputBuffer
 * @param inputOffset
 * @param inputLength
 * @param sigBuffer
 * @param sigOffset
 * @return
 */
public short testECDSA_sign(ECPrivateKey signKey, byte[] inputBuffer, short inputOffset, short inputLength, byte[] sigBuffer, short sigOffset) {
    short length = 0;
    try {
        sw = AppletUtil.signCheck(ecdsaSignature);

        if (!dryRun) {
            ecdsaSignature.init(signKey, Signature.MODE_SIGN);
            length = ecdsaSignature.sign(inputBuffer, inputOffset, inputLength, sigBuffer, sigOffset);
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return length;
}
 
Example #13
Source File: ECKeyGenerator.java    From ECTester with MIT License 4 votes vote down vote up
/**
 * Exports a selected parameter from a given keyPairs key.
 *
 * @param keypair      keypair to export from
 * @param key          key to export from (KEY_PUBLIC || KEY_PRIVATE)
 * @param param        parameter to export (EC_Consts.PARAMETER_* || ...)
 * @param outputBuffer buffer to write to
 * @param outputOffset offset to start writing in buffer
 * @return length of data written
 */
public short exportParameter(KeyPair keypair, byte key, short param, byte[] outputBuffer, short outputOffset) {
    short length = 0;
    try {
        sw = AppletUtil.keypairCheck(keypair);

        ECPublicKey ecPublicKey = null;
        ECPrivateKey ecPrivateKey = null;
        if (!dryRun) {
            ecPublicKey = (ECPublicKey) keypair.getPublic();
            ecPrivateKey = (ECPrivateKey) keypair.getPrivate();
        }

        switch (param) {
            case EC_Consts.PARAMETER_FP:
                if (!dryRun) {
                    if ((key & EC_Consts.KEY_PUBLIC) != 0)
                        length = ecPublicKey.getField(outputBuffer, outputOffset);
                    if ((key & EC_Consts.KEY_PRIVATE) != 0)
                        length = ecPrivateKey.getField(outputBuffer, outputOffset);
                }
                break;
            case EC_Consts.PARAMETER_F2M:
                if ((key & EC_Consts.KEY_PUBLIC) != 0 && !dryRun) {
                    Util.setShort(outputBuffer, outputOffset, ecPublicKey.getSize());
                    length = 2;
                    length += ecPublicKey.getField(outputBuffer, (short) (outputOffset + 2));
                }
                if ((key & EC_Consts.KEY_PRIVATE) != 0 && !dryRun) {
                    Util.setShort(outputBuffer, outputOffset, ecPrivateKey.getSize());
                    length = 2;
                    length += ecPrivateKey.getField(outputBuffer, (short) (outputOffset + 2));
                }
                break;
            case EC_Consts.PARAMETER_A:
                if (!dryRun) {
                    if ((key & EC_Consts.KEY_PUBLIC) != 0) length = ecPublicKey.getA(outputBuffer, outputOffset);
                    if ((key & EC_Consts.KEY_PRIVATE) != 0) length = ecPrivateKey.getA(outputBuffer, outputOffset);
                }
                break;
            case EC_Consts.PARAMETER_B:
                if (!dryRun) {
                    if ((key & EC_Consts.KEY_PUBLIC) != 0) length = ecPublicKey.getB(outputBuffer, outputOffset);
                    if ((key & EC_Consts.KEY_PRIVATE) != 0) length = ecPrivateKey.getB(outputBuffer, outputOffset);
                }
                break;
            case EC_Consts.PARAMETER_G:
                if (!dryRun) {
                    if ((key & EC_Consts.KEY_PUBLIC) != 0) length = ecPublicKey.getG(outputBuffer, outputOffset);
                    if ((key & EC_Consts.KEY_PRIVATE) != 0) length = ecPrivateKey.getG(outputBuffer, outputOffset);
                }
                break;
            case EC_Consts.PARAMETER_R:
                if (!dryRun) {
                    if ((key & EC_Consts.KEY_PUBLIC) != 0) length = ecPublicKey.getR(outputBuffer, outputOffset);
                    if ((key & EC_Consts.KEY_PRIVATE) != 0) length = ecPrivateKey.getR(outputBuffer, outputOffset);
                }
                break;
            case EC_Consts.PARAMETER_K:
                if (!dryRun) {
                    length = 2;
                    if ((key & EC_Consts.KEY_PUBLIC) != 0)
                        Util.setShort(outputBuffer, outputOffset, ecPublicKey.getK());
                    if ((key & EC_Consts.KEY_PRIVATE) != 0)
                        Util.setShort(outputBuffer, outputOffset, ecPrivateKey.getK());
                }
                break;
            case EC_Consts.PARAMETER_W:
                if ((key & EC_Consts.KEY_PUBLIC) != 0 && !dryRun)
                    length = ecPublicKey.getW(outputBuffer, outputOffset);
                break;
            case EC_Consts.PARAMETER_S:
                if ((key & EC_Consts.KEY_PRIVATE) != 0 && !dryRun)
                    length = ecPrivateKey.getS(outputBuffer, outputOffset);
                break;
            default:
                ISOException.throwIt(ISO7816.SW_FUNC_NOT_SUPPORTED);
        }
    } catch (CardRuntimeException ce) {
        sw = ce.getReason();
    }
    return length;
}