Skip to content

Commit 57d4ec5

Browse files
authored
Merge pull request #384 from h2o/kazuho/fastls
non-temporal aes-gcm engine
2 parents c3d2156 + 65d3e79 commit 57d4ec5

13 files changed

Lines changed: 1600 additions & 197 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
matrix:
1717
include:
1818
- name: "Linux / OpenSSL 1.1.0"
19-
command: make -f misc/docker-ci.mk CMAKE_ARGS='-DOPENSSL_ROOT_DIR=-DOPENSSL_ROOT_DIR=/opt/openssl-1.1.0' CONTAINER_NAME='h2oserver/h2o-ci:ubuntu1604'
19+
command: make -f misc/docker-ci.mk CMAKE_ARGS='-DOPENSSL_ROOT_DIR=-DOPENSSL_ROOT_DIR=/opt/openssl-1.1.0 -DWITH_FUSION=OFF' CONTAINER_NAME='h2oserver/h2o-ci:ubuntu1604'
2020
- name: "Linux / OpenSSL 1.1.1"
2121
command: make -f misc/docker-ci.mk
2222
- name: "Linux / OpenSSL 1.1.1 + ASan & UBSan"

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ ENDIF ()
148148

149149
IF (WITH_FUSION)
150150
ADD_LIBRARY(picotls-fusion lib/fusion.c)
151-
SET_TARGET_PROPERTIES(picotls-fusion PROPERTIES COMPILE_FLAGS "-mavx2 -maes -mpclmul")
151+
SET_TARGET_PROPERTIES(picotls-fusion PROPERTIES COMPILE_FLAGS "-mavx2 -maes -mpclmul -mvaes -mvpclmulqdq")
152152
TARGET_LINK_LIBRARIES(picotls-fusion picotls-core)
153153
ADD_EXECUTABLE(test-fusion.t
154154
deps/picotest/picotest.c
155155
lib/picotls.c
156156
t/fusion.c)
157157
TARGET_LINK_LIBRARIES(test-fusion.t picotls-minicrypto)
158-
SET_TARGET_PROPERTIES(test-fusion.t PROPERTIES COMPILE_FLAGS "-mavx2 -maes -mpclmul")
158+
SET_TARGET_PROPERTIES(test-fusion.t PROPERTIES COMPILE_FLAGS "-mavx2 -maes -mpclmul -mvaes -mvpclmulqdq")
159159
IF (WITH_DTRACE)
160160
ADD_DEPENDENCIES(test-fusion.t generate-picotls-probes)
161161
ENDIF ()

