Skip to content

Commit 9f16452

Browse files
committed
Make various improvements, mostly around supporting OpenSSH.
- Add an --enable-openssh configure option. This will cause the RSA, DH, and ECC code to all use the global RNG. This is necessary because OpenSSH's sshd launches unprivileged processes to handle incoming ssh connections, and these processes aren't allowed to open /dev/urandom to create new RNGs. Add a new macro, WE_DH_USE_GLOBAL_RNG, to handle this in the DH code. - Make some minor improvements to openssh-tests.sh. - Clean up we_aes_ctr.c. - Clean up we_aes_gcm.c. - Fix some incorrect log lines (erroneous WE_LOG_CIPHER usage). - Modify test_rsa.c to use RSA_MIN_SIZE and RSA_MAX_SIZE to determine what constitutes an invalid key size. This makes the RSA key generations tests work with wolfSSL installations configured with larger FP_MAX_BITS than the default.
1 parent 81be2ea commit 9f16452

File tree

12 files changed

+185
-146
lines changed

12 files changed

+185
-146
lines changed

configure.ac

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ then
112112
AM_CFLAGS="$AM_CFLAGS -DWE_ALIGNMENT_SAFETY"
113113
fi
114114

115+
# Adds the necessary flags to support using wolfEngine with OpenSSH.
116+
AC_ARG_ENABLE([openssh],
117+
[AS_HELP_STRING([--enable-openssh],[Support using wolfEngine with OpenSSH. (default: disabled).])],
118+
[ ENABLED_OPENSSH=$enableval ],
119+
[ ENABLED_OPENSSH=no ]
120+
)
121+
122+
if test "$ENABLED_OPENSSH" = "yes"
123+
then
124+
AM_CFLAGS="$AM_CFLAGS -DWE_RSA_USE_GLOBAL_RNG -DWE_ECC_USE_GLOBAL_RNG -DWE_DH_USE_GLOBAL_RNG"
125+
fi
126+
115127
# Single threaded
116128
AC_ARG_ENABLE([singlethreaded],
117129
[AS_HELP_STRING([--enable-singlethreaded],[Enable wolfEngine single threaded (default: disabled).])],
@@ -704,6 +716,7 @@ echo " Features "
704716
echo " * User settings: $ENABLED_USERSETTINGS"
705717
echo " * Dynamic engine: $ENABLED_DYNAMIC_ENGINE"
706718
echo " * Alignment safety: $ENABLED_ALIGNMENT_SAFETY"
719+
echo " * OpenSSH support: $ENABLED_OPENSSH"
707720
echo " * Digest:"
708721
echo " * - SHA-1: $ENABLED_SHA1"
709722
echo " * - SHA-224: $ENABLED_SHA224"

scripts/build-openssl-wolfengine.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,17 @@ build_wolfengine() {
326326
if [ -n "${OPENSSL_INSTALL}" ]; then
327327
./configure $OPENSSL_CPPFLAGS $OPENSSL_LDFLAGS \
328328
--with-openssl=$OPENSSL_INSTALL \
329-
--enable-debug >> $LOGFILE 2>&1
329+
--enable-debug \
330+
$WOLFENGINE_EXTRA_OPTS >> $LOGFILE 2>&1
330331
else
331332
# Tests have been patched to use debug logging - must enable debug.
332333
# User can set WOLFENGINE_EXTRA_LDFLAGS to provide extra LDFLAGS and
333334
# WOLFENGINE_EXTRA_CPPFLAGS to provide extra CPPFLAGS.
334335
./configure LDFLAGS="-L$OPENSSL_SOURCE $WOLFENGINE_EXTRA_LDFLAGS" \
335336
CPPFLAGS="$WOLFENGINE_EXTRA_CPPFLAGS" \
336337
--with-openssl=$OPENSSL_SOURCE \
337-
$WOLFENGINE_EXTRA_OPTS \
338-
--enable-debug >> $LOGFILE 2>&1
338+
--enable-debug \
339+
$WOLFENGINE_EXTRA_OPTS >> $LOGFILE 2>&1
339340
fi
340341
if [ "$?" != 0 ]; then
341342
printf "failed\n"

scripts/openssh-tests.sh

Lines changed: 93 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
88
WOLFENGINE_ROOT="${SCRIPT_DIR}/.."
99

10-
OPENSSL_INSTALL_DIR=${SCRIPT_DIR}/openssl
11-
OPENSSH_DIR=${SCRIPT_DIR}/openssh-portable
10+
if [ -z "${OPENSSL_INSTALL_DIR}" ]; then
11+
OPENSSL_INSTALL_DIR=${SCRIPT_DIR}/openssl
12+
fi
13+
OPENSSH_DIR=${SCRIPT_DIR}/openssh
1214

1315
source ${SCRIPT_DIR}/build-openssl-wolfengine.sh
1416

1517
do_cleanup() {
1618
printf "Cleaning up.\n"
1719

18-
# Use the environment variable KEEP_OPENSSH to prevent OpenSSH directories
19-
# from being deleted at the end of the run.
20+
# Use the environment variable KEEP_OPENSSH to prevent OpenSSH and OpenSSL
21+
# directories from being deleted at the end of the run.
2022
if [ -z "${KEEP_OPENSSH}" ]; then
2123
printf "\tDeleting OpenSSH directory.\n"
2224
rm -rf ${OPENSSH_DIR}
@@ -27,8 +29,8 @@ do_cleanup() {
2729
}
2830

2931
do_failure() {
30-
# Keep the logs around to help debug the failure.
31-
KEEP_LOGS=1
32+
# Keep the OpenSSH and OpenSSL directories around to help debug the failure.
33+
KEEP_OPENSSH=1
3234
do_cleanup
3335
exit 1
3436
}
@@ -37,7 +39,7 @@ do_failure() {
3739
trap do_failure INT TERM
3840

3941
download_openssh() {
40-
printf "Setting up OpenSSH.\n"
42+
printf "Downloading OpenSSH..."
4143
if [ -n "${OPENSSH_NO_DOWNLOAD}" -o -n "${OPENSSH_NO_BUILD}" ]; then
4244
return
4345
fi
@@ -46,8 +48,7 @@ download_openssh() {
4648

4749
cd ${SCRIPT_DIR}
4850

49-
printf "\tDownloading..."
50-
git clone https://github.com/openssh/openssh-portable.git >> $LOGFILE 2>&1
51+
git clone https://github.com/openssh/openssh-portable.git $OPENSSH_DIR >> $LOGFILE 2>&1
5152
if [ $? != 0 ]; then
5253
printf "failed\n"
5354
do_failure
@@ -65,7 +66,7 @@ build_openssh() {
6566
cd ${OPENSSH_DIR}
6667

6768
printf "Building OpenSSH.\n"
68-
printf "\tAutoreconf..."
69+
printf "\tRunning autoreconf..."
6970
autoreconf >> $LOGFILE 2>&1
7071
if [ $? != 0 ]; then
7172
printf "failed.\n"
@@ -98,81 +99,81 @@ test_openssh_separate() {
9899

99100
printf "Running OpenSSH tests with wolfEngine\n"
100101
for T in connect \
101-
proxy-connect \
102-
connect-privsep \
103-
connect-uri \
104-
proto-version \
105-
proto-mismatch \
106-
exit-status \
107-
envpass \
108-
transfer \
109-
banner \
110-
rekey \
111-
dhgex \
112-
stderr-data \
113-
stderr-after-eof \
114-
broken-pipe \
115-
try-ciphers \
116-
yes-head \
117-
login-timeout \
118-
agent \
119-
agent-getpeereid \
120-
agent-timeout \
121-
agent-ptrace \
122-
agent-subprocess \
123-
keyscan \
124-
keygen-change \
125-
keygen-convert \
126-
keygen-moduli \
127-
key-options \
128-
scp \
129-
scp-uri \
130-
sftp \
131-
sftp-chroot \
132-
sftp-cmds \
133-
sftp-badcmds \
134-
sftp-batch \
135-
sftp-glob \
136-
sftp-perm \
137-
sftp-uri \
138-
reconfigure \
139-
dynamic-forward \
140-
forwarding \
141-
multiplex \
142-
reexec \
143-
brokenkeys \
144-
sshcfgparse \
145-
cfgparse \
146-
cfgmatch \
147-
cfgmatchlisten \
148-
percent \
149-
addrmatch \
150-
localcommand \
151-
forcecommand \
152-
portnum \
153-
keytype \
154-
kextype \
155-
cert-hostkey \
156-
cert-userkey \
157-
host-expand \
158-
keys-command \
159-
forward-control \
160-
integrity \
161-
krl \
162-
multipubkey \
163-
limit-keytype \
164-
hostkey-agent \
165-
keygen-knownhosts \
166-
hostkey-rotate \
167-
principals-command \
168-
cert-file \
169-
cfginclude \
170-
servcfginclude \
171-
allow-deny-users \
172-
authinfo \
173-
sshsig \
174-
keygen-comment \
175-
knownhosts-command
102+
proxy-connect \
103+
agent \
104+
connect-privsep \
105+
connect-uri \
106+
proto-version \
107+
proto-mismatch \
108+
exit-status \
109+
envpass \
110+
transfer \
111+
banner \
112+
rekey \
113+
dhgex \
114+
stderr-data \
115+
stderr-after-eof \
116+
broken-pipe \
117+
try-ciphers \
118+
yes-head \
119+
login-timeout \
120+
agent-getpeereid \
121+
agent-timeout \
122+
agent-ptrace \
123+
agent-subprocess \
124+
keyscan \
125+
keygen-change \
126+
keygen-convert \
127+
keygen-moduli \
128+
key-options \
129+
scp \
130+
scp-uri \
131+
sftp \
132+
sftp-chroot \
133+
sftp-cmds \
134+
sftp-badcmds \
135+
sftp-batch \
136+
sftp-glob \
137+
sftp-perm \
138+
sftp-uri \
139+
reconfigure \
140+
dynamic-forward \
141+
forwarding \
142+
multiplex \
143+
reexec \
144+
brokenkeys \
145+
sshcfgparse \
146+
cfgparse \
147+
cfgmatch \
148+
cfgmatchlisten \
149+
percent \
150+
addrmatch \
151+
localcommand \
152+
forcecommand \
153+
portnum \
154+
keytype \
155+
kextype \
156+
cert-hostkey \
157+
cert-userkey \
158+
host-expand \
159+
keys-command \
160+
forward-control \
161+
integrity \
162+
krl \
163+
multipubkey \
164+
limit-keytype \
165+
hostkey-agent \
166+
keygen-knownhosts \
167+
hostkey-rotate \
168+
principals-command \
169+
cert-file \
170+
cfginclude \
171+
servcfginclude \
172+
allow-deny-users \
173+
authinfo \
174+
sshsig \
175+
keygen-comment \
176+
knownhosts-command
176177
do
177178
printf "\t$T..."
178179
make t-exec LTESTS=$T >> $LOGFILE 2>&1
@@ -256,14 +257,17 @@ do
256257
OPENSSL_INSTALL=${OPENSSL_INSTALL_DIR}
257258
setup_openssl_install
258259

259-
WE_OPENSSL_CONF=${SCRIPT_DIR}/wolfengine.conf
260-
WE_DEBUG=0
261-
260+
WOLFENGINE_EXTRA_OPTS="--enable-openssh"
262261
build_wolfengine
262+
263+
# We don't want to print debug messages as that will trigger false failures
264+
# in the OpenSSH tests.
265+
WE_DEBUG=0
266+
WE_OPENSSL_CONF=${SCRIPT_DIR}/wolfengine.conf
263267
write_conf_file
264268

265269
build_openssh
266-
test_openssh
270+
test_openssh_separate
267271
done
268272

269273

src/we_aes_block.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ typedef struct we_AesBlock
3434
unsigned char lastBlock[AES_BLOCK_SIZE];
3535
/** Number of buffered bytes. */
3636
unsigned int over;
37-
/** Flag to indicate whether wolfSSL AES object initialized. */
38-
unsigned int init:1;
3937
/** Flag to indicate whether we are doing encrypt (1) or decrpyt (0). */
4038
unsigned int enc:1;
4139
} we_AesBlock;
@@ -86,25 +84,23 @@ static int we_aes_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
8684
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc);
8785
ret = 0;
8886
}
89-
aes->init = (ret == 1);
87+
if (ret == 1) {
88+
WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES key (%d bytes)",
89+
EVP_CIPHER_CTX_key_length(ctx));
90+
rc = wc_AesSetKey(&aes->aes, key, EVP_CIPHER_CTX_key_length(ctx),
91+
iv, enc ? AES_ENCRYPTION : AES_DECRYPTION);
92+
if (rc != 0) {
93+
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetKey", rc);
94+
ret = 0;
95+
}
96+
}
9097
}
9198

9299
if (ret == 1) {
93100
/* Store whether encrypting. */
94101
aes->enc = enc;
95102
}
96103

97-
if ((ret == 1) && (key != NULL)) {
98-
WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES key (%d bytes)",
99-
EVP_CIPHER_CTX_key_length(ctx));
100-
rc = wc_AesSetKey(&aes->aes, key, EVP_CIPHER_CTX_key_length(ctx), iv,
101-
enc ? AES_ENCRYPTION : AES_DECRYPTION);
102-
if (rc != 0) {
103-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetKey", rc);
104-
ret = 0;
105-
}
106-
}
107-
108104
WOLFENGINE_LEAVE(WE_LOG_CIPHER, "we_aes_cbc_init", ret);
109105

110106
return ret;
@@ -440,9 +436,6 @@ static int we_aes_ecb_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
440436
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc);
441437
ret = 0;
442438
}
443-
else {
444-
aes->init = 1;
445-
}
446439
}
447440

