Soatok<p><strong>Don’t Use Session (Signal Fork)</strong></p><p>Last year, I outlined the specific requirements that an app needs to have in order for me to <a href="https://soatok.blog/2024/07/31/what-does-it-mean-to-be-a-signal-competitor/" rel="nofollow noopener noreferrer" target="_blank">consider it a Signal competitor</a>.</p><p>Afterwards, I had several people ask me what I think of a Signal fork called Session. My answer then is the same thing I’ll say today: </p><p><strong>Don’t use Session.</strong></p><p>The main reason I said to avoid Session, all those months ago, was simply due to <a href="https://web.archive.org/web/20241225131654/https://getsession.org/session-protocol-explained" rel="nofollow noopener noreferrer" target="_blank">their decision to remove forward secrecy</a> (which is an important security property of cryptographic protocols they inherited for free when they forked libsignal).</p><p>Lack of forward secrecy puts you in the scope of <a href="https://www.cryptologie.net/article/372/key-compromise-impersonation-attacks-kci/" rel="nofollow noopener noreferrer" target="_blank">Key Compromise Impersonation (KCI) attacks</a>, which serious end-to-end encryption apps should prevent if they want to sit at the adults table. This is <a href="https://github.com/TokTok/c-toxcore/issues/426" rel="nofollow noopener noreferrer" target="_blank">why I don’t recommend Tox</a>.</p><p>And that observation alone should have been enough for anyone to <strong>run, screaming, in the other direction from Session</strong>. After all, removing important security properties from a cryptographic security protocol is exactly the sort of thing a malicious government would do (especially if the cover story for such a change involves the introduction of swarms and “onion routing”–which computer criminals might think sounds attractive due to their familiarity with the Tor network).</p><p>Unfortunately, some people love to dig their heels in about messaging apps. So let’s take a closer look at Session.</p><blockquote><p>I did not disclose this blog post privately to the Session developers before pressing publish.</p><p>I do not feel that cryptographic issues always require coordinated disclosure with the software vendor. <a href="https://www.schneier.com/essays/archives/2007/01/schneier_full_disclo.html" rel="nofollow noopener noreferrer" target="_blank">As Bruce Schneier argues</a>, full disclosure of security vulnerabilities is a “damned good idea”.</p></blockquote><p>I have separated this blog post into two sections: Security Issues and Gripes.</p><p><strong>Security Issues</strong></p><ol><li>Insufficient Entropy in Ed25519 Keys</li><li>In-Band Negotiation for Message Signatures</li><li>Using Public Keys as AES-GCM Keys</li></ol><p><strong>Insufficient Entropy in Ed25519 Keys</strong></p><p>One of the departures of Session from Signal is the use of Ed25519 rather than X25519 for everything.</p><p>Ed25519 Keypairs generated from their <code>KeyPairUtilities</code> object <a href="https://github.com/session-foundation/session-android/blob/75e2b87278cc378e21b77b27fa1a2aa773d25520/app/src/main/java/org/thoughtcrime/securesms/crypto/KeyPairUtilities.kt#L15-L28" rel="nofollow noopener noreferrer" target="_blank">only have 128 bits of entropy</a>, rather than the ~253 bits (after clamping) you’d expect from an Ed25519 seed.</p> <pre>fun generate(): KeyPairGenerationResult { val seed = sodium.randomBytesBuf(16) try { return generate(seed) } catch (exception: Exception) { return generate() }}fun generate(seed: ByteArray): KeyPairGenerationResult { val padding = ByteArray(16) { 0 } val ed25519KeyPair = sodium.cryptoSignSeedKeypair(seed + padding)</pre> <p>As an implementation detail, they encode a recovery key as a “mnemonic” (see also: a gripe about their mnemonic decoding).</p><p><strong>Does This Matter?</strong></p><p>You might think that clearing the highest 128 bits of the Ed25519 seed is fine for one of the following reasons:</p><ol><li>It’s hashed with SHA512 before clamping.</li><li>Ed25519 only offers 128 bits of security.</li><li>Some secret third (and possibly unreasonable) argument.</li></ol><p>It’s true that Ed25519 targets the 128-bit security level, if you’re focused on the security of the Elliptic Curve Discrete Logarithm Problem (ECDLP).</p><p>Achieving 128 bits of security in this model requires 256-bit secrets, since the best attack against the ECDLP finds a discrete logarithm in guesses.</p><p>Additionally, having 256-bit secrets makes the multi-user security of the scheme easy to reason about, whereas 128-bit secrets makes it a lot harder. (This mostly comes up in criticism of AES, which has a 128-bit block size.)</p><p>When your secret only has possible values, your multi-user security is no longer as secure as Ed25519 expects.</p><p>Additionally, you can shove the SHA512 + clamping in your attack script (thus negating the first objection) and find the corresponding secret key in queries if you know the top 128 bits were initialized to 0, using a modified version of Pollard’s rho for discrete logarithms.</p><p>This means that Session’s <code>KeyPairUtilities</code> class only provides 64 bits of ECDLP security.</p> <a href="https://cmykat.carrd.co/" rel="nofollow noopener noreferrer" target="_blank">CMYKat</a> <p><strong>What does 64 bits of ECDLP Security actually mean?</strong></p><p>I provided a technical definition already, but that’s probably not meaningful to most people outside computer security.</p><p>What this means is that a distributed computing effort can find the secret key for a given Ed25519 public key generated from this algorithm in only queries.</p><p>For flavor, queries is approximately the attack cost to find a SHA1 collision, <a href="https://shattered.io/" rel="nofollow noopener noreferrer" target="_blank">which we know is possible and economical</a>.</p><blockquote><p>Based on this attack, the authors projected that a collision attack on SHA-1 may cost between US$75K and US$120K by renting GPU computing time on Amazon EC2 using spot-instances, which is significantly lower than Schneier’s 2012 estimates.</p><p>— from the <a href="https://shattered.io/static/shattered.pdf" rel="nofollow noopener noreferrer" target="_blank">Shattered paper</a>, page 2.</p></blockquote><p>I don’t know if this was mere stupidity or an intentional NOBUS backdoor that only well-resourced adversaries can crack. (I also don’t have hundreds of thousands of dollars lying around to test this myself.)</p><p><strong>How would you exploit this in practice?</strong></p><p>If you’re not familiar with Pollard’s rho, then this section might be a bit abstract and difficult to follow.</p><p>Instead of directly passing a full 256-bit value to your oracle with each iteration (like you do with a standard Pollard’s rho implementation), you would need mutate the output the same way Session does (n.b., replace 128 bits of the seed with zeroes), hash & clamp that, and then perform the scalar multiplication.</p><p>It <em>should</em> be a bit more expensive than a raw ECDLP attack against a 128-bit curve (due to the hashing), but the strategy should succeed in the expected number of queries (average case).</p><p>Although this makes the attack totally feasible for a nation state, I do not have the resources to build and test a proof of concept against a candidate keypair. If anyone does, get in touch, it would make for a fun research project.</p> <a href="https://cmykat.carrd.co/" rel="nofollow noopener noreferrer" target="_blank">CMYKat</a> <p>Alternatively, <a href="https://en.wikipedia.org/wiki/Pollard%27s_kangaroo_algorithm" rel="nofollow noopener noreferrer" target="_blank">Pollard’s kangaroo</a> might be a better cryptanalysis technique for Session’s setup.</p><blockquote><p>Note: If there is any classified government algorithm especially suited for cracking Ed25519 keys constructed exactly like Session does, it’s not one I’ve ever heard of. I don’t have any security clearances, nor do I want one.</p><p>However, ECDLP security of elliptic curve-based protocols is extremely well-understood in the cryptography literature.</p></blockquote><p><strong>In-Band Negotiation for Message Signatures</strong></p><p>If you thought the previous issue was mitigated by the use of Ed25519 signatures on each message, don’t worry, <a href="https://github.com/session-foundation/session-android/blob/75e2b87278cc378e21b77b27fa1a2aa773d25520/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageDecrypter.kt#L44-L56" rel="nofollow noopener noreferrer" target="_blank">the Session developers screwed this up too</a>!</p> <pre>// 2. ) Get the message partsval signature = plaintextWithMetadata.sliceArray(plaintextWithMetadata.size - signatureSize until plaintextWithMetadata.size)val senderED25519PublicKey = plaintextWithMetadata.sliceArray(plaintextWithMetadata.size - (signatureSize + ed25519PublicKeySize) until plaintextWithMetadata.size - signatureSize)val plaintext = plaintextWithMetadata.sliceArray(0 until plaintextWithMetadata.size - (signatureSize + ed25519PublicKeySize))// 3. ) Verify the signatureval verificationData = (plaintext + senderED25519PublicKey + recipientX25519PublicKey)try { val isValid = sodium.cryptoSignVerifyDetached(signature, verificationData, verificationData.size, senderED25519PublicKey) if (!isValid) { throw Error.InvalidSignature }} catch (exception: Exception) { Log.d("Loki", "Couldn't verify message signature due to error: $exception.") throw Error.InvalidSignature}</pre> <p>What this code is doing (after decryption):</p><ol><li>Grab the public key from the payload.</li><li>Grab the signature from the payload.</li><li>Verify that the signature on the rest of the payload is valid… <strong>for the public key that was included in the payload</strong>.</li></ol><p>Congratulations, Session, you successfully reduced the utility of Ed25519 to that of a CRC32!</p> Art: <a href="https://bsky.app/profile/ajlovesdinos.bsky.social" rel="nofollow noopener noreferrer" target="_blank">AJ</a> <p><strong>Using Public Keys As AES-GCM Keys</strong></p><p>I wasn’t entirely sure whether this belongs in the “gripes” section or not, because it’s so blatantly stupid that there’s basically no way <a href="https://web.archive.org/web/20250115034416/https://blog.quarkslab.com/resources/2021-05-04_audit-of-session-secure-messaging-application/20-08-Oxen-REP-v1.4.pdf" rel="nofollow noopener noreferrer" target="_blank">Quarkslab would miss it if it mattered</a>.</p><p>When encrypting payloads <a href="https://github.com/session-foundation/session-android/blob/75e2b87278cc378e21b77b27fa1a2aa773d25520/libsession/src/main/java/org/session/libsession/snode/OnionRequestEncryption.kt#L56-L57" rel="nofollow noopener noreferrer" target="_blank">for onion routing</a>, it uses the X25519 public key… as a symmetric key, for AES-GCM. See, <code>encryptPayloadForDestination()</code>.</p> <pre>val result = AESGCM.encrypt(plaintext, x25519PublicKey)deferred.resolve(result)</pre> <p>Session<a href="https://github.com/session-foundation/session-android/blob/75e2b87278cc378e21b77b27fa1a2aa773d25520/libsession/src/main/java/org/session/libsession/snode/OnionRequestEncryption.kt#L95-L96" rel="nofollow noopener noreferrer" target="_blank"> also does</a> this inside of <code>encryptHop()</code>.</p> <pre>val plaintext = encode(previousEncryptionResult.ciphertext, payload)val result = AESGCM.encrypt(plaintext, x25519PublicKey)</pre> <p>In case you thought, maybe, that this is just a poorly named HPKE wrapper… <a href="https://github.com/session-foundation/session-android/blob/75e2b87278cc378e21b77b27fa1a2aa773d25520/libsession/src/main/java/org/session/libsession/utilities/AESGCM.kt#L48-L58" rel="nofollow noopener noreferrer" target="_blank">nope</a>!</p> <pre> /** * Sync. Don't call from the main thread. */internal fun encrypt(plaintext: ByteArray, symmetricKey: ByteArray): ByteArray { val iv = Util.getSecretBytes(ivSize) synchronized(CIPHER_LOCK) { val cipher = Cipher.getInstance("AES/GCM/NoPadding") cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(symmetricKey, "AES"), GCMParameterSpec(gcmTagSize, iv)) return ByteUtil.combine(iv, cipher.doFinal(plaintext)) }}</pre> <p>This obviously doesn’t encrypt it such that only the recipient (that owns the secret key corresponding to the public key) can decrypt the message. It makes it to where anyone that knows the public key can decrypt it.</p><p><em>I wonder if this impacts their onion routing assumptions?</em></p><blockquote><p><strong>Why should I trust session?</strong></p><p>(…)</p><p>When using Session, your messages are sent to their destinations through a decentralised onion routing network similar to Tor (with a few key differences) (…)</p><p><a href="https://web.archive.org/web/20250102225433/https://getsession.org/faq#trust-session" rel="nofollow noopener noreferrer" target="_blank">Session FAQs</a></p></blockquote><p><strong>Gripes</strong></p><p>Some of these aren’t really security issues, but are things I found annoying as a security engineer that specializes in applied cryptography.</p><ol><li>Mnemonic Decoding Isn’t Constant-Time</li><li>Unsafe Use of SecureRandom on Android</li></ol><p><strong>Mnemonic Decoding Isn’t Constant-Time</strong></p><p><a href="https://github.com/session-foundation/session-android/blob/75e2b87278cc378e21b77b27fa1a2aa773d25520/libsignal/src/main/java/org/session/libsignal/crypto/MnemonicCodec.kt#L107-L112" rel="nofollow noopener noreferrer" target="_blank">The way mnemonics are decoded involves the modulo operator</a>, which implicitly uses integer division (which neither Java nor Kotlin nor Swift implement in constant-time).</p> <pre>return wordIndexes.windowed(3, 3) { (w1, w2, w3) -> val x = w1 + n * ((n - w1 + w2) % n) + n * n * ((n - w2 + w3) % n) if (x % n != w1.toLong()) throw DecodingError.Generic val string = "0000000" + x.toString(16) swap(string.substring(string.length - 8 until string.length))}.joinToString(separator = "") { it }</pre> <p>This isn’t a real security problem, but I did find it <a href="https://soatok.blog/2020/08/27/soatoks-guide-to-side-channel-attacks/#integer-division" rel="nofollow noopener noreferrer" target="_blank">annoying to see</a> in an app <a href="https://old.reddit.com/r/privacy/comments/jy3hjo/signal_vs_session_private_messenger/hgzef3y/" rel="nofollow noopener noreferrer" target="_blank">evangelized as “better than Signal”</a> on privacy forums.</p><p><strong>Unsafe Use of SecureRandom on Android</strong></p><p><a href="https://stackoverflow.com/questions/27622625/securerandom-with-nativeprng-vs-sha1prng/27638413#27638413" rel="nofollow noopener noreferrer" target="_blank">The recommended way to get secure random numbers on Android</a> (or any Java or Kotlin software, really) is simply <code>new SecureRandom()</code>. If you’re running a service in a high-demand environment, you can take extra care to make a <a href="https://stackoverflow.com/a/34340717" rel="nofollow noopener noreferrer" target="_blank">thread-local instance of SecureRandom</a>. But a local RNG for a single user isn’t that.</p><p><strong>What does Session do?</strong> <a href="https://github.com/session-foundation/session-android/blob/75e2b87278cc378e21b77b27fa1a2aa773d25520/libsignal/src/main/java/org/session/libsignal/utilities/Util.java#L71-L79" rel="nofollow noopener noreferrer" target="_blank">They use SHA1PRNG</a>, of course.</p> <pre>public static byte[] getSecretBytes(int size) { try { byte[] secret = new byte[size]; SecureRandom.getInstance("SHA1PRNG").nextBytes(secret); return secret; } catch (NoSuchAlgorithmException e) { throw new AssertionError(e); }}</pre> <p>And again <a href="https://github.com/session-foundation/session-android/blob/75e2b87278cc378e21b77b27fa1a2aa773d25520/libsignal/src/main/java/org/session/libsignal/utilities/KeyHelper.java#L32" rel="nofollow noopener noreferrer" target="_blank">here</a>.</p> <pre>SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");</pre> <p><strong>Why would anyone care about this?</strong></p><p>On modern Android devices, this isn’t a major concern, but the use of SHA1PRNG <a href="https://web.archive.org/web/20240601000000*/https://blog.k3170makan.com/2013/08/more-details-on-android-jca-prng-flaw.html" rel="nofollow noopener noreferrer" target="_blank">used to be a source of vulnerabilities in Android apps</a>. (See also:<a href="https://web.archive.org/web/20240301002810/https://www-fourier.ujf-grenoble.fr/JC2/exposes/ruhault.pdf" rel="nofollow noopener noreferrer" target="_blank"> this slide deck</a>.)</p><p><strong>Closing Thoughts</strong></p><p>There are a lot of Session’s design decisions that are poorly specified in their Whitepaper and I didn’t look at. For example, how group messaging keys are managed.</p><p>When I did try to skim that part of the code, I did find a component where you can coerce Android clients into running a moderately expensive Argon2 KDF <a href="https://github.com/session-foundation/session-android/blob/75e2b87278cc378e21b77b27fa1a2aa773d25520/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt#L237-L264" rel="nofollow noopener noreferrer" target="_blank">by simply deleting the <code>nonce</code> from the message</a>.</p> <pre>val isArgon2Based = (intermediate["nonce"] == null)if (isArgon2Based) { // Handle old Argon2-based encryption used before HF16</pre> <p><strong>That’s hilarious.</strong></p><p>Cryptography nerds should NOT be finding the software that activists trust with their privacy <em>hilarious</em>.</p> <a href="https://cmykat.carrd.co/" rel="nofollow noopener noreferrer" target="_blank">CMYKat</a> <p>So if you were wondering what my opinion on Session is, now you know: <strong>Don’t use Session.</strong> Don’t let your friends use Session.</p><p><a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/aes-gcm/" target="_blank">#AESGCM</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/android/" target="_blank">#Android</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/asymmetric-cryptography/" target="_blank">#asymmetricCryptography</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/cryptography/" target="_blank">#cryptography</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/e2ee/" target="_blank">#E2EE</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/ed25519/" target="_blank">#Ed25519</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/java/" target="_blank">#Java</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/kotlin/" target="_blank">#Kotlin</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/messaging-apps/" target="_blank">#messagingApps</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/online-privacy/" target="_blank">#OnlinePrivacy</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/private-messaging/" target="_blank">#privateMessaging</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/session/" target="_blank">#Session</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/signal/" target="_blank">#Signal</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/signal-alternatives/" target="_blank">#SignalAlternatives</a> <a rel="nofollow noopener noreferrer" class="hashtag u-tag u-category" href="https://soatok.blog/tag/vulnerability/" target="_blank">#vuln</a></p>