<commit_before>/*
* ECDSA Tests
*
* (C) 2007 <NAME>
*     2007 <NAME>
*     2008 <NAME>
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_ECDSA)

#if defined(BOTAN_HAS_RSA)

#include <botan/hex.h>
#include <botan/pkcs8.h>
#include <botan/pubkey.h>
#include <botan/ecdsa.h>
#include <botan/rsa.h>
#include <botan/oids.h>

#if defined(BOTAN_HAS_X509_CERTIFICATES)
#include <botan/x509cert.h>
#endif

#include <iostream>
#include <fstream>
#include <memory>

using namespace Botan;

#define CHECK_MESSAGE(expr, print) try { if(!(expr)) { ++fails; std::cout << print << std::endl; } } catch(std::exception& e) { std::cout << __FUNCTION__ << ": " << e.what() << std::endl; }
#define CHECK(expr) try { if(!(expr)) { ++fails; std::cout << #expr << std::endl; } } catch(std::exception& e) { std::cout << __FUNCTION__ << ": " << e.what() << std::endl; }

namespace {

/**

* Tests whether the the signing routine will work correctly in case
* the integer e that is constructed from the message (thus the hash
* value) is larger than n, the order of the base point.  Tests the
* signing function of the pk signer object */

size_t test_hash_larger_than_n(RandomNumberGenerator& rng)
   {
   EC_Group dom_pars(OID("172.16.31.10.8")); // secp160r1
   // n = 0x0100000000000000000001f4c8f927aed3ca752257 (21 bytes)
   // -> shouldn't work with SHA224 which outputs 28 bytes

   size_t fails = 0;
   ECDSA_PrivateKey priv_key(rng, dom_pars);

   std::vector<byte> message(20);
   for(size_t i = 0; i != message.size(); ++i)
      message[i] = i;

   PK_Signer pk_signer_160(priv_key, "EMSA1_BSI(SHA-1)");
   PK_Verifier pk_verifier_160(priv_key, "EMSA1_BSI(SHA-1)");

   PK_Signer pk_signer_224(priv_key, "EMSA1_BSI(SHA-224)");

   // Verify we can sign and verify with SHA-160
   std::vector<byte> signature_160 = pk_signer_160.sign_message(message, rng);

   CHECK(pk_verifier_160.verify_message(message, signature_160));

   bool signature_failed = false;
   try
      {
      std::vector<byte> signature_224 = pk_signer_224.sign_message(message, rng);
      }
   catch(Encoding_Error)
      {
      signature_failed = true;
      }

   CHECK(signature_failed);

   // now check that verification alone fails

   // sign it with the normal EMSA1
   PK_Signer pk_signer(priv_key, "EMSA1(SHA-224)");
   std::vector<byte> signature = pk_signer.sign_message(message, rng);

   PK_Verifier pk_verifier(priv_key, "EMSA1_BSI(SHA-224)");

   // verify against EMSA1_BSI
   if(pk_verifier.verify_message(message, signature))
      {
      std::cout << "Corrupt ECDSA signature verified, should not have" << std::endl;
      ++fails;
      }

   return fails;
   }

