Skip to content

Commit 9d54f1d

Browse files
committed
1.3 updates
1 parent 601f2f0 commit 9d54f1d

File tree

10 files changed

+1825
-0
lines changed

10 files changed

+1825
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.bouncycastle.crypto;
2+
3+
/**
4+
* Interface describing secret with encapsulation details.
5+
*/
6+
public interface SecretWithEncapsulation
7+
{
8+
// Destroyable methods
9+
void destroy();
10+
boolean isDestroyed();
11+
12+
/**
13+
* Return the secret associated with the encapsulation.
14+
*
15+
* @return the secret the encapsulation is for.
16+
*/
17+
byte[] getSecret();
18+
19+
/**
20+
* Return the data that carries the secret in its encapsulated form.
21+
*
22+
* @return the encapsulation of the secret.
23+
*/
24+
byte[] getEncapsulation();
25+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.bouncycastle.pqc.crypto.util;
2+
3+
import org.bouncycastle.crypto.SecretWithEncapsulation;
4+
import org.bouncycastle.util.Arrays;
5+
6+
public class SecretWithEncapsulationImpl
7+
implements SecretWithEncapsulation
8+
{
9+
private final AtomicBoolean hasBeenDestroyed = new AtomicBoolean(false);
10+
11+
private final byte[] sessionKey;
12+
private final byte[] cipher_text;
13+
14+
public SecretWithEncapsulationImpl(byte[] sessionKey, byte[] cipher_text)
15+
{
16+
this.sessionKey = sessionKey;
17+
this.cipher_text = cipher_text;
18+
}
19+
20+
public byte[] getSecret()
21+
{
22+
checkDestroyed();
23+
24+
return Arrays.clone(sessionKey);
25+
}
26+
27+
public byte[] getEncapsulation()
28+
{
29+
checkDestroyed();
30+
31+
return Arrays.clone(cipher_text);
32+
}
33+
34+
public void destroy()
35+
{
36+
if (!hasBeenDestroyed.getAndSet(true))
37+
{
38+
Arrays.clear(sessionKey);
39+
Arrays.clear(cipher_text);
40+
}
41+
}
42+
43+
public boolean isDestroyed()
44+
{
45+
return hasBeenDestroyed.get();
46+
}
47+
48+
void checkDestroyed()
49+
{
50+
if (isDestroyed())
51+
{
52+
throw new IllegalStateException("data has been destroyed");
53+
}
54+
}
55+
56+
private static class AtomicBoolean
57+
{
58+
private volatile boolean value;
59+
60+
AtomicBoolean(boolean value)
61+
{
62+
this.value = value;
63+
}
64+
65+
public synchronized void set(boolean value)
66+
{
67+
this.value = value;
68+
}
69+
70+
public synchronized boolean getAndSet(boolean value)
71+
{
72+
boolean tmp = this.value;
73+
74+
this.value = value;
75+
76+
return tmp;
77+
}
78+
79+
public synchronized boolean get()
80+
{
81+
return this.value;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)