blob: 8342300a4163d354a27029eb8aa7ab1f71a584d0 [file] [log] [blame]
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07004 *
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"
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070023#include <ndn-cxx/encoding/buffer-stream.hpp>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070024#include <ndn-cxx/security/transform/block-cipher.hpp>
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070025#include <ndn-cxx/security/transform/buffer-source.hpp>
26#include <ndn-cxx/security/transform/stream-sink.hpp>
27#include <ndn-cxx/util/random.hpp>
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070028
29namespace ndn {
30namespace ndncert {
31
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070032Block
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070033encodeBlockWithAesGcm128(uint32_t tlv_type, const uint8_t* key, const uint8_t* payload, size_t payloadSize,
34 const uint8_t* associatedData, size_t associatedDataSize)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070035{
36 Buffer iv;
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070037 iv.resize(12);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070038 random::generateSecureBytes(iv.data(), iv.size());
39
tylerliudc393022020-10-13 02:18:39 -070040 uint8_t* encryptedPayload = new uint8_t[payloadSize];
tylerliua3d87252020-10-13 01:48:52 -070041 uint8_t tag[16];
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070042 size_t encryptedPayloadLen = aes_gcm_128_encrypt(payload, payloadSize, associatedData, associatedDataSize,
43 key, iv.data(), encryptedPayload, tag);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044 auto content = makeEmptyBlock(tlv_type);
tylerliu50d679e2020-10-14 14:08:39 -070045 content.push_back(makeBinaryBlock(tlv::InitializationVector, iv.data(), iv.size()));
46 content.push_back(makeBinaryBlock(tlv::AuthenticationTag, tag, 16));
47 content.push_back(makeBinaryBlock(tlv::EncryptedPayload, encryptedPayload, encryptedPayloadLen));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048 content.encode();
tylerliudc393022020-10-13 02:18:39 -070049 delete[] encryptedPayload;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070050 return content;
51}
52
53Buffer
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070054decodeBlockWithAesGcm128(const Block& block, const uint8_t* key, const uint8_t* associatedData, size_t associatedDataSize)
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070055{
56 block.parse();
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070057 Buffer result;
tylerliu50d679e2020-10-14 14:08:39 -070058 result.resize(block.get(tlv::EncryptedPayload).value_size());
59 int resultLen = aes_gcm_128_decrypt(block.get(tlv::EncryptedPayload).value(),
60 block.get(tlv::EncryptedPayload).value_size(),
61 associatedData, associatedDataSize, block.get(tlv::AuthenticationTag).value(),
62 key, block.get(tlv::InitializationVector).value(), result.data());
63 if (resultLen == -1 || resultLen != (int)block.get(tlv::EncryptedPayload).value_size()) {
Zhiyi Zhangb8cb0472020-05-05 20:55:05 -070064 return Buffer();
65 }
66 return result;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070067}
68
Zhiyi Zhange4891b72020-10-10 15:11:57 -070069} // namespace ndncert
70} // namespace ndn