|
31 | 31 | /* |
32 | 32 | * Reminder: update profiles in x509_crt.c when adding a new hash! |
33 | 33 | */ |
34 | | -static const int32_t supported_digests[] = { |
35 | | - MBEDTLS_MD_SHA256, |
36 | | - MBEDTLS_MD_NONE |
37 | | -}; |
38 | 34 |
|
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) |
40 | 36 | { |
41 | | - return( supported_digests ); |
42 | | -} |
| 37 | + const mbedtls_md_info_t *md_info; |
43 | 38 |
|
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) |
47 | 40 | { |
48 | 41 | case MBEDTLS_MD_SHA256: |
49 | | - return( &mbedtls_sha256_info ); |
| 42 | + md_info = &mbedtls_sha256_info; |
| 43 | + break; |
50 | 44 | default: |
51 | | - return( NULL ); |
| 45 | + md_info = NULL; |
| 46 | + break; |
52 | 47 | } |
53 | | -} |
54 | 48 |
|
55 | | -void mbedtls_md_init( mbedtls_md_context_t *ctx ) |
56 | | -{ |
57 | | - memset( ctx, 0, sizeof( mbedtls_md_context_t ) ); |
| 49 | + return md_info; |
58 | 50 | } |
59 | 51 |
|
60 | | -void mbedtls_md_free( mbedtls_md_context_t *ctx ) |
| 52 | +void mbedtls_md_init(mbedtls_md_context_t *ctx) |
61 | 53 | { |
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)); |
66 | 55 | } |
67 | 56 |
|
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) |
70 | 58 | { |
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)); |
76 | 61 | } |
77 | 62 |
|
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; |
107 | 64 | } |
108 | 65 |
|
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) |
110 | 67 | { |
111 | | - if( ctx == NULL || ctx->md_info == NULL ) |
112 | | - return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 68 | + int32_t ret = 0; |
113 | 69 |
|
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 | + } |
122 | 75 |
|
123 | | - return( md_info->digest_func( input, ilen, output ) ); |
| 76 | + return ret; |
124 | 77 | } |
125 | 78 |
|
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) |
127 | 80 | { |
128 | | - int32_t ret; |
| 81 | + int32_t ret = 0; |
129 | 82 | uint8_t sum[MBEDTLS_MD_MAX_SIZE]; |
130 | 83 | uint8_t *ipad, *opad; |
| 84 | + const uint8_t *temp_key = key; |
131 | 85 | size_t i; |
132 | 86 |
|
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; |
147 | 89 | } |
148 | 90 |
|
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)); |
159 | 126 | } |
160 | 127 |
|
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; |
171 | 129 | } |
172 | 130 |
|
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) |
174 | 132 | { |
175 | | - if( ctx == NULL || ctx->md_info == NULL || ctx->hmac_ctx == NULL ) |
176 | | - return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); |
| 133 | + int32_t ret; |
177 | 134 |
|
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; |
179 | 142 | } |
180 | 143 |
|
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) |
182 | 145 | { |
183 | | - int32_t ret; |
| 146 | + int32_t ret = 0; |
184 | 147 | uint8_t tmp[MBEDTLS_MD_MAX_SIZE]; |
185 | 148 | uint8_t *opad; |
186 | 149 |
|
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 | + } |
204 | 153 |
|
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; |
209 | 156 |
|
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 | + } |
212 | 162 |
|
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 | + } |
214 | 176 |
|
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; |
219 | 178 | } |
220 | 179 |
|
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, |
222 | 181 | const uint8_t *key, size_t keylen, |
223 | 182 | const uint8_t *input, size_t ilen, |
224 | | - uint8_t *output ) |
| 183 | + uint8_t *output) |
225 | 184 | { |
226 | 185 | mbedtls_md_context_t ctx; |
227 | | - int32_t ret; |
| 186 | + int32_t ret = 0; |
228 | 187 |
|
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 | + } |
233 | 191 |
|
234 | | - if( ( ret = mbedtls_md_setup( &ctx, md_info ) ) != 0 ) |
235 | | - goto cleanup; |
| 192 | + if (ret == 0) { |
| 193 | + mbedtls_md_init(&ctx); |
236 | 194 |
|
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 | + } |
243 | 199 |
|
244 | | -cleanup: |
245 | | - mbedtls_md_free( &ctx ); |
| 200 | + if (ret == 0) { |
| 201 | + ret = mbedtls_md_hmac_update(&ctx, input, ilen); |
| 202 | + } |
246 | 203 |
|
247 | | - return( ret ); |
248 | | -} |
| 204 | + if (ret == 0) { |
| 205 | + ret = mbedtls_md_hmac_finish(&ctx, output); |
| 206 | + } |
249 | 207 |
|
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 | + } |
254 | 210 |
|
255 | | - return( ctx->md_info->process_func( ctx->md_ctx, data ) ); |
| 211 | + return ret; |
256 | 212 | } |
257 | 213 |
|
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) |
259 | 215 | { |
260 | | - if( md_info == NULL ) |
261 | | - return( 0 ); |
262 | | - |
263 | | - return md_info->size; |
264 | | -} |
| 216 | + uint8_t ret = 0U; |
265 | 217 |
|
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 | + } |
270 | 221 |
|
271 | | - return md_info->type; |
| 222 | + return ret; |
272 | 223 | } |
0 commit comments