RatchetSKM: Group debug output for IB tagsets by pubkey, not session key

ElGamalSKM: Debug header change
ElGamalAESEngine: Minor cleanups for efficiency
This commit is contained in:
zzz
2020-04-06 11:31:18 +00:00
parent c77e41c59e
commit 1ca9674f3f
3 changed files with 31 additions and 26 deletions

View File

@ -94,8 +94,8 @@ public final class ElGamalAESEngine {
if (_log.shouldLog(Log.ERROR)) _log.error("Null data being decrypted?"); if (_log.shouldLog(Log.ERROR)) _log.error("Null data being decrypted?");
return null; return null;
} else if (data.length < MIN_ENCRYPTED_SIZE) { } else if (data.length < MIN_ENCRYPTED_SIZE) {
if (_log.shouldLog(Log.ERROR)) if (_log.shouldWarn())
_log.error("Data is less than the minimum size (" + data.length + " < " + MIN_ENCRYPTED_SIZE + ")"); _log.warn("Data is less than the minimum size (" + data.length + " < " + MIN_ENCRYPTED_SIZE + ")");
return null; return null;
} }
if (targetPrivateKey.getType() != EncType.ELGAMAL_2048) if (targetPrivateKey.getType() != EncType.ELGAMAL_2048)
@ -663,9 +663,9 @@ public final class ElGamalAESEngine {
long paddedSize, int prefixBytes) { long paddedSize, int prefixBytes) {
//_log.debug("iv for encryption: " + DataHelper.toString(iv, 16)); //_log.debug("iv for encryption: " + DataHelper.toString(iv, 16));
//_log.debug("Encrypting AES"); //_log.debug("Encrypting AES");
if (tagsForDelivery == null) tagsForDelivery = Collections.emptySet(); int tagCount = (tagsForDelivery != null) ? tagsForDelivery.size() : 0;
int size = 2 // sizeof(tags) int size = 2 // sizeof(tags)
+ SessionTag.BYTE_LENGTH*tagsForDelivery.size() + SessionTag.BYTE_LENGTH * tagCount
+ 4 // payload length + 4 // payload length
+ Hash.HASH_LENGTH + Hash.HASH_LENGTH
+ (newKey == null ? 1 : 1 + SessionKey.KEYSIZE_BYTES) + (newKey == null ? 1 : 1 + SessionKey.KEYSIZE_BYTES)
@ -675,11 +675,13 @@ public final class ElGamalAESEngine {
byte aesData[] = new byte[totalSize + prefixBytes]; byte aesData[] = new byte[totalSize + prefixBytes];
int cur = prefixBytes; int cur = prefixBytes;
DataHelper.toLong(aesData, cur, 2, tagsForDelivery.size()); DataHelper.toLong(aesData, cur, 2, tagCount);
cur += 2; cur += 2;
for (SessionTag tag : tagsForDelivery) { if (tagsForDelivery != null && !tagsForDelivery.isEmpty()) {
System.arraycopy(tag.getData(), 0, aesData, cur, SessionTag.BYTE_LENGTH); for (SessionTag tag : tagsForDelivery) {
cur += SessionTag.BYTE_LENGTH; System.arraycopy(tag.getData(), 0, aesData, cur, SessionTag.BYTE_LENGTH);
cur += SessionTag.BYTE_LENGTH;
}
} }
//_log.debug("# tags created, registered, and written: " + tagsForDelivery.size()); //_log.debug("# tags created, registered, and written: " + tagsForDelivery.size());
DataHelper.toLong(aesData, cur, 4, data.length); DataHelper.toLong(aesData, cur, 4, data.length);

View File

@ -779,7 +779,7 @@ public class TransientSessionKeyManager extends SessionKeyManager {
@Override @Override
public void renderStatusHTML(Writer out) throws IOException { public void renderStatusHTML(Writer out) throws IOException {
StringBuilder buf = new StringBuilder(1024); StringBuilder buf = new StringBuilder(1024);
buf.append("<h3 class=\"debug_inboundsessions\">Inbound sessions</h3>" + buf.append("<h3 class=\"debug_inboundsessions\">ElGamal Inbound Sessions</h3>" +
"<table>"); "<table>");
Map<SessionKey, Set<TagSet>> inboundSets = getInboundTagSetsBySessionKey(); Map<SessionKey, Set<TagSet>> inboundSets = getInboundTagSetsBySessionKey();
int total = 0; int total = 0;
@ -814,7 +814,7 @@ public class TransientSessionKeyManager extends SessionKeyManager {
.append("; sessions: ").append(inboundSets.size()) .append("; sessions: ").append(inboundSets.size())
.append("</th></tr>\n" + .append("</th></tr>\n" +
"</table>" + "</table>" +
"<h3 class=\"debug_outboundsessions\">Outbound sessions</h3>" + "<h3 class=\"debug_outboundsessions\">ElGamal Outbound Sessions</h3>" +
"<table>"); "<table>");
total = 0; total = 0;
totalSets = 0; totalSets = 0;

View File

@ -710,17 +710,19 @@ public class RatchetSKM extends SessionKeyManager implements SessionTagListener
/// end ACKS /// /// end ACKS ///
/** /**
* Return a map of session key to a set of inbound RatchetTagSets for that SessionKey * Return a map of PublicKey to a set of inbound RatchetTagSets for that key.
* Only for renderStatusHTML() below.
*/ */
private Map<SessionKey, Set<RatchetTagSet>> getRatchetTagSetsBySessionKey() { private Map<PublicKey, Set<RatchetTagSet>> getRatchetTagSetsByPublicKey() {
Set<RatchetTagSet> inbound = getRatchetTagSets(); Set<RatchetTagSet> inbound = getRatchetTagSets();
Map<SessionKey, Set<RatchetTagSet>> inboundSets = new HashMap<SessionKey, Set<RatchetTagSet>>(inbound.size()); Map<PublicKey, Set<RatchetTagSet>> inboundSets = new HashMap<PublicKey, Set<RatchetTagSet>>(inbound.size());
// Build a map of the inbound tag sets, grouped by SessionKey // Build a map of the inbound tag sets, grouped by PublicKey
for (RatchetTagSet ts : inbound) { for (RatchetTagSet ts : inbound) {
Set<RatchetTagSet> sets = inboundSets.get(ts.getAssociatedKey()); PublicKey pk = ts.getRemoteKey();
Set<RatchetTagSet> sets = inboundSets.get(pk);
if (sets == null) { if (sets == null) {
sets = new HashSet<RatchetTagSet>(4); sets = new HashSet<RatchetTagSet>(4);
inboundSets.put(ts.getAssociatedKey(), sets); inboundSets.put(pk, sets);
} }
sets.add(ts); sets.add(ts);
} }
@ -734,31 +736,32 @@ public class RatchetSKM extends SessionKeyManager implements SessionTagListener
// inbound // inbound
buf.append("<h3 class=\"debug_inboundsessions\">Ratchet Inbound sessions</h3>" + buf.append("<h3 class=\"debug_inboundsessions\">Ratchet Inbound sessions</h3>" +
"<table>"); "<table>");
Map<SessionKey, Set<RatchetTagSet>> inboundSets = getRatchetTagSetsBySessionKey(); Map<PublicKey, Set<RatchetTagSet>> inboundSets = getRatchetTagSetsByPublicKey();
int total = 0; int total = 0;
int totalSets = 0; int totalSets = 0;
long now = _context.clock().now(); long now = _context.clock().now();
Set<RatchetTagSet> sets = new TreeSet<RatchetTagSet>(new RatchetTagSetComparator()); Set<RatchetTagSet> sets = new TreeSet<RatchetTagSet>(new RatchetTagSetComparator());
for (Map.Entry<SessionKey, Set<RatchetTagSet>> e : inboundSets.entrySet()) { for (Map.Entry<PublicKey, Set<RatchetTagSet>> e : inboundSets.entrySet()) {
SessionKey skey = e.getKey(); PublicKey skey = e.getKey();
sets.clear(); sets.clear();
sets.addAll(e.getValue()); sets.addAll(e.getValue());
totalSets += sets.size(); totalSets += sets.size();
buf.append("<tr><td><b>Session key:</b> ").append(skey.toBase64()).append("</td>" + buf.append("<tr><td><b>Public key:</b> ").append(skey.toBase64()).append("</td>" +
"<td><b>Sets:</b> ").append(sets.size()).append("</td></tr>" + "<td><b>Sets:</b> ").append(sets.size()).append("</td></tr>" +
"<tr class=\"expiry\"><td colspan=\"2\"><ul>"); "<tr class=\"expiry\"><td colspan=\"2\"><ul>");
for (RatchetTagSet ts : sets) { for (RatchetTagSet ts : sets) {
int size = ts.size(); int size = ts.size();
total += size; total += size;
buf.append("<li><b>ID: ").append(ts.getID()) buf.append("<li><b>ID: ").append(ts.getID())
.append(" / ").append(ts.getDebugID()); .append('/').append(ts.getDebugID());
buf.append(" created:</b> ").append(DataHelper.formatTime(ts.getCreated())) // inbound sets are multi-column, keep it short
.append(" <b>last use:</b> ").append(DataHelper.formatTime(ts.getDate())); //buf.append(" created:</b> ").append(DataHelper.formatTime(ts.getCreated()))
// .append(" <b>last use:</b> ").append(DataHelper.formatTime(ts.getDate()));
long expires = ts.getExpiration() - now; long expires = ts.getExpiration() - now;
if (expires > 0) if (expires > 0)
buf.append(" <b>expires in:</b> ").append(DataHelper.formatDuration2(expires)).append(" with "); buf.append(" expires in:</b> ").append(DataHelper.formatDuration2(expires)).append(" with ");
else else
buf.append(" <b>expired:</b> ").append(DataHelper.formatDuration2(0 - expires)).append(" ago with "); buf.append(" expired:</b> ").append(DataHelper.formatDuration2(0 - expires)).append(" ago with ");
buf.append(size).append('/').append(ts.remaining()).append(" tags remaining</li>"); buf.append(size).append('/').append(ts.remaining()).append(" tags remaining</li>");
} }
buf.append("</ul></td></tr>\n"); buf.append("</ul></td></tr>\n");
@ -792,7 +795,7 @@ public class RatchetSKM extends SessionKeyManager implements SessionTagListener
for (RatchetTagSet ts : sets) { for (RatchetTagSet ts : sets) {
int size = ts.remaining(); int size = ts.remaining();
buf.append("<li><b>ID: ").append(ts.getID()) buf.append("<li><b>ID: ").append(ts.getID())
.append(" / ").append(ts.getDebugID()) .append('/').append(ts.getDebugID())
.append(" created:</b> ").append(DataHelper.formatTime(ts.getCreated())) .append(" created:</b> ").append(DataHelper.formatTime(ts.getCreated()))
.append(" <b>last use:</b> ").append(DataHelper.formatTime(ts.getDate())); .append(" <b>last use:</b> ").append(DataHelper.formatTime(ts.getDate()));
long expires = ts.getExpiration() - now; long expires = ts.getExpiration() - now;