aboutsummaryrefslogtreecommitdiff
path: root/lib/ext/cryptocell-312-runtime/host/src/cc3x_lib/cc_lib.c
blob: 7fc7ee107b6ee4d9ccdaf8da0a650edf865f1f68 (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
/*
 * Copyright (c) 2001-2020, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */


#define CC_PAL_LOG_CUR_COMPONENT CC_LOG_MASK_CCLIB

#include "cc_pal_types.h"
#include "cc_pal_log.h"
#include "cc_pal_mem.h"
#include "cc_pal_abort.h"
#include "cc_lib.h"
#include "cc_hal.h"
#include "cc_pal_init.h"
#include "cc_pal_mutex.h"
#include "cc_pal_perf.h"
#include "cc_regs.h"
#include "dx_crys_kernel.h"
#include "dx_rng.h"
#include "dx_reg_common.h"
#include "llf_rnd_trng.h"
#include "cc_rng_plat.h"
#include "dx_id_registers.h"
#include "cc_util_pm.h"
#include "dx_nvm.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
#include "mbedtls/threading.h"
#include "mbedtls_cc_mng_int.h"
#include "mbedtls_cc_mng.h"
#include "cc_rnd_common.h"
#include "cc_int_general_defs.h"

CC_PalMutex CCSymCryptoMutex;
CC_PalMutex CCAsymCryptoMutex;
CC_PalMutex *pCCRndCryptoMutex;
CC_PalMutex CCApbFilteringRegMutex;
CC_PalMutex CCRndCryptoMutex;

static CCError_t RndStartupTest(
        CCRndWorkBuff_t  *workBuff_ptr/*in/out*/)
{
        /* error identifier definition */
        CCError_t error = CC_OK;
        CCRndState_t   rndState;
        CCRndParams_t  trngParams;

        error = RNG_PLAT_SetUserRngParameters(&trngParams);
        if (error != CC_SUCCESS) {
                return error;
        }

        error = CC_PalMutexLock(pCCRndCryptoMutex, CC_INFINITE);
        if (error != CC_SUCCESS) {
                CC_PalAbort("Fail to acquire mutex\n");
        }

        /* verify that the device is not in fatal error state before activating the PKA engine */
        CC_IS_FATAL_ERR_ON(error);
        if (error == CC_TRUE) {
                error = CC_LIB_RET_RND_INST_ERR;
                goto EndUnlockMutex;
        }

        /* increase CC counter at the beginning of each operation */
        error = CC_IS_WAKE;
        if (error != CC_SUCCESS) {
            CC_PalAbort("Fail to increase PM counter\n");
        }

        /* call on Instantiation mode */
        error = LLF_RND_RunTrngStartupTest(&rndState, &trngParams, (uint32_t*)workBuff_ptr);

        /* decrease CC counter at the end of each operation */
        if (CC_IS_IDLE != CC_SUCCESS) {
            CC_PalAbort("Fail to decrease PM counter\n");
        }

EndUnlockMutex:
        if (CC_PalMutexUnlock(pCCRndCryptoMutex) != CC_SUCCESS) {
                CC_PalAbort("Fail to release mutex\n");
        }
        return error;
}

static CClibRetCode_t InitHukRma(CCRndContext_t *rndContext_ptr)
{
    uint32_t lcsVal = 0;
    uint32_t kdrValues[CC_AES_KDR_MAX_SIZE_WORDS];
    CCError_t error = CC_OK;
    uint32_t i = 0;
    CCRndGenerateVectWorkFunc_t RndGenerateVectFunc = rndContext_ptr->rndGenerateVectFunc;

    mbedtls_mng_lcsGet( &lcsVal );

    if (lcsVal == CC_MNG_LCS_RMA){ /* in case lcs == RMA set the KDR*/
        error = RndGenerateVectFunc((void *)rndContext_ptr->rndState,
                        (unsigned char *)kdrValues, (size_t) CC_AES_KDR_MAX_SIZE_BYTES);
        if (error != CC_OK){
            return CC_LIB_RET_RND_INST_ERR;
        }

        /* set the random value to the KDR register */
        for (i = 0; i < CC_AES_KDR_MAX_SIZE_WORDS; i++){
            CC_HAL_WRITE_REGISTER( DX_HOST_SHADOW_KDR_REG_REG_OFFSET, kdrValues[i] );
        }
    }

    return CC_LIB_RET_OK;
}


static CClibRetCode_t VerifyPidVal(void)
{
    uint32_t pidReg[CC_BSV_PID_SIZE_WORDS] = {0};
    uint32_t pidVal1[CC_BSV_PID_SIZE_WORDS] = {CC_BSV_PID_0_VAL, CC_BSV_PID_1_VAL, CC_BSV_PID_2_VAL, CC_BSV_PID_3_VAL, CC_BSV_PID_4_VAL};
    uint32_t pidVal2[CC_BSV_PID_SIZE_WORDS] = {CC_BSV_PID_0_VAL, CC_BSV_PID_1_VAL, CC_BSV_PID_2_1_VAL, CC_BSV_PID_3_VAL, CC_BSV_PID_4_VAL};

    pidReg[0] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, PERIPHERAL_ID_0));
    pidReg[1] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, PERIPHERAL_ID_1));
    pidReg[2] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, PERIPHERAL_ID_2));
    pidReg[3] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, PERIPHERAL_ID_3));
    pidReg[4] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, PERIPHERAL_ID_4));

    if ((CC_PalMemCmp((uint8_t*)pidVal1, (uint8_t*)pidReg, sizeof(pidVal1)) != 0) &&
            (CC_PalMemCmp((uint8_t*)pidVal2, (uint8_t*)pidReg, sizeof(pidVal2)) != 0)) {
        return CC_LIB_RET_EINVAL_PIDR;
    }

    return CC_LIB_RET_OK;
}

