File: test_buffer.c

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (162 lines) | stat: -rw-r--r-- 4,918 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2014-2015,2017 Andrew Cagney <cagney@gnu.org>
 *
 * 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.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * 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.
 */

#include <stdlib.h>

#include "constants.h"
#include "lswalloc.h"
#include "pk11pub.h"

#include "crypt_symkey.h"
#include "crypt_mac.h"
#include "test_buffer.h"
#include "ike_alg.h"

#include "lswlog.h"

/*
 * Given an ASCII string, convert it into a chunk of bytes.  If the
 * string is prefixed by 0x assume the contents are hex (with spaces)
 * and decode it; otherwise it is assumed that the ASCII (minus the
 * NUL) should be copied.
 *
 * The caller must free the chunk.
 */
chunk_t decode_to_chunk(const char *prefix, const char *original,
			struct logger *logger, where_t where UNUSED)
{
	if (LDBGP(DBG_CRYPT, logger)) {
		LDBG_log(logger, "%s() %s: input \"%s\"",
			 __func__, prefix, original);
	}
	chunk_t chunk;
	if (startswith(original, "0x")) {
		chunk = chunk_from_hex(original + strlen("0x"), original);
	} else {
		chunk = alloc_chunk(strlen(original), original);
		memcpy(chunk.ptr, original, chunk.len);
	}
	if (LDBGP(DBG_CRYPT, logger)) {
		LDBG_log(logger, "%s() output:", __func__);
		LDBG_hunk(logger, chunk);
	}
	return chunk;
}

/*
 * Given an ASCII string, convert it into a chunk of bytes.  If the
 * string is prefixed by 0x assume the contents are hex (with spaces)
 * and decode it; otherwise it is assumed that the ASCII (minus the
 * NUL) should be copied.
 *
 * The caller must free the chunk.
 */

struct crypt_mac decode_to_mac(const char *prefix, const char *original,
			       struct logger *logger, where_t where UNUSED)
{
	if (LDBGP(DBG_CRYPT, logger)) {
		LDBG_log(logger, "%s() %s: input \"%s\"",
			 __func__, prefix, original);
	}
	struct crypt_mac mac;
	if (startswith(original, "0x")) {
		chunk_t chunk = chunk_from_hex(original + strlen("0x"), original);
		PASSERT(logger, chunk.len <= sizeof(mac.ptr/*array*/));
		mac.len = chunk.len;
		memcpy(mac.ptr, chunk.ptr, mac.len);
		free_chunk_content(&chunk);
	} else {
		mac.len = strlen(original);
		PASSERT(logger, mac.len <= sizeof(mac.ptr/*array*/));
		memcpy(mac.ptr, original, mac.len);
	}
	if (LDBGP(DBG_CRYPT, logger)) {
		LDBG_log(logger, "%s() output:", __func__);
		LDBG_hunk(logger, mac);
	}
	return mac;
}

PK11SymKey *decode_hex_to_symkey(const char *prefix, const char *string,
				 struct logger *logger, where_t where UNUSED)
{
	chunk_t chunk = chunk_from_hex(string, prefix);
	PK11SymKey *symkey = symkey_from_hunk(prefix, chunk, logger);
	free_chunk_content(&chunk);
	return symkey;
}

/*
 * Verify that the chunk's data is the same as actual.
 */

bool verify_bytes(const char *desc, const char *verifying,
		  const void *expected, size_t expected_size,
		  const void *actual, size_t actual_size,
		  struct logger *logger, where_t where)
{
	if (expected_size != actual_size) {
		llog_pexpect(logger, where,
			     "%s: %s: expected length %zd but got %zd",
			     desc, verifying, expected_size, actual_size);
		return false;
	}

	size_t i;
	for (i = 0; i < expected_size; i++) {
		uint8_t e = ((const uint8_t*)expected)[i];
		uint8_t a = ((const uint8_t*)actual)[i];
		if (e != a) {
			/* Caller should issue the real log message. */
			llog_pexpect(logger, where,
				     "%s: %s: bytes at %zd differ, expected %02x found %02x",
				     desc, verifying, i, e, a);
			return false;
		}
	}
	if (LDBGP(DBG_CRYPT, logger)) {
		LDBG_log(logger, "%s() %s: %s: ok", __func__, desc, verifying);
	}
	return true;
}

/* verify that expected is the same as actual */
bool verify_symkey(const char *desc, const char *verifying,
		   chunk_t expected, PK11SymKey *actual,
		   struct logger *logger, where_t where)
{
	if (expected.len != sizeof_symkey(actual)) {
		llog_pexpect(logger, where, "%s: %s: expected length %zd but got %zd",
			     desc, verifying, expected.len, sizeof_symkey(actual));
		return false;
	}
	chunk_t chunk = chunk_from_symkey(desc, actual, logger);
	bool ok = verify_hunk(desc, verifying, expected, chunk, logger, where);
	free_chunk_content(&chunk);
	return ok;
}

/*
 * Turn the raw key into SymKey.
 */
PK11SymKey *decode_to_key(const struct encrypt_desc *encrypt_desc,
			  const char *encoded_key,
			  struct logger *logger, where_t where)
{
	chunk_t raw_key = decode_to_chunk("raw_key", encoded_key, logger, where);
	PK11SymKey *symkey = encrypt_key_from_hunk("symkey", encrypt_desc, raw_key, logger);
	free_chunk_content(&raw_key);
	return symkey;
}