title: Concurrency signing in HSM with LunaProvider throw CKR_DATA_INVALID
body: <p>When I concurrently attempt to sign with the same key inside the HSM, some signatures fail with the following error :
  com.safenetinc.luna.exception.LunaCryptokiException: function 'C_Sign' returns 0x20 (CKR_DATA_INVALID)</p>
  
  <p>With only one thread and one instance of my jar, I have no error. 
  More threads I have more often the signatures fail.
  <strong>An important point, if I have 2 instances of only one thread each the issue also occurs.</strong></p>
  
  <p>I'm using Luna JSP provider. 
  The algo is SHA256withECDSA. </p>
  
  <p>I tried also with token connection, but same result.
  I check my timeout in crystoki.ini, failing occurs before the timeout.
  Even with a Singleton for the keyStore, or for the privateKey I have the issue. </p>
  
  <p>Thread generator:</p>
  
  <pre class="lang-java prettyprint-override"><code>public static void main(String[] args)  {
      SpringApplication.run(DccHsmTest.class, args);
  
  
      for (int i=0; i&lt;4; i++)
      {
        Multithreading object = new Multithreading();
        object.start();
      }
    }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code> private byte[] CHALLENGE = new byte[10000];
  
   public void run()
    {
      try
      {
        final HsmService hsmService = new HsmService();
  
        hsmKeyStore = hsmService.hsmKeyStore(hsmPartition, hsmPassword);
  
        PrivateKey privateKey = (PrivateKey) hsmKeyStore.getKey(aliasToRetrieve, password);
  
        while(sign(privateKey)){
          ...
        }
  
        throw new RuntimeException();
      }
      catch (Exception e)
      {
        ...
      }
    }
  
    private static boolean sign(PrivateKey privateKey)  {
      try {
        Signature signature = Signature.getInstance("SHA256withECDSA", "LunaProvider");
  
        signature.initSign(privateKey);
        signature.update(CHALLENGE);
  
        signature.sign();
        return true;
      }
      catch(Exception e){
        ...    
        return false;
      }
    }
  </code></pre>
  
  <p>HsmService</p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
  public class HsmService {
  
    public KeyStore hsmKeyStore(String hsmPartition, String hsmPassword) throws
  KeyStoreException,                                                                   CertificateException,                                                                       NoSuchAlgorithmException,                                                                       IOException {
  
      LunaProvider lunaProvider = new LunaProvider();
      Security.addProvider(lunaProvider);
  
      LunaSlotManager lunaSlotManager = LunaSlotManager.getInstance();
      lunaSlotManager.login(hsmPartition, hsmPassword);
  
  
      KeyStore keyStore = KeyStore.getInstance("Luna");
      keyStore.load(null, null);
      return keyStore;
    }
  }
  </code></pre>
  
  <p>The error definition of CKR_DATA_INVALID is:<br>
  The plaintext input data to a cryptographic operation is invalid. </p>
  
  <p>But I always pass the same input, sometimes it works.</p>
  
  <p>The CKlog doesn't provide much details :
  FINISign <strong><em>CKR_DATA_INVALID</em></strong>(5811ms) {"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" }</p>
  
  <p>Just seems to return a bad signature</p>