konstantinullrich / crypton

A simple Dart library for asymmetric encryption and digital signatures
https://pub.dev/packages/crypton
MIT License
33 stars 12 forks source link

SHA-384/PSS Signature generation and verification #26

Closed GyuriMajercsik closed 2 years ago

GyuriMajercsik commented 3 years ago

I am investigating on how I could do this using crypton but it seems there is no support for that, yet.

Although I managed to use directly pointycastle but somehow I stuck on creating the salt...

Basically, I have a public key, a message and a signature and I would verify that the signature matches the message using the public key.

var rsaPublicKey = crypton.RSAPublicKey.fromPEM(publicKey);

  final signer = Signer('SHA-384/PSS');
  AsymmetricKeyParameter<RSAAsymmetricKey> keyParams =
      PublicKeyParameter<RSAPublicKey>(rsaPublicKey.asPointyCastle);
  signer.init(
    false,
    ParametersWithSalt(keyParams, Uint8List()),
  );
  final sig = PSSSignature(base64Decode(signature));

  final verified = signer.verifySignature(
    Uint8List.fromList(message.codeUnits),
    sig,
  );

I'm not sure how to build `ParametersWithSalt(keyParams, Uint8List()' needed to initialise the signer.

Any hint is highly appreciated.

GyuriMajercsik commented 3 years ago

I posted a stackoverflow question as well: https://stackoverflow.com/questions/67840150/sha-384-pss-signature-verification-using-pointycastle-in-dart

konstantinullrich commented 3 years ago

Hey, sorry for my late answer. Pointy Castle is the dart implementation of bouncy castle, which has been around for quite some time now. So maybe it helps to search for your answer. This is at least how I searched during the development process of crypton.

Anyways maybe this link helps. https://www.programcreek.com/java-api-examples/?api=org.bouncycastle.crypto.signers.PSSSigner

GyuriMajercsik commented 3 years ago

Thank you for your input. Unfortunately, it seems to be a different approach in BouncyCastle ( or at least in the link you sent ). They initialise the signer directly with the public key, without specifying the salt, as seen below. In PointyCastle there are two options: ParametersWithSalt or ParametersWithSaltConfiguration. The second is for signing purposes and the first requires a salt ...

                 if (forSigning) {
            pss.init(true, PrivateKeyFactory.createKey(key));
        } else {
            pss.init(false, PublicKeyFactory.createKey(key));
        }