2 Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 /* Based on code from https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption */
24 #include "exceptions.h"
25 #include <openssl/conf.h>
26 #include <openssl/evp.h>
27 #include <openssl/err.h>
28 #include <openssl/rand.h>
29 #include <boost/scoped_array.hpp>
32 using boost::shared_array;
33 using namespace dcpomatic;
35 /** The cipher that this code uses */
36 #define CIPHER EVP_aes_256_cbc()
39 dcpomatic::random_iv ()
41 EVP_CIPHER const * cipher = CIPHER;
42 dcp::Data iv (EVP_CIPHER_iv_length(cipher));
43 RAND_bytes (iv.data().get(), iv.size());
48 dcpomatic::encrypt (string plaintext, dcp::Data key, dcp::Data iv)
50 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new ();
52 throw CryptoError ("could not create cipher context");
55 int r = EVP_EncryptInit_ex (ctx, CIPHER, 0, key.data().get(), iv.data().get());
57 throw CryptoError ("could not initialise cipher context for encryption");
60 dcp::Data ciphertext (plaintext.size() * 2);
63 r = EVP_EncryptUpdate (ctx, ciphertext.data().get(), &len, (uint8_t const *) plaintext.c_str(), plaintext.size());
65 throw CryptoError ("could not encrypt data");
68 int ciphertext_len = len;
70 r = EVP_EncryptFinal_ex (ctx, ciphertext.data().get() + len, &len);
72 throw CryptoError ("could not finish encryption");
75 ciphertext.set_size (ciphertext_len + len);
77 EVP_CIPHER_CTX_free (ctx);
83 dcpomatic::decrypt (dcp::Data ciphertext, dcp::Data key, dcp::Data iv)
85 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new ();
87 throw CryptoError ("could not create cipher context");
90 int r = EVP_DecryptInit_ex (ctx, CIPHER, 0, key.data().get(), iv.data().get());
92 throw CryptoError ("could not initialise cipher context for decryption");
95 dcp::Data plaintext (ciphertext.size() * 2);
98 r = EVP_DecryptUpdate (ctx, plaintext.data().get(), &len, ciphertext.data().get(), ciphertext.size());
100 throw CryptoError ("could not decrypt data");
103 int plaintext_len = len;
105 r = EVP_DecryptFinal_ex (ctx, plaintext.data().get() + len, &len);
107 throw CryptoError ("could not finish decryption");
110 plaintext_len += len;
111 plaintext.set_size (plaintext_len + 1);
112 plaintext.data().get()[plaintext_len] = '\0';
114 EVP_CIPHER_CTX_free (ctx);
116 return string ((char *) plaintext.data().get());
120 dcpomatic::crypto_key_length ()
122 return EVP_CIPHER_key_length (CIPHER);