Add project

Basic formatting applied. Unnecessary comments have been removed. Suspicious code is covered by TODO.
This commit is contained in:
2025-08-12 11:21:10 +09:00
parent bbcbe841a7
commit 33ab055b43
546 changed files with 176950 additions and 0 deletions

View File

@ -0,0 +1,69 @@
using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
public sealed class Gost_28147_89_BlobParams : Asn1Type
{
public Asn1ObjectIdentifier EncryptionParamSet { get; set; }
public Asn1OpenExt ExtElement { get; set; }
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
{
var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;
EncryptionParamSet = null;
ExtElement = null;
var context = new Asn1BerDecodeContext(buffer, elemLength);
var parsedLen = new IntHolder();
if (!context.MatchElemTag(0, 0, ObjectIdentifierTypeCode, parsedLen, false))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
}
EncryptionParamSet = new Asn1ObjectIdentifier();
EncryptionParamSet.Decode(buffer, true, parsedLen.Value);
if (!context.Expired())
{
if (buffer.PeekTag().Equals(0, 0, ObjectIdentifierTypeCode))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1SeqOrderException);
}
ExtElement = new Asn1OpenExt();
while (!context.Expired())
{
ExtElement.DecodeComponent(buffer);
}
}
else
{
ExtElement = null;
}
}
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
{
var len = 0;
if (ExtElement != null)
{
len += ExtElement.Encode(buffer, false);
}
len += EncryptionParamSet.Encode(buffer, true);
if (explicitTagging)
{
len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
}
return len;
}
}
}

View File

@ -0,0 +1,10 @@
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
public static class Gost_28147_89_Constants
{
/// <summary>
/// Алгоритм шифрования ГОСТ 28147-89
/// </summary>
public static readonly OidValue EncryptAlgorithm = OidValue.FromString("1.2.643.2.2.21");
}
}

View File

@ -0,0 +1,82 @@
using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
public sealed class Gost_28147_89_EncryptedKey : Asn1Type
{
public Gost_28147_89_Key EncryptedKey { get; set; }
public Gost_28147_89_Mac MacKey { get; set; }
public Gost_28147_89_Key MaskKey { get; set; }
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
{
var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;
EncryptedKey = null;
MacKey = null;
MaskKey = null;
var context = new Asn1BerDecodeContext(buffer, elemLength);
var parsedLen = new IntHolder();
if (!context.MatchElemTag(0, 0, OctetStringTypeCode, parsedLen, false))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
}
EncryptedKey = new Gost_28147_89_Key();
EncryptedKey.Decode(buffer, true, parsedLen.Value);
if (context.MatchElemTag(0x80, 0, EocTypeCode, parsedLen, true))
{
MaskKey = new Gost_28147_89_Key();
MaskKey.Decode(buffer, false, parsedLen.Value);
}
if (!context.MatchElemTag(0, 0, OctetStringTypeCode, parsedLen, false))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
}
MacKey = new Gost_28147_89_Mac();
MacKey.Decode(buffer, true, parsedLen.Value);
if (MacKey.Length != 4)
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(MacKey.Length), MacKey.Length);
}
}
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
{
var len = 0;
if (MacKey.Length != 4)
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(MacKey.Length), MacKey.Length);
}
len += MacKey.Encode(buffer, true);
if (MaskKey != null)
{
var maskKeyLen = MaskKey.Encode(buffer, false);
len += maskKeyLen;
len += buffer.EncodeTagAndLength(0x80, 0, EocTypeCode, maskKeyLen);
}
len += EncryptedKey.Encode(buffer, true);
if (explicitTagging)
{
len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
}
return len;
}
}
}

View File

@ -0,0 +1,35 @@
using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
public sealed class Gost_28147_89_Iv : Asn1OctetString
{
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
{
base.Decode(buffer, explicitTagging, implicitLength);
if (Length != 8)
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Length), Length);
}
}
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
{
if (Length != 8)
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Length), Length);
}
var len = base.Encode(buffer, false);
if (explicitTagging)
{
len += buffer.EncodeTagAndLength(Tag, len);
}
return len;
}
}
}

View File

@ -0,0 +1,44 @@
using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
public sealed class Gost_28147_89_Key : Asn1OctetString
{
public Gost_28147_89_Key()
{
}
public Gost_28147_89_Key(byte[] data)
: base(data)
{
}
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
{
base.Decode(buffer, explicitTagging, implicitLength);
if (Length != 32)
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Length), Length);
}
}
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
{
if (Length != 32)
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Length), Length);
}
var len = base.Encode(buffer, false);
if (explicitTagging)
{
len += buffer.EncodeTagAndLength(Tag, len);
}
return len;
}
}
}

