Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
briar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
briar
briar
Commits
9d64b186
Verified
Commit
9d64b186
authored
6 years ago
by
akwizgran
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for hashing public keys into shared secret.
parent
ca591b5c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!1093
Add tests for equivalent handshake public keys
Pipeline
#3312
passed
6 years ago
Stage: test
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bramble-core/src/test/java/org/briarproject/bramble/crypto/KeyAgreementTest.java
+62
-9
62 additions, 9 deletions
...ava/org/briarproject/bramble/crypto/KeyAgreementTest.java
with
62 additions
and
9 deletions
bramble-core/src/test/java/org/briarproject/bramble/crypto/KeyAgreementTest.java
+
62
−
9
View file @
9d64b186
...
...
@@ -14,9 +14,9 @@ import java.security.GeneralSecurityException;
import
java.util.Arrays
;
import
java.util.Random
;
import
static
org
.
briarproject
.
bramble
.
api
.
keyagreement
.
KeyAgreementConstants
.
SHARED_SECRET_LABEL
;
import
static
org
.
briarproject
.
bramble
.
test
.
TestUtils
.
getRandomBytes
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
fromHexString
;
import
static
org
.
briarproject
.
bramble
.
util
.
StringUtils
.
getRandomString
;
import
static
org
.
junit
.
Assert
.
assertArrayEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
...
...
@@ -38,6 +38,7 @@ public class KeyAgreementTest extends BrambleTestCase {
private
final
CryptoComponent
crypto
=
new
CryptoComponentImpl
(
new
TestSecureRandomProvider
(),
null
);
private
final
String
label
=
getRandomString
(
123
);
private
final
byte
[][]
inputs
;
public
KeyAgreementTest
()
{
...
...
@@ -51,9 +52,9 @@ public class KeyAgreementTest extends BrambleTestCase {
public
void
testDerivesSharedSecret
()
throws
Exception
{
KeyPair
aPair
=
crypto
.
generateAgreementKeyPair
();
KeyPair
bPair
=
crypto
.
generateAgreementKeyPair
();
SecretKey
aShared
=
crypto
.
deriveSharedSecret
(
SHARED_SECRET_LABEL
,
SecretKey
aShared
=
crypto
.
deriveSharedSecret
(
label
,
bPair
.
getPublic
(),
aPair
,
inputs
);
SecretKey
bShared
=
crypto
.
deriveSharedSecret
(
SHARED_SECRET_LABEL
,
SecretKey
bShared
=
crypto
.
deriveSharedSecret
(
label
,
aPair
.
getPublic
(),
bPair
,
inputs
);
assertArrayEquals
(
aShared
.
getBytes
(),
bShared
.
getBytes
());
}
...
...
@@ -62,8 +63,7 @@ public class KeyAgreementTest extends BrambleTestCase {
public
void
testRejectsInvalidPublicKey
()
throws
Exception
{
KeyPair
keyPair
=
crypto
.
generateAgreementKeyPair
();
PublicKey
invalid
=
new
AgreementPublicKey
(
new
byte
[
32
]);
crypto
.
deriveSharedSecret
(
SHARED_SECRET_LABEL
,
invalid
,
keyPair
,
inputs
);
crypto
.
deriveSharedSecret
(
label
,
invalid
,
keyPair
,
inputs
);
}
@Test
...
...
@@ -88,15 +88,68 @@ public class KeyAgreementTest extends BrambleTestCase {
Curve25519
curve25519
=
Curve25519
.
getInstance
(
"java"
);
// Flip the unused most significant bit of the little-endian public key
byte
[]
aPubEquiv
alent
=
aPub
.
clone
();
aPubEquiv
alent
[
31
]
^=
(
byte
)
128
;
byte
[]
aPubEquiv
=
aPub
.
clone
();
aPubEquiv
[
31
]
^=
(
byte
)
128
;
// The public keys should be different but give the same shared secret
assertFalse
(
Arrays
.
equals
(
aPub
,
aPubEquiv
alent
));
assertFalse
(
Arrays
.
equals
(
aPub
,
aPubEquiv
));
assertArrayEquals
(
sharedSecret
,
curve25519
.
calculateAgreement
(
aPub
,
bPriv
));
assertArrayEquals
(
sharedSecret
,
curve25519
.
calculateAgreement
(
aPubEquivalent
,
bPriv
));
curve25519
.
calculateAgreement
(
aPubEquiv
,
bPriv
));
}
@Test
public
void
testDerivesSameSharedSecretFromEquivalentPublicKeyWithoutPublicKeysHashedIn
()
throws
Exception
{
KeyPair
aPair
=
crypto
.
generateAgreementKeyPair
();
KeyPair
bPair
=
crypto
.
generateAgreementKeyPair
();
// Flip the unused most significant bit of the little-endian public key
byte
[]
aPub
=
aPair
.
getPublic
().
getEncoded
();
byte
[]
aPubEquiv
=
aPub
.
clone
();
aPubEquiv
[
31
]
^=
(
byte
)
128
;
KeyPair
aPairEquiv
=
new
KeyPair
(
new
AgreementPublicKey
(
aPubEquiv
),
aPair
.
getPrivate
());
// The public keys should be different but give the same shared secret
assertFalse
(
Arrays
.
equals
(
aPub
,
aPubEquiv
));
SecretKey
shared
=
crypto
.
deriveSharedSecret
(
label
,
aPair
.
getPublic
(),
bPair
);
SecretKey
sharedEquiv
=
crypto
.
deriveSharedSecret
(
label
,
aPairEquiv
.
getPublic
(),
bPair
);
assertArrayEquals
(
shared
.
getBytes
(),
sharedEquiv
.
getBytes
());
}
@Test
public
void
testDerivesDifferentSharedSecretFromEquivalentPublicKeyWithPublicKeysHashedIn
()
throws
Exception
{
KeyPair
aPair
=
crypto
.
generateAgreementKeyPair
();
KeyPair
bPair
=
crypto
.
generateAgreementKeyPair
();
// Flip the unused most significant bit of the little-endian public key
byte
[]
aPub
=
aPair
.
getPublic
().
getEncoded
();
byte
[]
aPubEquiv
=
aPub
.
clone
();
aPubEquiv
[
31
]
^=
(
byte
)
128
;
KeyPair
aPairEquiv
=
new
KeyPair
(
new
AgreementPublicKey
(
aPubEquiv
),
aPair
.
getPrivate
());
// The public keys should be different and give different shared secrets
assertFalse
(
Arrays
.
equals
(
aPub
,
aPubEquiv
));
SecretKey
shared
=
deriveSharedSecretWithPublicKeysHashedIn
(
label
,
aPair
.
getPublic
(),
bPair
);
SecretKey
sharedEquiv
=
deriveSharedSecretWithPublicKeysHashedIn
(
label
,
aPairEquiv
.
getPublic
(),
bPair
);
assertFalse
(
Arrays
.
equals
(
shared
.
getBytes
(),
sharedEquiv
.
getBytes
()));
}
private
SecretKey
deriveSharedSecretWithPublicKeysHashedIn
(
String
label
,
PublicKey
publicKey
,
KeyPair
keyPair
)
throws
Exception
{
byte
[][]
inputs
=
new
byte
[][]
{
publicKey
.
getEncoded
(),
keyPair
.
getPublic
().
getEncoded
()
};
return
crypto
.
deriveSharedSecret
(
label
,
publicKey
,
keyPair
,
inputs
);
}
private
byte
[]
parsePrivateKey
(
String
hex
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment