1 /**
2 * Hash Function Identification
3 * 
4 * Copyright:
5 * (C) 1999-2007 Jack Lloyd
6 * (C) 2014-2015 Etienne Cimon
7 *
8 * License:
9 * Botan is released under the Simplified BSD License (see LICENSE.md)
10 */
11 module botan.pk_pad.hash_id;
12 
13 import memutils.vector;
14 import botan.utils.exceptn;
15 import botan.utils.types;
16 
17 /**
18 * Return the PKCS #1 hash identifier
19 * @see RFC 3447 section 9.2
20 * Params:
21 *  name = the name of the hash function
22 * Returns: ubyte sequence identifying the hash
23 * Throws: $D(InvalidArgument) if the hash has no known PKCS #1 hash id
24 */
25 Vector!ubyte pkcsHashId(in string name)
26 {
27     // Special case for SSL/TLS RSA signatures
28     if (name == "Parallel(MD5,SHA-160)")
29         return Vector!ubyte();
30     
31     if (name == "MD2")
32         return Vector!ubyte(MD2_PKCS_ID[0 .. (MD2_PKCS_ID).length]);
33     
34     if (name == "MD5")
35         return Vector!ubyte(MD5_PKCS_ID);
36     
37     if (name == "RIPEMD-128")
38         return Vector!ubyte(RIPEMD_128_PKCS_ID);
39     
40     if (name == "RIPEMD-160")
41         return Vector!ubyte(RIPEMD_160_PKCS_ID);
42     
43     if (name == "SHA-160")
44         return Vector!ubyte(SHA_160_PKCS_ID);
45     
46     if (name == "SHA-224")
47         return Vector!ubyte(SHA_224_PKCS_ID);
48     
49     if (name == "SHA-256")
50         return Vector!ubyte(SHA_256_PKCS_ID);
51     
52     if (name == "SHA-384")
53         return Vector!ubyte(SHA_384_PKCS_ID);
54     
55     if (name == "SHA-512")
56         return Vector!ubyte(SHA_512_PKCS_ID);
57     
58     if (name == "Tiger(24,3)")
59         return Vector!ubyte(TIGER_PKCS_ID);
60     
61     throw new InvalidArgument("No PKCS #1 identifier for " ~ name);
62 }
63 
64 /**
65 * Return the IEEE 1363 hash identifier
66 * Params:
67 *  name = the name of the hash function
68 * Returns: ubyte code identifying the hash, or 0 if not known
69 */
70 
71 ubyte ieee1363HashId(in string name)
72 {
73     if (name == "SHA-160")     return 0x33;
74     
75     if (name == "SHA-224")     return 0x38;
76     if (name == "SHA-256")     return 0x34;
77     if (name == "SHA-384")     return 0x36;
78     if (name == "SHA-512")     return 0x35;
79     
80     if (name == "RIPEMD-160") return 0x31;
81     if (name == "RIPEMD-128") return 0x32;
82     
83     if (name == "Whirlpool")  return 0x37;
84     
85     return 0;
86 }
87 
88 
89 private:
90 
91 __gshared immutable ubyte[] MD2_PKCS_ID = [
92     0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
93     0xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 ];
94 
95 __gshared immutable ubyte[] MD5_PKCS_ID = [
96     0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
97     0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 ];
98 
99 __gshared immutable ubyte[] RIPEMD_128_PKCS_ID = [
100     0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
101     0x02, 0x05, 0x00, 0x04, 0x14 ];
102 
103 __gshared immutable ubyte[] RIPEMD_160_PKCS_ID = [
104     0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
105     0x01, 0x05, 0x00, 0x04, 0x14 ];
106 
107 __gshared immutable ubyte[] SHA_160_PKCS_ID = [
108     0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02,
109     0x1A, 0x05, 0x00, 0x04, 0x14 ];
110 
111 __gshared immutable ubyte[] SHA_224_PKCS_ID = [
112     0x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
113     0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C ];
114 
115 __gshared immutable ubyte[] SHA_256_PKCS_ID = [
116     0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
117     0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 ];
118 
119 __gshared immutable ubyte[] SHA_384_PKCS_ID = [
120     0x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
121     0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 ];
122 
123 __gshared immutable ubyte[] SHA_512_PKCS_ID = [
124     0x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
125     0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 ];
126 
127 __gshared immutable ubyte[] TIGER_PKCS_ID = [
128     0x30, 0x29, 0x30, 0x0D, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04,
129     0x01, 0xDA, 0x47, 0x0C, 0x02, 0x05, 0x00, 0x04, 0x18 ];