forked from saltstack/libnacl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request saltstack#109 from reputage/libsodium-bindings
Libsodium bindings
- Loading branch information
Showing
6 changed files
with
323 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
Utilities to make secret box easy encryption simple | ||
''' | ||
# Import libnacl | ||
import libnacl | ||
import libnacl.utils | ||
import libnacl.base | ||
|
||
|
||
class SecretBoxEasy(libnacl.base.BaseKey): | ||
''' | ||
Manage symetric encryption using the salsa20 algorithm | ||
''' | ||
def __init__(self, key=None): | ||
if key is None: | ||
key = libnacl.utils.salsa_key() | ||
if len(key) != libnacl.crypto_secretbox_KEYBYTES: | ||
raise ValueError('Invalid key') | ||
self.sk = key | ||
|
||
def encrypt(self, msg, nonce=None, pack_nonce=True): | ||
''' | ||
Encrypt the given message. If a nonce is not given it will be | ||
generated via the rand_nonce function | ||
''' | ||
if nonce is None: | ||
nonce = libnacl.utils.rand_nonce() | ||
if len(nonce) != libnacl.crypto_secretbox_NONCEBYTES: | ||
raise ValueError('Invalid nonce size') | ||
ctxt = libnacl.crypto_secretbox_easy(msg, nonce, self.sk) | ||
if pack_nonce: | ||
return nonce + ctxt | ||
else: | ||
return nonce, ctxt | ||
|
||
def decrypt(self, ctxt, nonce=None): | ||
''' | ||
Decrypt the given message, if no nonce is given the nonce will be | ||
extracted from the message | ||
''' | ||
if nonce is None: | ||
nonce = ctxt[:libnacl.crypto_secretbox_NONCEBYTES] | ||
ctxt = ctxt[libnacl.crypto_secretbox_NONCEBYTES:] | ||
if len(nonce) != libnacl.crypto_secretbox_NONCEBYTES: | ||
raise ValueError('Invalid nonce') | ||
return libnacl.crypto_secretbox_open_easy(ctxt, nonce, self.sk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Import nacl libs | ||
import libnacl | ||
import libnacl.utils | ||
|
||
# Import python libs | ||
import unittest | ||
|
||
|
||
class TestSecretBox(unittest.TestCase): | ||
''' | ||
Test sign functions | ||
''' | ||
def test_secret_box_easy(self): | ||
msg = b'Are you suggesting coconuts migrate?' | ||
sk1 = libnacl.utils.salsa_key() | ||
nonce1 = libnacl.utils.rand_nonce() | ||
enc_msg = libnacl.crypto_secretbox_easy(msg, nonce1, sk1) | ||
self.assertNotEqual(msg, enc_msg) | ||
clear_msg = libnacl.crypto_secretbox_open_easy(enc_msg, nonce1, sk1) | ||
self.assertEqual(msg, clear_msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Import libnacl libs | ||
import libnacl | ||
import libnacl.utils | ||
|
||
# Import python libs | ||
import unittest | ||
|
||
|
||
class TestSecret(unittest.TestCase): | ||
""" | ||
Test secret functions | ||
""" | ||
def test_secretbox_easy(self): | ||
msg = b'Are you suggesting coconuts migrate?' | ||
|
||
nonce = libnacl.utils.rand_nonce() | ||
key = libnacl.utils.salsa_key() | ||
|
||
c = libnacl.crypto_secretbox_easy(msg, nonce, key) | ||
m = libnacl.crypto_secretbox_open_easy(c, nonce, key) | ||
self.assertEqual(msg, m) | ||
|
||
with self.assertRaises(ValueError): | ||
libnacl.crypto_secretbox_easy(msg, b'too_short', key) | ||
|
||
with self.assertRaises(ValueError): | ||
libnacl.crypto_secretbox_easy(msg, nonce, b'too_short') | ||
|
||
with self.assertRaises(ValueError): | ||
libnacl.crypto_secretbox_open_easy(c, b'too_short', key) | ||
|
||
with self.assertRaises(ValueError): | ||
libnacl.crypto_secretbox_open_easy(c, nonce, b'too_short') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Import libnacl libs | ||
import libnacl.secret_easy | ||
# Import python libs | ||
import unittest | ||
|
||
class TestSecretEasy(unittest.TestCase): | ||
''' | ||
''' | ||
def test_secret(self): | ||
msg = b'But then of course African swallows are not migratory.' | ||
box = libnacl.secret_easy.SecretBoxEasy() | ||
ctxt = box.encrypt(msg) | ||
self.assertNotEqual(msg, ctxt) | ||
box2 = libnacl.secret_easy.SecretBoxEasy(box.sk) | ||
clear1 = box.decrypt(ctxt) | ||
self.assertEqual(msg, clear1) | ||
clear2 = box2.decrypt(ctxt) | ||
self.assertEqual(clear1, clear2) | ||
ctxt2 = box2.encrypt(msg) | ||
clear3 = box.decrypt(ctxt2) | ||
self.assertEqual(clear3, msg) | ||
|