1 From 119348dd9aa52ab229afb5e2d3342d2b76fe81bf Mon Sep 17 00:00:00 2001
2 From: Jussi Kivilinna <jussi.kivilinna@iki.fi>
3 Date: Fri, 31 May 2019 17:18:09 +0300
4 Subject: [PATCH 2/3] AES: move look-up tables to .data section and unshare between
7 * cipher/rijndael-internal.h (ATTR_ALIGNED_64): New.
8 * cipher/rijndael-tables.h (encT): Move to 'enc_tables' structure.
9 (enc_tables): New structure for encryption table with counters before
12 (dec_tables): Add counters before and after encryption table; Move
13 from .rodata to .data section.
14 (do_encrypt): Change 'encT' to 'enc_tables.T'.
15 (do_decrypt): Change '&dec_tables' to 'dec_tables.T'.
16 * cipher/cipher-gcm.c (prefetch_table): Make inline; Handle input
17 with length not multiple of 256.
18 (prefetch_enc, prefetch_dec): Modify pre- and post-table counters
19 to unshare look-up table pages between processes.
23 Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
25 Upstream-Status: Backport
26 [https://github.com/gpg/libgcrypt/commit/daedbbb5541cd8ecda1459d3b843ea4d92788762]
30 Signed-off-by: Yi Zhao <yi.zhao@windriver.com>
32 cipher/rijndael-internal.h | 4 +-
33 cipher/rijndael-tables.h | 155 +++++++++++++++++++++++++--------------------
34 cipher/rijndael.c | 35 ++++++++--
35 3 files changed, 118 insertions(+), 76 deletions(-)
37 diff --git a/cipher/rijndael-internal.h b/cipher/rijndael-internal.h
38 index 160fb8c..a62d4b7 100644
39 --- a/cipher/rijndael-internal.h
40 +++ b/cipher/rijndael-internal.h
42 #define BLOCKSIZE (128/8)
45 -/* Helper macro to force alignment to 16 bytes. */
46 +/* Helper macro to force alignment to 16 or 64 bytes. */
47 #ifdef HAVE_GCC_ATTRIBUTE_ALIGNED
48 # define ATTR_ALIGNED_16 __attribute__ ((aligned (16)))
49 +# define ATTR_ALIGNED_64 __attribute__ ((aligned (64)))
51 # define ATTR_ALIGNED_16
52 +# define ATTR_ALIGNED_64
56 diff --git a/cipher/rijndael-tables.h b/cipher/rijndael-tables.h
57 index 8359470..b54d959 100644
58 --- a/cipher/rijndael-tables.h
59 +++ b/cipher/rijndael-tables.h
61 /* To keep the actual implementation at a readable size we use this
62 include file to define the tables. */
64 -static const u32 encT[256] =
67 + volatile u32 counter_head;
68 + u32 cacheline_align[64 / 4 - 1];
70 + volatile u32 counter_tail;
71 +} enc_tables ATTR_ALIGNED_64 =
73 - 0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6,
74 - 0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591,
75 - 0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56,
76 - 0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec,
77 - 0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa,
78 - 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb,
79 - 0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45,
80 - 0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b,
81 - 0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c,
82 - 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83,
83 - 0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9,
84 - 0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a,
85 - 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d,
86 - 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f,
87 - 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df,
88 - 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea,
89 - 0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34,
90 - 0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b,
91 - 0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d,
92 - 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413,
93 - 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1,
94 - 0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6,
95 - 0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972,
96 - 0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85,
97 - 0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed,
98 - 0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511,
99 - 0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe,
100 - 0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b,
101 - 0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05,
102 - 0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1,
103 - 0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142,
104 - 0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf,
105 - 0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3,
106 - 0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e,
107 - 0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a,
108 - 0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6,
109 - 0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3,
110 - 0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b,
111 - 0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428,
112 - 0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad,
113 - 0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14,
114 - 0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8,
115 - 0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4,
116 - 0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2,
117 - 0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda,
118 - 0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949,
119 - 0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf,
120 - 0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810,
121 - 0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c,
122 - 0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697,
123 - 0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e,
124 - 0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f,
125 - 0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc,
126 - 0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c,
127 - 0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969,
128 - 0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27,
129 - 0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122,
130 - 0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433,
131 - 0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9,
132 - 0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5,
133 - 0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a,
134 - 0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0,
135 - 0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e,
136 - 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c
140 + 0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6,
141 + 0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591,
142 + 0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56,
143 + 0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec,
144 + 0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa,
145 + 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb,
146 + 0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45,
147 + 0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b,
148 + 0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c,
149 + 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83,
150 + 0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9,
151 + 0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a,
152 + 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d,
153 + 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f,
154 + 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df,
155 + 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea,
156 + 0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34,
157 + 0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b,
158 + 0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d,
159 + 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413,
160 + 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1,
161 + 0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6,
162 + 0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972,
163 + 0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85,
164 + 0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed,
165 + 0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511,
166 + 0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe,
167 + 0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b,
168 + 0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05,
169 + 0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1,
170 + 0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142,
171 + 0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf,
172 + 0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3,
173 + 0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e,
174 + 0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a,
175 + 0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6,
176 + 0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3,
177 + 0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b,
178 + 0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428,
179 + 0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad,
180 + 0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14,
181 + 0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8,
182 + 0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4,
183 + 0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2,
184 + 0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda,
185 + 0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949,
186 + 0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf,
187 + 0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810,
188 + 0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c,
189 + 0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697,
190 + 0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e,
191 + 0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f,
192 + 0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc,
193 + 0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c,
194 + 0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969,
195 + 0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27,
196 + 0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122,
197 + 0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433,
198 + 0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9,
199 + 0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5,
200 + 0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a,
201 + 0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0,
202 + 0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e,
203 + 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c
209 +#define encT enc_tables.T
213 + volatile u32 counter_head;
214 + u32 cacheline_align[64 / 4 - 1];
218 + volatile u32 counter_tail;
219 +} dec_tables ATTR_ALIGNED_64 =
224 0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a,
225 0xcb6bab3b, 0xf1459d1f, 0xab58faac, 0x9303e34b,
226 @@ -194,7 +212,8 @@ static const struct
227 0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61,
228 0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,
229 0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d
235 #define decT dec_tables.T
236 diff --git a/cipher/rijndael.c b/cipher/rijndael.c
237 index 8637195..d0edab2 100644
238 --- a/cipher/rijndael.c
239 +++ b/cipher/rijndael.c
240 @@ -227,11 +227,11 @@ static const char *selftest(void);
243 /* Prefetching for encryption/decryption tables. */
244 -static void prefetch_table(const volatile byte *tab, size_t len)
245 +static inline void prefetch_table(const volatile byte *tab, size_t len)
249 - for (i = 0; i < len; i += 8 * 32)
250 + for (i = 0; len - i >= 8 * 32; i += 8 * 32)
252 (void)tab[i + 0 * 32];
253 (void)tab[i + 1 * 32];
254 @@ -242,17 +242,37 @@ static void prefetch_table(const volatile byte *tab, size_t len)
255 (void)tab[i + 6 * 32];
256 (void)tab[i + 7 * 32];
258 + for (; i < len; i += 32)
266 static void prefetch_enc(void)
268 - prefetch_table((const void *)encT, sizeof(encT));
269 + /* Modify counters to trigger copy-on-write and unsharing if physical pages
270 + * of look-up table are shared between processes. Modifying counters also
271 + * causes checksums for pages to change and hint same-page merging algorithm
272 + * that these pages are frequently changing. */
273 + enc_tables.counter_head++;
274 + enc_tables.counter_tail++;
276 + /* Prefetch look-up tables to cache. */
277 + prefetch_table((const void *)&enc_tables, sizeof(enc_tables));
280 static void prefetch_dec(void)
282 + /* Modify counters to trigger copy-on-write and unsharing if physical pages
283 + * of look-up table are shared between processes. Modifying counters also
284 + * causes checksums for pages to change and hint same-page merging algorithm
285 + * that these pages are frequently changing. */
286 + dec_tables.counter_head++;
287 + dec_tables.counter_tail++;
289 + /* Prefetch look-up tables to cache. */
290 prefetch_table((const void *)&dec_tables, sizeof(dec_tables));
293 @@ -737,7 +757,7 @@ do_encrypt (const RIJNDAEL_context *ctx,
295 # ifdef HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS
296 return _gcry_aes_amd64_encrypt_block(ctx->keyschenc, bx, ax, ctx->rounds,
300 /* Call SystemV ABI function without storing non-volatile XMM registers,
301 * as target function does not use vector instruction sets. */
302 @@ -757,7 +777,8 @@ do_encrypt (const RIJNDAEL_context *ctx,
304 # endif /* HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS */
305 #elif defined(USE_ARM_ASM)
306 - return _gcry_aes_arm_encrypt_block(ctx->keyschenc, bx, ax, ctx->rounds, encT);
307 + return _gcry_aes_arm_encrypt_block(ctx->keyschenc, bx, ax, ctx->rounds,
310 return do_encrypt_fn (ctx, bx, ax);
311 #endif /* !USE_ARM_ASM && !USE_AMD64_ASM*/
312 @@ -1120,7 +1141,7 @@ do_decrypt (const RIJNDAEL_context *ctx, unsigned char *bx,
314 # ifdef HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS
315 return _gcry_aes_amd64_decrypt_block(ctx->keyschdec, bx, ax, ctx->rounds,
319 /* Call SystemV ABI function without storing non-volatile XMM registers,
320 * as target function does not use vector instruction sets. */
321 @@ -1141,7 +1162,7 @@ do_decrypt (const RIJNDAEL_context *ctx, unsigned char *bx,
322 # endif /* HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS */
323 #elif defined(USE_ARM_ASM)
324 return _gcry_aes_arm_decrypt_block(ctx->keyschdec, bx, ax, ctx->rounds,
328 return do_decrypt_fn (ctx, bx, ax);
329 #endif /*!USE_ARM_ASM && !USE_AMD64_ASM*/