licel / jcardsim

https://jcardsim.org
222 stars 123 forks source link

test: Do not reuse KeyPair context to generate second key pair #174

Open Jakuje opened 3 years ago

Jakuje commented 3 years ago

This occasionally fails with java.lang.ArrayIndexOutOfBoundsException when the ECDSA key starts with 0 and the length is trimmed in setBigInteger() of ByteContainer for the first key pair and the second one does not fit.

Backtraces collected from OpenSC testsuite were sometimes very different, but all point to this place:

Error:  testGenerateSecretECDH(com.licel.jcardsim.crypto.KeyAgreementImplTest)  Time elapsed: 0.2 s  <<< ERROR!
java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at javacard.framework.Util.arrayCopy(Unknown Source)
    at com.licel.jcardsim.crypto.ByteContainer.setBytes(ByteContainer.java:122)
    at com.licel.jcardsim.crypto.ByteContainer.setBytes(ByteContainer.java:99)
    at com.licel.jcardsim.crypto.ByteContainer.setBigInteger(ByteContainer.java:87)
    at com.licel.jcardsim.crypto.ECPrivateKeyImpl.setParameters(ECPrivateKeyImpl.java:57)
    at com.licel.jcardsim.crypto.KeyPairImpl.genKeyPair(KeyPairImpl.java:93)
    at javacard.security.KeyPair.genKeyPair(Unknown Source)
    at com.licel.jcardsim.crypto.KeyAgreementImplTest.testGenerateSecret(KeyAgreementImplTest.java:119)
    at com.licel.jcardsim.crypto.KeyAgreementImplTest.testGenerateSecretECDH(KeyAgreementImplTest.java:56)
Error:  Errors:
Error:    com.licel.jcardsim.crypto.KeyAgreementImplTest#testGenerateSecretECDH ArrayIndexOutOfBoundsException