From 281f36894f51edfa1774fe2772134e19feb290d3 Mon Sep 17 00:00:00 2001 From: Pointcheval Hugo Date: Thu, 17 Dec 2020 22:11:20 +0100 Subject: [PATCH] Add RSA KEM implementation --- lib/src/asym/RSA.dart | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 lib/src/asym/RSA.dart diff --git a/lib/src/asym/RSA.dart b/lib/src/asym/RSA.dart new file mode 100644 index 0000000..dffdb6d --- /dev/null +++ b/lib/src/asym/RSA.dart @@ -0,0 +1,74 @@ +// Copyright (c) 2020 +// Author: Hugo Pointcheval + +import 'package:native_crypto/src/exceptions.dart'; +import 'package:native_crypto/src/kem.dart'; +import 'package:native_crypto/src/key.dart'; + +/// This represents RSA Key Encapsulation Mechanism +class RSAKeyEncapsulationMechanism implements KeyEncapsulationMechanism { + bool _isInit = false; + KemMode _mode; + + PublicKey _publicKey; + PrivateKey _privateKey; + + @override + String get algorithm => "RSA"; + + @override + Object get options => throw UnimplementedError(); + + @override + bool get isInitialized => _isInit; + + @override + KemMode get mode => _mode; + + @override + KeyPair get keypair => KeyPair.from(_publicKey, _privateKey); + + RSAKeyEncapsulationMechanism( + KemMode mode, { + KeyPair keyPair, + PublicKey publicKey, + }) { + _mode = mode; + if (_mode == KemMode.ENCAPSULATION) { + if (publicKey != null) { + _isInit = true; + _publicKey = publicKey; + } else if (keyPair != null) { + if (keyPair.publicKey != null) { + _isInit = true; + _publicKey = keyPair.publicKey; + } + } + } else if (_mode == KemMode.DECAPSULATION) { + if (keyPair != null) { + if (keyPair.isComplete) { + _isInit = true; + _publicKey = keyPair.publicKey; + _privateKey = keyPair.privateKey; + } else { + throw KemInitException("Keypair must be complete for decapsulation."); + } + } else { + throw KemInitException("You must provide a keypair for decapsulation."); + } + } + } + + @override + Future encapsulate() { + if (!_isInit) { + throw KemInitException("KEM not properly initialized."); + } + throw UnimplementedError(); + } + + @override + Future decapsulate(Encapsulation encapsulation) { + throw UnimplementedError(); + } +}