forked from bailey27/cppcryptfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaes.cpp
More file actions
136 lines (114 loc) · 3.5 KB
/
Copy pathaes.cpp
File metadata and controls
136 lines (114 loc) · 3.5 KB
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
/*
cppcryptfs : user-mode cryptographic virtual overlay filesystem.
Copyright (C) 2016-2026 Bailey Brown (github.com/bailey27/cppcryptfs)
cppcryptfs is based on the design of gocryptfs (github.com/rfjakob/gocryptfs)
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "stdafx.h"
#include "AES.h"
#ifdef USE_AES_NI
#ifdef __cplusplus
extern "C" {
#endif
unsigned int OPENSSL_ia32cap_P[];
#define HAVE_AES_NI (OPENSSL_ia32cap_P[1]&(1<<(57-32)))
int aesni_set_encrypt_key(const unsigned char *userKey, int bits,
AES_KEY *key);
int aesni_set_decrypt_key(const unsigned char *userKey, int bits,
AES_KEY *key);
void aesni_encrypt(const unsigned char *in, unsigned char *out,
const AES_KEY *key);
void aesni_decrypt(const unsigned char *in, unsigned char *out,
const AES_KEY *key);
#ifdef __cplusplus
};
#endif
#endif // USE_AES_NI
bool AES::use_aes_ni()
{
#ifdef USE_AES_NI
if (HAVE_AES_NI)
return true;
#endif
return false;
}
void AES::initialize_keys(const unsigned char *key, int keylen /* in bits */,
AES_KEY *encrypt_key, AES_KEY *decrypt_key)
{
#ifdef USE_AES_NI
if (AES::use_aes_ni()) {
aesni_set_encrypt_key(key, keylen, encrypt_key);
aesni_set_decrypt_key(key, keylen, decrypt_key);
} else
#endif
{
// low-level AES functions are deprecated in Openssl 3.0
#pragma warning( push )
#pragma warning(disable : 4996)
AES_set_encrypt_key(key, keylen, encrypt_key);
AES_set_decrypt_key(key, keylen, decrypt_key);
#pragma warning( pop )
}
}
AES::AES()
{
m_key_encrypt = NULL;
m_key_decrypt = NULL;
m_use_aes_ni = use_aes_ni();
}
AES::~AES()
{
// don't delete keys
}
void AES::set_keys(const AES_KEY *key_encrypt, const AES_KEY *key_decrypt)
{
m_key_encrypt = key_encrypt;
m_key_decrypt = key_decrypt;
}
// encrypt single AES block (16 bytes)
void AES::encrypt(const unsigned char* plain, unsigned char *cipher) const
{
#ifdef USE_AES_NI
if (m_use_aes_ni) {
aesni_encrypt(plain, cipher, m_key_encrypt);
} else
#endif
{
// low-level AES functions are deprecated in Openssl 3.0
#pragma warning( push )
#pragma warning(disable : 4996)
AES_encrypt(plain, cipher, m_key_encrypt);
#pragma warning( pop )
}
}
// decrypt single AES block (16 bytes)
void AES::decrypt(const unsigned char *cipher, unsigned char *plain) const
{
#ifdef USE_AES_NI
if (m_use_aes_ni) {
aesni_decrypt(cipher, plain, m_key_decrypt);
} else
#endif
{
// low-level AES functions are deprecated in Openssl 3.0
#pragma warning( push )
#pragma warning(disable : 4996)
AES_decrypt(cipher, plain, m_key_decrypt);
#pragma warning( pop )
}
}