|
| 1 | +package org.bouncycastle.cms.test; |
| 2 | + |
| 3 | +import java.security.SecureRandom; |
| 4 | +import java.security.Security; |
| 5 | + |
| 6 | +import junit.framework.TestCase; |
| 7 | +import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
| 8 | +import org.bouncycastle.asn1.ASN1OctetString; |
| 9 | +import org.bouncycastle.asn1.ASN1Sequence; |
| 10 | +import org.bouncycastle.cms.CMSAlgorithm; |
| 11 | +import org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder; |
| 12 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 13 | +import org.bouncycastle.operator.OutputEncryptor; |
| 14 | +import org.bouncycastle.util.Arrays; |
| 15 | +import org.bouncycastle.util.test.FixedSecureRandom; |
| 16 | + |
| 17 | +/** |
| 18 | + * The SecureRandom passed to {@link JceCMSContentEncryptorBuilder#setSecureRandom} has to drive the |
| 19 | + * content IV / nonce generation, not just RC2. Previously it reached only the RC2 branch of |
| 20 | + * EnvelopedDataHelper.generateParameters, so for every other algorithm - including AES-GCM - the IV |
| 21 | + * came from a default SecureRandom and setSecureRandom() was silently ignored. |
| 22 | + */ |
| 23 | +public class ContentEncryptorRandomTest |
| 24 | + extends TestCase |
| 25 | +{ |
| 26 | + public void setUp() |
| 27 | + { |
| 28 | + if (Security.getProvider("BC") == null) |
| 29 | + { |
| 30 | + Security.addProvider(new BouncyCastleProvider()); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public void testSuppliedRandomDrivesGcmNonce() |
| 35 | + throws Exception |
| 36 | + { |
| 37 | + byte[] nonce = new byte[12]; |
| 38 | + for (int i = 0; i != nonce.length; i++) |
| 39 | + { |
| 40 | + nonce[i] = (byte)(0xC0 + i); |
| 41 | + } |
| 42 | + |
| 43 | + // the parameters for AES-GCM are a SEQUENCE whose first element is the nonce OCTET STRING |
| 44 | + ASN1Sequence params = ASN1Sequence.getInstance(gcmParameters(CMSAlgorithm.AES256_GCM, nonce)); |
| 45 | + |
| 46 | + assertTrue("supplied SecureRandom did not drive the GCM nonce", |
| 47 | + Arrays.areEqual(nonce, ASN1OctetString.getInstance(params.getObjectAt(0)).getOctets())); |
| 48 | + } |
| 49 | + |
| 50 | + public void testSuppliedRandomDrivesCbcIV() |
| 51 | + throws Exception |
| 52 | + { |
| 53 | + byte[] iv = new byte[16]; |
| 54 | + for (int i = 0; i != iv.length; i++) |
| 55 | + { |
| 56 | + iv[i] = (byte)(0xA0 + i); |
| 57 | + } |
| 58 | + |
| 59 | + // the parameters for AES-CBC are the IV as a bare OCTET STRING |
| 60 | + byte[] emitted = ASN1OctetString.getInstance( |
| 61 | + gcmParameters(CMSAlgorithm.AES256_CBC, iv)).getOctets(); |
| 62 | + |
| 63 | + assertTrue("supplied SecureRandom did not drive the CBC IV", Arrays.areEqual(iv, emitted)); |
| 64 | + } |
| 65 | + |
| 66 | + private static org.bouncycastle.asn1.ASN1Encodable gcmParameters(ASN1ObjectIdentifier alg, byte[] ivMaterial) |
| 67 | + throws Exception |
| 68 | + { |
| 69 | + // a supplied key means the SecureRandom is consumed only for the IV / nonce, so a |
| 70 | + // FixedSecureRandom emitting exactly ivMaterial makes the emitted parameters deterministic |
| 71 | + SecureRandom fixed = new FixedSecureRandom(ivMaterial); |
| 72 | + |
| 73 | + OutputEncryptor enc = new JceCMSContentEncryptorBuilder(alg) |
| 74 | + .setProvider("BC") |
| 75 | + .setSecureRandom(fixed) |
| 76 | + .build(new byte[32]); |
| 77 | + |
| 78 | + return enc.getAlgorithmIdentifier().getParameters(); |
| 79 | + } |
| 80 | +} |
0 commit comments