Skip to content

Commit b249a50

Browse files
author
Oren Novotny
committed
merge from master
2 parents d311746 + c6f976f commit b249a50

30 files changed

+581
-218
lines changed

crypto/crypto.csproj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,11 @@
568568
SubType = "Code"
569569
BuildAction = "Compile"
570570
/>
571+
<File
572+
RelPath = "src\asn1\bsi\BsiObjectIdentifiers.cs"
573+
SubType = "Code"
574+
BuildAction = "Compile"
575+
/>
571576
<File
572577
RelPath = "src\asn1\cmp\CAKeyUpdAnnContent.cs"
573578
SubType = "Code"
@@ -3098,6 +3103,11 @@
30983103
SubType = "Code"
30993104
BuildAction = "Compile"
31003105
/>
3106+
<File
3107+
RelPath = "src\crypto\IDsaExt.cs"
3108+
SubType = "Code"
3109+
BuildAction = "Compile"
3110+
/>
31013111
<File
31023112
RelPath = "src\crypto\IEntropySource.cs"
31033113
SubType = "Code"
@@ -4783,6 +4793,11 @@
47834793
SubType = "Code"
47844794
BuildAction = "Compile"
47854795
/>
4796+
<File
4797+
RelPath = "src\crypto\signers\IDsaEncoding.cs"
4798+
SubType = "Code"
4799+
BuildAction = "Compile"
4800+
/>
47864801
<File
47874802
RelPath = "src\crypto\signers\IDsaKCalculator.cs"
47884803
SubType = "Code"
@@ -4803,6 +4818,11 @@
48034818
SubType = "Code"
48044819
BuildAction = "Compile"
48054820
/>
4821+
<File
4822+
RelPath = "src\crypto\signers\PlainDsaEncoding.cs"
4823+
SubType = "Code"
4824+
BuildAction = "Compile"
4825+
/>
48064826
<File
48074827
RelPath = "src\crypto\signers\PSSSigner.cs"
48084828
SubType = "Code"
@@ -4823,6 +4843,11 @@
48234843
SubType = "Code"
48244844
BuildAction = "Compile"
48254845
/>
4846+
<File
4847+
RelPath = "src\crypto\signers\StandardDsaEncoding.cs"
4848+
SubType = "Code"
4849+
BuildAction = "Compile"
4850+
/>
48264851
<File
48274852
RelPath = "src\crypto\signers\X931Signer.cs"
48284853
SubType = "Code"
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
3+
namespace Org.BouncyCastle.Asn1.Bsi
4+
{
5+
/// <remarks>See https://www.bsi.bund.de/cae/servlet/contentblob/471398/publicationFile/30615/BSI-TR-03111_pdf.pdf</remarks>
6+
public abstract class BsiObjectIdentifiers
7+
{
8+
public static readonly DerObjectIdentifier bsi_de = new DerObjectIdentifier("0.4.0.127.0.7");
9+
10+
/* 0.4.0.127.0.7.1.1 */
11+
public static readonly DerObjectIdentifier id_ecc = bsi_de.Branch("1.1");
12+
13+
/* 0.4.0.127.0.7.1.1.4.1 */
14+
public static readonly DerObjectIdentifier ecdsa_plain_signatures = id_ecc.Branch("4.1");
15+
16+
/* 0.4.0.127.0.7.1.1.4.1.1 */
17+
public static readonly DerObjectIdentifier ecdsa_plain_SHA1 = ecdsa_plain_signatures.Branch("1");
18+
19+
/* 0.4.0.127.0.7.1.1.4.1.2 */
20+
public static readonly DerObjectIdentifier ecdsa_plain_SHA224 = ecdsa_plain_signatures.Branch("2");
21+
22+
/* 0.4.0.127.0.7.1.1.4.1.3 */
23+
public static readonly DerObjectIdentifier ecdsa_plain_SHA256 = ecdsa_plain_signatures.Branch("3");
24+
25+
/* 0.4.0.127.0.7.1.1.4.1.4 */
26+
public static readonly DerObjectIdentifier ecdsa_plain_SHA384 = ecdsa_plain_signatures.Branch("4");
27+
28+
/* 0.4.0.127.0.7.1.1.4.1.5 */
29+
public static readonly DerObjectIdentifier ecdsa_plain_SHA512 = ecdsa_plain_signatures.Branch("5");
30+
31+
/* 0.4.0.127.0.7.1.1.4.1.6 */
32+
public static readonly DerObjectIdentifier ecdsa_plain_RIPEMD160 = ecdsa_plain_signatures.Branch("6");
33+
34+
/** 0.4.0.127.0.7.1 */
35+
public static readonly DerObjectIdentifier algorithm = bsi_de.Branch("1");
36+
37+
public static readonly DerObjectIdentifier ecka_eg = id_ecc.Branch("5.1");
38+
39+
/** ElGamal Elliptic Curve Key Agreement and Key Derivation according to X963 OID: 0.4.0.127.0.7.1.1.5.1.1 */
40+
public static readonly DerObjectIdentifier ecka_eg_X963kdf = ecka_eg.Branch("1");
41+
42+
/** ElGamal Elliptic Curve Key Agreement and Key Derivation according to X963
43+
* with hash function SHA-1
44+
* OID: 0.4.0.127.0.7.1.1.5.1.1.1 */
45+
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA1 = ecka_eg_X963kdf.Branch("1");
46+
47+
/** ElGamal Elliptic Curve Key Agreement and Key Derivation according to X963
48+
* with hash function SHA224
49+
* OID: 0.4.0.127.0.7.1.1.5.1.1.2 */
50+
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA224 = ecka_eg_X963kdf.Branch("2");
51+
52+
/** ElGamal Elliptic Curve Key Agreement and Key Derivation according to X963
53+
* with hash function SHA256
54+
* OID: 0.4.0.127.0.7.1.1.5.1.1.3 */
55+
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA256 = ecka_eg_X963kdf.Branch("3");
56+
57+
/** ElGamal Elliptic Curve Key Agreement and Key Derivation according to X963
58+
* with hash function SHA384
59+
* OID: 0.4.0.127.0.7.1.1.5.1.1.4 */
60+
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA384 = ecka_eg_X963kdf.Branch("4");
61+
62+
/** ElGamal Elliptic Curve Key Agreement and Key Derivation according to X963
63+
* with hash function SHA512
64+
* OID: 0.4.0.127.0.7.1.1.5.1.1.5 */
65+
public static readonly DerObjectIdentifier ecka_eg_X963kdf_SHA512 = ecka_eg_X963kdf.Branch("5");
66+
67+
/** ElGamal Elliptic Curve Key Agreement and Key Derivation according to X963
68+
* with hash function RIPEMD160
69+
* OID: 0.4.0.127.0.7.1.1.5.1.1.6 */
70+
public static readonly DerObjectIdentifier ecka_eg_X963kdf_RIPEMD160 = ecka_eg_X963kdf.Branch("6");
71+
72+
/**
73+
* Key Derivation Function for Session Keys
74+
*/
75+
public static readonly DerObjectIdentifier ecka_eg_SessionKDF = ecka_eg.Branch("2");
76+
77+
public static readonly DerObjectIdentifier ecka_eg_SessionKDF_3DES = ecka_eg_SessionKDF.Branch("1");
78+
public static readonly DerObjectIdentifier ecka_eg_SessionKDF_AES128 = ecka_eg_SessionKDF.Branch("2");
79+
public static readonly DerObjectIdentifier ecka_eg_SessionKDF_AES192 = ecka_eg_SessionKDF.Branch("3");
80+
public static readonly DerObjectIdentifier ecka_eg_SessionKDF_AES256 = ecka_eg_SessionKDF.Branch("4");
81+
82+
/** AES encryption (CBC) and authentication (CMAC)
83+
* OID: 0.4.0.127.0.7.1.x */
84+
//TODO: replace "1" with correct OID
85+
//public static readonly DerObjectIdentifier aes_cbc_cmac = algorithm.Branch("1");
86+
87+
/** AES encryption (CBC) and authentication (CMAC) with 128 bit
88+
* OID: 0.4.0.127.0.7.1.x.y1 */
89+
//TODO: replace "1" with correct OID
90+
//public static readonly DerObjectIdentifier id_aes128_CBC_CMAC = aes_cbc_cmac.Branch("1");
91+
92+
93+
/** AES encryption (CBC) and authentication (CMAC) with 192 bit
94+
* OID: 0.4.0.127.0.7.1.x.y2 */
95+
//TODO: replace "1" with correct OID
96+
//public static readonly DerObjectIdentifier id_aes192_CBC_CMAC = aes_cbc_cmac.Branch("1");
97+
98+
/** AES encryption (CBC) and authentication (CMAC) with 256 bit
99+
* OID: 0.4.0.127.0.7.1.x.y3 */
100+
//TODO: replace "1" with correct OID
101+
//public static readonly DerObjectIdentifier id_aes256_CBC_CMAC = aes_cbc_cmac.Branch("1");
102+
}
103+
}

