Botan  2.8.0
Crypto and TLS for C++11
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 uint8_t MD5_PKCS_ID[] = {
16 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
17 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
18 
19 const uint8_t RIPEMD_160_PKCS_ID[] = {
20 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
21 0x01, 0x05, 0x00, 0x04, 0x14 };
22 
23 const uint8_t SHA_160_PKCS_ID[] = {
24 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02,
25 0x1A, 0x05, 0x00, 0x04, 0x14 };
26 
27 const uint8_t SHA_224_PKCS_ID[] = {
28 0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
29 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C };
30 
31 const uint8_t SHA_256_PKCS_ID[] = {
32 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
33 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
34 
35 const uint8_t SHA_384_PKCS_ID[] = {
36 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
37 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 };
38 
39 const uint8_t SHA_512_PKCS_ID[] = {
40 0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
41 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 };
42 
43 const uint8_t SHA_512_256_PKCS_ID[] = {
44 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
45 0x65, 0x03, 0x04, 0x02, 0x06, 0x05, 0x00, 0x04, 0x20 };
46 
47 const uint8_t SHA3_224_PKCS_ID[] = {
48 0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
49 0x03, 0x04, 0x02, 0x07, 0x05, 0x00, 0x04, 0x1C };
50 
51 const uint8_t SHA3_256_PKCS_ID[] = {
52 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
53 0x03, 0x04, 0x02, 0x08, 0x05, 0x00, 0x04, 0x20 };
54 
55 const uint8_t SHA3_384_PKCS_ID[] = {
56 0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
57 0x03, 0x04, 0x02, 0x09, 0x05, 0x00, 0x04, 0x30 };
58 
59 const uint8_t SHA3_512_PKCS_ID[] = {
60 0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
61 0x03, 0x04, 0x02, 0x0A, 0x05, 0x00, 0x04, 0x40 };
62 
63 const uint8_t SM3_PKCS_ID[] = {
64 0x30, 0x30, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x81, 0x1C, 0xCF,
65 0x55, 0x01, 0x83, 0x11, 0x05, 0x00, 0x04, 0x20,
66 };
67 
68 const uint8_t TIGER_PKCS_ID[] = {
69 0x30, 0x29, 0x30, 0x0D, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04,
70 0x01, 0xDA, 0x47, 0x0C, 0x02, 0x05, 0x00, 0x04, 0x18 };
71 
72 }
73 
74 /*
75 * HashID as specified by PKCS
76 */
77 std::vector<uint8_t> pkcs_hash_id(const std::string& name)
78  {
79  // Special case for SSL/TLS RSA signatures
80  if(name == "Parallel(MD5,SHA-160)")
81  return std::vector<uint8_t>();
82 
83  // If you add a value to this function, also update test_hash_id.cpp
84 
85  if(name == "MD5")
86  return std::vector<uint8_t>(MD5_PKCS_ID,
87  MD5_PKCS_ID + sizeof(MD5_PKCS_ID));
88 
89  if(name == "RIPEMD-160")
90  return std::vector<uint8_t>(RIPEMD_160_PKCS_ID,
91  RIPEMD_160_PKCS_ID + sizeof(RIPEMD_160_PKCS_ID));
92 
93  if(name == "SHA-160" || name == "SHA-1" || name == "SHA1")
94  return std::vector<uint8_t>(SHA_160_PKCS_ID,
95  SHA_160_PKCS_ID + sizeof(SHA_160_PKCS_ID));
96 
97  if(name == "SHA-224")
98  return std::vector<uint8_t>(SHA_224_PKCS_ID,
99  SHA_224_PKCS_ID + sizeof(SHA_224_PKCS_ID));
100 
101  if(name == "SHA-256")
102  return std::vector<uint8_t>(SHA_256_PKCS_ID,
103  SHA_256_PKCS_ID + sizeof(SHA_256_PKCS_ID));
104 
105  if(name == "SHA-384")
106  return std::vector<uint8_t>(SHA_384_PKCS_ID,
107  SHA_384_PKCS_ID + sizeof(SHA_384_PKCS_ID));
108 
109  if(name == "SHA-512")
110  return std::vector<uint8_t>(SHA_512_PKCS_ID,
111  SHA_512_PKCS_ID + sizeof(SHA_512_PKCS_ID));
112 
113  if(name == "SHA-512-256")
114  return std::vector<uint8_t>(SHA_512_256_PKCS_ID,
115  SHA_512_256_PKCS_ID + sizeof(SHA_512_256_PKCS_ID));
116 
117  if(name == "SHA-3(224)")
118  return std::vector<uint8_t>(SHA3_224_PKCS_ID,
119  SHA3_224_PKCS_ID + sizeof(SHA3_224_PKCS_ID));
120 
121  if(name == "SHA-3(256)")
122  return std::vector<uint8_t>(SHA3_256_PKCS_ID,
123  SHA3_256_PKCS_ID + sizeof(SHA3_256_PKCS_ID));
124 
125  if(name == "SHA-3(384)")
126  return std::vector<uint8_t>(SHA3_384_PKCS_ID,
127  SHA3_384_PKCS_ID + sizeof(SHA3_384_PKCS_ID));
128 
129  if(name == "SHA-3(512)")
130  return std::vector<uint8_t>(SHA3_512_PKCS_ID,
131  SHA3_512_PKCS_ID + sizeof(SHA3_512_PKCS_ID));
132 
133  if(name == "SM3")
134  return std::vector<uint8_t>(SM3_PKCS_ID, SM3_PKCS_ID + sizeof(SM3_PKCS_ID));
135 
136  if(name == "Tiger(24,3)")
137  return std::vector<uint8_t>(TIGER_PKCS_ID,
138  TIGER_PKCS_ID + sizeof(TIGER_PKCS_ID));
139 
140  throw Invalid_Argument("No PKCS #1 identifier for " + name);
141  }
142 
143 /*
144 * HashID as specified by IEEE 1363/X9.31
145 */
146 uint8_t ieee1363_hash_id(const std::string& name)
147  {
148  if(name == "SHA-160" || name == "SHA-1" || name == "SHA1")
149  return 0x33;
150 
151  if(name == "SHA-224") return 0x38;
152  if(name == "SHA-256") return 0x34;
153  if(name == "SHA-384") return 0x36;
154  if(name == "SHA-512") return 0x35;
155 
156  if(name == "RIPEMD-160") return 0x31;
157 
158  if(name == "Whirlpool") return 0x37;
159 
160  return 0;
161  }
162 
163 }
uint8_t ieee1363_hash_id(const std::string &name)
Definition: hash_id.cpp:146
std::string name
Definition: alg_id.cpp:13
std::vector< uint8_t > pkcs_hash_id(const std::string &name)
Definition: hash_id.cpp:77