Skip to content

Commit 6c540cc

Browse files
authored
Merge pull request #1453 from denravonska/update-crypto
Update crypto module
2 parents d171fcd + 4bae084 commit 6c540cc

24 files changed

+875
-163
lines changed

configure.ac

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,20 @@ if test "x$CXXFLAGS_overridden" = "xno"; then
234234
AX_CHECK_COMPILE_FLAG([-Wimplicit-fallthrough],[CXXFLAGS="$CXXFLAGS -Wno-implicit-fallthrough"],,[[$CXXFLAG_WERROR]])
235235
fi
236236

237-
# Check for optional instruction set support. Enabling these does _not_ imply that all code will
238-
# be compiled with them, rather that specific objects/libs may use them after checking for runtime
239-
# compatibility.
240-
AX_CHECK_COMPILE_FLAG([-msse4.2],[[SSE42_CXXFLAGS="-msse4.2"]],,[[$CXXFLAG_WERROR]])
237+
enable_hwcrc32=no
238+
enable_sse41=no
239+
enable_avx2=no
240+
enable_shani=no
241+
242+
if test "x$use_asm" = "xyes"; then
243+
244+
# Check for optional instruction set support. Enabling these does _not_ imply that all code will
245+
# be compiled with them, rather that specific objects/libs may use them after checking for runtime
246+
# compatibility.
247+
AX_CHECK_COMPILE_FLAG([-msse4.2],[[SSE42_CXXFLAGS="-msse4.2"]],,[[$CXXFLAG_WERROR]])
241248
AX_CHECK_COMPILE_FLAG([-msse4.1],[[SSE41_CXXFLAGS="-msse4.1"]],,[[$CXXFLAG_WERROR]])
242249
AX_CHECK_COMPILE_FLAG([-mavx -mavx2],[[AVX2_CXXFLAGS="-mavx -mavx2"]],,[[$CXXFLAG_WERROR]])
250+
AX_CHECK_COMPILE_FLAG([-msse4 -msha],[[SHANI_CXXFLAGS="-msse4 -msha"]],,[[$CXXFLAG_WERROR]])
243251

244252
TEMP_CXXFLAGS="$CXXFLAGS"
245253
CXXFLAGS="$CXXFLAGS $SSE42_CXXFLAGS"
@@ -301,6 +309,25 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
301309
)
302310
CXXFLAGS="$TEMP_CXXFLAGS"
303311

312+
TEMP_CXXFLAGS="$CXXFLAGS"
313+
CXXFLAGS="$CXXFLAGS $SHANI_CXXFLAGS"
314+
AC_MSG_CHECKING(for SHA-NI intrinsics)
315+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
316+
#include <stdint.h>
317+
#include <immintrin.h>
318+
]],[[
319+
__m128i i = _mm_set1_epi32(0);
320+
__m128i j = _mm_set1_epi32(1);
321+
__m128i k = _mm_set1_epi32(2);
322+
return _mm_extract_epi32(_mm_sha256rnds2_epu32(i, i, k), 0);
323+
]])],
324+
[ AC_MSG_RESULT(yes); enable_shani=yes; AC_DEFINE(ENABLE_SHANI, 1, [Define this symbol to build code that uses SHA-NI intrinsics]) ],
325+
[ AC_MSG_RESULT(no)]
326+
)
327+
CXXFLAGS="$TEMP_CXXFLAGS"
328+
329+
fi
330+
304331
CPPFLAGS="$CPPFLAGS -DBOOST_SPIRIT_THREADSAFE -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS"
305332

