Single retrieval of AES_CBC cipher instance and RandomData

This commit is contained in:
Arnaud Fontaine 2017-08-26 18:42:56 +02:00
parent 04404d6a4e
commit fa15c73220

View File

@ -31,8 +31,12 @@ public final class SmartPGPApplet extends Applet {
private final Transients transients; private final Transients transients;
private final Cipher cipher_aes_cbc_nopad;
private final RandomData random_data;
public SmartPGPApplet() { public SmartPGPApplet() {
cipher_aes_cbc_nopad = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false);
random_data = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
data = new Persistent(); data = new Persistent();
transients = new Transients(); transients = new Transients();
} }
@ -1235,10 +1239,9 @@ public final class SmartPGPApplet extends Applet {
return 0; return 0;
} }
final Cipher cipher = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false); cipher_aes_cbc_nopad.init(data.aes_key, Cipher.MODE_DECRYPT);
cipher.init(data.aes_key, Cipher.MODE_DECRYPT);
final short res = cipher.doFinal(transients.buffer, (short)1, (short)(lc - 1), final short res = cipher_aes_cbc_nopad.doFinal(transients.buffer, (short)1, (short)(lc - 1),
transients.buffer, lc); transients.buffer, lc);
Util.arrayCopyNonAtomic(transients.buffer, lc, Util.arrayCopyNonAtomic(transients.buffer, lc,
@ -1267,10 +1270,9 @@ public final class SmartPGPApplet extends Applet {
return 0; return 0;
} }
final Cipher cipher = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false); cipher_aes_cbc_nopad.init(data.aes_key, Cipher.MODE_ENCRYPT);
cipher.init(data.aes_key, Cipher.MODE_ENCRYPT);
final short res = cipher.doFinal(transients.buffer, (short)0, lc, final short res = cipher_aes_cbc_nopad.doFinal(transients.buffer, (short)0, lc,
transients.buffer, (short)(lc + 1)); transients.buffer, (short)(lc + 1));
transients.buffer[lc] = (byte)0x02; transients.buffer[lc] = (byte)0x02;
@ -1319,7 +1321,7 @@ public final class SmartPGPApplet extends Applet {
} }
if(le != 0) { if(le != 0) {
RandomData.getInstance(RandomData.ALG_SECURE_RANDOM).generateData(transients.buffer, (short)0, le); random_data.generateData(transients.buffer, (short)0, le);
} }
return le; return le;