static CClibRetCode_t VerifyCidVal(void)
{
    uint32_t cidReg[CC_BSV_CID_SIZE_WORDS] = {0};
    uint32_t cidVal[CC_BSV_CID_SIZE_WORDS] = {CC_BSV_CID_0_VAL, CC_BSV_CID_1_VAL, CC_BSV_CID_2_VAL, CC_BSV_CID_3_VAL};

    cidReg[0] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, COMPONENT_ID_0));
    cidReg[1] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, COMPONENT_ID_1));
    cidReg[2] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, COMPONENT_ID_2));
    cidReg[3] = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, COMPONENT_ID_3));

    if (CC_PalMemCmp((uint8_t*)cidVal, (uint8_t*)cidReg, sizeof(cidVal)) != 0){
        return CC_LIB_RET_EINVAL_CIDR;
    }

    return CC_LIB_RET_OK;
}


/*!
 * TEE (Trusted Execution Environment) entry point.
 * Init CryptoCell for TEE.
 *
 * @param[in/out] rndContext_ptr  - Pointer to the RND context buffer.
 * @param[in/out] rndWorkBuff_ptr  - Pointer to the RND scratch buffer.
 *
 * \return CClibRetCode_t one of the error codes defined in cc_lib.h
 */
CClibRetCode_t CC_LibInit(CCRndContext_t *rndContext_ptr, CCRndWorkBuff_t  *rndWorkBuff_ptr)
{
    int rc = 0;
    CClibRetCode_t retCode = CC_LIB_RET_OK;
    CCError_t error = CC_OK;
    uint32_t reg = 0;
    uint32_t tempVal = 0;

    /* check parameters */
    if (rndContext_ptr == NULL)
        return CC_LIB_RET_EINVAL_CTX_PTR;
    if (rndWorkBuff_ptr == NULL)
        return CC_LIB_RET_EINVAL_WORK_BUF_PTR;
    if (rndContext_ptr->rndState == NULL)
        return CC_LIB_RET_EINVAL_CTX_PTR;
    if (rndContext_ptr->entropyCtx == NULL)
        return CC_LIB_RET_EINVAL_CTX_PTR;

    rc = CC_HalInit();
    if (rc != CC_LIB_RET_OK) {
        retCode = CC_LIB_RET_HAL;
        goto InitErr1;
    }

    rc = CC_PalInit();
    if (rc != CC_LIB_RET_OK) {
        retCode = CC_LIB_RET_PAL;
        goto InitErr;
    }

    /* verify peripheral ID (PIDR) */
    rc = VerifyPidVal();
    if (rc != CC_LIB_RET_OK) {
        retCode = CC_LIB_RET_EINVAL_PIDR;
        goto InitErr2;
    }

    /* verify component ID (CIDR) */
    rc = VerifyCidVal();
    if (rc != CC_LIB_RET_OK) {
        retCode = CC_LIB_RET_EINVAL_CIDR;
        goto InitErr2;
    }

    /* turn off the DFA since Cerberus doen't support it */
    reg = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_AO_LOCK_BITS));
    CC_REG_FLD_SET(0, HOST_AO_LOCK_BITS, HOST_FORCE_DFA_ENABLE, reg, 0x0);
    CC_HAL_WRITE_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_AO_LOCK_BITS)  ,reg );
    tempVal = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF,HOST_AO_LOCK_BITS));
    if(tempVal != reg) {
        retCode = CC_LIB_AO_WRITE_FAILED_ERR;
        goto InitErr2;
    }

    CC_REG_FLD_SET(0, HOST_AO_LOCK_BITS, HOST_DFA_ENABLE_LOCK, reg, CC_TRUE);
    CC_HAL_WRITE_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_AO_LOCK_BITS)  ,reg );
    tempVal = CC_HAL_READ_REGISTER(CC_REG_OFFSET(HOST_RGF,HOST_AO_LOCK_BITS));
    if(tempVal != reg) {
        retCode = CC_LIB_AO_WRITE_FAILED_ERR;
        goto InitErr2;
    }

    CC_HAL_WRITE_REGISTER(CC_REG_OFFSET(HOST_RGF, AES_DFA_IS_ON)  ,0x0UL );

