1 /*
2 * Copyright (c) 2001-2022, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6 #include "mbedtls/build_info.h"
7
8 #if defined(MBEDTLS_CHACHA20_C)
9 #include "mbedtls/chacha20.h"
10 #include "mbedtls/error.h"
11 #include "chacha20_alt.h"
12 #include "mbedtls/platform_util.h"
13 #include "chacha_driver.h"
14 #include "cc_pal_abort.h"
15 #include "cc_pal_mem.h"
16 #include "cc_pal_types.h"
17
18
19 #if defined(MBEDTLS_SELF_TEST)
20 #if defined(MBEDTLS_PLATFORM_C)
21 #include "mbedtls/platform.h"
22 #else
23 #define mbedtls_printf printf
24 #endif /* MBEDTLS_PLATFORM_C */
25 #endif /* MBEDTLS_SELF_TEST */
26
27 #if defined(MBEDTLS_CHACHA20_ALT)
28
29
mbedtls_chacha20_init(mbedtls_chacha20_context * ctx)30 void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx )
31 {
32 ChachaContext_t *chachaCtx = NULL;
33
34 /* ............... checking the parameters validity ................... */
35 /* -------------------------------------------------------------------- */
36 if (NULL == ctx)
37 {
38 CC_PalAbort("ctx cannot be NULL");
39 }
40
41 if (sizeof(mbedtls_chacha20_context) != sizeof(ChachaContext_t)) {
42 CC_PalAbort("\nChacha context sizes mismatch\n");
43 }
44
45 chachaCtx = (ChachaContext_t *)ctx;
46
47 chachaCtx->inputDataAddrType = DLLI_ADDR;
48 chachaCtx->outputDataAddrType = DLLI_ADDR;
49 }
50
mbedtls_chacha20_free(mbedtls_chacha20_context * ctx)51 void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx )
52 {
53 if( ctx != NULL )
54 {
55 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_chacha20_context ) );
56 }
57 }
58
mbedtls_chacha20_setkey(mbedtls_chacha20_context * ctx,const unsigned char key[32])59 int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx,
60 const unsigned char key[32] )
61 {
62
63 ChachaContext_t *chachaCtx = NULL;
64 if( ( ctx == NULL ) || ( key == NULL ) )
65 {
66 return( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
67 }
68
69 chachaCtx = (ChachaContext_t *)ctx;
70 CC_PalMemCopy(chachaCtx->keyBuf, key, MBEDTLS_CHACHA_KEY_SIZE_BYTES);
71
72 return( 0 );
73 }
74
mbedtls_chacha20_starts(mbedtls_chacha20_context * ctx,const unsigned char nonce[12],uint32_t counter)75 int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx,
76 const unsigned char nonce[12],
77 uint32_t counter )
78 {
79 ChachaContext_t *chachaCtx = NULL;
80 if( ( ctx == NULL ) || ( nonce == NULL ) )
81 {
82 return( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
83 }
84 if (sizeof(mbedtls_chacha20_context) != sizeof(ChachaContext_t)) {
85 return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA;
86 }
87 chachaCtx = (ChachaContext_t *)ctx;
88
89 chachaCtx->nonceSize = (chachaNonceSize_t)MBEDTLS_CHACHA_NONCE_SIZE_12BYTE_TYPE;
90
91 /* Copy the nonce to the context */
92 CC_PalMemCopy(chachaCtx->nonceBuf, nonce, MBEDTLS_CHACHA_NONCE_SIZE_BYTES);
93
94 /* init the block counter */
95 chachaCtx->blockCounterLsb = counter;
96 chachaCtx->blockCounterMsb = 0;
97
98 return( 0 );
99 }
100
mbedtls_chacha20_update(mbedtls_chacha20_context * ctx,size_t size,const unsigned char * input,unsigned char * output)101 int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx,
102 size_t size,
103 const unsigned char *input,
104 unsigned char *output )
105 {
106 ChachaContext_t *chachaCtx = NULL;
107 drvError_t drvRc;
108 uintptr_t upDataOut = 0;
109 uintptr_t upDataIn = 0;
110 CCBuffInfo_t inBuffInfo;
111 CCBuffInfo_t outBuffInfo;
112
113 if( ctx == NULL )
114 {
115 return( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA );
116 }
117
118 if ( size != 0 && input == NULL ) {
119 return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA;
120 }
121 if ( size != 0 && output == NULL ) {
122 return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA;
123 }
124 if (size == 0) {
125 return ( 0 );
126 }
127
128 upDataOut = (uintptr_t)input;
129 upDataIn = (uintptr_t)output;
130 if ((((upDataOut > upDataIn) && (upDataOut - upDataIn < size))) ||
131 (((upDataIn > upDataOut) && (upDataIn - upDataOut < size)))) {
132 return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA;
133 }
134
135 drvRc = SetDataBuffersInfo(input, size, &inBuffInfo,
136 output, size, &outBuffInfo);
137 if (drvRc != 0) {
138 CC_PAL_LOG_ERR("illegal data buffers\n");
139 return MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA;
140 }
141
142 chachaCtx = (ChachaContext_t *)ctx;
143
144 drvRc = ProcessChacha(chachaCtx, &inBuffInfo, &outBuffInfo, size);
145 if (drvRc != 0) {
146 CC_PAL_LOG_ERR("\nProcessChacha failed %d", drvRc);
147 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
148 }
149
150 return ( 0 );
151 }
152
mbedtls_chacha20_crypt(const unsigned char key[32],const unsigned char nonce[12],uint32_t counter,size_t data_len,const unsigned char * input,unsigned char * output)153 int mbedtls_chacha20_crypt( const unsigned char key[32],
154 const unsigned char nonce[12],
155 uint32_t counter,
156 size_t data_len,
157 const unsigned char* input,
158 unsigned char* output )
159 {
160 mbedtls_chacha20_context ctx;
161 int ret;
162
163 mbedtls_chacha20_init( &ctx );
164
165 ret = mbedtls_chacha20_setkey( &ctx, key );
166 if( ret != 0 )
167 goto cleanup;
168
169 ret = mbedtls_chacha20_starts( &ctx, nonce, counter );
170 if( ret != 0 )
171 goto cleanup;
172
173 ret = mbedtls_chacha20_update( &ctx, data_len, input, output );
174
175 cleanup:
176 mbedtls_chacha20_free( &ctx );
177 return( ret );
178 }
179
180 #endif /* !MBEDTLS_CHACHA20_ALT */
181
182 #endif /* !MBEDTLS_CHACHA20_C */
183