File: sad_openssl.c

package info (click to toggle)
linuxptp 4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,664 kB
  • sloc: ansic: 28,366; sh: 178; makefile: 104
file content (200 lines) | stat: -rw-r--r-- 4,502 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
 * @file sad_openssl.c
 * @brief Security Association Database openssl functions
 * @note Copyright (C) 2024 IBM Corporation, Clay Kaiser <Clay.Kaiser@ibm.com>
 * @note SPDX-License-Identifier: GPL-2.0+
 */
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>

#include "print.h"
#include "sad.h"
#include "sad_private.h"

struct mac_data {
	EVP_MAC *algorithm;
	EVP_MAC_CTX *context;
};

struct mac_data *sad_init_mac(integrity_alg_type algorithm,
			      const unsigned char *key, size_t key_len)
{
	int err;
	size_t length;

	const char *name;
	char *param, *algo;
	EVP_MAC *mac_algorithm;
	EVP_MAC_CTX *context;
	OSSL_PARAM params[2];
	EVP_CIPHER *cipher;
	struct mac_data *mac_data;

	/* verify key length */
	if (key_len == 0) {
		pr_err("BUG: key_len is zero");
		return NULL;
	}

	/* retrieve mac algorithm */
	switch (algorithm) {
	case HMAC_SHA256_128:
	case HMAC_SHA256:
		name = "HMAC";
		param = "digest";
		algo = "SHA-256";
		break;
	case CMAC_AES128:
		name = "CMAC";
		param = "cipher";
		algo = "AES-128-CBC";
		break;
	case CMAC_AES256:
		name = "CMAC";
		param = "cipher";
		algo = "AES-256-CBC";
		break;
	default:
		pr_err("BUG: unknown algorithm");
		return NULL;
	}
	/* verify key length matches for cmac only */
	switch (algorithm) {
	case CMAC_AES128:
	case CMAC_AES256:
		cipher = EVP_CIPHER_fetch(NULL, algo, NULL);
		length = EVP_CIPHER_get_key_length(cipher);
		EVP_CIPHER_free(cipher);
		if (key_len != length) {
			pr_err("BUG: cipher key_len does not match");
			return NULL;
		}
		break;
	default:
		break;
	}
	mac_algorithm = EVP_MAC_fetch(NULL, name, NULL);
	if (!mac_algorithm) {
		pr_err("EVP_MAC_fetch() failed");
		return NULL;
	}
	context = EVP_MAC_CTX_new(mac_algorithm);
	if (!context) {
		pr_err("EVP_MAC_CTX_new() failed");
		EVP_MAC_free(mac_algorithm);
		return NULL;
	}
	params[0] = OSSL_PARAM_construct_utf8_string(param, algo, 0);
	params[1] = OSSL_PARAM_construct_end();
	err = EVP_MAC_CTX_set_params(context, params);
	if (err == 0) {
		pr_err("EVP_MAC_CTX_set_params() failed");
		EVP_MAC_free(mac_algorithm);
		EVP_MAC_CTX_free(context);
		return NULL;
	}

	/* initialize context */
	err = EVP_MAC_init(context, key, key_len, NULL);
	if (err == 0) {
		pr_err("EVP_MAC_init() failed");
		EVP_MAC_free(mac_algorithm);
		EVP_MAC_CTX_free(context);
		return NULL;
	}
	/* initialize mac_data */
	mac_data = calloc(1, sizeof(*mac_data));
	if (!mac_data) {
		EVP_MAC_free(mac_algorithm);
		EVP_MAC_CTX_free(context);
		return NULL;
	}
	mac_data->algorithm = mac_algorithm;
	mac_data->context = context;

	return mac_data;
}

void sad_deinit_mac(struct mac_data *data)
{
	EVP_MAC_free(data->algorithm);
	EVP_MAC_CTX_free(data->context);
	free(data);
}

static inline int sad_update_mac(struct mac_data *mac_data,
				 const void *data, size_t data_len,
				 unsigned char *mac, size_t mac_len)
{
	int err;
	size_t digest_len;

	/* confirm mac length is within buffer size */
	if (mac_len > MAX_DIGEST_LENGTH) {
		pr_err("BUG: mac_len larger than buffer");
		return 0;
	}

	/* update data and retrieve mac */
	err = EVP_MAC_init(mac_data->context, NULL, 0, NULL);
	if (err == 0) {
		pr_err("EVP_MAC_init() failed");
		return 0;
	}
	err = EVP_MAC_update(mac_data->context, data, data_len);
	if (err == 0) {
		pr_err("EVP_MAC_update() failed");
		return 0;
	}
	err = EVP_MAC_final(mac_data->context, mac,
			    &digest_len, MAX_DIGEST_LENGTH);
	if (err == 0) {
		pr_err("EVP_MAC_final() failed");
		return 0;
	}

	/* confirm mac length is within library support */
	if (mac_len > digest_len) {
		pr_err("BUG: mac_len larger than library support");
		return 0;
	}

	return 1;
}

int sad_hash(struct mac_data *mac_data,
	     const void *data, size_t data_len,
	     unsigned char *mac, size_t mac_len)
{
	unsigned char digest_buffer[MAX_DIGEST_LENGTH];

	/* update data and retrieve mac */
	if (!sad_update_mac(mac_data, data, data_len,
			    digest_buffer, mac_len)) {
		return 0;
	}

	/* move mac to desired location */
	memcpy(mac, digest_buffer, mac_len);

	return mac_len;
}

int sad_verify(struct mac_data *mac_data,
	       const void *data, size_t data_len,
	       unsigned char *mac, size_t mac_len)
{
	unsigned char digest_buffer[MAX_DIGEST_LENGTH];

	/* update data and retrieve mac */
	if (!sad_update_mac(mac_data, data, data_len,
			    digest_buffer, mac_len)) {
		return -1;
	}

	/* compare calculated with received */
	return CRYPTO_memcmp(digest_buffer, mac, mac_len);
}