Skip to content

Commit afd619f

Browse files
author
gefeili
committed
Merge branch 'main' into 1958-aead-parameters
# Conflicts: # core/src/test/java/org/bouncycastle/crypto/test/RegressionTest.java
2 parents f31874d + 0f08ecd commit afd619f

File tree

30 files changed

+2051
-219
lines changed

30 files changed

+2051
-219
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.bouncycastle.asn1.x509;
2+
3+
import org.bouncycastle.asn1.ASN1EncodableVector;
4+
import org.bouncycastle.asn1.ASN1Object;
5+
import org.bouncycastle.asn1.ASN1Primitive;
6+
import org.bouncycastle.asn1.ASN1Sequence;
7+
import org.bouncycastle.asn1.DERSequence;
8+
import org.bouncycastle.asn1.pkcs.IssuerAndSerialNumber;
9+
10+
/**
11+
* <pre>
12+
* PrivateKeyStatement ::= SEQUENCE {
13+
* signer IssuerAndSerialNumber,
14+
* cert Certificate OPTIONAL }
15+
* </pre>
16+
*/
17+
public class PrivateKeyStatement
18+
extends ASN1Object
19+
{
20+
private final IssuerAndSerialNumber signer;
21+
private final Certificate cert;
22+
23+
public static PrivateKeyStatement getInstance(Object obj)
24+
{
25+
if (obj instanceof PrivateKeyStatement)
26+
{
27+
return (PrivateKeyStatement)obj;
28+
}
29+
30+
if (obj != null)
31+
{
32+
return new PrivateKeyStatement(ASN1Sequence.getInstance(obj));
33+
}
34+
35+
return null;
36+
}
37+
38+
private PrivateKeyStatement(ASN1Sequence seq)
39+
{
40+
if (seq.size() == 1)
41+
{
42+
this.signer = IssuerAndSerialNumber.getInstance(seq.getObjectAt(0));
43+
this.cert = null;
44+
}
45+
else if (seq.size() == 2)
46+
{
47+
this.signer = IssuerAndSerialNumber.getInstance(seq.getObjectAt(0));
48+
this.cert = Certificate.getInstance(seq.getObjectAt(1));
49+
}
50+
else
51+
{
52+
throw new IllegalArgumentException("unknown sequence in PrivateKeyStatement");
53+
}
54+
}
55+
56+
public PrivateKeyStatement(IssuerAndSerialNumber signer)
57+
{
58+
this.signer = signer;
59+
this.cert = null;
60+
}
61+
62+
public PrivateKeyStatement(Certificate cert)
63+
{
64+
this.signer = new IssuerAndSerialNumber(cert.getIssuer(), cert.getSerialNumber().getValue());
65+
this.cert = cert;
66+
}
67+
68+
public IssuerAndSerialNumber getSigner()
69+
{
70+
return signer;
71+
}
72+
73+
public Certificate getCert()
74+
{
75+
return cert;
76+
}
77+
78+
public ASN1Primitive toASN1Primitive()
79+
{
80+
ASN1EncodableVector v = new ASN1EncodableVector(2);
81+
82+
v.add(signer);
83+
84+
if (cert != null)
85+
{
86+
v.add(cert);
87+
}
88+
89+
return new DERSequence(v);
90+
}
91+
}

core/src/main/java/org/bouncycastle/asn1/x509/X509AttributeIdentifiers.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ public interface X509AttributeIdentifiers
77
/**
88
* @deprecated use id_at_role
99
*/
10-
static final ASN1ObjectIdentifier RoleSyntax = new ASN1ObjectIdentifier("2.5.4.72");
10+
ASN1ObjectIdentifier RoleSyntax = new ASN1ObjectIdentifier("2.5.4.72");
1111

12-
static final ASN1ObjectIdentifier id_pe_ac_auditIdentity = X509ObjectIdentifiers.id_pe.branch("4");
13-
static final ASN1ObjectIdentifier id_pe_aaControls = X509ObjectIdentifiers.id_pe.branch("6");
14-
static final ASN1ObjectIdentifier id_pe_ac_proxying = X509ObjectIdentifiers.id_pe.branch("10");
12+
ASN1ObjectIdentifier id_pe_ac_auditIdentity = X509ObjectIdentifiers.id_pe.branch("4");
13+
ASN1ObjectIdentifier id_pe_aaControls = X509ObjectIdentifiers.id_pe.branch("6");
14+
ASN1ObjectIdentifier id_pe_ac_proxying = X509ObjectIdentifiers.id_pe.branch("10");
1515

16-
static final ASN1ObjectIdentifier id_ce_targetInformation= X509ObjectIdentifiers.id_ce.branch("55");
16+
ASN1ObjectIdentifier id_ce_targetInformation = X509ObjectIdentifiers.id_ce.branch("55");
1717

18-
static final ASN1ObjectIdentifier id_aca = X509ObjectIdentifiers.id_pkix.branch("10");
18+
ASN1ObjectIdentifier id_aca = X509ObjectIdentifiers.id_pkix.branch("10");
1919

