Skip to content

Commit da78ed4

Browse files
committed
hv: fix violations in md.c md.h and md_internal.h for crypto lib
-remove goto -remove multiple return -Modify assignment operator in boolean expression -Modify/fix code style violations -fix attempt to change parameters passed by value -fix value need U suffix -fix use of mixed arithmetic -fix assigment in expression -other fixes Tracked-On: projectacrn#861 Signed-off-by: Chen Gang G <gang.g.chen@intel.com> Reviewed-by: Bing Zhu <bing.zhu@intel.com> Acked-by: Eddie Dong <eddie.dong@intel.com>
1 parent 34fe019 commit da78ed4

3 files changed

Lines changed: 139 additions & 329 deletions

File tree

hypervisor/lib/crypto/mbedtls/md.c

Lines changed: 125 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -31,242 +31,193 @@
3131
/*
3232
* Reminder: update profiles in x509_crt.c when adding a new hash!
3333
*/
34-
static const int32_t supported_digests[] = {
35-
MBEDTLS_MD_SHA256,
36-
MBEDTLS_MD_NONE
37-
};
3834

39-
const int32_t *mbedtls_md_list( void )
35+
const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
4036
{
41-
return( supported_digests );
42-
}
37+
const mbedtls_md_info_t *md_info;
4338

44-
const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type )
45-
{
46-
switch( md_type )
39+
switch (md_type)
4740
{
4841
case MBEDTLS_MD_SHA256:
49-
return( &mbedtls_sha256_info );
42+
md_info = &mbedtls_sha256_info;
43+
break;
5044
default:
51-
return( NULL );
45+
md_info = NULL;
46+
break;
5247
}
53-
}
5448

55-
void mbedtls_md_init( mbedtls_md_context_t *ctx )
56-
{
57-
memset( ctx, 0, sizeof( mbedtls_md_context_t ) );
49+
return md_info;
5850
}
5951

60-
void mbedtls_md_free( mbedtls_md_context_t *ctx )
52+
void mbedtls_md_init(mbedtls_md_context_t *ctx)
6153
{
62-
if( ctx == NULL )
63-
return;
64-
65-
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_md_context_t ) );
54+
(void) memset(ctx, 0U, sizeof(mbedtls_md_context_t));
6655
}
6756

68-
int32_t mbedtls_md_clone( mbedtls_md_context_t *dst,
69-
const mbedtls_md_context_t *src )
57+
void mbedtls_md_free(mbedtls_md_context_t *ctx)
7058
{
71-
if( dst == NULL || dst->md_info == NULL ||
72-
src == NULL || src->md_info == NULL ||
73-
dst->md_info != src->md_info )
74-
{
75-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
59+
if (ctx != NULL) {
60+
(void) mbedtls_platform_zeroize(ctx, sizeof(mbedtls_md_context_t));
7661
}
7762

78-
dst->md_info->clone_func( dst->md_ctx, src->md_ctx );
79-
80-
return( 0 );
81-
}
82-
83-
int32_t mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info )
84-
{
85-
if( md_info == NULL || ctx == NULL )
86-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
87-
88-
ctx->md_info = md_info;
89-
90-
return( 0 );
91-
}
92-
93-
int32_t mbedtls_md_starts( mbedtls_md_context_t *ctx )
94-
{
95-
if( ctx == NULL || ctx->md_info == NULL )
96-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
97-
98-
return( ctx->md_info->starts_func( ctx->md_ctx ) );
99-
}
100-
101-
int32_t mbedtls_md_update( mbedtls_md_context_t *ctx, const uint8_t *input, size_t ilen )
102-
{
103-
if( ctx == NULL || ctx->md_info == NULL )
104-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
105-
106-
return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
63+
return;
10764
}
10865

109-
int32_t mbedtls_md_finish( mbedtls_md_context_t *ctx, uint8_t *output )
66+
int32_t mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info)
11067
{
111-
if( ctx == NULL || ctx->md_info == NULL )
112-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
68+
int32_t ret = 0;
11369

114-
return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
115-
}
116-
117-
int32_t mbedtls_md( const mbedtls_md_info_t *md_info, const uint8_t *input, size_t ilen,
118-
uint8_t *output )
119-
{
120-
if( md_info == NULL )
121-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
70+
if ((md_info == NULL) || (ctx == NULL)) {
71+
ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
72+
} else {
73+
ctx->md_info = md_info;
74+
}
12275

123-
return( md_info->digest_func( input, ilen, output ) );
76+
return ret;
12477
}
12578