306333
AC_ARG_WITH([utils],
@@ -1115,6 +1142,7 @@ AM_CONDITIONAL([HARDEN],[test x$use_hardening = xyes])
11151142
AM_CONDITIONAL([ENABLE_HWCRC32],[test x$enable_hwcrc32 = xyes])
11161143
AM_CONDITIONAL([ENABLE_SSE41],[test x$enable_sse41 = xyes])
11171144
AM_CONDITIONAL([ENABLE_AVX2],[test x$enable_avx2 = xyes])
1145+
AM_CONDITIONAL([ENABLE_SHANI],[test x$enable_shani = xyes])
11181146
AM_CONDITIONAL([USE_ASM],[test x$use_asm = xyes])
11191147

11201148
AC_DEFINE(CLIENT_VERSION_MAJOR, _CLIENT_VERSION_MAJOR, [Major version])
@@ -1149,6 +1177,7 @@ AC_SUBST(PIE_FLAGS)
11491177
AC_SUBST(SSE42_CXXFLAGS)
11501178
AC_SUBST(SSE41_CXXFLAGS)
11511179
AC_SUBST(AVX2_CXXFLAGS)
1180+
AC_SUBST(SHANI_CXXFLAGS)
11521181
AC_SUBST(LIBTOOL_APP_LDFLAGS)
11531182
AC_SUBST(USE_UPNP)
11541183
AC_SUBST(USE_QRCODE)

src/Makefile.am

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ if ENABLE_AVX2
3434
LIBGRIDCOIN_CRYPTO_AVX2 = crypto/libgridcoin_crypto_avx2.a
3535
LIBGRIDCOIN_CRYPTO += $(LIBGRIDCOIN_CRYPTO_AVX2)
3636
endif
37+
if ENABLE_SHANI
38+
LIBGRIDCOIN_CRYPTO_SHANI = crypto/libgridcoin_crypto_shani.a
39+
LIBGRIDCOIN_CRYPTO += $(LIBGRIDCOIN_CRYPTO_SHANI)
40+
endif
3741

3842
# Make is not made aware of per-object dependencies to avoid limiting building parallelization
3943
# But to build the less dependent modules first, we manually select their order here:
@@ -236,6 +240,12 @@ crypto_libgridcoin_crypto_avx2_a_CXXFLAGS += $(AVX2_CXXFLAGS)
236240
crypto_libgridcoin_crypto_avx2_a_CPPFLAGS += -DENABLE_AVX2
237241
crypto_libgridcoin_crypto_avx2_a_SOURCES = crypto/sha256_avx2.cpp
238242

243+
crypto_libgridcoin_crypto_shani_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
244+
crypto_libgridcoin_crypto_shani_a_CPPFLAGS = $(AM_CPPFLAGS)
245+
crypto_libgridcoin_crypto_shani_a_CXXFLAGS += $(SHANI_CXXFLAGS)
246+
crypto_libgridcoin_crypto_shani_a_CPPFLAGS += -DENABLE_SHANI
247+
crypto_libgridcoin_crypto_shani_a_SOURCES = crypto/sha256_shani.cpp
248+
239249
CTAES_DIST = crypto/ctaes/bench.c
240250
CTAES_DIST += crypto/ctaes/ctaes.c
241251
CTAES_DIST += crypto/ctaes/ctaes.h

src/crypto/aes.cpp

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2016-2017 The Bitcoin Core developers
1+
// Copyright (c) 2016-2018 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -12,36 +12,6 @@ extern "C" {
1212
#include <crypto/ctaes/ctaes.c>
1313
}
1414