crypto/src/crypto/IDSA.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
23
using Org.BouncyCastle.Math;
34

45
namespace Org.BouncyCastle.Crypto

crypto/src/crypto/IDsaExt.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
using Org.BouncyCastle.Math;
4+
5+
namespace Org.BouncyCastle.Crypto
6+
{
7+
/// <summary>
8+
/// An "extended" interface for classes implementing DSA-style algorithms, that provides access
9+
/// to the group order.
10+
/// </summary>
11+
public interface IDsaExt
12+
: IDsa
13+
{
14+
/// <summary>The order of the group that the r, s values in signatures belong to.</summary>
15+
BigInteger Order { get; }
16+
}
17+
}

crypto/src/crypto/parameters/Ed25519PrivateKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class Ed25519PrivateKeyParameters
1919
public Ed25519PrivateKeyParameters(SecureRandom random)
2020
: base(true)
2121
{
22-
random.NextBytes(data);
22+
Ed25519.GeneratePrivateKey(random, data);
2323
}
2424

2525
public Ed25519PrivateKeyParameters(byte[] buf, int off)

crypto/src/crypto/parameters/Ed448PrivateKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class Ed448PrivateKeyParameters
1919
public Ed448PrivateKeyParameters(SecureRandom random)
2020
: base(true)
2121
{
22-
random.NextBytes(data);
22+
Ed448.GeneratePrivateKey(random, data);
2323
}
2424

