/*************************************************
* EMSA/EME/KDF/MGF Retrieval Source File         *
* (C) 1999-2003 The Botan Project                *
*************************************************/

#include <botan/look_pk.h>
#include <botan/lookup.h>
#include <botan/emsa.h>
#include <botan/eme.h>
#include <botan/kdf.h>
#include <botan/mgf1.h>

namespace Botan {

/*************************************************
* Get an EMSA by name                            *
*************************************************/
EMSA* get_emsa(const std::string& emsa)
   {
   if(emsa == "Raw")
      return new EMSA_Raw;

   std::vector<std::string> name = parse_algorithm_name(emsa);
   if(name.size() < 2)
      throw Algorithm_Not_Found(emsa);
   const std::string emsa_name = deref_alias(name[0]);

   if(emsa_name == "EMSA4")
      {
      if(name.size() == 2)
         return new EMSA4(name[1], "MGF1");
      if(name.size() == 3)
         return new EMSA4(name[1], name[2]);
      throw Invalid_Algorithm_Name(emsa);
      }

   if(name.size() != 2)
      throw Algorithm_Not_Found(emsa);

   if(emsa_name == "EMSA1")
      return new EMSA1(name[1]);
   if(emsa_name == "EMSA2")
      return new EMSA2(name[1]);
   if(emsa_name == "EMSA3")
      return new EMSA3(name[1]);
   throw Algorithm_Not_Found(emsa);
   }

/*************************************************
* Get an EME by name                             *
*************************************************/
EME* get_eme(const std::string& eme)
   {
   std::vector<std::string> name = parse_algorithm_name(eme);

   const std::string eme_name = deref_alias(name[0]);

   if(eme_name == "PKCS1v15")
      return new EME_PKCS1v15;

   if(eme_name == "EME1")
      {
      if(name.size() < 2 || name.size() > 3)
         throw Invalid_Algorithm_Name(eme);
      if(name.size() == 2)
         return new EME1(name[1], "MGF1");
      if(name.size() == 3)
         return new EME1(name[1], name[2]);
      }
   throw Algorithm_Not_Found(eme);
   }

/*************************************************
* Get an KDF by name                             *
*************************************************/
KDF* get_kdf(const std::string& kdf)
   {
   std::vector<std::string> name = parse_algorithm_name(kdf);

   const std::string kdf_name = deref_alias(name[0]);

   if(kdf_name == "TLS-PRF")
      return new TLS_PRF;
   if(kdf_name == "SSL3-PRF")
      return new SSL3_PRF;

   if(name.size() != 2)
      throw Algorithm_Not_Found(kdf);

   if(kdf_name == "KDF1")
      return new KDF1(name[1]);
   if(kdf_name == "KDF2")
      return new KDF2(name[1]);
   throw Algorithm_Not_Found(kdf);
   }

/*************************************************
* Get a MGF by name                              *
*************************************************/
MGF* get_mgf(const std::string& mgf)
   {
   std::vector<std::string> name = parse_algorithm_name(mgf);
   if(name.size() != 2)
      throw Invalid_Algorithm_Name(mgf);

   const std::string mgf_name = deref_alias(name[0]);

   if(mgf_name == "MGF1")
      return new MGF1(name[1]);
   throw Algorithm_Not_Found(mgf);
   }

}
