1 /*
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 
32 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
33     defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
34     defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
35     defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
mbedtls_md_info_from_psa(psa_algorithm_t alg)36 const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
37 {
38     switch( alg )
39     {
40 #if defined(MBEDTLS_MD2_C)
41         case PSA_ALG_MD2:
42             return( &mbedtls_md2_info );
43 #endif
44 #if defined(MBEDTLS_MD4_C)
45         case PSA_ALG_MD4:
46             return( &mbedtls_md4_info );
47 #endif
48 #if defined(MBEDTLS_MD5_C)
49         case PSA_ALG_MD5:
50             return( &mbedtls_md5_info );
51 #endif
52 #if defined(MBEDTLS_RIPEMD160_C)
53         case PSA_ALG_RIPEMD160:
54             return( &mbedtls_ripemd160_info );
55 #endif
56 #if defined(MBEDTLS_SHA1_C)
57         case PSA_ALG_SHA_1:
58             return( &mbedtls_sha1_info );
59 #endif
60 #if defined(MBEDTLS_SHA256_C)
61         case PSA_ALG_SHA_224:
62             return( &mbedtls_sha224_info );
63 #endif
64 #if defined(MBEDTLS_SHA256_C)
65         case PSA_ALG_SHA_256:
66             return( &mbedtls_sha256_info );
67 #endif
68 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
69         case PSA_ALG_SHA_384:
70             return( &mbedtls_sha384_info );
71 #endif
72 #if defined(MBEDTLS_SHA512_C)
73         case PSA_ALG_SHA_512:
74             return( &mbedtls_sha512_info );
75 #endif
76         default:
77             return( NULL );
78     }
79 }
80 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
81         * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
82         * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
83         * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
84 
85 #if defined(MBEDTLS_PSA_BUILTIN_HASH)
mbedtls_psa_hash_abort(mbedtls_psa_hash_operation_t * operation)86 psa_status_t mbedtls_psa_hash_abort(
87     mbedtls_psa_hash_operation_t *operation )
88 {
89     switch( operation->alg )
90     {
91         case 0:
92             /* The object has (apparently) been initialized but it is not
93              * in use. It's ok to call abort on such an object, and there's
94              * nothing to do. */
95             break;
96 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
97         case PSA_ALG_MD2:
98             mbedtls_md2_free( &operation->ctx.md2 );
99             break;
100 #endif
101 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
102         case PSA_ALG_MD4:
103             mbedtls_md4_free( &operation->ctx.md4 );
104             break;
105 #endif
106 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
107         case PSA_ALG_MD5:
108             mbedtls_md5_free( &operation->ctx.md5 );
109             break;
110 #endif
111 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
112         case PSA_ALG_RIPEMD160:
113             mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
114             break;
115 #endif
116 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
117         case PSA_ALG_SHA_1:
118             mbedtls_sha1_free( &operation->ctx.sha1 );
119             break;
120 #endif
121 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
122         case PSA_ALG_SHA_224:
123             mbedtls_sha256_free( &operation->ctx.sha256 );
124             break;
125 #endif
126 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
127         case PSA_ALG_SHA_256:
128             mbedtls_sha256_free( &operation->ctx.sha256 );
129             break;
130 #endif
131 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
132         case PSA_ALG_SHA_384:
133             mbedtls_sha512_free( &operation->ctx.sha512 );
134             break;
135 #endif
136 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
137         case PSA_ALG_SHA_512:
138             mbedtls_sha512_free( &operation->ctx.sha512 );
139             break;
140 #endif
141         default:
142             return( PSA_ERROR_BAD_STATE );
143     }
144     operation->alg = 0;
145     return( PSA_SUCCESS );
146 }
147 
mbedtls_psa_hash_setup(mbedtls_psa_hash_operation_t * operation,psa_algorithm_t alg)148 psa_status_t mbedtls_psa_hash_setup(
149     mbedtls_psa_hash_operation_t *operation,
150     psa_algorithm_t alg )
151 {
152     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
153 
154     /* A context must be freshly initialized before it can be set up. */
155     if( operation->alg != 0 )
156     {
157         return( PSA_ERROR_BAD_STATE );
158     }
159 
160     switch( alg )
161     {
162 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
163         case PSA_ALG_MD2:
164             mbedtls_md2_init( &operation->ctx.md2 );
165             ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
166             break;
167 #endif
168 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
169         case PSA_ALG_MD4:
170             mbedtls_md4_init( &operation->ctx.md4 );
171             ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
172             break;
173 #endif
174 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
175         case PSA_ALG_MD5:
176             mbedtls_md5_init( &operation->ctx.md5 );
177             ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
178             break;
179 #endif
180 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
181         case PSA_ALG_RIPEMD160:
182             mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
183             ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
184             break;
185 #endif
186 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
187         case PSA_ALG_SHA_1:
188             mbedtls_sha1_init( &operation->ctx.sha1 );
189             ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
190             break;
191 #endif
192 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
193         case PSA_ALG_SHA_224:
194             mbedtls_sha256_init( &operation->ctx.sha256 );
195             ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
196             break;
197 #endif
198 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
199         case PSA_ALG_SHA_256:
200             mbedtls_sha256_init( &operation->ctx.sha256 );
201             ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
202             break;
203 #endif
204 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
205         case PSA_ALG_SHA_384:
206             mbedtls_sha512_init( &operation->ctx.sha512 );
207             ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
208             break;
209 #endif
210 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
211         case PSA_ALG_SHA_512:
212             mbedtls_sha512_init( &operation->ctx.sha512 );
213             ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
214             break;
215 #endif
216         default:
217             return( PSA_ALG_IS_HASH( alg ) ?
218                     PSA_ERROR_NOT_SUPPORTED :
219                     PSA_ERROR_INVALID_ARGUMENT );
220     }
221     if( ret == 0 )
222         operation->alg = alg;
223     else
224         mbedtls_psa_hash_abort( operation );
225     return( mbedtls_to_psa_error( ret ) );
226 }
227 
mbedtls_psa_hash_clone(const mbedtls_psa_hash_operation_t * source_operation,mbedtls_psa_hash_operation_t * target_operation)228 psa_status_t mbedtls_psa_hash_clone(
229     const mbedtls_psa_hash_operation_t *source_operation,
230     mbedtls_psa_hash_operation_t *target_operation )
231 {
232     switch( source_operation->alg )
233     {
234         case 0:
235             return( PSA_ERROR_BAD_STATE );
236 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
237         case PSA_ALG_MD2:
238             mbedtls_md2_clone( &target_operation->ctx.md2,
239                                &source_operation->ctx.md2 );
240             break;
241 #endif
242 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
243         case PSA_ALG_MD4:
244             mbedtls_md4_clone( &target_operation->ctx.md4,
245                                &source_operation->ctx.md4 );
246             break;
247 #endif
248 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
249         case PSA_ALG_MD5:
250             mbedtls_md5_clone( &target_operation->ctx.md5,
251                                &source_operation->ctx.md5 );
252             break;
253 #endif
254 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
255         case PSA_ALG_RIPEMD160:
256             mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
257                                      &source_operation->ctx.ripemd160 );
258             break;
259 #endif
260 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
261         case PSA_ALG_SHA_1:
262             mbedtls_sha1_clone( &target_operation->ctx.sha1,
263                                 &source_operation->ctx.sha1 );
264             break;
265 #endif
266 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
267         case PSA_ALG_SHA_224:
268             mbedtls_sha256_clone( &target_operation->ctx.sha256,
269                                   &source_operation->ctx.sha256 );
270             break;
271 #endif
272 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
273         case PSA_ALG_SHA_256:
274             mbedtls_sha256_clone( &target_operation->ctx.sha256,
275                                   &source_operation->ctx.sha256 );
276             break;
277 #endif
278 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
279         case PSA_ALG_SHA_384:
280             mbedtls_sha512_clone( &target_operation->ctx.sha512,
281                                   &source_operation->ctx.sha512 );
282             break;
283 #endif
284 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
285         case PSA_ALG_SHA_512:
286             mbedtls_sha512_clone( &target_operation->ctx.sha512,
287                                   &source_operation->ctx.sha512 );
288             break;
289 #endif
290         default:
291             (void) source_operation;
292             (void) target_operation;
293             return( PSA_ERROR_NOT_SUPPORTED );
294     }
295 
296     target_operation->alg = source_operation->alg;
297     return( PSA_SUCCESS );
298 }
299 
mbedtls_psa_hash_update(mbedtls_psa_hash_operation_t * operation,const uint8_t * input,size_t input_length)300 psa_status_t mbedtls_psa_hash_update(
301     mbedtls_psa_hash_operation_t *operation,
302     const uint8_t *input,
303     size_t input_length )
304 {
305     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306 
307     switch( operation->alg )
308     {
309 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
310         case PSA_ALG_MD2:
311             ret = mbedtls_md2_update_ret( &operation->ctx.md2,
312                                           input, input_length );
313             break;
314 #endif
315 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
316         case PSA_ALG_MD4:
317             ret = mbedtls_md4_update_ret( &operation->ctx.md4,
318                                           input, input_length );
319             break;
320 #endif
321 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
322         case PSA_ALG_MD5:
323             ret = mbedtls_md5_update_ret( &operation->ctx.md5,
324                                           input, input_length );
325             break;
326 #endif
327 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
328         case PSA_ALG_RIPEMD160:
329             ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
330                                                 input, input_length );
331             break;
332 #endif
333 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
334         case PSA_ALG_SHA_1:
335             ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
336                                            input, input_length );
337             break;
338 #endif
339 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
340         case PSA_ALG_SHA_224:
341             ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
342                                              input, input_length );
343             break;
344 #endif
345 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
346         case PSA_ALG_SHA_256:
347             ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
348                                              input, input_length );
349             break;
350 #endif
351 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
352         case PSA_ALG_SHA_384:
353             ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
354                                              input, input_length );
355             break;
356 #endif
357 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
358         case PSA_ALG_SHA_512:
359             ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
360                                              input, input_length );
361             break;
362 #endif
363         default:
364             (void) input;
365             (void) input_length;
366             return( PSA_ERROR_BAD_STATE );
367     }
368 
369     return( mbedtls_to_psa_error( ret ) );
370 }
371 
mbedtls_psa_hash_finish(mbedtls_psa_hash_operation_t * operation,uint8_t * hash,size_t hash_size,size_t * hash_length)372 psa_status_t mbedtls_psa_hash_finish(
373     mbedtls_psa_hash_operation_t *operation,
374     uint8_t *hash,
375     size_t hash_size,
376     size_t *hash_length )
377 {
378     psa_status_t status;
379     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
380     size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
381 
382     /* Fill the output buffer with something that isn't a valid hash
383      * (barring an attack on the hash and deliberately-crafted input),
384      * in case the caller doesn't check the return status properly. */
385     *hash_length = hash_size;
386     /* If hash_size is 0 then hash may be NULL and then the
387      * call to memset would have undefined behavior. */
388     if( hash_size != 0 )
389         memset( hash, '!', hash_size );
390 
391     if( hash_size < actual_hash_length )
392     {
393         status = PSA_ERROR_BUFFER_TOO_SMALL;
394         goto exit;
395     }
396 
397     switch( operation->alg )
398     {
399 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
400         case PSA_ALG_MD2:
401             ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
402             break;
403 #endif
404 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
405         case PSA_ALG_MD4:
406             ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
407             break;
408 #endif
409 #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
410         case PSA_ALG_MD5:
411             ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
412             break;
413 #endif
414 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
415         case PSA_ALG_RIPEMD160:
416             ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
417             break;
418 #endif
419 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
420         case PSA_ALG_SHA_1:
421             ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
422             break;
423 #endif
424 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
425         case PSA_ALG_SHA_224:
426             ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
427             break;
428 #endif
429 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
430         case PSA_ALG_SHA_256:
431             ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
432             break;
433 #endif
434 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
435         case PSA_ALG_SHA_384:
436             ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
437             break;
438 #endif
439 #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
440         case PSA_ALG_SHA_512:
441             ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
442             break;
443 #endif
444         default:
445             (void) hash;
446             return( PSA_ERROR_BAD_STATE );
447     }
448     status = mbedtls_to_psa_error( ret );
449 
450 exit:
451     if( status == PSA_SUCCESS )
452         *hash_length = actual_hash_length;
453     return( status );
454 }
455 
mbedtls_psa_hash_compute(psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * hash,size_t hash_size,size_t * hash_length)456 psa_status_t mbedtls_psa_hash_compute(
457     psa_algorithm_t alg,
458     const uint8_t *input,
459     size_t input_length,
460     uint8_t *hash,
461     size_t hash_size,
462     size_t *hash_length)
463 {
464     mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
465     psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
466     psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
467 
468     *hash_length = hash_size;
469     status = mbedtls_psa_hash_setup( &operation, alg );
470     if( status != PSA_SUCCESS )
471         goto exit;
472     status = mbedtls_psa_hash_update( &operation, input, input_length );
473     if( status != PSA_SUCCESS )
474         goto exit;
475     status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length );
476     if( status != PSA_SUCCESS )
477         goto exit;
478 
479 exit:
480     abort_status = mbedtls_psa_hash_abort( &operation );
481     if( status == PSA_SUCCESS )
482         return( abort_status );
483     else
484         return( status );
485 
486 }
487 #endif /* MBEDTLS_PSA_BUILTIN_HASH */
488 
489 #endif /* MBEDTLS_PSA_CRYPTO_C */
490