2525
public Ed448PrivateKeyParameters(byte[] buf, int off)

crypto/src/crypto/parameters/X25519KeyGenerationParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class X25519KeyGenerationParameters
88
: KeyGenerationParameters
99
{
1010
public X25519KeyGenerationParameters(SecureRandom random)
11-
: base(random, 256)
11+
: base(random, 255)
1212
{
1313
}
1414
}

crypto/src/crypto/parameters/X25519PrivateKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class X25519PrivateKeyParameters
1919
public X25519PrivateKeyParameters(SecureRandom random)
2020
: base(true)
2121
{
22-
random.NextBytes(data);
22+
X25519.GeneratePrivateKey(random, data);
2323
}
2424

2525
public X25519PrivateKeyParameters(byte[] buf, int off)

crypto/src/crypto/parameters/X448PrivateKeyParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class X448PrivateKeyParameters
1919
public X448PrivateKeyParameters(SecureRandom random)
2020
: base(true)
2121
{
22-
random.NextBytes(data);
22+
X448.GeneratePrivateKey(random, data);
2323
}
2424

2525
public X448PrivateKeyParameters(byte[] buf, int off)

crypto/src/crypto/signers/DsaDigestSigner.cs

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
using System;
2-
using System.Collections;
3-
using System.IO;
4-
using System.Text;
52

