Add a common function for zeroing sensitive data
[platform/core/security/key-manager.git] / src / include / ckm / ckm-raw-buffer.h
1 /* Copyright (c) 2014 Samsung Electronics Co.
2  *
3  *  Licensed under the Apache License, Version 2.0 (the "License");
4  *  you may not use this file except in compliance with the License.
5  *  You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software
10  *  distributed under the License is distributed on an "AS IS" BASIS,
11  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  *  See the License for the specific language governing permissions and
13  *  limitations under the License
14  *
15  *
16  * @file        ckm-raw-buffer.h
17  * @author      Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
18  * @version     1.0
19  * @brief       Custom allocator for std
20  */
21
22 #ifndef _SAFE_BUFFER_H_
23 #define _SAFE_BUFFER_H_
24
25 #include <stddef.h>
26 #include <vector>
27
28 #include <ckm/ckm-zero-memory.h>
29
30 namespace CKM {
31
32 template <typename T>
33 struct std_erase_on_dealloc {
34         // MJK: if re-factoring, remember not to inherit from the std::allocator !
35         // MJK: to be replaced with much shorter version once std::allocator_traits
36         // becomes supported in STL containers (i.e. list, vector and string)
37         typedef size_t    size_type;
38         typedef ptrdiff_t difference_type;
39         typedef T        *pointer;
40         typedef const T  *const_pointer;
41         typedef T        &reference;
42         typedef const T  &const_reference;
43         typedef T         value_type;
44
45         std_erase_on_dealloc() = default;
46
47         template <typename U>
48         std_erase_on_dealloc(const std_erase_on_dealloc<U> &) {}
49
50         T *allocate(std::size_t n)
51         {
52                 return static_cast<T *>(::operator new(n * sizeof(T)));
53         }
54
55         void deallocate(T *ptr, std::size_t n)
56         {
57                 // clear the memory before deleting
58                 ZeroMemory(reinterpret_cast<unsigned char*>(ptr), n * sizeof(T));
59                 ::operator delete(ptr);
60         }
61
62         template<typename _Tp1>
63         struct rebind {
64                 typedef std_erase_on_dealloc<_Tp1> other;
65         };
66
67         void construct(pointer p, const T &val)
68         {
69                 new(p) T(val);
70         }
71
72         void destroy(pointer p)
73         {
74                 p->~T();
75         }
76
77         size_type max_size() const
78         {
79                 return size_type(-1);
80         }
81 };
82
83 template <typename T, typename U>
84 inline bool operator==(const std_erase_on_dealloc<T> &,
85                                            const std_erase_on_dealloc<U> &)
86 {
87         return true;
88 }
89
90 template <typename T, typename U>
91 inline bool operator!=(const std_erase_on_dealloc<T> &a,
92                                            const std_erase_on_dealloc<U> &b)
93 {
94         return !(a == b);
95 }
96
97
98 /*
99  * TODO replace with:
100  *
101  *  template <typename T>
102  *  using SafeBuffer = std::vector<T, erase_on_dealloc<T>>;
103  *
104  *  typedef SafeBuffer<unsigned char> RawBuffer
105  *
106  * when gcc 4.7/4.8 is available.
107  */
108 template <typename T>
109 struct SafeBuffer {
110         typedef std::vector<T, std_erase_on_dealloc<T>> Type;
111 };
112
113 // used to pass password and raw key data
114 typedef SafeBuffer<unsigned char>::Type RawBuffer;
115
116 } // namespace CKM
117
118 #endif //_SAFE_BUFFER_H_