@@ -166,6 +166,16 @@ void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
166
166
if ( ctx == NULL )
167
167
return ;
168
168
169
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
170
+ if ( ctx -> psa_enabled == 1 )
171
+ {
172
+ /* TODO: Add free'ing of PSA-specific context. */
173
+
174
+ mbedtls_platform_zeroize ( ctx , sizeof (mbedtls_cipher_context_t ) );
175
+ return ;
176
+ }
177
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
178
+
169
179
#if defined(MBEDTLS_CMAC_C )
170
180
if ( ctx -> cmac_ctx )
171
181
{
@@ -212,7 +222,14 @@ int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
212
222
int mbedtls_cipher_setup_psa ( mbedtls_cipher_context_t * ctx ,
213
223
const mbedtls_cipher_info_t * cipher_info )
214
224
{
215
- return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
225
+ if ( NULL == cipher_info || NULL == ctx )
226
+ return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
227
+
228
+ memset ( ctx , 0 , sizeof ( mbedtls_cipher_context_t ) );
229
+
230
+ ctx -> cipher_info = cipher_info ;
231
+ ctx -> psa_enabled = 1 ;
232
+ return ( 0 );
216
233
}
217
234
#endif /* MBEDTLS_USE_PSA_CRYPTO */
218
235
@@ -224,6 +241,14 @@ int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
224
241
if ( NULL == ctx || NULL == ctx -> cipher_info )
225
242
return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
226
243
244
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
245
+ if ( ctx -> psa_enabled == 1 )
246
+ {
247
+ /* TODO */
248
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
249
+ }
250
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
251
+
227
252
if ( ( ctx -> cipher_info -> flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
228
253
(int ) ctx -> cipher_info -> key_bitlen != key_bitlen )
229
254
{
@@ -262,6 +287,16 @@ int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
262
287
else if ( NULL == iv && iv_len != 0 )
263
288
return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
264
289
290
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
291
+ if ( ctx -> psa_enabled == 1 )
292
+ {
293
+ /* While PSA Crypto has an API for multipart
294
+ * operations, we currently don't make it
295
+ * accessible through the cipher layer. */
296
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
297
+ }
298
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
299
+
265
300
if ( NULL == iv && iv_len == 0 )
266
301
ctx -> iv_size = 0 ;
267
302
@@ -306,6 +341,15 @@ int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
306
341
if ( NULL == ctx || NULL == ctx -> cipher_info )
307
342
return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
308
343
344
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
345
+ if ( ctx -> psa_enabled == 1 )
346
+ {
347
+ /* We don't support resetting PSA-based
348
+ * cipher contexts, yet. */
349
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
350
+ }
351
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
352
+
309
353
ctx -> unprocessed_len = 0 ;
310
354
311
355
return ( 0 );
@@ -318,6 +362,16 @@ int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
318
362
if ( NULL == ctx || NULL == ctx -> cipher_info )
319
363
return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
320
364
365
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
366
+ if ( ctx -> psa_enabled == 1 )
367
+ {
368
+ /* While PSA Crypto has an API for multipart
369
+ * operations, we currently don't make it
370
+ * accessible through the cipher layer. */
371
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
372
+ }
373
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
374
+
321
375
#if defined(MBEDTLS_GCM_C )
322
376
if ( MBEDTLS_MODE_GCM == ctx -> cipher_info -> mode )
323
377
{
@@ -362,6 +416,16 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
362
416
return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
363
417
}
364
418
419
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
420
+ if ( ctx -> psa_enabled == 1 )
421
+ {
422
+ /* While PSA Crypto has an API for multipart
423
+ * operations, we currently don't make it
424
+ * accessible through the cipher layer. */
425
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
426
+ }
427
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
428
+
365
429
* olen = 0 ;
366
430
block_size = mbedtls_cipher_get_block_size ( ctx );
367
431
@@ -768,6 +832,16 @@ int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
768
832
if ( NULL == ctx || NULL == ctx -> cipher_info || NULL == olen )
769
833
return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
770
834
835
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
836
+ if ( ctx -> psa_enabled == 1 )
837
+ {
838
+ /* While PSA Crypto has an API for multipart
839
+ * operations, we currently don't make it
840
+ * accessible through the cipher layer. */
841
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
842
+ }
843
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
844
+
771
845
* olen = 0 ;
772
846
773
847
if ( MBEDTLS_MODE_CFB == ctx -> cipher_info -> mode ||
@@ -859,6 +933,19 @@ int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
859
933
return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
860
934
}
861
935
936
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
937
+ if ( ctx -> psa_enabled == 1 )
938
+ {
939
+ /* While PSA Crypto knows about CBC padding
940
+ * schemes, we currently don't make them
941
+ * accessible through the cipher layer. */
942
+ if ( mode != MBEDTLS_PADDING_NONE )
943
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
944
+
945
+ return ( 0 );
946
+ }
947
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
948
+
862
949
switch ( mode )
863
950
{
864
951
#if defined(MBEDTLS_CIPHER_PADDING_PKCS7 )
@@ -908,6 +995,18 @@ int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
908
995
if ( MBEDTLS_ENCRYPT != ctx -> operation )
909
996
return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
910
997
998
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
999
+ if ( ctx -> psa_enabled == 1 )
1000
+ {
1001
+ /* While PSA Crypto has an API for multipart
1002
+ * operations, we currently don't make it
1003
+ * accessible through the cipher layer. */
1004
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1005
+
1006
+ return ( 0 );
1007
+ }
1008
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
1009
+
911
1010
#if defined(MBEDTLS_GCM_C )
912
1011
if ( MBEDTLS_MODE_GCM == ctx -> cipher_info -> mode )
913
1012
return ( mbedtls_gcm_finish ( (mbedtls_gcm_context * ) ctx -> cipher_ctx ,
@@ -941,6 +1040,16 @@ int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
941
1040
return ( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
942
1041
}
943
1042
1043
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
1044
+ if ( ctx -> psa_enabled == 1 )
1045
+ {
1046
+ /* While PSA Crypto has an API for multipart
1047
+ * operations, we currently don't make it
1048
+ * accessible through the cipher layer. */
1049
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1050
+ }
1051
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
1052
+
944
1053
#if defined(MBEDTLS_GCM_C )
945
1054
if ( MBEDTLS_MODE_GCM == ctx -> cipher_info -> mode )
946
1055
{
@@ -999,6 +1108,14 @@ int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
999
1108
int ret ;
1000
1109
size_t finish_olen ;
1001
1110
1111
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
1112
+ if ( ctx -> psa_enabled == 1 )
1113
+ {
1114
+ /* TODO */
1115
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1116
+ }
1117
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
1118
+
1002
1119
if ( ( ret = mbedtls_cipher_set_iv ( ctx , iv , iv_len ) ) != 0 )
1003
1120
return ( ret );
1004
1121
@@ -1029,6 +1146,14 @@ int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
1029
1146
unsigned char * output , size_t * olen ,
1030
1147
unsigned char * tag , size_t tag_len )
1031
1148
{
1149
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
1150
+ if ( ctx -> psa_enabled == 1 )
1151
+ {
1152
+ /* TODO */
1153
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1154
+ }
1155
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
1156
+
1032
1157
#if defined(MBEDTLS_GCM_C )
1033
1158
if ( MBEDTLS_MODE_GCM == ctx -> cipher_info -> mode )
1034
1159
{
@@ -1076,6 +1201,14 @@ int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
1076
1201
unsigned char * output , size_t * olen ,
1077
1202
const unsigned char * tag , size_t tag_len )
1078
1203
{
1204
+ #if defined(MBEDTLS_USE_PSA_CRYPTO )
1205
+ if ( ctx -> psa_enabled == 1 )
1206
+ {
1207
+ /* TODO */
1208
+ return ( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
1209
+ }
1210
+ #endif /* MBEDTLS_USE_PSA_CRYPTO */
1211
+
1079
1212
#if defined(MBEDTLS_GCM_C )
1080
1213
if ( MBEDTLS_MODE_GCM == ctx -> cipher_info -> mode )
1081
1214
{
0 commit comments