Skip to content

Commit 81be2ea

Browse files
SparkiDevhaydenroche5
authored andcommitted
Test: OpenSSH testing
AES-CBC/DES3-CBC: - IV is passed down to engine by OpenSSL layer. The IV is cached in ctx->iv (streaming). Copy IV after encryption/decryption back into cache and always set. (OpenSSH sets IV without changing key.) AES-CTR: - Don't use EVP_CIPH_FLAG_CUSTOM_CIPHER. - IV is cached and therefore always needs to be set and put back in cache (streaming). (OpenSSH sets IV without changing key.) AES-GCM: - Split IV set and IV inc into two different flags. Move increment of IV to after encrypt/decrypt. And no longer cache current IV (One shot enc/dec operation so final IV is not needed.) - Initialize and free the wolfCrypt AES object. - Return the authentication failure error when EVP_DecryptFinal is called rather than update function. OpenSSH expects authentication failure error when EVP_DecryptFinal fails and not when update fails. RSA: - Added flag to indicate the message digest has been set from above. - Only check length of RSA sign input matches the digest length when digest set from above. Otherwise the input will have been BER encoded.
1 parent 0a54fd4 commit 81be2ea

File tree

8 files changed

+384
-116
lines changed

8 files changed

+384
-116
lines changed

include/wolfengine/we_openssl_bc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ void EVP_MD_meth_free(EVP_MD *md);
8080
const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx);
8181
void *EVP_CIPHER_CTX_get_cipher_data(const EVP_CIPHER_CTX *ctx);
8282
unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx);
83+
int EVP_CIPHER_CTX_num(const EVP_CIPHER_CTX *ctx);
84+
void EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num);
8385

8486
int EVP_CIPHER_meth_set_iv_length(EVP_CIPHER *cipher, int iv_len);
8587
int EVP_CIPHER_meth_set_flags(EVP_CIPHER *cipher, unsigned long flags);

