<div dir="ltr">On Mon, Dec 8, 2014 at 6:01 PM, roko <span dir="ltr"><<a href="mailto:roko98@yahoo.com" target="_blank">roko98@yahoo.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="color:rgb(0,0,0);background-color:rgb(255,255,255);font-family:'Courier New',courier,monaco,monospace,sans-serif;font-size:10px"><div>So, I think padding is now supported, but in my application  now I have this exception:<br></div><div dir="ltr"><br></div><div dir="ltr">Caused by: javax.crypto.ShortBufferException<br>        at sun.security.pkcs11.P11Cipher.implUpdate(P11Cipher.java:561)<span class=""><br>        at sun.security.pkcs11.P11Cipher.engineUpdate(P11Cipher.java:464)<br>        at sun.security.pkcs11.P11Cipher.engineUpdate(P11Cipher.java:452)<br></span>        ... 49 more<br>Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_BUFFER_TOO_SMALL<span class=""><br>        at sun.security.pkcs11.wrapper.PKCS11.C_EncryptUpdate(Native Method)<br>        at sun.security.pkcs11.P11Cipher.implUpdate(P11Cipher.java:517)<br>        ... 51 more<br><br></span></div></div></blockquote><div><br></div><div>After reading some source code for sun.security.pkcs11.P11Cipher I see that it is not following the PKCS#11 convention for functions returning output in a variable-length buffer. It is tracking the buffered bytes and thus know the exact expected number of bytes that should be returned. A program should normally call C_EncryptUpdate twice, first to get the number of bytes and second call to make the actual encryption. C_EncryptUpdate in SoftHSM is currently not tracking this and will round up to the nearest block size. This is ok according to PKCS#11: "This number may somewhat exceed the precise number of bytes needed, but should not exceed it by a large amount."</div><div><br></div><div>We can rewrite the code to calculate the exact number of bytes need when calling C_EncryptUpdate.</div><div><br></div><div>Just to verify this, are you encrypting data that is not equal to a multiple of the block size (16 bytes)? Does it work better when adjusting it to be equal to a multiple of the block size?</div><div><br></div><div>// Rickard</div></div></div></div>