Commit 02cad94
Replace native AEAD heuristic with managed retry-and-observe on Android
The previous fix treated any GeneralSecurityException during an Android AEAD
decrypt as an authentication tag mismatch. That depended on Conscrypt
implementation details and risked false positives, so revert the three native
commits (pal_cipher.c / pal_jni.c / pal_jni.h) back to recognizing only a
genuine AEADBadTagException.
Root cause: on AEAD decrypt failure Conscrypt derives the Java exception type
by popping the oldest entry from BoringSSL's thread-local FIFO error queue. A
stale error left by an unrelated prior operation on the same pooled thread
(e.g. an RSA failure) is read first and mapped to the wrong type
(InvalidKeyException), which is not a BadPaddingException and so is never
converted to AEADBadTagException. Conscrypt clears the whole queue after
reading, so the failed attempt leaves a clean queue.
Fix: in the managed Android AEAD DecryptCore, when the first attempt throws a
CryptographicException that is not AuthenticationTagMismatchException, retry the
decrypt once on the same thread. The retry reads a clean error queue and
observes the true result (a real tag mismatch surfaces as AEADBadTagException).
A valid ciphertext authenticates regardless of the queue, so the retry only ever
runs after a genuine failure and can never turn an authentication failure into
unauthenticated plaintext.
Validated on a physical 32-bit Android device (Samsung Galaxy A16, armeabi-v7a):
5x full System.Security.Cryptography test suite, 12373 tests, 0 failures,
including all ChaCha20Poly1305 EncryptTamperAADDecrypt cases.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 2409f7c commit 02cad94
6 files changed
Lines changed: 77 additions & 49 deletions
File tree
- src
- libraries/System.Security.Cryptography/src/System/Security/Cryptography
- native/libs/System.Security.Cryptography.Native.Android
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
128 | 152 | | |
129 | 153 | | |
130 | 154 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
117 | 141 | | |
118 | 142 | | |
119 | 143 | | |
| |||
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
112 | 136 | | |
113 | 137 | | |
114 | 138 | | |
| |||
Lines changed: 5 additions & 42 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
| 79 | + | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
89 | 89 | | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 90 | + | |
128 | 91 | | |
129 | 92 | | |
130 | 93 | | |
| |||
334 | 297 | | |
335 | 298 | | |
336 | 299 | | |
337 | | - | |
| 300 | + | |
338 | 301 | | |
339 | 302 | | |
340 | 303 | | |
| |||
394 | 357 | | |
395 | 358 | | |
396 | 359 | | |
397 | | - | |
| 360 | + | |
398 | 361 | | |
399 | 362 | | |
400 | 363 | | |
| |||
Lines changed: 0 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | 94 | | |
98 | 95 | | |
99 | 96 | | |
| |||
741 | 738 | | |
742 | 739 | | |
743 | 740 | | |
744 | | - | |
745 | 741 | | |
746 | 742 | | |
747 | 743 | | |
| |||
Lines changed: 0 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | 77 | | |
81 | 78 | | |
82 | 79 | | |
| |||
0 commit comments