summaryrefslogtreecommitdiff
path: root/nacl/crypto_secretbox/wrapper-open.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/crypto_secretbox/wrapper-open.cpp')
-rw-r--r--nacl/crypto_secretbox/wrapper-open.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/nacl/crypto_secretbox/wrapper-open.cpp b/nacl/crypto_secretbox/wrapper-open.cpp
new file mode 100644
index 00000000..07989813
--- /dev/null
+++ b/nacl/crypto_secretbox/wrapper-open.cpp
@@ -0,0 +1,22 @@
1#include <string>
2using std::string;
3#include "crypto_secretbox.h"
4
5string crypto_secretbox_open(const string &c,const string &n,const string &k)
6{
7 if (k.size() != crypto_secretbox_KEYBYTES) throw "incorrect key length";
8 if (n.size() != crypto_secretbox_NONCEBYTES) throw "incorrect nonce length";
9 size_t clen = c.size() + crypto_secretbox_BOXZEROBYTES;
10 unsigned char cpad[clen];
11 for (int i = 0;i < crypto_secretbox_BOXZEROBYTES;++i) cpad[i] = 0;
12 for (int i = crypto_secretbox_BOXZEROBYTES;i < clen;++i) cpad[i] = c[i - crypto_secretbox_BOXZEROBYTES];
13 unsigned char mpad[clen];
14 if (crypto_secretbox_open(mpad,cpad,clen,(const unsigned char *) n.c_str(),(const unsigned char *) k.c_str()) != 0)
15 throw "ciphertext fails verification";
16 if (clen < crypto_secretbox_ZEROBYTES)
17 throw "ciphertext too short"; // should have been caught by _open
18 return string(
19 (char *) mpad + crypto_secretbox_ZEROBYTES,
20 clen - crypto_secretbox_ZEROBYTES
21 );
22}