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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
b698687f
Commit
b698687f
authored
9 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
crypto: add documentation
parent
92962f78
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/opendht/crypto.h
+6
-1
6 additions, 1 deletion
include/opendht/crypto.h
src/crypto.cpp
+9
-3
9 additions, 3 deletions
src/crypto.cpp
with
15 additions
and
4 deletions
include/opendht/crypto.h
+
6
−
1
View file @
b698687f
...
...
@@ -45,7 +45,7 @@ class CryptoException : public std::runtime_error {
};
/**
* Exception thrown when a
n expected
decryption
fail
ed.
* Exception thrown when a decryption
error happen
ed.
*/
class
DecryptError
:
public
CryptoException
{
public:
...
...
@@ -108,7 +108,12 @@ struct PrivateKey
{
PrivateKey
();
//PrivateKey(gnutls_privkey_t k) : key(k) {}
/**
* Takes ownership of an existing gnutls_x509_privkey.
*/
PrivateKey
(
gnutls_x509_privkey_t
k
);
PrivateKey
(
PrivateKey
&&
o
)
noexcept
;
PrivateKey
&
operator
=
(
PrivateKey
&&
o
)
noexcept
;
...
...
This diff is collapsed.
Click to expand it.
src/crypto.cpp
+
9
−
3
View file @
b698687f
...
...
@@ -78,7 +78,7 @@ static constexpr std::array<size_t, 3> AES_LENGTHS {{128/8, 192/8, 256/8}};
size_t
aesKeySize
(
size_t
max
)
{
unsigned
aes_key_len
=
0
;
size_t
aes_key_len
=
0
;
for
(
size_t
s
=
0
;
s
<
AES_LENGTHS
.
size
();
s
++
)
{
if
(
AES_LENGTHS
[
s
]
<=
max
)
aes_key_len
=
AES_LENGTHS
[
s
];
...
...
@@ -311,7 +311,7 @@ PrivateKey::serialize(const std::string& password) const
Blob
buffer
;
buffer
.
resize
(
buf_sz
);
int
err
=
password
.
empty
()
?
gnutls_x509_privkey_export_pkcs8
(
x509_key
,
GNUTLS_X509_FMT_PEM
,
nullptr
,
GNUTLS_PKCS_PLAIN
,
buffer
.
data
(),
&
buf_sz
)
?
gnutls_x509_privkey_export_pkcs8
(
x509_key
,
GNUTLS_X509_FMT_PEM
,
nullptr
,
GNUTLS_PKCS_PLAIN
,
buffer
.
data
(),
&
buf_sz
)
:
gnutls_x509_privkey_export_pkcs8
(
x509_key
,
GNUTLS_X509_FMT_PEM
,
password
.
c_str
(),
GNUTLS_PKCS_PBES2_AES_256
,
buffer
.
data
(),
&
buf_sz
);
if
(
err
!=
GNUTLS_E_SUCCESS
)
{
std
::
cerr
<<
"Could not export private key - "
<<
gnutls_strerror
(
err
)
<<
std
::
endl
;
...
...
@@ -393,7 +393,8 @@ PublicKey::msgpack_unpack(msgpack::object o)
}
bool
PublicKey
::
checkSignature
(
const
Blob
&
data
,
const
Blob
&
signature
)
const
{
PublicKey
::
checkSignature
(
const
Blob
&
data
,
const
Blob
&
signature
)
const
{
if
(
!
pk
)
return
false
;
const
gnutls_datum_t
sig
{(
uint8_t
*
)
signature
.
data
(),
(
unsigned
)
signature
.
size
()};
...
...
@@ -431,12 +432,17 @@ PublicKey::encrypt(const Blob& data) const
const
unsigned
max_block_sz
=
key_len
/
8
-
11
;
const
unsigned
cypher_block_sz
=
key_len
/
8
;
/* Use plain RSA if the data is small enough */
if
(
data
.
size
()
<=
max_block_sz
)
{
Blob
ret
(
cypher_block_sz
);
encryptBloc
(
data
.
data
(),
data
.
size
(),
ret
.
data
(),
cypher_block_sz
);
return
ret
;
}
/* Otherwise use RSA+AES-GCM,
using the max. AES key size that can fit
in a single RSA packet () */
unsigned
aes_key_sz
=
aesKeySize
(
max_block_sz
);
if
(
aes_key_sz
==
0
)
throw
CryptoException
(
"Key is not long enough for AES128"
);
...
...
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