summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
authorDavid Hook <dgh@bouncycastle.org>2019-01-19 10:38:36 +1100
committerDavid Hook <dgh@bouncycastle.org>2019-01-19 10:38:36 +1100
commitcb3dcd102e7915805bfb3c6d731ee292ebecd3eb (patch)
treed8a3734d1f1a364ba1273fcb391d1ae6ff7cafbf /crypto/src
parentadded use of IKeyWrapper for managing CMS KeyTransRecient (diff)
downloadBouncyCastle.NET-ed25519-cb3dcd102e7915805bfb3c6d731ee292ebecd3eb.tar.xz
added basic wrapper code
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/crypto/SimpleBlockResult.cs51
-rw-r--r--crypto/src/crypto/operators/Asn1KeyWrapper.cs41
2 files changed, 92 insertions, 0 deletions
diff --git a/crypto/src/crypto/SimpleBlockResult.cs b/crypto/src/crypto/SimpleBlockResult.cs
new file mode 100644
index 000000000..01a6c4e01
--- /dev/null
+++ b/crypto/src/crypto/SimpleBlockResult.cs
@@ -0,0 +1,51 @@
+using System;
+
+namespace Org.BouncyCastle.Crypto
+{
+	/// <summary>
+	/// A simple block result object which just carries a byte array.
+	/// </summary>
+	public class SimpleBlockResult: IBlockResult
+	{
+        private readonly bool approvedOnlyMode;
+		private readonly byte[] result;
+
+        /// <summary>
+        /// Base constructor - a wrapper for the passed in byte array.
+        /// </summary>
+        /// <param name="result">The byte array to be wrapped.</param>
+		public SimpleBlockResult (byte[] result)
+		{
+			this.result = result;
+		}
+
+		/// <summary>
+		/// Return the number of bytes in the result
+		/// </summary>
+		/// <value>The length of the result in bytes.</value>
+		public int Length { get { return result.Length; } }
+
+		/// <summary>
+		/// Return the final result of the operation.
+		/// </summary>
+		/// <returns>A block of bytes, representing the result of an operation.</returns>
+		public byte[] Collect()
+		{
+            return result;
+		}
+
+		/// <summary>
+		/// Store the final result of the operation by copying it into the destination array.
+		/// </summary>
+		/// <returns>The number of bytes copied into destination.</returns>
+		/// <param name="destination">The byte array to copy the result into.</param>
+		/// <param name="offset">The offset into destination to start copying the result at.</param>
+		public int Collect(byte[] destination, int offset)
+		{
+            Array.Copy (result, 0, destination, offset, result.Length);
+
+			return result.Length;
+		}
+	}
+}
+
diff --git a/crypto/src/crypto/operators/Asn1KeyWrapper.cs b/crypto/src/crypto/operators/Asn1KeyWrapper.cs
index f710b8f7d..d84af8777 100644
--- a/crypto/src/crypto/operators/Asn1KeyWrapper.cs
+++ b/crypto/src/crypto/operators/Asn1KeyWrapper.cs
@@ -2,6 +2,11 @@
 using System.Collections.Generic;
 using System.Text;
 using Org.BouncyCastle.X509;
+using Org.BouncyCastle.Asn1;
+using Org.BouncyCastle.Asn1.Pkcs;
+using Org.BouncyCastle.Asn1.X509;
+using Org.BouncyCastle.Crypto.Encodings;
+using Org.BouncyCastle.Crypto.Engines;
 
 namespace Org.BouncyCastle.Crypto.Operators
 {
@@ -29,4 +34,40 @@ namespace Org.BouncyCastle.Crypto.Operators
             throw new NotImplementedException();
         }
     }
+
+    internal class RsaOaepWrapper : IKeyWrapper, IKeyUnwrapper
+    {
+        private readonly AlgorithmIdentifier algId;
+        private readonly IAsymmetricBlockCipher engine;
+
+        RsaOaepWrapper(IDigest digest, DerObjectIdentifier digestOid)
+        {
+            AlgorithmIdentifier digestAlgId = new AlgorithmIdentifier(digestOid, DerNull.Instance);
+
+            this.algId = new AlgorithmIdentifier(
+                PkcsObjectIdentifiers.IdRsaesOaep,
+                new RsaesOaepParameters(
+                    digestAlgId,
+                    new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, digestAlgId),
+                    RsaesOaepParameters.DefaultPSourceAlgorithm));
+            this.engine = new OaepEncoding(new RsaBlindedEngine());
+        }
+        public object AlgorithmDetails
+        {
+            get
+            {
+                return algId;
+            }
+        }
+
+        public IBlockResult Unwrap(byte[] cipherText, int offset, int length)
+        {
+            return new SimpleBlockResult(engine.ProcessBlock(cipherText, offset, length));
+        }
+
+        public IBlockResult Wrap(byte[] keyData)
+        {
+            return new SimpleBlockResult(engine.ProcessBlock(keyData, 0, keyData.Length));
+        }
+    }
 }