6-
using Org.BouncyCastle.Asn1;
7-
using Org.BouncyCastle.Crypto.Signers;
83
using Org.BouncyCastle.Crypto.Parameters;
94
using Org.BouncyCastle.Math;
105
using Org.BouncyCastle.Security;
@@ -14,26 +9,38 @@ namespace Org.BouncyCastle.Crypto.Signers
149
public class DsaDigestSigner
1510
: ISigner
1611
{
17-
private readonly IDigest digest;
18-
private readonly IDsa dsaSigner;
19-
private bool forSigning;
12+
private readonly IDsa dsa;
13+
private readonly IDigest digest;
14+
private readonly IDsaEncoding encoding;
15+
private bool forSigning;
2016

2117
public DsaDigestSigner(
22-
IDsa signer,
18+
IDsa dsa,
2319
IDigest digest)
2420
{
25-
this.digest = digest;
26-
this.dsaSigner = signer;
21+
this.dsa = dsa;
22+
this.digest = digest;
23+
this.encoding = StandardDsaEncoding.Instance;
2724
}
2825

26+
public DsaDigestSigner(
27+
IDsaExt dsa,
28+
IDigest digest,
29+
IDsaEncoding encoding)
30+
{
31+
this.dsa = dsa;
32+
this.digest = digest;
33+
this.encoding = encoding;
34+
}
35+
2936
public virtual string AlgorithmName
3037
{
31-
get { return digest.AlgorithmName + "with" + dsaSigner.AlgorithmName; }
38+
get { return digest.AlgorithmName + "with" + dsa.AlgorithmName; }
3239
}
3340

3441
public virtual void Init(
35-
bool forSigning,
36-
ICipherParameters parameters)
42+
bool forSigning,
43+
ICipherParameters parameters)
3744
{
3845
this.forSigning = forSigning;
3946

@@ -56,7 +63,7 @@ public virtual void Init(
5663

5764
Reset();
5865

59-
dsaSigner.Init(forSigning, parameters);
66+
dsa.Init(forSigning, parameters);
6067
}
6168

6269
/**
@@ -91,9 +98,16 @@ public virtual byte[] GenerateSignature()
9198
byte[] hash = new byte[digest.GetDigestSize()];
9299
digest.DoFinal(hash, 0);
93100

94-
BigInteger[] sig = dsaSigner.GenerateSignature(hash);
101+
BigInteger[] sig = dsa.GenerateSignature(hash);
95102

96-
return DerEncode(sig[0], sig[1]);
103+
try
104+
{
105+
return encoding.Encode(GetOrder(), sig[0], sig[1]);
106+
}
107+
catch (Exception)
108+
{
109+
throw new InvalidOperationException("unable to encode signature");
110+
}
97111
}
98112

99113
/// <returns>true if the internal state represents the signature described in the passed in array.</returns>
@@ -106,15 +120,16 @@ public virtual bool VerifySignature(
106120
byte[] hash = new byte[digest.GetDigestSize()];
107121
digest.DoFinal(hash, 0);
108122

109-
try
110-
{
111-
BigInteger[] sig = DerDecode(signature);
112-
return dsaSigner.VerifySignature(hash, sig[0], sig[1]);
113-
}
114-
catch (IOException)
115-
{
116-
return false;
117-
}
123+
try
124+
{
125+
BigInteger[] sig = encoding.Decode(GetOrder(), signature);
126+
127+
return dsa.VerifySignature(hash, sig[0], sig[1]);
128+
}
129+
catch (Exception e)
130+
{
131+
return false;
132+
}
118133
}
119134

120135
/// <summary>Reset the internal state</summary>
@@ -123,23 +138,9 @@ public virtual void Reset()
123138
digest.Reset();
124139
}
125140

126-
private byte[] DerEncode(
127-
BigInteger r,
128-
BigInteger s)
129-
{
130-
return new DerSequence(new DerInteger(r), new DerInteger(s)).GetDerEncoded();
131-
}
132-
133-
private BigInteger[] DerDecode(
134-
byte[] encoding)
135-
{
136-
Asn1Sequence s = (Asn1Sequence) Asn1Object.FromByteArray(encoding);
137-
138-
return new BigInteger[]
139-
{
140-
((DerInteger) s[0]).Value,
141-
((DerInteger) s[1]).Value
142-
};
143-
}
141+
protected virtual BigInteger GetOrder()
142+
{
143+
return dsa is IDsaExt ? ((IDsaExt)dsa).Order : null;
144+
}
144145
}
145146
}

0 commit comments

Comments
 (0)