Botan  1.11.31
hash_id.cpp
Go to the documentation of this file.
1 /*
2 * Hash Function Identification
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/hash_id.h>
9 #include <botan/exceptn.h>
10 
11 namespace Botan {
12 
13 namespace {
14 
15 const byte MD2_PKCS_ID[] = {
16 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
17 0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 };
18 
19 const byte MD5_PKCS_ID[] = {
20 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
21 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
22 
23 const byte RIPEMD_128_PKCS_ID[] = {
24 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
25 0x02, 0x05, 0x00, 0x04, 0x14 };
26 
27 const byte RIPEMD_160_PKCS_ID[] = {
28 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
29 0x01, 0x05, 0x00, 0x04, 0x14 };
30 
31 const byte SHA_160_PKCS_ID[] = {
32 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02,
33 0x1A, 0x05, 0x00, 0x04, 0x14 };
34 
35 const byte SHA_224_PKCS_ID[] = {
36 0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
37 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C };
38 
39 const byte SHA_256_PKCS_ID[] = {
40 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
41 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
42 
43 const byte SHA_384_PKCS_ID[] = {
44 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
45 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 };
46 
47 const byte SHA_512_PKCS_ID[] = {
48 0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
49 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 };
50 
51 const byte SHA_512_256_PKCS_ID[] = {
52 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
53 0x65, 0x03, 0x04, 0x02, 0x06, 0x05, 0x00, 0x04, 0x20 };
54 
55 const byte TIGER_PKCS_ID[] = {
56 0x30, 0x29, 0x30, 0x0D, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04,
57 0x01, 0xDA, 0x47, 0x0C, 0x02, 0x05, 0x00, 0x04, 0x18 };
58 
59 }
60 
61 /*
62 * HashID as specified by PKCS
63 */
64 std::vector<byte> pkcs_hash_id(const std::string& name)
65  {
66  // Special case for SSL/TLS RSA signatures
67  if(name == "Parallel(MD5,SHA-160)")
68  return std::vector<byte>();
69 
70  if(name == "MD2")
71  return std::vector<byte>(MD2_PKCS_ID,
72  MD2_PKCS_ID + sizeof(MD2_PKCS_ID));
73 
74  if(name == "MD5")
75  return std::vector<byte>(MD5_PKCS_ID,
76  MD5_PKCS_ID + sizeof(MD5_PKCS_ID));
77 
78  if(name == "RIPEMD-128")
79  return std::vector<byte>(RIPEMD_128_PKCS_ID,
80  RIPEMD_128_PKCS_ID + sizeof(RIPEMD_128_PKCS_ID));
81 
82  if(name == "RIPEMD-160")
83  return std::vector<byte>(RIPEMD_160_PKCS_ID,
84  RIPEMD_160_PKCS_ID + sizeof(RIPEMD_160_PKCS_ID));
85 
86  if(name == "SHA-160")
87  return std::vector<byte>(SHA_160_PKCS_ID,
88  SHA_160_PKCS_ID + sizeof(SHA_160_PKCS_ID));
89 
90  if(name == "SHA-224")
91  return std::vector<byte>(SHA_224_PKCS_ID,
92  SHA_224_PKCS_ID + sizeof(SHA_224_PKCS_ID));
93 
94  if(name == "SHA-256")
95  return std::vector<byte>(SHA_256_PKCS_ID,
96  SHA_256_PKCS_ID + sizeof(SHA_256_PKCS_ID));
97 
98  if(name == "SHA-384")
99  return std::vector<byte>(SHA_384_PKCS_ID,
100  SHA_384_PKCS_ID + sizeof(SHA_384_PKCS_ID));
101 
102  if(name == "SHA-512")
103  return std::vector<byte>(SHA_512_PKCS_ID,
104  SHA_512_PKCS_ID + sizeof(SHA_512_PKCS_ID));
105 
106  if(name == "SHA-512-256")
107  return std::vector<byte>(SHA_512_256_PKCS_ID,
108  SHA_512_256_PKCS_ID + sizeof(SHA_512_256_PKCS_ID));
109 
110  if(name == "Tiger(24,3)")
111  return std::vector<byte>(TIGER_PKCS_ID,
112  TIGER_PKCS_ID + sizeof(TIGER_PKCS_ID));
113 
114  throw Invalid_Argument("No PKCS #1 identifier for " + name);
115  }
116 
117 /*
118 * HashID as specified by IEEE 1363/X9.31
119 */
120 byte ieee1363_hash_id(const std::string& name)
121  {
122  if(name == "SHA-160") return 0x33;
123 
124  if(name == "SHA-224") return 0x38;
125  if(name == "SHA-256") return 0x34;
126  if(name == "SHA-384") return 0x36;
127  if(name == "SHA-512") return 0x35;
128 
129  if(name == "RIPEMD-160") return 0x31;
130  if(name == "RIPEMD-128") return 0x32;
131 
132  if(name == "Whirlpool") return 0x37;
133 
134  return 0;
135  }
136 
137 }
byte ieee1363_hash_id(const std::string &name)
Definition: hash_id.cpp:120
Definition: alg_id.cpp:13
std::vector< byte > pkcs_hash_id(const std::string &name)
Definition: hash_id.cpp:64
std::uint8_t byte
Definition: types.h:31