126-
int32_t mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const uint8_t *key, size_t keylen )
79+
int32_t mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const uint8_t *key, size_t keylen)
12780
{
128-
int32_t ret;
81+
int32_t ret = 0;
12982
uint8_t sum[MBEDTLS_MD_MAX_SIZE];
13083
uint8_t *ipad, *opad;
84+
const uint8_t *temp_key = key;
13185
size_t i;
13286

133-
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
134-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
135-
136-
if( keylen > (size_t) ctx->md_info->block_size )
137-
{
138-
if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
139-
goto cleanup;
140-
if( ( ret = ctx->md_info->update_func( ctx->md_ctx, key, keylen ) ) != 0 )
141-
goto cleanup;
142-
if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, sum ) ) != 0 )
143-
goto cleanup;
144-
145-
keylen = ctx->md_info->size;
146-
key = sum;
87+
if ((ctx == NULL) || (ctx->md_info == NULL) || (ctx->hmac_ctx == NULL) || (temp_key == NULL)) {
88+
ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
14789
}
14890

149-
ipad = (uint8_t *) ctx->hmac_ctx;
150-
opad = (uint8_t *) ctx->hmac_ctx + ctx->md_info->block_size;
151-
152-
memset( ipad, 0x36, ctx->md_info->block_size );
153-
memset( opad, 0x5C, ctx->md_info->block_size );
154-
155-
for( i = 0; i < keylen; i++ )
156-
{
157-
ipad[i] = (uint8_t)( ipad[i] ^ key[i] );
158-
opad[i] = (uint8_t)( opad[i] ^ key[i] );
91+
if (ret == 0) {
92+
if (keylen > ctx->md_info->block_size) {
93+
ret = ctx->md_info->starts_func((void *) ctx->md_ctx);
94+
if (ret == 0) {
95+
ret = ctx->md_info->update_func((void *) ctx->md_ctx, temp_key, keylen);
96+
if (ret == 0) {
97+
ret = ctx->md_info->finish_func((void *) ctx->md_ctx, sum);
98+
}
99+
}
100+
101+
if (ret == 0) {
102+
keylen = (size_t) ctx->md_info->size;
103+
temp_key = sum;
104+
}
105+
}
106+
107+
if (ret == 0) {
108+
ipad = (uint8_t *) ctx->hmac_ctx;
109+
opad = (uint8_t *) ctx->hmac_ctx + ctx->md_info->block_size;
110+
111+
(void) memset(ipad, 0x36U, ctx->md_info->block_size);
112+
(void) memset(opad, 0x5CU, ctx->md_info->block_size);
113+
114+
for(i = 0U; i < keylen; i++) {
115+
*(ipad + i) = (uint8_t) (*(ipad + i) ^ *(temp_key + i));
116+
*(opad + i) = (uint8_t) (*(opad + i) ^ *(temp_key + i));
117+
}
118+
119+
ret = ctx->md_info->starts_func((void *) ctx->md_ctx);
120+
if (ret == 0) {
121+
ret = ctx->md_info->update_func((void *) ctx->md_ctx, ipad,
122+
ctx->md_info->block_size);
123+
}
124+
}
125+
(void) mbedtls_platform_zeroize(sum, sizeof(sum));
159126
}
160127

161-
if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
162-
goto cleanup;
163-
if( ( ret = ctx->md_info->update_func( ctx->md_ctx, ipad,
164-
ctx->md_info->block_size ) ) != 0 )
165-
goto cleanup;
166-
167-
cleanup:
168-
mbedtls_platform_zeroize( sum, sizeof( sum ) );
169-
170-
return( ret );
128+
return ret;
171129
}
172130

173-
int32_t mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const uint8_t *input, size_t ilen )
131+
int32_t mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const uint8_t *input, size_t ilen)
174132
{
175-
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
176-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
133+
int32_t ret;
177134

178-
return( ctx->md_info->update_func( ctx->md_ctx, input, ilen ) );
135+
if ((ctx == NULL) || (ctx->md_info == NULL) || (ctx->hmac_ctx == NULL)) {
136+
ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
137+
} else {
138+
ret = ctx->md_info->update_func((void *) ctx->md_ctx, input, ilen);
139+
}
140+
141+
return ret;
179142
}
180143

181-
int32_t mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, uint8_t *output )
144+
int32_t mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, uint8_t *output)
182145
{
183-
int32_t ret;
146+
int32_t ret = 0;
184147
uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
185148
uint8_t *opad;
186149

187-
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
188-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
189-
190-
opad = (uint8_t *) ctx->hmac_ctx + ctx->md_info->block_size;
191-
192-
if( ( ret = ctx->md_info->finish_func( ctx->md_ctx, tmp ) ) != 0 )
193-
return( ret );
194-
if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
195-
return( ret );
196-
if( ( ret = ctx->md_info->update_func( ctx->md_ctx, opad,
197-
ctx->md_info->block_size ) ) != 0 )
198-
return( ret );
199-
if( ( ret = ctx->md_info->update_func( ctx->md_ctx, tmp,
200-
ctx->md_info->size ) ) != 0 )
201-
return( ret );
202-
return( ctx->md_info->finish_func( ctx->md_ctx, output ) );
203-
}
150+
if ((ctx == NULL) || (ctx->md_info == NULL) || (ctx->hmac_ctx == NULL)) {
151+
ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
152+
}
204153

