Botan 3.0.0
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/internal/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_1_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
68}
69
70/*
71* HashID as specified by PKCS
72*/
73std::vector<uint8_t> pkcs_hash_id(std::string_view name)
74 {
75 // Special case for SSL/TLS RSA signatures
76 if(name == "Parallel(MD5,SHA-1)")
77 return std::vector<uint8_t>();
78
79 // If you add a value to this function, also update test_hash_id.cpp
80
81 if(name == "MD5")
82 return std::vector<uint8_t>(MD5_PKCS_ID,
83 MD5_PKCS_ID + sizeof(MD5_PKCS_ID));
84
85 if(name == "RIPEMD-160")
86 return std::vector<uint8_t>(RIPEMD_160_PKCS_ID,
87 RIPEMD_160_PKCS_ID + sizeof(RIPEMD_160_PKCS_ID));
88
89 if(name == "SHA-1")
90 return std::vector<uint8_t>(SHA_1_PKCS_ID,
91 SHA_1_PKCS_ID + sizeof(SHA_1_PKCS_ID));
92
93 if(name == "SHA-224")
94 return std::vector<uint8_t>(SHA_224_PKCS_ID,
95 SHA_224_PKCS_ID + sizeof(SHA_224_PKCS_ID));
96
97 if(name == "SHA-256")
98 return std::vector<uint8_t>(SHA_256_PKCS_ID,
99 SHA_256_PKCS_ID + sizeof(SHA_256_PKCS_ID));
100
101 if(name == "SHA-384")
102 return std::vector<uint8_t>(SHA_384_PKCS_ID,
103 SHA_384_PKCS_ID + sizeof(SHA_384_PKCS_ID));
104
105 if(name == "SHA-512")
106 return std::vector<uint8_t>(SHA_512_PKCS_ID,
107 SHA_512_PKCS_ID + sizeof(SHA_512_PKCS_ID));
108
109 if(name == "SHA-512-256")
110 return std::vector<uint8_t>(SHA_512_256_PKCS_ID,
111 SHA_512_256_PKCS_ID + sizeof(SHA_512_256_PKCS_ID));
112
113 if(name == "SHA-3(224)")
114 return std::vector<uint8_t>(SHA3_224_PKCS_ID,
115 SHA3_224_PKCS_ID + sizeof(SHA3_224_PKCS_ID));
116
117 if(name == "SHA-3(256)")
118 return std::vector<uint8_t>(SHA3_256_PKCS_ID,
119 SHA3_256_PKCS_ID + sizeof(SHA3_256_PKCS_ID));
120
121 if(name == "SHA-3(384)")
122 return std::vector<uint8_t>(SHA3_384_PKCS_ID,
123 SHA3_384_PKCS_ID + sizeof(SHA3_384_PKCS_ID));
124
125 if(name == "SHA-3(512)")
126 return std::vector<uint8_t>(SHA3_512_PKCS_ID,
127 SHA3_512_PKCS_ID + sizeof(SHA3_512_PKCS_ID));
128
129 if(name == "SM3")
130 return std::vector<uint8_t>(SM3_PKCS_ID, SM3_PKCS_ID + sizeof(SM3_PKCS_ID));
131
132 throw Invalid_Argument("No PKCS #1 identifier for " + std::string(name));
133 }
134
135/*
136* HashID as specified by IEEE 1363/X9.31
137*/
138uint8_t ieee1363_hash_id(std::string_view name)
139 {
140 if(name == "SHA-1") return 0x33;
141
142 if(name == "SHA-224") return 0x38;
143 if(name == "SHA-256") return 0x34;
144 if(name == "SHA-384") return 0x36;
145 if(name == "SHA-512") return 0x35;
146
147 if(name == "RIPEMD-160") return 0x31;
148
149 if(name == "Whirlpool") return 0x37;
150
151 return 0;
152 }
153
154}
std::string name
Definition: alg_id.cpp:12
uint8_t ieee1363_hash_id(std::string_view name)
Definition: hash_id.cpp:138
std::vector< uint8_t > pkcs_hash_id(std::string_view name)
Definition: hash_id.cpp:73