Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions regarding Capgemini.Cauldron.Cryptography #86

Open
baf1-fr opened this issue Feb 29, 2020 · 0 comments
Open

Questions regarding Capgemini.Cauldron.Cryptography #86

baf1-fr opened this issue Feb 29, 2020 · 0 comments

Comments

@baf1-fr
Copy link

baf1-fr commented Feb 29, 2020

The nuget package "Capgemini.Cauldron.Cryptography" came to my attention. With around 30k downloads I was curious and did take a look.

I really would like some feedback on my thoughts/ideas.

ZipAsBytes

Why are you try to compress data, that shouldn't be compressible? What do you want to archive?

Same samples:

return result.ZipAsBytes();

this.key = keyMaterial.ToArray().ZipAsBytes();

this.key = keyMaterial.GetBytes(32).ZipAsBytes();

Creating a crypto key

If creating a key for hybrid cryptosystem, in my understanding the key should just be created from a cryptographic random number generator. Which is much simpler and faster.

using (var randomPassword = (CryptoUtils.BrewPassword(80) + publicKey + DateTime.Now.ToString()).GetHash(HashAlgorithms.Sha512).ToSecureString())

It would be just simpler to use for salt a cryptographic random number generator too.

new KeyMaterial(password, UTF8Encoding.UTF8.GetBytes(CryptoUtils.BrewPassword(42).GetHash(HashAlgorithms.Sha256)).GetBytes(16), iterations);

Use MD5 as standard hash

Why is the unsafe MD5 the standard hash? MD5 is cryptographically broken and unsuitable for further use.

public static string GetHash(this string target) => target.GetHash(HashAlgorithms.Md5);

Securing a byte array with SecureString

If you want to protect data, take a look to ProtectedMemory or ProtectedData. I think to use SecureString to store an array of byte is not the idea of SecureString. And what it the benefit when the content of SecureString is unwrapped a few statements later?

public static SecureString ToSecureString(this byte[] value)

GC

Calling the GC is in most case a bad sign, maybe not here. But I don't how this in combination with OnDispose could help with security.

this.saltHandle = GCHandle.Alloc(this.salt, GCHandleType.Pinned);

Initialisation Vector

The initialisation vector is part of the key. So I think you disclose information from the key. Instead of the comment "Possibly security critical" it should be fixed or thrown an exception.

this.initialisationVector = keyMaterial.ToArray().GetBytes(16).ZipAsBytes();

Magic Numbers

The code would be easier to read if the magic numbers had descriptive variables.

For example:

Array.Resize(ref result, result.Length + 16);

result = new byte[k.Length + d.Length + 4 + 1];

Authenticated Encryption

I would suggest to use an authenticated encryption (AE) or authenticated encryption with associated data (AEAD) mode for AES. Take a look to System.Security.Cryptography.AesGcm

using (var aes = System.Security.Cryptography.Aes.Create())

This would break backwards combability but it maybe something for the next major version.

Random

For the Seed for Random you could just use Guid.NewGuid().GetHashCode().
This would reduce the complexity and you have the same result.

private readonly static Random local = new Random(GetCryptographicSeed());

private readonly static Random local = new Random(GetCryptographicSeed());
private readonly static Random local = new Random(Guid.NewGuid().GetHashCode());

Cryptographic Message Syntax

You are copying some date from one array to get your parameter for your crypto system. Maybe the Cryptographic Message Syntax is something for you, instead of invent your own message syntax.

Using RsaPkcs1

As I know, you shouldn't use PKCS1 to pad the plaintext. You could use Optimal Asymmetric Encryption Padding (OAEP) see RsaOaepSha512

"In principle, usage of this [RSA with PKCS1v1.5 padding] padding format is encouraged neither for encryption nor for signature applications" See "1.4. Handling of legacy algorithms" in BSI TR-02102-1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant