net/ipv4/tcp_ao.c

Source file repositories/reference/linux-study-clean/net/ipv4/tcp_ao.c

File Facts

System
Linux kernel
Corpus path
net/ipv4/tcp_ao.c
Extension
.c
Size
66649 bytes
Lines
2420
Domain
Networking Core
Bucket
Sockets, Protocols, Packet Path, And Network Policy
Inferred role
Networking Core: implementation source
Status
source implementation candidate

Why This File Exists

Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.

Dependency Surface

Detected Declarations

Annotated Snippet

struct tcp_ao_mac_ctx {
	enum tcp_ao_algo_id algo;
	union {
		struct hmac_sha1_ctx hmac_sha1;
		struct hmac_sha256_ctx hmac_sha256;
		struct {
			struct aes_cmac_key key;
			struct aes_cmac_ctx ctx;
		} aes_cmac;
	};
};

static const struct tcp_ao_algo *tcp_ao_find_algo(const char *name)
{
	for (size_t i = 0; i < ARRAY_SIZE(tcp_ao_algos); i++) {
		const struct tcp_ao_algo *algo = &tcp_ao_algos[i];

		if (!algo->name)
			continue;
		if (WARN_ON_ONCE(algo->digest_size > TCP_AO_MAX_MAC_LEN ||
				 algo->digest_size >
					 TCP_AO_MAX_TRAFFIC_KEY_LEN))
			continue;
		if (strcmp(name, algo->name) == 0)
			return algo;
	}
	return NULL;
}

static void tcp_ao_mac_init(struct tcp_ao_mac_ctx *mac_ctx,
			    enum tcp_ao_algo_id algo, const u8 *traffic_key)
{
	mac_ctx->algo = algo;
	switch (mac_ctx->algo) {
	case TCP_AO_ALGO_HMAC_SHA1:
		hmac_sha1_init_usingrawkey(&mac_ctx->hmac_sha1, traffic_key,
					   SHA1_DIGEST_SIZE);
		return;
	case TCP_AO_ALGO_HMAC_SHA256:
		hmac_sha256_init_usingrawkey(&mac_ctx->hmac_sha256, traffic_key,
					     SHA256_DIGEST_SIZE);
		return;
	case TCP_AO_ALGO_AES_128_CMAC:
		aes_cmac_preparekey(&mac_ctx->aes_cmac.key, traffic_key,
				    AES_KEYSIZE_128);
		aes_cmac_init(&mac_ctx->aes_cmac.ctx, &mac_ctx->aes_cmac.key);
		return;
	default:
		WARN_ON_ONCE(1); /* algo was validated earlier. */
	}
}

void tcp_ao_mac_update(struct tcp_ao_mac_ctx *mac_ctx, const void *data,
		       size_t data_len)
{
	switch (mac_ctx->algo) {
	case TCP_AO_ALGO_HMAC_SHA1:
		hmac_sha1_update(&mac_ctx->hmac_sha1, data, data_len);
		return;
	case TCP_AO_ALGO_HMAC_SHA256:
		hmac_sha256_update(&mac_ctx->hmac_sha256, data, data_len);
		return;
	case TCP_AO_ALGO_AES_128_CMAC:
		aes_cmac_update(&mac_ctx->aes_cmac.ctx, data, data_len);
		return;
	default:
		WARN_ON_ONCE(1); /* algo was validated earlier. */
	}
}

static void tcp_ao_mac_final(struct tcp_ao_mac_ctx *mac_ctx, u8 *out)
{
	switch (mac_ctx->algo) {
	case TCP_AO_ALGO_HMAC_SHA1:
		hmac_sha1_final(&mac_ctx->hmac_sha1, out);
		return;
	case TCP_AO_ALGO_HMAC_SHA256:
		hmac_sha256_final(&mac_ctx->hmac_sha256, out);
		return;
	case TCP_AO_ALGO_AES_128_CMAC:
		aes_cmac_final(&mac_ctx->aes_cmac.ctx, out);
		return;
	default:
		WARN_ON_ONCE(1); /* algo was validated earlier. */
	}
}

void tcp_ao_calc_traffic_key(const struct tcp_ao_key *mkt, u8 *traffic_key,
			     const void *input, unsigned int input_len)
{

Annotation

Implementation Notes