494924daad4486587bc7fb3200a900c3776390ec
[dcpomatic.git] / src / lib / crypto.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 /* Based on code from https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption */
22
23 #include "crypto.h"
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>
30
31 using std::string;
32 using namespace dcpomatic;
33
34 /** The cipher that this code uses */
35 #define CIPHER EVP_aes_256_cbc()
36
37 dcp::ArrayData
38 dcpomatic::random_iv ()
39 {
40         EVP_CIPHER const * cipher = CIPHER;
41         dcp::ArrayData iv (EVP_CIPHER_iv_length(cipher));
42         RAND_bytes (iv.data(), iv.size());
43         return iv;
44 }
45
46 dcp::ArrayData
47 dcpomatic::encrypt (string plaintext, dcp::ArrayData key, dcp::ArrayData iv)
48 {
49         EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new ();
50         if (!ctx) {
51                 throw CryptoError ("could not create cipher context");
52         }
53
54         int r = EVP_EncryptInit_ex (ctx, CIPHER, 0, key.data(), iv.data());
55         if (r != 1) {
56                 throw CryptoError ("could not initialise cipher context for encryption");
57         }
58
59         dcp::ArrayData ciphertext (plaintext.size() * 2);
60
61         int len;
62         r = EVP_EncryptUpdate (ctx, ciphertext.data(), &len, (uint8_t const *) plaintext.c_str(), plaintext.size());
63         if (r != 1) {
64                 throw CryptoError ("could not encrypt data");
65         }
66
67         int ciphertext_len = len;
68
69         r = EVP_EncryptFinal_ex (ctx, ciphertext.data() + len, &len);
70         if (r != 1) {
71                 throw CryptoError ("could not finish encryption");
72         }
73
74         ciphertext.set_size (ciphertext_len + len);
75
76         EVP_CIPHER_CTX_free (ctx);
77
78         return ciphertext;
79 }
80
81 string
82 dcpomatic::decrypt (dcp::ArrayData ciphertext, dcp::ArrayData key, dcp::ArrayData iv)
83 {
84         EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new ();
85         if (!ctx) {
86                 throw CryptoError ("could not create cipher context");
87         }
88
89         int r = EVP_DecryptInit_ex (ctx, CIPHER, 0, key.data(), iv.data());
90         if (r != 1) {
91                 throw CryptoError ("could not initialise cipher context for decryption");
92         }
93
94         dcp::ArrayData plaintext (ciphertext.size() * 2);
95
96         int len;
97         r = EVP_DecryptUpdate (ctx, plaintext.data(), &len, ciphertext.data(), ciphertext.size());
98         if (r != 1) {
99                 throw CryptoError ("could not decrypt data");
100         }
101
102         int plaintext_len = len;
103
104         r = EVP_DecryptFinal_ex (ctx, plaintext.data() + len, &len);
105         if (r != 1) {
106                 throw CryptoError ("could not finish decryption");
107         }
108
109         plaintext_len += len;
110         plaintext.set_size (plaintext_len + 1);
111         plaintext.data()[plaintext_len] = '\0';
112
113         EVP_CIPHER_CTX_free (ctx);
114
115         return string ((char *) plaintext.data());
116 }
117
118 int
119 dcpomatic::crypto_key_length ()
120 {
121         return EVP_CIPHER_key_length (CIPHER);
122 }