Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
AES.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "HAL/UnrealMemory.h"
7
8// AES-256 implementation - using ECB mode for multiple blocks
9
10// The currently implemented approach has the shortcoming that it encrypts and decrypts each
11// 128-bit block separately. If the plaintext contains identical 128-byte blocks, the blocks
12// will be encrypted identically. This makes some of the plaintext structure visible in the
13// ciphertext, even to someone who does not have the key.
14
15// DO NOT USE this functionality for any new place where you might need encryption. Because
16// current code is meant for keeping backwards compatiblity with existing data. Better way
17// to use AES would be integrated with authentication, for example, in AES-GCM mode.
18
19struct FAES
20{
21 static constexpr uint32 AESBlockSize = 16;
22
23 /**
24 * Class representing a 256 bit AES key
25 */
26 struct FAESKey
27 {
28 static constexpr int32 KeySize = 32;
29
30 uint8 Key[KeySize];
31
33 {
34 Reset();
35 }
36
37 bool IsValid() const
38 {
39 uint32* Words = (uint32*)Key;
40 for (int32 Index = 0; Index < KeySize / 4; ++Index)
41 {
42 if (Words[Index] != 0)
43 {
44 return true;
45 }
46 }
47 return false;
48 }
49
50 void Reset()
51 {
53 }
54
55 bool operator == (const FAESKey& Other) const
56 {
57 return FMemory::Memcmp(Key, Other.Key, KeySize) == 0;
58 }
59 };
60
61 /**
62 * Encrypts a chunk of data using a specific key
63 *
64 * @param Contents the buffer to encrypt
65 * @param NumBytes the size of the buffer
66 * @param Key An FAESKey object containing the encryption key
67 */
68 static void EncryptData(uint8* Contents, uint64 NumBytes, const FAESKey& Key);
69
70 /**
71 * Encrypts a chunk of data using a specific key
72 *
73 * @param Contents the buffer to encrypt
74 * @param NumBytes the size of the buffer
75 * @param Key a null terminated string that is a 32 bytes long
76 */
77 static void EncryptData(uint8* Contents, uint64 NumBytes, const ANSICHAR* Key);
78
79 /**
80 * Encrypts a chunk of data using a specific key
81 *
82 * @param Contents the buffer to encrypt
83 * @param NumBytes the size of the buffer
84 * @param Key a byte array that is a 32 byte length
85 * @param NumKeyBytes length of Key byte array, must be 32
86 */
87 static void EncryptData(uint8* Contents, uint64 NumBytes, const uint8* KeyBytes, uint32 NumKeyBytes);
88
89 /**
90 * Decrypts a chunk of data using a specific key
91 *
92 * @param Contents the buffer to encrypt
93 * @param NumBytes the size of the buffer
94 * @param Key An FAESKey object containing the decryption key
95 */
96 static void DecryptData(uint8* Contents, uint64 NumBytes, const FAESKey& Key);
97
98 /**
99 * Decrypts a chunk of data using a specific key
100 *
101 * @param Contents the buffer to encrypt
102 * @param NumBytes the size of the buffer
103 * @param Key a null terminated string that is a 32 bytes long
104 */
105 static void DecryptData(uint8* Contents, uint64 NumBytes, const ANSICHAR* Key);
106
107 /**
108 * Decrypts a chunk of data using a specific key
109 *
110 * @param Contents the buffer to encrypt
111 * @param NumBytes the size of the buffer
112 * @param Key a byte array that is a 32 byte length
113 * @param NumKeyBytes length of Key byte array, must be 32
114 */
115 static void DecryptData(uint8* Contents, uint64 NumBytes, const uint8* KeyBytes, uint32 NumKeyBytes);
116};
FAESKey()
Definition AES.h:32
uint8 Key[KeySize]
Definition AES.h:30
void Reset()
Definition AES.h:50
bool IsValid() const
Definition AES.h:37
static constexpr int32 KeySize
Definition AES.h:28
bool operator==(const FAESKey &Other) const
Definition AES.h:55
Definition AES.h:20
static void EncryptData(uint8 *Contents, uint64 NumBytes, const uint8 *KeyBytes, uint32 NumKeyBytes)
static void EncryptData(uint8 *Contents, uint64 NumBytes, const FAESKey &Key)
static void DecryptData(uint8 *Contents, uint64 NumBytes, const uint8 *KeyBytes, uint32 NumKeyBytes)
static void DecryptData(uint8 *Contents, uint64 NumBytes, const ANSICHAR *Key)
static constexpr uint32 AESBlockSize
Definition AES.h:21
static void EncryptData(uint8 *Contents, uint64 NumBytes, const ANSICHAR *Key)
static void DecryptData(uint8 *Contents, uint64 NumBytes, const FAESKey &Key)
static FORCEINLINE void * Memset(void *Dest, uint8 Char, SIZE_T Count)
static FORCEINLINE int32 Memcmp(const void *Buf1, const void *Buf2, SIZE_T Count)