69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek Crypto tests
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek Author: Jakub Hrozek <jhrozek@redhat.com>
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek Copyright (C) Red Hat, Inc 2010
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek This program is free software; you can redistribute it and/or modify
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek it under the terms of the GNU General Public License as published by
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek (at your option) any later version.
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek This program is distributed in the hope that it will be useful,
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek GNU General Public License for more details.
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek You should have received a copy of the GNU General Public License
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek/* interfaces under test */
96d239e83e671b82525cec760cf0bcaa5ee1c249Lukas SlebodnikSTART_TEST(test_sss_password_encrypt_decrypt)
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek const char *password[] = { "test123", /* general */
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek "12345678901234567", /* just above blocksize */
e07a94a66985b674c5df11ca466792902164c4e2George McCollister#if defined(HAVE_NSS) || defined(HAVE_LIBCRYPTO)
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek for (i=0; password[i]; i++) {
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek ret = sss_password_encrypt(test_ctx, password[i], strlen(password[i])+1,
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek ret = sss_password_decrypt(test_ctx, obfpwd, &ctpwd);
172c07013d1ea99447a780fd36f49d5c3a76981bJakub Hrozek fail_if(ctpwd && strcmp(password[i], ctpwd) != 0);
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta "proper6789012345678901234567890123456789012345678901234567890123",
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta "longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong",
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta "\x2b\x27\x53\x07\x17\xd8\xc0\x8f\x97\x27\xdd\xb3\xec\x41\xd8\xa3\x94\x97\xaa\x35",
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta "\x37\xe7\x0a\x6f\x71\x0b\xa9\x93\x81\x53\x8f\x5c\x06\x83\x44\x2f\xc9\x41\xe3\xed",
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta "\xbd\x99\xa7\x7f\xfc\x5e\xde\x04\x32\x7f\x7b\x71\x4d\xc0\x3f\x51\x2d\x25\x01\x28",
e07a94a66985b674c5df11ca466792902164c4e2George McCollister#if defined(HAVE_NSS) || defined(HAVE_LIBCRYPTO)
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta for (i = 0; keys[i]; i++) {
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta ret = sss_hmac_sha1((const unsigned char *)keys[i], strlen(keys[i]),
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta (const unsigned char *)message, strlen(message),
b35f20cd8ecdc8308a3201e55752fb0443ec6ae4Jan Cholasta fail_if(ret == EOK && memcmp(out, results[i], SSS_SHA1_LENGTH) != 0);
e07a94a66985b674c5df11ca466792902164c4e2George McCollister /* Base64 encode the buffer */
e07a94a66985b674c5df11ca466792902164c4e2George McCollister obfpwd = sss_base64_encode(test_ctx, obfbuf, strlen((const char*)obfbuf));
e07a94a66985b674c5df11ca466792902164c4e2George McCollister /* Base64 decode the buffer */
e07a94a66985b674c5df11ca466792902164c4e2George McCollister obfbuf = sss_base64_decode(test_ctx, b64encoded, &obflen);
e07a94a66985b674c5df11ca466792902164c4e2George McCollister fail_if(obflen != strlen((const char*)expected));
e07a94a66985b674c5df11ca466792902164c4e2George McCollister fail_if(memcmp(obfbuf, expected, obflen) != 0);
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik size_t key_len = sizeof(key); /* need to be 32 */
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik const size_t input_text_len = sizeof(input_text) - 1;
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik ret = sss_encrypt(test_ctx, AES256CBC_HMAC_SHA256, key, key_len,
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik (const uint8_t *)input_text, input_text_len,
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik ret = memcmp(input_text, cipher_text, input_text_len);
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik fail_if(ret == 0, "Input and encrypted text has common prefix");
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik ret = sss_decrypt(test_ctx, AES256CBC_HMAC_SHA256, key, key_len,
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik ret = memcmp(plain_text, input_text, input_text_len);
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik fail_if(ret != 0, "input text is not the same as de-encrypted text");
4139a7a731f2831963a42b26aac111422be28792Jakub Hrozek tcase_add_checked_fixture(tc, ck_leak_check_setup, ck_leak_check_teardown);
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek /* Do some testing */
96d239e83e671b82525cec760cf0bcaa5ee1c249Lukas Slebodnik tcase_add_test(tc, test_sss_password_encrypt_decrypt);
65c85654d9b32a866caa01c28fe743eeb0bdef67Lukas Slebodnik tcase_add_test(tc, test_sss_encrypt_decrypt);
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek /* Add all test cases to the test suite */
66c238caf7a35701ea7b8bc8a3c15424c9867e9fJan Zeleny { "debug-level", 'd', POPT_ARG_INT, &debug_level, 0, "Set debug level", NULL },
49dd8ee2834d9477418961dbaffa4a03cfa9fd1eRené Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
69aaef8719c5cf33ed1c4090fa313ba281bf8a02Jakub Hrozek pc = poptGetContext(argv[0], argc, argv, long_options, 0);