Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libnacl / crypto_box / wrapper-open.cpp
blob67663a21c83c07d40bd1d0f28cdbce16b6a6141f
1 #include <string>
2 using std::string;
3 #include "crypto_box.h"
5 string crypto_box_open(const string &c,const string &n,const string &pk,const string &sk)
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