From fa15c7322015e34343dae47993fe7aeed97c5980 Mon Sep 17 00:00:00 2001 From: Arnaud Fontaine Date: Sat, 26 Aug 2017 18:42:56 +0200 Subject: [PATCH] Single retrieval of AES_CBC cipher instance and RandomData --- src/fr/anssi/smartpgp/SmartPGPApplet.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/fr/anssi/smartpgp/SmartPGPApplet.java b/src/fr/anssi/smartpgp/SmartPGPApplet.java index 59cd78b..66d7916 100644 --- a/src/fr/anssi/smartpgp/SmartPGPApplet.java +++ b/src/fr/anssi/smartpgp/SmartPGPApplet.java @@ -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;