update crypto helper

Change-Id: I59718964ce305888a8fc3947cde68c937a3ba64a
diff --git a/src/detail/ca-sqlite.cpp b/src/detail/ca-sqlite.cpp
index b0d6af5..468fd2c 100644
--- a/src/detail/ca-sqlite.cpp
+++ b/src/detail/ca-sqlite.cpp
@@ -66,7 +66,8 @@
     remaining_tries INTEGER,
     remaining_time INTEGER,
     challenge_secrets TEXT,
-    encryption_key BLOB NOT NULL
+    encryption_key BLOB NOT NULL,
+    aes_block_counter INTEGER
   );
 CREATE UNIQUE INDEX IF NOT EXISTS
   CaStateIdIndex ON CaStates(request_id);
@@ -128,7 +129,8 @@
                              R"_SQLTEXT_(SELECT id, ca_name, status,
                              challenge_status, cert_request,
                              challenge_type, challenge_secrets,
-                             challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
+                             challenge_tp, remaining_tries, remaining_time,
+                             request_type, encryption_key, aes_block_counter
                              FROM CaStates where request_id = ?)_SQLTEXT_");
   statement.bind(1, requestId, SQLITE_TRANSIENT);
 
@@ -144,11 +146,12 @@
     auto remainingTime = statement.getInt(9);
     auto requestType = static_cast<RequestType>(statement.getInt(10));
     auto encryptionKey = statement.getBlock(11);
+    auto aesCounter = statement.getInt(12);
     if (challengeType != "") {
       return CaState(caName, requestId, requestType, status, cert,
                      challengeType, challengeStatus, time::fromIsoString(challengeTp),
                      remainingTries, time::seconds(remainingTime),
-                     convertString2Json(challengeSecrets), encryptionKey);
+                     convertString2Json(challengeSecrets), encryptionKey, aesCounter);
     }
     else {
       return CaState(caName, requestId, requestType, status, cert, encryptionKey);
@@ -166,14 +169,15 @@
       m_database,
       R"_SQLTEXT_(INSERT OR ABORT INTO CaStates (request_id, ca_name, status, request_type,
                   cert_request, challenge_type, challenge_status, challenge_secrets,
-                  challenge_tp, remaining_tries, remaining_time, encryption_key)
-                  values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
+                  challenge_tp, remaining_tries, remaining_time, encryption_key, aes_block_counter)
+                  values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?))_SQLTEXT_");
   statement.bind(1, request.m_requestId, SQLITE_TRANSIENT);
   statement.bind(2, request.m_caPrefix.wireEncode(), SQLITE_TRANSIENT);
   statement.bind(3, static_cast<int>(request.m_status));
   statement.bind(4, static_cast<int>(request.m_requestType));
   statement.bind(5, request.m_cert.wireEncode(), SQLITE_TRANSIENT);
   statement.bind(12, request.m_encryptionKey, SQLITE_TRANSIENT);
+  statement.bind(13, request.m_aesBlockCounter);
   if (request.m_challengeState) {
     statement.bind(6, request.m_challengeType, SQLITE_TRANSIENT);
     statement.bind(7, request.m_challengeState->m_challengeStatus, SQLITE_TRANSIENT);
@@ -194,7 +198,7 @@
   Sqlite3Statement statement(m_database,
                              R"_SQLTEXT_(UPDATE CaStates
                              SET status = ?, challenge_type = ?, challenge_status = ?, challenge_secrets = ?,
-                             challenge_tp = ?, remaining_tries = ?, remaining_time = ?
+                             challenge_tp = ?, remaining_tries = ?, remaining_time = ?, aes_block_counter = ?
                              WHERE request_id = ?)_SQLTEXT_");
   statement.bind(1, static_cast<int>(request.m_status));
   statement.bind(2, request.m_challengeType, SQLITE_TRANSIENT);
@@ -212,7 +216,8 @@
     statement.bind(6, 0);
     statement.bind(7, 0);
   }
-  statement.bind(8, request.m_requestId, SQLITE_TRANSIENT);
+  statement.bind(8, request.m_aesBlockCounter);
+  statement.bind(9, request.m_requestId, SQLITE_TRANSIENT);
 
   if (statement.step() != SQLITE_DONE) {
     addRequest(request);
@@ -225,7 +230,8 @@
   std::list<CaState> result;
   Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
                              challenge_status, cert_request, challenge_type, challenge_secrets,
-                             challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
+                             challenge_tp, remaining_tries, remaining_time, request_type,
+                             encryption_key, aes_block_counter
                              FROM CaStates)_SQLTEXT_");
   while (statement.step() == SQLITE_ROW) {
     auto requestId = statement.getString(1);
@@ -240,14 +246,15 @@
     auto remainingTime = statement.getInt(10);
     auto requestType = static_cast<RequestType>(statement.getInt(11));
     auto encryptionKey = statement.getBlock(12);
+    auto aesBlockCounter = statement.getInt(13);
     if (challengeType != "") {
       result.push_back(CaState(caName, requestId, requestType, status, cert,
                                challengeType, challengeStatus, time::fromIsoString(challengeTp),
                                remainingTries, time::seconds(remainingTime),
-                               convertString2Json(challengeSecrets), encryptionKey));
+                               convertString2Json(challengeSecrets), encryptionKey, aesBlockCounter));
     }
     else {
-      result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
+      result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey, aesBlockCounter));
     }
   }
   return result;
@@ -260,7 +267,8 @@
   Sqlite3Statement statement(m_database,
                              R"_SQLTEXT_(SELECT id, request_id, ca_name, status,
                              challenge_status, cert_request, challenge_type, challenge_secrets,
-                             challenge_tp, remaining_tries, remaining_time, request_type, encryption_key
+                             challenge_tp, remaining_tries, remaining_time, request_type, 
+                             encryption_key, aes_block_counter
                              FROM CaStates WHERE ca_name = ?)_SQLTEXT_");
   statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
 
@@ -277,14 +285,15 @@
     auto remainingTime = statement.getInt(10);
     auto requestType = static_cast<RequestType>(statement.getInt(11));
     auto encryptionKey = statement.getBlock(12);
+    auto aesBlockCounter = statement.getInt(13);
     if (challengeType != "") {
       result.push_back(CaState(caName, requestId, requestType, status, cert,
                                challengeType, challengeStatus, time::fromIsoString(challengeTp),
                                remainingTries, time::seconds(remainingTime),
-                               convertString2Json(challengeSecrets), encryptionKey));
+                               convertString2Json(challengeSecrets), encryptionKey, aesBlockCounter));
     }
     else {
-      result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey));
+      result.push_back(CaState(caName, requestId, requestType, status, cert, encryptionKey, aesBlockCounter));
     }
   }
   return result;