-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
145 additions
and
44 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
library fido2; | ||
|
||
export 'src/ctap2/base.dart'; | ||
export 'src/utils/string.dart'; | ||
export 'src/ctap2/pin.dart'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'package:convert/convert.dart'; | ||
import 'package:cryptography/cryptography.dart'; | ||
import 'package:elliptic/ecdh.dart'; | ||
import 'package:elliptic/elliptic.dart'; | ||
import 'package:fido2/fido2.dart'; | ||
import 'package:fido2/src/cose.dart'; | ||
import 'package:quiver/collection.dart'; | ||
|
||
class EncapsulateResult { | ||
final CoseKey coseKey; | ||
final List<int> sharedSecret; | ||
|
||
EncapsulateResult(this.coseKey, this.sharedSecret); | ||
} | ||
|
||
abstract class PinProtocol { | ||
Future<EncapsulateResult> encapsulate(CoseKey peerCoseKey); | ||
|
||
Future<List<int>> encrypt(List<int> key, List<int> plaintext); | ||
|
||
Future<List<int>> decrypt(List<int> key, List<int> ciphertext); | ||
|
||
Future<List<int>> authenticate(List<int> key, List<int> message); | ||
|
||
Future<bool> verify(List<int> key, List<int> message, List<int> signature); | ||
} | ||
|
||
class PinProtocolV1 extends PinProtocol { | ||
@override | ||
Future<EncapsulateResult> encapsulate(CoseKey peerCoseKey) async { | ||
final ec = getP256(); | ||
final priv = ec.generatePrivateKey(); | ||
final pub = priv.publicKey; | ||
final pubBytes = hex.decode(pub.toHex().substring(2)); | ||
final keyAgreement = EcdhEsHkdf256.fromPublicKey(pubBytes.sublist(0, 32), pubBytes.sublist(32, 64)); | ||
final sharedSecret = computeSecret(priv, ec.hexToPublicKey('04${hex.encode(peerCoseKey[-2] + peerCoseKey[-3])}')); | ||
return EncapsulateResult(keyAgreement, await sharedSecret); | ||
} | ||
|
||
@override | ||
Future<List<int>> encrypt(List<int> key, List<int> plaintext) async { | ||
final algorithm = AesCbc.with128bits(macAlgorithm: MacAlgorithm.empty, paddingAlgorithm: PaddingAlgorithm.zero); | ||
final secretBox = await algorithm.encrypt(plaintext, secretKey: SecretKeyData(key), nonce: List.filled(16, 0)); | ||
return secretBox.cipherText; | ||
} | ||
|
||
@override | ||
Future<List<int>> decrypt(List<int> key, List<int> ciphertext) async { | ||
final algorithm = AesCbc.with128bits(macAlgorithm: MacAlgorithm.empty, paddingAlgorithm: PaddingAlgorithm.zero); | ||
return await algorithm.decrypt(SecretBox(ciphertext, nonce: List.filled(16, 0), mac: Mac.empty), secretKey: SecretKeyData(key)); | ||
} | ||
|
||
@override | ||
Future<List<int>> authenticate(List<int> key, List<int> message) async { | ||
final algorithm = Hmac.sha256(); | ||
final mac = await algorithm.calculateMac(message, secretKey: SecretKeyData(key)); | ||
return mac.bytes; | ||
} | ||
|
||
@override | ||
Future<bool> verify(List<int> key, List<int> message, List<int> signature) async { | ||
final algorithm = Hmac.sha256(); | ||
final mac = await algorithm.calculateMac(message, secretKey: SecretKeyData(key)); | ||
return listsEqual(mac.bytes, signature); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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,58 @@ | ||
import 'package:convert/convert.dart'; | ||
import 'package:elliptic/ecdh.dart'; | ||
import 'package:elliptic/elliptic.dart'; | ||
import 'package:fido2/fido2.dart'; | ||
import 'package:fido2/src/cose.dart'; | ||
import 'package:fido2/src/ctap2/pin.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Protocol 1', () { | ||
test('encapsulate', () async { | ||
final ec = getP256(); | ||
final priv = ec.generatePrivateKey(); | ||
final pub = priv.publicKey; | ||
final pubBytes = hex.decode(pub.toHex().substring(2)); | ||
final peerCoseKey = EcdhEsHkdf256.fromPublicKey(pubBytes.sublist(0, 32), pubBytes.sublist(32, 64)); | ||
|
||
PinProtocolV1 pinProtocol = PinProtocolV1(); | ||
EncapsulateResult result = await pinProtocol.encapsulate(peerCoseKey); | ||
final sharedSecret = computeSecret(priv, ec.hexToPublicKey('04${hex.encode(result.coseKey[-2] + result.coseKey[-3])}')); | ||
expect(sharedSecret, equals(result.sharedSecret)); | ||
}); | ||
|
||
test('encrypt', () async { | ||
final key = hex.decode('000102030405060708090a0b0c0d0e0f'); | ||
final plaintext = hex.decode('00112233445566778899aabbccddeeff'); | ||
final ciphertext = hex.decode('69c4e0d86a7b0430d8cdb78070b4c55a'); | ||
PinProtocolV1 pinProtocol = PinProtocolV1(); | ||
expect(await pinProtocol.encrypt(key, plaintext), equals(ciphertext)); | ||
}); | ||
|
||
test('decrypt', () async { | ||
final key = hex.decode('000102030405060708090a0b0c0d0e0f'); | ||
final plaintext = hex.decode('00112233445566778899aabbccddeeff'); | ||
final ciphertext = hex.decode('69c4e0d86a7b0430d8cdb78070b4c55a'); | ||
PinProtocolV1 pinProtocol = PinProtocolV1(); | ||
expect(await pinProtocol.decrypt(key, ciphertext), equals(plaintext)); | ||
}); | ||
|
||
test('authenticate', () async { | ||
final key = hex.decode('000102030405060708090a0b0c0d0e0f'); | ||
final message = hex.decode('00112233445566778899aabbccddeeff'); | ||
final signature = hex.decode('32cd28477b88c12e515b0e1fd7330d19616a4a51f6c502d64fe6a93fe7f786fa'); | ||
PinProtocolV1 pinProtocol = PinProtocolV1(); | ||
expect(await pinProtocol.authenticate(key, message), equals(signature)); | ||
}); | ||
|
||
test('verify', () async { | ||
final key = hex.decode('000102030405060708090a0b0c0d0e0f'); | ||
final message = hex.decode('00112233445566778899aabbccddeeff'); | ||
final signature = hex.decode('32cd28477b88c12e515b0e1fd7330d19616a4a51f6c502d64fe6a93fe7f786fa'); | ||
final signatureFalse = hex.decode('32cd28477b88c12e515b0e1fd7330d19616a4a51f6c502d64fe6a93fe7f786fb'); | ||
PinProtocolV1 pinProtocol = PinProtocolV1(); | ||
expect(await pinProtocol.verify(key, message, signature), equals(true)); | ||
expect(await pinProtocol.verify(key, message, signatureFalse), equals(false)); | ||
}); | ||
}); | ||
} |