37#include <scolPlugin.h>
41using CryptoPP::InvertibleRSAFunction;
42using CryptoPP::RSAES_OAEP_SHA_Encryptor;
43using CryptoPP::RSAES_OAEP_SHA_Decryptor;
45using CryptoPP::RSAES_PKCS1v15_Encryptor;
46using CryptoPP::RSAES_PKCS1v15_Decryptor;
48using CryptoPP::InvertibleRSAFunction;
49typedef CryptoPP::RSAFunction RSAPublicKey;
50typedef CryptoPP::InvertibleRSAFunction RSAPrivateKey;
56using CryptoPP::StringSink;
57using CryptoPP::StringSource;
58using CryptoPP::PK_EncryptorFilter;
59using CryptoPP::PK_DecryptorFilter;
60using CryptoPP::StreamTransformationFilter;
62#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
63#include "../cryptopp/md5.h"
69using CryptoPP::CBC_Mode;
72using CryptoPP::FileSink;
73using CryptoPP::FileSource;
76using CryptoPP::AutoSeededRandomPool;
79using CryptoPP::SecByteBlock;
82using CryptoPP::Exception;
83using CryptoPP::DecodingResult;
86using CryptoPP::HexEncoder;
87using CryptoPP::HexDecoder;
128 MMechostr(0,
"_RSAgetKeyPair\n");
131 int keysize = MMget(m, 0);
140 AutoSeededRandomPool rng;
154 MMechostr(0,
"_RSAgetKeyPair Privilege Error\n");
162 HexEncoder hxPublicKey(
new StringSink(sPublicKey));
163 HexEncoder hxPrivateKey(
new StringSink(sPrivateKey));
165 publicKey.
Save(hxPublicKey);
166 privateKey.
Save(hxPrivateKey);
170 Mpushstrbloc(m, (
char *)sPrivateKey.c_str());
171 Mpushstrbloc(m, (
char *)sPublicKey.c_str());
175 if(
int k=MBdeftab(m))
198 MMechostr(0,
"_RSAcryptMessage\n");
201 int key = MTOP(MMpull(m));
202 int mess = MTOP(MMpull(m));
203 if (mess==NIL || key==NIL)
209 int msize = MMsizestr(m, mess);
210 byte* smess = (
byte*)MMstart(m, (mess)+1);
211 char* sckey = MMstartstr(m, key);
213 if (smess == NULL || sckey == NULL)
219 string sPublicKey = string(sckey);
220 string sEncrypted, sEncryptedHex;
224 AutoSeededRandomPool rng;
230 decoder.
Put((
byte*)sPublicKey.c_str(), sPublicKey.size());
232 publicKey.
Load(decoder);
235 RSAES_OAEP_SHA_Encryptor encrypt( publicKey );
240 encoder.
Attach(
new StringSink(sEncryptedHex));
241 encoder.
Put((
byte *)sEncrypted.c_str(), sEncrypted.size());
244 catch (CryptoPP::Exception& encrypt)
246 MMechostr(0,
"_RSAencryptMessage Error : %s \n", encrypt.what());
251 int res = MMmalloc(m, STR_SIZE(sEncryptedHex.size()), TYPEBUF);
258 MMstore(m, res, 0, sEncryptedHex.size());
259 char* BS = MMstartstr(m, res);
260 memcpy(BS, sEncryptedHex.c_str(), sEncryptedHex.size());
261 BS[sEncryptedHex.size()] = 0;
262 int k = MMpush(m, PTOM(res));
284 MMechostr(0,
"_RSAdecryptMessage\n");
287 int key = MTOP(MMpull(m));
288 int mess = MTOP(MMpull(m));
289 if (mess==NIL || key==NIL)
295 int msize = MMsizestr(m, mess);
296 byte* smess = (
byte*)MMstart(m, (mess)+1);
297 char* sckey = MMstartstr(m,key);
299 if (smess == NULL || sckey == NULL)
305 string sPrivateKey = string(sckey);
306 string sDecrypted, sMessage;
310 AutoSeededRandomPool rng;
315 mdecoder.
Attach(
new StringSink(sMessage));
316 mdecoder.
Put(smess, msize);
321 kdecoder.
Put((
byte*)sPrivateKey.c_str(), sPrivateKey.size());
323 privateKey.
Load(kdecoder);
326 RSAES_OAEP_SHA_Decryptor decrypt(privateKey);
333 catch (CryptoPP::Exception& decrypt)
335 MMechostr(0,
"_RSAdecryptMessage Error : %s \n", decrypt.what());
336 return MMpush(m, NIL);
339 int res = MMmalloc(m, STR_SIZE(sDecrypted.size()), TYPEBUF);
346 MMstore(m, res, 0, sDecrypted.size());
347 char* BS = MMstartstr(m, res);
348 memcpy(BS, sDecrypted.c_str(), sDecrypted.size());
349 BS[sDecrypted.size()] = 0;
350 int k = MMpush(m, PTOM(res));
371 MMechostr(0,
"_AESgetKey\n");
374 AutoSeededRandomPool prng;
376 byte key[AES::DEFAULT_KEYLENGTH];
377 prng.GenerateBlock(key,
sizeof(key));
384 int k = Mpushstrbloc(m, (
char *)sKey.c_str());
407 MMechostr(0,
"_AESencryptMessage\n");
410 int key = MTOP(MMpull(m));
411 int mess = MTOP(MMpull(m));
412 if (mess==NIL || key==NIL)
418 int msize = MMsizestr(m, mess);
419 byte* smess = (
byte*)MMstart(m, (mess)+1);
420 char* sckey = MMstartstr(m, key);
422 if (smess == NULL || sckey == NULL)
430 string sKeyHex = string(sckey);
431 string sEncrypted, sEncryptedHex, sIv, sKey;
436 AutoSeededRandomPool prng;
438 byte iv[AES::BLOCKSIZE];
439 prng.GenerateBlock(iv,
sizeof(iv));
444 kdecoder.
Attach(
new StringSink(sKey));
445 kdecoder.
Put((
byte *)sKeyHex.c_str(), sKeyHex.size());
448 catch( CryptoPP::Exception& e)
450 MMechostr(0,
"_AESencryptMessage Error : %s \n", e.what());
451 return MMpush(m,NIL) ;
457 encrypt.SetKeyWithIV((
byte *)sKey.c_str(), sKey.size(), iv);
464 catch( CryptoPP::Exception& encrypt)
466 MMechostr(0,
"_AESencryptMessage Error : %s \n", encrypt.what());
467 return MMpush(m, NIL);
473 encoder.
Attach(
new StringSink(sEncryptedHex));
474 encoder.
Put((
byte *)sEncrypted.c_str(), sEncrypted.size());
477 catch( CryptoPP::Exception& e)
479 MMechostr(0,
"_AESencryptMessage Error : %s \n", e.what());
480 return MMpush(m, NIL);
488 catch( CryptoPP::Exception& e)
490 MMechostr(0,
"_AESencryptMessage Error : %s \n", e.what());
491 return MMpush(m, NIL);
495 sIv.append(sEncryptedHex);
497 int res = MMmalloc(m, STR_SIZE(sIv.size()), TYPEBUF);
504 MMstore(m, res, 0, sIv.size());
505 char* BS = MMstartstr(m, res);
506 memcpy(BS, sIv.c_str(), sIv.size());
508 int k = MMpush(m, PTOM(res));
531 MMechostr(0,
"_AESencryptMessage\n");
534 int key = MTOP(MMpull(m));
535 int mess = MTOP(MMpull(m));
536 if (mess==NIL || key==NIL)
542 int msize = MMsizestr(m, mess);
543 byte* smess = (
byte*)MMstart(m, (mess)+1);
544 char* sckey = MMstartstr(m,key);
546 if (smess == NULL || sckey == NULL)
552 string sKeyHex = string(sckey);
553 string sMessage, sEncrypted, sEncryptedHex, sKey;
558 mdecoder.
Attach(
new StringSink( sEncrypted ) );
559 mdecoder.
Put(smess, msize);
562 catch( CryptoPP::Exception& e)
564 MMechostr(0,
"_AESdecryptMessage Error : %s \n", e.what());
565 return MMpush(m, NIL);
571 kdecoder.
Attach(
new StringSink( sKey ) );
572 kdecoder.
Put( (
byte *)sKeyHex.c_str(), sKeyHex.size() );
575 catch( CryptoPP::Exception& e)
577 MMechostr(0,
"_AESdecryptMessage Error : %s \n", e.what());
578 return MMpush(m, NIL);
584 decrypt.SetKeyWithIV((
byte *)sKey.c_str(), sKey.size(), (
byte *)sEncrypted.substr(0, 16).c_str());
590 catch( CryptoPP::Exception& decrypt)
592 MMechostr(0,
"_AESdecryptMessage Error : %s \n", decrypt.what());
593 return MMpush(m, NIL);
596 int res = MMmalloc(m, STR_SIZE(sMessage.size()), TYPEBUF);
603 MMstore(m, res, 0, sMessage.size());
604 char* BS = MMstartstr(m, res);
605 memcpy(BS, sMessage.c_str(), sMessage.size());
606 BS[sMessage.size()] = 0;
607 int k = MMpush(m, PTOM(res));
628 MMechostr(0,
"_MD5value\n");
631 int mess = MTOP(MMpull(m));
638 int msize = MMsizestr(m, mess);
639 byte* smess = (
byte*)MMstart(m, (mess)+1);
650 byte digest[ CryptoPP::Weak::MD5::DIGESTSIZE ];
652 CryptoPP::Weak::MD5 hash;
653 hash.CalculateDigest( digest, (
const byte*)smess, msize );
655 CryptoPP::HexEncoder encoder;
656 encoder.Attach(
new CryptoPP::StringSink( sMD5 ) );
657 encoder.Put( digest,
sizeof(digest) );
658 encoder.MessageEnd();
660 catch (CryptoPP::Exception& encrypt)
662 MMechostr(0,
"_MD5value Error : %s \n", encrypt.what());
667 int res = MMmalloc(m, STR_SIZE(sMD5.size()), TYPEBUF);
674 MMstore(m, res, 0, sMD5.size());
675 char* BS = MMstartstr(m, res);
676 memcpy(BS, sMD5.c_str(), sMD5.size());
678 int k = MMpush(m, PTOM(res));
698 MMechostr(0,
"_SHA256value\n");
701 int mess = MTOP(MMpull(m));
708 int msize = MMsizestr(m, mess);
709 byte* smess = (
byte*)MMstart(m, (mess)+1);
720 byte digest[CryptoPP::SHA256::DIGESTSIZE];
722 CryptoPP::SHA256 hash;
723 hash.CalculateDigest(digest, (
const byte*)smess, msize);
725 CryptoPP::HexEncoder encoder;
726 encoder.Attach(
new CryptoPP::StringSink(sScrypt));
727 encoder.Put(digest,
sizeof(digest));
728 encoder.MessageEnd();
730 catch (CryptoPP::Exception& encrypt)
732 MMechostr(0,
"_SHA256value Error : %s \n", encrypt.what());
737 int res = MMmalloc(m, STR_SIZE(sScrypt.size()), TYPEBUF);
744 MMstore(m, res, 0, sScrypt.size());
745 char* BS = MMstartstr(m, res);
746 memcpy(BS, sScrypt.c_str(), sScrypt.size());
747 BS[sScrypt.size()] = 0;
748 int k = MMpush(m, PTOM(res));
751 MMechostr(0,
"ok\n");
768 MMechostr(0,
"_SHA512value\n");
771 int mess = MTOP(MMpull(m));
778 int msize = MMsizestr(m, mess);
779 byte* smess = (
byte*)MMstart(m, (mess)+1);
790 byte digest[CryptoPP::SHA512::DIGESTSIZE];
792 CryptoPP::SHA512 hash;
793 hash.CalculateDigest(digest, (
const byte*)smess, msize);
795 CryptoPP::HexEncoder encoder;
796 encoder.Attach(
new CryptoPP::StringSink(sScrypt));
797 encoder.Put(digest,
sizeof(digest));
798 encoder.MessageEnd();
800 catch (CryptoPP::Exception& encrypt)
802 MMechostr(0,
"_SHA512value Error : %s \n", encrypt.what());
807 int res = MMmalloc(m, STR_SIZE(sScrypt.size()), TYPEBUF);
814 MMstore(m, res, 0, sScrypt.size());
815 char* BS = MMstartstr(m, res);
816 memcpy(BS, sScrypt.c_str(), sScrypt.size());
817 BS[sScrypt.size()] = 0;
818 int k = MMpush(m, PTOM(res));
821 MMechostr(0,
"ok\n");
827static NativeDefinition sSecurityDef[] =
835 {
"_MD5value", 1,
"fun [S] S",
_MD5value },
847int LoadSecurity(mmachine m)
849 int k = PKhardpak2(m,
"SecurityEngine.pkg-1.0",
sizeof(sSecurityDef) /
sizeof(sSecurityDef[0]), sSecurityDef);
850 MMechostr(MSKDEBUG,
"\n" );
859extern "C" SCOL_EXPORT
int ScolLoadPlugin(mmachine m, cbmachine w)
870extern "C" SCOL_EXPORT
int ScolUnloadPlugin()
878extern "C" SCOL_EXPORT
int ScolSecurityLoadPlugin(mmachine m, cbmachine w)
889extern "C" SCOL_EXPORT
int ScolSecurityUnloadPlugin()
Class file for the AES cipher (Rijndael)
CCM block cipher mode of operation.
void Load(BufferedTransformation &bt)
BER decode ASN.1 object.
void Save(BufferedTransformation &bt) const
DER encode ASN.1 object.
Block cipher mode of operation aggregate.
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
Decode base 16 data back to bytes.
Converts given data to base 16.
RSA trapdoor function using the private key.
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
void Save(BufferedTransformation &bt) const
DER encode ASN.1 object.
void Load(BufferedTransformation &bt)
BER decode ASN.1 object.
Filter wrapper for PK_Decryptor.
Filter wrapper for PK_Encryptor.
RSA trapdoor function using the public key.
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
String-based implementation of the Source interface.
Abstract base classes that provide a uniform interface to this library.
Classes providing file-based library services.
Implementation of BufferedTransformation's attachment interface.
int _RSAdecryptMessage(mmachine m)
_RSAdecryptMessage : This function decrypt a message using the RSA private key
int _AESgetKey(mmachine m)
_AESgetKey : This function generate an AES key
int _AESdecryptMessage(mmachine m)
_AESdecryptMessage : This function decrypt a message using the AES key
int _MD5value(mmachine m)
_MD5value : This function get the MD5 value from a string
int _SHA512value(mmachine m)
_SHA512value : This function get the SHA512 value from a string
int _RSAencryptMessage(mmachine m)
_RSAencryptMessage : This function encrypt a message using the RSA public key
int _RSAgetKeyPair(mmachine m)
_RSAgetKeyPair : This function generate the RSA private and public key with the size passed in parame...
int _SHA256value(mmachine m)
_SHA256value : This function get the SHA256 value from a string
int _AESencryptMessage(mmachine m)
_AESencryptMessage : This function encrypt a message using the AES key
Classes for HexEncoder and HexDecoder.
Classes for access to the operating system's random number generators.
Classes for the RSA cryptosystem.
Classes and functions for secure memory allocations.
Classes for SHA-1 and SHA-2 family of message digests.