@@ -1603,14 +1603,15 @@ void psa_crypto_generator_operations(void)
1603
1603
uint8_t * output = NULL ;
1604
1604
size_t output_length = msg .out_size [0 ];
1605
1605
1606
- output = mbedtls_calloc (1 , output_length );
1607
- if (output == NULL ) {
1608
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1609
- break ;
1606
+ if (output_length > 0 ) {
1607
+ output = mbedtls_calloc (1 , output_length );
1608
+ if (output == NULL ) {
1609
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1610
+ break ;
1611
+ }
1610
1612
}
1611
1613
1612
- status = psa_generator_read (msg .rhandle ,
1613
- output , output_length );
1614
+ status = psa_generator_read (msg .rhandle , output , output_length );
1614
1615
if (status == PSA_SUCCESS ) {
1615
1616
psa_write (msg .handle , 0 , output , output_length );
1616
1617
}
@@ -1654,38 +1655,41 @@ void psa_crypto_generator_operations(void)
1654
1655
}
1655
1656
1656
1657
case PSA_KEY_DERIVATION : {
1657
- uint8_t * salt = NULL ;
1658
- uint8_t * label = NULL ;
1658
+ uint8_t * salt = NULL , * label = NULL ;
1659
+ size_t salt_size = msg .in_size [1 ],
1660
+ label_size = msg .in_size [2 ];
1659
1661
1660
- if (!psa_crypto_access_control_is_handle_permitted (psa_crypto_ipc .handle ,
1661
- msg .client_id )) {
1662
+ if (!psa_crypto_access_control_is_handle_permitted (psa_crypto_ipc .handle , msg .client_id )) {
1662
1663
status = PSA_ERROR_INVALID_HANDLE ;
1663
1664
break ;
1664
1665
}
1665
1666
1666
- salt = mbedtls_calloc (1 , msg .in_size [1 ]);
1667
- label = mbedtls_calloc (1 , msg .in_size [2 ]);
1668
- if (salt == NULL || label == NULL ) {
1669
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1670
- } else {
1671
- bytes_read = psa_read (msg .handle , 1 , salt , msg .in_size [1 ]);
1672
- if (bytes_read != msg .in_size [1 ]) {
1673
- SPM_PANIC ("SPM read length mismatch" );
1667
+ if (salt_size > 0 ) {
1668
+ salt = mbedtls_calloc (1 , salt_size );
1669
+ if (salt == NULL ) {
1670
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1671
+ } else {
1672
+ bytes_read = psa_read (msg .handle , 1 , salt , salt_size );
1673
+ if (bytes_read != salt_size ) {
1674
+ SPM_PANIC ("SPM read length mismatch" );
1675
+ }
1674
1676
}
1675
-
1676
- bytes_read = psa_read (msg .handle , 2 , label , msg .in_size [2 ]);
1677
- if (bytes_read != msg .in_size [2 ]) {
1678
- SPM_PANIC ("SPM read length mismatch" );
1677
+ }
1678
+ if (status == PSA_SUCCESS && label_size > 0 ) {
1679
+ label = mbedtls_calloc (1 , label_size );
1680
+ if (label == NULL ) {
1681
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1682
+ } else {
1683
+ bytes_read = psa_read (msg .handle , 2 , label , label_size );
1684
+ if (bytes_read != label_size ) {
1685
+ SPM_PANIC ("SPM read length mismatch" );
1686
+ }
1679
1687
}
1688
+ }
1680
1689
1681
- status = psa_key_derivation (msg .rhandle , psa_crypto_ipc .handle ,
1682
- psa_crypto_ipc .alg ,
1683
- salt ,
1684
- msg .in_size [1 ],//salt length
1685
- label ,
1686
- msg .in_size [2 ],//label length
1687
- psa_crypto_ipc .capacity );
1688
-
1690
+ if (status == PSA_SUCCESS ) {
1691
+ status = psa_key_derivation (msg .rhandle , psa_crypto_ipc .handle , psa_crypto_ipc .alg ,
1692
+ salt , salt_size , label , label_size , psa_crypto_ipc .capacity );
1689
1693
}
1690
1694
1691
1695
mbedtls_free (salt );
@@ -1694,40 +1698,40 @@ void psa_crypto_generator_operations(void)
1694
1698
mbedtls_free (msg .rhandle );
1695
1699
psa_set_rhandle (msg .handle , NULL );
1696
1700
}
1697
-
1698
1701
break ;
1699
1702
}
1700
1703
1701
1704
case PSA_KEY_AGREEMENT : {
1702
1705
uint8_t * private_key = NULL ;
1706
+ size_t private_key_size = msg .in_size [1 ];
1703
1707
1704
- if (!psa_crypto_access_control_is_handle_permitted (psa_crypto_ipc .handle ,
1705
- msg .client_id )) {
1708
+ if (!psa_crypto_access_control_is_handle_permitted (psa_crypto_ipc .handle , msg .client_id )) {
1706
1709
status = PSA_ERROR_INVALID_HANDLE ;
1707
1710
break ;
1708
1711
}
1709
1712
1710
- private_key = mbedtls_calloc (1 , msg .in_size [1 ]);
1711
- if (private_key == NULL ) {
1712
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1713
- } else {
1714
- bytes_read = psa_read (msg .handle , 1 , private_key , msg .in_size [1 ]);
1715
- if (bytes_read != msg .in_size [1 ]) {
1716
- SPM_PANIC ("SPM read length mismatch" );
1713
+ if (private_key_size > 0 ) {
1714
+ private_key = mbedtls_calloc (1 , private_key_size );
1715
+ if (private_key == NULL ) {
1716
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1717
+ } else {
1718
+ bytes_read = psa_read (msg .handle , 1 , private_key , private_key_size );
1719
+ if (bytes_read != private_key_size ) {
1720
+ SPM_PANIC ("SPM read length mismatch" );
1721
+ }
1717
1722
}
1723
+ }
1718
1724
1725
+ if (status == PSA_SUCCESS ) {
1719
1726
status = psa_key_agreement (msg .rhandle , psa_crypto_ipc .handle ,
1720
- private_key ,
1721
- msg .in_size [1 ],//private_key length
1722
- psa_crypto_ipc .alg );
1727
+ private_key , private_key_size , psa_crypto_ipc .alg );
1723
1728
mbedtls_free (private_key );
1724
1729
}
1725
1730
1726
1731
if (status != PSA_SUCCESS ) {
1727
1732
mbedtls_free (msg .rhandle );
1728
1733
psa_set_rhandle (msg .handle , NULL );
1729
1734
}
1730
-
1731
1735
break ;
1732
1736
}
1733
1737
0 commit comments