@@ -571,36 +571,6 @@ Error MbedTLSCryptoProvider::ASN1DecodeOID(const Array<uint8_t>& inOID, Array<ui
571571 return crypto::ASN1RemoveTag (inOID, dst, MBEDTLS_ASN1_OID);
572572}
573573
574- RetWithError<uuid::UUID> MbedTLSCryptoProvider::CreateUUIDv5 (const uuid::UUID& space, const Array<uint8_t >& name)
575- {
576- constexpr auto cUUIDVersion = 5 ;
577-
578- StaticArray<uint8_t , cSHA1InputDataSize> buffer = space;
579-
580- auto err = buffer.Insert (buffer.end (), name.begin (), name.end ());
581- if (!err.IsNone ()) {
582- return {{}, AOS_ERROR_WRAP (err)};
583- }
584-
585- StaticArray<uint8_t , cSHA1DigestSize> sha1;
586-
587- sha1.Resize (sha1.MaxSize ());
588-
589- int ret = mbedtls_sha1 (buffer.Get (), buffer.Size (), sha1.Get ());
590- if (ret != 0 ) {
591- return {{}, AOS_ERROR_WRAP (ret)};
592- }
593-
594- // copy lowest 16 bytes
595- uuid::UUID result = Array<uint8_t >(sha1.Get (), uuid::cUUIDSize);
596-
597- // The version of the UUID will be the lower 4 bits of cUUIDVersion
598- result[6 ] = (result[6 ] & 0x0f ) | uint8_t ((cUUIDVersion & 0xf ) << 4 );
599- result[8 ] = (result[8 ] & 0x3f ) | 0x80 ; // RFC 4122 variant
600-
601- return result;
602- }
603-
604574RetWithError<UniquePtr<HashItf>> MbedTLSCryptoProvider::CreateHash (Hash algorithm)
605575{
606576 psa_algorithm_t alg = PSA_ALG_SHA3_256;
@@ -674,6 +644,53 @@ Error MbedTLSCryptoProvider::RandBuffer(Array<uint8_t>& buffer, size_t size)
674644 return ErrorEnum::eNone;
675645}
676646
647+ RetWithError<uuid::UUID> MbedTLSCryptoProvider::CreateUUIDv4 ()
648+ {
649+ constexpr auto cUUIDVersion = 4 ;
650+
651+ uuid::UUID uuid;
652+
653+ if (auto err = RandBuffer (uuid, uuid.MaxSize ()); !err.IsNone ()) {
654+ return {{}, AOS_ERROR_WRAP (err)};
655+ }
656+
657+ // The version of the UUID will be the lower 4 bits of cUUIDVersion
658+ uuid[6 ] = (uuid[6 ] & 0x0f ) | uint8_t ((cUUIDVersion & 0xf ) << 4 );
659+ uuid[8 ] = (uuid[8 ] & 0x3f ) | 0x80 ; // RFC 4122 variant
660+
661+ return uuid;
662+ }
663+
664+ RetWithError<uuid::UUID> MbedTLSCryptoProvider::CreateUUIDv5 (const uuid::UUID& space, const Array<uint8_t >& name)
665+ {
666+ constexpr auto cUUIDVersion = 5 ;
667+
668+ StaticArray<uint8_t , cSHA1InputDataSize> buffer = space;
669+
670+ auto err = buffer.Insert (buffer.end (), name.begin (), name.end ());
671+ if (!err.IsNone ()) {
672+ return {{}, AOS_ERROR_WRAP (err)};
673+ }
674+
675+ StaticArray<uint8_t , cSHA1DigestSize> sha1;
676+
677+ sha1.Resize (sha1.MaxSize ());
678+
679+ int ret = mbedtls_sha1 (buffer.Get (), buffer.Size (), sha1.Get ());
680+ if (ret != 0 ) {
681+ return {{}, AOS_ERROR_WRAP (ret)};
682+ }
683+
684+ // copy lowest 16 bytes
685+ uuid::UUID result = Array<uint8_t >(sha1.Get (), uuid::cUUIDSize);
686+
687+ // The version of the UUID will be the lower 4 bits of cUUIDVersion
688+ result[6 ] = (result[6 ] & 0x0f ) | uint8_t ((cUUIDVersion & 0xf ) << 4 );
689+ result[8 ] = (result[8 ] & 0x3f ) | 0x80 ; // RFC 4122 variant
690+
691+ return result;
692+ }
693+
677694/* **********************************************************************************************************************
678695 * Private
679696 **********************************************************************************************************************/
0 commit comments