Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
f84131c8
Commit
f84131c8
authored
Oct 22, 2019
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
crypto: improve authenticated encryption performance
parent
28de9831
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/crypto.cpp
+23
-13
23 additions, 13 deletions
src/crypto.cpp
tests/cryptotester.cpp
+18
-6
18 additions, 6 deletions
tests/cryptotester.cpp
with
41 additions
and
19 deletions
src/crypto.cpp
+
23
−
13
View file @
f84131c8
...
...
@@ -52,6 +52,9 @@ static std::uniform_int_distribution<uint8_t> rand_byte;
#define GNUTLS_PKCS_PBES2_AES_256 GNUTLS_PKCS_USE_PBES2_AES_256
#endif
#define DHT_AES_LEGACY_ENCRYPT 1
#define DHT_AES_LEGACY_DECRYPT 1
namespace
dht
{
namespace
crypto
{
...
...
@@ -105,7 +108,9 @@ Blob aesEncrypt(const uint8_t* data, size_t data_length, const Blob& key)
struct
gcm_aes_ctx
aes
;
gcm_aes_set_key
(
&
aes
,
key
.
size
(),
key
.
data
());
gcm_aes_set_iv
(
&
aes
,
GCM_IV_SIZE
,
ret
.
data
());
#if DHT_AES_LEGACY_ENCRYPT
gcm_aes_update
(
&
aes
,
data_length
,
data
);
#endif
gcm_aes_encrypt
(
&
aes
,
data_length
,
ret
.
data
()
+
GCM_IV_SIZE
,
data
);
gcm_aes_digest
(
&
aes
,
GCM_DIGEST_SIZE
,
ret
.
data
()
+
GCM_IV_SIZE
+
data_length
);
...
...
@@ -137,11 +142,12 @@ Blob aesDecrypt(const Blob& data, const Blob& key)
size_t
data_sz
=
data
.
size
()
-
GCM_IV_SIZE
-
GCM_DIGEST_SIZE
;
Blob
ret
(
data_sz
);
//gcm_aes_update(&aes, data_sz, data.data() + GCM_IV_SIZE);
gcm_aes_decrypt
(
&
aes
,
data_sz
,
ret
.
data
(),
data
.
data
()
+
GCM_IV_SIZE
);
//
gcm_aes_digest(aes, GCM_DIGEST_SIZE, digest.data());
gcm_aes_digest
(
&
aes
,
GCM_DIGEST_SIZE
,
digest
.
data
());
// TODO compute the proper digest directly from the decryption pass
if
(
not
std
::
equal
(
digest
.
begin
(),
digest
.
end
(),
data
.
end
()
-
GCM_DIGEST_SIZE
))
{
#if DHT_AES_LEGACY_DECRYPT
//gcm_aes_decrypt(&aes, data_sz, ret.data(), data.data() + GCM_IV_SIZE);
Blob
ret_tmp
(
data_sz
);
struct
gcm_aes_ctx
aes_d
;
gcm_aes_set_key
(
&
aes_d
,
key
.
size
(),
key
.
data
());
...
...
@@ -152,6 +158,10 @@ Blob aesDecrypt(const Blob& data, const Blob& key)
if
(
not
std
::
equal
(
digest
.
begin
(),
digest
.
end
(),
data
.
end
()
-
GCM_DIGEST_SIZE
))
throw
DecryptError
(
"Can't decrypt data"
);
#else
throw
DecryptError
(
"Can't decrypt data"
);
#endif
}
return
ret
;
}
...
...
This diff is collapsed.
Click to expand it.
tests/cryptotester.cpp
+
18
−
6
View file @
f84131c8
...
...
@@ -34,16 +34,28 @@ CryptoTester::testSignatureEncryption() {
auto
key
=
dht
::
crypto
::
PrivateKey
::
generate
();
auto
public_key
=
key
.
getPublicKey
();
std
::
vector
<
uint8_t
>
data
{
5
,
10
};
std
::
vector
<
uint8_t
>
signature
=
key
.
sign
(
data
);
std
::
vector
<
uint8_t
>
data1
{
5
,
10
};
std
::
vector
<
uint8_t
>
data2
(
64
*
1024
,
10
);
std
::
vector
<
uint8_t
>
signature1
=
key
.
sign
(
data1
);
std
::
vector
<
uint8_t
>
signature2
=
key
.
sign
(
data2
);
// check signature
CPPUNIT_ASSERT
(
public_key
.
checkSignature
(
data
,
signature
));
CPPUNIT_ASSERT
(
public_key
.
checkSignature
(
data1
,
signature1
));
CPPUNIT_ASSERT
(
public_key
.
checkSignature
(
data2
,
signature2
));
// encrypt data
std
::
vector
<
uint8_t
>
encrypted
=
public_key
.
encrypt
(
data
);
{
std
::
vector
<
uint8_t
>
encrypted
=
public_key
.
encrypt
(
data1
);
std
::
vector
<
uint8_t
>
decrypted
=
key
.
decrypt
(
encrypted
);
CPPUNIT_ASSERT
(
data
==
decrypted
);
CPPUNIT_ASSERT
(
data1
==
decrypted
);
}
{
std
::
vector
<
uint8_t
>
encrypted
=
public_key
.
encrypt
(
data2
);
std
::
vector
<
uint8_t
>
decrypted
=
key
.
decrypt
(
encrypted
);
CPPUNIT_ASSERT
(
data2
==
decrypted
);
}
}
void
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment