security: Generate random block in Tpm.

Change-Id: Ia65d32802ed0ebc76605ae7975ea02f4c2db737d
diff --git a/src/security/sec-tpm-file.cpp b/src/security/sec-tpm-file.cpp
index 52f14ba..47cf83b 100644
--- a/src/security/sec-tpm-file.cpp
+++ b/src/security/sec-tpm-file.cpp
@@ -35,7 +35,7 @@
 
 class SecTpmFile::Impl {
 public:
-  Impl(const string &dir)
+  Impl(const string& dir)
   {
     if(dir.empty())
       m_keystorePath = boost::filesystem::path(getenv("HOME")) / ".ndnx" / "ndnsec-tpm-file";
@@ -49,8 +49,9 @@
   boost::filesystem::path m_keystorePath;
 };
 
+
 SecTpmFile::SecTpmFile(const string & dir)
-  : impl_(new Impl(dir))
+  : m_impl(new Impl(dir))
 {}
 
 void
@@ -70,7 +71,9 @@
     switch(keyType){
     case KEY_TYPE_RSA:
       {
-	AutoSeededRandomPool rng;
+        using namespace CryptoPP;
+        AutoSeededRandomPool rng;
+
 	InvertibleRSAFunction privateKey;
 	privateKey.Initialize(rng, keySize);
 	
@@ -139,8 +142,9 @@
     throw Error("private key doesn't exists");
  
   try{
+    using namespace CryptoPP;
     AutoSeededRandomPool rng;
-      
+
     //Read private key
     ByteQueue bytes;
     string privateKeyFileName = nameTransform(keyURI, ".pri");
@@ -180,8 +184,9 @@
 	throw Error("private key doesn't exist");
 
       try{
-	AutoSeededRandomPool rng;
-	
+	using namespace CryptoPP;
+        AutoSeededRandomPool rng;
+
 	//Read private key
 	ByteQueue bytes;
 	string privateKeyFileName = nameTransform(keyURI, ".pri");
@@ -241,7 +246,8 @@
 	throw Error("public key doesn't exist");
       try
 	{
-	  AutoSeededRandomPool rng;
+          using namespace CryptoPP;
+          AutoSeededRandomPool rng;
 
 	  //Read private key
 	  ByteQueue bytes;
@@ -307,9 +313,11 @@
     switch(keyType){
     case KEY_TYPE_AES:
       {
-	AutoSeededRandomPool rnd;
+        using namespace CryptoPP;
+        AutoSeededRandomPool rng;
+
 	SecByteBlock key(0x00, keySize);
-	rnd.GenerateBlock(key, keySize );
+	rng.GenerateBlock(key, keySize);
 	
 	StringSource(key, key.size(), true, new HexEncoder(new FileSink(symKeyFileName.c_str())));
 	
@@ -376,18 +384,30 @@
         }
     }
 
-  return (impl_->m_keystorePath / (digest + extension)).string();
+  return (m_impl->m_keystorePath / (digest + extension)).string();
 }
 
 void 
 SecTpmFile::maintainMapping(string str1, string str2)
 {
   std::ofstream outfile;
-  string dirFile = (impl_->m_keystorePath / "mapping.txt").string();
+  string dirFile = (m_impl->m_keystorePath / "mapping.txt").string();
 
   outfile.open(dirFile.c_str(), std::ios_base::app);
   outfile << str1 << ' ' << str2 << '\n';
   outfile.close();
 }
 
+bool
+SecTpmFile::generateRandomBlock(uint8_t* res, size_t size)
+{
+  try{
+    CryptoPP::AutoSeededRandomPool rng;
+    rng.GenerateBlock(res, size);
+    return true;
+  }catch(const CryptoPP::Exception& e){
+    return false;
+  }
+}
+
 } //ndn