diff options
Diffstat (limited to 'nacl/crypto_box/wrapper-open.cpp')
-rw-r--r-- | nacl/crypto_box/wrapper-open.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/nacl/crypto_box/wrapper-open.cpp b/nacl/crypto_box/wrapper-open.cpp new file mode 100644 index 00000000..67663a21 --- /dev/null +++ b/nacl/crypto_box/wrapper-open.cpp | |||
@@ -0,0 +1,27 @@ | |||
1 | #include <string> | ||
2 | using std::string; | ||
3 | #include "crypto_box.h" | ||
4 | |||
5 | string crypto_box_open(const string &c,const string &n,const string &pk,const string &sk) | ||
6 | { | ||
7 | if (pk.size() != crypto_box_PUBLICKEYBYTES) throw "incorrect public-key length"; | ||
8 | if (sk.size() != crypto_box_SECRETKEYBYTES) throw "incorrect secret-key length"; | ||
9 | if (n.size() != crypto_box_NONCEBYTES) throw "incorrect nonce length"; | ||
10 | size_t clen = c.size() + crypto_box_BOXZEROBYTES; | ||
11 | unsigned char cpad[clen]; | ||
12 | for (int i = 0;i < crypto_box_BOXZEROBYTES;++i) cpad[i] = 0; | ||
13 | for (int i = crypto_box_BOXZEROBYTES;i < clen;++i) cpad[i] = c[i - crypto_box_BOXZEROBYTES]; | ||
14 | unsigned char mpad[clen]; | ||
15 | if (crypto_box_open(mpad,cpad,clen, | ||
16 | (const unsigned char *) n.c_str(), | ||
17 | (const unsigned char *) pk.c_str(), | ||
18 | (const unsigned char *) sk.c_str() | ||
19 | ) != 0) | ||
20 | throw "ciphertext fails verification"; | ||
21 | if (clen < crypto_box_ZEROBYTES) | ||
22 | throw "ciphertext too short"; // should have been caught by _open | ||
23 | return string( | ||
24 | (char *) mpad + crypto_box_ZEROBYTES, | ||
25 | clen - crypto_box_ZEROBYTES | ||
26 | ); | ||
27 | } | ||