summaryrefslogtreecommitdiff
path: root/exynos-hdcp2-dplink-protocol-msg.c
blob: 75f97c8a98903f668f65c041d782f0fcaa38e625 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
 * drivers/soc/samsung/exynos-hdcp/dp_link/exynos-hdcp2-dplink-protocol-msg.c
 *
 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/module.h>

#include "exynos-hdcp2-config.h"
#include "exynos-hdcp2-protocol-msg.h"
#include "exynos-hdcp2.h"
#include "exynos-hdcp2-misc.h"
#include "exynos-hdcp2-log.h"
#include "exynos-hdcp2-dplink-protocol-msg.h"
#include "exynos-hdcp2-dplink-if.h"

/* HDCP protocol message capulate & decapulate functions for DP */
static int dp_cap_ake_init(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_cap_ake_no_stored_km(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_cap_ake_stored_km(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_decap_ake_send_cert(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_decap_ake_send_h_prime(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_decap_ake_send_pairing_info(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_cap_lc_init(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_decap_lc_send_l_prime(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_cap_ske_send_eks(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_decap_rpauth_send_recvid_list(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_cap_rpauth_send_ack(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_cap_rpauth_stream_manage(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int dp_decap_rpauth_stream_ready(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx);
static int (*proto_dp[])(uint8_t *, size_t *,
			struct hdcp_tx_ctx *,
			struct hdcp_rx_ctx *) = {
	dp_cap_ake_init,
	dp_decap_ake_send_cert,
	dp_cap_ake_no_stored_km,
	dp_cap_ake_stored_km,
	NULL, /* not defined in HDCP2.2 DP spec */
	dp_decap_ake_send_h_prime,
	dp_decap_ake_send_pairing_info,
	dp_cap_lc_init,
	dp_decap_lc_send_l_prime,
	dp_cap_ske_send_eks,
	dp_decap_rpauth_send_recvid_list,
	NULL, /* not defined in HDCP2.2 DP spec */
	NULL, /* not defined in HDCP2.2 DP spec */
	dp_cap_rpauth_send_ack,
	dp_cap_rpauth_stream_manage,
	dp_decap_rpauth_stream_ready,
	NULL, /* to do */
	NULL, /* not defined in HDCP2.2 DP spec */
	NULL, /* not defined in HDCP2.2 DP spec */
};

static int dp_rp_verify_stream_ready(uint8_t *m_prime)
{
	int ret;

	ret = teei_verify_m_prime(m_prime, NULL, 0);
	if (ret) {
		hdcp_err("teei_verify_m_prime() is failed with %x\n", ret);
		return ERR_RP_INVALID_M_PRIME;
	}

	return 0;
}

static int dp_rp_gen_stream_manage(struct dp_stream_info *strm)
{
	int ret;

	ret = teei_gen_stream_manage(strm->stream_num,
				strm->streamid,
				strm->seq_num_m,
				strm->k,
				strm->streamid_type);
	if (ret) {
		hdcp_err("teei_gen_stream_manage() is failed with %x\n", ret);
		return ERR_RP_GEN_STREAM_MG;
	}

	return 0;
}

static int dp_rp_set_rcvid_list(struct hdcp_rpauth_info *rp)
{
	int ret;

	ret = teei_set_rcvlist_info(rp->rx_info,
				rp->seq_num_v,
				rp->v_prime,
				rp->u_rcvid.lst,
				rp->v,
				&rp->valid);
	if (ret) {
		hdcp_err("teei_set_rcvid_list() is failed with %x\n", ret);
		return ERR_RP_VERIFY_RCVLIST;
	}

	return 0;
}

static int dp_ake_generate_rtx(uint8_t *rtx, size_t rtx_len,
				uint8_t *caps, uint32_t caps_len)
{
	int ret;

	ret = teei_gen_rtx(HDCP_LINK_TYPE_DP, rtx, rtx_len, caps, caps_len);
	if (ret) {
		hdcp_err("generate_none_secret_key() is failed with %x\n", ret);
		return ERR_GENERATE_NON_SECKEY;
	}

	return 0;
}

static int dp_cap_ake_init(uint8_t *m, size_t *m_len, struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx)
{
	size_t rtx_len;
	uint32_t caps_len;
	int ret;

	/* Buffer Check */
	if ((m == NULL) || (m_len == NULL) || (tx_ctx == NULL))
		return ERR_WRONG_BUFFER;

	rtx_len = HDCP_AKE_RTX_BYTE_LEN;
	caps_len = HDCP_CAPS_BYTE_LEN;

	/* Generate rtx */
	ret = dp_ake_generate_rtx(tx_ctx->rtx, rtx_len,
				tx_ctx->caps, caps_len);
	if (ret) {
		hdcp_err("failed to generate rtx. ret(%d)\n", ret);
		return ERR_GENERATE_RTX;
	}

	/* Make Message */
	memcpy(&m[0], tx_ctx->rtx, rtx_len);
	memcpy(&m[rtx_len], tx_ctx->caps, HDCP_CAPS_BYTE_LEN);
	*m_len = rtx_len + HDCP_CAPS_BYTE_LEN;

	return 0;
}

static int dp_cap_ake_no_stored_km(uint8_t *m, size_t *m_len,
				struct hdcp_tx_ctx *tx_ctx,
				struct hdcp_rx_ctx *rx_ctx)
{
	uint8_t enc_mkey[HDCP_AKE_ENCKEY_BYTE_LEN];
	int ret;

	if ((m == NULL) || (m_len == NULL) ||
		(tx_ctx == NULL) || (rx_ctx == NULL))
		return ERR_WRONG_BUFFER;

	/* Generate/Encrypt master key */
	ret = ake_generate_masterkey(HDCP_LINK_TYPE_DP,
					enc_mkey,
					sizeof(enc_mkey));
	/* Generate/Encrypt master key */
	if (ret)
		return ret;

	/* Make Message */
	memcpy(m, enc_mkey, HDCP_AKE_ENCKEY_BYTE_LEN);
	*m_len = HDCP_AKE_ENCKEY_BYTE_LEN;

	return 0;
}

static int dp_cap_ake_stored_km(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx)
{
	int found_km;
	int ret;

	if ((m == NULL) || (m_len == NULL) ||
		(tx_ctx == NULL) || (rx_ctx == NULL))
		return ERR_WRONG_BUFFER;

	ret = ake_find_masterkey(&found_km,
				tx_ctx->ekh_mkey, HDCP_AKE_EKH_MKEY_BYTE_LEN,
				tx_ctx->m, HDCP_AKE_M_BYTE_LEN);
	if (ret || !found_km) {
		hdcp_err("find_masterkey() is failed with ret(0x%x). found(%d)\n", ret, found_km);
		return -1;
	}

	/* Make Message */
	memcpy(m, tx_ctx->ekh_mkey, HDCP_AKE_EKH_MKEY_BYTE_LEN);
	memcpy(&m[HDCP_AKE_EKH_MKEY_BYTE_LEN], tx_ctx->m, HDCP_AKE_M_BYTE_LEN);
	*m_len = HDCP_AKE_EKH_MKEY_BYTE_LEN + HDCP_AKE_M_BYTE_LEN;

	return 0;
}

static int dp_decap_ake_send_cert(uint8_t *m, size_t *m_len, struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx)
{
	int ret;

	ret = dp_check_received_msg(m, *m_len, HDCP_RX_CERT_LEN +
					       HDCP_RRX_BYTE_LEN +
					       HDCP_CAPS_BYTE_LEN);
	if (ret)
		return ret;

	ret = ake_verify_cert(m, HDCP_RX_CERT_LEN,
				&m[HDCP_RX_CERT_LEN], HDCP_RRX_BYTE_LEN,
				&m[HDCP_RX_CERT_LEN + HDCP_RRX_BYTE_LEN], HDCP_CAPS_BYTE_LEN);
	if (ret) {
		hdcp_err("teei_verify_cert is failed with %x\n", ret);
		return ERR_VERIFY_CERT;
	}

	return 0;
}

static int dp_decap_ake_send_h_prime(uint8_t *m, size_t *m_len, struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx)
{
	int ret;

	/* compare H == H' */
	ret = ake_compare_hmac(m, HDCP_HMAC_SHA256_LEN);
	if (ret) {
		hdcp_err("decap_ake_send_h_prime is failed. ret(%d)\n", ret);
		return ret;
	}

	return 0;
}

static int dp_decap_ake_send_pairing_info(uint8_t *m, size_t *m_len,
				struct hdcp_tx_ctx *tx_ctx,
				struct hdcp_rx_ctx *rx_ctx)
{
	int ret;

	memcpy(tx_ctx->ekh_mkey, &m[0], HDCP_AKE_EKH_MKEY_BYTE_LEN);

	/* Store the Key */
	ret = ake_store_master_key(tx_ctx->ekh_mkey, HDCP_AKE_EKH_MKEY_BYTE_LEN);
	if (ret) {
		hdcp_err("failed to store master key. ret(%d)\n", ret);
		return ret;
	}

	return 0;
}

int dp_cap_lc_init(uint8_t *m, size_t *m_len, struct hdcp_tx_ctx *tx_ctx,
		struct hdcp_rx_ctx *rx_ctx)
{
	int ret;

	if ((m == NULL) || (m_len == NULL) || (tx_ctx == NULL) ||
	    (rx_ctx == NULL))
		return ERR_WRONG_BUFFER;

	/* Generate rn */
	ret = lc_generate_rn(tx_ctx->rn, HDCP_RTX_BYTE_LEN);
	if (ret) {
		hdcp_err("failed to generate rtx. ret(%d)\n", ret);
		return ret;
	}

	/* Make Message */
	memcpy(m, tx_ctx->rn, HDCP_RTX_BYTE_LEN);
	*m_len = HDCP_RTX_BYTE_LEN;

	return 0;
}

static int dp_decap_lc_send_l_prime(uint8_t *m, size_t *m_len, struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx)
{
	uint8_t *rx_hmac;
	size_t len;
	int ret;

	len = HDCP_HMAC_SHA256_LEN;

	/* No_precomputation: compare hmac
	   precomputation: compare the most significant 128bits of L & L' */
	rx_hmac = m;
	ret = lc_compare_hmac(rx_hmac, len);
	if (ret)
		return ret;

	return 0;
}

static int dp_cap_ske_send_eks(uint8_t *m, size_t *m_len,
			struct hdcp_tx_ctx *tx_ctx,
			struct hdcp_rx_ctx *rx_ctx)
{
	int ret;
	uint8_t enc_skey[HDCP_AKE_MKEY_BYTE_LEN];

	if ((m == NULL) || (m_len == NULL) || (tx_ctx == NULL) ||
	    (rx_ctx == NULL))
		return ERR_WRONG_BUFFER;

	/* Generate riv */
	if (!tx_ctx->share_skey) {
		ret = ske_generate_riv(tx_ctx->riv);
		if (ret)
			return ERR_GENERATE_RIV;
	}

	/* Generate encrypted Session Key */
	ret = ske_generate_sessionkey(HDCP_LINK_TYPE_DP,
					enc_skey,
					tx_ctx->share_skey);
	if (ret)
		return ret;

	/* Make Message */
	memcpy(m, enc_skey, HDCP_AKE_MKEY_BYTE_LEN);
	memcpy(&m[HDCP_AKE_MKEY_BYTE_LEN], tx_ctx->riv, HDCP_RTX_BYTE_LEN);
	*m_len = HDCP_AKE_MKEY_BYTE_LEN + HDCP_RTX_BYTE_LEN;

	return 0;
}

static int dp_decap_rpauth_send_recvid_list(uint8_t *m,
					size_t *m_len,
					struct hdcp_tx_ctx *tx_ctx,
					struct hdcp_rx_ctx *rx_ctx)
{
	int ret;
	struct hdcp_rpauth_info *rp;

	rp = &tx_ctx->rpauth_info;

	memcpy(rp->rx_info, &m[0], HDCP_RP_RX_INFO_LEN);
	memcpy(rp->seq_num_v, &m[HDCP_RP_RX_INFO_LEN], HDCP_RP_SEQ_NUM_V_LEN);
	memcpy(rp->v_prime, &m[HDCP_RP_RX_INFO_LEN + HDCP_RP_SEQ_NUM_V_LEN],
		HDCP_RP_HMAC_V_LEN / 2);
	memcpy(rp->u_rcvid.lst,
		&m[HDCP_RP_RX_INFO_LEN + HDCP_RP_SEQ_NUM_V_LEN + (HDCP_RP_HMAC_V_LEN / 2)],
		HDCP_RP_RCVID_LIST_LEN);

	/* set receiver id list */
	ret = dp_rp_set_rcvid_list(rp);
	if (ret) {
		hdcp_err("failed to set receiver id list. ret(%d)\n", ret);
		return ret;
	}

	return 0;
}

static int dp_cap_rpauth_send_ack(uint8_t *m,
				size_t *m_len,
				struct hdcp_tx_ctx *tx_ctx,
				struct hdcp_rx_ctx *rx_ctx)
{
        /* Make Message */
	if (tx_ctx->rpauth_info.valid == 0) {
		memcpy(m, tx_ctx->rpauth_info.v, HDCP_RP_HMAC_V_LEN / 2);
		*m_len = HDCP_RP_HMAC_V_LEN / 2;
	} else {
		*m_len = 0;
		return ERR_SEND_ACK;
	}

	return 0;
}

static int dp_cap_rpauth_stream_manage(uint8_t *m,
				size_t *m_len,
				struct hdcp_tx_ctx *tx_ctx,
				struct hdcp_rx_ctx *rx_ctx)
{
	struct dp_stream_info *strm;
	int ret;

	strm = &tx_ctx->strm.dp;

	hdcp_dplink_get_stream_info(&strm->stream_num, strm->streamid);

	/* set receiver id list */
	ret = dp_rp_gen_stream_manage(strm);
	if (ret) {
		hdcp_err("failed to gen stream manage. ret(%d)\n", ret);
		return ret;
	}

	/* Make Message */
	memcpy(m, strm->seq_num_m, HDCP_RP_SEQ_NUM_M_LEN);
	memcpy(&m[HDCP_RP_SEQ_NUM_M_LEN], strm->k, HDCP_RP_K_LEN);
	memcpy(&m[HDCP_RP_SEQ_NUM_M_LEN + HDCP_RP_K_LEN],
		strm->streamid_type, HDCP_RP_STREAMID_TYPE_LEN * strm->stream_num);

	*m_len = HDCP_RP_SEQ_NUM_M_LEN + \
		HDCP_RP_K_LEN + \
		(HDCP_RP_STREAMID_TYPE_LEN * strm->stream_num);

	return 0;
}

static int dp_decap_rpauth_stream_ready(uint8_t *m,
					size_t *m_len,
					struct hdcp_tx_ctx *tx_ctx,
					struct hdcp_rx_ctx *rx_ctx)
{
	uint8_t *m_prime;
	int ret;

	m_prime = m;
	ret = dp_rp_verify_stream_ready(m_prime);
	if (ret)
		hdcp_err("dp_rp_verify_stream_ready ret(%d)\n", ret);

	return 0;
}

int dp_cap_protocol_msg(uint8_t msg_id,
		uint8_t *msg,
		size_t *msg_len,
		struct hdcp_tx_ctx *tx_ctx,
		struct hdcp_rx_ctx *rx_ctx)
{
	int ret = 0;
	int (**proto_func)(uint8_t *, size_t *,
			struct hdcp_tx_ctx *,
			struct hdcp_rx_ctx *);
	proto_func = proto_dp;

	if (msg_id > 1) {
		ret = proto_func[msg_id - 2](msg, msg_len, tx_ctx, rx_ctx);
		return ret;
	}
	else
		return -1;
}

int dp_decap_protocol_msg(uint8_t msg_id,
		uint8_t *msg,
		size_t msg_len,
		struct hdcp_tx_ctx *tx_ctx,
		struct hdcp_rx_ctx *rx_ctx)
{
	int ret = 0;
	int (**proto_func)(uint8_t *, size_t *,
			struct hdcp_tx_ctx *,
			struct hdcp_rx_ctx *);
	proto_func = proto_dp;

	if (msg_id > 1) {
		ret = proto_func[msg_id - 2](msg, &msg_len, tx_ctx, rx_ctx);
		return ret;
	}

	else
		return -1;
}

MODULE_LICENSE("GPL");