Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2019, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
| 21 | #include "enc-tlv.hpp" |
| 22 | #include "crypto-helper.hpp" |
| 23 | #include <ndn-cxx/util/random.hpp> |
| 24 | #include <ndn-cxx/security/transform/stream-sink.hpp> |
| 25 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
| 26 | #include <ndn-cxx/security/transform/buffer-source.hpp> |
| 27 | #include <ndn-cxx/security/transform/block-cipher.hpp> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace ndncert { |
| 31 | |
| 32 | const size_t DEFAULT_IV_SIZE = 16; |
| 33 | |
| 34 | Block |
| 35 | genEncBlock(uint32_t tlv_type, const uint8_t* key, size_t keyLen, const uint8_t* payload, size_t payloadSize) |
| 36 | { |
| 37 | Buffer iv; |
| 38 | iv.resize(DEFAULT_IV_SIZE); |
| 39 | random::generateSecureBytes(iv.data(), iv.size()); |
| 40 | |
| 41 | OBufferStream os; |
| 42 | security::transform::bufferSource(payload, payloadSize) |
| 43 | >> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC, |
| 44 | CipherOperator::ENCRYPT, |
| 45 | key, keyLen, iv.data(), iv.size()) |
| 46 | >> security::transform::streamSink(os); |
| 47 | auto encryptedPayload = *os.buf(); |
| 48 | |
| 49 | // create the content block |
| 50 | auto content = makeEmptyBlock(tlv_type); |
| 51 | content.push_back(makeBinaryBlock(ENCRYPTED_PAYLOAD, encryptedPayload.data(), encryptedPayload.size())); |
| 52 | content.push_back(makeBinaryBlock(INITIAL_VECTOR, iv.data(), iv.size())); |
| 53 | content.encode(); |
| 54 | return content; |
| 55 | } |
| 56 | |
| 57 | Buffer |
| 58 | parseEncBlock(const uint8_t* key, size_t keyLen, const Block& block) |
| 59 | { |
| 60 | block.parse(); |
| 61 | Buffer iv(block.get(INITIAL_VECTOR).value(), |
| 62 | block.get(INITIAL_VECTOR).value_size()); |
| 63 | Buffer encryptedPayload(block.get(ENCRYPTED_PAYLOAD).value(), |
| 64 | block.get(ENCRYPTED_PAYLOAD).value_size()); |
| 65 | |
| 66 | OBufferStream os; |
| 67 | security::transform::bufferSource(encryptedPayload.data(), encryptedPayload.size()) |
| 68 | >> security::transform::blockCipher(BlockCipherAlgorithm::AES_CBC, |
| 69 | CipherOperator::DECRYPT, |
| 70 | key, keyLen, iv.data(), iv.size()) |
| 71 | >> security::transform::streamSink(os); |
| 72 | |
| 73 | auto payload = *os.buf(); |
| 74 | return payload; |
| 75 | } |
| 76 | |
| 77 | } // namespace ndncert |
| 78 | } // namespace ndn |