forked from rapier1/hpn-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher-ctr-mt.c
602 lines (525 loc) · 14.1 KB
/
cipher-ctr-mt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* OpenSSH Multi-threaded AES-CTR Cipher
*
* Author: Benjamin Bennett <[email protected]>
* Author: Mike Tasota <[email protected]>
* Author: Chris Rapier <[email protected]>
* Copyright (c) 2008-2013 Pittsburgh Supercomputing Center. All rights reserved.
*
* Based on original OpenSSH AES-CTR cipher. Small portions remain unchanged,
* Copyright (c) 2003 Markus Friedl <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "includes.h"
#if defined(WITH_OPENSSL)
#include <sys/types.h>
#include <stdarg.h>
#include <string.h>
#include <openssl/evp.h>
#include "xmalloc.h"
#include "log.h"
/* compatibility with old or broken OpenSSL versions */
#include "openbsd-compat/openssl-compat.h"
#ifndef USE_BUILTIN_RIJNDAEL
#include <openssl/aes.h>
#endif
#include <pthread.h>
/*-------------------- TUNABLES --------------------*/
/* Number of pregen threads to use */
#define CIPHER_THREADS 2
/* Number of keystream queues */
#define NUMKQ (CIPHER_THREADS + 2)
/* Length of a keystream queue */
#define KQLEN 4096
/* Processor cacheline length */
#define CACHELINE_LEN 64
/* Collect thread stats and print at cancellation when in debug mode */
/* #define CIPHER_THREAD_STATS */
/* Can the system do unaligned loads natively? */
#if defined(__aarch64__) || \
defined(__i386__) || \
defined(__powerpc__) || \
defined(__x86_64__)
# define CIPHER_UNALIGNED_OK
#endif
#if defined(__SIZEOF_INT128__)
# define CIPHER_INT128_OK
#endif
/*-------------------- END TUNABLES --------------------*/
const EVP_CIPHER *evp_aes_ctr_mt(void);
#ifdef CIPHER_THREAD_STATS
/*
* Struct to collect thread stats
*/
struct thread_stats {
u_int fills;
u_int skips;
u_int waits;
u_int drains;
};
/*
* Debug print the thread stats
* Use with pthread_cleanup_push for displaying at thread cancellation
*/
static void
thread_loop_stats(void *x)
{
struct thread_stats *s = x;
debug("tid %lu - %u fills, %u skips, %u waits", pthread_self(),
s->fills, s->skips, s->waits);
}
# define STATS_STRUCT(s) struct thread_stats s
# define STATS_INIT(s) { memset(&s, 0, sizeof(s)); }
# define STATS_FILL(s) { s.fills++; }
# define STATS_SKIP(s) { s.skips++; }
# define STATS_WAIT(s) { s.waits++; }
# define STATS_DRAIN(s) { s.drains++; }
#else
# define STATS_STRUCT(s)
# define STATS_INIT(s)
# define STATS_FILL(s)
# define STATS_SKIP(s)
# define STATS_WAIT(s)
# define STATS_DRAIN(s)
#endif
/* Keystream Queue state */
enum {
KQINIT,
KQEMPTY,
KQFILLING,
KQFULL,
KQDRAINING
};
/* Keystream Queue struct */
struct kq {
u_char keys[KQLEN][AES_BLOCK_SIZE];
u_char ctr[AES_BLOCK_SIZE];
u_char pad0[CACHELINE_LEN];
int qstate;
pthread_mutex_t lock;
pthread_cond_t cond;
u_char pad1[CACHELINE_LEN];
};
/* Context struct */
struct ssh_aes_ctr_ctx
{
struct kq q[NUMKQ];
AES_KEY aes_ctx;
STATS_STRUCT(stats);
u_char aes_counter[AES_BLOCK_SIZE];
pthread_t tid[CIPHER_THREADS];
pthread_rwlock_t tid_lock;
#ifdef __APPLE__
pthread_rwlock_t stop_lock;
int exit_flag;
#endif /* __APPLE__ */
int state;
int qidx;
int ridx;
};
/* <friedl>
* increment counter 'ctr',
* the counter is of size 'len' bytes and stored in network-byte-order.
* (LSB at ctr[len-1], MSB at ctr[0])
*/
static void
ssh_ctr_inc(u_char *ctr, size_t len)
{
int i;
for (i = len - 1; i >= 0; i--)
if (++ctr[i]) /* continue on overflow */
return;
}
/*
* Add num to counter 'ctr'
*/
static void
ssh_ctr_add(u_char *ctr, uint32_t num, u_int len)
{
int i;
uint16_t n;
for (n = 0, i = len - 1; i >= 0 && (num || n); i--) {
n = ctr[i] + (num & 0xff) + n;
num >>= 8;
ctr[i] = n & 0xff;
n >>= 8;
}
}
/*
* Threads may be cancelled in a pthread_cond_wait, we must free the mutex
*/
static void
thread_loop_cleanup(void *x)
{
pthread_mutex_unlock((pthread_mutex_t *)x);
}
#ifdef __APPLE__
/* Check if we should exit, we are doing both cancel and exit condition
* since on OSX threads seem to occasionally fail to notice when they have
* been cancelled. We want to have a backup to make sure that we won't hang
* when the main process join()-s the cancelled thread.
*/
static void
thread_loop_check_exit(struct ssh_aes_ctr_ctx *c)
{
int exit_flag;
pthread_rwlock_rdlock(&c->stop_lock);
exit_flag = c->exit_flag;
pthread_rwlock_unlock(&c->stop_lock);
if (exit_flag)
pthread_exit(NULL);
}
#else
# define thread_loop_check_exit(s)
#endif /* __APPLE__ */
/*
* Helper function to terminate the helper threads
*/
static void
stop_and_join_pregen_threads(struct ssh_aes_ctr_ctx *c)
{
int i;
#ifdef __APPLE__
/* notify threads that they should exit */
pthread_rwlock_wrlock(&c->stop_lock);
c->exit_flag = TRUE;
pthread_rwlock_unlock(&c->stop_lock);
#endif /* __APPLE__ */
/* Cancel pregen threads */
for (i = 0; i < CIPHER_THREADS; i++) {
pthread_cancel(c->tid[i]);
}
for (i = 0; i < NUMKQ; i++) {
pthread_mutex_lock(&c->q[i].lock);
pthread_cond_broadcast(&c->q[i].cond);
pthread_mutex_unlock(&c->q[i].lock);
}
for (i = 0; i < CIPHER_THREADS; i++) {
pthread_join(c->tid[i], NULL);
}
}
/*
* The life of a pregen thread:
* Find empty keystream queues and fill them using their counter.
* When done, update counter for the next fill.
*/
static void *
thread_loop(void *x)
{
AES_KEY key;
STATS_STRUCT(stats);
struct ssh_aes_ctr_ctx *c = x;
struct kq *q;
int i;
int qidx;
pthread_t first_tid;
/* Threads stats on cancellation */
STATS_INIT(stats);
#ifdef CIPHER_THREAD_STATS
pthread_cleanup_push(thread_loop_stats, &stats);
#endif
/* Thread local copy of AES key */
memcpy(&key, &c->aes_ctx, sizeof(key));
pthread_rwlock_rdlock(&c->tid_lock);
first_tid = c->tid[0];
pthread_rwlock_unlock(&c->tid_lock);
/*
* Handle the special case of startup, one thread must fill
* the first KQ then mark it as draining. Lock held throughout.
*/
if (pthread_equal(pthread_self(), first_tid)) {
q = &c->q[0];
pthread_mutex_lock(&q->lock);
if (q->qstate == KQINIT) {
for (i = 0; i < KQLEN; i++) {
AES_encrypt(q->ctr, q->keys[i], &key);
ssh_ctr_inc(q->ctr, AES_BLOCK_SIZE);
}
ssh_ctr_add(q->ctr, KQLEN * (NUMKQ - 1), AES_BLOCK_SIZE);
q->qstate = KQDRAINING;
STATS_FILL(stats);
pthread_cond_broadcast(&q->cond);
}
pthread_mutex_unlock(&q->lock);
} else
STATS_SKIP(stats);
/*
* Normal case is to find empty queues and fill them, skipping over
* queues already filled by other threads and stopping to wait for
* a draining queue to become empty.
*
* Multiple threads may be waiting on a draining queue and awoken
* when empty. The first thread to wake will mark it as filling,
* others will move on to fill, skip, or wait on the next queue.
*/
for (qidx = 1;; qidx = (qidx + 1) % NUMKQ) {
/* Check if I was cancelled, also checked in cond_wait */
pthread_testcancel();
/* Check if we should exit as well */
thread_loop_check_exit(c);
/* Lock queue and block if its draining */
q = &c->q[qidx];
pthread_mutex_lock(&q->lock);
pthread_cleanup_push(thread_loop_cleanup, &q->lock);
while (q->qstate == KQDRAINING || q->qstate == KQINIT) {
STATS_WAIT(stats);
thread_loop_check_exit(c);
pthread_cond_wait(&q->cond, &q->lock);
}
pthread_cleanup_pop(0);
/* If filling or full, somebody else got it, skip */
if (q->qstate != KQEMPTY) {
pthread_mutex_unlock(&q->lock);
STATS_SKIP(stats);
continue;
}
/*
* Empty, let's fill it.
* Queue lock is relinquished while we do this so others
* can see that it's being filled.
*/
q->qstate = KQFILLING;
pthread_cond_broadcast(&q->cond);
pthread_mutex_unlock(&q->lock);
for (i = 0; i < KQLEN; i++) {
AES_encrypt(q->ctr, q->keys[i], &key);
ssh_ctr_inc(q->ctr, AES_BLOCK_SIZE);
}
/* Re-lock, mark full and signal consumer */
pthread_mutex_lock(&q->lock);
ssh_ctr_add(q->ctr, KQLEN * (NUMKQ - 1), AES_BLOCK_SIZE);
q->qstate = KQFULL;
STATS_FILL(stats);
pthread_cond_broadcast(&q->cond);
pthread_mutex_unlock(&q->lock);
}
#ifdef CIPHER_THREAD_STATS
/* Stats */
pthread_cleanup_pop(1);
#endif
return NULL;
}
static int
ssh_aes_ctr(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
LIBCRYPTO_EVP_INL_TYPE len)
{
typedef union {
#ifdef CIPHER_INT128_OK
__uint128_t *u128;
#endif
uint64_t *u64;
uint32_t *u32;
uint8_t *u8;
const uint8_t *cu8;
uintptr_t u;
} ptrs_t;
ptrs_t destp, srcp, bufp;
uintptr_t align;
struct ssh_aes_ctr_ctx *c;
struct kq *q, *oldq;
int ridx;
u_char *buf;
if (len == 0)
return 1;
if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
return 0;
q = &c->q[c->qidx];
ridx = c->ridx;
/* src already padded to block multiple */
srcp.cu8 = src;
destp.u8 = dest;
while (len > 0) {
buf = q->keys[ridx];
bufp.u8 = buf;
/* figure out the alignment on the fly */
#ifdef CIPHER_UNALIGNED_OK
align = 0;
#else
align = destp.u | srcp.u | bufp.u;
#endif
#ifdef CIPHER_INT128_OK
if ((align & 0xf) == 0) {
destp.u128[0] = srcp.u128[0] ^ bufp.u128[0];
} else
#endif
if ((align & 0x7) == 0) {
destp.u64[0] = srcp.u64[0] ^ bufp.u64[0];
destp.u64[1] = srcp.u64[1] ^ bufp.u64[1];
} else if ((align & 0x3) == 0) {
destp.u32[0] = srcp.u32[0] ^ bufp.u32[0];
destp.u32[1] = srcp.u32[1] ^ bufp.u32[1];
destp.u32[2] = srcp.u32[2] ^ bufp.u32[2];
destp.u32[3] = srcp.u32[3] ^ bufp.u32[3];
} else {
size_t i;
for (i = 0; i < AES_BLOCK_SIZE; ++i)
dest[i] = src[i] ^ buf[i];
}
destp.u += AES_BLOCK_SIZE;
srcp.u += AES_BLOCK_SIZE;
len -= AES_BLOCK_SIZE;
ssh_ctr_inc(ctx->iv, AES_BLOCK_SIZE);
/* Increment read index, switch queues on rollover */
if ((ridx = (ridx + 1) % KQLEN) == 0) {
oldq = q;
/* Mark next queue draining, may need to wait */
c->qidx = (c->qidx + 1) % NUMKQ;
q = &c->q[c->qidx];
pthread_mutex_lock(&q->lock);
while (q->qstate != KQFULL) {
STATS_WAIT(c->stats);
pthread_cond_wait(&q->cond, &q->lock);
}
q->qstate = KQDRAINING;
pthread_cond_broadcast(&q->cond);
pthread_mutex_unlock(&q->lock);
/* Mark consumed queue empty and signal producers */
pthread_mutex_lock(&oldq->lock);
oldq->qstate = KQEMPTY;
STATS_DRAIN(c->stats);
pthread_cond_broadcast(&oldq->cond);
pthread_mutex_unlock(&oldq->lock);
}
}
c->ridx = ridx;
return 1;
}
#define HAVE_NONE 0
#define HAVE_KEY 1
#define HAVE_IV 2
static int
ssh_aes_ctr_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
int enc)
{
struct ssh_aes_ctr_ctx *c;
int i;
if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
c = xmalloc(sizeof(*c));
pthread_rwlock_init(&c->tid_lock, NULL);
#ifdef __APPLE__
pthread_rwlock_init(&c->stop_lock, NULL);
c->exit_flag = FALSE;
#endif /* __APPLE__ */
c->state = HAVE_NONE;
for (i = 0; i < NUMKQ; i++) {
pthread_mutex_init(&c->q[i].lock, NULL);
pthread_cond_init(&c->q[i].cond, NULL);
}
STATS_INIT(c->stats);
EVP_CIPHER_CTX_set_app_data(ctx, c);
}
if (c->state == (HAVE_KEY | HAVE_IV)) {
/* tell the pregen threads to exit */
stop_and_join_pregen_threads(c);
#ifdef __APPLE__
/* reset the exit flag */
c->exit_flag = FALSE;
#endif /* __APPLE__ */
/* Start over getting key & iv */
c->state = HAVE_NONE;
}
if (key != NULL) {
AES_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
&c->aes_ctx);
c->state |= HAVE_KEY;
}
if (iv != NULL) {
memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
c->state |= HAVE_IV;
}
if (c->state == (HAVE_KEY | HAVE_IV)) {
/* Clear queues */
memcpy(c->q[0].ctr, ctx->iv, AES_BLOCK_SIZE);
c->q[0].qstate = KQINIT;
for (i = 1; i < NUMKQ; i++) {
memcpy(c->q[i].ctr, ctx->iv, AES_BLOCK_SIZE);
ssh_ctr_add(c->q[i].ctr, i * KQLEN, AES_BLOCK_SIZE);
c->q[i].qstate = KQEMPTY;
}
c->qidx = 0;
c->ridx = 0;
/* Start threads */
for (i = 0; i < CIPHER_THREADS; i++) {
debug("spawned a thread");
pthread_rwlock_wrlock(&c->tid_lock);
pthread_create(&c->tid[i], NULL, thread_loop, c);
pthread_rwlock_unlock(&c->tid_lock);
}
pthread_mutex_lock(&c->q[0].lock);
while (c->q[0].qstate == KQINIT)
pthread_cond_wait(&c->q[0].cond, &c->q[0].lock);
pthread_mutex_unlock(&c->q[0].lock);
}
return 1;
}
/* this function is no longer used but might prove handy in the future
* this comment also applies to ssh_aes_ctr_thread_reconstruction
*/
void
ssh_aes_ctr_thread_destroy(EVP_CIPHER_CTX *ctx)
{
struct ssh_aes_ctr_ctx *c;
c = EVP_CIPHER_CTX_get_app_data(ctx);
stop_and_join_pregen_threads(c);
}
void
ssh_aes_ctr_thread_reconstruction(EVP_CIPHER_CTX *ctx)
{
struct ssh_aes_ctr_ctx *c;
int i;
c = EVP_CIPHER_CTX_get_app_data(ctx);
/* reconstruct threads */
for (i = 0; i < CIPHER_THREADS; i++) {
debug("spawned a thread");
pthread_rwlock_wrlock(&c->tid_lock);
pthread_create(&c->tid[i], NULL, thread_loop, c);
pthread_rwlock_unlock(&c->tid_lock);
}
}
static int
ssh_aes_ctr_cleanup(EVP_CIPHER_CTX *ctx)
{
struct ssh_aes_ctr_ctx *c;
if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
#ifdef CIPHER_THREAD_STATS
debug("main thread: %u drains, %u waits", c->stats.drains,
c->stats.waits);
#endif
stop_and_join_pregen_threads(c);
memset(c, 0, sizeof(*c));
free(c);
EVP_CIPHER_CTX_set_app_data(ctx, NULL);
}
return 1;
}
/* <friedl> */
const EVP_CIPHER *
evp_aes_ctr_mt(void)
{
static EVP_CIPHER aes_ctr;
memset(&aes_ctr, 0, sizeof(EVP_CIPHER));
aes_ctr.nid = NID_undef;
aes_ctr.block_size = AES_BLOCK_SIZE;
aes_ctr.iv_len = AES_BLOCK_SIZE;
aes_ctr.key_len = 16;
aes_ctr.init = ssh_aes_ctr_init;
aes_ctr.cleanup = ssh_aes_ctr_cleanup;
aes_ctr.do_cipher = ssh_aes_ctr;
#ifndef SSH_OLD_EVP
aes_ctr.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
#endif
return &aes_ctr;
}
#endif /* defined(WITH_OPENSSL) */