Add support for AUTHENTICATION_FAILED code in getData function.
[platform/core/security/key-manager.git] / src / manager / common / digest.cpp
1 /*
2  * Copyright (c) 2014 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 #include <dpl/log/log.h>
18
19 #include <openssl/evp.h>
20
21 #include <digest.h>
22
23 namespace CKM {
24
25 Digest::Digest() :
26     m_digest(EVP_MAX_MD_SIZE)
27 {
28     m_ctx = nullptr;
29     m_md = EVP_sha1();
30     m_initialized = false;
31     m_finalized = false;
32 }
33
34 Digest::~Digest()
35 {
36     EVP_MD_CTX_destroy(m_ctx);
37 }
38
39 void Digest::reset()
40 {
41     int ret = -1;
42
43     if (m_initialized) {
44         EVP_MD_CTX_destroy(m_ctx);
45         m_ctx = nullptr;
46     }
47
48     m_initialized = false;
49     m_finalized = false;
50     m_ctx = EVP_MD_CTX_create();
51     if (m_ctx == nullptr) {
52     }
53
54     ret = EVP_DigestInit_ex(m_ctx, m_md, NULL);
55     if (ret != 1) {
56         ThrowMsg(Exception::InternalError,
57                  "Failed to create digest context.");
58     }
59     m_digest.clear();
60     m_digest.resize(EVP_MAX_MD_SIZE);
61     m_initialized = true;
62 }
63
64 void Digest::append(const RawBuffer &data, std::size_t len)
65 {
66     int ret = -1;
67
68     if (data.size() == 0) {
69         ThrowMsg(Exception::InternalError, "Empty data.");
70     }
71     if (0 == len)
72         len = data.size();
73     if (m_finalized) {
74         ThrowMsg(Exception::InternalError, "Already finalized.");
75     }
76     if (not m_initialized)
77         reset();
78     ret = EVP_DigestUpdate(m_ctx, data.data(), len);
79     if (ret != 1) {
80         ThrowMsg(Exception::InternalError,
81                  "Failed to calculate digest in openssl.");
82     }
83 }
84
85 RawBuffer Digest::finalize()
86 {
87     int ret = -1;
88     unsigned int dlen;
89
90     if (m_finalized) {
91         ThrowMsg(Exception::InternalError, "Already finalized.");
92     }
93     m_finalized = true;
94     ret = EVP_DigestFinal_ex(m_ctx, m_digest.data(), &dlen);
95     if (ret != 1) {
96         ThrowMsg(Exception::InternalError,
97                  "Failed in digest final in openssl.");
98     }
99     if (dlen != length()) {
100         ThrowMsg(Exception::InternalError, "Invalid digest length.");
101     }
102     if (dlen != EVP_MAX_MD_SIZE)
103         m_digest.resize(dlen);
104     return m_digest;
105 }
106
107 RawBuffer Digest::get()
108 {
109     if (m_finalized)
110         return m_digest;
111     else
112         return RawBuffer();
113 }
114
115 unsigned int Digest::length()
116 {
117     return m_md->md_size;
118 }
119
120 } // namespace CKM
121