1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2018-2020, Linaro Limited
4  */
5 
6 #include <assert.h>
7 #include <compiler.h>
8 #include <tee_internal_api.h>
9 #include <trace.h>
10 #include <util.h>
11 
12 #include "pkcs11_helpers.h"
13 #include "pkcs11_token.h"
14 #include "processing.h"
15 #include "serializer.h"
16 
tee_init_ctr_operation(struct active_processing * processing,void * proc_params,size_t params_size)17 enum pkcs11_rc tee_init_ctr_operation(struct active_processing *processing,
18 				      void *proc_params, size_t params_size)
19 {
20 	struct serialargs args = { };
21 	enum pkcs11_rc rc = PKCS11_CKR_OK;
22 	/* CTR parameters */
23 	uint32_t incr_counter = 0;
24 	void *counter_bits = NULL;
25 
26 	if (!proc_params)
27 		return PKCS11_CKR_ARGUMENTS_BAD;
28 
29 	serialargs_init(&args, proc_params, params_size);
30 
31 	rc = serialargs_get(&args, &incr_counter, sizeof(uint32_t));
32 	if (rc)
33 		return rc;
34 
35 	rc = serialargs_get_ptr(&args, &counter_bits, 16);
36 	if (rc)
37 		return rc;
38 
39 	if (serialargs_remaining_bytes(&args))
40 		return PKCS11_CKR_ARGUMENTS_BAD;
41 
42 	if (incr_counter != 1) {
43 		DMSG("Supports only 1 bit increment counter: %"PRIu32,
44 		     incr_counter);
45 
46 		return PKCS11_CKR_MECHANISM_PARAM_INVALID;
47 	}
48 
49 	TEE_CipherInit(processing->tee_op_handle, counter_bits, 16);
50 
51 	return PKCS11_CKR_OK;
52 }
53