KBKDF HMAC implementation in sw backend
[platform/core/security/key-manager.git] / src / manager / crypto / sw-backend / obj.h
1 /*
2  *  Copyright (c) 2000-2020 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       obj.h
18  * @author     BartÅ‚omiej Grzelewski (b.grzelewski@samsung.com)
19  * @version    1.0
20  */
21 #pragma once
22 #include <functional>
23 #include <memory>
24
25 #include <openssl/evp.h>
26
27 #include <generic-backend/gobj.h>
28 #include <generic-backend/gstore.h>
29 #include <data-type.h>
30
31 namespace CKM {
32 namespace Crypto {
33 namespace SW {
34
35 typedef std::shared_ptr<EVP_PKEY> EvpShPtr;
36
37 class BData : public GObj {
38 public:
39         BData(CryptoBackend backendId, RawBuffer buffer, DataType keyType) :
40                 GObj(backendId), m_raw(std::move(buffer)), m_type(keyType) {}
41
42         RawBuffer getBinary() const override
43         {
44                 return m_raw;
45         }
46         Token derive(const CryptoAlgorithm &, const Password &, const RawBuffer &) override;
47
48 protected:
49         RawBuffer m_raw;
50         DataType m_type;
51 };
52
53 class SKey : public BData {
54 public:
55         SKey(CryptoBackend backendId, RawBuffer buffer, DataType keyType) :
56                 BData(backendId, std::move(buffer), keyType) {}
57
58         RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &) override;
59         RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &) override;
60 };
61
62 class AKey : public BData {
63 public:
64         AKey(CryptoBackend backendId, RawBuffer buffer, DataType dataType) :
65                 BData(backendId, std::move(buffer), dataType) {}
66
67         RawBuffer sign(const CryptoAlgorithm &alg, const RawBuffer &message) override;
68         int verify(const CryptoAlgorithm &alg,
69                    const RawBuffer &message,
70                    const RawBuffer &sign) override;
71         RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &) override;
72         RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &) override;
73         Token derive(const CryptoAlgorithm &, const Password &, const RawBuffer &) override;
74
75 protected:
76         virtual EvpShPtr getEvpShPtr();
77
78         EvpShPtr m_evp;
79 };
80
81 class Cert : public AKey {
82 public:
83         Cert(CryptoBackend backendId, RawBuffer buffer, DataType dataType) :
84                 AKey(backendId, std::move(buffer), dataType) {}
85         Token derive(const CryptoAlgorithm &, const Password &, const RawBuffer &) override;
86
87 protected:
88         EvpShPtr getEvpShPtr() override;
89 };
90
91 } // namespace SW
92 } // namespace Crypto
93 } // namespace CKM
94