init commit

This commit is contained in:
2025-10-09 09:57:24 +09:00
commit 4d551bd74f
6636 changed files with 1218703 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Kisa;
using Org.BouncyCastle.Asn1.Misc;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Ntt;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Utilities;
public class AlgorithmIdentifierFactory
{
public static readonly DerObjectIdentifier IDEA_CBC = new DerObjectIdentifier("1.3.6.1.4.1.188.7.1.1.2");
public static readonly DerObjectIdentifier CAST5_CBC = new DerObjectIdentifier("1.2.840.113533.7.66.10");
private static readonly short[] rc2Table = new short[256]
{
189, 86, 234, 242, 162, 241, 172, 42, 176, 147,
209, 156, 27, 51, 253, 208, 48, 4, 182, 220,
125, 223, 50, 75, 247, 203, 69, 155, 49, 187,
33, 90, 65, 159, 225, 217, 74, 77, 158, 218,
160, 104, 44, 195, 39, 95, 128, 54, 62, 238,
251, 149, 26, 254, 206, 168, 52, 169, 19, 240,
166, 63, 216, 12, 120, 36, 175, 35, 82, 193,
103, 23, 245, 102, 144, 231, 232, 7, 184, 96,
72, 230, 30, 83, 243, 146, 164, 114, 140, 8,
21, 110, 134, 0, 132, 250, 244, 127, 138, 66,
25, 246, 219, 205, 20, 141, 80, 18, 186, 60,
6, 78, 236, 179, 53, 17, 161, 136, 142, 43,
148, 153, 183, 113, 116, 211, 228, 191, 58, 222,
150, 14, 188, 10, 237, 119, 252, 55, 107, 3,
121, 137, 98, 198, 215, 192, 210, 124, 106, 139,
34, 163, 91, 5, 93, 2, 117, 213, 97, 227,
24, 143, 85, 81, 173, 31, 11, 94, 133, 229,
194, 87, 99, 202, 61, 108, 180, 197, 204, 112,
178, 145, 89, 13, 71, 32, 200, 79, 88, 224,
1, 226, 22, 56, 196, 111, 59, 15, 101, 70,
190, 126, 45, 123, 130, 249, 64, 181, 29, 115,
248, 235, 38, 199, 135, 151, 37, 84, 177, 40,
170, 152, 157, 165, 100, 109, 122, 212, 16, 129,
68, 239, 73, 214, 174, 46, 221, 118, 92, 47,
167, 28, 201, 9, 105, 154, 131, 207, 41, 57,
185, 233, 76, 255, 67, 171
};
public static AlgorithmIdentifier GenerateEncryptionAlgID(DerObjectIdentifier encryptionOID, int keySize, SecureRandom random)
{
if (encryptionOID.Equals(NistObjectIdentifiers.IdAes128Cbc) || encryptionOID.Equals(NistObjectIdentifiers.IdAes192Cbc) || encryptionOID.Equals(NistObjectIdentifiers.IdAes256Cbc) || encryptionOID.Equals(NttObjectIdentifiers.IdCamellia128Cbc) || encryptionOID.Equals(NttObjectIdentifiers.IdCamellia192Cbc) || encryptionOID.Equals(NttObjectIdentifiers.IdCamellia256Cbc) || encryptionOID.Equals(KisaObjectIdentifiers.IdSeedCbc))
{
byte[] array = new byte[16];
random.NextBytes(array);
return new AlgorithmIdentifier(encryptionOID, new DerOctetString(array));
}
if (encryptionOID.Equals(PkcsObjectIdentifiers.DesEde3Cbc) || encryptionOID.Equals(IDEA_CBC) || encryptionOID.Equals(OiwObjectIdentifiers.DesCbc))
{
byte[] array2 = new byte[8];
random.NextBytes(array2);
return new AlgorithmIdentifier(encryptionOID, new DerOctetString(array2));
}
if (encryptionOID.Equals(CAST5_CBC))
{
byte[] array3 = new byte[8];
random.NextBytes(array3);
Cast5CbcParameters parameters = new Cast5CbcParameters(array3, keySize);
return new AlgorithmIdentifier(encryptionOID, parameters);
}
if (encryptionOID.Equals(PkcsObjectIdentifiers.rc4))
{
return new AlgorithmIdentifier(encryptionOID, DerNull.Instance);
}
if (encryptionOID.Equals(PkcsObjectIdentifiers.RC2Cbc))
{
byte[] array4 = new byte[8];
random.NextBytes(array4);
RC2CbcParameter parameters2 = new RC2CbcParameter(rc2Table[128], array4);
return new AlgorithmIdentifier(encryptionOID, parameters2);
}
throw new InvalidOperationException("unable to match algorithm");
}
}

