blob: 7b1e13fc5dfd29ea62c94e62d8d65f6ed173d5ce [file] [log] [blame]
[email protected]663d0ee2011-02-16 10:11:031// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]70372d42010-10-22 13:12:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/encryptor.h"
[email protected]70372d42010-10-22 13:12:346
[email protected]25007102010-11-12 16:29:067#include <openssl/aes.h>
8#include <openssl/evp.h>
9
[email protected]70372d42010-10-22 13:12:3410#include "base/logging.h"
[email protected]25007102010-11-12 16:29:0611#include "base/string_util.h"
[email protected]4b559b4d2011-04-14 17:37:1412#include "crypto/openssl_util.h"
13#include "crypto/symmetric_key.h"
[email protected]70372d42010-10-22 13:12:3414
[email protected]4b559b4d2011-04-14 17:37:1415namespace crypto {
[email protected]70372d42010-10-22 13:12:3416
[email protected]25007102010-11-12 16:29:0617namespace {
18
19const EVP_CIPHER* GetCipherForKey(SymmetricKey* key) {
20 switch (key->key().length()) {
21 case 16: return EVP_aes_128_cbc();
22 case 24: return EVP_aes_192_cbc();
23 case 32: return EVP_aes_256_cbc();
24 default: return NULL;
25 }
26}
27
28// On destruction this class will cleanup the ctx, and also clear the OpenSSL
29// ERR stack as a convenience.
30class ScopedCipherCTX {
31 public:
32 explicit ScopedCipherCTX() {
33 EVP_CIPHER_CTX_init(&ctx_);
34 }
35 ~ScopedCipherCTX() {
36 EVP_CIPHER_CTX_cleanup(&ctx_);
[email protected]be796bb2010-11-18 15:43:4337 ClearOpenSSLERRStack(FROM_HERE);
[email protected]25007102010-11-12 16:29:0638 }
39 EVP_CIPHER_CTX* get() { return &ctx_; }
40
41 private:
42 EVP_CIPHER_CTX ctx_;
43};
44
45} // namespace
46
[email protected]313834722010-11-17 09:57:1847Encryptor::Encryptor()
[email protected]663d0ee2011-02-16 10:11:0348 : key_(NULL),
49 mode_(CBC) {
[email protected]70372d42010-10-22 13:12:3450}
51
52Encryptor::~Encryptor() {
53}
54
55bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
[email protected]25007102010-11-12 16:29:0656 DCHECK(key);
57 DCHECK_EQ(CBC, mode);
58
[email protected]313834722010-11-17 09:57:1859 EnsureOpenSSLInit();
[email protected]25007102010-11-12 16:29:0660 if (iv.size() != AES_BLOCK_SIZE)
61 return false;
62
63 if (GetCipherForKey(key) == NULL)
64 return false;
65
66 key_ = key;
67 mode_ = mode;
68 iv_ = iv;
69 return true;
[email protected]70372d42010-10-22 13:12:3470}
71
72bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
[email protected]25007102010-11-12 16:29:0673 return Crypt(true, plaintext, ciphertext);
[email protected]70372d42010-10-22 13:12:3474}
75
76bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) {
[email protected]25007102010-11-12 16:29:0677 return Crypt(false, ciphertext, plaintext);
78}
79
80bool Encryptor::Crypt(bool do_encrypt,
81 const std::string& input,
82 std::string* output) {
[email protected]313834722010-11-17 09:57:1883 DCHECK(key_); // Must call Init() before En/De-crypt.
[email protected]25007102010-11-12 16:29:0684 // Work on the result in a local variable, and then only transfer it to
85 // |output| on success to ensure no partial data is returned.
86 std::string result;
87 output->swap(result);
88
89 const EVP_CIPHER* cipher = GetCipherForKey(key_);
90 DCHECK(cipher); // Already handled in Init();
91
92 const std::string& key = key_->key();
93 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), static_cast<int>(iv_.length()));
94 DCHECK_EQ(EVP_CIPHER_key_length(cipher), static_cast<int>(key.length()));
95
96 ScopedCipherCTX ctx;
97 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL,
98 reinterpret_cast<const uint8*>(key.data()),
99 reinterpret_cast<const uint8*>(iv_.data()),
100 do_encrypt))
101 return false;
102
103 // When encrypting, add another block size of space to allow for any padding.
104 const size_t output_size = input.size() + (do_encrypt ? iv_.size() : 0);
105 uint8* out_ptr = reinterpret_cast<uint8*>(WriteInto(&result,
106 output_size + 1));
107 int out_len;
108 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len,
109 reinterpret_cast<const uint8*>(input.data()),
110 input.length()))
111 return false;
112
113 // Write out the final block plus padding (if any) to the end of the data
114 // just written.
115 int tail_len;
116 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len))
117 return false;
118
119 out_len += tail_len;
120 DCHECK_LE(out_len, static_cast<int>(output_size));
121 result.resize(out_len);
122
123 output->swap(result);
124 return true;
[email protected]70372d42010-10-22 13:12:34125}
126
[email protected]4b559b4d2011-04-14 17:37:14127} // namespace crypto