Skip to content

Commit 7244390

Browse files
committed
[common,crypto] Add crypto provider interface
Signed-off-by: Mykhailo Lohvynenko <[email protected]>
1 parent e8a4f9e commit 7244390

File tree

17 files changed

+192
-115
lines changed

17 files changed

+192
-115
lines changed

include/aos/common/crypto/crypto.hpp

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,33 @@ class RandomItf {
296296
virtual ~RandomItf() = default;
297297
};
298298

299+
/***
300+
* UUID generator interface.
301+
*/
302+
class UUIDItf {
303+
public:
304+
/**
305+
* Creates UUID v4.
306+
*
307+
* @return RetWithError<uuid::UUID>.
308+
*/
309+
virtual RetWithError<uuid::UUID> CreateUUIDv4() = 0;
310+
311+
/**
312+
* Creates UUID version 5 based on a given namespace identifier and name.
313+
*
314+
* @param space namespace identifier.
315+
* @param name name.
316+
* @result RetWithError<uuid::UUID>.
317+
*/
318+
virtual RetWithError<uuid::UUID> CreateUUIDv5(const uuid::UUID& space, const Array<uint8_t>& name) = 0;
319+
320+
/**
321+
* Destructor.
322+
*/
323+
virtual ~UUIDItf() = default;
324+
};
325+
299326
/**
300327
* Options being used while signing.
301328
*/
@@ -700,15 +727,6 @@ class ProviderItf {
700727
*/
701728
virtual Error ASN1DecodeOID(const Array<uint8_t>& inOID, Array<uint8_t>& dst) = 0;
702729

703-
/**
704-
* Creates UUID version 5 based on a given namespace identifier and name.
705-
*
706-
* @param space namespace identifier.
707-
* @param name name.
708-
* @result RetWithError<uuid::UUID>.
709-
*/
710-
virtual RetWithError<uuid::UUID> CreateUUIDv5(const uuid::UUID& space, const Array<uint8_t>& name) = 0;
711-
712730
/**
713731
* Destroys object instance.
714732
*/
@@ -721,6 +739,18 @@ class ProviderItf {
721739
using CertificateChain = StaticArray<Certificate, cCertChainSize>;
722740

723741
} // namespace x509
742+
743+
/**
744+
* Crypto provider interface.
745+
*/
746+
class CryptoProviderItf : public x509::ProviderItf, public HasherItf, public RandomItf, public UUIDItf {
747+
public:
748+
/**
749+
* Destructor.
750+
*/
751+
virtual ~CryptoProviderItf() = default;
752+
};
753+
724754
} // namespace aos::crypto
725755

726756
#endif