scripts/openssh-tests.sh

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
#!/bin/bash
2+
3+
#
4+
# Tests that using OpenSSH with wolfEngine works.
5+
#
6+
7+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
8+
WOLFENGINE_ROOT="${SCRIPT_DIR}/.."
9+
10+
OPENSSL_INSTALL_DIR=${SCRIPT_DIR}/openssl
11+
OPENSSH_DIR=${SCRIPT_DIR}/openssh-portable
12+
13+
source ${SCRIPT_DIR}/build-openssl-wolfengine.sh
14+
15+
do_cleanup() {
16+
printf "Cleaning up.\n"
17+
18+
# Use the environment variable KEEP_OPENSSH to prevent OpenSSH directories
19+
# from being deleted at the end of the run.
20+
if [ -z "${KEEP_OPENSSH}" ]; then
21+
printf "\tDeleting OpenSSH directory.\n"
22+
rm -rf ${OPENSSH_DIR}
23+
24+
printf "\tDeleting OpenSSL install directory.\n"
25+
rm -rf ${OPENSSL_INSTALL_DIR}
26+
fi
27+
}
28+
29+
do_failure() {
30+
# Keep the logs around to help debug the failure.
31+
KEEP_LOGS=1
32+
do_cleanup
33+
exit 1
34+
}
35+
36+
# Register trap on interrupt (2) and terminate (15)
37+
trap do_failure INT TERM
38+
39+
download_openssh() {
40+
printf "Setting up OpenSSH.\n"
41+
if [ -n "${OPENSSH_NO_DOWNLOAD}" -o -n "${OPENSSH_NO_BUILD}" ]; then
42+
return
43+
fi
44+
45+
rm -rf ${OPENSSH_DIR}
46+
47+
cd ${SCRIPT_DIR}
48+
49+
printf "\tDownloading..."
50+
git clone https://github.com/openssh/openssh-portable.git >> $LOGFILE 2>&1
51+
if [ $? != 0 ]; then
52+
printf "failed\n"
53+
do_failure
54+
fi
55+
printf "ok.\n"
56+
57+
cd ${WOLFENGINE_ROOT}
58+
}
59+
60+
build_openssh() {
61+
if [ -n "${OPENSSH_NO_BUILD}" ]; then
62+
return
63+
fi
64+
65+
cd ${OPENSSH_DIR}
66+
67+
printf "Building OpenSSH.\n"
68+
printf "\tAutoreconf..."
69+
autoreconf >> $LOGFILE 2>&1
70+
if [ $? != 0 ]; then
71+
printf "failed.\n"
72+
do_failure
73+
fi
74+
printf "ok.\n"
75+
76+
printf "\tConfiguring..."
77+
./configure --with-ssl-dir=${OPENSSL_INSTALL} --without-openssl-header-check --with-ssl-engine >> $LOGFILE 2>&1
78+
if [ $? != 0 ]; then
79+
printf "failed.\n"
80+
do_failure
81+
fi
82+
printf "ok.\n"
83+
make clean >> $LOGFILE 2>&1
84+
85+
printf "\tBuilding..."
86+
make -j$MAKE_JOBS >> $LOGFILE 2>&1
87+
if [ $? != 0 ]; then
88+
printf "failed.\n"
89+
do_failure
90+
fi
91+
printf "ok.\n"
92+
93+
cd ${WOLFENGINE_ROOT}
94+
}
95+
96+
test_openssh_separate() {
97+
cd ${OPENSSH_DIR}
98+
99+
printf "Running OpenSSH tests with wolfEngine\n"
100+
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
176+
do
177+
printf "\t$T..."
178+
make t-exec LTESTS=$T >> $LOGFILE 2>&1
179+
if [ $? != 0 ]; then
180+
printf "failed\n"
181+
do_failure
182+
fi
183+
printf "ok.\n"
184+
done
185+
186+
cd ${WOLFENGINE_ROOT}
187+
}
188+
189+
test_openssh_one() {
190+
cd ${OPENSSH_DIR}
191+
192+
printf "Running OpenSSH tests with wolfEngine\n"
193+
for T in integrity
194+
do
195+
printf "\t$T..."
196+
make t-exec LTESTS=$T >> $LOGFILE 2>&1
197+
if [ $? != 0 ]; then
198+
printf "failed\n"
199+
do_failure
200+
fi
201+
printf "ok.\n"
202+
done
203+
204+
cd ${WOLFENGINE_ROOT}
205+
}
206+
207+
test_openssh() {
208+
cd ${OPENSSH_DIR}
209+
210+
printf "Running OpenSSH tests with wolfEngine..."
211+
make tests >> $LOGFILE 2>&1
212+
if [ $? != 0 ]; then
213+
printf "failed\n"
214+
do_failure
215+
fi
216+
printf "ok.\n"
217+
218+
cd ${WOLFENGINE_ROOT}
219+
}
220+
221+
if [ -z "${LOGFILE}" ]; then
222+
LOGFILE=${SCRIPT_DIR}/openssh-tests.log
223+
fi
224+
rm -f $LOGFILE
225+
226+
export OPENSSL_EXTRA_CFLAGS="-g3 -O0 -fno-omit-frame-pointer -fno-inline-functions"
227+
228+
# Versions of OpenSSL to test
229+
if [ -n "${OPENSSL_VERSIONS}" ]; then
230+
VERSIONS=${OPENSSL_VERSIONS}
231+
else
232+
VERSIONS="1.0.2 1.1.1"
233+
fi
234+
235+
export OPENSSL_CONF=$WOLFENGINE_ROOT/engine.conf
236+
export OPENSSL_ENGINES=$WOLFENGINE_ROOT/.libs
237+
export LD_LIBRARY_PATH="$WOLFENGINE_ROOT/.libs:$WOLFENGINE_ROOT:$LD_LIBRARY_PATH"
238+
239+
download_openssh
240+
241+
for VERSION in $VERSIONS
242+
do
243+
if [ "${VERSION}" = "1.0.2" ]; then
244+
OPENSSL_VERS_STR="OpenSSL 1.0.2h"
245+
get_openssl_102h
246+
configure_openssl_102h
247+
build_openssl_102h
248+
install_openssl_102h
249+
elif [ "${VERSION}" = "1.1.1" ]; then
250+
OPENSSL_VERS_STR="OpenSSL 1.1.1b"
251+
get_openssl_111b
252+
configure_openssl_111b
253+
build_openssl_111b
254+
install_openssl_111b
255+
fi
256+
OPENSSL_INSTALL=${OPENSSL_INSTALL_DIR}
257+
setup_openssl_install
258+
259+
WE_OPENSSL_CONF=${SCRIPT_DIR}/wolfengine.conf
260+
WE_DEBUG=0
261+
262+
build_wolfengine
263+
write_conf_file
264+
265+
build_openssh
266+
test_openssh
267+
done
268+
269+

