Botan  1.11.15
src/lib/pk_pad/hash_id/hash_id.cpp
Go to the documentation of this file.
00001 /*
00002 * Hash Function Identification
00003 * (C) 1999-2008 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/hash_id.h>
00009 #include <botan/exceptn.h>
00010 
00011 namespace Botan {
00012 
00013 namespace {
00014 
00015 const byte MD2_PKCS_ID[] = {
00016 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
00017 0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 };
00018 
00019 const byte MD5_PKCS_ID[] = {
00020 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
00021 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
00022 
00023 const byte RIPEMD_128_PKCS_ID[] = {
00024 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
00025 0x02, 0x05, 0x00, 0x04, 0x14 };
00026 
00027 const byte RIPEMD_160_PKCS_ID[] = {
00028 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
00029 0x01, 0x05, 0x00, 0x04, 0x14 };
00030 
00031 const byte SHA_160_PKCS_ID[] = {
00032 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02,
00033 0x1A, 0x05, 0x00, 0x04, 0x14 };
00034 
00035 const byte SHA_224_PKCS_ID[] = {
00036 0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
00037 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C };
00038 
00039 const byte SHA_256_PKCS_ID[] = {
00040 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
00041 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
00042 
00043 const byte SHA_384_PKCS_ID[] = {
00044 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
00045 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 };
00046 
00047 const byte SHA_512_PKCS_ID[] = {
00048 0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
00049 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 };
00050 
00051 const byte TIGER_PKCS_ID[] = {
00052 0x30, 0x29, 0x30, 0x0D, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04,
00053 0x01, 0xDA, 0x47, 0x0C, 0x02, 0x05, 0x00, 0x04, 0x18 };
00054 
00055 }
00056 
00057 /*
00058 * HashID as specified by PKCS
00059 */
00060 std::vector<byte> pkcs_hash_id(const std::string& name)
00061    {
00062    // Special case for SSL/TLS RSA signatures
00063    if(name == "Parallel(MD5,SHA-160)")
00064       return std::vector<byte>();
00065 
00066    if(name == "MD2")
00067       return std::vector<byte>(MD2_PKCS_ID,
00068                                MD2_PKCS_ID + sizeof(MD2_PKCS_ID));
00069 
00070    if(name == "MD5")
00071       return std::vector<byte>(MD5_PKCS_ID,
00072                                MD5_PKCS_ID + sizeof(MD5_PKCS_ID));
00073 
00074    if(name == "RIPEMD-128")
00075       return std::vector<byte>(RIPEMD_128_PKCS_ID,
00076                                RIPEMD_128_PKCS_ID + sizeof(RIPEMD_128_PKCS_ID));
00077 
00078    if(name == "RIPEMD-160")
00079       return std::vector<byte>(RIPEMD_160_PKCS_ID,
00080                                RIPEMD_160_PKCS_ID + sizeof(RIPEMD_160_PKCS_ID));
00081 
00082    if(name == "SHA-160")
00083       return std::vector<byte>(SHA_160_PKCS_ID,
00084                                SHA_160_PKCS_ID + sizeof(SHA_160_PKCS_ID));
00085 
00086    if(name == "SHA-224")
00087       return std::vector<byte>(SHA_224_PKCS_ID,
00088                                SHA_224_PKCS_ID + sizeof(SHA_224_PKCS_ID));
00089 
00090    if(name == "SHA-256")
00091       return std::vector<byte>(SHA_256_PKCS_ID,
00092                                SHA_256_PKCS_ID + sizeof(SHA_256_PKCS_ID));
00093 
00094    if(name == "SHA-384")
00095       return std::vector<byte>(SHA_384_PKCS_ID,
00096                                SHA_384_PKCS_ID + sizeof(SHA_384_PKCS_ID));
00097 
00098    if(name == "SHA-512")
00099       return std::vector<byte>(SHA_512_PKCS_ID,
00100                                SHA_512_PKCS_ID + sizeof(SHA_512_PKCS_ID));
00101 
00102    if(name == "Tiger(24,3)")
00103       return std::vector<byte>(TIGER_PKCS_ID,
00104                                TIGER_PKCS_ID + sizeof(TIGER_PKCS_ID));
00105 
00106    throw Invalid_Argument("No PKCS #1 identifier for " + name);
00107    }
00108 
00109 /*
00110 * HashID as specified by IEEE 1363/X9.31
00111 */
00112 byte ieee1363_hash_id(const std::string& name)
00113    {
00114    if(name == "SHA-160")    return 0x33;
00115 
00116    if(name == "SHA-224")    return 0x38;
00117    if(name == "SHA-256")    return 0x34;
00118    if(name == "SHA-384")    return 0x36;
00119    if(name == "SHA-512")    return 0x35;
00120 
00121    if(name == "RIPEMD-160") return 0x31;
00122    if(name == "RIPEMD-128") return 0x32;
00123 
00124    if(name == "Whirlpool")  return 0x37;
00125 
00126    return 0;
00127    }
00128 
00129 }