Marmos
v juptune
v asn1
v lang
v ast
v Classes
v Enums
v ir
v parser
v Enums
v Structs
v core
v crypto
v keyexchange
v memory
v data
v alphabet
v buffer
v json
v builder
v parser
v Enums
v Structs
v serialise
v Structs
v Templates
v pem
v Enums
v Structs

Structs SecureMemory

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
module juptune.crypto.memory;

struct SecureMemory
{
    /++ STRUCTS ++/
    
    /// A loose wrapper around a T[] that originates from a SecureMemory struct. 
    struct Slice(T)
    {
        /++ FUNCTIONS ++/
        
        ///
        T[] memory();
        ///
        bool mustBeContiguous( other);
        ///
        bool mustNotOverlap( other);
        ///
         opSlice(size_t _)(size_t start, size_t end);
        ///
         reinterpret(NewT)();
        ///
        const  reinterpret(NewT)();
        
        /++ VARIABLES ++/
        
        ///
        T[] _memory;
    }
    
    /++ ALIASES ++/
    
    ///
    alias __xdtor = __MARMOS_BUG__;
    ///
    alias __xpostblit = __MARMOS_BUG__;
    /// Sets the memory to no access. 
    alias makeNoAccess = __MARMOS_BUG__;
    /// Sets the memory to read-only. 
    alias makeReadOnly = __MARMOS_BUG__;
    /// Sets the memory to read/write. 
    alias makeReadWrite = __MARMOS_BUG__;
    /// Sets the memory to write-only. 
    alias makeWriteOnly = __MARMOS_BUG__;
    ///
    alias opDollar = __MARMOS_BUG__;
    
    /++ FUNCTIONS ++/
    
    /// Provides a safe(ish) way to access the underlying user memory. 
    void access(void function(void[]) func);
    /// ditto 
    void access(void function(void[]) func);
    /// Allocates a new SecureMemory struct, with the given length. 
    static Result allocate(SecureMemory memory, ulong length);
    /// Compares this memory to another memory in constant time for the given length of this memory. 
    bool constantTimeCompare(const const void[] other);
    /// ditto. 
    bool constantTimeCompare(SecureMemory other);
    /// Slices the memory into the specified slices, ensuring that the slices do not overlap. 
    void contigiousSlice(T)(size_t[] lengths, *[] slices);
    ///
    ulong length();
    ///
    SecureMemory opAssign(SecureMemory p);
    /// Creates a SecureMemory.Slice from the specified memory range. 
     opSlice(ulong _ = 0)(size_t start, size_t end);
    /// Unless you realllllly need this, just use access or constantTimeCompare instead. 
    void[] unsafeSlice();
    
}

Documentation

Memory for sensitive data - hardened using OS memory protection mechanisms, as well as providing constant-time operations to help mitigate timing attacks.

Performance

Please note that this struct has to make a lot of syscalls, and is therefore pretty slow compared to general-purpose memory, especially due to mmap and munmap.

You should only use this for secret data, and potentially setup something like an object pool to reuse the memory if you need to do a lot of operations on it, as even deallocation is slow.

Kind reminder to never let performance get in the way of security - benchmark results matter a lot less than security results.

Technical

This struct directly uses mmap and munmap for memory allocation and deallocation.

The user memory range is locked in RAM using mlock, so that it probably won't be swapped; it is also marked as MADV_DONTDUMP, so that it probably won't be dumped to disk e.g. by a core dump.

More minorly, the memory is also marked as MADV_WIPEONFORK, so that it is wiped on fork, in case you use fork() + Juptune for some reason (which isn't supported anyway, btw)

Given a desired length of 1024 bytes, and a page size of 4096 bytes: - Two guard pages are allocated, one before the user memory, and one after the user memory. - A page for the user memory is allocated. (1024 < 4096)

The memory layout is as follows:

======================================== = Guard Page (1 page) = ======================================== = User Memory (enough pages as needed) = ======================================== = Guard Page (1 page) = ========================================

The guard pages are guard pages - they will crash on access.

The user memory is a set of (by default) read/write pages. The slice returned to the user ends at the very last page, so that any overflow will be caught by the guard page.

The start of the user memory therefore is unused by the user. This is filled with a canary value of 0xCC, which is checked on deallocation to ensure that no data has underflowed out of the user memory.

The guard page at the end is... also a guard page - it will crash on access.

Aliases

Name Description
__xdtor
__xpostblit
makeNoAccess

Sets the memory to no access.

makeReadOnly

Sets the memory to read-only.

makeReadWrite

Sets the memory to read/write.

makeWriteOnly

Sets the memory to write-only.

opDollar

Functions

Name Description
access

Provides a safe(ish) way to access the underlying user memory.

access

ditto

allocate

Allocates a new SecureMemory struct, with the given length.

constantTimeCompare

Compares this memory to another memory in constant time for the given length of this memory.

constantTimeCompare

ditto.

contigiousSlice

Slices the memory into the specified slices, ensuring that the slices do not overlap.

length
opAssign
opSlice

Creates a SecureMemory.Slicefrom the specified memory range.

unsafeSlice

Unless you realllllly need this, just use accessor constantTimeCompareinstead.

Structs

Name Description
Slice

A loose wrapper around a T[]that originates from a SecureMemorystruct.