15-
AES128Encrypt::AES128Encrypt(const unsigned char key[16])
16-
{
17-
AES128_init(&ctx, key);
18-
}
19-
20-
AES128Encrypt::~AES128Encrypt()
21-
{
22-
memset(&ctx, 0, sizeof(ctx));
23-
}
24-
25-
void AES128Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
26-
{
27-
AES128_encrypt(&ctx, 1, ciphertext, plaintext);
28-
}
29-
30-
AES128Decrypt::AES128Decrypt(const unsigned char key[16])
31-
{
32-
AES128_init(&ctx, key);
33-
}
34-
35-
AES128Decrypt::~AES128Decrypt()
36-
{
37-
memset(&ctx, 0, sizeof(ctx));
38-
}
39-
40-
void AES128Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
41-
{
42-
AES128_decrypt(&ctx, 1, plaintext, ciphertext);
43-
}
44-
4515
AES256Encrypt::AES256Encrypt(const unsigned char key[32])
4616
{
4717
AES256_init(&ctx, key);
@@ -182,35 +152,3 @@ AES256CBCDecrypt::~AES256CBCDecrypt()
182152
{
183153
memset(iv, 0, sizeof(iv));
184154
}
185-
186-
AES128CBCEncrypt::AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
187-
: enc(key), pad(padIn)
188-
{
189-
memcpy(iv, ivIn, AES_BLOCKSIZE);
190-
}
191-
192-
AES128CBCEncrypt::~AES128CBCEncrypt()
193-
{
194-
memset(iv, 0, AES_BLOCKSIZE);
195-
}
196-
197-
int AES128CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
198-
{
199-
return CBCEncrypt(enc, iv, data, size, pad, out);
200-
}
201-
202-
AES128CBCDecrypt::AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
203-
: dec(key), pad(padIn)
204-
{
205-
memcpy(iv, ivIn, AES_BLOCKSIZE);
206-
}
207-
208-
AES128CBCDecrypt::~AES128CBCDecrypt()
209-
{
210-
memset(iv, 0, AES_BLOCKSIZE);
211-
}
212-
213-
int AES128CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const
214-
{
215-
return CBCDecrypt(dec, iv, data, size, pad, out);
216-
}

src/crypto/aes.h

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2015-2017 The Bitcoin Core developers
1+
// Copyright (c) 2015-2018 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
//
@@ -12,33 +12,8 @@ extern "C" {
1212
}
1313

1414
static const int AES_BLOCKSIZE = 16;
15-
static const int AES128_KEYSIZE = 16;
1615
static const int AES256_KEYSIZE = 32;
1716

18-
/** An encryption class for AES-128. */
19-
class AES128Encrypt
20-
{
21-
private:
22-
AES128_ctx ctx;
23-
24-
public:
25-
explicit AES128Encrypt(const unsigned char key[16]);
26-
~AES128Encrypt();
27-
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const;
28-
};
29-
30-
/** A decryption class for AES-128. */
31-
class AES128Decrypt
32-
{
33-
private:
34-
AES128_ctx ctx;
35-
36-
public:
37-
explicit AES128Decrypt(const unsigned char key[16]);
38-
~AES128Decrypt();
39-
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const;
40-
};
41-
4217
/** An encryption class for AES-256. */
4318
class AES256Encrypt
4419
{
@@ -89,30 +64,4 @@ class AES256CBCDecrypt
8964
unsigned char iv[AES_BLOCKSIZE];
9065
};
9166

92-
class AES128CBCEncrypt
93-
{
94-
public:
95-
AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
96-
~AES128CBCEncrypt();
97-
int Encrypt(const unsigned char* data, int size, unsigned char* out) const;
98-
99-
private:
100-
const AES128Encrypt enc;
101-
const bool pad;
102-
unsigned char iv[AES_BLOCKSIZE];
103-
};
104-
105-
class AES128CBCDecrypt
106-
{
107-
public:
108-
AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
109-
~AES128CBCDecrypt();
110-
int Decrypt(const unsigned char* data, int size, unsigned char* out) const;
111-
112-
private:
113-
const AES128Decrypt dec;
114-
const bool pad;
115-
unsigned char iv[AES_BLOCKSIZE];
116-
};
117-
11867
#endif // BITCOIN_CRYPTO_AES_H

src/crypto/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2014-2017 The Bitcoin Core developers
1+
// Copyright (c) 2014-2018 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

src/crypto/hmac_sha256.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2014-2017 The Bitcoin Core developers
1+
// Copyright (c) 2014-2018 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

src/crypto/hmac_sha256.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// Copyright (c) 2014-2017 The Bitcoin Core developers
1+
// Copyright (c) 2014-2018 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#ifndef BITCOIN_CRYPTO_AES_H
6-
#define BITCOIN_CRYPTO_AES_H
5+
#ifndef BITCOIN_CRYPTO_HMAC_SHA256_H
6+
#define BITCOIN_CRYPTO_HMAC_SHA256_H
77

88
#include <crypto/sha256.h>
99

@@ -29,4 +29,4 @@ class CHMAC_SHA256
2929
void Finalize(unsigned char hash[OUTPUT_SIZE]);
3030
};
3131

32-
#endif // BITCOIN_CRYPTO_AES_H
32+
#endif // BITCOIN_CRYPTO_HMAC_SHA256_H

src/crypto/hmac_sha512.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2014-2017 The Bitcoin Core developers
1+
// Copyright (c) 2014-2018 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

src/crypto/hmac_sha512.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2014-2017 The Bitcoin Core developers
1+
// Copyright (c) 2014-2018 The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

0 commit comments

Comments
 (0)