Botan 1.10.17
hash_id.cpp
Go to the documentation of this file.
1/*
2* Hash Function Identification
3* (C) 1999-2008 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#include <botan/hash_id.h>
9#include <botan/exceptn.h>
10
11namespace Botan {
12
13namespace {
14
15const byte MD2_PKCS_ID[] = {
160x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
170xF7, 0x0D, 0x02, 0x02, 0x05, 0x00, 0x04, 0x10 };
18
19const byte MD5_PKCS_ID[] = {
200x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86,
210xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
22
23const byte RIPEMD_128_PKCS_ID[] = {
240x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
250x02, 0x05, 0x00, 0x04, 0x14 };
26
27const byte RIPEMD_160_PKCS_ID[] = {
280x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02,
290x01, 0x05, 0x00, 0x04, 0x14 };
30
31const byte SHA_160_PKCS_ID[] = {
320x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02,
330x1A, 0x05, 0x00, 0x04, 0x14 };
34
35const byte SHA_224_PKCS_ID[] = {
360x30, 0x2D, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
370x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C };
38
39const byte SHA_256_PKCS_ID[] = {
400x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
410x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
42
43const byte SHA_384_PKCS_ID[] = {
440x30, 0x41, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
450x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30 };
46
47const byte SHA_512_PKCS_ID[] = {
480x30, 0x51, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
490x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 };
50
51const byte TIGER_PKCS_ID[] = {
520x30, 0x29, 0x30, 0x0D, 0x06, 0x09, 0x2B, 0x06, 0x01, 0x04,
530x01, 0xDA, 0x47, 0x0C, 0x02, 0x05, 0x00, 0x04, 0x18 };
54
55}
56
57/*
58* HashID as specified by PKCS
59*/
60MemoryVector<byte> pkcs_hash_id(const std::string& name)
61 {
62 // Special case for SSL/TLS RSA signatures
63 if(name == "Parallel(MD5,SHA-160)")
64 return MemoryVector<byte>();
65
66 if(name == "MD2")
67 return MemoryVector<byte>(MD2_PKCS_ID, sizeof(MD2_PKCS_ID));
68 if(name == "MD5")
69 return MemoryVector<byte>(MD5_PKCS_ID, sizeof(MD5_PKCS_ID));
70 if(name == "RIPEMD-128")
71 return MemoryVector<byte>(RIPEMD_128_PKCS_ID, sizeof(RIPEMD_128_PKCS_ID));
72 if(name == "RIPEMD-160")
73 return MemoryVector<byte>(RIPEMD_160_PKCS_ID, sizeof(RIPEMD_160_PKCS_ID));
74 if(name == "SHA-160")
75 return MemoryVector<byte>(SHA_160_PKCS_ID, sizeof(SHA_160_PKCS_ID));
76 if(name == "SHA-224")
77 return MemoryVector<byte>(SHA_224_PKCS_ID, sizeof(SHA_224_PKCS_ID));
78 if(name == "SHA-256")
79 return MemoryVector<byte>(SHA_256_PKCS_ID, sizeof(SHA_256_PKCS_ID));
80 if(name == "SHA-384")
81 return MemoryVector<byte>(SHA_384_PKCS_ID, sizeof(SHA_384_PKCS_ID));
82 if(name == "SHA-512")
83 return MemoryVector<byte>(SHA_512_PKCS_ID, sizeof(SHA_512_PKCS_ID));
84 if(name == "Tiger(24,3)")
85 return MemoryVector<byte>(TIGER_PKCS_ID, sizeof(TIGER_PKCS_ID));
86
87 throw Invalid_Argument("No PKCS #1 identifier for " + name);
88 }
89
90/*
91* HashID as specified by IEEE 1363/X9.31
92*/
93byte ieee1363_hash_id(const std::string& name)
94 {
95 if(name == "SHA-160") return 0x33;
96
97 if(name == "SHA-224") return 0x38;
98 if(name == "SHA-256") return 0x34;
99 if(name == "SHA-384") return 0x36;
100 if(name == "SHA-512") return 0x35;
101
102 if(name == "RIPEMD-160") return 0x31;
103 if(name == "RIPEMD-128") return 0x32;
104
105 if(name == "Whirlpool") return 0x37;
106
107 return 0;
108 }
109
110}
MemoryVector< byte > pkcs_hash_id(const std::string &name)
Definition hash_id.cpp:60
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
byte ieee1363_hash_id(const std::string &name)
Definition hash_id.cpp:93