File: check_reduce.c

package info (click to toggle)
libzc 0.4.3-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 872 kB
  • sloc: ansic: 7,407; sh: 327; python: 171; makefile: 99
file content (126 lines) | stat: -rw-r--r-- 3,198 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 *  zc - zip crack library
 *  Copyright (C) 2012-2018 Marc Ferland
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <check.h>
#include <stdlib.h>

#include "libzc.h"
#include "ptext_private.h"
#include "test_plaintext.h"
#include "ptext_reduce.c"

#define KEY3(index) test_plaintext[index] ^ test_ciphertext[index]

struct zc_ctx *ctx;
struct zc_crk_ptext *ptext;

void setup_reduce()
{
	zc_new(&ctx);
	zc_crk_ptext_new(ctx, &ptext);
}

void teardown_reduce()
{
	zc_crk_ptext_unref(ptext);
	zc_unref(ctx);
}

START_TEST(test_can_get_bits_15_2)
{
	fail_if(ptext->bits_15_2[0] != 0);
}
END_TEST

START_TEST(test_can_generate_first_gen_key2)
{
	uint32_t *key2_first_gen;
	uint16_t *bits15_2;

	bits15_2 = get_bits_15_2(ptext->bits_15_2, 0);
	key2_first_gen = calloc((1 << 22), sizeof(uint32_t));
	generate_all_key2_bits_31_2(key2_first_gen, bits15_2);
	fail_if(key2_first_gen[0] != 0);
	free(key2_first_gen);
}
END_TEST

#ifdef EXTRACHECK
START_TEST(test_can_generate_next_array_from_plaintext)
{
	uint32_t *key2_first_gen, *key2_next_gen;
	size_t key2_first_gen_size, total = 0;

	uint8_t key3i = KEY3(TEST_PLAINTEXT_SIZE - 1);
	uint8_t key3im1 = KEY3(TEST_PLAINTEXT_SIZE - 2);
	uint8_t key3im2 = KEY3(TEST_PLAINTEXT_SIZE - 3);

	key2_first_gen = calloc((1 << 22), sizeof(uint32_t));
	generate_all_key2_bits_31_2(key2_first_gen, get_bits_15_2(ptext->bits_15_2, key3i));
	key2_first_gen_size = (1 << 22);
	key2_next_gen = calloc((1 << 22), sizeof(uint32_t));

	for (size_t i = 0; i < key2_first_gen_size; ++i)
		total += key2r_compute_single(key2_first_gen[i],
					      &key2_next_gen[total],
					      get_bits_15_2(ptext->bits_15_2, key3im1),
					      get_bits_15_2(ptext->bits_15_2, key3im2),
					      KEY2_MASK_6BITS);

	uniq(key2_next_gen, &total);

	fail_if(total != 2256896);

	free(key2_next_gen);
	free(key2_first_gen);
}
END_TEST
#endif

Suite *reduce_suite()
{
	Suite *s = suite_create("reduce");

	TCase *tc_core = tcase_create("Core");
	tcase_add_checked_fixture(tc_core, setup_reduce, teardown_reduce);
	tcase_add_test(tc_core, test_can_get_bits_15_2);
	tcase_add_test(tc_core, test_can_generate_first_gen_key2);
#ifdef EXTRACHECK
	tcase_add_test(tc_core, test_can_generate_next_array_from_plaintext);
#endif
	tcase_set_timeout(tc_core, 60);
	suite_add_tcase(s, tc_core);

	return s;
}

int main()
{
	int number_failed;
	Suite *s;
	SRunner *sr;

	s = reduce_suite();
	sr = srunner_create(s);

	srunner_run_all(sr, CK_NORMAL);
	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}