File: sm2-kep.c

package info (click to toggle)
optee-os 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,560 kB
  • sloc: ansic: 441,914; asm: 12,903; python: 3,719; makefile: 1,676; sh: 238
file content (499 lines) | stat: -rw-r--r-- 12,041 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
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
497
498
499
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2020-21 Huawei Technologies Co., Ltd
 */

#include <crypto/crypto.h>
#include <crypto/sm2-kdf.h>
#include <mbedtls/bignum.h>
#include <mbedtls/ecp.h>
#include <string_ext.h>
#include <tee_api_types.h>
#include <utee_defines.h>

#include "mbed_helpers.h"

/* SM2 uses 256 bit unsigned integers in big endian format */
#define SM2_INT_SIZE_BYTES 32

/* The public x and y values extracted from a public or private ECC key */
struct key_xy {
	mbedtls_mpi *x;
	mbedtls_mpi *y;
};

/*
 * Compute a hash of a user's identity and public key
 * For user A: ZA = SM3(ENTLA || IDA || a || b || xG || yG || xA || yA)
 */
static TEE_Result sm2_kep_compute_Z(const mbedtls_ecp_group *grp, uint8_t *Z,
				    size_t Zlen, const uint8_t *id,
				    size_t idlen, struct key_xy *key)
{
	TEE_Result res = TEE_ERROR_GENERIC;
	uint8_t ENTLEN[2] = { };
	uint8_t buf[SM2_INT_SIZE_BYTES] = { };
	void *ctx = NULL;
	int mres = 0;

	if (Zlen < TEE_SM3_HASH_SIZE)
		return TEE_ERROR_SHORT_BUFFER;

	/*
	 * ENTLEN is the length in bits if the user's distinguished identifier
	 * encoded over 16 bits in big endian format.
	 */
	ENTLEN[0] = (idlen * 8) >> 8;
	ENTLEN[1] = idlen * 8;

	res = crypto_hash_alloc_ctx(&ctx, TEE_ALG_SM3);
	if (res)
		goto out;

	res = crypto_hash_init(ctx);
	if (res)
		goto out;

	res = crypto_hash_update(ctx, ENTLEN, sizeof(ENTLEN));
	if (res)
		goto out;

	res = crypto_hash_update(ctx, id, idlen);
	if (res)
		goto out;

	mres = mbedtls_mpi_write_binary(&grp->A, buf, SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	mres = mbedtls_mpi_write_binary(&grp->B, buf, SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	mres = mbedtls_mpi_write_binary(&grp->G.X, buf, SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	mres = mbedtls_mpi_write_binary(&grp->G.Y, buf, SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	mres = mbedtls_mpi_write_binary(key->x, buf, SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	mres = mbedtls_mpi_write_binary(key->y, buf, SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	res = crypto_hash_final(ctx, Z, TEE_SM3_HASH_SIZE);
out:
	crypto_hash_free_ctx(ctx);
	return res;
}

/*
 * Compute a verification value, to be checked against the value sent by the
 * peer.
 * On the initiator's side:
 *   S1 = SM3(0x02 || yU || SM3(xU || ZA || ZB || x1 || y1 || x2 || y2))
 * On the responder's side:
 *   S2 = SM3(0x03 || yV || SM3(xV || ZA || ZB || x1 || y1 || x2 || y2))
 */
static TEE_Result sm2_kep_compute_S(uint8_t *S, size_t S_len, uint8_t flag,
				    mbedtls_ecp_point *UV, const uint8_t *ZAZB,
				    size_t ZAZB_len,
				    struct key_xy *initiator_eph_key,
				    struct key_xy *responder_eph_key)
{
	uint8_t hash[TEE_SM3_HASH_SIZE] = { };
	TEE_Result res = TEE_ERROR_GENERIC;
	uint8_t buf[SM2_INT_SIZE_BYTES];
	void *ctx = NULL;
	int mres = 0;

	if (S_len < TEE_SM3_HASH_SIZE)
		return TEE_ERROR_SHORT_BUFFER;

	res = crypto_hash_alloc_ctx(&ctx, TEE_ALG_SM3);
	if (res)
		goto out;

	/* Compute the inner hash */

	res = crypto_hash_init(ctx);
	if (res)
		goto out;

	/* xU or xV */
	mres = mbedtls_mpi_write_binary(&UV->X, buf, SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	/* ZA || ZB */
	res = crypto_hash_update(ctx, ZAZB, ZAZB_len);
	if (res)
		goto out;

	/* x1 */
	mres = mbedtls_mpi_write_binary(initiator_eph_key->x, buf,
					SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	/* y1 */
	mres = mbedtls_mpi_write_binary(initiator_eph_key->y, buf,
					SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	/* x2 */
	mres = mbedtls_mpi_write_binary(responder_eph_key->x, buf,
					SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	/* y2 */
	mres = mbedtls_mpi_write_binary(responder_eph_key->y, buf,
					SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	res = crypto_hash_final(ctx, hash, sizeof(hash));
	if (res)
		goto out;

	/* Now compute S */

	res = crypto_hash_init(ctx);
	if (res)
		goto out;

	/* 0x02 or 0x03  */
	res = crypto_hash_update(ctx, &flag, sizeof(flag));
	if (res)
		goto out;

	/* yU or yV */
	mres = mbedtls_mpi_write_binary(&UV->Y, buf, SM2_INT_SIZE_BYTES);
	if (mres) {
		res = TEE_ERROR_GENERIC;
		goto out;
	}
	res = crypto_hash_update(ctx, buf, sizeof(buf));
	if (res)
		goto out;

	/* Inner SM3(...) */
	res = crypto_hash_update(ctx, hash, sizeof(hash));
	if (res)
		goto out;

	res = crypto_hash_final(ctx, S, TEE_SM3_HASH_SIZE);

out:
	crypto_hash_free_ctx(ctx);
	return res;

}

static void extract_xy_from_keypair(struct key_xy *xy,
				    const struct ecc_keypair *pair)
{
	xy->x = (mbedtls_mpi *)pair->x;
	xy->y = (mbedtls_mpi *)pair->y;
	/* Other fields are not used */
}

static void extract_xy_from_public_key(struct key_xy *xy,
				       const struct ecc_public_key *from)
{
	xy->x = (mbedtls_mpi *)from->x;
	xy->y = (mbedtls_mpi *)from->y;
}

/*
 * GM/T 0003.1‒2012 Part 3 Section 6.1
 * Key exchange protocol
 */
TEE_Result crypto_acipher_sm2_kep_derive(struct ecc_keypair *my_key,
					 struct ecc_keypair *my_eph_key,
					 struct ecc_public_key *peer_key,
					 struct ecc_public_key *peer_eph_key,
					 struct sm2_kep_parms *p)
{
	/*
	 * Variable names and documented steps reflect the initator side (user A
	 * in the spec), but the other side is quite similar hence only one
	 * function.
	 */
	uint8_t xUyUZAZB[2 * SM2_INT_SIZE_BYTES + 2 * TEE_SM3_HASH_SIZE] = { };
	struct key_xy initiator_eph_key = { };
	struct key_xy responder_eph_key = { };
	struct key_xy initiator_key = { };
	struct key_xy responder_key = { };
	TEE_Result res = TEE_ERROR_BAD_STATE;
	uint8_t tmp[SM2_INT_SIZE_BYTES] = { };
	mbedtls_ecp_group grp = { };
	mbedtls_ecp_point PB = { };
	mbedtls_ecp_point RB = { };
	mbedtls_ecp_point U = { };
	mbedtls_mpi x1bar = { };
	mbedtls_mpi x2bar = { };
	mbedtls_mpi tA = { };
	mbedtls_mpi h = { };
	mbedtls_mpi htA = { };
	mbedtls_mpi one = { };
	int mres = 0;

	if (p->is_initiator) {
		extract_xy_from_keypair(&initiator_eph_key, my_eph_key);
		extract_xy_from_public_key(&responder_eph_key, peer_eph_key);
		extract_xy_from_keypair(&initiator_key, my_key);
		extract_xy_from_public_key(&responder_key, peer_key);
	} else {
		extract_xy_from_public_key(&initiator_eph_key, peer_eph_key);
		extract_xy_from_keypair(&responder_eph_key, my_eph_key);
		extract_xy_from_public_key(&initiator_key, peer_key);
		extract_xy_from_keypair(&responder_key, my_key);
	}

	mbedtls_mpi_init(&x1bar);
	mbedtls_mpi_init(&x2bar);
	mbedtls_mpi_init(&tA);
	mbedtls_mpi_init(&h);
	mbedtls_mpi_init(&htA);
	mbedtls_mpi_init(&one);

	mbedtls_ecp_point_init(&PB);
	mbedtls_ecp_point_init(&RB);
	mbedtls_ecp_point_init(&U);

	mbedtls_ecp_group_init(&grp);
	mres = mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SM2);
	if (mres)
		goto out;

	/*
	 * Steps A1-A3 are supposedly done already (generate ephemeral key, send
	 * it to peer).
	 * Step A4: (x1, y1) = RA; x1bar = 2^w + (x1 & (2^w - 1))
	 */

	mres = mbedtls_mpi_write_binary((mbedtls_mpi *)my_eph_key->x, tmp,
					SM2_INT_SIZE_BYTES);
	if (mres)
		goto out;
	tmp[SM2_INT_SIZE_BYTES / 2] |= 0x80;
	mres = mbedtls_mpi_read_binary(&x1bar,  tmp + SM2_INT_SIZE_BYTES / 2,
				       SM2_INT_SIZE_BYTES / 2);
	if (mres)
		goto out;

	/* Step A5: tA = (dA + x1bar * rA) mod n */

	mres = mbedtls_mpi_mul_mpi(&tA, &x1bar, (mbedtls_mpi *)my_eph_key->d);
	if (mres)
		goto out;
	mres = mbedtls_mpi_mod_mpi(&tA, &tA, &grp.N);
	if (mres)
		goto out;
	mres = mbedtls_mpi_add_mpi(&tA, &tA, (mbedtls_mpi *)my_key->d);
	if (mres)
		goto out;
	mres = mbedtls_mpi_mod_mpi(&tA, &tA, &grp.N);
	if (mres)
		goto out;

	/* Step A6: verify whether RB verifies the curve equation */

	mbedtls_mpi_copy(&RB.X, (mbedtls_mpi *)peer_eph_key->x);
	mbedtls_mpi_copy(&RB.Y, (mbedtls_mpi *)peer_eph_key->y);
	mbedtls_mpi_lset(&RB.Z, 1);
	mres = mbedtls_ecp_check_pubkey(&grp, &RB);
	if (mres)
		goto out;

	/* Step A6 (continued): (x2, y2) = RB; x2bar = 2^w + (x2 & (2^w - 1)) */

	mres = mbedtls_mpi_write_binary((mbedtls_mpi *)peer_eph_key->x, tmp,
					SM2_INT_SIZE_BYTES);
	if (mres)
		goto out;
	tmp[SM2_INT_SIZE_BYTES / 2] |= 0x80;
	mres = mbedtls_mpi_read_binary(&x2bar,  tmp + SM2_INT_SIZE_BYTES / 2,
				SM2_INT_SIZE_BYTES / 2);
	if (mres)
		goto out;

	/* Step A7: compute U = [h.tA](PB + [x2bar]RB) and check for infinity */

	mres = mbedtls_mpi_copy(&PB.X, (mbedtls_mpi *)peer_key->x);
	if (mres)
		goto out;
	mres = mbedtls_mpi_copy(&PB.Y, (mbedtls_mpi *)peer_key->y);
	if (mres)
		goto out;
	mres = mbedtls_mpi_lset(&PB.Z, 1);
	if (mres)
		goto out;
	mres = mbedtls_mpi_lset(&one, 1);
	if (mres)
		goto out;

	mres = mbedtls_ecp_muladd(&grp, &U, &one, &PB, &x2bar, &RB);
	if (mres)
		goto out;

	/* Note: the cofactor for SM2 is 1 so [h.tA] == tA */
	mres = mbedtls_ecp_mul(&grp, &U, &tA, &U, mbd_rand, NULL);
	if (mres)
		goto out;

	/*
	 * "Point is zero" is same as "point is at infinity". Returns 1 if
	 * point is zero, < 0 on error and 0 if point is non-zero.
	 */
	mres = mbedtls_ecp_is_zero(&U);
	if (mres)
		goto out;

	/* Step A8: compute KA = KDF(xU || yU || ZA || ZB, klen) */

	/* xU */
	mres = mbedtls_mpi_write_binary(&U.X, xUyUZAZB, SM2_INT_SIZE_BYTES);
	if (mres)
		goto out;

	/* yU */
	mres = mbedtls_mpi_write_binary(&U.Y, xUyUZAZB + SM2_INT_SIZE_BYTES,
					SM2_INT_SIZE_BYTES);
	if (mres)
		goto out;

	/* ZA */
	res = sm2_kep_compute_Z(&grp, xUyUZAZB + 2 * SM2_INT_SIZE_BYTES,
				TEE_SM3_HASH_SIZE, p->initiator_id,
				p->initiator_id_len, &initiator_key);
	if (res)
		goto out;

	/* ZB */
	res = sm2_kep_compute_Z(&grp, xUyUZAZB + 2 * SM2_INT_SIZE_BYTES +
					TEE_SM3_HASH_SIZE,
				TEE_SM3_HASH_SIZE, p->responder_id,
				p->responder_id_len, &responder_key);
	if (res)
		goto out;

	res = sm2_kdf(xUyUZAZB, sizeof(xUyUZAZB), p->out, p->out_len);
	if (res)
		goto out;

	/* Step A9: compute S1 and check S1 == SB */

	if (p->conf_in) {
		uint8_t S1[TEE_SM3_HASH_SIZE] = { };
		uint8_t flag = p->is_initiator ? 0x02 : 0x03;

		if (p->conf_in_len < TEE_SM3_HASH_SIZE) {
			res = TEE_ERROR_BAD_PARAMETERS;
			goto out;
		}
		res = sm2_kep_compute_S(S1, sizeof(S1), flag, &U,
					xUyUZAZB + 2 * SM2_INT_SIZE_BYTES,
					2 * SM2_INT_SIZE_BYTES,
					&initiator_eph_key, &responder_eph_key);
		if (res)
			goto out;

		if (consttime_memcmp(S1, p->conf_in, sizeof(S1))) {
			/* Verification failed */
			res = TEE_ERROR_BAD_STATE;
			goto out;
		}
	}

	/* Step A10: compute SA */

	if (p->conf_out) {
		uint8_t flag = p->is_initiator ? 0x03 : 0x02;

		if (p->conf_out_len < TEE_SM3_HASH_SIZE) {
			res = TEE_ERROR_BAD_PARAMETERS;
			goto out;
		}

		res = sm2_kep_compute_S(p->conf_out, TEE_SM3_HASH_SIZE, flag,
					&U, xUyUZAZB + 2 * SM2_INT_SIZE_BYTES,
					2 * SM2_INT_SIZE_BYTES,
					&initiator_eph_key, &responder_eph_key);
	}
out:
	mbedtls_mpi_free(&x1bar);
	mbedtls_mpi_free(&x2bar);
	mbedtls_mpi_free(&tA);
	mbedtls_mpi_free(&h);
	mbedtls_mpi_free(&htA);
	mbedtls_mpi_free(&one);
	mbedtls_ecp_point_free(&PB);
	mbedtls_ecp_point_free(&RB);
	mbedtls_ecp_point_free(&U);
	mbedtls_ecp_group_free(&grp);
	return res;
}