View File

@@ -0,0 +1,125 @@
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Kisa;
using Org.BouncyCastle.Asn1.Misc;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Ntt;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
namespace Org.BouncyCastle.Crypto.Utilities;
public class CipherFactory
{
private static readonly short[] rc2Ekb = new short[256]
{
93, 190, 155, 139, 17, 153, 110, 77, 89, 243,
133, 166, 63, 183, 131, 197, 228, 115, 107, 58,
104, 90, 192, 71, 160, 100, 52, 12, 241, 208,
82, 165, 185, 30, 150, 67, 65, 216, 212, 44,
219, 248, 7, 119, 42, 202, 235, 239, 16, 28,
22, 13, 56, 114, 47, 137, 193, 249, 128, 196,
109, 174, 48, 61, 206, 32, 99, 254, 230, 26,
199, 184, 80, 232, 36, 23, 252, 37, 111, 187,
106, 163, 68, 83, 217, 162, 1, 171, 188, 182,
31, 152, 238, 154, 167, 45, 79, 158, 142, 172,
224, 198, 73, 70, 41, 244, 148, 138, 175, 225,
91, 195, 179, 123, 87, 209, 124, 156, 237, 135,
64, 140, 226, 203, 147, 20, 201, 97, 46, 229,
204, 246, 94, 168, 92, 214, 117, 141, 98, 149,
88, 105, 118, 161, 74, 181, 85, 9, 120, 51,
130, 215, 221, 121, 245, 27, 11, 222, 38, 33,
40, 116, 4, 151, 86, 223, 60, 240, 55, 57,
220, 255, 6, 164, 234, 66, 8, 218, 180, 113,
176, 207, 18, 122, 78, 250, 108, 29, 132, 0,
200, 127, 145, 69, 170, 43, 194, 177, 143, 213,
186, 242, 173, 25, 178, 103, 54, 247, 15, 10,
146, 125, 227, 157, 233, 144, 62, 35, 39, 102,
19, 236, 129, 21, 189, 34, 191, 159, 126, 169,
81, 75, 76, 251, 2, 211, 112, 134, 49, 231,
59, 5, 3, 84, 96, 72, 101, 24, 210, 205,
95, 50, 136, 14, 53, 253
};
private CipherFactory()
{
}
public static object CreateContentCipher(bool forEncryption, ICipherParameters encKey, AlgorithmIdentifier encryptionAlgID)
{
DerObjectIdentifier algorithm = encryptionAlgID.Algorithm;
if (algorithm.Equals(PkcsObjectIdentifiers.rc4))
{
IStreamCipher streamCipher = new RC4Engine();
streamCipher.Init(forEncryption, encKey);
return streamCipher;
}
BufferedBlockCipher bufferedBlockCipher = CreateCipher(encryptionAlgID.Algorithm);
Asn1Object asn1Object = encryptionAlgID.Parameters.ToAsn1Object();
if (asn1Object != null && !(asn1Object is DerNull))
{
if (algorithm.Equals(PkcsObjectIdentifiers.DesEde3Cbc) || algorithm.Equals(AlgorithmIdentifierFactory.IDEA_CBC) || algorithm.Equals(NistObjectIdentifiers.IdAes128Cbc) || algorithm.Equals(NistObjectIdentifiers.IdAes192Cbc) || algorithm.Equals(NistObjectIdentifiers.IdAes256Cbc) || algorithm.Equals(NttObjectIdentifiers.IdCamellia128Cbc) || algorithm.Equals(NttObjectIdentifiers.IdCamellia192Cbc) || algorithm.Equals(NttObjectIdentifiers.IdCamellia256Cbc) || algorithm.Equals(KisaObjectIdentifiers.IdSeedCbc) || algorithm.Equals(OiwObjectIdentifiers.DesCbc))
{
bufferedBlockCipher.Init(forEncryption, new ParametersWithIV(encKey, Asn1OctetString.GetInstance(asn1Object).GetOctets()));
}
else if (algorithm.Equals(AlgorithmIdentifierFactory.CAST5_CBC))
{
Cast5CbcParameters instance = Cast5CbcParameters.GetInstance(asn1Object);
bufferedBlockCipher.Init(forEncryption, new ParametersWithIV(encKey, instance.GetIV()));
}
else
{
if (!algorithm.Equals(PkcsObjectIdentifiers.RC2Cbc))
{
throw new InvalidOperationException("cannot match parameters");
}
RC2CbcParameter instance2 = RC2CbcParameter.GetInstance(asn1Object);
bufferedBlockCipher.Init(forEncryption, new ParametersWithIV(new RC2Parameters(((KeyParameter)encKey).GetKey(), rc2Ekb[instance2.RC2ParameterVersion.IntValue]), instance2.GetIV()));
}
}
else if (algorithm.Equals(PkcsObjectIdentifiers.DesEde3Cbc) || algorithm.Equals(AlgorithmIdentifierFactory.IDEA_CBC) || algorithm.Equals(AlgorithmIdentifierFactory.CAST5_CBC))
{
bufferedBlockCipher.Init(forEncryption, new ParametersWithIV(encKey, new byte[8]));
}
else
{
bufferedBlockCipher.Init(forEncryption, encKey);
}
return bufferedBlockCipher;
}
private static BufferedBlockCipher CreateCipher(DerObjectIdentifier algorithm)
{
IBlockCipher cipher;
if (NistObjectIdentifiers.IdAes128Cbc.Equals(algorithm) || NistObjectIdentifiers.IdAes192Cbc.Equals(algorithm) || NistObjectIdentifiers.IdAes256Cbc.Equals(algorithm))
{
cipher = new CbcBlockCipher(new AesEngine());
}
else if (PkcsObjectIdentifiers.DesEde3Cbc.Equals(algorithm))
{
cipher = new CbcBlockCipher(new DesEdeEngine());
}
else if (OiwObjectIdentifiers.DesCbc.Equals(algorithm))
{
cipher = new CbcBlockCipher(new DesEngine());
}
else if (PkcsObjectIdentifiers.RC2Cbc.Equals(algorithm))
{
cipher = new CbcBlockCipher(new RC2Engine());
}
else
{
if (!MiscObjectIdentifiers.cast5CBC.Equals(algorithm))
{
throw new InvalidOperationException("cannot recognise cipher: " + algorithm);
}
cipher = new CbcBlockCipher(new Cast5Engine());
}
return new PaddedBufferedBlockCipher(cipher, new Pkcs7Padding());
}
}

