Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 4 | * |
| 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 20 | #include "aes.hpp" |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 21 | #include "error.hpp" |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 22 | #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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace gep { |
| 29 | namespace algo { |
| 30 | |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 31 | DecryptKey<Aes> |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 32 | Aes::generateKey(AesKeyParams& params) |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 33 | { |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 34 | uint8_t key[32]; |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 35 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 36 | 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 41 | return decryptKey; |
| 42 | } |
| 43 | |
| 44 | EncryptKey<Aes> |
| 45 | Aes::deriveEncryptKey(const Buffer& keyBits) |
| 46 | { |
| 47 | Buffer copy = keyBits; |
| 48 | EncryptKey<Aes> encryptKey(std::move(copy)); |
| 49 | return encryptKey; |
| 50 | } |
| 51 | |
| 52 | Buffer |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 53 | Aes::decrypt(const uint8_t* key, size_t keyLen, |
| 54 | const uint8_t* payload, size_t payloadLen, |
| 55 | const EncryptParams& params) |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 56 | { |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 57 | if (params.getAlgorithmType() != tlv::AlgorithmAesCbc) { |
| 58 | BOOST_THROW_EXCEPTION(Error("unsupported AES decryption mode")); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 59 | } |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 60 | |
| 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | Buffer |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame] | 73 | Aes::encrypt(const uint8_t* key, size_t keyLen, |
| 74 | const uint8_t* payload, size_t payloadLen, |
| 75 | const EncryptParams& params) |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 76 | { |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 77 | if (params.getAlgorithmType() != tlv::AlgorithmAesCbc) { |
| 78 | BOOST_THROW_EXCEPTION(Error("unsupported AES decryption mode")); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 79 | } |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 80 | |
| 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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 93 | } // namespace algo |
| 94 | } // namespace gep |
| 95 | } // namespace ndn |