20-
static final ASN1ObjectIdentifier id_aca_authenticationInfo = id_aca.branch("1");
21-
static final ASN1ObjectIdentifier id_aca_accessIdentity = id_aca.branch("2");
22-
static final ASN1ObjectIdentifier id_aca_chargingIdentity = id_aca.branch("3");
23-
static final ASN1ObjectIdentifier id_aca_group = id_aca.branch("4");
20+
ASN1ObjectIdentifier id_aca_authenticationInfo = id_aca.branch("1");
21+
ASN1ObjectIdentifier id_aca_accessIdentity = id_aca.branch("2");
22+
ASN1ObjectIdentifier id_aca_chargingIdentity = id_aca.branch("3");
23+
ASN1ObjectIdentifier id_aca_group = id_aca.branch("4");
2424
// { id-aca 5 } is reserved
25-
static final ASN1ObjectIdentifier id_aca_encAttrs = id_aca.branch("6");
25+
ASN1ObjectIdentifier id_aca_encAttrs = id_aca.branch("6");
2626

27-
static final ASN1ObjectIdentifier id_at_role = new ASN1ObjectIdentifier("2.5.4.72");
28-
static final ASN1ObjectIdentifier id_at_clearance = new ASN1ObjectIdentifier("2.5.1.5.55");
27+
ASN1ObjectIdentifier id_at_role = new ASN1ObjectIdentifier("2.5.4.72");
28+
ASN1ObjectIdentifier id_at_clearance = new ASN1ObjectIdentifier("2.5.1.5.55");
29+
30+
ASN1ObjectIdentifier id_at_privateKeyStatement = new ASN1ObjectIdentifier("1.3.6.1.4.1.22112.2.1");
2931
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.bouncycastle.crypto.generators;
2+
3+
import java.math.BigInteger;
4+
import java.security.SecureRandom;
5+
6+
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
7+
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;
8+
import org.bouncycastle.crypto.CryptoServicePurpose;
9+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
10+
import org.bouncycastle.crypto.Digest;
11+
import org.bouncycastle.crypto.KeyGenerationParameters;
12+
import org.bouncycastle.crypto.constraints.DefaultServiceProperties;
13+
import org.bouncycastle.crypto.params.ECCSIKeyGenerationParameters;
14+
import org.bouncycastle.crypto.params.ECCSIPrivateKeyParameters;
15+
import org.bouncycastle.crypto.params.ECCSIPublicKeyParameters;
16+
import org.bouncycastle.math.ec.ECPoint;
17+
import org.bouncycastle.util.BigIntegers;
18+
19+
/**
20+
* A key pair generator for the ECCSI scheme (Elliptic Curve-based Certificateless Signatures
21+
* for Identity-based Encryption) as defined in RFC 6507.
22+
*
23+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc6507">
24+
* RFC 6507: Elliptic Curve-Based Certificateless Signatures for Identity-based Encryption (ECCSI)
25+
* </a>
26+
*/
27+
28+
public class ECCSIKeyPairGenerator
29+
implements AsymmetricCipherKeyPairGenerator
30+
{
31+
private BigInteger q;
32+
private ECPoint G;
33+
private Digest digest;
34+
private ECCSIKeyGenerationParameters parameters;
35+
36+
/**
37+
* Initializes the key pair generator with the specified parameters.
38+
*
39+
* @param parameters an instance of {@link ECCSIKeyGenerationParameters} which encapsulates the elliptic
40+
* curve domain parameters, the digest algorithm, and an associated identifier.
41+
*/
42+
@Override
43+
public void init(KeyGenerationParameters parameters)
44+
{
45+
this.parameters = (ECCSIKeyGenerationParameters)parameters;
46+
this.q = this.parameters.getQ();
47+
this.G = this.parameters.getG();
48+
this.digest = this.parameters.getDigest();
49+
50+
CryptoServicesRegistrar.checkConstraints(new DefaultServiceProperties("ECCSI", this.parameters.getN(), null, CryptoServicePurpose.KEYGEN));
51+
}
52+
53+
@Override
54+
public AsymmetricCipherKeyPair generateKeyPair()
55+
{
56+
SecureRandom random = parameters.getRandom();
57+
this.digest.reset();
58+
byte[] id = parameters.getId();
59+
ECPoint kpak = parameters.getKPAK();
60+
// 1) Choose v, a random (ephemeral) non-zero element of F_q;
61+
BigInteger v = BigIntegers.createRandomBigInteger(256, random).mod(q);
62+
// 2) Compute PVT = [v]G
63+
ECPoint pvt = G.multiply(v).normalize();
64+
65+
// 3) Compute a hash value HS = hash( G || KPAK || ID || PVT ), an N-octet integer;
66+
byte[] tmp = G.getEncoded(false);
67+
digest.update(tmp, 0, tmp.length);
68+
tmp = kpak.getEncoded(false);
69+
digest.update(tmp, 0, tmp.length);
70+
digest.update(id, 0, id.length);
71+
tmp = pvt.getEncoded(false);
72+
digest.update(tmp, 0, tmp.length);
73+
tmp = new byte[digest.getDigestSize()];
74+
digest.doFinal(tmp, 0);
75+
BigInteger HS = new BigInteger(1, tmp).mod(q);
76+
77+
// 4) Compute SSK = ( KSAK + HS * v ) modulo q;
78+
BigInteger ssk = parameters.computeSSK(HS.multiply(v));
79+
ECCSIPublicKeyParameters pub = new ECCSIPublicKeyParameters(pvt);
80+
return new AsymmetricCipherKeyPair(new ECCSIPublicKeyParameters(pvt), new ECCSIPrivateKeyParameters(ssk, pub));
81+
}
82+
}

0 commit comments

Comments
 (0)