File: test_crypt.c

package info (click to toggle)
swupdate 2025.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,004 kB
  • sloc: ansic: 66,621; python: 6,291; makefile: 791; sh: 538; javascript: 229
file content (172 lines) | stat: -rw-r--r-- 5,454 bytes parent folder | download | duplicates (3)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: GPL-2.0-or-later

/*
 * Author: Christian Storm
 * Copyright (C) 2017, Siemens AG
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc.
 */

#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <util.h>
#include <swupdate_crypto.h>

#define EVP_MAX_BLOCK_LENGTH (16)

struct cryptdata {
	unsigned char *key;
	unsigned char *iv;
	unsigned char *crypttext;
	cipher_t cipher;
};

static void hex2bin(unsigned char *dest, const unsigned char *source)
{
	unsigned int val;
	for (unsigned int i = 0; i < strlen((const char *)source); i += 2) {
		val = from_ascii((const char *)&source[i], 2, LG_16);
		dest[i / 2] = val;
	}
}

static void do_crypt(struct cryptdata *crypt, char keylen, unsigned char *CRYPTTEXT, unsigned char *PLAINTEXT)
{
	int len;
	void *dcrypt = swupdate_DECRYPT_init(crypt->key, keylen, crypt->iv, crypt->cipher);
	assert_non_null(dcrypt);

	unsigned char *buffer = calloc(1, strlen((const char *)CRYPTTEXT) + EVP_MAX_BLOCK_LENGTH);
	int ret = swupdate_DECRYPT_update(dcrypt, buffer, &len, crypt->crypttext, strlen((const char *)CRYPTTEXT) / 2);
	assert_true(ret >= 0);
	assert_true(len == 0);

	ret = swupdate_DECRYPT_final(dcrypt, buffer, &len);
	assert_true(ret == 0);
	assert_true(len == (int)strlen((const char *)PLAINTEXT));
	assert_true(strncmp((const char *)buffer, (const char *)PLAINTEXT, len) == 0);
	free(buffer);
}

static void test_crypt_128(void **state)
{
	(void)state;

	unsigned char KEY[] = "E5E9FA1BA31ECD1AE84F75CAAA474FB2";
	unsigned char IV[] = "E93DA465B309C53FEC5FF93C9637DA58";
	unsigned char CRYPTTEXT[] = "a68148be39f9c60175ccc31c19ab92e7";
	unsigned char PLAINTEXT[] = "CRYPTTEST";

	struct cryptdata crypt;
	hex2bin((crypt.key = calloc(1, strlen((const char *)KEY))), KEY);
	hex2bin((crypt.iv = calloc(1, strlen((const char *)IV))), IV);
	hex2bin((crypt.crypttext = calloc(1, strlen((const char *)CRYPTTEXT))), CRYPTTEXT);
	crypt.cipher = AES_CBC_128;

	do_crypt(&crypt, 16, &CRYPTTEXT[0], &PLAINTEXT[0]);

	free(crypt.key);
	free(crypt.iv);
	free(crypt.crypttext);
}

static void test_crypt_192(void **state)
{
	(void)state;

	unsigned char KEY[] = "F8A4B2D01A4A28C39E50D789C5B3CC386E56B63F16A7211A";
	unsigned char IV[] = "08E8E00743E98EE82B90BBCC0DE83A77";
	unsigned char CRYPTTEXT[] = "b5adf128eed12c9f13bd85cfdbe2d0fc";
	unsigned char PLAINTEXT[] = "CRYPTTEST";

	struct cryptdata crypt;
	hex2bin((crypt.key = calloc(1, strlen((const char *)KEY))), KEY);
	hex2bin((crypt.iv = calloc(1, strlen((const char *)IV))), IV);
	hex2bin((crypt.crypttext = calloc(1, strlen((const char *)CRYPTTEXT))), CRYPTTEXT);
	crypt.cipher = AES_CBC_192;

	do_crypt(&crypt, 24, &CRYPTTEXT[0], &PLAINTEXT[0]);

	free(crypt.key);
	free(crypt.iv);
	free(crypt.crypttext);
}

static void test_crypt_256(void **state)
{
	(void)state;

	unsigned char KEY[] = "69D54287F856D30B51B812FDF714556778CF31E1B104D9C68BD90C669C37D1AB";
	unsigned char IV[] = "E7039ABFCA63EB8EB1D320F7918275B2";
	unsigned char CRYPTTEXT[] = "A17EBBB1A28459352FE3A994404E35AA";
	unsigned char PLAINTEXT[] = "CRYPTTEST";

	struct cryptdata crypt;
	hex2bin((crypt.key = calloc(1, strlen((const char *)KEY))), KEY);
	hex2bin((crypt.iv = calloc(1, strlen((const char *)IV))), IV);
	hex2bin((crypt.crypttext = calloc(1, strlen((const char *)CRYPTTEXT))), CRYPTTEXT);
	crypt.cipher = AES_CBC; // AES_CBC_256

	do_crypt(&crypt, 32, &CRYPTTEXT[0], &PLAINTEXT[0]);

	free(crypt.key);
	free(crypt.iv);
	free(crypt.crypttext);
}

static void test_crypt_failure(void **state)
{
	(void)state;

	unsigned char KEY[] = "E5E9FA1BA31ECD1AE84F75CAAA474F3A663F05F412028F81DA65D26EE56424B2";
	unsigned char IV[] = "E93DA465B309C53FEC5FF93C9637DA58";
	unsigned char CRYPTTEXT[] = "CAFECAFECAFECAFECAFECAFECAFECAFE";

	struct cryptdata crypt;
	hex2bin((crypt.key = calloc(1, strlen((const char *)KEY))), KEY);
	hex2bin((crypt.iv = calloc(1, strlen((const char *)IV))), IV);
	hex2bin((crypt.crypttext = calloc(1, strlen((const char *)CRYPTTEXT))), CRYPTTEXT);
	crypt.cipher = AES_CBC;

	int len;
	void *dcrypt = swupdate_DECRYPT_init(crypt.key, 32, crypt.iv, crypt.cipher);
	assert_non_null(dcrypt);

	unsigned char *buffer = calloc(1, strlen((const char *)CRYPTTEXT) + EVP_MAX_BLOCK_LENGTH);
	int ret = swupdate_DECRYPT_update(dcrypt, buffer, &len, crypt.crypttext, strlen((const char *)CRYPTTEXT) / 2);
	ret = swupdate_DECRYPT_final(dcrypt, buffer, &len);
	assert_true(ret != 0);
	free(buffer);

	free(crypt.key);
	free(crypt.iv);
	free(crypt.crypttext);
}

int main(void)
{
	int error_count = 0;
	const struct CMUnitTest crypt_tests[] = {
		cmocka_unit_test(test_crypt_128),
		cmocka_unit_test(test_crypt_192),
		cmocka_unit_test(test_crypt_256),
		cmocka_unit_test(test_crypt_failure)
	};
	error_count += cmocka_run_group_tests_name("crypt", crypt_tests, NULL, NULL);
	return error_count;
}