xref: /optee_os/ta/pkcs11/src/serializer.c (revision 4f8a354fe12895c806e678d626f85ab07297d7be)
1*4f8a354fSEtienne Carriere // SPDX-License-Identifier: BSD-2-Clause
2*4f8a354fSEtienne Carriere /*
3*4f8a354fSEtienne Carriere  * Copyright (c) 2017-2020, Linaro Limited
4*4f8a354fSEtienne Carriere  */
5*4f8a354fSEtienne Carriere 
6*4f8a354fSEtienne Carriere #include <pkcs11_ta.h>
7*4f8a354fSEtienne Carriere #include <stdbool.h>
8*4f8a354fSEtienne Carriere #include <stdint.h>
9*4f8a354fSEtienne Carriere #include <tee_internal_api.h>
10*4f8a354fSEtienne Carriere #include <trace.h>
11*4f8a354fSEtienne Carriere 
12*4f8a354fSEtienne Carriere #include "serializer.h"
13*4f8a354fSEtienne Carriere 
14*4f8a354fSEtienne Carriere /*
15*4f8a354fSEtienne Carriere  * Util routines for serializes unformatted arguments in a client memref
16*4f8a354fSEtienne Carriere  */
17*4f8a354fSEtienne Carriere void serialargs_init(struct serialargs *args, void *in, size_t size)
18*4f8a354fSEtienne Carriere {
19*4f8a354fSEtienne Carriere 	args->start = in;
20*4f8a354fSEtienne Carriere 	args->next = in;
21*4f8a354fSEtienne Carriere 	args->size = size;
22*4f8a354fSEtienne Carriere }
23*4f8a354fSEtienne Carriere 
24*4f8a354fSEtienne Carriere uint32_t serialargs_get(struct serialargs *args, void *out, size_t size)
25*4f8a354fSEtienne Carriere {
26*4f8a354fSEtienne Carriere 	if (args->next + size > args->start + args->size) {
27*4f8a354fSEtienne Carriere 		EMSG("arg too short: full %zd, remain %zd, expect %zd",
28*4f8a354fSEtienne Carriere 		     args->size, args->size - (args->next - args->start), size);
29*4f8a354fSEtienne Carriere 		return PKCS11_CKR_ARGUMENTS_BAD;
30*4f8a354fSEtienne Carriere 	}
31*4f8a354fSEtienne Carriere 
32*4f8a354fSEtienne Carriere 	TEE_MemMove(out, args->next, size);
33*4f8a354fSEtienne Carriere 
34*4f8a354fSEtienne Carriere 	args->next += size;
35*4f8a354fSEtienne Carriere 
36*4f8a354fSEtienne Carriere 	return PKCS11_CKR_OK;
37*4f8a354fSEtienne Carriere }
38*4f8a354fSEtienne Carriere 
39*4f8a354fSEtienne Carriere uint32_t serialargs_alloc_and_get(struct serialargs *args,
40*4f8a354fSEtienne Carriere 				  void **out, size_t size)
41*4f8a354fSEtienne Carriere {
42*4f8a354fSEtienne Carriere 	void *ptr = NULL;
43*4f8a354fSEtienne Carriere 
44*4f8a354fSEtienne Carriere 	if (!size) {
45*4f8a354fSEtienne Carriere 		*out = NULL;
46*4f8a354fSEtienne Carriere 		return PKCS11_CKR_OK;
47*4f8a354fSEtienne Carriere 	}
48*4f8a354fSEtienne Carriere 
49*4f8a354fSEtienne Carriere 	if (args->next + size > args->start + args->size) {
50*4f8a354fSEtienne Carriere 		EMSG("arg too short: full %zd, remain %zd, expect %zd",
51*4f8a354fSEtienne Carriere 		     args->size, args->size - (args->next - args->start), size);
52*4f8a354fSEtienne Carriere 		return PKCS11_CKR_ARGUMENTS_BAD;
53*4f8a354fSEtienne Carriere 	}
54*4f8a354fSEtienne Carriere 
55*4f8a354fSEtienne Carriere 	ptr = TEE_Malloc(size, TEE_MALLOC_FILL_ZERO);
56*4f8a354fSEtienne Carriere 	if (!ptr)
57*4f8a354fSEtienne Carriere 		return PKCS11_CKR_DEVICE_MEMORY;
58*4f8a354fSEtienne Carriere 
59*4f8a354fSEtienne Carriere 	TEE_MemMove(ptr, args->next, size);
60*4f8a354fSEtienne Carriere 
61*4f8a354fSEtienne Carriere 	args->next += size;
62*4f8a354fSEtienne Carriere 	*out = ptr;
63*4f8a354fSEtienne Carriere 
64*4f8a354fSEtienne Carriere 	return PKCS11_CKR_OK;
65*4f8a354fSEtienne Carriere }
66*4f8a354fSEtienne Carriere 
67*4f8a354fSEtienne Carriere uint32_t serialargs_get_ptr(struct serialargs *args, void **out, size_t size)
68*4f8a354fSEtienne Carriere {
69*4f8a354fSEtienne Carriere 	void *ptr = args->next;
70*4f8a354fSEtienne Carriere 
71*4f8a354fSEtienne Carriere 	if (!size) {
72*4f8a354fSEtienne Carriere 		*out = NULL;
73*4f8a354fSEtienne Carriere 		return PKCS11_CKR_OK;
74*4f8a354fSEtienne Carriere 	}
75*4f8a354fSEtienne Carriere 
76*4f8a354fSEtienne Carriere 	if (args->next + size > args->start + args->size) {
77*4f8a354fSEtienne Carriere 		EMSG("arg too short: full %zd, remain %zd, expect %zd",
78*4f8a354fSEtienne Carriere 		     args->size, args->size - (args->next - args->start), size);
79*4f8a354fSEtienne Carriere 		return PKCS11_CKR_ARGUMENTS_BAD;
80*4f8a354fSEtienne Carriere 	}
81*4f8a354fSEtienne Carriere 
82*4f8a354fSEtienne Carriere 	args->next += size;
83*4f8a354fSEtienne Carriere 	*out = ptr;
84*4f8a354fSEtienne Carriere 
85*4f8a354fSEtienne Carriere 	return PKCS11_CKR_OK;
86*4f8a354fSEtienne Carriere }
87*4f8a354fSEtienne Carriere 
88*4f8a354fSEtienne Carriere bool serialargs_remaining_bytes(struct serialargs *args)
89*4f8a354fSEtienne Carriere {
90*4f8a354fSEtienne Carriere 	return args->next < args->start + args->size;
91*4f8a354fSEtienne Carriere }
92