Coverage Report

Created: 2020-11-21 08:34

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