Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
IRMA
Github mirrors
irmago
Commits
40ff4726
Commit
40ff4726
authored
May 27, 2021
by
Sietse Ringers
Browse files
refactor: rename structs, interfaces, members, vars and more of the keyshare server
parent
66771bd7
Changes
41
Hide whitespace changes
Inline
Side-by-side
internal/keysharecore/
keys
.go
→
internal/keysharecore/
core
.go
View file @
40ff4726
...
...
@@ -16,17 +16,20 @@ const (
)
type
(
A
es
Key
[
32
]
byte
A
ES
Key
[
32
]
byte
Core
struct
{
// Keys used for storage encryption/decryption
decryptionKeys
map
[
uint32
]
A
es
Key
e
n
cryptionKey
A
es
Key
e
n
cryptionKeyID
uint32
decryptionKeys
map
[
uint32
]
A
ES
Key
d
ecryptionKey
A
ES
Key
d
ecryptionKeyID
uint32
// Key used to sign keyshare protocol messages
signKey
*
rsa
.
PrivateKey
signKeyID
uint32
jwtPrivateKey
*
rsa
.
PrivateKey
jwtPrivateKeyID
uint32
jwtIssuer
string
jwtPinExpiry
int
// Commit values generated in first step of keyshare protocol
commitmentData
map
[
uint64
]
*
big
.
Int
...
...
@@ -35,16 +38,17 @@ type (
// IRMA issuer keys that are allowed to be used in keyshare
// sessions
trustedKeys
map
[
irma
.
PublicKeyIdentifier
]
*
gabikeys
.
PublicKey
jwtIssuer
string
jwtPinExpiry
int
}
Configuration
struct
{
AESKeyID
uint32
AESKey
AesKey
SignKeyID
uint32
SignKey
*
rsa
.
PrivateKey
// Keys used for storage encryption/decryption
DecryptionKey
AESKey
DecryptionKeyID
uint32
// Key used to sign keyshare protocol messages
JWTPrivateKey
*
rsa
.
PrivateKey
JWTPrivateKeyID
uint32
JWTIssuer
string
JWTPinExpiry
int
// in seconds
}
...
...
@@ -52,13 +56,13 @@ type (
func
NewKeyshareCore
(
conf
*
Configuration
)
*
Core
{
c
:=
&
Core
{
decryptionKeys
:
map
[
uint32
]
A
es
Key
{},
decryptionKeys
:
map
[
uint32
]
A
ES
Key
{},
commitmentData
:
map
[
uint64
]
*
big
.
Int
{},
trustedKeys
:
map
[
irma
.
PublicKeyIdentifier
]
*
gabikeys
.
PublicKey
{},
}
c
.
set
AESEn
cryptionKey
(
conf
.
AES
KeyID
,
conf
.
AES
Key
)
c
.
set
SignKey
(
conf
.
Sign
KeyID
,
conf
.
Sign
Key
)
c
.
set
De
cryptionKey
(
conf
.
Decryption
KeyID
,
conf
.
Decryption
Key
)
c
.
set
JWTPrivateKey
(
conf
.
JWTPrivate
KeyID
,
conf
.
JWTPrivate
Key
)
c
.
jwtIssuer
=
conf
.
JWTIssuer
if
c
.
jwtIssuer
==
""
{
...
...
@@ -72,31 +76,31 @@ func NewKeyshareCore(conf *Configuration) *Core {
return
c
}
func
Generate
AES
Key
()
(
A
es
Key
,
error
)
{
var
res
A
es
Key
func
Generate
Decryption
Key
()
(
A
ES
Key
,
error
)
{
var
res
A
ES
Key
_
,
err
:=
rand
.
Read
(
res
[
:
])
return
res
,
err
}
// Add an aes key for decryption, with identifier keyid
// Calling this will cause all keyshare packets generated with the key to be trusted
func
(
c
*
Core
)
DangerousAdd
AES
Key
(
keyID
uint32
,
key
A
es
Key
)
{
func
(
c
*
Core
)
DangerousAdd
Decryption
Key
(
keyID
uint32
,
key
A
ES
Key
)
{
c
.
decryptionKeys
[
keyID
]
=
key
}
// Set the aes key for encrypting new/changed keyshare data
// with identifier keyid
// Calling this wil also cause all keyshare packets generated with the key to be trusted
func
(
c
*
Core
)
set
AESEn
cryptionKey
(
keyID
uint32
,
key
A
es
Key
)
{
// Calling this wil
l
also cause all keyshare packets generated with the key to be trusted
func
(
c
*
Core
)
set
De
cryptionKey
(
keyID
uint32
,
key
A
ES
Key
)
{
c
.
decryptionKeys
[
keyID
]
=
key
c
.
e
n
cryptionKey
=
key
c
.
e
n
cryptionKeyID
=
keyID
c
.
d
ecryptionKey
=
key
c
.
d
ecryptionKeyID
=
keyID
}
// Set key used to sign keyshare protocol messages
func
(
c
*
Core
)
set
Sign
Key
(
id
uint32
,
key
*
rsa
.
PrivateKey
)
{
c
.
sign
Key
=
key
c
.
sign
KeyID
=
id
func
(
c
*
Core
)
set
JWTPrivate
Key
(
id
uint32
,
key
*
rsa
.
PrivateKey
)
{
c
.
jwtPrivate
Key
=
key
c
.
jwtPrivate
KeyID
=
id
}
// Add public key as trusted by keyshareCore. Calling this on incorrectly generated key material WILL compromise keyshare secrets!
...
...
internal/keysharecore/operations.go
View file @
40ff4726
...
...
@@ -26,43 +26,43 @@ var (
)
// Generate a new keyshare secret, secured with the given pin
func
(
c
*
Core
)
GenerateKeyshareSecret
(
pinRaw
string
)
(
EncryptedKeysharePacket
,
error
)
{
func
(
c
*
Core
)
NewUser
(
pinRaw
string
)
(
User
,
error
)
{
secret
,
err
:=
gabi
.
NewKeyshareSecret
()
if
err
!=
nil
{
return
EncryptedKeysharePacket
{},
err
return
User
{},
err
}
return
c
.
DangerousBuildKeyshare
Secret
(
pinRaw
,
secret
)
return
c
.
newUserFrom
Secret
(
pinRaw
,
secret
)
}
func
(
c
*
Core
)
DangerousBuildKeyshare
Secret
(
pinRaw
string
,
secret
*
big
.
Int
)
(
EncryptedKeysharePacket
,
error
)
{
func
(
c
*
Core
)
newUserFrom
Secret
(
pinRaw
string
,
secret
*
big
.
Int
)
(
User
,
error
)
{
pin
,
err
:=
padPin
(
pinRaw
)
if
err
!=
nil
{
return
EncryptedKeysharePacket
{},
err
return
User
{},
err
}
var
id
[
32
]
byte
_
,
err
=
rand
.
Read
(
id
[
:
])
if
err
!=
nil
{
return
EncryptedKeysharePacket
{},
err
return
User
{},
err
}
// Build unencrypted packet
var
p
unencrypted
KeysharePacket
var
p
unencrypted
User
p
.
setPin
(
pin
)
err
=
p
.
setKeyshareSecret
(
secret
)
if
err
!=
nil
{
return
EncryptedKeysharePacket
{},
err
return
User
{},
err
}
p
.
setID
(
id
)
// And encrypt
return
c
.
encrypt
Packet
(
p
)
return
c
.
encrypt
User
(
p
)
}
// Check pin for validity, and generate jwt for future access
func
(
c
*
Core
)
ValidatePin
(
ep
EncryptedKeysharePacket
,
pin
string
,
userID
string
)
(
string
,
error
)
{
p
,
err
:=
c
.
decrypt
Packet
IfPinOK
(
ep
,
pin
)
func
(
c
*
Core
)
ValidatePin
(
ep
User
,
pin
string
,
userID
string
)
(
string
,
error
)
{
p
,
err
:=
c
.
decrypt
User
IfPinOK
(
ep
,
pin
)
if
err
!=
nil
{
return
""
,
err
}
...
...
@@ -76,88 +76,88 @@ func (c *Core) ValidatePin(ep EncryptedKeysharePacket, pin string, userID string
"exp"
:
time
.
Now
()
.
Add
(
time
.
Duration
(
c
.
jwtPinExpiry
)
*
time
.
Second
)
.
Unix
(),
"token_id"
:
base64
.
StdEncoding
.
EncodeToString
(
id
[
:
]),
})
token
.
Header
[
"kid"
]
=
c
.
sign
KeyID
return
token
.
SignedString
(
c
.
sign
Key
)
token
.
Header
[
"kid"
]
=
c
.
jwtPrivate
KeyID
return
token
.
SignedString
(
c
.
jwtPrivate
Key
)
}
// Check whether the given JWT is currently valid as an access token for operations on the provided encrypted keyshare packet
func
(
c
*
Core
)
ValidateJWT
(
ep
EncryptedKeysharePacket
,
jwt
string
)
error
{
func
(
c
*
Core
)
ValidateJWT
(
ep
User
,
jwt
string
)
error
{
_
,
err
:=
c
.
verifyAccess
(
ep
,
jwt
)
return
err
}
// Change pin in an encrypted keyshare packet to a new value, after validating that the old value is known by caller.
func
(
c
*
Core
)
ChangePin
(
ep
EncryptedKeysharePacket
,
oldpinRaw
,
newpinRaw
string
)
(
EncryptedKeysharePacket
,
error
)
{
p
,
err
:=
c
.
decrypt
Packet
IfPinOK
(
ep
,
oldpinRaw
)
func
(
c
*
Core
)
ChangePin
(
ep
User
,
oldpinRaw
,
newpinRaw
string
)
(
User
,
error
)
{
p
,
err
:=
c
.
decrypt
User
IfPinOK
(
ep
,
oldpinRaw
)
if
err
!=
nil
{
return
EncryptedKeysharePacket
{},
err
return
User
{},
err
}
newpin
,
err
:=
padPin
(
newpinRaw
)
if
err
!=
nil
{
return
EncryptedKeysharePacket
{},
err
return
User
{},
err
}
// change and reencrypt
var
id
[
32
]
byte
_
,
err
=
rand
.
Read
(
id
[
:
])
if
err
!=
nil
{
return
EncryptedKeysharePacket
{},
err
return
User
{},
err
}
p
.
setPin
(
newpin
)
p
.
setID
(
id
)
return
c
.
encrypt
Packet
(
p
)
return
c
.
encrypt
User
(
p
)
}
// Verify that a given access jwt is valid, and if so, return decrypted keyshare packet
// Note: Although this is an internal function, it is tested directly
func
(
c
*
Core
)
verifyAccess
(
ep
EncryptedKeysharePacket
,
jwtToken
string
)
(
unencrypted
KeysharePacket
,
error
)
{
func
(
c
*
Core
)
verifyAccess
(
ep
User
,
jwtToken
string
)
(
unencrypted
User
,
error
)
{
// Verify token validity
token
,
err
:=
jwt
.
Parse
(
jwtToken
,
func
(
token
*
jwt
.
Token
)
(
interface
{},
error
)
{
if
token
.
Method
!=
jwt
.
SigningMethodRS256
{
return
nil
,
ErrInvalidJWT
}
return
&
c
.
sign
Key
.
PublicKey
,
nil
return
&
c
.
jwtPrivate
Key
.
PublicKey
,
nil
})
if
err
!=
nil
{
return
unencrypted
KeysharePacket
{},
ErrInvalidJWT
return
unencrypted
User
{},
ErrInvalidJWT
}
claims
,
ok
:=
token
.
Claims
.
(
jwt
.
MapClaims
)
if
!
ok
||
claims
.
Valid
()
!=
nil
{
return
unencrypted
KeysharePacket
{},
ErrInvalidJWT
return
unencrypted
User
{},
ErrInvalidJWT
}
if
!
claims
.
VerifyExpiresAt
(
time
.
Now
()
.
Unix
(),
true
)
{
return
unencrypted
KeysharePacket
{},
ErrInvalidJWT
return
unencrypted
User
{},
ErrInvalidJWT
}
if
_
,
present
:=
claims
[
"token_id"
];
!
present
{
return
unencrypted
KeysharePacket
{},
ErrInvalidJWT
return
unencrypted
User
{},
ErrInvalidJWT
}
tokenIDB64
,
ok
:=
claims
[
"token_id"
]
.
(
string
)
if
!
ok
{
return
unencrypted
KeysharePacket
{},
ErrInvalidJWT
return
unencrypted
User
{},
ErrInvalidJWT
}
tokenID
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
tokenIDB64
)
if
err
!=
nil
{
return
unencrypted
KeysharePacket
{},
ErrInvalidJWT
return
unencrypted
User
{},
ErrInvalidJWT
}
p
,
err
:=
c
.
decrypt
Packet
(
ep
)
p
,
err
:=
c
.
decrypt
User
(
ep
)
if
err
!=
nil
{
return
unencrypted
KeysharePacket
{},
err
return
unencrypted
User
{},
err
}
refId
:=
p
.
id
()
if
subtle
.
ConstantTimeCompare
(
refId
[
:
],
tokenID
)
!=
1
{
return
unencrypted
KeysharePacket
{},
ErrInvalidJWT
return
unencrypted
User
{},
ErrInvalidJWT
}
return
p
,
nil
}
// Get keyshare commitment usign given idemix public key(s)
func
(
c
*
Core
)
GenerateCommitments
(
ep
EncryptedKeysharePacket
,
accessToken
string
,
keyIDs
[]
irma
.
PublicKeyIdentifier
)
([]
*
gabi
.
ProofPCommitment
,
uint64
,
error
)
{
func
(
c
*
Core
)
GenerateCommitments
(
ep
User
,
accessToken
string
,
keyIDs
[]
irma
.
PublicKeyIdentifier
)
([]
*
gabi
.
ProofPCommitment
,
uint64
,
error
)
{
// Validate input request and build key list
var
keyList
[]
*
gabikeys
.
PublicKey
for
_
,
keyID
:=
range
keyIDs
{
...
...
@@ -196,7 +196,7 @@ func (c *Core) GenerateCommitments(ep EncryptedKeysharePacket, accessToken strin
}
// Generate response for zero-knowledge proof of keyshare secret, for a given previous commit and challenge
func
(
c
*
Core
)
GenerateResponse
(
ep
EncryptedKeysharePacket
,
accessToken
string
,
commitID
uint64
,
challenge
*
big
.
Int
,
keyID
irma
.
PublicKeyIdentifier
)
(
string
,
error
)
{
func
(
c
*
Core
)
GenerateResponse
(
ep
User
,
accessToken
string
,
commitID
uint64
,
challenge
*
big
.
Int
,
keyID
irma
.
PublicKeyIdentifier
)
(
string
,
error
)
{
// Validate request
if
uint
(
challenge
.
BitLen
())
>
gabikeys
.
DefaultSystemParameters
[
1024
]
.
Lh
||
challenge
.
Cmp
(
big
.
NewInt
(
0
))
<
0
{
return
""
,
ErrInvalidChallenge
...
...
@@ -228,8 +228,8 @@ func (c *Core) GenerateResponse(ep EncryptedKeysharePacket, accessToken string,
"sub"
:
"ProofP"
,
"iss"
:
c
.
jwtIssuer
,
})
token
.
Header
[
"kid"
]
=
c
.
sign
KeyID
return
token
.
SignedString
(
c
.
sign
Key
)
token
.
Header
[
"kid"
]
=
c
.
jwtPrivate
KeyID
return
token
.
SignedString
(
c
.
jwtPrivate
Key
)
}
// Pad pin string into 64 bytes, extending it with 0s if neccessary
...
...
internal/keysharecore/operations_test.go
View file @
40ff4726
...
...
@@ -21,10 +21,10 @@ import (
func
TestPinFunctionality
(
t
*
testing
.
T
)
{
// Setup keys for test
var
key
A
es
Key
var
key
A
ES
Key
_
,
err
:=
rand
.
Read
(
key
[
:
])
require
.
NoError
(
t
,
err
)
c
:=
NewKeyshareCore
(
&
Configuration
{
AESKeyID
:
1
,
AESKey
:
key
,
SignKeyID
:
1
,
Sign
Key
:
jwtTestKey
})
c
:=
NewKeyshareCore
(
&
Configuration
{
DecryptionKeyID
:
1
,
DecryptionKey
:
key
,
JWTPrivateKeyID
:
1
,
JWTPrivate
Key
:
jwtTestKey
})
// generate test pin
var
bpin
[
64
]
byte
...
...
@@ -33,7 +33,7 @@ func TestPinFunctionality(t *testing.T) {
pin
:=
string
(
bpin
[
:
])
// Generate package
ep
,
err
:=
c
.
GenerateKeyshareSecret
(
pin
)
ep
,
err
:=
c
.
NewUser
(
pin
)
require
.
NoError
(
t
,
err
)
// Test with correct pin
...
...
@@ -67,10 +67,10 @@ func TestPinFunctionality(t *testing.T) {
func
TestVerifyAccess
(
t
*
testing
.
T
)
{
// Setup keys for test
var
key
A
es
Key
var
key
A
ES
Key
_
,
err
:=
rand
.
Read
(
key
[
:
])
require
.
NoError
(
t
,
err
)
c
:=
NewKeyshareCore
(
&
Configuration
{
AESKeyID
:
1
,
AESKey
:
key
,
SignKeyID
:
1
,
Sign
Key
:
jwtTestKey
})
c
:=
NewKeyshareCore
(
&
Configuration
{
DecryptionKeyID
:
1
,
DecryptionKey
:
key
,
JWTPrivateKeyID
:
1
,
JWTPrivate
Key
:
jwtTestKey
})
// Generate test pins
var
bpin
[
64
]
byte
...
...
@@ -82,9 +82,9 @@ func TestVerifyAccess(t *testing.T) {
pin2
:=
string
(
bpin
[
:
])
// and test keyshare secrets
ep1
,
err
:=
c
.
GenerateKeyshareSecret
(
pin1
)
ep1
,
err
:=
c
.
NewUser
(
pin1
)
require
.
NoError
(
t
,
err
)
ep2
,
err
:=
c
.
GenerateKeyshareSecret
(
pin2
)
ep2
,
err
:=
c
.
NewUser
(
pin2
)
require
.
NoError
(
t
,
err
)
// Test use jwt on wrong packet
...
...
@@ -105,7 +105,7 @@ func TestVerifyAccess(t *testing.T) {
"exp"
:
time
.
Now
()
.
Add
(
-
3
*
time
.
Minute
)
.
Unix
(),
"token_id"
:
tokenID
,
})
jwtt
,
err
=
token
.
SignedString
(
c
.
sign
Key
)
jwtt
,
err
=
token
.
SignedString
(
c
.
jwtPrivate
Key
)
require
.
NoError
(
t
,
err
)
_
,
err
=
c
.
verifyAccess
(
ep1
,
jwtt
)
assert
.
Error
(
t
,
err
)
...
...
@@ -115,7 +115,7 @@ func TestVerifyAccess(t *testing.T) {
"iat"
:
time
.
Now
()
.
Unix
(),
"token_id"
:
tokenID
,
})
jwtt
,
err
=
token
.
SignedString
(
c
.
sign
Key
)
jwtt
,
err
=
token
.
SignedString
(
c
.
jwtPrivate
Key
)
require
.
NoError
(
t
,
err
)
_
,
err
=
c
.
verifyAccess
(
ep1
,
jwtt
)
assert
.
Error
(
t
,
err
)
...
...
@@ -126,7 +126,7 @@ func TestVerifyAccess(t *testing.T) {
"exp"
:
"test"
,
"token_id"
:
tokenID
,
})
jwtt
,
err
=
token
.
SignedString
(
c
.
sign
Key
)
jwtt
,
err
=
token
.
SignedString
(
c
.
jwtPrivate
Key
)
require
.
NoError
(
t
,
err
)
_
,
err
=
c
.
verifyAccess
(
ep1
,
jwtt
)
assert
.
Error
(
t
,
err
)
...
...
@@ -136,7 +136,7 @@ func TestVerifyAccess(t *testing.T) {
"iat"
:
time
.
Now
()
.
Unix
(),
"exp"
:
time
.
Now
()
.
Add
(
3
*
time
.
Minute
)
.
Unix
(),
})
jwtt
,
err
=
token
.
SignedString
(
c
.
sign
Key
)
jwtt
,
err
=
token
.
SignedString
(
c
.
jwtPrivate
Key
)
require
.
NoError
(
t
,
err
)
_
,
err
=
c
.
verifyAccess
(
ep1
,
jwtt
)
assert
.
Error
(
t
,
err
)
...
...
@@ -147,7 +147,7 @@ func TestVerifyAccess(t *testing.T) {
"exp"
:
time
.
Now
()
.
Add
(
3
*
time
.
Minute
)
.
Unix
(),
"token_id"
:
7
,
})
jwtt
,
err
=
token
.
SignedString
(
c
.
sign
Key
)
jwtt
,
err
=
token
.
SignedString
(
c
.
jwtPrivate
Key
)
require
.
NoError
(
t
,
err
)
_
,
err
=
c
.
verifyAccess
(
ep1
,
jwtt
)
assert
.
Error
(
t
,
err
)
...
...
@@ -166,10 +166,10 @@ func TestVerifyAccess(t *testing.T) {
func
TestProofFunctionality
(
t
*
testing
.
T
)
{
// Setup keys for test
var
key
A
es
Key
var
key
A
ES
Key
_
,
err
:=
rand
.
Read
(
key
[
:
])
require
.
NoError
(
t
,
err
)
c
:=
NewKeyshareCore
(
&
Configuration
{
AESKeyID
:
1
,
AESKey
:
key
,
SignKeyID
:
1
,
Sign
Key
:
jwtTestKey
})
c
:=
NewKeyshareCore
(
&
Configuration
{
DecryptionKeyID
:
1
,
DecryptionKey
:
key
,
JWTPrivateKeyID
:
1
,
JWTPrivate
Key
:
jwtTestKey
})
c
.
DangerousAddTrustedPublicKey
(
irma
.
PublicKeyIdentifier
{
Issuer
:
irma
.
NewIssuerIdentifier
(
"test"
),
Counter
:
1
},
testPubK1
)
// generate test pin
...
...
@@ -179,7 +179,7 @@ func TestProofFunctionality(t *testing.T) {
pin
:=
string
(
bpin
[
:
])
// generate keyshare secret
ep
,
err
:=
c
.
GenerateKeyshareSecret
(
pin
)
ep
,
err
:=
c
.
NewUser
(
pin
)
require
.
NoError
(
t
,
err
)
// Validate pin
...
...
@@ -201,7 +201,7 @@ func TestProofFunctionality(t *testing.T) {
}{}
fmt
.
Println
(
Rjwt
)
_
,
err
=
jwt
.
ParseWithClaims
(
Rjwt
,
claims
,
func
(
tok
*
jwt
.
Token
)
(
interface
{},
error
)
{
return
&
c
.
sign
Key
.
PublicKey
,
nil
return
&
c
.
jwtPrivate
Key
.
PublicKey
,
nil
})
require
.
NoError
(
t
,
err
)
...
...
@@ -216,10 +216,10 @@ func TestProofFunctionality(t *testing.T) {
func
TestCorruptedPacket
(
t
*
testing
.
T
)
{
// Setup keys for test
var
key
A
es
Key
var
key
A
ES
Key
_
,
err
:=
rand
.
Read
(
key
[
:
])
require
.
NoError
(
t
,
err
)
c
:=
NewKeyshareCore
(
&
Configuration
{
AESKeyID
:
1
,
AESKey
:
key
,
SignKeyID
:
1
,
Sign
Key
:
jwtTestKey
})
c
:=
NewKeyshareCore
(
&
Configuration
{
DecryptionKeyID
:
1
,
DecryptionKey
:
key
,
JWTPrivateKeyID
:
1
,
JWTPrivate
Key
:
jwtTestKey
})
c
.
DangerousAddTrustedPublicKey
(
irma
.
PublicKeyIdentifier
{
Issuer
:
irma
.
NewIssuerIdentifier
(
"test"
),
Counter
:
1
},
testPubK1
)
// Test parameters
...
...
@@ -229,7 +229,7 @@ func TestCorruptedPacket(t *testing.T) {
pin
:=
string
(
bpin
[
:
])
// Generate packet
ep
,
err
:=
c
.
GenerateKeyshareSecret
(
pin
)
ep
,
err
:=
c
.
NewUser
(
pin
)
require
.
NoError
(
t
,
err
)
jwtt
,
err
:=
c
.
ValidatePin
(
ep
,
pin
,
"testid"
)
...
...
@@ -260,10 +260,10 @@ func TestCorruptedPacket(t *testing.T) {
func
TestIncorrectPin
(
t
*
testing
.
T
)
{
// Setup keys for test
var
key
A
es
Key
var
key
A
ES
Key
_
,
err
:=
rand
.
Read
(
key
[
:
])
require
.
NoError
(
t
,
err
)
c
:=
NewKeyshareCore
(
&
Configuration
{
AESKeyID
:
1
,
AESKey
:
key
,
SignKeyID
:
1
,
Sign
Key
:
jwtTestKey
})
c
:=
NewKeyshareCore
(
&
Configuration
{
DecryptionKeyID
:
1
,
DecryptionKey
:
key
,
JWTPrivateKeyID
:
1
,
JWTPrivate
Key
:
jwtTestKey
})
c
.
DangerousAddTrustedPublicKey
(
irma
.
PublicKeyIdentifier
{
Issuer
:
irma
.
NewIssuerIdentifier
(
"test"
),
Counter
:
1
},
testPubK1
)
// Test parameters
...
...
@@ -273,7 +273,7 @@ func TestIncorrectPin(t *testing.T) {
pin
:=
string
(
bpin
[
:
])
// Generate packet
ep
,
err
:=
c
.
GenerateKeyshareSecret
(
pin
)
ep
,
err
:=
c
.
NewUser
(
pin
)
require
.
NoError
(
t
,
err
)
// validate pin
...
...
@@ -297,10 +297,10 @@ func TestIncorrectPin(t *testing.T) {
func
TestMissingKey
(
t
*
testing
.
T
)
{
// Setup keys for test
var
key
A
es
Key
var
key
A
ES
Key
_
,
err
:=
rand
.
Read
(
key
[
:
])
require
.
NoError
(
t
,
err
)
c
:=
NewKeyshareCore
(
&
Configuration
{
AESKeyID
:
1
,
AESKey
:
key
,
SignKeyID
:
1
,
Sign
Key
:
jwtTestKey
})
c
:=
NewKeyshareCore
(
&
Configuration
{
DecryptionKeyID
:
1
,
DecryptionKey
:
key
,
JWTPrivateKeyID
:
1
,
JWTPrivate
Key
:
jwtTestKey
})
c
.
DangerousAddTrustedPublicKey
(
irma
.
PublicKeyIdentifier
{
Issuer
:
irma
.
NewIssuerIdentifier
(
"test"
),
Counter
:
1
},
testPubK1
)
// Test parameters
...
...
@@ -310,7 +310,7 @@ func TestMissingKey(t *testing.T) {
pin
:=
string
(
bpin
[
:
])
// Generate packet
ep
,
err
:=
c
.
GenerateKeyshareSecret
(
pin
)
ep
,
err
:=
c
.
NewUser
(
pin
)
require
.
NoError
(
t
,
err
)
// Generate jwt
...
...
@@ -330,10 +330,10 @@ func TestMissingKey(t *testing.T) {
func
TestInvalidChallenge
(
t
*
testing
.
T
)
{
// Setup keys for test
var
key
A
es
Key
var
key
A
ES
Key
_
,
err
:=
rand
.
Read
(
key
[
:
])
require
.
NoError
(
t
,
err
)
c
:=
NewKeyshareCore
(
&
Configuration
{
AESKeyID
:
1
,
AESKey
:
key
,
SignKeyID
:
1
,
Sign
Key
:
jwtTestKey
})
c
:=
NewKeyshareCore
(
&
Configuration
{
DecryptionKeyID
:
1
,
DecryptionKey
:
key
,
JWTPrivateKeyID
:
1
,
JWTPrivate
Key
:
jwtTestKey
})
c
.
DangerousAddTrustedPublicKey
(
irma
.
PublicKeyIdentifier
{
Issuer
:
irma
.
NewIssuerIdentifier
(
"test"
),
Counter
:
1
},
testPubK1
)
// Test parameters
...
...
@@ -343,7 +343,7 @@ func TestInvalidChallenge(t *testing.T) {
pin
:=
string
(
bpin
[
:
])
// Generate packet
ep
,
err
:=
c
.
GenerateKeyshareSecret
(
pin
)
ep
,
err
:=
c
.
NewUser
(
pin
)
require
.
NoError
(
t
,
err
)
// Validate pin
...
...
@@ -371,10 +371,10 @@ func TestInvalidChallenge(t *testing.T) {
func
TestDoubleCommitUse
(
t
*
testing
.
T
)
{
// Setup keys for test
var
key
A
es
Key
var
key
A
ES
Key
_
,
err
:=
rand
.
Read
(
key
[
:
])
require
.
NoError
(
t
,
err
)
c
:=
NewKeyshareCore
(
&
Configuration
{
AESKeyID
:
1
,
AESKey
:
key
,
SignKeyID
:
1
,
Sign
Key
:
jwtTestKey
})
c
:=
NewKeyshareCore
(
&
Configuration
{
DecryptionKeyID
:
1
,
DecryptionKey
:
key
,
JWTPrivateKeyID
:
1
,
JWTPrivate
Key
:
jwtTestKey
})
c
.
DangerousAddTrustedPublicKey
(
irma
.
PublicKeyIdentifier
{
Issuer
:
irma
.
NewIssuerIdentifier
(
"test"
),
Counter
:
1
},
testPubK1
)
// Test parameters
...
...
@@ -384,7 +384,7 @@ func TestDoubleCommitUse(t *testing.T) {
pin
:=
string
(
bpin
[
:
])
// Generate packet
ep
,
err
:=
c
.
GenerateKeyshareSecret
(
pin
)
ep
,
err
:=
c
.
NewUser
(
pin
)
require
.
NoError
(
t
,
err
)
// validate pin
...
...
@@ -402,10 +402,10 @@ func TestDoubleCommitUse(t *testing.T) {
func
TestNonExistingCommit
(
t
*
testing
.
T
)
{
// Setup keys for test
var
key
A
es
Key
var
key
A
ES
Key
_
,
err
:=
rand
.
Read
(
key
[
:
])
require
.
NoError
(
t
,
err
)
c
:=
NewKeyshareCore
(
&
Configuration
{
AESKeyID
:
1
,
AESKey
:
key
,
SignKeyID
:
1
,
Sign
Key
:
jwtTestKey
})
c
:=
NewKeyshareCore
(
&
Configuration
{
DecryptionKeyID
:
1
,
DecryptionKey
:
key
,
JWTPrivateKeyID
:
1
,
JWTPrivate
Key
:
jwtTestKey
})
c
.
DangerousAddTrustedPublicKey
(
irma
.
PublicKeyIdentifier
{
Issuer
:
irma
.
NewIssuerIdentifier
(
"test"
),
Counter
:
1
},
testPubK1
)
// Test parameters
...
...
@@ -415,7 +415,7 @@ func TestNonExistingCommit(t *testing.T) {
pin
:=
string
(
bpin
[
:
])
// Generate packet
ep
,
err
:=
c
.
GenerateKeyshareSecret
(
pin
)
ep
,
err
:=
c
.
NewUser
(
pin
)
require
.
NoError
(
t
,
err
)
// Generate jwt
...
...
internal/keysharecore/packet.go
View file @
40ff4726
...
...
@@ -18,10 +18,10 @@ type (
// encryption layer applied before storing it. As such, we keep it here more explicit than
// is standard in go. When modifying this structure, analyse whether such changes can have a
// security impact through error side channels.
unencrypted
KeysharePacket
[
64
+
64
+
32
]
byte
unencrypted
User
[
64
+
64
+
32
]
byte
// Size is that of unencrypted packet + 12 bytes for nonce + 16 bytes for tag + 4 bytes for key ID
EncryptedKeysharePacket
[
64
+
64
+
32
+
12
+
16
+
4
]
byte
User
[
64
+
64
+
32
+
12
+
16
+
4
]
byte
)
var
(
...
...
@@ -30,22 +30,22 @@ var (
ErrNoSuchKey
=
errors
.
New
(
"Key identifier unknown"
)
)
func
(
p
*
unencrypted
KeysharePacket
)
pin
()
[
64
]
byte
{
func
(
p
*
unencrypted
User
)
pin
()
[
64
]
byte
{
var
result
[
64
]
byte
copy
(
result
[
:
],
p
[
0
:
64
])
return
result
}
func
(
p
*
unencrypted
KeysharePacket
)
setPin
(
pw
[
64
]
byte
)
{
func
(
p
*
unencrypted
User
)
setPin
(
pw
[
64
]
byte
)
{
copy
(
p
[
0
:
64
],
pw
[
:
])
}
func
(
p
*
unencrypted
KeysharePacket
)
keyshareSecret
()
*
big
.
Int
{
func
(
p
*
unencrypted
User
)
keyshareSecret
()
*
big
.
Int
{
result
:=
new
(
big
.
Int
)
return
result
.
SetBytes
(
p
[
64
:
128
])
}
func
(
p
*
unencrypted
KeysharePacket
)
setKeyshareSecret
(
val
*
big
.
Int
)
error
{
func
(
p
*
unencrypted
User
)
setKeyshareSecret
(
val
*
big
.
Int
)
error
{
if
val
.
Sign
()
==
-
1
{
return
ErrKeyshareSecretNegative
}
...
...
@@ -63,80 +63,80 @@ func (p *unencryptedKeysharePacket) setKeyshareSecret(val *big.Int) error {
return
nil
}
func
(
p
*
unencrypted
KeysharePacket
)
id
()
[
32
]
byte
{
func
(
p
*
unencrypted
User
)
id
()
[
32
]
byte
{
var
result
[
32
]
byte
copy
(
result
[
:
],
p
[
128
:
160
])
return
result
}
func
(
p
*
unencrypted
KeysharePacket
)
setID
(
id
[
32
]
byte
)
{
func
(
p
*
unencrypted
User
)
setID
(
id
[
32
]
byte
)
{
copy
(
p
[
128
:
160
],
id