Debian Codegraph @

tests/unittests/torture_threads_crypto.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2018 by Anderson Toshiyuki Sasaki <[email protected]>
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23
24 #define LIBSSH_STATIC
25
26 #include "torture.h"
27 #include "libssh/crypto.h"
28
29 #include <pthread.h>
30
31 #define NUM_THREADS 100
32
33 static int8_t key[32] =
34 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"
35 "\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d"
36 "\x1e\x1f";
37
38 static uint8_t IV[16] =
39 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e"
40 "\x1f";
41
42 static uint8_t cleartext[144] =
43 "\xb4\xfc\x5d\xc2\x49\x8d\x2c\x29\x4a\xc9\x9a\xb0\x1b\xf8\x29"
44 "\xee\x85\x6d\x8c\x04\x34\x7c\x65\xf4\x89\x97\xc5\x71\x70\x41"
45 "\x91\x40\x19\x60\xe1\xf1\x8f\x4d\x8c\x17\x51\xd6\xbc\x69\x6e"
46 "\xf2\x21\x87\x18\x6c\xef\xc4\xf4\xd9\xe6\x1b\x94\xf7\xd8\xb2"
47 "\xe9\x24\xb9\xe7\xe6\x19\xf5\xec\x55\x80\x9a\xc8\x7d\x70\xa3"
48 "\x50\xf8\x03\x10\x35\x49\x9b\x53\x58\xd7\x4c\xfc\x5f\x02\xd6"
49 "\x28\xea\xcc\x43\xee\x5e\x2b\x8a\x7a\x66\xf7\x00\xee\x09\x18"
50 "\x30\x1b\x47\xa2\x16\x69\xc4\x6e\x44\x3f\xbd\xec\x52\xce\xe5"
51 "\x41\xf2\xe0\x04\x4f\x5a\x55\x58\x37\xba\x45\x8d\x15\x53\xf6"
52 "\x31\x91\x13\x8c\x51\xed\x08\x07\xdb";
53
54 static uint8_t aes256_cbc_encrypted[144] =
55 "\x7f\x1b\x92\xac\xc5\x16\x05\x55\x74\xac\xb4\xe0\x91\x8c\xf8"
56 "\x0d\xa9\x72\xa5\x09\xb8\x44\xee\x55\x02\x13\xb7\x52\x0a\xf0"
57 "\xac\xd0\x21\x0e\x58\x7b\x34\xfe\xdb\x36\x01\x60\x7d\x18\x3a"
58 "\xa9\x15\x18\x5b\x13\xca\xdd\x77\x7d\xdf\x64\xc6\xd5\x75\x4b"
59 "\x02\x02\x37\xb1\xf4\x33\xff\x93\xe6\x32\x08\xda\xcb\x5d\xa2"
60 "\x8f\x17\x1f\x99\x92\x60\x22\x9d\x6b\xe6\xb2\x5e\xb0\x5d\x26"
61 "\x3f\xde\xb8\xc1\xb0\x70\x80\x1c\x00\xd0\x93\x2b\xeb\x0f\xd7"
62 "\x70\x7a\x9a\x7a\xa6\x21\x23\x2c\x02\xb7\xcd\x88\x10\x9c\x2d"
63 "\x0c\xd3\xfa\xc1\x33\x5b\xe1\xa1\xd4\x3d\x8f\xb8\x50\xc5\xb5"
64 "\x72\xdd\x6d\x32\x1f\x58\x00\x48\xbe";
65
66 static int run_on_threads(void *(*func)(void *))
67 {
68 pthread_t threads[NUM_THREADS];
69 int rc;
70 int i;
71
72 for (i = 0; i < NUM_THREADS; ++i) {
73 rc = pthread_create(&threads[i], NULL, func, NULL);
74 assert_int_equal(rc, 0);
75 }
76
77 for (i = 0; i < NUM_THREADS; ++i) {
78 void *p = NULL;
79 uint64_t *result;
80
81 rc = pthread_join(threads[i], &p);
82 assert_int_equal(rc, 0);
83
84 result = (uint64_t *)p;
85 assert_null(result);
86 }
87
88 return rc;
89 }
90
91 static int get_cipher(struct ssh_cipher_struct *cipher, const char *ciphername)
92 {
93 struct ssh_cipher_struct *ciphers = ssh_get_ciphertab();
94 int i, cmp;
95
96 for (i = 0; ciphers[i].name != NULL; i++) {
97 cmp = strcmp(ciphername, ciphers[i].name);
98 if (cmp == 0) {
99 memcpy(cipher, &ciphers[i], sizeof(*cipher));
100 return SSH_OK;
101 }
102 }
103
104 return SSH_ERROR;
105 }
106
107 static void *thread_crypto_aes256_cbc(void *threadid)
108 {
109 uint8_t output[sizeof(cleartext)] = {0};
110 uint8_t iv[16] = {0};
111 struct ssh_cipher_struct cipher = {
112 .name = NULL,
113 };
114 int rc;
115
116 /* Unused */
117 (void) threadid;
118
119 rc = get_cipher(&cipher, "aes256-cbc");
120 assert_int_equal(rc, SSH_OK);
121 assert_non_null(cipher.set_encrypt_key);
122 assert_non_null(cipher.encrypt);
123
124 /* This is for dump static analizyer without modelling support */
125 if (cipher.set_encrypt_key == NULL ||
126 cipher.encrypt == NULL) {
127 return NULL;
128 }
129
130 memcpy(iv, IV, sizeof(IV));
131 cipher.set_encrypt_key(&cipher,
132 key,
133 iv
134 );
135
136 cipher.encrypt(&cipher,
137 cleartext,
138 output,
139 sizeof(cleartext)
140 );
141
142 assert_memory_equal(output,
143 aes256_cbc_encrypted,
144 sizeof(aes256_cbc_encrypted));
145 ssh_cipher_clear(&cipher);
146
147 rc = get_cipher(&cipher, "aes256-cbc");
148 assert_int_equal(rc, SSH_OK);
149 assert_non_null(cipher.set_encrypt_key);
150 assert_non_null(cipher.encrypt);
151
152 /* This is for dump static analizyer without modelling support */
153 if (cipher.set_encrypt_key == NULL ||
154 cipher.encrypt == NULL) {
155 return NULL;
156 }
157
158 memcpy(iv, IV, sizeof(IV));
159 cipher.set_decrypt_key(&cipher,
160 key,
161 iv
162 );
163
164 memset(output, '\0', sizeof(output));
165 cipher.decrypt(&cipher,
166 aes256_cbc_encrypted,
167 output,
168 sizeof(aes256_cbc_encrypted)
169 );
170
171 assert_memory_equal(output, cleartext, sizeof(cleartext));
172
173 ssh_cipher_clear(&cipher);
174
175 pthread_exit(NULL);
176 }
177
178 static void torture_crypto_aes256_cbc(void **state)
179 {
180 int rc;
181
182 /* Unused */
183 (void) state;
184
185 rc = run_on_threads(thread_crypto_aes256_cbc);
186 assert_int_equal(rc, 0);
187 }
188
189 int torture_run_tests(void)
190 {
191 int rc;
192 const struct CMUnitTest tests[] = {
193 cmocka_unit_test(torture_crypto_aes256_cbc),
194 };
195
196 /*
197 * If the library is statically linked, ssh_init() is not called
198 * automatically
199 */
200 ssh_init();
201 rc = cmocka_run_group_tests(tests, NULL, NULL);
202 ssh_finalize();
203
204 return rc;
205 }