df331ec59bb261f9997705278d15b676209b773b
[platform/core/security/key-manager.git] / src / manager / crypto / sw-backend / key.h
1 /*
2  *  Copyright (c) 2000 - 2015 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       key.h
18  * @author     BartÅ‚omiej Grzelewski (b.grzelewski@samsung.com)
19  * @version    1.0
20  */
21 #pragma once
22 #include <memory>
23
24 #include <openssl/evp.h>
25
26 #include <generic-backend/gkey.h>
27 #include <data-type.h>
28
29 namespace CKM {
30 namespace Crypto {
31 namespace SW {
32
33 typedef std::unique_ptr<EVP_PKEY_CTX,std::function<void(EVP_PKEY_CTX*)>> ContextUPtr;
34 typedef std::shared_ptr<EVP_PKEY> EvpShPtr;
35
36 class SKey : public GKey {
37 public:
38     SKey(RawBuffer buffer, DataType keyType)
39       : m_key(std::move(buffer))
40       , m_type(keyType)
41     {}
42
43     virtual RawBuffer getBinary() const;
44     virtual RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &);
45     virtual RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &);
46 protected:
47     RawBuffer m_key;
48     DataType m_type;
49 };
50
51 class AKey : public GKey {
52 public:
53     AKey(RawBuffer buffer, DataType dataType)
54       : m_key(std::move(buffer))
55       , m_type(dataType)
56     {}
57     virtual RawBuffer sign(const CryptoAlgorithm &alg, const RawBuffer &message);
58     virtual int verify(const CryptoAlgorithm &alg, const RawBuffer &message, const RawBuffer &sign);
59     virtual RawBuffer getBinary() const;
60     virtual ~AKey(){}
61 protected:
62     virtual EvpShPtr getEvpShPtr();
63
64     EvpShPtr m_evp;
65     RawBuffer m_key;
66     DataType m_type;
67 };
68
69 class Cert : public AKey {
70 public:
71     Cert(RawBuffer buffer, DataType dataType)
72       : AKey(std::move(buffer), dataType)
73     {}
74     virtual ~Cert(){}
75 protected:
76     virtual EvpShPtr getEvpShPtr();
77 };
78
79 } // namespace SW
80 } // namespace Crypto
81 } // namespace CKM
82