We have a system that has several SecretKeys (for example, one for a user, and one for us). We want to encrypt data in a way that requires all of those keys to be available in order to decrypt.
I think that the correct way to do this is to use a key derivation function of some sort to merge the source SecretKeys together, and use the result a the encryption key.
Given that all of the source keys are cryptographically generated SecretKeys using the following (apologies in advance if this isn't the right :
KeyGenerator generator = KeyGenerator.getInstance("AES"); generator.init(256); return generator.generateKey();
is it safe to just use a cryptographic hash of the source keys, or is that potentially introducing a vulnerability somehow? Like this:
SecretKey secretKey1 = ... SecretKey secretKey2 = ... SecretKey secretKey3 = ... MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(secretKey1.getEncoded()); md.update(secretKey2.getEncoded()); byte digest = md.digest(secretKey3.getEncoded()); SecretKey mergedSecretKey = new SecretKeySpec(digest, "AES");
The database we are working with doesn't support native encryption, and we are only encrypting a very small number of fields.
Related (but no helpful responses): Using multiple cryptographic keys for encryption?