mirrored from https://www.bouncycastle.org/repositories/bc-java
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
14 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
tls/src/main/java/org/bouncycastle/jsse/provider/GcmTls12NonceGeneratorUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.bouncycastle.jsse.provider; | ||
|
||
import org.bouncycastle.tls.crypto.TlsNonceGenerator; | ||
|
||
final public class GcmTls12NonceGeneratorUtil | ||
{ | ||
private static TlsNonceGeneratorFactory tlsNonceGeneratorFactory = null; | ||
|
||
public static void setGcmTlsNonceGeneratorFactory(final TlsNonceGeneratorFactory factory) | ||
{ | ||
tlsNonceGeneratorFactory = factory; | ||
} | ||
|
||
public static boolean isGcmFipsNonceGeneratorFactorySet() | ||
{ | ||
return tlsNonceGeneratorFactory != null; | ||
} | ||
|
||
public static TlsNonceGenerator createGcmFipsNonceGenerator(final byte[] baseNonce, final int counterSizeInBits) | ||
{ | ||
return tlsNonceGeneratorFactory != null | ||
? tlsNonceGeneratorFactory.create(baseNonce, counterSizeInBits) | ||
: null; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
tls/src/main/java/org/bouncycastle/jsse/provider/TlsNonceGeneratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.bouncycastle.jsse.provider; | ||
|
||
import org.bouncycastle.tls.crypto.TlsNonceGenerator; | ||
|
||
public interface TlsNonceGeneratorFactory | ||
{ | ||
TlsNonceGenerator create(byte[] baseNonce, int counterSizeInBits); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tls/src/test/java/org/bouncycastle/tls/test/TestNonceGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.bouncycastle.tls.test; | ||
|
||
import org.bouncycastle.tls.crypto.TlsNonceGenerator; | ||
|
||
import java.util.Arrays; | ||
|
||
class TestNonceGenerator implements TlsNonceGenerator | ||
{ | ||
private final byte[] baseNonce; | ||
private final long counterMask; | ||
private final int counterBytes; | ||
|
||
private long counterValue; | ||
private boolean counterExhausted; | ||
|
||
TestNonceGenerator(final byte[] baseNonce, final int counterBits) | ||
{ | ||
this.baseNonce = Arrays.copyOf(baseNonce, baseNonce.length); | ||
this.counterMask = -1L >>> (64 - counterBits); | ||
this.counterBytes = (counterBits + 7) / 8; | ||
|
||
this.counterValue = 0L; | ||
this.counterExhausted = false; | ||
} | ||
|
||
@Override | ||
public byte[] generateNonce(final int size) | ||
{ | ||
if (size != baseNonce.length) | ||
{ | ||
throw new IllegalArgumentException("requested length is not equal to the length of the base nonce."); | ||
} | ||
|
||
if (counterExhausted) | ||
{ | ||
throw new IllegalStateException("TLS nonce generator exhausted"); | ||
} | ||
|
||
final byte[] nonce = Arrays.copyOf(baseNonce, baseNonce.length); | ||
final int offset = baseNonce.length - counterBytes; | ||
|
||
for (int i = 0; i < counterBytes; i++) | ||
{ | ||
nonce[offset + i] ^= (byte)(counterValue >>> ((counterBytes - 1 - i) * 8)); | ||
} | ||
|
||
counterExhausted |= ((++counterValue & counterMask) == 0); | ||
|
||
return nonce; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tls/src/test/java/org/bouncycastle/tls/test/TestTlsNonceGeneratorFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.bouncycastle.tls.test; | ||
|
||
import org.bouncycastle.jsse.provider.TlsNonceGeneratorFactory; | ||
import org.bouncycastle.tls.crypto.TlsNonceGenerator; | ||
|
||
class TestTlsNonceGeneratorFactory implements TlsNonceGeneratorFactory { | ||
public static final TlsNonceGeneratorFactory INSTANCE = new TestTlsNonceGeneratorFactory(); | ||
|
||
private TestTlsNonceGeneratorFactory() | ||
{ | ||
// no op | ||
} | ||
|
||
@Override | ||
public TlsNonceGenerator create(final byte[] baseNonce, final int counterSizeInBits) | ||
{ | ||
return new TestNonceGenerator(baseNonce, counterSizeInBits); | ||
} | ||
} |