448441
if (ret == 1) {

src/we_aes_ctr.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ static int we_aes_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
5252
int ret = 1;
5353
int rc;
5454
we_AesCtr *aes;
55-
const unsigned char *tmpIv;
5655

5756
WOLFENGINE_ENTER(WE_LOG_CIPHER, "we_aes_ctr_init");
5857
WOLFENGINE_MSG_VERBOSE(WE_LOG_CIPHER, "ARGS [ctx = %p, key = %p, iv = %p, "
@@ -100,7 +99,7 @@ static int we_aes_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
10099
* @param in [in] Data to encrypt/decrypt.
101100
* @param len [in] Length of data to encrypt/decrypt.
102101
* @return -1 on failure.
103-
* @return Number of bytes put in out on success.
102+
* @return 1 on success.
104103
*/
105104
static int we_aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
106105
const unsigned char *in, size_t len)
@@ -125,7 +124,7 @@ static int we_aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
125124
rc = wc_AesSetIV(&aes->aes, EVP_CIPHER_CTX_iv_noconst(ctx));
126125
if (rc != 0) {
127126
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetIV", rc);
128-
ret = 0;
127+
ret = -1;
129128
}
130129
}
131130
if (ret == 1) {
@@ -207,7 +206,6 @@ EVP_CIPHER* we_aes192_ctr_ciph = NULL;
207206
/** AES256-CTR EVP cipher method. */
208207
EVP_CIPHER* we_aes256_ctr_ciph = NULL;
209208

210-
211209
/**
212210
* Initialize an AES-CTR method.
213211
*
@@ -315,4 +313,3 @@ int we_init_aesctr_meths()
315313
}
316314

317315
#endif /* WE_HAVE_AESCTR */
318-

0 commit comments

Comments
 (0)