View File

@@ -0,0 +1,82 @@
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Kisa;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Ntt;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Utilities;
public class CipherKeyGeneratorFactory
{
private CipherKeyGeneratorFactory()
{
}
public static CipherKeyGenerator CreateKeyGenerator(DerObjectIdentifier algorithm, SecureRandom random)
{
if (NistObjectIdentifiers.IdAes128Cbc.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 128);
}
if (NistObjectIdentifiers.IdAes192Cbc.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 192);
}
if (NistObjectIdentifiers.IdAes256Cbc.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 256);
}
if (PkcsObjectIdentifiers.DesEde3Cbc.Equals(algorithm))
{
DesEdeKeyGenerator desEdeKeyGenerator = new DesEdeKeyGenerator();
desEdeKeyGenerator.Init(new KeyGenerationParameters(random, 192));
return desEdeKeyGenerator;
}
if (NttObjectIdentifiers.IdCamellia128Cbc.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 128);
}
if (NttObjectIdentifiers.IdCamellia192Cbc.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 192);
}
if (NttObjectIdentifiers.IdCamellia256Cbc.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 256);
}
if (KisaObjectIdentifiers.IdSeedCbc.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 128);
}
if (AlgorithmIdentifierFactory.CAST5_CBC.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 128);
}
if (OiwObjectIdentifiers.DesCbc.Equals(algorithm))
{
DesKeyGenerator desKeyGenerator = new DesKeyGenerator();
desKeyGenerator.Init(new KeyGenerationParameters(random, 64));
return desKeyGenerator;
}
if (PkcsObjectIdentifiers.rc4.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 128);
}
if (PkcsObjectIdentifiers.RC2Cbc.Equals(algorithm))
{
return CreateCipherKeyGenerator(random, 128);
}
throw new InvalidOperationException("cannot recognise cipher: " + algorithm);
}
private static CipherKeyGenerator CreateCipherKeyGenerator(SecureRandom random, int keySize)
{
CipherKeyGenerator cipherKeyGenerator = new CipherKeyGenerator();
cipherKeyGenerator.Init(new KeyGenerationParameters(random, keySize));
return cipherKeyGenerator;
}
}

View File

