summary refs log tree commit diff
path: root/crypto/src/cms
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-04-13 16:48:27 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-04-13 16:48:27 +0700
commita0b75007cc33d1ead75b2aed6439a7b272469bed (patch)
tree4a831e9dfc6fc39b99246610e878bd7917b67c62 /crypto/src/cms
parentGuard against null nonce and clone returned nonce (diff)
downloadBouncyCastle.NET-ed25519-a0b75007cc33d1ead75b2aed6439a7b272469bed.tar.xz
Refactoring around AeadParameters and ParametersWIthIV
Diffstat (limited to 'crypto/src/cms')
-rw-r--r--crypto/src/cms/CMSEnvelopedHelper.cs8
-rw-r--r--crypto/src/cms/PasswordRecipientInfoGenerator.cs20
-rw-r--r--crypto/src/cms/PasswordRecipientInformation.cs13
3 files changed, 33 insertions, 8 deletions
diff --git a/crypto/src/cms/CMSEnvelopedHelper.cs b/crypto/src/cms/CMSEnvelopedHelper.cs
index 40c5e6868..c623ec677 100644
--- a/crypto/src/cms/CMSEnvelopedHelper.cs
+++ b/crypto/src/cms/CMSEnvelopedHelper.cs
@@ -1,4 +1,7 @@
 using System;
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+using System.Buffers;
+#endif
 using System.Collections.Generic;
 using System.IO;
 
@@ -240,7 +243,12 @@ namespace Org.BouncyCastle.Cms
 							|| alg.Equals(CmsEnvelopedGenerator.IdeaCbc)
 							|| alg.Equals(CmsEnvelopedGenerator.Cast5Cbc))
 						{
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+							cipherParameters = ParametersWithIV.Create<byte>(cipherParameters, 8, 0,
+								(bytes, state) => bytes.Fill(state));
+#else
 							cipherParameters = new ParametersWithIV(cipherParameters, new byte[8]);
+#endif
 						}
 					}
 
diff --git a/crypto/src/cms/PasswordRecipientInfoGenerator.cs b/crypto/src/cms/PasswordRecipientInfoGenerator.cs
index 5bbf8f727..1243bea9f 100644
--- a/crypto/src/cms/PasswordRecipientInfoGenerator.cs
+++ b/crypto/src/cms/PasswordRecipientInfoGenerator.cs
@@ -48,17 +48,29 @@ namespace Org.BouncyCastle.Cms
 
 			// Note: In Java build, the IV is automatically generated in JCE layer
 			int ivLength = Platform.StartsWithIgnoreCase(rfc3211WrapperName, "DES") ? 8 : 16;
-			byte[] iv = new byte[ivLength];
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+            var parametersWithIV = ParametersWithIV.Create(keyEncryptionKey, ivLength, random,
+                (bytes, random) => random.NextBytes(bytes));
+#else
+            byte[] iv = new byte[ivLength];
 			random.NextBytes(iv);
 
-			ICipherParameters parameters = new ParametersWithIV(keyEncryptionKey, iv);
-			keyWrapper.Init(true, new ParametersWithRandom(parameters, random));
+			var parametersWithIV = new ParametersWithIV(keyEncryptionKey, iv);
+#endif
+
+            keyWrapper.Init(true, new ParametersWithRandom(parametersWithIV, random));
         	Asn1OctetString encryptedKey = new DerOctetString(
 				keyWrapper.Wrap(keyBytes, 0, keyBytes.Length));
 
 			DerSequence seq = new DerSequence(
 				new DerObjectIdentifier(keyEncryptionKeyOID),
-				new DerOctetString(iv));
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                new DerOctetString(parametersWithIV.IV)
+#else
+                new DerOctetString(iv)
+#endif
+            );
 
 			AlgorithmIdentifier keyEncryptionAlgorithm = new AlgorithmIdentifier(
 				PkcsObjectIdentifiers.IdAlgPwriKek, seq);
diff --git a/crypto/src/cms/PasswordRecipientInformation.cs b/crypto/src/cms/PasswordRecipientInformation.cs
index f629caba6..ff4a20d68 100644
--- a/crypto/src/cms/PasswordRecipientInformation.cs
+++ b/crypto/src/cms/PasswordRecipientInformation.cs
@@ -54,12 +54,17 @@ namespace Org.BouncyCastle.Cms
 				string				cName = CmsEnvelopedHelper.Instance.GetRfc3211WrapperName(kekAlgName);
 				IWrapper			keyWrapper = WrapperUtilities.GetWrapper(cName);
 
-				byte[] iv = Asn1OctetString.GetInstance(kekAlgParams[1]).GetOctets();
+                var iv = Asn1OctetString.GetInstance(kekAlgParams[1]);
 
-				ICipherParameters parameters = ((CmsPbeKey)key).GetEncoded(kekAlgName);
-				parameters = new ParametersWithIV(parameters, iv);
+                ICipherParameters parameters = ((CmsPbeKey)key).GetEncoded(kekAlgName);
 
-				keyWrapper.Init(false, parameters);
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+                parameters = new ParametersWithIV(parameters, iv.GetOctetsSpan());
+#else
+				parameters = new ParametersWithIV(parameters, iv.GetOctets());
+#endif
+
+                keyWrapper.Init(false, parameters);
 
 				KeyParameter sKey = ParameterUtilities.CreateKeyParameter(
 					GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length));