5611dafef97688de01b62892bd7b84bd4de3b66b
[platform/core/security/cert-svc.git] / vcore / src / vcore / CryptoHash.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        crypto_hash.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of cryptographic hasing algorithms
21  */
22 #ifndef _CRYPTO_HASH_H_
23 #define _CRYPTO_HASH_H_
24
25 #include <openssl/evp.h>
26 #include <istream>
27 #include <string>
28 #include <vector>
29
30 #include <vcore/exception.h>
31
32 namespace ValidationCore
33 {
34 namespace Crypto
35 {
36 namespace Hash
37 {
38 typedef std::vector<unsigned char> Raw;
39
40 VCORE_DECLARE_EXCEPTION_TYPE(ValidationCore::Exception, OutOfSequence)
41 VCORE_DECLARE_EXCEPTION_TYPE(ValidationCore::Exception, AppendFailed)
42
43 class Base
44 {
45 private:
46     Raw m_raw;
47     std::string m_base64StringHash;
48     bool m_hasFinal;
49
50 protected:
51     virtual void HashUpdate(const void *data, size_t dataSize) = 0;
52     virtual Raw HashFinal() = 0;
53
54 public:
55     Base();
56     virtual ~Base();
57
58     virtual void Append(const char *buffer);
59     virtual void Append(const char *buffer, size_t bufferSize);
60     virtual void Append(const std::string &buffer);
61     virtual void Append(std::istream &stream);
62     virtual void Append(const void *data, size_t dataSize);
63
64     virtual void Finish();
65
66     virtual std::string ToBase64String() const;
67     virtual Raw GetHash() const;
68 };
69
70 /**
71  * OpenSSL hashing algorithm base
72  */
73 class OpenSSL
74     : public Base
75 {
76 private:
77     EVP_MD_CTX m_context;
78     bool m_finalized;
79
80 protected:
81     virtual void HashUpdate(const void *data, size_t dataSize);
82     virtual Raw HashFinal();
83
84 public:
85     OpenSSL(const EVP_MD *evpMd);
86     virtual ~OpenSSL();
87 };
88
89 #define DECLARE_OPENSSL_HASH_ALGORITHM(ClassName, EvpMd) \
90     class ClassName                                      \
91         : public OpenSSL                                 \
92     {                                                    \
93     public:                                              \
94         ClassName() : OpenSSL(EvpMd()) {}                \
95         virtual ~ClassName() {}                          \
96     };
97
98 DECLARE_OPENSSL_HASH_ALGORITHM(MD2, EVP_md2)
99 DECLARE_OPENSSL_HASH_ALGORITHM(MD4, EVP_md4)
100 DECLARE_OPENSSL_HASH_ALGORITHM(MD5, EVP_md5)
101 DECLARE_OPENSSL_HASH_ALGORITHM(SHA, EVP_sha)
102 DECLARE_OPENSSL_HASH_ALGORITHM(SHA1, EVP_sha1)
103 DECLARE_OPENSSL_HASH_ALGORITHM(DSS, EVP_dss)
104 DECLARE_OPENSSL_HASH_ALGORITHM(DSS1, EVP_dss1)
105 DECLARE_OPENSSL_HASH_ALGORITHM(ECDSA, EVP_ecdsa)
106 DECLARE_OPENSSL_HASH_ALGORITHM(SHA224, EVP_sha224)
107 DECLARE_OPENSSL_HASH_ALGORITHM(SHA256, EVP_sha256)
108 DECLARE_OPENSSL_HASH_ALGORITHM(SHA384, EVP_sha384)
109 DECLARE_OPENSSL_HASH_ALGORITHM(SHA512, EVP_sha512)
110
111 #undef DECLARE_OPENSSL_HASH_ALGORITHM
112
113 } // namespace Hash
114 } // namespace Crypto
115 } // namespace ValidationCore
116
117 #endif // DPL_CRYPTO_HASH_H