View File

@ -0,0 +1,151 @@
using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
using System;
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
/// <summary>
/// Информация о зашифрованном ключе ГОСТ 28147-89
/// </summary>
public sealed class Gost_28147_89_KeyExchangeInfo
{
/// <summary>
/// Идентификатор OID параметров шифрования
/// </summary>
public string EncryptionParamSet { get; set; }
/// <summary>
/// Зашифрованный ключ
/// </summary>
public byte[] EncryptedKey { get; set; }
/// <summary>
/// Контрольная сумма зашифрованного ключа (Message Authentication Code, MAC)
/// </summary>
public byte[] Mac { get; set; }
/// <summary>
/// Материал ключа пользователя (User Keying Material, UKM)
/// </summary>
public byte[] Ukm { get; set; }
/// <summary>
/// Зашифровать информацию о ключе
/// </summary>
public void Decode(byte[] data)
{
if (data == null)
{
throw ExceptionUtility.ArgumentNull(nameof(data));
}
try
{
var asnDecoder = new Asn1BerDecodeBuffer(data);
var keyWrap = new Gost_28147_89_KeyWrap();
keyWrap.Decode(asnDecoder);
EncryptionParamSet = keyWrap.EncryptedParams.EncryptionParamSet.Oid.Value;
EncryptedKey = keyWrap.EncryptedKey.EncryptedKey.Value;
Mac = keyWrap.EncryptedKey.MacKey.Value;
Ukm = keyWrap.EncryptedParams.Ukm.Value;
}
catch (Exception exception)
{
throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, nameof(Gost_28147_89_KeyWrap));
}
}
/// <summary>
/// Расшифровать информацию о ключе
/// </summary>
public byte[] Encode()
{
byte[] data;
var keyWrap = new Gost_28147_89_KeyWrap();
try
{
keyWrap.EncryptedKey = new Gost_28147_89_EncryptedKey
{
EncryptedKey = new Gost_28147_89_Key(EncryptedKey),
MacKey = new Gost_28147_89_Mac(Mac)
};
keyWrap.EncryptedParams = new Gost_28147_89_KeyWrapParams
{
EncryptionParamSet = Asn1ObjectIdentifier.FromString(EncryptionParamSet),
Ukm = new Asn1OctetString(Ukm)
};
var asnEncoder = new Asn1BerEncodeBuffer();
keyWrap.Encode(asnEncoder);
data = asnEncoder.MsgCopy;
}
catch (Exception exception)
{
throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, nameof(Gost_28147_89_KeyWrap));
}
return data;
}
/// <summary>
/// Расшифровать идентификатор OID параметров шифрования
/// </summary>
public static string DecodeEncryptionParamSet(byte[] data)
{
if (data == null)
{
throw ExceptionUtility.ArgumentNull(nameof(data));
}
string encryptionParamSet;
try
{
var asnDecoder = new Asn1BerDecodeBuffer(data);
var parameters = new Gost_28147_89_BlobParams();
parameters.Decode(asnDecoder);
encryptionParamSet = parameters.EncryptionParamSet.Oid.Value;
}
catch (Exception exception)
{
throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, typeof(Gost_28147_89_BlobParams).FullName);
}
return encryptionParamSet;
}
/// <summary>
/// Зашифровать идентификатор OID параметров шифрования
/// </summary>
public static byte[] EncodeEncryptionParamSet(string encryptionParamSet)
{
if (encryptionParamSet == null)
{
throw ExceptionUtility.ArgumentNull(nameof(encryptionParamSet));
}
byte[] data;
try
{
var parameters = new Gost_28147_89_BlobParams { EncryptionParamSet = Asn1ObjectIdentifier.FromString(encryptionParamSet) };
var asnEncoder = new Asn1BerEncodeBuffer();
parameters.Encode(asnEncoder);
data = asnEncoder.MsgCopy;
}
catch (Exception exception)
{
throw ExceptionUtility.CryptographicException(exception, Resources.Asn1EncodeError, nameof(Gost_28147_89_BlobParams));
}
return data;
}
}
}

View File