src/we_aes_block.c

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ typedef struct we_AesBlock
3838
unsigned int init:1;
3939
/** Flag to indicate whether we are doing encrypt (1) or decrpyt (0). */
4040
unsigned int enc:1;
41-
/** Flag to indicate whether iv has been set. */
42-
unsigned int ivSet:1;
4341
} we_AesBlock;
4442

4543
#endif
@@ -88,32 +86,22 @@ static int we_aes_cbc_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
8886
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc);
8987
ret = 0;
9088
}
91-
aes->init = 1;
89+
aes->init = (ret == 1);
9290
}
9391

94-
if (ret == 1 && (aes->init == 1)) {
95-
aes->over = 0;
92+
if (ret == 1) {
9693
/* Store whether encrypting. */
9794
aes->enc = enc;
95+
}
9896

99-
if (key != NULL) {
100-
WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES key (%d bytes)",
101-
EVP_CIPHER_CTX_key_length(ctx));
102-
rc = wc_AesSetKey(&aes->aes, key, EVP_CIPHER_CTX_key_length(ctx),
103-
iv, enc ? AES_ENCRYPTION : AES_DECRYPTION);
104-
if (rc != 0) {
105-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetKey", rc);
106-
ret = 0;
107-
}
108-
}
109-
if (ret == 1 && iv != NULL) {
110-
WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES IV");
111-
rc = wc_AesSetIV(&aes->aes, iv);
112-
if (rc != 0) {
113-
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetIV", rc);
114-
ret = 0;
115-
}
116-
aes->ivSet = (ret == 1);
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;
117105
}
118106
}
119107

@@ -216,14 +204,13 @@ static int we_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
216204
"EVP_CIPHER_CTX_get_cipher_data", aes);
217205
ret = -1;
218206
}
219-
if ((ret == 1) && (!aes->ivSet)) {
207+
if (ret == 1) {
220208
WOLFENGINE_MSG(WE_LOG_CIPHER, "Setting AES IV");
221209
rc = wc_AesSetIV(&aes->aes, EVP_CIPHER_CTX_iv_noconst(ctx));
222210
if (rc != 0) {
223211
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetIV", rc);
224212
ret = 0;
225213
}
226-
aes->ivSet = (ret == 1);
227214
}
228215
if (ret == 1) {
229216
if (aes->enc) {
@@ -232,6 +219,8 @@ static int we_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
232219
else {
233220
ret = we_aes_cbc_decrypt(aes, out, in, len);
234221
}
222+
223+
XMEMCPY(EVP_CIPHER_CTX_iv_noconst(ctx), aes->aes.reg, AES_BLOCK_SIZE);
235224
}
236225

237226
WOLFENGINE_LEAVE(WE_LOG_CIPHER, "we_aes_cbc_cipher", ret);

0 commit comments

Comments
 (0)