#if defined(BOTAN_HAS_X509_CERTIFICATES)
size_t test_decode_ecdsa_X509()
   {
   X509_Certificate cert(TEST_DATA_DIR_ECC "/CSCA.CSCA.csca-germany.1.crt");
   size_t fails = 0;

   CHECK_MESSAGE(OIDS::lookup(cert.signature_algorithm().oid) == "ECDSA/EMSA1(SHA-224)", "error reading signature algorithm from x509 ecdsa certificate");

   CHECK_MESSAGE(hex_encode(cert.serial_number()) == "01", "error reading serial from x509 ecdsa certificate");
   CHECK_MESSAGE(hex_encode(cert.authority_key_id()) == "0096452DE588F966C4CCDF161DD1F3F5341B71E7", "error reading authority key id from x509 ecdsa certificate");
   CHECK_MESSAGE(hex_encode(cert.subject_key_id()) == "0096452DE588F966C4CCDF161DD1F3F5341B71E7", "error reading Subject key id from x509 ecdsa certificate");

   std::unique_ptr<X509_PublicKey> pubkey(cert.subject_public_key());
   bool ver_ec = cert.check_signature(*pubkey);
   CHECK_MESSAGE(ver_ec, "could not positively verify correct selfsigned x509-ecdsa certificate");

   return fails;<commit_msg>Add a test for X509_Certificate::fingerprint<commit_after>/*
* ECDSA Tests
*
* (C) 2007 <NAME>
*     2007 <NAME>
*     2008 <NAME>
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_ECDSA)

#if defined(BOTAN_HAS_RSA)

#include <botan/hex.h>
#include <botan/pkcs8.h>
#include <botan/pubkey.h>
#include <botan/ecdsa.h>
#include <botan/rsa.h>
#include <botan/oids.h>

#if defined(BOTAN_HAS_X509_CERTIFICATES)
#include <botan/x509cert.h>
#endif

#include <iostream>
#include <fstream>
#include <memory>

using namespace Botan;

#define CHECK_MESSAGE(expr, print) try { if(!(expr)) { ++fails; std::cout << print << std::endl; } } catch(std::exception& e) { std::cout << __FUNCTION__ << ": " << e.what() << std::endl; }
#define CHECK(expr) try { if(!(expr)) { ++fails; std::cout << #expr << std::endl; } } catch(std::exception& e) { std::cout << __FUNCTION__ << ": " << e.what() << std::endl; }

namespace {

/**

* Tests whether the the signing routine will work correctly in case
* the integer e that is constructed from the message (thus the hash
* value) is larger than n, the order of the base point.  Tests the
* signing function of the pk signer object */

size_t test_hash_larger_than_n(RandomNumberGenerator& rng)
   {
   EC_Group dom_pars(OID("1.3.132.0.8")); // secp160r1
   // n = 0x0100000000000000000001f4c8f927aed3ca752257 (21 bytes)
   // -> shouldn't work with SHA224 which outputs 28 bytes

   size_t fails = 0;
   ECDSA_PrivateKey priv_key(rng, dom_pars);

   std::vector<byte> message(20);
   for(size_t i = 0; i != message.size(); ++i)
      message[i] = i;

   PK_Signer pk_signer_160(priv_key, "EMSA1_BSI(SHA-1)");
   PK_Verifier pk_verifier_160(priv_key, "EMSA1_BSI(SHA-1)");

   PK_Signer pk_signer_224(priv_key, "EMSA1_BSI(SHA-224)");

   // Verify we can sign and verify with SHA-160
   std::vector<byte> signature_160 = pk_signer_160.sign_message(message, rng);

   CHECK(pk_verifier_160.verify_message(message, signature_160));

   bool signature_failed = false;
   try
      {
      std::vector<byte> signature_224 = pk_signer_224.sign_message(message, rng);
      }
   catch(Encoding_Error)
      {
      signature_failed = true;
      }

   CHECK(signature_failed);

   // now check that verification alone fails

   // sign it with the normal EMSA1
   PK_Signer pk_signer(priv_key, "EMSA1(SHA-224)");
   std::vector<byte> signature = pk_signer.sign_message(message, rng);

   PK_Verifier pk_verifier(priv_key, "EMSA1_BSI(SHA-224)");

   // verify against EMSA1_BSI
   if(pk_verifier.verify_message(message, signature))
      {
      std::cout << "Corrupt ECDSA signature verified, should not have" << std::endl;
      ++fails;
      }

   return fails;
   }

#if defined(BOTAN_HAS_X509_CERTIFICATES)
size_t test_decode_ecdsa_X509()
   {
   X509_Certificate cert(TEST_DATA_DIR_ECC "/CSCA.CSCA.csca-germany.1.crt");
   size_t fails = 0;

   CHECK_MESSAGE(OIDS::lookup(cert.signature_algorithm().oid) == "ECDSA/EMSA1(SHA-224)", "error reading signature algorithm from x509 ecdsa certificate");

   CHECK_MESSAGE(hex_encode(cert.serial_number()) == "01", "error reading serial from x509 ecdsa certificate");
   CHECK_MESSAGE(hex_encode(cert.authority_key_id()) == "0096452DE588F966C4CCDF161DD1F3F5341B71E7", "error reading authority key id from x509 ecdsa certificate");
   CHECK_MESSAGE(hex_encode(cert.subject_key_id()) == "0096452DE588F966C4CCDF161DD1F3F5341B71E7", "error reading Subject key id from x509 ecdsa certificate");
   CHECK_MESSAGE(cert.fingerprint("SHA-1") == "fc00:db20:35b:7399::5:43:EC:51:F0:19:23:BD:85:1D:F2:1B:B9", "Incorrect fingerprint");

   std::unique_ptr<X509_PublicKey> pubkey(cert.subject_public_key());
   bool ver_ec = cert.check_signature(*pubkey);
   CHECK_MESSAGE(ver_ec, "could not positively verify correct selfsigned x509-ecdsa certificate");

   return fails;
{'fraction_non_alphanumeric': 0.1345674149927793, 'fraction_numerical': 0.05973480372850203, 'mean_word_length': 4.655530809205642, 'pattern_counts': {'":': 4, '<': 95, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1518698', 'n_tokens_mistral': 3114, 'n_tokens_neox': 2784, 'n_words': 607}