@@ -0,0 +1,326 @@
namespace Org.BouncyCastle.Crypto.Utilities;
internal sealed class Pack
{
private Pack()
{
}
internal static void UInt16_To_BE(ushort n, byte[] bs)
{
bs[0] = (byte)(n >> 8);
bs[1] = (byte)n;
}
internal static void UInt16_To_BE(ushort n, byte[] bs, int off)
{
bs[off] = (byte)(n >> 8);
bs[off + 1] = (byte)n;
}
internal static ushort BE_To_UInt16(byte[] bs)
{
uint num = (uint)((bs[0] << 8) | bs[1]);
return (ushort)num;
}
internal static ushort BE_To_UInt16(byte[] bs, int off)
{
uint num = (uint)((bs[off] << 8) | bs[off + 1]);
return (ushort)num;
}
internal static byte[] UInt32_To_BE(uint n)
{
byte[] array = new byte[4];
UInt32_To_BE(n, array, 0);
return array;
}
internal static void UInt32_To_BE(uint n, byte[] bs)
{
bs[0] = (byte)(n >> 24);
bs[1] = (byte)(n >> 16);
bs[2] = (byte)(n >> 8);
bs[3] = (byte)n;
}
internal static void UInt32_To_BE(uint n, byte[] bs, int off)
{
bs[off] = (byte)(n >> 24);
bs[off + 1] = (byte)(n >> 16);
bs[off + 2] = (byte)(n >> 8);
bs[off + 3] = (byte)n;
}
internal static byte[] UInt32_To_BE(uint[] ns)
{
byte[] array = new byte[4 * ns.Length];
UInt32_To_BE(ns, array, 0);
return array;
}
internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off)
{
for (int i = 0; i < ns.Length; i++)
{
UInt32_To_BE(ns[i], bs, off);
off += 4;
}
}
internal static uint BE_To_UInt32(byte[] bs)
{
return (uint)((bs[0] << 24) | (bs[1] << 16) | (bs[2] << 8) | bs[3]);
}
internal static uint BE_To_UInt32(byte[] bs, int off)
{
return (uint)((bs[off] << 24) | (bs[off + 1] << 16) | (bs[off + 2] << 8) | bs[off + 3]);
}
internal static void BE_To_UInt32(byte[] bs, int off, uint[] ns)
{
for (int i = 0; i < ns.Length; i++)
{
ns[i] = BE_To_UInt32(bs, off);
off += 4;
}
}
internal static byte[] UInt64_To_BE(ulong n)
{
byte[] array = new byte[8];
UInt64_To_BE(n, array, 0);
return array;
}
internal static void UInt64_To_BE(ulong n, byte[] bs)
{
UInt32_To_BE((uint)(n >> 32), bs);
UInt32_To_BE((uint)n, bs, 4);
}
internal static void UInt64_To_BE(ulong n, byte[] bs, int off)
{
UInt32_To_BE((uint)(n >> 32), bs, off);
UInt32_To_BE((uint)n, bs, off + 4);
}
internal static byte[] UInt64_To_BE(ulong[] ns)
{
byte[] array = new byte[8 * ns.Length];
UInt64_To_BE(ns, array, 0);
return array;
}
internal static void UInt64_To_BE(ulong[] ns, byte[] bs, int off)
{
for (int i = 0; i < ns.Length; i++)
{
UInt64_To_BE(ns[i], bs, off);
off += 8;
}
}
internal static ulong BE_To_UInt64(byte[] bs)
{
uint num = BE_To_UInt32(bs);
uint num2 = BE_To_UInt32(bs, 4);
return ((ulong)num << 32) | num2;
}
internal static ulong BE_To_UInt64(byte[] bs, int off)
{
uint num = BE_To_UInt32(bs, off);
uint num2 = BE_To_UInt32(bs, off + 4);
return ((ulong)num << 32) | num2;
}
internal static void BE_To_UInt64(byte[] bs, int off, ulong[] ns)
{
for (int i = 0; i < ns.Length; i++)
{
ns[i] = BE_To_UInt64(bs, off);
off += 8;
}
}
internal static void UInt16_To_LE(ushort n, byte[] bs)
{
bs[0] = (byte)n;
bs[1] = (byte)(n >> 8);
}
internal static void UInt16_To_LE(ushort n, byte[] bs, int off)
{
bs[off] = (byte)n;
bs[off + 1] = (byte)(n >> 8);
}
internal static ushort LE_To_UInt16(byte[] bs)
{
uint num = (uint)(bs[0] | (bs[1] << 8));
return (ushort)num;
}
internal static ushort LE_To_UInt16(byte[] bs, int off)
{
uint num = (uint)(bs[off] | (bs[off + 1] << 8));
return (ushort)num;
}
internal static byte[] UInt32_To_LE(uint n)
{
byte[] array = new byte[4];
UInt32_To_LE(n, array, 0);
return array;
}
internal static void UInt32_To_LE(uint n, byte[] bs)
{
bs[0] = (byte)n;
bs[1] = (byte)(n >> 8);
bs[2] = (byte)(n >> 16);
bs[3] = (byte)(n >> 24);
}
internal static void UInt32_To_LE(uint n, byte[] bs, int off)
{
bs[off] = (byte)n;
bs[off + 1] = (byte)(n >> 8);
bs[off + 2] = (byte)(n >> 16);
bs[off + 3] = (byte)(n >> 24);
}
internal static byte[] UInt32_To_LE(uint[] ns)
{
byte[] array = new byte[4 * ns.Length];
UInt32_To_LE(ns, array, 0);
return array;
}
internal static void UInt32_To_LE(uint[] ns, byte[] bs, int off)
{
for (int i = 0; i < ns.Length; i++)
{
UInt32_To_LE(ns[i], bs, off);
off += 4;
}
}
internal static uint LE_To_UInt32(byte[] bs)
{
return (uint)(bs[0] | (bs[1] << 8) | (bs[2] << 16) | (bs[3] << 24));
}
internal static uint LE_To_UInt32(byte[] bs, int off)
{
return (uint)(bs[off] | (bs[off + 1] << 8) | (bs[off + 2] << 16) | (bs[off + 3] << 24));
}
internal static void LE_To_UInt32(byte[] bs, int off, uint[] ns)
{
for (int i = 0; i < ns.Length; i++)
{
ns[i] = LE_To_UInt32(bs, off);
off += 4;
}
}
internal static void LE_To_UInt32(byte[] bs, int bOff, uint[] ns, int nOff, int count)
{
for (int i = 0; i < count; i++)
{
ns[nOff + i] = LE_To_UInt32(bs, bOff);
bOff += 4;
}
}
internal static uint[] LE_To_UInt32(byte[] bs, int off, int count)
{
uint[] array = new uint[count];
for (int i = 0; i < array.Length; i++)
{
array[i] = LE_To_UInt32(bs, off);
off += 4;
}
return array;
}
internal static byte[] UInt64_To_LE(ulong n)
{
byte[] array = new byte[8];
UInt64_To_LE(n, array, 0);
return array;
}
internal static void UInt64_To_LE(ulong n, byte[] bs)
{
UInt32_To_LE((uint)n, bs);
UInt32_To_LE((uint)(n >> 32), bs, 4);
}
internal static void UInt64_To_LE(ulong n, byte[] bs, int off)
{
UInt32_To_LE((uint)n, bs, off);
UInt32_To_LE((uint)(n >> 32), bs, off + 4);
}
internal static byte[] UInt64_To_LE(ulong[] ns)
{
byte[] array = new byte[8 * ns.Length];
UInt64_To_LE(ns, array, 0);
return array;
}
internal static void UInt64_To_LE(ulong[] ns, byte[] bs, int off)
{
for (int i = 0; i < ns.Length; i++)
{
UInt64_To_LE(ns[i], bs, off);
off += 8;
}
}
internal static void UInt64_To_LE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff)
{
for (int i = 0; i < nsLen; i++)
{
UInt64_To_LE(ns[nsOff + i], bs, bsOff);
bsOff += 8;
}
}
internal static ulong LE_To_UInt64(byte[] bs)
{
uint num = LE_To_UInt32(bs);
uint num2 = LE_To_UInt32(bs, 4);
return ((ulong)num2 << 32) | num;
}
internal static ulong LE_To_UInt64(byte[] bs, int off)
{
uint num = LE_To_UInt32(bs, off);
uint num2 = LE_To_UInt32(bs, off + 4);
return ((ulong)num2 << 32) | num;
}
internal static void LE_To_UInt64(byte[] bs, int off, ulong[] ns)
{
for (int i = 0; i < ns.Length; i++)
{
ns[i] = LE_To_UInt64(bs, off);
off += 8;
}
}
internal static void LE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen)
{
for (int i = 0; i < nsLen; i++)
{
ns[nsOff + i] = LE_To_UInt64(bs, bsOff);
bsOff += 8;
}
}
}