blob: c71946b3d3a96187d24df4b116b5aff08c161f9b [file] [log] [blame]
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07004 *
5 * This file is part of gep (Group-based Encryption Protocol for NDN).
6 * See AUTHORS.md for complete list of gep authors and contributors.
7 *
8 * gep is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * gep is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * gep, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070020#include "aes.hpp"
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070021#include "error.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070022#include <openssl/rand.h>
23#include <ndn-cxx/encoding/buffer-stream.hpp>
24#include <ndn-cxx/security/transform/buffer-source.hpp>
25#include <ndn-cxx/security/transform/stream-sink.hpp>
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070026
27namespace ndn {
28namespace gep {
29namespace algo {
30
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070031DecryptKey<Aes>
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070032Aes::generateKey(AesKeyParams& params)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070033{
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070034 uint8_t key[32];
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070035
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070036 int result = RAND_bytes(key, sizeof(key));
37 if (result != 1) {
38 BOOST_THROW_EXCEPTION(Error("Cannot generate 32 bytes random AES key"));
39 }
40 DecryptKey<Aes> decryptKey(Buffer(key, sizeof(key)));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070041 return decryptKey;
42}
43
44EncryptKey<Aes>
45Aes::deriveEncryptKey(const Buffer& keyBits)
46{
47 Buffer copy = keyBits;
48 EncryptKey<Aes> encryptKey(std::move(copy));
49 return encryptKey;
50}
51
52Buffer
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070053Aes::decrypt(const uint8_t* key, size_t keyLen,
54 const uint8_t* payload, size_t payloadLen,
55 const EncryptParams& params)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070056{
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070057 if (params.getAlgorithmType() != tlv::AlgorithmAesCbc) {
58 BOOST_THROW_EXCEPTION(Error("unsupported AES decryption mode"));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070059 }
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070060
61 const Buffer& initVector = params.getIV();
62 OBufferStream os;
63 security::transform::bufferSource(payload, payloadLen)
64 >> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::DECRYPT,
65 key, keyLen, initVector.data(), initVector.size())
66 >> security::transform::streamSink(os);
67
68 auto result = os.buf();
69 return *result;
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070070}
71
72Buffer
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070073Aes::encrypt(const uint8_t* key, size_t keyLen,
74 const uint8_t* payload, size_t payloadLen,
75 const EncryptParams& params)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070076{
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070077 if (params.getAlgorithmType() != tlv::AlgorithmAesCbc) {
78 BOOST_THROW_EXCEPTION(Error("unsupported AES decryption mode"));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070079 }
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070080
81 const Buffer& initVector = params.getIV();
82 OBufferStream os;
83 security::transform::bufferSource(payload, payloadLen)
84 >> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC,
85 CipherOperator::ENCRYPT,
86 key, keyLen, initVector.data(), initVector.size())
87 >> security::transform::streamSink(os);
88
89 auto result = os.buf();
90 return *result;
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070091}
92
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070093} // namespace algo
94} // namespace gep
95} // namespace ndn