Botan 2.19.1
Crypto and TLS for C&
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
11namespace Botan {
12
13namespace {
14
15const uint8_t MD5_PKCS_ID[] = {
160x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
170xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
18
19const uint8_t RIPEMD_160_PKCS_ID[] = {
200x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
210x01, 0x05, 0x00, 0x04, 0x14 };
22
23const uint8_t SHA_160_PKCS_ID[] = {
240x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02,
250x1A, 0x05, 0x00, 0x04, 0x14 };
26
27const uint8_t SHA_224_PKCS_ID[] = {
280x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
290x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C };
30
31const uint8_t SHA_256_PKCS_ID[] = {
320x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
330x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
34
35const uint8_t SHA_384_PKCS_ID[] = {
360x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
370x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 };
38
39const uint8_t SHA_512_PKCS_ID[] = {
400x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
410x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 };
42
43const uint8_t SHA_512_256_PKCS_ID[] = {
440x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
450x65, 0x03, 0x04, 0x02, 0x06, 0x05, 0x00, 0x04, 0x20 };
46
47const uint8_t SHA3_224_PKCS_ID[] = {
480x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
490x03, 0x04, 0x02, 0x07, 0x05, 0x00, 0x04, 0x1C };
50
51const uint8_t SHA3_256_PKCS_ID[] = {
520x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
530x03, 0x04, 0x02, 0x08, 0x05, 0x00, 0x04, 0x20 };
54
55const uint8_t SHA3_384_PKCS_ID[] = {
560x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
570x03, 0x04, 0x02, 0x09, 0x05, 0x00, 0x04, 0x30 };
58
59const uint8_t SHA3_512_PKCS_ID[] = {
600x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
610x03, 0x04, 0x02, 0x0A, 0x05, 0x00, 0x04, 0x40 };
62
63const uint8_t SM3_PKCS_ID[] = {
640x30, 0x30, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x81, 0x1C, 0xCF,
650x55, 0x01, 0x83, 0x11, 0x05, 0x00, 0x04, 0x20,
66};
67
68const uint8_t TIGER_PKCS_ID[] = {
690x30, 0x29, 0x30, 0x0D, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04,
700x01, 0xDA, 0x47, 0x0C, 0x02, 0x05, 0x00, 0x04, 0x18 };
71
72}
73
74/*
75* HashID as specified by PKCS
76*/
77std::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*/
146uint8_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}
std::string name
Definition: alg_id.cpp:13
std::vector< uint8_t > pkcs_hash_id(const std::string &name)
Definition: hash_id.cpp:77
uint8_t ieee1363_hash_id(const std::string &name)
Definition: hash_id.cpp:146