blob: 8ac21d0cb66436c1bfc2fe637f717abc861ff822 [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
Steven Cooreman0e307642021-02-18 16:18:32 +0100219 switch( operation->alg )
220 {
221#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
222 case PSA_ALG_MD2:
223 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
224 input, input_length );
225 break;
226#endif
227#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
228 case PSA_ALG_MD4:
229 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
230 input, input_length );
231 break;
232#endif
233#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
234 case PSA_ALG_MD5:
235 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
236 input, input_length );
237 break;
238#endif
239#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
240 case PSA_ALG_RIPEMD160:
241 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
242 input, input_length );
243 break;
244#endif
245#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
246 case PSA_ALG_SHA_1:
247 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
248 input, input_length );
249 break;
250#endif
251#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
252 case PSA_ALG_SHA_224:
253 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
254 input, input_length );
255 break;
256#endif
257#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
258 case PSA_ALG_SHA_256:
259 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
260 input, input_length );
261 break;
262#endif
263#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
264 case PSA_ALG_SHA_384:
265 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
266 input, input_length );
267 break;
268#endif
269#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
270 case PSA_ALG_SHA_512:
271 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
272 input, input_length );
273 break;
274#endif
275 default:
276 (void)input;
277 return( PSA_ERROR_BAD_STATE );
278 }
279
280 if( ret != 0 )
281 mbedtls_psa_hash_abort( operation );
282 return( mbedtls_to_psa_error( ret ) );
283}
284
285psa_status_t mbedtls_psa_hash_finish(
286 mbedtls_psa_hash_operation_t *operation,
287 uint8_t *hash,
288 size_t hash_size,
289 size_t *hash_length )
290{
291 psa_status_t status;
292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
293 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
294
295 /* Fill the output buffer with something that isn't a valid hash
296 * (barring an attack on the hash and deliberately-crafted input),
297 * in case the caller doesn't check the return status properly. */
298 *hash_length = hash_size;
299 /* If hash_size is 0 then hash may be NULL and then the
300 * call to memset would have undefined behavior. */
301 if( hash_size != 0 )
302 memset( hash, '!', hash_size );
303
304 if( hash_size < actual_hash_length )
305 {
306 status = PSA_ERROR_BUFFER_TOO_SMALL;
307 goto exit;
308 }
309
310 switch( operation->alg )
311 {
312#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
313 case PSA_ALG_MD2:
314 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
315 break;
316#endif
317#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
318 case PSA_ALG_MD4:
319 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
320 break;
321#endif
322#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
323 case PSA_ALG_MD5:
324 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
325 break;
326#endif
327#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
328 case PSA_ALG_RIPEMD160:
329 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
330 break;
331#endif
332#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
333 case PSA_ALG_SHA_1:
334 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
335 break;
336#endif
337#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
338 case PSA_ALG_SHA_224:
339 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
340 break;
341#endif
342#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
343 case PSA_ALG_SHA_256:
344 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
345 break;
346#endif
347#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
348 case PSA_ALG_SHA_384:
349 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
350 break;
351#endif
352#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
353 case PSA_ALG_SHA_512:
354 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
355 break;
356#endif
357 default:
358 return( PSA_ERROR_BAD_STATE );
359 }
360 status = mbedtls_to_psa_error( ret );
361
362exit:
363 if( status == PSA_SUCCESS )
364 {
365 *hash_length = actual_hash_length;
366 return( mbedtls_psa_hash_abort( operation ) );
367 }
368 else
369 {
370 mbedtls_psa_hash_abort( operation );
371 return( status );
372 }
373}
374
375psa_status_t mbedtls_psa_hash_abort(
376 mbedtls_psa_hash_operation_t *operation )
377{
378 switch( operation->alg )
379 {
380 case 0:
381 /* The object has (apparently) been initialized but it is not
382 * in use. It's ok to call abort on such an object, and there's
383 * nothing to do. */
384 break;
385#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
386 case PSA_ALG_MD2:
387 mbedtls_md2_free( &operation->ctx.md2 );
388 break;
389#endif
390#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
391 case PSA_ALG_MD4:
392 mbedtls_md4_free( &operation->ctx.md4 );
393 break;
394#endif
395#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
396 case PSA_ALG_MD5:
397 mbedtls_md5_free( &operation->ctx.md5 );
398 break;
399#endif
400#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
401 case PSA_ALG_RIPEMD160:
402 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
403 break;
404#endif
405#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
406 case PSA_ALG_SHA_1:
407 mbedtls_sha1_free( &operation->ctx.sha1 );
408 break;
409#endif
410#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
411 case PSA_ALG_SHA_224:
412 mbedtls_sha256_free( &operation->ctx.sha256 );
413 break;
414#endif
415#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
416 case PSA_ALG_SHA_256:
417 mbedtls_sha256_free( &operation->ctx.sha256 );
418 break;
419#endif
420#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
421 case PSA_ALG_SHA_384:
422 mbedtls_sha512_free( &operation->ctx.sha512 );
423 break;
424#endif
425#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
426 case PSA_ALG_SHA_512:
427 mbedtls_sha512_free( &operation->ctx.sha512 );
428 break;
429#endif
430 default:
431 return( PSA_ERROR_BAD_STATE );
432 }
433 operation->alg = 0;
434 return( PSA_SUCCESS );
435}
436
437#endif /* MBEDTLS_PSA_CRYPTO_C */