| 1 | /* test_crypt.cc - Test cryptography implementation |
| 2 | part of the minizip-ng project |
| 3 | |
| 4 | Copyright (C) Nathan Moinvaziri |
| 5 | https://github.com/zlib-ng/minizip-ng |
| 6 | |
| 7 | This program is distributed under the terms of the same license as zlib. |
| 8 | See the accompanying LICENSE file for the full text of the license. |
| 9 | */ |
| 10 | |
| 11 | #include "mz.h" |
| 12 | #include "mz_os.h" |
| 13 | #include "mz_crypt.h" |
| 14 | |
| 15 | #include <string> |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <stdio.h> /* printf, snprintf */ |
| 20 | |
| 21 | #if GTEST_OS_WINDOWS |
| 22 | # include <sdkddkver.h> /* _WIN32_WINNT defines */ |
| 23 | #endif |
| 24 | |
| 25 | #ifndef MZ_ZIP_NO_CRYPTO |
| 26 | static const char *hash_test_string = "the quick and lazy fox did his thang"; |
| 27 | |
| 28 | static void convert_buffer_to_hex_string(uint8_t *buf, int32_t buf_size, char *hex_string, int32_t max_hex_string) { |
| 29 | int32_t p = 0; |
| 30 | int32_t i = 0; |
| 31 | |
| 32 | if (max_hex_string > 0) |
| 33 | hex_string[0] = 0; |
| 34 | for (i = 0, p = 0; i < (int32_t)buf_size && p < max_hex_string; i += 1, p += 2) |
| 35 | snprintf(hex_string + p, max_hex_string - p, "%02x", buf[i]); |
| 36 | if (p < max_hex_string) |
| 37 | hex_string[p] = 0; |
| 38 | } |
| 39 | |
| 40 | TEST(crypt, rand) { |
| 41 | uint8_t random_bytes[256]; |
| 42 | |
| 43 | memset(random_bytes, 0, sizeof(random_bytes)); |
| 44 | |
| 45 | EXPECT_EQ(mz_crypt_rand(random_bytes, sizeof(random_bytes)), sizeof(random_bytes)); |
| 46 | |
| 47 | EXPECT_NE(std::string((char *)random_bytes, sizeof(random_bytes)).find_first_not_of('\0'), std::string::npos); |
| 48 | } |
| 49 | |
| 50 | TEST(crypt, sha1) { |
| 51 | void *sha1 = nullptr; |
| 52 | uint8_t hash1[MZ_HASH_SHA1_SIZE]; |
| 53 | char computed_hash[256]; |
| 54 | |
| 55 | memset(hash1, 0, sizeof(hash1)); |
| 56 | |
| 57 | sha1 = mz_crypt_sha_create(); |
| 58 | ASSERT_NE(sha1, nullptr); |
| 59 | mz_crypt_sha_set_algorithm(sha1, MZ_HASH_SHA1); |
| 60 | mz_crypt_sha_begin(sha1); |
| 61 | mz_crypt_sha_update(sha1, hash_test_string, (int32_t)strlen(hash_test_string)); |
| 62 | mz_crypt_sha_end(sha1, hash1, sizeof(hash1)); |
| 63 | mz_crypt_sha_delete(&sha1); |
| 64 | |
| 65 | convert_buffer_to_hex_string(hash1, sizeof(hash1), computed_hash, sizeof(computed_hash)); |
| 66 | |
| 67 | EXPECT_STREQ(computed_hash, "3efb8392b6cd8e14bd76bd08081521dc73df418c"); |
| 68 | } |
| 69 | |
| 70 | TEST(crypt, sha224) { |
| 71 | # if GTEST_OS_WINDOWS |
| 72 | GTEST_SKIP() << "SHA224 not supported on Windows"; |
| 73 | # else |
| 74 | void *sha224 = nullptr; |
| 75 | uint8_t hash224[MZ_HASH_SHA224_SIZE]; |
| 76 | char computed_hash[256]; |
| 77 | |
| 78 | memset(hash224, 0, sizeof(hash224)); |
| 79 | |
| 80 | sha224 = mz_crypt_sha_create(); |
| 81 | ASSERT_NE(sha224, nullptr); |
| 82 | mz_crypt_sha_set_algorithm(sha224, MZ_HASH_SHA224); |
| 83 | mz_crypt_sha_begin(sha224); |
| 84 | mz_crypt_sha_update(sha224, hash_test_string, (int32_t)strlen(hash_test_string)); |
| 85 | mz_crypt_sha_end(sha224, hash224, sizeof(hash224)); |
| 86 | mz_crypt_sha_delete(&sha224); |
| 87 | |
| 88 | convert_buffer_to_hex_string(hash224, sizeof(hash224), computed_hash, sizeof(computed_hash)); |
| 89 | |
| 90 | EXPECT_STREQ(computed_hash, "9e444f5f0b6582a923bd48696155f4a2f0d914e044cb64b8729a6600"); |
| 91 | # endif |
| 92 | } |
| 93 | |
| 94 | TEST(crypt, sha256) { |
| 95 | # if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP |
| 96 | GTEST_SKIP() << "SHA256 not supported on Windows XP"; |
| 97 | # else |
| 98 | void *sha256 = nullptr; |
| 99 | uint8_t hash256[MZ_HASH_SHA256_SIZE]; |
| 100 | char computed_hash[256]; |
| 101 | |
| 102 | memset(hash256, 0, sizeof(hash256)); |
| 103 | |
| 104 | sha256 = mz_crypt_sha_create(); |
| 105 | ASSERT_NE(sha256, nullptr); |
| 106 | mz_crypt_sha_set_algorithm(sha256, MZ_HASH_SHA256); |
| 107 | mz_crypt_sha_begin(sha256); |
| 108 | mz_crypt_sha_update(sha256, hash_test_string, (int32_t)strlen(hash_test_string)); |
| 109 | mz_crypt_sha_end(sha256, hash256, sizeof(hash256)); |
| 110 | mz_crypt_sha_delete(&sha256); |
| 111 | |
| 112 | convert_buffer_to_hex_string(hash256, sizeof(hash256), computed_hash, sizeof(computed_hash)); |
| 113 | |
| 114 | EXPECT_STREQ(computed_hash, "7a31ea0848525f7ebfeec9ee532bcc5d6d26772427e097b86cf440a56546541c"); |
| 115 | # endif |
| 116 | } |
| 117 | |
| 118 | TEST(crypt, sha384) { |
| 119 | # if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP |
| 120 | GTEST_SKIP() << "SHA384 not supported on Windows XP"; |
| 121 | # else |
| 122 | void *sha384 = nullptr; |
| 123 | uint8_t hash384[MZ_HASH_SHA384_SIZE]; |
| 124 | char computed_hash[256]; |
| 125 | |
| 126 | memset(hash384, 0, sizeof(hash384)); |
| 127 | |
| 128 | sha384 = mz_crypt_sha_create(); |
| 129 | ASSERT_NE(sha384, nullptr); |
| 130 | mz_crypt_sha_set_algorithm(sha384, MZ_HASH_SHA384); |
| 131 | mz_crypt_sha_begin(sha384); |
| 132 | mz_crypt_sha_update(sha384, hash_test_string, (int32_t)strlen(hash_test_string)); |
| 133 | mz_crypt_sha_end(sha384, hash384, sizeof(hash384)); |
| 134 | mz_crypt_sha_delete(&sha384); |
| 135 | |
| 136 | convert_buffer_to_hex_string(hash384, sizeof(hash384), computed_hash, sizeof(computed_hash)); |
| 137 | |
| 138 | EXPECT_STREQ(computed_hash, |
| 139 | "e1e42e5977965bb3621231a5df3a1e83c471fa91fde33b6a30c8c4fa0d8be29ba7171c7c9487db91e9ee7e85049f7b41"); |
| 140 | # endif |
| 141 | } |
| 142 | |
| 143 | TEST(crypt, sha512) { |
| 144 | # if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP |
| 145 | GTEST_SKIP() << "SHA512 not supported on Windows XP"; |
| 146 | # else |
| 147 | void *sha512 = nullptr; |
| 148 | uint8_t hash512[MZ_HASH_SHA512_SIZE]; |
| 149 | char computed_hash[256]; |
| 150 | |
| 151 | memset(hash512, 0, sizeof(hash512)); |
| 152 | |
| 153 | sha512 = mz_crypt_sha_create(); |
| 154 | ASSERT_NE(sha512, nullptr); |
| 155 | mz_crypt_sha_set_algorithm(sha512, MZ_HASH_SHA512); |
| 156 | mz_crypt_sha_begin(sha512); |
| 157 | mz_crypt_sha_update(sha512, hash_test_string, (int32_t)strlen(hash_test_string)); |
| 158 | mz_crypt_sha_end(sha512, hash512, sizeof(hash512)); |
| 159 | mz_crypt_sha_delete(&sha512); |
| 160 | |
| 161 | convert_buffer_to_hex_string(hash512, sizeof(hash512), computed_hash, sizeof(computed_hash)); |
| 162 | |
| 163 | EXPECT_STREQ(computed_hash, |
| 164 | "6627e7643ee7ce633e03f52d22329c3a32597364247c5275d4369985e1518626da46f595ad327667346479d246359b8b381af" |
| 165 | "791ce2ac8c53a4788050eea11fe"); |
| 166 | # endif |
| 167 | } |
| 168 | |
| 169 | TEST(crypt, aes128) { |
| 170 | void *aes = nullptr; |
| 171 | const char *key = "awesomekeythisis"; |
| 172 | const char *test = "youknowitsogrowi"; |
| 173 | int32_t key_length = 0; |
| 174 | int32_t test_length = 0; |
| 175 | uint8_t buf[120]; |
| 176 | |
| 177 | key_length = (int32_t)strlen(key); |
| 178 | test_length = (int32_t)strlen(test); |
| 179 | |
| 180 | strncpy((char *)buf, test, sizeof(buf)); |
| 181 | |
| 182 | aes = mz_crypt_aes_create(); |
| 183 | ASSERT_NE(aes, nullptr); |
| 184 | mz_crypt_aes_set_encrypt_key(aes, key, key_length, nullptr, 0); |
| 185 | EXPECT_EQ(mz_crypt_aes_encrypt(aes, nullptr, 0, buf, test_length), test_length); |
| 186 | mz_crypt_aes_delete(&aes); |
| 187 | |
| 188 | EXPECT_STRNE((char *)buf, test); |
| 189 | |
| 190 | aes = mz_crypt_aes_create(); |
| 191 | ASSERT_NE(aes, nullptr); |
| 192 | mz_crypt_aes_set_decrypt_key(aes, key, key_length, nullptr, 0); |
| 193 | EXPECT_EQ(mz_crypt_aes_decrypt(aes, nullptr, 0, buf, test_length), test_length); |
| 194 | mz_crypt_aes_delete(&aes); |
| 195 | |
| 196 | EXPECT_STREQ((char *)buf, test); |
| 197 | } |
| 198 | |
| 199 | TEST(crypt, aes128_cbc) { |
| 200 | void *aes = nullptr; |
| 201 | const char *key = "awesomekeythisis"; |
| 202 | const char *test = "youknowitsogrowi"; |
| 203 | const char *iv = "0123456789123456"; |
| 204 | int32_t key_length = 0; |
| 205 | int32_t test_length = 0; |
| 206 | int32_t iv_length = 0; |
| 207 | uint8_t buf[120]; |
| 208 | |
| 209 | key_length = (int32_t)strlen(key); |
| 210 | test_length = (int32_t)strlen(test); |
| 211 | iv_length = (int32_t)strlen(iv); |
| 212 | |
| 213 | strncpy((char *)buf, test, sizeof(buf)); |
| 214 | |
| 215 | aes = mz_crypt_aes_create(); |
| 216 | ASSERT_NE(aes, nullptr); |
| 217 | mz_crypt_aes_set_mode(aes, MZ_AES_MODE_CBC); |
| 218 | EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, iv, iv_length), MZ_OK); |
| 219 | EXPECT_EQ(mz_crypt_aes_encrypt(aes, nullptr, 0, buf, test_length), test_length); |
| 220 | mz_crypt_aes_delete(&aes); |
| 221 | |
| 222 | EXPECT_STRNE((char *)buf, test); |
| 223 | |
| 224 | aes = mz_crypt_aes_create(); |
| 225 | ASSERT_NE(aes, nullptr); |
| 226 | mz_crypt_aes_set_mode(aes, MZ_AES_MODE_CBC); |
| 227 | EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, iv, iv_length), MZ_OK); |
| 228 | EXPECT_EQ(mz_crypt_aes_decrypt(aes, nullptr, 0, buf, test_length), test_length); |
| 229 | mz_crypt_aes_delete(&aes); |
| 230 | |
| 231 | EXPECT_STREQ((char *)buf, test); |
| 232 | } |
| 233 | |
| 234 | TEST(crypt, aes128_gcm) { |
| 235 | # if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP |
| 236 | GTEST_SKIP() << "SHA256 not supported on Windows XP"; |
| 237 | # else |
| 238 | void *aes = nullptr; |
| 239 | const char *key = "awesomekeythisis"; |
| 240 | const char *test = "youknowitsogrowi"; |
| 241 | const char *iv = "0123456789123456"; |
| 242 | const char *aad = "additional authentication data"; |
| 243 | int32_t key_length = 0; |
| 244 | int32_t test_length = 0; |
| 245 | int32_t iv_length = 0; |
| 246 | int32_t aad_length = 0; |
| 247 | uint8_t buf[120]; |
| 248 | uint8_t tag[MZ_AES_BLOCK_SIZE] = {0}; |
| 249 | |
| 250 | key_length = (int32_t)strlen(key); |
| 251 | test_length = (int32_t)strlen(test); |
| 252 | iv_length = (int32_t)strlen(iv); |
| 253 | aad_length = (int32_t)strlen(aad); |
| 254 | |
| 255 | strncpy((char *)buf, test, sizeof(buf)); |
| 256 | strncpy((char *)buf + test_length, test, sizeof(buf) - test_length); |
| 257 | |
| 258 | aes = mz_crypt_aes_create(); |
| 259 | ASSERT_NE(aes, nullptr); |
| 260 | mz_crypt_aes_set_mode(aes, MZ_AES_MODE_GCM); |
| 261 | EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, iv, iv_length), MZ_OK); |
| 262 | EXPECT_EQ(mz_crypt_aes_encrypt(aes, aad, aad_length, buf, test_length), test_length); |
| 263 | EXPECT_EQ(mz_crypt_aes_encrypt_final(aes, buf + test_length, test_length - 1, tag, sizeof(tag)), test_length - 1); |
| 264 | mz_crypt_aes_delete(&aes); |
| 265 | |
| 266 | EXPECT_STRNE((char *)buf, test); |
| 267 | |
| 268 | aes = mz_crypt_aes_create(); |
| 269 | ASSERT_NE(aes, nullptr); |
| 270 | mz_crypt_aes_set_mode(aes, MZ_AES_MODE_GCM); |
| 271 | EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, iv, iv_length), MZ_OK); |
| 272 | EXPECT_EQ(mz_crypt_aes_decrypt(aes, aad, aad_length, buf, test_length), test_length); |
| 273 | EXPECT_EQ(mz_crypt_aes_decrypt_final(aes, buf + test_length, test_length - 1, tag, sizeof(tag)), test_length - 1); |
| 274 | mz_crypt_aes_delete(&aes); |
| 275 | |
| 276 | EXPECT_EQ(memcmp(buf, test, test_length), 0); |
| 277 | EXPECT_EQ(memcmp(buf + test_length, test, test_length - 1), 0); |
| 278 | # endif |
| 279 | } |
| 280 | |
| 281 | TEST(crypt, aes194) { |
| 282 | void *aes = nullptr; |
| 283 | const char *key = "awesomekeythisisbeefyone"; |
| 284 | const char *test = "youknowitsogrowi"; |
| 285 | int32_t key_length = 0; |
| 286 | int32_t test_length = 0; |
| 287 | uint8_t buf[120]; |
| 288 | |
| 289 | key_length = (int32_t)strlen(key); |
| 290 | test_length = (int32_t)strlen(test); |
| 291 | |
| 292 | strncpy((char *)buf, test, sizeof(buf)); |
| 293 | |
| 294 | aes = mz_crypt_aes_create(); |
| 295 | ASSERT_NE(aes, nullptr); |
| 296 | EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, nullptr, 0), MZ_OK); |
| 297 | EXPECT_EQ(mz_crypt_aes_encrypt(aes, nullptr, 0, buf, test_length), test_length); |
| 298 | mz_crypt_aes_delete(&aes); |
| 299 | |
| 300 | EXPECT_STRNE((char *)buf, test); |
| 301 | |
| 302 | aes = mz_crypt_aes_create(); |
| 303 | ASSERT_NE(aes, nullptr); |
| 304 | EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, nullptr, 0), MZ_OK); |
| 305 | EXPECT_EQ(mz_crypt_aes_decrypt(aes, nullptr, 0, buf, test_length), test_length); |
| 306 | mz_crypt_aes_delete(&aes); |
| 307 | |
| 308 | EXPECT_STREQ((char *)buf, test); |
| 309 | } |
| 310 | |
| 311 | TEST(crypt, aes256) { |
| 312 | void *aes = nullptr; |
| 313 | const char *key = "awesomekeythisisevenmoresolidone"; |
| 314 | const char *test = "youknowitsogrowi"; |
| 315 | int32_t key_length = 0; |
| 316 | int32_t test_length = 0; |
| 317 | uint8_t buf[120]; |
| 318 | |
| 319 | key_length = (int32_t)strlen(key); |
| 320 | test_length = (int32_t)strlen(test); |
| 321 | |
| 322 | strncpy((char *)buf, test, sizeof(buf)); |
| 323 | |
| 324 | aes = mz_crypt_aes_create(); |
| 325 | ASSERT_NE(aes, nullptr); |
| 326 | EXPECT_EQ(mz_crypt_aes_set_encrypt_key(aes, key, key_length, nullptr, 0), MZ_OK); |
| 327 | EXPECT_EQ(mz_crypt_aes_encrypt(aes, nullptr, 0, buf, test_length), test_length); |
| 328 | mz_crypt_aes_delete(&aes); |
| 329 | |
| 330 | EXPECT_STRNE((char *)buf, test); |
| 331 | |
| 332 | aes = mz_crypt_aes_create(); |
| 333 | ASSERT_NE(aes, nullptr); |
| 334 | EXPECT_EQ(mz_crypt_aes_set_decrypt_key(aes, key, key_length, nullptr, 0), MZ_OK); |
| 335 | EXPECT_EQ(mz_crypt_aes_decrypt(aes, nullptr, 0, buf, test_length), test_length); |
| 336 | mz_crypt_aes_delete(&aes); |
| 337 | |
| 338 | EXPECT_STREQ((char *)buf, test); |
| 339 | } |
| 340 | |
| 341 | TEST(crypt, hmac_sha1) { |
| 342 | void *hmac; |
| 343 | const char *key = "hm123"; |
| 344 | const char *test = "12345678"; |
| 345 | char computed_hash[256]; |
| 346 | uint8_t hash1[MZ_HASH_SHA1_SIZE]; |
| 347 | |
| 348 | hmac = mz_crypt_hmac_create(); |
| 349 | ASSERT_NE(hmac, nullptr); |
| 350 | mz_crypt_hmac_set_algorithm(hmac, MZ_HASH_SHA1); |
| 351 | mz_crypt_hmac_init(hmac, key, (int32_t)strlen(key)); |
| 352 | mz_crypt_hmac_update(hmac, test, (int32_t)strlen(test)); |
| 353 | mz_crypt_hmac_update(hmac, test, (int32_t)strlen(test)); |
| 354 | mz_crypt_hmac_end(hmac, hash1, sizeof(hash1)); |
| 355 | mz_crypt_hmac_delete(&hmac); |
| 356 | |
| 357 | convert_buffer_to_hex_string(hash1, sizeof(hash1), computed_hash, sizeof(computed_hash)); |
| 358 | |
| 359 | EXPECT_STREQ(computed_hash, "3c8c576d947994177af9366c05a6d6cb060edab7"); |
| 360 | } |
| 361 | |
| 362 | TEST(crypt, hmac_sha1_short_password) { |
| 363 | /* Test fixes for CryptImportKey returning ERROR_INVALID_PARAMETER on Windows */ |
| 364 | void *hmac; |
| 365 | /* Intended key is "h", the extra "x" helps test that padding does not read past supplied length */ |
| 366 | const char *key = "hx"; |
| 367 | const char *test = "helloworld"; |
| 368 | char computed_hash[256]; |
| 369 | uint8_t hash1[MZ_HASH_SHA1_SIZE]; |
| 370 | |
| 371 | hmac = mz_crypt_hmac_create(); |
| 372 | ASSERT_NE(hmac, nullptr); |
| 373 | mz_crypt_hmac_set_algorithm(hmac, MZ_HASH_SHA1); |
| 374 | /* Key length is 1, only first char from key should used for this test */ |
| 375 | mz_crypt_hmac_init(hmac, key, 1); |
| 376 | mz_crypt_hmac_update(hmac, test, (int32_t)strlen(test)); |
| 377 | mz_crypt_hmac_end(hmac, hash1, sizeof(hash1)); |
| 378 | mz_crypt_hmac_delete(&hmac); |
| 379 | |
| 380 | convert_buffer_to_hex_string(hash1, sizeof(hash1), computed_hash, sizeof(computed_hash)); |
| 381 | |
| 382 | EXPECT_STREQ(computed_hash, "0d216a670164deca30181479908f65b6b01199e2"); |
| 383 | } |
| 384 | |
| 385 | TEST(crypt, hmac_sha256) { |
| 386 | # if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP |
| 387 | GTEST_SKIP() << "SHA256 not supported on Windows XP"; |
| 388 | # else |
| 389 | void *hmac; |
| 390 | const char *key = "hm123"; |
| 391 | const char *test = "12345678"; |
| 392 | char computed_hash[256]; |
| 393 | uint8_t hash256[MZ_HASH_SHA256_SIZE]; |
| 394 | |
| 395 | hmac = mz_crypt_hmac_create(); |
| 396 | ASSERT_NE(hmac, nullptr); |
| 397 | mz_crypt_hmac_set_algorithm(hmac, MZ_HASH_SHA256); |
| 398 | mz_crypt_hmac_init(hmac, key, (int32_t)strlen(key)); |
| 399 | mz_crypt_hmac_update(hmac, test, (int32_t)strlen(test)); |
| 400 | mz_crypt_hmac_end(hmac, hash256, sizeof(hash256)); |
| 401 | mz_crypt_hmac_delete(&hmac); |
| 402 | |
| 403 | convert_buffer_to_hex_string(hash256, sizeof(hash256), computed_hash, sizeof(computed_hash)); |
| 404 | |
| 405 | EXPECT_STREQ(computed_hash, "fb22a9c715a47a06bad4f6cee9badc31c921562f5d6b24adf2be009f73181f7a"); |
| 406 | # endif |
| 407 | } |
| 408 | |
| 409 | # ifdef HAVE_WZAES |
| 410 | TEST(crypt, pbkdf2) { |
| 411 | uint16_t iteration_count = 1000; |
| 412 | uint8_t key[MZ_HASH_SHA1_SIZE]; |
| 413 | char key_hex[256]; |
| 414 | const char *password = "passwordpasswordpasswordpassword"; |
| 415 | const char *salt = "8F3472E4EA57F56E36F30246DC22C173"; |
| 416 | |
| 417 | EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt), |
| 418 | iteration_count, key, (uint16_t)sizeof(key)), |
| 419 | MZ_OK); |
| 420 | |
| 421 | convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex)); |
| 422 | |
| 423 | EXPECT_STREQ(key_hex, "852c7b71a104aaa8d8996c840c3d4d5d0db780aa"); |
| 424 | } |
| 425 | |
| 426 | TEST(crypt, pbkdf2_long_odd_password) { |
| 427 | uint16_t iteration_count = 1000; |
| 428 | uint8_t key[MZ_HASH_SHA1_SIZE]; |
| 429 | char key_hex[256]; |
| 430 | /* Password has odd length and longer than 64 chars */ |
| 431 | const char *password = "passwordpasswordpasswordpasswordpasswordpasswordpasswordpasswordp"; |
| 432 | const char *salt = "8F3472E4EA57F56E36F30246DC22C173"; |
| 433 | |
| 434 | EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt), |
| 435 | iteration_count, key, (uint16_t)sizeof(key)), |
| 436 | MZ_OK); |
| 437 | |
| 438 | convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex)); |
| 439 | |
| 440 | EXPECT_STREQ(key_hex, "b3e0b5ece77332506972e97b63b3a0a6d36c39a9"); |
| 441 | } |
| 442 | |
| 443 | TEST(crypt, pbkdf2_short_password) { |
| 444 | /* Test fixes for CryptImportKey returns ERROR_INVALID_PARAMETER on Windows */ |
| 445 | uint16_t iteration_count = 1000; |
| 446 | uint8_t key[MZ_HASH_SHA1_SIZE]; |
| 447 | char key_hex[256]; |
| 448 | const char *password = "p"; |
| 449 | const char *salt = "8F3472E4EA57F56E36F30246DC22C173"; |
| 450 | |
| 451 | EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt), |
| 452 | iteration_count, key, (uint16_t)sizeof(key)), |
| 453 | MZ_OK); |
| 454 | |
| 455 | convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex)); |
| 456 | |
| 457 | EXPECT_STREQ(key_hex, "91cf25bb4c2978620255d7fed8cc1751c7d283b9"); |
| 458 | } |
| 459 | |
| 460 | TEST(crypt, pbkdf2_rfc6070_v1) { |
| 461 | /* https://www.ietf.org/rfc/rfc6070.txt */ |
| 462 | uint16_t iteration_count = 2; |
| 463 | uint8_t key[MZ_HASH_SHA1_SIZE]; |
| 464 | char key_hex[256]; |
| 465 | const char *password = "password"; |
| 466 | const char *salt = "salt"; |
| 467 | |
| 468 | EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt), |
| 469 | iteration_count, key, (uint16_t)sizeof(key)), |
| 470 | MZ_OK); |
| 471 | |
| 472 | convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex)); |
| 473 | |
| 474 | EXPECT_STREQ(key_hex, "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"); |
| 475 | } |
| 476 | |
| 477 | TEST(crypt, pbkdf2_rfc6070_v2) { |
| 478 | /* https://www.ietf.org/rfc/rfc6070.txt */ |
| 479 | uint16_t iteration_count = 4096; |
| 480 | uint8_t key[MZ_HASH_SHA1_SIZE]; |
| 481 | char key_hex[256]; |
| 482 | const char *password = "password"; |
| 483 | const char *salt = "salt"; |
| 484 | |
| 485 | EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt), |
| 486 | iteration_count, key, (uint16_t)sizeof(key)), |
| 487 | MZ_OK); |
| 488 | |
| 489 | convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex)); |
| 490 | |
| 491 | EXPECT_STREQ(key_hex, "4b007901b765489abead49d926f721d065a429c1"); |
| 492 | } |
| 493 | |
| 494 | TEST(crypt, pbkdf2_rfc6070_v3) { |
| 495 | /* https://www.ietf.org/rfc/rfc6070.txt */ |
| 496 | uint32_t iteration_count = 16777216U; |
| 497 | uint8_t key[MZ_HASH_SHA1_SIZE]; |
| 498 | char key_hex[256]; |
| 499 | const char *password = "password"; |
| 500 | const char *salt = "salt"; |
| 501 | |
| 502 | EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt), |
| 503 | iteration_count, key, (uint16_t)sizeof(key)), |
| 504 | MZ_OK); |
| 505 | |
| 506 | convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex)); |
| 507 | |
| 508 | EXPECT_STREQ(key_hex, "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984"); |
| 509 | } |
| 510 | |
| 511 | TEST(crypt, pbkdf2_rfc6070_v4) { |
| 512 | /* https://www.ietf.org/rfc/rfc6070.txt */ |
| 513 | uint16_t iteration_count = 4096; |
| 514 | uint8_t key[25]; |
| 515 | char key_hex[256]; |
| 516 | const char *password = "passwordPASSWORDpassword"; |
| 517 | const char *salt = "saltSALTsaltSALTsaltSALTsaltSALTsalt"; |
| 518 | |
| 519 | EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt), |
| 520 | iteration_count, key, (uint16_t)sizeof(key)), |
| 521 | MZ_OK); |
| 522 | |
| 523 | convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex)); |
| 524 | |
| 525 | EXPECT_STREQ(key_hex, "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038"); |
| 526 | } |
| 527 | |
| 528 | TEST(crypt, pbkdf2_rfc6070_v5) { |
| 529 | /* https://www.ietf.org/rfc/rfc6070.txt */ |
| 530 | uint16_t iteration_count = 4096; |
| 531 | uint8_t key[16]; |
| 532 | char key_hex[256]; |
| 533 | const char *password = "pass\0word"; |
| 534 | const char *salt = "sa\0lt"; |
| 535 | |
| 536 | EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, 9, (uint8_t *)salt, 5, iteration_count, key, (uint16_t)sizeof(key)), |
| 537 | MZ_OK); |
| 538 | |
| 539 | convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex)); |
| 540 | |
| 541 | EXPECT_STREQ(key_hex, "56fa6aa75548099dcc37d7f03425e0c3"); |
| 542 | } |
| 543 | # endif |
| 544 | #endif |