blob: 4df96f0011a973b44114527bc1d15878327362a2 [file] [log] [blame]
Brad Bishop64c979e2019-11-04 13:55:29 -05001From 1374254c2904ab5b18ba4a890856824a102d4705 Mon Sep 17 00:00:00 2001
2From: Jussi Kivilinna <jussi.kivilinna@iki.fi>
3Date: Sat, 27 Apr 2019 19:33:28 +0300
4Subject: [PATCH 1/3] Prefetch GCM look-up tables
5
6* cipher/cipher-gcm.c (prefetch_table, do_prefetch_tables)
7(prefetch_tables): New.
8(ghash_internal): Call prefetch_tables.
9--
10
11Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
12
13Upstream-Status: Backport
14[https://github.com/gpg/libgcrypt/commit/1374254c2904ab5b18ba4a890856824a102d4705]
15
16CVE: CVE-2019-12904
17
18Signed-off-by: Yi Zhao <yi.zhao@windriver.com>
19---
20 cipher/cipher-gcm.c | 33 +++++++++++++++++++++++++++++++++
21 1 file changed, 33 insertions(+)
22
23diff --git a/cipher/cipher-gcm.c b/cipher/cipher-gcm.c
24index c19f09f..11f119a 100644
25--- a/cipher/cipher-gcm.c
26+++ b/cipher/cipher-gcm.c
27@@ -118,6 +118,34 @@ static const u16 gcmR[256] = {
28 0xbbf0, 0xba32, 0xb874, 0xb9b6, 0xbcf8, 0xbd3a, 0xbf7c, 0xbebe,
29 };
30
31+static inline
32+void prefetch_table(const void *tab, size_t len)
33+{
34+ const volatile byte *vtab = tab;
35+ size_t i;
36+
37+ for (i = 0; i < len; i += 8 * 32)
38+ {
39+ (void)vtab[i + 0 * 32];
40+ (void)vtab[i + 1 * 32];
41+ (void)vtab[i + 2 * 32];
42+ (void)vtab[i + 3 * 32];
43+ (void)vtab[i + 4 * 32];
44+ (void)vtab[i + 5 * 32];
45+ (void)vtab[i + 6 * 32];
46+ (void)vtab[i + 7 * 32];
47+ }
48+
49+ (void)vtab[len - 1];
50+}
51+
52+static inline void
53+do_prefetch_tables (const void *gcmM, size_t gcmM_size)
54+{
55+ prefetch_table(gcmM, gcmM_size);
56+ prefetch_table(gcmR, sizeof(gcmR));
57+}
58+
59 #ifdef GCM_TABLES_USE_U64
60 static void
61 bshift (u64 * b0, u64 * b1)
62@@ -365,6 +393,8 @@ do_ghash (unsigned char *result, const unsigned char *buf, const u32 *gcmM)
63 #define fillM(c) \
64 do_fillM (c->u_mode.gcm.u_ghash_key.key, c->u_mode.gcm.gcm_table)
65 #define GHASH(c, result, buf) do_ghash (result, buf, c->u_mode.gcm.gcm_table)
66+#define prefetch_tables(c) \
67+ do_prefetch_tables(c->u_mode.gcm.gcm_table, sizeof(c->u_mode.gcm.gcm_table))
68
69 #else
70
71@@ -430,6 +460,7 @@ do_ghash (unsigned char *hsub, unsigned char *result, const unsigned char *buf)
72
73 #define fillM(c) do { } while (0)
74 #define GHASH(c, result, buf) do_ghash (c->u_mode.gcm.u_ghash_key.key, result, buf)
75+#define prefetch_tables(c) do {} while (0)
76
77 #endif /* !GCM_USE_TABLES */
78
79@@ -441,6 +472,8 @@ ghash_internal (gcry_cipher_hd_t c, byte *result, const byte *buf,
80 const unsigned int blocksize = GCRY_GCM_BLOCK_LEN;
81 unsigned int burn = 0;
82
83+ prefetch_tables (c);
84+
85 while (nblocks)
86 {
87 burn = GHASH (c, result, buf);
88--
892.7.4
90