Skip to content

Commit 6bf37e5

Browse files
jamesyonanherbertx
authored andcommitted
crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks
When comparing MAC hashes, AEAD authentication tags, or other hash values in the context of authentication or integrity checking, it is important not to leak timing information to a potential attacker, i.e. when communication happens over a network. Bytewise memory comparisons (such as memcmp) are usually optimized so that they return a nonzero value as soon as a mismatch is found. E.g, on x86_64/i5 for 512 bytes this can be ~50 cyc for a full mismatch and up to ~850 cyc for a full match (cold). This early-return behavior can leak timing information as a side channel, allowing an attacker to iteratively guess the correct result. This patch adds a new method crypto_memneq ("memory not equal to each other") to the crypto API that compares memory areas of the same length in roughly "constant time" (cache misses could change the timing, but since they don't reveal information about the content of the strings being compared, they are effectively benign). Iow, best and worst case behaviour take the same amount of time to complete (in contrast to memcmp). Note that crypto_memneq (unlike memcmp) can only be used to test for equality or inequality, NOT for lexicographical order. This, however, is not an issue for its use-cases within the crypto API. We tried to locate all of the places in the crypto API where memcmp was being used for authentication or integrity checking, and convert them over to crypto_memneq. crypto_memneq is declared noinline, placed in its own source file, and compiled with optimizations that might increase code size disabled ("Os") because a smart compiler (or LTO) might notice that the return value is always compared against zero/nonzero, and might then reintroduce the same early-return optimization that we are trying to avoid. Using #pragma or __attribute__ optimization annotations of the code for disabling optimization was avoided as it seems to be considered broken or unmaintained for long time in GCC [1]. Therefore, we work around that by specifying the compile flag for memneq.o directly in the Makefile. We found that this seems to be most appropriate. As we use ("Os"), this patch also provides a loop-free "fast-path" for frequently used 16 byte digests. Similarly to kernel library string functions, leave an option for future even further optimized architecture specific assembler implementations. This was a joint work of James Yonan and Daniel Borkmann. Also thanks for feedback from Florian Weimer on this and earlier proposals [2]. [1] http://gcc.gnu.org/ml/gcc/2012-07/msg00211.html [2] https://lkml.org/lkml/2013/2/10/131 Signed-off-by: James Yonan <james@openvpn.net> Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Cc: Florian Weimer <fw@deneb.enyo.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent d319fe2 commit 6bf37e5

8 files changed

Lines changed: 174 additions & 14 deletions

File tree

‎crypto/Makefile‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
# Cryptographic API
33
#
44

5+
# memneq MUST be built with -Os or -O0 to prevent early-return optimizations
6+
# that will defeat memneq's actual purpose to prevent timing attacks.
7+
CFLAGS_REMOVE_memneq.o := -O1 -O2 -O3
8+
CFLAGS_memneq.o := -Os
9+
510
obj-$(CONFIG_CRYPTO) += crypto.o
6-
crypto-y := api.o cipher.o compress.o
11+
crypto-y := api.o cipher.o compress.o memneq.o
712

813
obj-$(CONFIG_CRYPTO_WORKQUEUE) += crypto_wq.o
914

‎crypto/asymmetric_keys/rsa.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/module.h>
1414
#include <linux/kernel.h>
1515
#include <linux/slab.h>
16+
#include <crypto/algapi.h>
1617
#include "public_key.h"
1718

1819
MODULE_LICENSE("GPL");
@@ -189,12 +190,12 @@ static int RSA_verify(const u8 *H, const u8 *EM, size_t k, size_t hash_size,
189190
}
190191
}
191192

