blob: a73e3f4380e5af1dd0bcbb16d1526cab1e00820c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
using System;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto.Agreement.Kdf;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
namespace Org.BouncyCastle.Crypto.Agreement
{
public class ECDHWithKdfBasicAgreement
: ECDHBasicAgreement
{
private readonly string algorithm;
private readonly IDerivationFunction kdf;
public ECDHWithKdfBasicAgreement(
string algorithm,
IDerivationFunction kdf)
{
if (algorithm == null)
throw new ArgumentNullException("algorithm");
if (kdf == null)
throw new ArgumentNullException("kdf");
this.algorithm = algorithm;
this.kdf = kdf;
}
public override BigInteger CalculateAgreement(
ICipherParameters pubKey)
{
// Note that the ec.KeyAgreement class in JCE only uses kdf in one
// of the engineGenerateSecret methods.
BigInteger result = base.CalculateAgreement(pubKey);
int keySize = GeneratorUtilities.GetDefaultKeySize(algorithm);
DHKdfParameters dhKdfParams = new DHKdfParameters(
new DerObjectIdentifier(algorithm),
keySize,
BigIntToBytes(result));
kdf.Init(dhKdfParams);
byte[] keyBytes = new byte[keySize / 8];
kdf.GenerateBytes(keyBytes, 0, keyBytes.Length);
return new BigInteger(1, keyBytes);
}
private byte[] BigIntToBytes(BigInteger r)
{
int byteLength = X9IntegerConverter.GetByteLength(privKey.Parameters.Curve);
return X9IntegerConverter.IntegerToBytes(r, byteLength);
}
}
}
|