Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / common / tizen / encryption.cc
1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "xwalk/application/common/tizen/encryption.h"
7
8 #include <string>
9
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "crypto/encryptor.h"
16 #include "crypto/random.h"
17 #include "crypto/symmetric_key.h"
18
19 namespace xwalk {
20 namespace application {
21
22 const int kInitCounterSize = 16;
23
24 namespace {
25 // The following file types should be encrypted when the application is
26 // installed on the client device.
27 // The application launching, the file should be decrypted by run time.
28 const base::FilePath::StringType kHTMLFormat(FILE_PATH_LITERAL(".html"));
29 const base::FilePath::StringType kHTMFormat(FILE_PATH_LITERAL(".htm"));
30 const base::FilePath::StringType kJSFormat(FILE_PATH_LITERAL(".js"));
31 const base::FilePath::StringType kCSSFormat(FILE_PATH_LITERAL(".css"));
32 }
33
34 bool RequiresEncryption(const base::FilePath& file_path) {
35   return EndsWith(file_path.value(), kHTMLFormat, false) ||
36     EndsWith(file_path.value(), kHTMFormat, false) ||
37     EndsWith(file_path.value(), kJSFormat, false) ||
38     EndsWith(file_path.value(), kCSSFormat, false);
39 }
40
41 bool EncryptData(const char* plain_data, int len,
42                  const std::string& key, std::string* out_encrypted_data) {
43   // Assume the input is valid (not empty).
44   DCHECK(plain_data);
45   DCHECK_GT(len, 0);
46   DCHECK(out_encrypted_data);
47
48   // generate initial vector
49   scoped_ptr<char[], base::FreeDeleter> initCounter =
50       scoped_ptr<char[], base::FreeDeleter>(
51           static_cast<char*>(malloc(kInitCounterSize)));
52   crypto::RandBytes(initCounter.get(), kInitCounterSize);
53   base::StringPiece init_counter_str(
54       initCounter.get(), kInitCounterSize);
55
56   scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
57       crypto::SymmetricKey::AES, key));
58   crypto::Encryptor encryptor;
59   encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, "");
60   base::StringPiece plaintext_str(plain_data, len);
61   encryptor.SetCounter(init_counter_str);
62   std::string encrypted;
63   bool result = encryptor.Encrypt(plaintext_str, &encrypted);
64   if (!result)
65     return result;
66   *out_encrypted_data = std::string(initCounter.get(),
67                                     kInitCounterSize) + encrypted;
68   return result;
69 }
70
71 bool DecryptData(const char* encrypted_data, int len,
72                  const std::string& key, std::string* out_plain_data) {
73   // Assume the input is valid (not empty).
74   DCHECK(encrypted_data);
75   DCHECK_GT(len, 0);
76   DCHECK(out_plain_data);
77
78   std::string tmp_encrypted = std::string(encrypted_data, len);
79   std::string init_counter = tmp_encrypted.substr(0, kInitCounterSize);
80   std::string encrypted = tmp_encrypted.substr(kInitCounterSize,
81                                                len-kInitCounterSize);
82
83   scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
84       crypto::SymmetricKey::AES, key));
85   crypto::Encryptor encryptor;
86   encryptor.Init(sym_key.get(), crypto::Encryptor::CTR, "");
87   base::StringPiece init_counter_str(init_counter.c_str(),
88                                      init_counter.size());
89   encryptor.SetCounter(init_counter_str);
90   return encryptor.Decrypt(encrypted, out_plain_data);
91 }
92
93 }  // namespace application
94 }  // namespace xwalk