192-
if (memcmp(asn1_template, EM + T_offset, asn1_size) != 0) {
193+
if (crypto_memneq(asn1_template, EM + T_offset, asn1_size) != 0) {
193194
kleave(" = -EBADMSG [EM[T] ASN.1 mismatch]");
194195
return -EBADMSG;
195196
}
196197

197-
if (memcmp(H, EM + T_offset + asn1_size, hash_size) != 0) {
198+
if (crypto_memneq(H, EM + T_offset + asn1_size, hash_size) != 0) {
198199
kleave(" = -EKEYREJECTED [EM[T] hash mismatch]");
199200
return -EKEYREJECTED;
200201
}

‎crypto/authenc.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static void authenc_verify_ahash_update_done(struct crypto_async_request *areq,
188188
scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
189189
authsize, 0);
190190

191-
err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
191+
err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
192192
if (err)
193193
goto out;
194194

@@ -227,7 +227,7 @@ static void authenc_verify_ahash_done(struct crypto_async_request *areq,
227227
scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
228228
authsize, 0);
229229

230-
err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
230+
err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
231231
if (err)
232232
goto out;
233233

@@ -462,7 +462,7 @@ static int crypto_authenc_verify(struct aead_request *req,
462462
ihash = ohash + authsize;
463463
scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
464464
authsize, 0);
465-
return memcmp(ihash, ohash, authsize) ? -EBADMSG : 0;
465+
return crypto_memneq(ihash, ohash, authsize) ? -EBADMSG : 0;
466466
}
467467

468468
static int crypto_authenc_iverify(struct aead_request *req, u8 *iv,

‎crypto/authencesn.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void authenc_esn_verify_ahash_update_done(struct crypto_async_request *ar
247247
scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
248248
authsize, 0);
249249

250-
err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
250+
err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
251251
if (err)
252252
goto out;
253253

@@ -296,7 +296,7 @@ static void authenc_esn_verify_ahash_update_done2(struct crypto_async_request *a
296296
scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
297297
authsize, 0);
298298

299-
err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
299+
err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
300300
if (err)
301301
goto out;
302302

@@ -336,7 +336,7 @@ static void authenc_esn_verify_ahash_done(struct crypto_async_request *areq,
336336
scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
337337
authsize, 0);
338338

339-
err = memcmp(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
339+
err = crypto_memneq(ihash, ahreq->result, authsize) ? -EBADMSG : 0;
340340
if (err)
341341
goto out;
342342

@@ -568,7 +568,7 @@ static int crypto_authenc_esn_verify(struct aead_request *req)
568568
ihash = ohash + authsize;
569569
scatterwalk_map_and_copy(ihash, areq_ctx->sg, areq_ctx->cryptlen,
570570
authsize, 0);
571-
return memcmp(ihash, ohash, authsize) ? -EBADMSG : 0;
571+
return crypto_memneq(ihash, ohash, authsize) ? -EBADMSG : 0;
572572
}
573573

574574
static int crypto_authenc_esn_iverify(struct aead_request *req, u8 *iv,

‎crypto/ccm.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ static void crypto_ccm_decrypt_done(struct crypto_async_request *areq,
363363

364364
if (!err) {
365365
err = crypto_ccm_auth(req, req->dst, cryptlen);
366-
if (!err && memcmp(pctx->auth_tag, pctx->odata, authsize))
366+
if (!err && crypto_memneq(pctx->auth_tag, pctx->odata, authsize))
367367
err = -EBADMSG;
368368
}
369369
aead_request_complete(req, err);
@@ -422,7 +422,7 @@ static int crypto_ccm_decrypt(struct aead_request *req)
422422
return err;
423423

424424
/* verify */
425-
if (memcmp(authtag, odata, authsize))
425+
if (crypto_memneq(authtag, odata, authsize))
426426
return -EBADMSG;
427427

428428
return err;

‎crypto/gcm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ static int crypto_gcm_verify(struct aead_request *req,
582582

583583
crypto_xor(auth_tag, iauth_tag, 16);
584584
scatterwalk_map_and_copy(iauth_tag, req->src, cryptlen, authsize, 0);
585-
return memcmp(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
585+
return crypto_memneq(iauth_tag, auth_tag, authsize) ? -EBADMSG : 0;
586586
}
587587

588588
static void gcm_decrypt_done(struct crypto_async_request *areq, int err)

‎crypto/memneq.c‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Constant-time equality testing of memory regions.
3+
*
4+
* Authors:
5+
*
6+
* James Yonan <james@openvpn.net>
7+
* Daniel Borkmann <dborkman@redhat.com>
8+
*
9+
* This file is provided under a dual BSD/GPLv2 license. When using or
10+
* redistributing this file, you may do so under either license.
11+
*
12+
* GPL LICENSE SUMMARY
13+
*
14+
* Copyright(c) 2013 OpenVPN Technologies, Inc. All rights reserved.
15+
*
16+
* This program is free software; you can redistribute it and/or modify
17+
* it under the terms of version 2 of the GNU General Public License as
18+
* published by the Free Software Foundation.
19+
*
20+
* This program is distributed in the hope that it will be useful, but
21+
* WITHOUT ANY WARRANTY; without even the implied warranty of
22+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23+
* General Public License for more details.
24+
*
25+
* You should have received a copy of the GNU General Public License
26+
* along with this program; if not, write to the Free Software
27+
* Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
28+
* The full GNU General Public License is included in this distribution
29+
* in the file called LICENSE.GPL.
30+
*
31+
* BSD LICENSE
32+
*
33+
* Copyright(c) 2013 OpenVPN Technologies, Inc. All rights reserved.
34+
*
35+
* Redistribution and use in source and binary forms, with or without
36+
* modification, are permitted provided that the following conditions
37+
* are met:
38+
*
39+
* * Redistributions of source code must retain the above copyright
40+
* notice, this list of conditions and the following disclaimer.
41+
* * Redistributions in binary form must reproduce the above copyright
42+
* notice, this list of conditions and the following disclaimer in
43+
* the documentation and/or other materials provided with the
44+
* distribution.
45+
* * Neither the name of OpenVPN Technologies nor the names of its
46+
* contributors may be used to endorse or promote products derived
47+
* from this software without specific prior written permission.
48+
*
49+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60+
*/
61+
62+
#include <crypto/algapi.h>
63+
64+
#ifndef __HAVE_ARCH_CRYPTO_MEMNEQ
65+
66+
/* Generic path for arbitrary size */
67+
static inline unsigned long
68+
__crypto_memneq_generic(const void *a, const void *b, size_t size)
69+
{
70+
unsigned long neq = 0;
71+
72+
#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
73+
while (size >= sizeof(unsigned long)) {
74+
neq |= *(unsigned long *)a ^ *(unsigned long *)b;
75+
a += sizeof(unsigned long);
76+
b += sizeof(unsigned long);
77+
size -= sizeof(unsigned long);
78+
}
79+
#endif /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */
80+
while (size > 0) {
81+
neq |= *(unsigned char *)a ^ *(unsigned char *)b;
82+
a += 1;
83+
b += 1;
84+
size -= 1;
85+
}
86+
return neq;
87+
}
88+
89+
/* Loop-free fast-path for frequently used 16-byte size */
90+
static inline unsigned long __crypto_memneq_16(const void *a, const void *b)
91+
{
92+
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
93+
if (sizeof(unsigned long) == 8)
94+
return ((*(unsigned long *)(a) ^ *(unsigned long *)(b))
95+
| (*(unsigned long *)(a+8) ^ *(unsigned long *)(b+8)));
96+
else if (sizeof(unsigned int) == 4)
97+
return ((*(unsigned int *)(a) ^ *(unsigned int *)(b))
98+
| (*(unsigned int *)(a+4) ^ *(unsigned int *)(b+4))
99+
| (*(unsigned int *)(a+8) ^ *(unsigned int *)(b+8))
100+
| (*(unsigned int *)(a+12) ^ *(unsigned int *)(b+12)));
101+
else
102+
#endif /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */
103+
return ((*(unsigned char *)(a) ^ *(unsigned char *)(b))
104+
| (*(unsigned char *)(a+1) ^ *(unsigned char *)(b+1))
105+
| (*(unsigned char *)(a+2) ^ *(unsigned char *)(b+2))
106+
| (*(unsigned char *)(a+3) ^ *(unsigned char *)(b+3))
107+
| (*(unsigned char *)(a+4) ^ *(unsigned char *)(b+4))
108+
| (*(unsigned char *)(a+5) ^ *(unsigned char *)(b+5))
109+
| (*(unsigned char *)(a+6) ^ *(unsigned char *)(b+6))
110+
| (*(unsigned char *)(a+7) ^ *(unsigned char *)(b+7))
111+
| (*(unsigned char *)(a+8) ^ *(unsigned char *)(b+8))
112+
| (*(unsigned char *)(a+9) ^ *(unsigned char *)(b+9))
113+
| (*(unsigned char *)(a+10) ^ *(unsigned char *)(b+10))
114+
| (*(unsigned char *)(a+11) ^ *(unsigned char *)(b+11))
115+
| (*(unsigned char *)(a+12) ^ *(unsigned char *)(b+12))
116+
| (*(unsigned char *)(a+13) ^ *(unsigned char *)(b+13))
117+
| (*(unsigned char *)(a+14) ^ *(unsigned char *)(b+14))
118+
| (*(unsigned char *)(a+15) ^ *(unsigned char *)(b+15)));
119+
}
120+
121+
/* Compare two areas of memory without leaking timing information,
122+
* and with special optimizations for common sizes. Users should
123+
* not call this function directly, but should instead use
124+
* crypto_memneq defined in crypto/algapi.h.
125+
*/
126+
noinline unsigned long __crypto_memneq(const void *a, const void *b,
127+
size_t size)
128+
{
129+
switch (size) {
130+
case 16:
131+
return __crypto_memneq_16(a, b);
132+
default:
133+
return __crypto_memneq_generic(a, b, size);
134+
}
135+
}
136+
EXPORT_SYMBOL(__crypto_memneq);
137+
138+
#endif /* __HAVE_ARCH_CRYPTO_MEMNEQ */

‎include/crypto/algapi.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,21 @@ static inline int crypto_requires_sync(u32 type, u32 mask)
386386
return (type ^ CRYPTO_ALG_ASYNC) & mask & CRYPTO_ALG_ASYNC;
387387
}
388388

389-
#endif /* _CRYPTO_ALGAPI_H */
389+
noinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size);
390+
391+
/**
392+
* crypto_memneq - Compare two areas of memory without leaking
393+
* timing information.
394+
*
395+
* @a: One area of memory
396+
* @b: Another area of memory
397+
* @size: The size of the area.
398+
*
399+
* Returns 0 when data is equal, 1 otherwise.
400+
*/
401+
static inline int crypto_memneq(const void *a, const void *b, size_t size)
402+
{
403+
return __crypto_memneq(a, b, size) != 0UL ? 1 : 0;
404+
}
390405

406+
#endif /* _CRYPTO_ALGAPI_H */

0 commit comments

Comments
 (0)