Coverage Report

Created: 2020-02-14 15:38

/src/botan/src/lib/pk_pad/hash_id/hash_id.cpp
Line
Count
Source (jump to first uncovered line)
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
6.22k
   {
79
6.22k
   // Special case for SSL/TLS RSA signatures
80
6.22k
   if(name == "Parallel(MD5,SHA-160)")
81
0
      return std::vector<uint8_t>();
82
6.22k
83
6.22k
   // If you add a value to this function, also update test_hash_id.cpp
84
6.22k
85
6.22k
   if(name == "MD5")
86
198
      return std::vector<uint8_t>(MD5_PKCS_ID,
87
198
                               MD5_PKCS_ID + sizeof(MD5_PKCS_ID));
88
6.03k
89
6.03k
   if(name == "RIPEMD-160")
90
0
      return std::vector<uint8_t>(RIPEMD_160_PKCS_ID,
91
0
                               RIPEMD_160_PKCS_ID + sizeof(RIPEMD_160_PKCS_ID));
92
6.03k
93
6.03k
   if(name == "SHA-160" || name == "SHA-1" || name == "SHA1")
94
216
      return std::vector<uint8_t>(SHA_160_PKCS_ID,
95
216
                               SHA_160_PKCS_ID + sizeof(SHA_160_PKCS_ID));
96
5.81k
97
5.81k
   if(name == "SHA-224")
98
6
      return std::vector<uint8_t>(SHA_224_PKCS_ID,
99
6
                               SHA_224_PKCS_ID + sizeof(SHA_224_PKCS_ID));
100
5.80k
101
5.80k
   if(name == "SHA-256")
102
5.75k
      return std::vector<uint8_t>(SHA_256_PKCS_ID,
103
5.75k
                               SHA_256_PKCS_ID + sizeof(SHA_256_PKCS_ID));
104
53
105
53
   if(name == "SHA-384")
106
9
      return std::vector<uint8_t>(SHA_384_PKCS_ID,
107
9
                               SHA_384_PKCS_ID + sizeof(SHA_384_PKCS_ID));
108
44
109
44
   if(name == "SHA-512")
110
27
      return std::vector<uint8_t>(SHA_512_PKCS_ID,
111
27
                               SHA_512_PKCS_ID + sizeof(SHA_512_PKCS_ID));
112
17
113
17
   if(name == "SHA-512-256")
114
17
      return std::vector<uint8_t>(SHA_512_256_PKCS_ID,
115
17
                               SHA_512_256_PKCS_ID + sizeof(SHA_512_256_PKCS_ID));
116
0
117
0
   if(name == "SHA-3(224)")
118
0
      return std::vector<uint8_t>(SHA3_224_PKCS_ID,
119
0
                                SHA3_224_PKCS_ID + sizeof(SHA3_224_PKCS_ID));
120
0
121
0
   if(name == "SHA-3(256)")
122
0
      return std::vector<uint8_t>(SHA3_256_PKCS_ID,
123
0
                                SHA3_256_PKCS_ID + sizeof(SHA3_256_PKCS_ID));
124
0
125
0
   if(name == "SHA-3(384)")
126
0
      return std::vector<uint8_t>(SHA3_384_PKCS_ID,
127
0
                                SHA3_384_PKCS_ID + sizeof(SHA3_384_PKCS_ID));
128
0
129
0
   if(name == "SHA-3(512)")
130
0
      return std::vector<uint8_t>(SHA3_512_PKCS_ID,
131
0
                                SHA3_512_PKCS_ID + sizeof(SHA3_512_PKCS_ID));
132
0
133
0
   if(name == "SM3")
134
0
      return std::vector<uint8_t>(SM3_PKCS_ID, SM3_PKCS_ID + sizeof(SM3_PKCS_ID));
135
0
136
0
   if(name == "Tiger(24,3)")
137
0
      return std::vector<uint8_t>(TIGER_PKCS_ID,
138
0
                               TIGER_PKCS_ID + sizeof(TIGER_PKCS_ID));
139
0
140
0
   throw Invalid_Argument("No PKCS #1 identifier for " + name);
141
0
   }
142
143
/*
144
* HashID as specified by IEEE 1363/X9.31
145
*/
146
uint8_t ieee1363_hash_id(const std::string& name)
147
0
   {
148
0
   if(name == "SHA-160" || name == "SHA-1" || name == "SHA1")
149
0
      return 0x33;
150
0
151
0
   if(name == "SHA-224")    return 0x38;
152
0
   if(name == "SHA-256")    return 0x34;
153
0
   if(name == "SHA-384")    return 0x36;
154
0
   if(name == "SHA-512")    return 0x35;
155
0
156
0
   if(name == "RIPEMD-160") return 0x31;
157
0
158
0
   if(name == "Whirlpool")  return 0x37;
159
0
160
0
   return 0;
161
0
   }
162
163
}