| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#define LIBSSH_STATIC |
| 4 |
|
| 5 |
#include "torture.h" |
| 6 |
#include "libssh/crypto.h" |
| 7 |
#include "libssh/chacha20-poly1305-common.h" |
| 8 |
|
| 9 |
uint8_t key[32] = |
| 10 |
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" |
| 11 |
"\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d" |
| 12 |
"\x1e\x1f"; |
| 13 |
|
| 14 |
uint8_t IV[16] = |
| 15 |
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e" |
| 16 |
"\x1f"; |
| 17 |
|
| 18 |
uint8_t cleartext[144] = |
| 19 |
"\xb4\xfc\x5d\xc2\x49\x8d\x2c\x29\x4a\xc9\x9a\xb0\x1b\xf8\x29" |
| 20 |
"\xee\x85\x6d\x8c\x04\x34\x7c\x65\xf4\x89\x97\xc5\x71\x70\x41" |
| 21 |
"\x91\x40\x19\x60\xe1\xf1\x8f\x4d\x8c\x17\x51\xd6\xbc\x69\x6e" |
| 22 |
"\xf2\x21\x87\x18\x6c\xef\xc4\xf4\xd9\xe6\x1b\x94\xf7\xd8\xb2" |
| 23 |
"\xe9\x24\xb9\xe7\xe6\x19\xf5\xec\x55\x80\x9a\xc8\x7d\x70\xa3" |
| 24 |
"\x50\xf8\x03\x10\x35\x49\x9b\x53\x58\xd7\x4c\xfc\x5f\x02\xd6" |
| 25 |
"\x28\xea\xcc\x43\xee\x5e\x2b\x8a\x7a\x66\xf7\x00\xee\x09\x18" |
| 26 |
"\x30\x1b\x47\xa2\x16\x69\xc4\x6e\x44\x3f\xbd\xec\x52\xce\xe5" |
| 27 |
"\x41\xf2\xe0\x04\x4f\x5a\x55\x58\x37\xba\x45\x8d\x15\x53\xf6" |
| 28 |
"\x31\x91\x13\x8c\x51\xed\x08\x07\xdb"; |
| 29 |
|
| 30 |
uint8_t aes256_cbc_encrypted[144] = |
| 31 |
"\x7f\x1b\x92\xac\xc5\x16\x05\x55\x74\xac\xb4\xe0\x91\x8c\xf8" |
| 32 |
"\x0d\xa9\x72\xa5\x09\xb8\x44\xee\x55\x02\x13\xb7\x52\x0a\xf0" |
| 33 |
"\xac\xd0\x21\x0e\x58\x7b\x34\xfe\xdb\x36\x01\x60\x7d\x18\x3a" |
| 34 |
"\xa9\x15\x18\x5b\x13\xca\xdd\x77\x7d\xdf\x64\xc6\xd5\x75\x4b" |
| 35 |
"\x02\x02\x37\xb1\xf4\x33\xff\x93\xe6\x32\x08\xda\xcb\x5d\xa2" |
| 36 |
"\x8f\x17\x1f\x99\x92\x60\x22\x9d\x6b\xe6\xb2\x5e\xb0\x5d\x26" |
| 37 |
"\x3f\xde\xb8\xc1\xb0\x70\x80\x1c\x00\xd0\x93\x2b\xeb\x0f\xd7" |
| 38 |
"\x70\x7a\x9a\x7a\xa6\x21\x23\x2c\x02\xb7\xcd\x88\x10\x9c\x2d" |
| 39 |
"\x0c\xd3\xfa\xc1\x33\x5b\xe1\xa1\xd4\x3d\x8f\xb8\x50\xc5\xb5" |
| 40 |
"\x72\xdd\x6d\x32\x1f\x58\x00\x48\xbe"; |
| 41 |
|
| 42 |
static int get_cipher(struct ssh_cipher_struct *cipher, const char *ciphername) |
| 43 |
{ |
| 44 |
struct ssh_cipher_struct *ciphers = ssh_get_ciphertab(); |
| 45 |
size_t i; |
| 46 |
int cmp; |
| 47 |
|
| 48 |
assert_non_null(cipher); |
| 49 |
|
| 50 |
for (i = 0; ciphers[i].name != NULL; i++) { |
| 51 |
cmp = strcmp(ciphername, ciphers[i].name); |
| 52 |
if (cmp == 0){ |
| 53 |
memcpy(cipher, &ciphers[i], sizeof(*cipher)); |
| 54 |
return SSH_OK; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return SSH_ERROR; |
| 59 |
} |
| 60 |
|
| 61 |
static void torture_crypto_aes256_cbc(void **state) |
| 62 |
{ |
| 63 |
uint8_t output[sizeof(cleartext)] = {0}; |
| 64 |
uint8_t iv[16] = {0}; |
| 65 |
struct ssh_cipher_struct cipher = {0}; |
| 66 |
int rc; |
| 67 |
(void)state; |
| 68 |
|
| 69 |
rc = get_cipher(&cipher, "aes256-cbc"); |
| 70 |
assert_int_equal(rc, SSH_OK); |
| 71 |
|
| 72 |
assert_non_null(cipher.set_encrypt_key); |
| 73 |
assert_non_null(cipher.encrypt); |
| 74 |
|
| 75 |
memcpy(iv, IV, sizeof(IV)); |
| 76 |
cipher.set_encrypt_key(&cipher, |
| 77 |
key, |
| 78 |
iv |
| 79 |
); |
| 80 |
|
| 81 |
cipher.encrypt(&cipher, |
| 82 |
cleartext, |
| 83 |
output, |
| 84 |
sizeof(cleartext) |
| 85 |
); |
| 86 |
|
| 87 |
assert_memory_equal(output, aes256_cbc_encrypted, sizeof(aes256_cbc_encrypted)); |
| 88 |
ssh_cipher_clear(&cipher); |
| 89 |
|
| 90 |
rc = get_cipher(&cipher, "aes256-cbc"); |
| 91 |
assert_int_equal(rc, SSH_OK); |
| 92 |
|
| 93 |
assert_non_null(cipher.set_decrypt_key); |
| 94 |
assert_non_null(cipher.decrypt); |
| 95 |
|
| 96 |
memcpy(iv, IV, sizeof(IV)); |
| 97 |
cipher.set_decrypt_key(&cipher, |
| 98 |
key, |
| 99 |
iv |
| 100 |
); |
| 101 |
|
| 102 |
memset(output, '\0', sizeof(output)); |
| 103 |
cipher.decrypt(&cipher, |
| 104 |
aes256_cbc_encrypted, |
| 105 |
output, |
| 106 |
sizeof(aes256_cbc_encrypted) |
| 107 |
); |
| 108 |
|
| 109 |
assert_memory_equal(output, cleartext, sizeof(cleartext)); |
| 110 |
|
| 111 |
ssh_cipher_clear(&cipher); |
| 112 |
} |
| 113 |
|
| 114 |
uint8_t chacha20poly1305_key[CHACHA20_KEYLEN*2] = |
| 115 |
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e" |
| 116 |
"\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d" |
| 117 |
"\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c" |
| 118 |
"\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b" |
| 119 |
"\x3c\x3d\x3e\x3f"; |
| 120 |
|
| 121 |
#define CLEARTEXT_LENGTH 144 |
| 122 |
uint8_t chacha20poly1305_cleartext[CLEARTEXT_LENGTH] = |
| 123 |
"\xb4\xfc\x5d\xc2\x49\x8d\x2c\x29\x4a\xc9\x9a\xb0\x1b\xf8\x29" |
| 124 |
"\xee\x85\x6d\x8c\x04\x34\x7c\x65\xf4\x89\x97\xc5\x71\x70\x41" |
| 125 |
"\x91\x40\x19\x60\xe1\xf1\x8f\x4d\x8c\x17\x51\xd6\xbc\x69\x6e" |
| 126 |
"\xf2\x21\x87\x18\x6c\xef\xc4\xf4\xd9\xe6\x1b\x94\xf7\xd8\xb2" |
| 127 |
"\xe9\x24\xb9\xe7\xe6\x19\xf5\xec\x55\x80\x9a\xc8\x7d\x70\xa3" |
| 128 |
"\x50\xf8\x03\x10\x35\x49\x9b\x53\x58\xd7\x4c\xfc\x5f\x02\xd6" |
| 129 |
"\x28\xea\xcc\x43\xee\x5e\x2b\x8a\x7a\x66\xf7\x00\xee\x09\x18" |
| 130 |
"\x30\x1b\x47\xa2\x16\x69\xc4\x6e\x44\x3f\xbd\xec\x52\xce\xe5" |
| 131 |
"\x41\xf2\xe0\x04\x4f\x5a\x55\x58\x37\xba\x45\x8d\x15\x53\xf6" |
| 132 |
"\x31\x91\x13\x8c\x51\xed\x08\x07\xdb"; |
| 133 |
|
| 134 |
uint64_t chacha20poly1305_seq = (uint64_t)1234567890 * 98765431; |
| 135 |
|
| 136 |
uint8_t chacha20poly1305_encrypted[sizeof(uint32_t) + CLEARTEXT_LENGTH + POLY1305_TAGLEN] = |
| 137 |
"\xac\x2e\x4c\x54\xf6\x97\x75\xb4\x3b\x8f\xb0\x8e\xb0\x0a\x8e" |
| 138 |
"\xb3\x90\x21\x0d\x7a\xb6\xd3\x03\xf6\xbc\x6e\x3a\x32\x67\xe1" |
| 139 |
"\x13\x65\x43\x3b\x34\x9d\xcb\x62\x7e\x0a\x80\xb0\x45\x87\x07" |
| 140 |
"\x85\x49\x8d\x23\x5f\xac\x9c\x8b\xa8\xd5\x01\x12\xfe\x52\xc6" |
| 141 |
"\x99\xb4\xf2\xde\x12\x78\x79\xea\x1c\x5f\x45\xcd\xf7\xe4\xa0" |
| 142 |
"\x66\x15\x7f\xe3\xf4\x73\x3b\xe0\x52\xac\x2a\x00\x73\xd0\xd7" |
| 143 |
"\x95\xa9\xb9\x3a\xe0\x50\x13\xf4\xdc\xfc\x2a\x64\xb5\xcf\x29" |
| 144 |
"\x88\xef\x4c\x56\x10\x30\x28\xbb\x59\xb8\x23\x58\xab\x01\xa2" |
| 145 |
"\xab\x6b\xdd\xee\x20\x43\xe1\xec\x7a\xe1\xaa\x8b\x60\x19\xde" |
| 146 |
"\x3a\xd1\xd6\x80\x49\x7d\x5c\x81\xb8\x96\xad\x62\x32\xe5\x25" |
| 147 |
"\x72\xe9\x63\x96\xa1\x44\x25\x91\xe1\xdc\x01\xc7\x5c\xa9"; |
| 148 |
|
| 149 |
static void torture_crypto_chacha20poly1305(void **state) |
| 150 |
{ |
| 151 |
uint8_t input[sizeof(uint32_t) + sizeof(chacha20poly1305_cleartext)]; |
| 152 |
uint8_t output[sizeof(input) + POLY1305_TAGLEN] = {0}; |
| 153 |
uint8_t *outtag = output + sizeof(input); |
| 154 |
struct ssh_cipher_struct cipher = {0}; |
| 155 |
uint32_t in_length; |
| 156 |
int rc; |
| 157 |
(void)state; |
| 158 |
|
| 159 |
/* Chacha20-poly1305 is not FIPS-allowed cipher */ |
| 160 |
if (ssh_fips_mode()) { |
| 161 |
skip(); |
| 162 |
} |
| 163 |
|
| 164 |
assert_int_equal(sizeof(output), sizeof(chacha20poly1305_encrypted)); |
| 165 |
|
| 166 |
in_length = htonl(sizeof(chacha20poly1305_cleartext)); |
| 167 |
memcpy(input, &in_length, sizeof(uint32_t)); |
| 168 |
memcpy(input + sizeof(uint32_t), chacha20poly1305_cleartext, |
| 169 |
sizeof(chacha20poly1305_cleartext)); |
| 170 |
|
| 171 |
rc = get_cipher(&cipher, "[email protected]"); |
| 172 |
assert_int_equal(rc, SSH_OK); |
| 173 |
|
| 174 |
assert_int_equal(sizeof(chacha20poly1305_key) * 8, cipher.keysize); |
| 175 |
assert_non_null(cipher.set_encrypt_key); |
| 176 |
assert_non_null(cipher.aead_encrypt); |
| 177 |
|
| 178 |
rc = cipher.set_encrypt_key(&cipher, chacha20poly1305_key, NULL); |
| 179 |
assert_int_equal(rc, SSH_OK); |
| 180 |
|
| 181 |
cipher.aead_encrypt(&cipher, input, output, sizeof(input), outtag, |
| 182 |
chacha20poly1305_seq); |
| 183 |
assert_memory_equal(output, chacha20poly1305_encrypted, |
| 184 |
sizeof(chacha20poly1305_encrypted)); |
| 185 |
ssh_cipher_clear(&cipher); |
| 186 |
|
| 187 |
memset(output, '\0', sizeof(output)); |
| 188 |
|
| 189 |
rc = get_cipher(&cipher, "[email protected]"); |
| 190 |
assert_int_equal(rc, SSH_OK); |
| 191 |
|
| 192 |
assert_non_null(cipher.set_decrypt_key); |
| 193 |
assert_non_null(cipher.aead_decrypt); |
| 194 |
assert_non_null(cipher.aead_decrypt_length); |
| 195 |
|
| 196 |
rc = cipher.set_decrypt_key(&cipher, chacha20poly1305_key, NULL); |
| 197 |
assert_int_equal(rc, SSH_OK); |
| 198 |
|
| 199 |
rc = cipher.aead_decrypt_length(&cipher, chacha20poly1305_encrypted, |
| 200 |
output, sizeof(uint32_t), |
| 201 |
chacha20poly1305_seq); |
| 202 |
assert_int_equal(rc, SSH_OK); |
| 203 |
|
| 204 |
rc = cipher.aead_decrypt(&cipher, chacha20poly1305_encrypted, |
| 205 |
output + sizeof(uint32_t), sizeof(cleartext), |
| 206 |
chacha20poly1305_seq); |
| 207 |
assert_int_equal(rc, SSH_OK); |
| 208 |
|
| 209 |
assert_memory_equal(output, input, sizeof(input)); |
| 210 |
|
| 211 |
ssh_cipher_clear(&cipher); |
| 212 |
} |
| 213 |
|
| 214 |
static void torture_crypto_chacha20poly1305_bad_packet_length(void **state) |
| 215 |
{ |
| 216 |
uint8_t output[sizeof(uint32_t) + sizeof(chacha20poly1305_cleartext)] = {0}; |
| 217 |
uint8_t encrypted_bad[sizeof(chacha20poly1305_encrypted)]; |
| 218 |
struct ssh_cipher_struct cipher = {0}; |
| 219 |
int rc; |
| 220 |
(void)state; |
| 221 |
|
| 222 |
/* Chacha20-poly1305 is not FIPS-allowed cipher */ |
| 223 |
if (ssh_fips_mode()) { |
| 224 |
skip(); |
| 225 |
} |
| 226 |
|
| 227 |
/* Test corrupted packet length */ |
| 228 |
memcpy(encrypted_bad, chacha20poly1305_encrypted, sizeof(encrypted_bad)); |
| 229 |
encrypted_bad[1] ^= 1; |
| 230 |
|
| 231 |
rc = get_cipher(&cipher, "[email protected]"); |
| 232 |
assert_int_equal(rc, SSH_OK); |
| 233 |
|
| 234 |
rc = cipher.set_decrypt_key(&cipher, chacha20poly1305_key, NULL); |
| 235 |
assert_int_equal(rc, SSH_OK); |
| 236 |
|
| 237 |
rc = cipher.aead_decrypt_length(&cipher, encrypted_bad, |
| 238 |
output, sizeof(uint32_t), |
| 239 |
chacha20poly1305_seq); |
| 240 |
assert_int_equal(rc, SSH_OK); |
| 241 |
|
| 242 |
rc = cipher.aead_decrypt(&cipher, encrypted_bad, |
| 243 |
output + sizeof(uint32_t), sizeof(cleartext), |
| 244 |
chacha20poly1305_seq); |
| 245 |
assert_int_equal(rc, SSH_ERROR); |
| 246 |
|
| 247 |
ssh_cipher_clear(&cipher); |
| 248 |
} |
| 249 |
|
| 250 |
static void torture_crypto_chacha20poly1305_bad_data(void **state) |
| 251 |
{ |
| 252 |
uint8_t output[sizeof(uint32_t) + sizeof(chacha20poly1305_cleartext)] = {0}; |
| 253 |
uint8_t encrypted_bad[sizeof(chacha20poly1305_encrypted)]; |
| 254 |
struct ssh_cipher_struct cipher = {0}; |
| 255 |
int rc; |
| 256 |
(void)state; |
| 257 |
|
| 258 |
/* Chacha20-poly1305 is not FIPS-allowed cipher */ |
| 259 |
if (ssh_fips_mode()) { |
| 260 |
skip(); |
| 261 |
} |
| 262 |
|
| 263 |
/* Test corrupted data */ |
| 264 |
memcpy(encrypted_bad, chacha20poly1305_encrypted, sizeof(encrypted_bad)); |
| 265 |
encrypted_bad[100] ^= 1; |
| 266 |
|
| 267 |
rc = get_cipher(&cipher, "[email protected]"); |
| 268 |
assert_int_equal(rc, SSH_OK); |
| 269 |
|
| 270 |
rc = cipher.set_decrypt_key(&cipher, chacha20poly1305_key, NULL); |
| 271 |
assert_int_equal(rc, SSH_OK); |
| 272 |
|
| 273 |
rc = cipher.aead_decrypt_length(&cipher, encrypted_bad, |
| 274 |
output, sizeof(uint32_t), |
| 275 |
chacha20poly1305_seq); |
| 276 |
assert_int_equal(rc, SSH_OK); |
| 277 |
|
| 278 |
rc = cipher.aead_decrypt(&cipher, encrypted_bad, |
| 279 |
output + sizeof(uint32_t), sizeof(cleartext), |
| 280 |
chacha20poly1305_seq); |
| 281 |
assert_int_equal(rc, SSH_ERROR); |
| 282 |
|
| 283 |
ssh_cipher_clear(&cipher); |
| 284 |
} |
| 285 |
|
| 286 |
static void torture_crypto_chacha20poly1305_bad_tag(void **state) |
| 287 |
{ |
| 288 |
uint8_t output[sizeof(uint32_t) + sizeof(chacha20poly1305_cleartext)] = {0}; |
| 289 |
uint8_t encrypted_bad[sizeof(chacha20poly1305_encrypted)]; |
| 290 |
struct ssh_cipher_struct cipher = {0}; |
| 291 |
int rc; |
| 292 |
(void)state; |
| 293 |
|
| 294 |
/* Chacha20-poly1305 is not FIPS-allowed cipher */ |
| 295 |
if (ssh_fips_mode()) { |
| 296 |
skip(); |
| 297 |
} |
| 298 |
|
| 299 |
/* Test corrupted tag */ |
| 300 |
assert_int_equal(sizeof(encrypted_bad), sizeof(chacha20poly1305_encrypted)); |
| 301 |
memcpy(encrypted_bad, chacha20poly1305_encrypted, sizeof(encrypted_bad)); |
| 302 |
encrypted_bad[sizeof(encrypted_bad) - 1] ^= 1; |
| 303 |
|
| 304 |
rc = get_cipher(&cipher, "[email protected]"); |
| 305 |
assert_int_equal(rc, SSH_OK); |
| 306 |
|
| 307 |
rc = cipher.set_decrypt_key(&cipher, chacha20poly1305_key, NULL); |
| 308 |
assert_int_equal(rc, SSH_OK); |
| 309 |
|
| 310 |
rc = cipher.aead_decrypt_length(&cipher, encrypted_bad, |
| 311 |
output, sizeof(uint32_t), |
| 312 |
chacha20poly1305_seq); |
| 313 |
assert_int_equal(rc, SSH_OK); |
| 314 |
|
| 315 |
rc = cipher.aead_decrypt(&cipher, encrypted_bad, |
| 316 |
output + sizeof(uint32_t), sizeof(cleartext), |
| 317 |
chacha20poly1305_seq); |
| 318 |
assert_int_equal(rc, SSH_ERROR); |
| 319 |
|
| 320 |
ssh_cipher_clear(&cipher); |
| 321 |
} |
| 322 |
|
| 323 |
int torture_run_tests(void) { |
| 324 |
int rc; |
| 325 |
const struct CMUnitTest tests[] = { |
| 326 |
cmocka_unit_test(torture_crypto_aes256_cbc), |
| 327 |
cmocka_unit_test(torture_crypto_chacha20poly1305), |
| 328 |
cmocka_unit_test(torture_crypto_chacha20poly1305_bad_packet_length), |
| 329 |
cmocka_unit_test(torture_crypto_chacha20poly1305_bad_data), |
| 330 |
cmocka_unit_test(torture_crypto_chacha20poly1305_bad_tag), |
| 331 |
}; |
| 332 |
|
| 333 |
ssh_init(); |
| 334 |
rc = cmocka_run_group_tests(tests, NULL, NULL); |
| 335 |
ssh_finalize(); |
| 336 |
return rc; |
| 337 |
} |