include/picotls.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ extern "C" {
7070
#define PTLS_FUZZ_HANDSHAKE 0
7171
#endif
7272

73+
#ifndef PTLS_SIZEOF_CACHE_LINE
74+
#define PTLS_SIZEOF_CACHE_LINE 64
75+
#endif
76+
7377
#define PTLS_HELLO_RANDOM_SIZE 32
7478

7579
#define PTLS_AES128_KEY_SIZE 16
@@ -392,6 +396,10 @@ typedef const struct st_ptls_aead_algorithm_t {
392396
* size of the tag
393397
*/
394398
size_t tag_size;
399+
/**
400+
* if encrypted bytes are going to be written using non-temporal store instructions (i.e., skip cache)
401+
*/
402+
unsigned non_temporal : 1;
395403
/**
396404
* size of memory allocated for ptls_aead_context_t. AEAD implementations can set this value to something greater than
397405
* sizeof(ptls_aead_context_t) and stuff additional data at the bottom of the struct.

include/picotls/fusion.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,24 @@ extern "C" {
2828

2929
#include <stddef.h>
3030
#include <emmintrin.h>
31+
#include <immintrin.h>
3132
#include "../picotls.h"
3233

3334
#define PTLS_FUSION_AES128_ROUNDS 10
3435
#define PTLS_FUSION_AES256_ROUNDS 14
3536

3637
typedef struct ptls_fusion_aesecb_context {
37-
__m128i keys[PTLS_FUSION_AES256_ROUNDS + 1];
38+
union {
39+
__m128i m128[PTLS_FUSION_AES256_ROUNDS + 1];
40+
__m256i m256[PTLS_FUSION_AES256_ROUNDS + 1];
41+
} keys;
3842
unsigned rounds;
39-
} ptls_fusion_aesecb_context_t;
43+
uint8_t aesni256;
44+
} __attribute__((aligned(32))) ptls_fusion_aesecb_context_t;
4045

4146
typedef struct ptls_fusion_aesgcm_context ptls_fusion_aesgcm_context_t;
4247

43-
void ptls_fusion_aesecb_init(ptls_fusion_aesecb_context_t *ctx, int is_enc, const void *key, size_t key_size);
48+
void ptls_fusion_aesecb_init(ptls_fusion_aesecb_context_t *ctx, int is_enc, const void *key, size_t key_size, int avx256);
4449
void ptls_fusion_aesecb_dispose(ptls_fusion_aesecb_context_t *ctx);
4550
void ptls_fusion_aesecb_encrypt(ptls_fusion_aesecb_context_t *ctx, void *dst, const void *src);
4651

@@ -84,8 +89,15 @@ void ptls_fusion_aesgcm_encrypt(ptls_fusion_aesgcm_context_t *ctx, void *output,
8489
int ptls_fusion_aesgcm_decrypt(ptls_fusion_aesgcm_context_t *ctx, void *output, const void *input, size_t inlen, __m128i ctr,
8590
const void *aad, size_t aadlen, const void *tag);
8691

92+
/**
93+
* A boolean flag indicating if vaes and vpclmulqdq (256-bit crypto instructions) should be used. This flag is set automatically
94+
* when `ptls_fusion_is_supported_by_cpu` is called. Users can update the flag to enforce behavior. Engines that do not have support
95+
* for these 256-bit instructions will continue using the 128-bit ones, even when this flag is set.
96+
*/
97+
extern int ptls_fusion_can_aesni256;
8798
extern ptls_cipher_algorithm_t ptls_fusion_aes128ctr, ptls_fusion_aes256ctr;
8899
extern ptls_aead_algorithm_t ptls_fusion_aes128gcm, ptls_fusion_aes256gcm;
100+
extern ptls_aead_algorithm_t ptls_non_temporal_aes128gcm, ptls_non_temporal_aes256gcm;
89101

90102
/**
91103
* Returns a boolean indicating if fusion can be used.

lib/cifra/aes128.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ptls_aead_algorithm_t ptls_minicrypto_aes128gcm = {"AES128-GCM",
5555
PTLS_AES128_KEY_SIZE,
5656
PTLS_AESGCM_IV_SIZE,
5757
PTLS_AESGCM_TAG_SIZE,
58+
0,
5859
sizeof(struct aesgcm_context_t),
5960
aead_aes128gcm_setup_crypto};
6061
ptls_cipher_suite_t ptls_minicrypto_aes128gcmsha256 = {.id = PTLS_CIPHER_SUITE_AES_128_GCM_SHA256,

lib/cifra/aes256.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ptls_aead_algorithm_t ptls_minicrypto_aes256gcm = {"AES256-GCM",
5555
PTLS_AES256_KEY_SIZE,
5656
PTLS_AESGCM_IV_SIZE,
5757
PTLS_AESGCM_TAG_SIZE,
58+
0,
5859
sizeof(struct aesgcm_context_t),
5960
aead_aes256gcm_setup_crypto};
6061
ptls_cipher_suite_t ptls_minicrypto_aes256gcmsha384 = {.id = PTLS_CIPHER_SUITE_AES_256_GCM_SHA384,

lib/cifra/chacha20.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ ptls_aead_algorithm_t ptls_minicrypto_chacha20poly1305 = {"CHACHA20-POLY1305",
226226
PTLS_CHACHA20_KEY_SIZE,
227227
PTLS_CHACHA20POLY1305_IV_SIZE,
228228
PTLS_CHACHA20POLY1305_TAG_SIZE,
229+
0,
229230
sizeof(struct chacha20poly1305_context_t),
230231
aead_chacha20poly1305_setup_crypto};
231232
ptls_cipher_suite_t ptls_minicrypto_chacha20poly1305sha256 = {.id = PTLS_CIPHER_SUITE_CHACHA20_POLY1305_SHA256,

0 commit comments

Comments
 (0)