#ifdef BIG__ENDIAN
/* Set DMA endianess to big */
    CC_HAL_WRITE_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_ENDIAN) , 0xCCUL);
#else /* LITTLE__ENDIAN */
    CC_HAL_WRITE_REGISTER(CC_REG_OFFSET(HOST_RGF, HOST_ENDIAN) , 0x00UL);
#endif

    CC_PAL_PERF_INIT();

    /* Initialize RND module */
    error = RndStartupTest(rndWorkBuff_ptr);
    if (error != 0) {
        retCode = CC_LIB_RET_RND_INST_ERR;
        goto InitErr2;
    }

    /* Initialize mbedTLS random function*/
    mbedtls_ctr_drbg_init(rndContext_ptr->rndState);
    mbedtls_entropy_init( rndContext_ptr->entropyCtx );
    error = mbedtls_ctr_drbg_seed(rndContext_ptr->rndState, mbedtls_entropy_func, rndContext_ptr->entropyCtx,
            NULL, 0);
    if (error != 0) {
        retCode = CC_LIB_RET_RND_INST_ERR;
        goto InitErr2;
    }

    error = CC_RndSetGenerateVectorFunc(rndContext_ptr, mbedtls_ctr_drbg_random);
    if (error != 0) {
        retCode = CC_LIB_RET_RND_INST_ERR;
        goto InitErr2;
    }
    error = InitHukRma(rndContext_ptr);
    if (error != 0) {
        retCode = CC_LIB_RET_RND_INST_ERR;
        goto InitErr2;
    }
    return CC_LIB_RET_OK;
    InitErr2:
    CC_HalTerminate();

    InitErr1:
    CC_PalTerminate();

    InitErr:
    return retCode;
}


/*!
 * TEE (Trusted Execution Environment) exit point.
 * Finalize CryptoCell for TEE operation, release associated resources.
 *                                                                    .
 * @param[in/out] rndContext_ptr  - Pointer to the RND context buffer.
 */
CClibRetCode_t CC_LibFini(CCRndContext_t *rndContext_ptr)
{
    CCError_t rc = CC_OK;
    CClibRetCode_t retCode = CC_LIB_RET_OK;

    /* check parameters */
    if (rndContext_ptr == NULL)
        return CC_LIB_RET_EINVAL_CTX_PTR;

    rc = CC_HalTerminate();
    if (rc != 0){
        retCode = CC_LIB_RET_HAL;
    }
    CC_PalTerminate();

    rndContext_ptr->rndGenerateVectFunc=NULL;
    mbedtls_ctr_drbg_free( rndContext_ptr->rndState );
    mbedtls_entropy_free( rndContext_ptr->entropyCtx );

    CC_PAL_PERF_FIN();

    return retCode;

}

void __cyg_profile_func_enter (void *this_fn, void *call_site) {
    unsigned int i;
    CC_UNUSED_PARAM(i);
    CC_UNUSED_PARAM(this_fn);
    CC_UNUSED_PARAM(call_site);

    CC_PAL_LOG_ERR("Entering: %p -> %p (stack: %p)\n",
                    call_site, this_fn, &i );
}

void __cyg_profile_func_exit (void *this_fn, void *call_site) {
    unsigned int i;
    CC_UNUSED_PARAM(i);
    CC_UNUSED_PARAM(this_fn);
    CC_UNUSED_PARAM(call_site);

    CC_PAL_LOG_ERR("Exiting: %p <- %p (stack: %p)\n",
                     call_site, this_fn, &i );
}