xref: /optee_os/core/kernel/tee_misc.c (revision 54e0470881add603f773fa48b8d26a78571815bb)
1*54e04708SPascal Brand /*
2*54e04708SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
3*54e04708SPascal Brand  * All rights reserved.
4*54e04708SPascal Brand  *
5*54e04708SPascal Brand  * Redistribution and use in source and binary forms, with or without
6*54e04708SPascal Brand  * modification, are permitted provided that the following conditions are met:
7*54e04708SPascal Brand  *
8*54e04708SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
9*54e04708SPascal Brand  * this list of conditions and the following disclaimer.
10*54e04708SPascal Brand  *
11*54e04708SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
12*54e04708SPascal Brand  * this list of conditions and the following disclaimer in the documentation
13*54e04708SPascal Brand  * and/or other materials provided with the distribution.
14*54e04708SPascal Brand  *
15*54e04708SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16*54e04708SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*54e04708SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*54e04708SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19*54e04708SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*54e04708SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*54e04708SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*54e04708SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*54e04708SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*54e04708SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*54e04708SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
26*54e04708SPascal Brand  */
27*54e04708SPascal Brand 
28*54e04708SPascal Brand #include <kernel/tee_common.h>
29*54e04708SPascal Brand #include <kernel/chip_services.h>
30*54e04708SPascal Brand #include <kernel/tee_misc.h>
31*54e04708SPascal Brand #include <mm/core_memprot.h>
32*54e04708SPascal Brand #include <mm/tee_mmu_defs.h>
33*54e04708SPascal Brand 
34*54e04708SPascal Brand 
35*54e04708SPascal Brand #include <kernel/tee_common_otp.h>
36*54e04708SPascal Brand #include <kernel/tee_core_trace.h>
37*54e04708SPascal Brand 
38*54e04708SPascal Brand static uint8_t tee_b2hs_add_base(uint8_t in)
39*54e04708SPascal Brand {
40*54e04708SPascal Brand 	if (in > 9)
41*54e04708SPascal Brand 		return in + 55;
42*54e04708SPascal Brand 	else
43*54e04708SPascal Brand 		return in + 48;
44*54e04708SPascal Brand }
45*54e04708SPascal Brand 
46*54e04708SPascal Brand static int tee_hs2b_rem_base(uint8_t in, uint8_t *out)
47*54e04708SPascal Brand {
48*54e04708SPascal Brand 	if (in < 48 || in > 70 || (in > 57 && in < 65))
49*54e04708SPascal Brand 		return -1;
50*54e04708SPascal Brand 
51*54e04708SPascal Brand 	if (in < 58)
52*54e04708SPascal Brand 		*out = in - 48;
53*54e04708SPascal Brand 	else
54*54e04708SPascal Brand 		*out = in - 55;
55*54e04708SPascal Brand 
56*54e04708SPascal Brand 	return 0;
57*54e04708SPascal Brand }
58*54e04708SPascal Brand 
59*54e04708SPascal Brand uint32_t tee_b2hs(uint8_t *b, uint8_t *hs, uint32_t blen, uint32_t hslen)
60*54e04708SPascal Brand {
61*54e04708SPascal Brand 	uint32_t i = 0;
62*54e04708SPascal Brand 
63*54e04708SPascal Brand 	if (blen * 2 + 1 > hslen)
64*54e04708SPascal Brand 		return 0;
65*54e04708SPascal Brand 
66*54e04708SPascal Brand 	for (; i < blen; i++) {
67*54e04708SPascal Brand 		hs[i * 2 + 1] = tee_b2hs_add_base(b[i] & 0xf);
68*54e04708SPascal Brand 		hs[i * 2] = tee_b2hs_add_base(b[i] >> 4);
69*54e04708SPascal Brand 	}
70*54e04708SPascal Brand 	hs[blen * 2] = 0;
71*54e04708SPascal Brand 
72*54e04708SPascal Brand 	return blen * 2;
73*54e04708SPascal Brand }
74*54e04708SPascal Brand 
75*54e04708SPascal Brand uint32_t tee_hs2b(uint8_t *hs, uint8_t *b, uint32_t hslen, uint32_t blen)
76*54e04708SPascal Brand {
77*54e04708SPascal Brand 	uint32_t i = 0;
78*54e04708SPascal Brand 	uint32_t len = TEE_HS2B_BBUF_SIZE(hslen);
79*54e04708SPascal Brand 	uint8_t hi;
80*54e04708SPascal Brand 	uint8_t lo;
81*54e04708SPascal Brand 
82*54e04708SPascal Brand 	if (len > blen)
83*54e04708SPascal Brand 		return 0;
84*54e04708SPascal Brand 
85*54e04708SPascal Brand 	for (; i < len; i++) {
86*54e04708SPascal Brand 		if (tee_hs2b_rem_base(hs[i * 2], &hi))
87*54e04708SPascal Brand 			return 0;
88*54e04708SPascal Brand 		if (tee_hs2b_rem_base(hs[i * 2 + 1], &lo))
89*54e04708SPascal Brand 			return 0;
90*54e04708SPascal Brand 		b[i] = (hi << 4) + lo;
91*54e04708SPascal Brand 	}
92*54e04708SPascal Brand 
93*54e04708SPascal Brand 	return len;
94*54e04708SPascal Brand }
95