include/aos/common/crypto/mbedtls/cryptoprovider.hpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace aos::crypto {
2222
/**
2323
* MbedTLSCryptoProvider provider.
2424
*/
25-
class MbedTLSCryptoProvider : public x509::ProviderItf, public HasherItf, public RandomItf {
25+
class MbedTLSCryptoProvider : public CryptoProviderItf {
2626
public:
2727
/**
2828
* Initializes the object.
@@ -164,15 +164,6 @@ class MbedTLSCryptoProvider : public x509::ProviderItf, public HasherItf, public
164164
*/
165165
Error ASN1DecodeOID(const Array<uint8_t>& inOID, Array<uint8_t>& dst) override;
166166

167-
/**
168-
* Creates UUID version 5 based on a given namespace identifier and name.
169-
*
170-
* @param space namespace identifier.
171-
* @param name name.
172-
* @result RetWithError<uuid::UUID>.
173-
*/
174-
RetWithError<uuid::UUID> CreateUUIDv5(const uuid::UUID& space, const Array<uint8_t>& name) override;
175-
176167
/**
177168
* Creates hash instance.
178169
*
@@ -198,6 +189,22 @@ class MbedTLSCryptoProvider : public x509::ProviderItf, public HasherItf, public
198189
*/
199190
Error RandBuffer(Array<uint8_t>& buffer, size_t size) override;
200191

192+
/**
193+
* Creates UUID v4.
194+
*
195+
* @return RetWithError<uuid::UUID>.
196+
*/
197+
RetWithError<uuid::UUID> CreateUUIDv4() override;
198+
199+
/**
200+
* Creates UUID version 5 based on a given namespace identifier and name.
201+
*
202+
* @param space namespace identifier.
203+
* @param name name.
204+
* @result RetWithError<uuid::UUID>.
205+
*/
206+
RetWithError<uuid::UUID> CreateUUIDv5(const uuid::UUID& space, const Array<uint8_t>& name) override;
207+
201208
private:
202209
class MBedTLSHash : public crypto::HashItf, private NonCopyable {
203210
public:

include/aos/common/crypto/openssl/cryptoprovider.hpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace aos::crypto {
1717
/**
1818
* OpenSSLCryptoProvider provider.
1919
*/
20-
class OpenSSLCryptoProvider : public x509::ProviderItf, public HasherItf, public RandomItf {
20+
class OpenSSLCryptoProvider : public CryptoProviderItf {
2121
public:
2222
/**
2323
* Destructor.
@@ -164,15 +164,6 @@ class OpenSSLCryptoProvider : public x509::ProviderItf, public HasherItf, public
164164
*/
165165
Error ASN1DecodeOID(const Array<uint8_t>& inOID, Array<uint8_t>& dst) override;
166166

167-
/**
168-
* Creates UUID version 5 based on a given namespace identifier and name.
169-
*
170-
* @param space namespace identifier.
171-
* @param name name.
172-
* @result RetWithError<uuid::UUID>.
173-
*/
174-
RetWithError<uuid::UUID> CreateUUIDv5(const uuid::UUID& space, const Array<uint8_t>& name) override;
175-
176167
/**
177168
* Creates hash instance.
178169
*
@@ -198,6 +189,22 @@ class OpenSSLCryptoProvider : public x509::ProviderItf, public HasherItf, public
198189
*/
199190
Error RandBuffer(Array<uint8_t>& buffer, size_t size = 0) override;
200191

192+
/**
193+
* Creates UUID v4.
194+
*
195+
* @return RetWithError<uuid::UUID>.
196+
*/
197+
RetWithError<uuid::UUID> CreateUUIDv4() override;
198+
199+
/**
200+
* Creates UUID version 5 based on a given namespace identifier and name.
201+
*
202+
* @param space namespace identifier.
203+
* @param name name.
204+
* @result RetWithError<uuid::UUID>.
205+
*/
206+
RetWithError<uuid::UUID> CreateUUIDv5(const uuid::UUID& space, const Array<uint8_t>& name) override;
207+
201208
private:
202209
class OpenSSLHash : public crypto::HashItf, private NonCopyable {
203210
public:

include/aos/iam/certmodules/pkcs11/pkcs11.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ class PKCS11Module : public HSMItf {
7373
* @param certType certificate type.
7474
* @param config module configuration.
7575
* @param pkcs11 reference to pkcs11 library context.
76-
* @param x509Provider reference to x509 crypto interface.
76+
* @param cryptoProvider reference to crypto provider interface.
7777
* @return Error.
7878
*/
7979
Error Init(const String& certType, const PKCS11ModuleConfig& config, pkcs11::PKCS11Manager& pkcs11,
80-
crypto::x509::ProviderItf& x509Provider);
80+
crypto::CryptoProviderItf& cryptoProvider);
8181

8282
/**
8383
* Owns the module.
@@ -209,7 +209,7 @@ class PKCS11Module : public HSMItf {
209209
PKCS11ModuleConfig mConfig {};
210210

211211
SharedPtr<pkcs11::LibraryContext> mPKCS11;
212-
crypto::x509::ProviderItf* mX509Provider {};
212+
crypto::CryptoProviderItf* mCryptoProvider {};
213213

214214
uint32_t mSlotID = 0;
215215
StaticString<pkcs11::cLabelLen> mTokenLabel;

include/aos/test/crypto/providers/cryptofactoryitf.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class CryptoFactoryItf {
4444
*
4545
* @return x509::ProviderItf&.
4646
*/
47-
virtual x509::ProviderItf& GetCryptoProvider() = 0;
47+
virtual CryptoProviderItf& GetCryptoProvider() = 0;
4848

4949
/**
5050
* Returns hash provider.

include/aos/test/crypto/providers/mbedtlsfactory.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class MBedTLSCryptoFactory : public CryptoFactoryItf {
4040
/**
4141
* Returns crypto provider.
4242
*
43-
* @return x509::ProviderItf&.
43+
* @return CryptoProviderItf&.
4444
*/
45-
x509::ProviderItf& GetCryptoProvider() override;
45+
CryptoProviderItf& GetCryptoProvider() override;
4646

4747
/**
4848
* Returns hash provider.

include/aos/test/crypto/providers/opensslfactory.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class OpenSSLCryptoFactory : public CryptoFactoryItf {
4040
/**
4141
* Returns crypto provider.
4242
*
43-
* @return x509::ProviderItf&.
43+
* @return CryptoProviderItf&.
4444
*/
45-
x509::ProviderItf& GetCryptoProvider() override;
45+
CryptoProviderItf& GetCryptoProvider() override;
4646

4747
/**
4848
* Returns hash provider.

src/common/crypto/mbedtls/cryptoprovider.cpp

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
604574
RetWithError<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
**********************************************************************************************************************/

src/common/crypto/openssl/cryptoprovider.cpp

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,33 +1487,6 @@ Error OpenSSLCryptoProvider::ASN1DecodeOID(const Array<uint8_t>& inOID, Array<ui
14871487
return ErrorEnum::eNone;
14881488
}
14891489

1490-
RetWithError<uuid::UUID> OpenSSLCryptoProvider::CreateUUIDv5(const uuid::UUID& space, const Array<uint8_t>& name)
1491-
{
1492-
constexpr auto cUUIDVersion = 5;
1493-
1494-
StaticArray<uint8_t, cSHA1InputDataSize> buffer = space;
1495-
1496-
auto err = buffer.Insert(buffer.end(), name.begin(), name.end());
1497-
if (!err.IsNone()) {
1498-
return {{}, AOS_ERROR_WRAP(err)};
1499-
}
1500-
1501-
StaticArray<uint8_t, cSHA1DigestSize> sha1;
1502-
1503-
sha1.Resize(sha1.MaxSize());
1504-
1505-
SHA1(buffer.Get(), buffer.Size(), sha1.Get());
1506-
1507-
// copy lowest 16 bytes
1508-
uuid::UUID result = Array<uint8_t>(sha1.Get(), uuid::cUUIDSize);
1509-
1510-
// The version of the UUID will be the lower 4 bits of cUUIDVersion
1511-
result[6] = (result[6] & 0x0f) | uint8_t((cUUIDVersion & 0xf) << 4);
1512-
result[8] = (result[8] & 0x3f) | 0x80; // RFC 4122 variant
1513-
1514-
return result;
1515-
}
1516-
15171490
RetWithError<UniquePtr<HashItf>> OpenSSLCryptoProvider::CreateHash(Hash algorithm)
15181491
{
15191492
if (algorithm == HashEnum::eNone) {
@@ -1556,6 +1529,50 @@ Error OpenSSLCryptoProvider::RandBuffer(Array<uint8_t>& buffer, size_t size)
15561529
return ErrorEnum::eNone;
15571530
}
15581531

1532+
RetWithError<uuid::UUID> OpenSSLCryptoProvider::CreateUUIDv4()
1533+
{
1534+
constexpr auto cUUIDVersion = 4;
1535+
1536+
uuid::UUID uuid;
1537+
1538+
if (auto err = RandBuffer(uuid, uuid.MaxSize()); !err.IsNone()) {
1539+
return {{}, AOS_ERROR_WRAP(err)};
1540+
}
1541+
1542+
// The version of the UUID will be the lower 4 bits of cUUIDVersion
1543+
uuid[6] = (uuid[6] & 0x0f) | uint8_t((cUUIDVersion & 0xf) << 4);
1544+
uuid[8] = (uuid[8] & 0x3f) | 0x80; // RFC 4122 variant
1545+
1546+
return uuid;
1547+
}
1548+
1549+
RetWithError<uuid::UUID> OpenSSLCryptoProvider::CreateUUIDv5(const uuid::UUID& space, const Array<uint8_t>& name)
1550+
{
1551+
constexpr auto cUUIDVersion = 5;
1552+
1553+
StaticArray<uint8_t, cSHA1InputDataSize> buffer = space;
1554+
1555+
auto err = buffer.Insert(buffer.end(), name.begin(), name.end());
1556+
if (!err.IsNone()) {
1557+
return {{}, AOS_ERROR_WRAP(err)};
1558+
}
1559+
1560+
StaticArray<uint8_t, cSHA1DigestSize> sha1;
1561+
1562+
sha1.Resize(sha1.MaxSize());
1563+
1564+
SHA1(buffer.Get(), buffer.Size(), sha1.Get());
1565+
1566+
// copy lowest 16 bytes
1567+
uuid::UUID result = Array<uint8_t>(sha1.Get(), uuid::cUUIDSize);
1568+
1569+
// The version of the UUID will be the lower 4 bits of cUUIDVersion
1570+
result[6] = (result[6] & 0x0f) | uint8_t((cUUIDVersion & 0xf) << 4);
1571+
result[8] = (result[8] & 0x3f) | 0x80; // RFC 4122 variant
1572+
1573+
return result;
1574+
}
1575+
15591576
/***********************************************************************************************************************
15601577
* Private
15611578
**********************************************************************************************************************/

0 commit comments

Comments
 (0)