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 Cipher cipher_aes_cbc_nopad;
private final RandomData random_data;
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();
transients = new Transients();
}
@ -1235,11 +1239,10 @@ public final class SmartPGPApplet extends Applet {
return 0;
}
final Cipher cipher = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false);
cipher.init(data.aes_key, Cipher.MODE_DECRYPT);
cipher_aes_cbc_nopad.init(data.aes_key, Cipher.MODE_DECRYPT);
final short res = cipher.doFinal(transients.buffer, (short)1, (short)(lc - 1),
transients.buffer, lc);
final short res = cipher_aes_cbc_nopad.doFinal(transients.buffer, (short)1, (short)(lc - 1),
transients.buffer, lc);
Util.arrayCopyNonAtomic(transients.buffer, lc,
transients.buffer, (short)0, res);
@ -1267,11 +1270,10 @@ public final class SmartPGPApplet extends Applet {
return 0;
}
final Cipher cipher = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false);
cipher.init(data.aes_key, Cipher.MODE_ENCRYPT);
cipher_aes_cbc_nopad.init(data.aes_key, Cipher.MODE_ENCRYPT);
final short res = cipher.doFinal(transients.buffer, (short)0, lc,
transients.buffer, (short)(lc + 1));
final short res = cipher_aes_cbc_nopad.doFinal(transients.buffer, (short)0, lc,
transients.buffer, (short)(lc + 1));
transients.buffer[lc] = (byte)0x02;
Util.arrayCopyNonAtomic(transients.buffer, lc,
@ -1319,7 +1321,7 @@ public final class SmartPGPApplet extends Applet {
}
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;