Commit 902f9831 authored by garciay's avatar garciay
Browse files

Add Brainpool Security support

parent 8af893d6
Loading
Loading
Loading
Loading
+76 −3
Original line number Diff line number Diff line
@@ -168,12 +168,13 @@ namespace LibItsSecurity__Functions
                                                const OCTETSTRING& p__ecdsaNistp256PublicKeyX,
                                                const OCTETSTRING& p__ecdsaNistp256PublicKeyY
                                                ) {
    // Calculate the hash
    sha256 hash;
    std::vector<unsigned char> hashData;
    // TODO Create SHX interface and add generate method with std::vector
    //std::vector<unsigned char> tbs(p__toBeSignedSecuredMessage.lengthof(), static_cast<const unsigned char *>(p__toBeSignedSecuredMessage));
    // hash.generate(tbs, hashData);
    hash.generate(static_cast<const unsigned char*>(p__toBeVerifiedData), p__toBeVerifiedData.lengthof(), hashData);
    std::vector<unsigned char> tbh(static_cast<const unsigned char *>(p__toBeVerifiedData), static_cast<const unsigned char *>(p__toBeVerifiedData) + p__toBeVerifiedData.lengthof());
    hash.generate(tbh, hashData);
    // Check the signature
    const unsigned char * p = static_cast<const unsigned char *>(p__signature);
    std::vector<unsigned char> signature(p, p + p__signature.lengthof());
    p = static_cast<const unsigned char *>(p__ecdsaNistp256PublicKeyX);
@@ -188,6 +189,78 @@ namespace LibItsSecurity__Functions
    return FALSE;
  }

  /**
   * @desc    Verify the signature of the specified data
   * @param   p_toBeVerifiedData          The data to be verified
   * @param   p_signature                 The signature
   * @param   p_ecdsaBrainpoolp256PublicKeyX   The public key (x coordinate)
   * @param   p_ecdsaBrainpoolp256PublicKeyY   The public key (y coordinate)
   * @return  true on success, false otherwise
   fx_verifyWithEcdsaBrainpoolp256WithSha256(in octetstring p_toBeVerifiedData, in octetstring p_signature, in octetstring p_ecdsaBrainpoolp256PublicKeyX, in octetstring p_ecdsaBrainpoolp256PublicKeyY) return boolean;
  */
  BOOLEAN fx__verifyWithEcdsaBrainpoolp256WithSha256(
                                                     const OCTETSTRING& p__toBeVerifiedData,
                                                     const OCTETSTRING& p__signature,
                                                     const OCTETSTRING& p__ecdsaBrainpoolp256PublicKeyX,
                                                     const OCTETSTRING& p__ecdsaBrainpoolp256PublicKeyY
                                                    ) {
    // Calculate the hash
    sha256 hash;
    std::vector<unsigned char> hashData;
    // TODO Create SHX interface and add generate method with std::vector
    std::vector<unsigned char> tbh(static_cast<const unsigned char *>(p__toBeVerifiedData), static_cast<const unsigned char *>(p__toBeVerifiedData) + p__toBeVerifiedData.lengthof());
    hash.generate(tbh, hashData);
    // Check the signature
    const unsigned char * p = static_cast<const unsigned char *>(p__signature);
    std::vector<unsigned char> signature(p, p + p__signature.lengthof());
    p = static_cast<const unsigned char *>(p__ecdsaBrainpoolp256PublicKeyX);
    std::vector<unsigned char> pub_key_x(p, p + p__ecdsaBrainpoolp256PublicKeyX.lengthof());
    p = static_cast<const unsigned char *>(p__ecdsaBrainpoolp256PublicKeyY);
    std::vector<unsigned char> pub_key_y(p, p + p__ecdsaBrainpoolp256PublicKeyY.lengthof());
    ec_keys k(ec_elliptic_curves::brainpool_p_256_r1, pub_key_x, pub_key_y);
    if (k.sign_verif(hashData, signature) == 0) {
      return TRUE;
    }

    return FALSE;
  }

  /**
   * @desc    Verify the signature of the specified data
   * @param   p_toBeVerifiedData          The data to be verified
   * @param   p_signature                 The signature
   * @param   p_ecdsaBrainpoolp384PublicKeyX   The public key (x coordinate)
   * @param   p_ecdsaBrainpoolp384PublicKeyY   The public key (y coordinate)
   * @return  true on success, false otherwise
   fx_verifyWithEcdsaBrainpoolp384WithSha384(in octetstring p_toBeVerifiedData, in octetstring p_signature, in octetstring p_ecdsaBrainpoolp384PublicKeyX, in octetstring p_ecdsaBrainpoolp384PublicKeyY) return boolean;
  */
  BOOLEAN fx__verifyWithEcdsaBrainpoolp384WithSha384(
                                                     const OCTETSTRING& p__toBeVerifiedData,
                                                     const OCTETSTRING& p__signature,
                                                     const OCTETSTRING& p__ecdsaBrainpoolp384PublicKeyX,
                                                     const OCTETSTRING& p__ecdsaBrainpoolp384PublicKeyY
                                                    ) {
    // Calculate the hash
    sha384 hash;
    std::vector<unsigned char> hashData;
    // TODO Create SHX interface and add generate method with std::vector
    std::vector<unsigned char> tbh(static_cast<const unsigned char *>(p__toBeVerifiedData), static_cast<const unsigned char *>(p__toBeVerifiedData) + p__toBeVerifiedData.lengthof());
    hash.generate(tbh, hashData);
    // Check the signature
    const unsigned char * p = static_cast<const unsigned char *>(p__signature);
    std::vector<unsigned char> signature(p, p + p__signature.lengthof());
    p = static_cast<const unsigned char *>(p__ecdsaBrainpoolp384PublicKeyX);
    std::vector<unsigned char> pub_key_x(p, p + p__ecdsaBrainpoolp384PublicKeyX.lengthof());
    p = static_cast<const unsigned char *>(p__ecdsaBrainpoolp384PublicKeyY);
    std::vector<unsigned char> pub_key_y(p, p + p__ecdsaBrainpoolp384PublicKeyY.lengthof());
    ec_keys k(ec_elliptic_curves::brainpool_p_384_r1, pub_key_x, pub_key_y);
    if (k.sign_verif(hashData, signature) == 0) {
      return TRUE;
    }

    return FALSE;
  }

  /**
   * @desc    Produce a new public/private key pair based on Elliptic Curve Digital Signature Algorithm (ECDSA) algorithm.
   *          This function should not be used by the ATS
+10 −2
Original line number Diff line number Diff line
@@ -95,11 +95,19 @@ ec_keys::ec_keys(const ec_elliptic_curves p_elliptic_curve, const std::vector<un

ec_keys::~ec_keys() {
  loggers::get_instance().log(">>> ec_keys::~ec_keys");
  
  _pr_key.clear();
  _pu_key_x.clear();
  _pu_key_y.clear();
  
  if(_ec_key != nullptr) {
    ::EC_KEY_free(_ec_key);
  }
  if (_bn_ctx != nullptr) {
    BN_CTX_free(_bn_ctx);
  }
  loggers::get_instance().log("<<< ec_keys::~ec_keys");
} // End of Destructor

int ec_keys::generate() {
  loggers::get_instance().log(">>> ec_keys::generate");