@ -0,0 +1,53 @@
using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
public sealed class Gost_28147_89_KeyWrap : Asn1Type
{
public Gost_28147_89_EncryptedKey EncryptedKey { get; set; }
public Gost_28147_89_KeyWrapParams EncryptedParams { get; set; }
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
{
var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;
EncryptedKey = null;
EncryptedParams = null;
var context = new Asn1BerDecodeContext(buffer, elemLength);
var parsedLen = new IntHolder();
if (!context.MatchElemTag(0, 0x20, SequenceTypeCode, parsedLen, false))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
}
EncryptedKey = new Gost_28147_89_EncryptedKey();
EncryptedKey.Decode(buffer, true, parsedLen.Value);
if (!context.MatchElemTag(0, 0x20, SequenceTypeCode, parsedLen, false))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
}
EncryptedParams = new Gost_28147_89_KeyWrapParams();
EncryptedParams.Decode(buffer, true, parsedLen.Value);
}
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
{
var len = 0;
len += EncryptedParams.Encode(buffer, true);
len += EncryptedKey.Encode(buffer, true);
if (explicitTagging)
{
len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
}
return len;
}
}
}

View File

@ -0,0 +1,66 @@
using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
public sealed class Gost_28147_89_KeyWrapParams : Asn1Type
{
public Asn1ObjectIdentifier EncryptionParamSet { get; set; }
public Asn1OctetString Ukm { get; set; }
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
{
var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;
EncryptionParamSet = null;
Ukm = null;
var context = new Asn1BerDecodeContext(buffer, elemLength);
var parsedLen = new IntHolder();
if (!context.MatchElemTag(0, 0, ObjectIdentifierTypeCode, parsedLen, false))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
}
EncryptionParamSet = new Asn1ObjectIdentifier();
EncryptionParamSet.Decode(buffer, true, parsedLen.Value);
if (context.MatchElemTag(0, 0, OctetStringTypeCode, parsedLen, false))
{
Ukm = new Asn1OctetString();
Ukm.Decode(buffer, true, parsedLen.Value);
if (Ukm.Length != 8)
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Ukm.Length), Ukm.Length);
}
}
}
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
{
var len = 0;
if (Ukm != null)
{
if (Ukm.Length != 8)
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Ukm.Length), Ukm.Length);
}
len += Ukm.Encode(buffer, true);
}
len += EncryptionParamSet.Encode(buffer, true);
if (explicitTagging)
{
len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
}
return len;
}
}
}

View File

@ -0,0 +1,44 @@
using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
public sealed class Gost_28147_89_Mac : Asn1OctetString
{
public Gost_28147_89_Mac()
{
}
public Gost_28147_89_Mac(byte[] data)
: base(data)
{
}
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
{
base.Decode(buffer, explicitTagging, implicitLength);
if ((Length < 1) || (Length > 4))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Length), Length);
}
}
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
{
if ((Length < 1) || (Length > 4))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1ConsVioException, nameof(Length), Length);
}
var len = base.Encode(buffer, false);
if (explicitTagging)
{
len += buffer.EncodeTagAndLength(Tag, len);
}
return len;
}
}
}

View File

@ -0,0 +1,54 @@
using GostCryptography.Asn1.Ber;
using GostCryptography.Properties;
namespace GostCryptography.Asn1.Gost.Gost_28147_89
{
public sealed class Gost_28147_89_Params : Asn1Type
{
public Asn1ObjectIdentifier EncryptionParamSet { get; private set; }
public Gost_28147_89_Iv Iv { get; private set; }
public override void Decode(Asn1BerDecodeBuffer buffer, bool explicitTagging, int implicitLength)
{
var elemLength = explicitTagging ? MatchTag(buffer, Asn1Tag.Sequence) : implicitLength;
EncryptionParamSet = null;
Iv = null;
var context = new Asn1BerDecodeContext(buffer, elemLength);
var parsedLen = new IntHolder();
if (!context.MatchElemTag(0, 0, OctetStringTypeCode, parsedLen, false))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
}
Iv = new Gost_28147_89_Iv();
Iv.Decode(buffer, true, parsedLen.Value);
if (!context.MatchElemTag(0, 0, ObjectIdentifierTypeCode, parsedLen, false))
{
throw ExceptionUtility.CryptographicException(Resources.Asn1MissingRequiredException, buffer.ByteCount);
}
EncryptionParamSet = new Asn1ObjectIdentifier();
EncryptionParamSet.Decode(buffer, true, parsedLen.Value);
}
public override int Encode(Asn1BerEncodeBuffer buffer, bool explicitTagging)
{
var len = 0;
len += EncryptionParamSet.Encode(buffer, true);
len += Iv.Encode(buffer, true);
if (explicitTagging)
{
len += buffer.EncodeTagAndLength(Asn1Tag.Sequence, len);
}
return len;
}
}
}