encrypted_pointerClass
Template class for managing encrypted pointers. More...
Public Functions
Type | Name |
---|---|
constexpr | encrypted_pointer () noexcept |
constexpr | encrypted_pointer (T *_Ptr) noexcept |
encrypted_pointer & | operator= (encrypted_pointer &&_Right) noexcept |
encrypted_pointer< T > & | operator= (T *other) |
encrypted_pointer (const encrypted_pointer &other)=delete | |
encrypted_pointer & | operator= (const encrypted_pointer &other)=delete |
~encrypted_pointer () | |
T * | get () const noexcept |
T & | operator* () |
T & | operator* () const |
T * | operator-> () const |
T * | operator-> () |
bool | operator== (const std::nullptr_t &rhs) const |
bool | operator== (const std::nullptr_t &rhs) |
operator bool () const |
Private Functions
Type | Name |
---|---|
ANTISPY_ENCRYPTED_VALUE (intptr_t, value_, 0) |
Friends
Type | Name |
---|---|
std::ostream & | operator<< (std::ostream &os, encrypted_pointer< T > &obj) |
Detailed Description
The encrypted_pointer
class template provides a secure mechanism for storing and managing pointers in an encrypted form. This class is particularly useful in scenarios where protecting pointers from unauthorized access, reverse engineering, or tampering is critical. The pointer's value is encrypted internally, ensuring that even if memory is inspected, the actual pointer value is not easily retrievable.
Template Parameters
T
: The type of the object that the pointer points to.
Features:
Pointer Encryption: The pointer value is encrypted before storage, enhancing security.
Automatic Memory Management: The class automatically manages the memory associated with the pointer, ensuring proper cleanup.
Safe Dereferencing: The class provides operators for safe dereferencing and member access.
Move Semantics: The class supports move operations, allowing efficient transfer of ownership.
Nullability Checks: Provides convenient operators to check if the pointer is null.
Stream Output: Allows the encrypted pointer's value to be output to a stream for debugging or logging purposes.
Example Usage:
int
This example demonstrates the creation of an encrypted_pointer
to an integer, with safe dereferencing and output.
Public Functions
constexpr encrypted_pointer () noexcept
Default constructor for the encrypted_pointer
class.
Definition
constexpr libantispy::encrypted_pointer< T >::encrypted_pointer
Detailed Description
This constructor initializes an encrypted_pointer
object with a nullptr
value, ensuring that the pointer is safely stored in an encrypted form. This constructor is constexpr, allowing for compile-time initialization, and noexcept, guaranteeing that it does not throw exceptions.
Detailed Behavior:
Initialization :
The
value_
member is initialized with thenullptr
value, which is safely stored after being cast to anintptr_t
and then encrypted.reinterpret_cast<intptr_t>(nullptr)
is used to convert thenullptr
to an integer type that can be stored invalue_
.
constexpr :
- The
constexpr
specifier allows this constructor to be used in constant expressions, enabling compile-time initialization ofencrypted_pointer
objects.
- The
noexcept :
- The constructor is marked as
noexcept
, ensuring that it does not throw any exceptions, making it safe for use in exception-sensitive contexts or performance-critical code.
- The constructor is marked as
Usage Example:
int
This constructor is useful when you want to create an encrypted_pointer
object that starts off pointing to nothing, but can later be assigned a valid pointer.
constexpr encrypted_pointer (T *_Ptr) noexcept
Constructs an encrypted_pointer
object from a raw pointer.
Definition
constexpr libantispy::encrypted_pointer< T >::encrypted_pointer
Detailed Description
This constructor initializes an encrypted_pointer
object with a specified raw pointer. The provided pointer is encrypted and stored securely within the object. This constructor is constexpr
, allowing for compile-time initialization, and noexcept
, ensuring it does not throw exceptions.
Parameters
_Ptr
: The raw pointer to be encrypted and securely stored. This pointer is of typeT*
, whereT
is the template parameter representing the type of the object pointed to.
Detailed Behavior:
Initialization :
The
value_
member is initialized by first converting the provided raw pointer_Ptr
to anintptr_t
usingreinterpret_cast<intptr_t>(_Ptr)
. This conversion allows the pointer to be stored in a format suitable for encryption and secure storage.The raw pointer
_Ptr
is then encrypted and stored invalue_
, making the stored pointer resistant to direct memory inspection or tampering.
explicit :
- The
explicit
specifier prevents implicit conversions from raw pointers toencrypted_pointer
objects, ensuring that such conversions must be intentional and explicit in the code. This helps avoid potential bugs or unintended behavior caused by implicit conversions.
- The
constexpr :
- The
constexpr
specifier allows this constructor to be used in constant expressions, enabling compile-time initialization ofencrypted_pointer
objects when the provided pointer is also available at compile time.
- The
noexcept :
- The constructor is marked as
noexcept
, ensuring that it does not throw any exceptions. This makes it safe to use in exception-sensitive contexts or performance-critical code where exception safety is required.
- The constructor is marked as
Usage Example:
int
This constructor is particularly useful when you need to securely store a raw pointer in an encrypted_pointer
object, ensuring that the pointer is protected against unauthorized access or manipulation.
encrypted_pointer & operator= (encrypted_pointer &&_Right) noexcept
Move assignment operator for encrypted_pointer
.
Definition
encrypted_pointer &
Detailed Description
This operator transfers ownership of the encrypted pointer from another encrypted_pointer
object to this object. It deletes the current managed pointer, if any, and then takes ownership of the pointer from the right-hand side ( _Right
) object. The _Right
object is left in a valid but unspecified state (typically set to nullptr
).
Parameters
_Right
: Theencrypted_pointer
object from which ownership is being transferred. After the assignment,_Right
will no longer manage the pointer and will typically be set tonullptr
.
Returns
A reference to this encrypted_pointer
object, now managing the pointer that was previously managed by _Right
.
Detailed Behavior:
Deleting the Current Pointer :
The operator begins by storing the current encrypted pointer value (
value_
) in a local variables
.The current pointer managed by this object is then deleted using
delete reinterpret_cast<T*>(s);
.This ensures that any dynamically allocated memory managed by the original pointer is properly freed before the assignment.
Transferring Ownership :
The value from
_Right.value_
is assigned tovalue_
, effectively transferring the ownership of the pointer to this object._Right.value_
is then set tonullptr
(represented byreinterpret_cast<intptr_t>(nullptr)
), ensuring that_Right
no longer manages the pointer.
Return Value :
- The operator returns a reference to the current object (
*this
), now managing the pointer that was originally managed by_Right
.
- The operator returns a reference to the current object (
noexcept :
- The operator is marked as
noexcept
, ensuring that it does not throw exceptions. This is crucial for performance-sensitive code or when usingencrypted_pointer
in contexts where exception safety is required.
- The operator is marked as
Usage Example:
int
This move assignment operator is particularly useful for managing the ownership of dynamically allocated pointers, ensuring that resources are correctly managed and that no double-deletions or memory leaks occur.
encrypted_pointer< T > & operator= (T *other)
Assignment operator for assigning a raw pointer to an encrypted_pointer
object.
Definition
encrypted_pointer< T > &
Detailed Description
This operator allows an encrypted_pointer
object to be assigned a raw pointer. The raw pointer is stored in an encrypted form within the encrypted_pointer
object, replacing any previously stored pointer. This operator is useful when you want to directly assign a new raw pointer to the encrypted_pointer
without having to create a temporary encrypted_pointer
object.
Parameters
other
: The raw pointer of typeT*
that will be assigned to thisencrypted_pointer
object. This pointer will be stored in an encrypted form within theencrypted_pointer
.
Returns
A reference to this encrypted_pointer
object after the assignment, now containing the encrypted form of the other
pointer.
Detailed Behavior:
Storing the New Pointer :
The raw pointer
other
is converted to an integer type (intptr_t
) usingreinterpret_cast<intptr_t>(other)
.This integer value is then assigned to the member variable
value_
, which stores the encrypted form of the pointer.Any previously stored pointer in
value_
is overwritten, meaning that if theencrypted_pointer
was managing a different pointer, that pointer is effectively replaced without being explicitly deleted.
Return Value :
- The operator returns a reference to the current
encrypted_pointer
object (*this
), now containing the encrypted representation of the new pointer.
- The operator returns a reference to the current
Usage Considerations :
This operator does not manage the lifetime of the pointer being assigned. It only stores the pointer in an encrypted form.
If the
encrypted_pointer
previously managed a dynamically allocated object, and that object is no longer needed, it must be deleted before assigning a new pointer to avoid memory leaks.
Example Usage:
int
In this example, enc_ptr
is assigned a raw pointer raw_ptr
, which is stored in an encrypted form within the encrypted_pointer
object.
encrypted_pointer (const encrypted_pointer &other)=delete
Deleted copy constructor for the encrypted_pointer
class.
Definition
libantispy::encrypted_pointer< T >::encrypted_pointer
Detailed Description
This constructor is explicitly deleted to prevent copying of encrypted_pointer
objects. The encrypted_pointer
class is designed to manage pointers in an encrypted form, and allowing copying of these objects could lead to security risks such as multiple objects managing the same underlying pointer, potentially leading to double deletion or unintentional exposure of the pointer's value. To ensure safe and secure management of the pointers, copying is disabled.
Parameters
other
: Theencrypted_pointer
object that was attempted to be copied.
Detailed Behavior:
Copy Prevention :
By deleting the copy constructor, the compiler will prevent any attempt to copy an
encrypted_pointer
object. This includes scenarios like passing anencrypted_pointer
by value, returning it from a function, or explicitly trying to create a copy.If a copy operation is attempted, the compiler will generate an error, ensuring that the codebase is safe from unintended copying of
encrypted_pointer
objects.
Security Considerations :
Allowing copying could lead to scenarios where multiple
encrypted_pointer
objects manage the same pointer, which could result in double deletion (if both objects try to delete the pointer) or inconsistent states between the objects.By disabling copying, the integrity of the pointer management system is maintained, ensuring that only one
encrypted_pointer
object is responsible for the lifecycle of any given pointer.
Example Usage:
int
In this example, attempting to copy enc_ptr
into copy_ptr
would result in a compilation error, as the copy constructor is deleted.
encrypted_pointer & operator= (const encrypted_pointer &other)=delete
Deleted copy assignment operator for the encrypted_pointer
class.
Definition
encrypted_pointer &
Detailed Description
This operator is explicitly deleted to prevent the assignment of one encrypted_pointer
object to another. The encrypted_pointer
class is designed to manage pointers in an encrypted form, and allowing assignment between these objects could lead to security risks, such as multiple objects managing the same underlying pointer, which could result in double deletion or inconsistent states. To maintain safe and secure management of the pointers, assignment is disabled.
Parameters
other
: Theencrypted_pointer
object that was attempted to be assigned.
Returns
This function does not return anything as the copy assignment operator is deleted.
Detailed Behavior:
Assignment Prevention :
By deleting the copy assignment operator, the compiler will prevent any attempt to assign one
encrypted_pointer
object to another. This includes scenarios where an assignment operation likea = b;
is performed, where botha
andb
areencrypted_pointer
objects.If an assignment operation is attempted, the compiler will generate an error, ensuring that the codebase is safe from unintended assignment of
encrypted_pointer
objects.
Security Considerations :
Allowing assignment could lead to scenarios where two
encrypted_pointer
objects manage the same pointer, which could result in double deletion (if both objects try to delete the pointer) or inconsistent states between the objects.By disabling assignment, the integrity of the pointer management system is maintained, ensuring that only one
encrypted_pointer
object is responsible for the lifecycle of any given pointer.
Example Usage:
int
In this example, attempting to assign enc_ptr1
to enc_ptr2
would result in a compilation error, as the copy assignment operator is deleted.
~encrypted_pointer ()
Destructor for the encrypted_pointer
class.
Definition
Detailed Description
This destructor is responsible for safely cleaning up the encrypted_pointer
object when it is destroyed. It deletes the managed pointer (if it exists) and securely clears the stored encrypted pointer value to prevent any residual data from being left in memory.
Detailed Behavior:
Pointer Deletion :
The destructor first retrieves the stored encrypted pointer value by interpreting it as an
intptr_t
.It then casts this value back to the original pointer type
T*
and deletes the object that the pointer refers to. This ensures that the dynamically allocated memory is properly deallocated when theencrypted_pointer
object is destroyed.
Clearing the Stored Value :
After deleting the managed pointer, the destructor sets the stored pointer value (
value_
) to0LL
.This step is important for security, as it prevents any leftover pointer value from being accessible after the
encrypted_pointer
object has been destroyed. It helps mitigate potential security risks like use-after-free or memory leaks.
Example Usage:
int
In this example, when enc_ptr
goes out of scope, the destructor is automatically called. The dynamically allocated integer is deleted, and the stored pointer value is cleared.
T * get () const noexcept
Retrieves the raw pointer stored in the encrypted_pointer
.
Definition
T *
Detailed Description
The get
method returns the decrypted raw pointer that is managed by the encrypted_pointer
object. This allows access to the underlying pointer in its original form, enabling operations on the object it points to.
Returns
A pointer of type T*
that points to the original object.
Details:
The stored value,
value_
, is an encrypted representation of the pointer.This method decrypts the pointer by reinterpreting the stored value as a pointer of type
T*
.The method is marked as
noexcept
, indicating that it is guaranteed not to throw any exceptions during its execution.The
[[nodiscard]]
attribute suggests that the return value should not be discarded, as the raw pointer is typically important for subsequent operations.
Example Usage:
int
In this example, the get
method is used to obtain the raw pointer from the encrypted_pointer
. The raw pointer is then used to access the integer value.
T & operator* ()
Dereferences the encrypted_pointer
to access the object it points to.
Definition
T &
Detailed Description
The dereference operator ( *
) allows access to the object that the encrypted_pointer
manages. It returns a reference to the object of type T
that the pointer points to after decrypting it.
Returns
A reference to the object of type T
that the encrypted_pointer
points to.
Details:
The stored
value_
is an encrypted representation of the pointer.This method decrypts the pointer by reinterpreting
value_
as a pointer of typeT*
.The method then dereferences the decrypted pointer to return a reference to the underlying object.
The
[[nodiscard]]
attribute is used to indicate that the returned reference should not be ignored, as it provides direct access to the object being managed.
Example Usage:
int
In this example, the operator*
is used to dereference the encrypted_pointer
, allowing direct access to the integer value stored in the heap.
T & operator* () const
Dereferences the constencrypted_pointer to access the object it points to.
Definition
T &
Detailed Description
This const version of the dereference operator ( *
) allows access to the object that the encrypted_pointer
manages when the encrypted_pointer
itself is constant. It returns a const reference to the object of type T
that the pointer points to after decrypting it.
Returns
A const reference to the object of type T
that the encrypted_pointer
points to.
Details:
The stored
value_
is an encrypted representation of the pointer.This method decrypts the pointer by reinterpreting
value_
as a pointer of typeT*
.The method then dereferences the decrypted pointer to return a const reference to the underlying object, ensuring that the object cannot be modified through this reference.
The
[[nodiscard]]
attribute is used to indicate that the returned reference should not be ignored, as it provides direct access to the object being managed.Being a const method, this operator guarantees that calling it does not modify the state of the
encrypted_pointer
.
Example Usage:
int
In this example, the operator*
is used to dereference the encrypted_pointer
in a const
context, allowing direct, read-only access to the integer value stored in the heap.
T * operator-> () const
Provides access to the members of the object pointed to by the const encrypted_pointer
.
Definition
T *
Detailed Description
This const
version of the member access operator ( ->
) allows access to the members of the object managed by the encrypted_pointer
when the encrypted_pointer
itself is constant. It returns a decrypted pointer of type T*
to the managed object, enabling direct access to the object's members.
Returns
A pointer of type T*
to the object managed by the encrypted_pointer
.
Details:
The stored
value_
is an encrypted representation of the pointer.This method decrypts the pointer by reinterpreting
value_
as a pointer of typeT*
.The method returns this decrypted pointer, allowing direct access to the members of the object that
encrypted_pointer
points to, even in aconst
context.The
[[nodiscard]]
attribute is used to indicate that the returned pointer should not be ignored, as it provides direct access to the object's members.Being a
const
method, this operator guarantees that calling it does not modify the state of theencrypted_pointer
.
Example Usage:
;
int
In this example, the operator->
is used to access a method of the MyClass
object managed by the encrypted_pointer
, demonstrating how to work with the object's members through the encrypted pointer in a const
context.
T * operator-> ()
Provides access to the members of the object pointed to by the encrypted_pointer
.
Definition
T *
Detailed Description
This member access operator ( ->
) allows access to the members of the object managed by the encrypted_pointer
. It returns a decrypted pointer of type T*
to the managed object, enabling direct access to the object's members.
Returns
A pointer of type T*
to the object managed by the encrypted_pointer
.
Details:
The stored
value_
is an encrypted representation of the pointer.This method decrypts the pointer by reinterpreting
value_
as a pointer of typeT*
.The method returns this decrypted pointer, allowing direct access to the members of the object that
encrypted_pointer
points to.The
[[nodiscard]]
attribute is used to indicate that the returned pointer should not be ignored, as it provides direct access to the object's members.Unlike the
const
version, this operator allows modification of the object's members.
Example Usage:
int
In this example, the operator->
is used to access a method of the MyClass
object managed by the encrypted_pointer
, demonstrating how to work with the object's members through the encrypted pointer.
bool operator== (const std::nullptr_t &rhs) const
Compares the encrypted_pointer
with nullptr
for equality.
Definition
bool
Detailed Description
This equality operator ( ==
) checks whether the encrypted_pointer
is equal to nullptr
. It determines if the encrypted pointer currently holds a null pointer by comparing the encrypted value to 0LL
, which represents a null pointer in this context.
Parameters
rhs
: Thenullptr
value to compare against.
Returns
true
if the encrypted_pointer
is equal to nullptr
, false
otherwise.
Details:
This method is used to check if the
encrypted_pointer
is empty or uninitialized, which is represented by it being equal tonullptr
.The stored
value_
is decrypted and compared to0LL
(the encrypted representation of a null pointer).The method returns
true
if the decrypted value equals0LL
, indicating that theencrypted_pointer
does not point to any valid object.The
[[nodiscard]]
attribute is used to ensure that the result of the comparison is not ignored, which is crucial when verifying whether the pointer is valid or not.
Example Usage:
int
In this example, the operator==
is used to check if encrypted_pointer
is null, allowing the program to handle cases where the encrypted_pointer
does not point to a valid object.
bool operator== (const std::nullptr_t &rhs)
Compares the encrypted_pointer
with nullptr
for equality.
Definition
bool
Detailed Description
This equality operator ( ==
) checks whether the encrypted_pointer
is equal to nullptr
. It determines if the encrypted pointer currently holds a null pointer by comparing the encrypted value to 0LL
, which represents a null pointer in this context.
Parameters
rhs
: Thenullptr
value to compare against.
Returns
true
if the encrypted_pointer
is equal to nullptr
, false
otherwise.
Details:
This method is used to check if the
encrypted_pointer
is empty or uninitialized, which is represented by it being equal tonullptr
.The stored
value_
is decrypted and compared to0LL
(the encrypted representation of a null pointer).The method returns
true
if the decrypted value equals0LL
, indicating that theencrypted_pointer
does not point to any valid object.The
[[nodiscard]]
attribute is used to ensure that the result of the comparison is not ignored, which is crucial when verifying whether the pointer is valid or not.
Example Usage:
int
In this example, the operator==
is used to check if enc_ptr
is null, allowing the program to handle cases where the encrypted_pointer
does not point to a valid object.
operator bool () const
Checks if the encrypted_pointer
is not nullptr
.
Definition
libantispy::encrypted_pointer< T >::operator bool
Detailed Description
This conversion operator allows an encrypted_pointer
object to be implicitly converted to a bool
. The conversion checks whether the encrypted_pointer
is not equal to nullptr
. If the encrypted_pointer
holds a valid pointer, the conversion returns true
; otherwise, it returns false
.
Returns
true
if the encrypted_pointer
is not nullptr
, false
if it is nullptr
.
Details:
This operator facilitates the use of
encrypted_pointer
objects in conditional statements where a check fornullptr
is required.It internally compares the
encrypted_pointer
tonullptr
using theoperator==
method.The result of this comparison is inverted (
this == nullptr
) to returntrue
if the pointer is non-null andfalse
if it is null.This implicit conversion enhances the usability of
encrypted_pointer
, allowing it to be seamlessly used in logical expressions and control flow constructs.
Example Usage:
int
In this example, the operator bool()
is used to check if enc_ptr
points to a valid object. The if
statement relies on this conversion to determine whether the pointer is nullptr
.
Private Functions
ANTISPY_ENCRYPTED_VALUE (intptr_t, value_, 0)
Defines an encrypted member variable within the encrypted_pointer
class.
Definition
libantispy::encrypted_pointer< T >::ANTISPY_ENCRYPTED_VALUE
Detailed Description
The ANTISPY_ENCRYPTED_VALUE
macro is used to declare a member variable that is stored in an encrypted form. This macro typically expands to code that manages the encryption and decryption of the variable's value, ensuring that it is protected against unauthorized access or tampering.
Details:
Encryption : The
value_
member variable is not stored in plain form; instead, it is encrypted using a mechanism defined by theANTISPY_ENCRYPTED_VALUE
macro. This encryption is intended to protect the data from being easily accessed or modified by malicious actors.Decryption : When the
value_
needs to be accessed or modified, it is decrypted transparently, ensuring that the rest of the code interacts with it as if it were a regularintptr_t
variable.Security : The encryption applied to
value_
helps prevent reverse engineering, memory dumps, or other forms of unauthorized access from revealing or altering the actual pointer value stored in theencrypted_pointer
class.
Example:
;
In this example, the value_
member variable of type intptr_t
is declared with encryption. The actual data stored in value_
is encrypted and only decrypted when accessed through the appropriate methods, adding a layer of security to pointer management.
Friends
std::ostream & operator<< (std::ostream &os, encrypted_pointer< T > &obj)
Overloads the stream insertion operator ( <<
) for encrypted_pointer
.
Definition
std::ostream &
Detailed Description
This function allows an encrypted_pointer
object to be output to an std::ostream
. The managed pointer is dereferenced and its value is inserted into the stream. This enables easy printing of the value pointed to by the encrypted_pointer
to standard output streams such as std::cout
.
Parameters
os
: The output stream to which the value of theencrypted_pointer
will be inserted.obj
: Theencrypted_pointer
object whose pointed-to value will be printed to the stream.
Returns
A reference to the std::ostream
, allowing for chaining of stream operations.
Details:
The function dereferences the pointer managed by the
encrypted_pointer
object using*reinterpret_cast<T*>(obj.value_)
and inserts the resulting value into the stream.The
encrypted_pointer
class template is designed to store pointers in an encrypted form, but this function decrypts and dereferences the pointer to obtain the value for output.This operator is marked as a
friend
of theencrypted_pointer
class, which allows it to access the private members of the class directly, specifically thevalue_
member.
Example Usage:
int
In this example, the operator<<
allows the value pointed to by enc_ptr
to be printed to std::cout
. The stream insertion operator is called with enc_ptr
as the second argument, and the dereferenced value is inserted into the stream.