Skip to content

Commit 56b795a

Browse files
committed
JNI: use constant-time comparison for GMAC tag verification (F-1519)
1 parent 509435f commit 56b795a

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

IDE/WIN/wolfcryptjni.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<ClInclude Include="..\..\jni\include\wolfcrypt_jni_debug.h" />
7272
<ClInclude Include="..\..\jni\include\wolfcrypt_jni_error.h" />
7373
<ClInclude Include="..\..\jni\include\wolfcrypt_jni_NativeStruct.h" />
74+
<ClInclude Include="..\..\jni\include\wolfcrypt_jni_util.h" />
7475
</ItemGroup>
7576
<ItemGroup>
7677
<ClCompile Include="..\..\jni\jni_aes.c" />

IDE/WIN/wolfcryptjni.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
<ClInclude Include="..\..\jni\include\wolfcrypt_jni_NativeStruct.h">
121121
<Filter>Header Files</Filter>
122122
</ClInclude>
123+
<ClInclude Include="..\..\jni\include\wolfcrypt_jni_util.h">
124+
<Filter>Header Files</Filter>
125+
</ClInclude>
123126
</ItemGroup>
124127
<ItemGroup>
125128
<ClCompile Include="..\..\jni\jni_aes.c">

jni/include/wolfcrypt_jni_util.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* wolfcrypt_jni_util.h
2+
*
3+
* Copyright (C) 2006-2026 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
#ifndef _Included_wolfcrypt_jni_util
23+
#define _Included_wolfcrypt_jni_util
24+
25+
#include <wolfssl/wolfcrypt/types.h>
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
/* check all length bytes for equality, return 0 on success */
32+
static WC_INLINE int ConstantCompare(const byte* a, const byte* b, int length)
33+
{
34+
int i;
35+
int compareSum = 0;
36+
37+
for (i = 0; i < length; i++) {
38+
compareSum |= a[i] ^ b[i];
39+
}
40+
41+
return compareSum;
42+
}
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
#endif
48+

jni/jni_aesgmac.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
/* #define WOLFCRYPT_JNI_DEBUG_ON */
3636
#include <wolfcrypt_jni_debug.h>
37+
#include <wolfcrypt_jni_util.h>
3738

3839
JNIEXPORT jlong JNICALL Java_com_wolfssl_wolfcrypt_AesGmac_mallocNativeStruct_1internal(
3940
JNIEnv* env, jobject this)
@@ -326,7 +327,8 @@ JNIEXPORT jint JNICALL Java_com_wolfssl_wolfcrypt_AesGmac_wc_1GmacVerify(
326327

327328
if (ret == 0) {
328329
/* Compare the computed tag with the provided tag */
329-
if (XMEMCMP(computedTag, authTag, authTagSz) != 0) {
330+
if (ConstantCompare(computedTag, authTag,
331+
authTagSz) != 0) {
330332
ret = AES_GCM_AUTH_E; /* Authentication failure */
331333
}
332334
}

0 commit comments

Comments
 (0)