blob: deb13c215535360a5cc2601428bc367698997e4a [file] [log] [blame]
Steven Cooreman0e307642021-02-18 16:18:32 +01001/*
2 * PSA hashing layer on top of Mbed TLS software crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include <psa/crypto.h>
26#include "psa_crypto_core.h"
27#include "psa_crypto_hash.h"
28
29#include <mbedtls/error.h>
30#include <string.h>
31
32psa_status_t mbedtls_psa_hash_compute(
33 psa_algorithm_t alg,
34 const uint8_t *input,
35 size_t input_length,
36 uint8_t *hash,
37 size_t hash_size,
38 size_t *hash_length)
39{
40 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
41 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
42
43 *hash_length = hash_size;
44 status = mbedtls_psa_hash_setup( &operation, alg );
45 if( status != PSA_SUCCESS )
46 goto exit;
47 status = mbedtls_psa_hash_update( &operation, input, input_length );
48 if( status != PSA_SUCCESS )
49 goto exit;
50 status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length );
51 if( status != PSA_SUCCESS )
52 goto exit;
53
54exit:
55 if( status == PSA_SUCCESS )
56 status = mbedtls_psa_hash_abort( &operation );
57 else
58 mbedtls_psa_hash_abort( &operation );
59 return( status );
60}
61
62psa_status_t mbedtls_psa_hash_setup(
63 mbedtls_psa_hash_operation_t *operation,
64 psa_algorithm_t alg )
65{
66 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
67
68 /* A context must be freshly initialized before it can be set up. */
69 if( operation->alg != 0 )
70 {
71 return( PSA_ERROR_BAD_STATE );
72 }
73
74 switch( alg )
75 {
76#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
77 case PSA_ALG_MD2:
78 mbedtls_md2_init( &operation->ctx.md2 );
79 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
80 break;
81#endif
82#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
83 case PSA_ALG_MD4:
84 mbedtls_md4_init( &operation->ctx.md4 );
85 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
86 break;
87#endif
88#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
89 case PSA_ALG_MD5:
90 mbedtls_md5_init( &operation->ctx.md5 );
91 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
92 break;
93#endif
94#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
95 case PSA_ALG_RIPEMD160:
96 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
97 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
98 break;
99#endif
100#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
101 case PSA_ALG_SHA_1:
102 mbedtls_sha1_init( &operation->ctx.sha1 );
103 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
104 break;
105#endif
106#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
107 case PSA_ALG_SHA_224:
108 mbedtls_sha256_init( &operation->ctx.sha256 );
109 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
110 break;
111#endif
112#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
113 case PSA_ALG_SHA_256:
114 mbedtls_sha256_init( &operation->ctx.sha256 );
115 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
116 break;
117#endif
118#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
119 case PSA_ALG_SHA_384:
120 mbedtls_sha512_init( &operation->ctx.sha512 );
121 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
122 break;
123#endif
124#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
125 case PSA_ALG_SHA_512:
126 mbedtls_sha512_init( &operation->ctx.sha512 );
127 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
128 break;
129#endif
130 default:
131 return( PSA_ALG_IS_HASH( alg ) ?
132 PSA_ERROR_NOT_SUPPORTED :
133 PSA_ERROR_INVALID_ARGUMENT );
134 }
135 if( ret == 0 )
136 operation->alg = alg;
137 else
138 mbedtls_psa_hash_abort( operation );
139 return( mbedtls_to_psa_error( ret ) );
140}
141
142psa_status_t mbedtls_psa_hash_clone(
143 const mbedtls_psa_hash_operation_t *source_operation,
144 mbedtls_psa_hash_operation_t *target_operation )
145{
146 switch( source_operation->alg )
147 {
148 case 0:
149 return( PSA_ERROR_BAD_STATE );
150#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
151 case PSA_ALG_MD2:
152 mbedtls_md2_clone( &target_operation->ctx.md2,
153 &source_operation->ctx.md2 );
154 break;
155#endif
156#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
157 case PSA_ALG_MD4:
158 mbedtls_md4_clone( &target_operation->ctx.md4,
159 &source_operation->ctx.md4 );
160 break;
161#endif
162#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
163 case PSA_ALG_MD5:
164 mbedtls_md5_clone( &target_operation->ctx.md5,
165 &source_operation->ctx.md5 );
166 break;
167#endif
168#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
169 case PSA_ALG_RIPEMD160:
170 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
171 &source_operation->ctx.ripemd160 );
172 break;
173#endif
174#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
175 case PSA_ALG_SHA_1:
176 mbedtls_sha1_clone( &target_operation->ctx.sha1,
177 &source_operation->ctx.sha1 );
178 break;
179#endif
180#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
181 case PSA_ALG_SHA_224:
182 mbedtls_sha256_clone( &target_operation->ctx.sha256,
183 &source_operation->ctx.sha256 );
184 break;
185#endif
186#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
187 case PSA_ALG_SHA_256:
188 mbedtls_sha256_clone( &target_operation->ctx.sha256,
189 &source_operation->ctx.sha256 );
190 break;
191#endif
192#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
193 case PSA_ALG_SHA_384:
194 mbedtls_sha512_clone( &target_operation->ctx.sha512,
195 &source_operation->ctx.sha512 );
196 break;
197#endif
198#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
199 case PSA_ALG_SHA_512:
200 mbedtls_sha512_clone( &target_operation->ctx.sha512,
201 &source_operation->ctx.sha512 );
202 break;
203#endif
204 default:
205 return( PSA_ERROR_NOT_SUPPORTED );
206 }
207
208 target_operation->alg = source_operation->alg;
209 return( PSA_SUCCESS );
210}
211
212psa_status_t mbedtls_psa_hash_update(
213 mbedtls_psa_hash_operation_t *operation,
214 const uint8_t *input,
215 size_t input_length )
216{
217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
218
219 /* Don't require hash implementations to behave correctly on a
220 * zero-length input, which may have an invalid pointer. */
221 if( input_length == 0 )
222 return( PSA_SUCCESS );
223
224 switch( operation->alg )
225 {
226#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
227 case PSA_ALG_MD2:
228 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
229 input, input_length );
230 break;
231#endif
232#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
233 case PSA_ALG_MD4:
234 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
235 input, input_length );
236 break;
237#endif
238#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
239 case PSA_ALG_MD5:
240 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
241 input, input_length );
242 break;
243#endif
244#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
245 case PSA_ALG_RIPEMD160:
246 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
247 input, input_length );
248 break;
249#endif
250#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
251 case PSA_ALG_SHA_1:
252 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
253 input, input_length );
254 break;
255#endif
256#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
257 case PSA_ALG_SHA_224:
258 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
259 input, input_length );
260 break;
261#endif
262#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
263 case PSA_ALG_SHA_256:
264 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
265 input, input_length );
266 break;
267#endif
268#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
269 case PSA_ALG_SHA_384:
270 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
271 input, input_length );
272 break;
273#endif
274#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
275 case PSA_ALG_SHA_512:
276 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
277 input, input_length );
278 break;
279#endif
280 default:
281 (void)input;
282 return( PSA_ERROR_BAD_STATE );
283 }
284
285 if( ret != 0 )
286 mbedtls_psa_hash_abort( operation );
287 return( mbedtls_to_psa_error( ret ) );
288}
289
290psa_status_t mbedtls_psa_hash_finish(
291 mbedtls_psa_hash_operation_t *operation,
292 uint8_t *hash,
293 size_t hash_size,
294 size_t *hash_length )
295{
296 psa_status_t status;
297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
298 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
299
300 /* Fill the output buffer with something that isn't a valid hash
301 * (barring an attack on the hash and deliberately-crafted input),
302 * in case the caller doesn't check the return status properly. */
303 *hash_length = hash_size;
304 /* If hash_size is 0 then hash may be NULL and then the
305 * call to memset would have undefined behavior. */
306 if( hash_size != 0 )
307 memset( hash, '!', hash_size );
308
309 if( hash_size < actual_hash_length )
310 {
311 status = PSA_ERROR_BUFFER_TOO_SMALL;
312 goto exit;
313 }
314
315 switch( operation->alg )
316 {
317#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
318 case PSA_ALG_MD2:
319 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
320 break;
321#endif
322#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
323 case PSA_ALG_MD4:
324 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
325 break;
326#endif
327#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
328 case PSA_ALG_MD5:
329 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
330 break;
331#endif
332#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
333 case PSA_ALG_RIPEMD160:
334 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
335 break;
336#endif
337#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
338 case PSA_ALG_SHA_1:
339 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
340 break;
341#endif
342#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
343 case PSA_ALG_SHA_224:
344 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
345 break;
346#endif
347#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
348 case PSA_ALG_SHA_256:
349 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
350 break;
351#endif
352#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
353 case PSA_ALG_SHA_384:
354 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
355 break;
356#endif
357#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
358 case PSA_ALG_SHA_512:
359 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
360 break;
361#endif
362 default:
363 return( PSA_ERROR_BAD_STATE );
364 }
365 status = mbedtls_to_psa_error( ret );
366
367exit:
368 if( status == PSA_SUCCESS )
369 {
370 *hash_length = actual_hash_length;
371 return( mbedtls_psa_hash_abort( operation ) );
372 }
373 else
374 {
375 mbedtls_psa_hash_abort( operation );
376 return( status );
377 }
378}
379
380psa_status_t mbedtls_psa_hash_abort(
381 mbedtls_psa_hash_operation_t *operation )
382{
383 switch( operation->alg )
384 {
385 case 0:
386 /* The object has (apparently) been initialized but it is not
387 * in use. It's ok to call abort on such an object, and there's
388 * nothing to do. */
389 break;
390#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
391 case PSA_ALG_MD2:
392 mbedtls_md2_free( &operation->ctx.md2 );
393 break;
394#endif
395#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
396 case PSA_ALG_MD4:
397 mbedtls_md4_free( &operation->ctx.md4 );
398 break;
399#endif
400#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
401 case PSA_ALG_MD5:
402 mbedtls_md5_free( &operation->ctx.md5 );
403 break;
404#endif
405#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
406 case PSA_ALG_RIPEMD160:
407 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
408 break;
409#endif
410#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
411 case PSA_ALG_SHA_1:
412 mbedtls_sha1_free( &operation->ctx.sha1 );
413 break;
414#endif
415#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
416 case PSA_ALG_SHA_224:
417 mbedtls_sha256_free( &operation->ctx.sha256 );
418 break;
419#endif
420#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
421 case PSA_ALG_SHA_256:
422 mbedtls_sha256_free( &operation->ctx.sha256 );
423 break;
424#endif
425#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
426 case PSA_ALG_SHA_384:
427 mbedtls_sha512_free( &operation->ctx.sha512 );
428 break;
429#endif
430#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
431 case PSA_ALG_SHA_512:
432 mbedtls_sha512_free( &operation->ctx.sha512 );
433 break;
434#endif
435 default:
436 return( PSA_ERROR_BAD_STATE );
437 }
438 operation->alg = 0;
439 return( PSA_SUCCESS );
440}
441
442#endif /* MBEDTLS_PSA_CRYPTO_C */