d5b7bfe0171833e919350732b42410a30f936bcd
[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 encrypt(const CryptoAlgorithm &, const RawBuffer &);
60     virtual RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &);
61     virtual RawBuffer getBinary() const;
62     virtual ~AKey(){}
63 protected:
64     virtual EvpShPtr getEvpShPtr();
65
66     EvpShPtr m_evp;
67     RawBuffer m_key;
68     DataType m_type;
69 };
70
71 class Cert : public AKey {
72 public:
73     Cert(RawBuffer buffer, DataType dataType)
74       : AKey(std::move(buffer), dataType)
75     {}
76     virtual ~Cert(){}
77 protected:
78     virtual EvpShPtr getEvpShPtr();
79 };
80
81 } // namespace SW
82 } // namespace Crypto
83 } // namespace CKM
84