1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use core::fmt;
use core::ptr::NonNull;
use core::ops::{ Deref, DerefMut };

#[cfg(feature = "use_os")]
use core::cell::Cell;

#[cfg(feature = "use_os")]
use memsec::{ mprotect, Prot };


#[cfg(feature = "use_os")]
mod alloc {
    use std::ptr::NonNull;

    #[inline]
    pub unsafe fn malloc_sized(size: usize) -> Option<NonNull<u8>> {
        let memptr = memsec::malloc_sized(size)?;
        Some(memptr.cast())
    }

    pub unsafe fn free(memptr: NonNull<u8>, _size: usize) {
        memsec::free(memptr);
    }
}

#[cfg(not(feature = "use_os"))]
mod alloc {
    use std::ptr::NonNull;
    use std::alloc::Layout;

    #[inline]
    pub unsafe fn malloc_sized(size: usize) -> Option<NonNull<u8>> {
        NonNull::new(std::alloc::alloc(Layout::from_size_align_unchecked(size, 1)))
    }

    #[inline]
    pub unsafe fn free(memptr: NonNull<u8>, size: usize) {
        std::alloc::dealloc(memptr.as_ptr(), Layout::from_size_align_unchecked(size, 1));
    }
}

pub struct SecBytes {
    ptr: NonNull<u8>,
    len: usize,

    #[cfg(feature = "use_os")]
    count: Cell<usize>
}

// Safety: It is safe to make SecBytes sendable because `ptr` is only used
//         by us and it doesn't have any thread specific behavior.
unsafe impl Send for SecBytes {}

impl SecBytes {
    pub fn new(len: usize) -> SecBytes {
        fn id(_: &mut [u8]) {}

        SecBytes::with(len, id)
    }

    pub fn with<F>(len: usize, f: F) -> SecBytes
        where F: FnOnce(&mut [u8])
    {
        let ptr = unsafe {
            let memptr = alloc::malloc_sized(len).expect("seckey alloc failed");

            {
                let arr = std::slice::from_raw_parts_mut(memptr.as_ptr(), len);
                f(arr);
            }

            #[cfg(feature = "use_os")]
            mprotect(memptr, Prot::NoAccess);

            memptr
        };

        SecBytes {
            ptr, len,

            #[cfg(feature = "use_os")]
            count: Cell::new(0)
        }
    }

    /// Borrow Read
    ///
    /// ```
    /// use seckey::SecBytes;
    ///
    /// let secpass = SecBytes::with(8, |buf| buf.copy_from_slice(&[8u8; 8][..]));
    /// assert_eq!([8u8; 8], *secpass.read());
    /// ```
    #[cfg_attr(not(feature = "use_os"), inline)]
    pub fn read(&self) -> SecReadGuard<'_> {
        #[cfg(feature = "use_os")] {
            let count = self.count.get();
            self.count.set(count + 1);
            if count == 0 {
                unsafe { mprotect(self.ptr, Prot::ReadOnly) };
            }
        }

        SecReadGuard(self)
    }

    /// Borrow Write
    ///
    /// ```
    /// # use seckey::SecBytes;
    /// #
    /// # let mut secpass = SecBytes::with(8, |buf| buf.copy_from_slice(&[8u8; 8][..]));
    /// let mut wpass = secpass.write();
    /// wpass[0] = 0;
    /// assert_eq!([0, 8, 8, 8, 8, 8, 8, 8], *wpass);
    /// ```
    #[cfg_attr(not(feature = "use_os"), inline)]
    pub fn write(&mut self) -> SecWriteGuard<'_> {
        #[cfg(feature = "use_os")]
        unsafe {
            mprotect(self.ptr, Prot::ReadWrite)
        };

        SecWriteGuard(self)
    }
}

impl fmt::Debug for SecBytes {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_tuple("SecBytes")
            .field(&format_args!("{:p}", self.ptr))
            .finish()
    }
}

impl fmt::Pointer for SecBytes {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:p}", self.ptr)
    }
}

impl Drop for SecBytes {
    fn drop(&mut self) {
        unsafe {
            #[cfg(feature = "use_os")]
            mprotect(self.ptr, Prot::ReadWrite);

            alloc::free(self.ptr, self.len);
        }
    }
}


/// Read Guard
pub struct SecReadGuard<'a>(&'a SecBytes);

impl<'a> Deref for SecReadGuard<'a> {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &[u8] {
        unsafe {
            std::slice::from_raw_parts(self.0.ptr.as_ptr(), self.0.len)
        }
    }
}

impl<'a> Drop for SecReadGuard<'a> {
    fn drop(&mut self) {
        #[cfg(feature = "use_os")]
        unsafe {
            let count = self.0.count.get();
            self.0.count.set(count - 1);
            if count <= 1 {
                mprotect(self.0.ptr, Prot::NoAccess);
            }
        }
    }
}


/// Write Guard
pub struct SecWriteGuard<'a>(&'a mut SecBytes);

impl<'a> Deref for SecWriteGuard<'a> {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &[u8] {
        unsafe {
            std::slice::from_raw_parts(self.0.ptr.as_ptr(), self.0.len)
        }
    }
}

impl<'a> DerefMut for SecWriteGuard<'a> {
    #[inline]
    fn deref_mut(&mut self) -> &mut [u8] {
        unsafe {
            std::slice::from_raw_parts_mut(self.0.ptr.as_ptr(), self.0.len)
        }
    }
}

impl<'a> Drop for SecWriteGuard<'a> {
    fn drop(&mut self) {
        #[cfg(feature = "use_os")]
        unsafe {
            mprotect(self.0.ptr, Prot::NoAccess);
        }
    }
}