205-
int32_t mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx )
206-
{
207-
int32_t ret;
208-
uint8_t *ipad;
154+
if (ret == 0) {
155+
opad = (uint8_t *) ctx->hmac_ctx + ctx->md_info->block_size;
209156

210-
if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL )
211-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
157+
ret = ctx->md_info->finish_func((void *) ctx->md_ctx, tmp);
158+
if (ret == 0) {
159+
ret = ctx->md_info->starts_func((void *) ctx->md_ctx);
160+
}
161+
}
212162

213-
ipad = (uint8_t *) ctx->hmac_ctx;
163+
if (ret == 0) {
164+
ret = ctx->md_info->update_func((void *) ctx->md_ctx, opad,
165+
ctx->md_info->block_size);
166+
if (ret == 0) {
167+
ret = ctx->md_info->update_func((void *) ctx->md_ctx, tmp,
168+
ctx->md_info->size);
169+
}
170+
171+
if (ret == 0) {
172+
ret = ctx->md_info->finish_func((void *) ctx->md_ctx,
173+
(uint8_t *) output);
174+
}
175+
}
214176

215-
if( ( ret = ctx->md_info->starts_func( ctx->md_ctx ) ) != 0 )
216-
return( ret );
217-
return( ctx->md_info->update_func( ctx->md_ctx, ipad,
218-
ctx->md_info->block_size ) );
177+
return ret;
219178
}
220179

221-
int32_t mbedtls_md_hmac( const mbedtls_md_info_t *md_info,
180+
int32_t mbedtls_md_hmac(const mbedtls_md_info_t *md_info,
222181
const uint8_t *key, size_t keylen,
223182
const uint8_t *input, size_t ilen,
224-
uint8_t *output )
183+
uint8_t *output)
225184
{
226185
mbedtls_md_context_t ctx;
227-
int32_t ret;
186+
int32_t ret = 0;
228187

229-
if( md_info == NULL )
230-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
231-
232-
mbedtls_md_init( &ctx );
188+
if (md_info == NULL) {
189+
ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
190+
}
233191

234-
if( ( ret = mbedtls_md_setup( &ctx, md_info ) ) != 0 )
235-
goto cleanup;
192+
if (ret == 0) {
193+
mbedtls_md_init(&ctx);
236194

237-
if( ( ret = mbedtls_md_hmac_starts( &ctx, key, keylen ) ) != 0 )
238-
goto cleanup;
239-
if( ( ret = mbedtls_md_hmac_update( &ctx, input, ilen ) ) != 0 )
240-
goto cleanup;
241-
if( ( ret = mbedtls_md_hmac_finish( &ctx, output ) ) != 0 )
242-
goto cleanup;
195+
ret = mbedtls_md_setup(&ctx, md_info);
196+
if (ret == 0) {
197+
ret = mbedtls_md_hmac_starts(&ctx, key, keylen);
198+
}
243199

244-
cleanup:
245-
mbedtls_md_free( &ctx );
200+
if (ret == 0) {
201+
ret = mbedtls_md_hmac_update(&ctx, input, ilen);
202+
}
246203

247-
return( ret );
248-
}
204+
if (ret == 0) {
205+
ret = mbedtls_md_hmac_finish(&ctx, output);
206+
}
249207

250-
int32_t mbedtls_md_process( mbedtls_md_context_t *ctx, const uint8_t *data )
251-
{
252-
if( ctx == NULL || ctx->md_info == NULL )
253-
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
208+
mbedtls_md_free(&ctx);
209+
}
254210

255-
return( ctx->md_info->process_func( ctx->md_ctx, data ) );
211+
return ret;
256212
}
257213

258-
uint8_t mbedtls_md_get_size( const mbedtls_md_info_t *md_info )
214+
uint8_t mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
259215
{
260-
if( md_info == NULL )
261-
return( 0 );
262-
263-
return md_info->size;
264-
}
216+
uint8_t ret = 0U;
265217

266-
mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info )
267-
{
268-
if( md_info == NULL )
269-
return( MBEDTLS_MD_NONE );
218+
if (md_info != NULL) {
219+
ret = (uint8_t) md_info->size;
220+
}
270221

271-
return md_info->type;
222+
return ret;
272223
}

0 commit comments

Comments
 (0)