blob: 66632499b2f5d6163aca0f08bc634e8b10f9344e [file] [log] [blame]
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9091d832018-04-18 17:21:08 -04003 * Copyright (c) 2014-2018, Regents of the University of California
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07004 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04005 * This file is part of NAC (Name-Based Access Control for NDN).
6 * See AUTHORS.md for complete list of NAC authors and contributors.
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07007 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -04008 * NAC is free software: you can redistribute it and/or modify it under the terms
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07009 * 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 *
Alexander Afanasyev9091d832018-04-18 17:21:08 -040012 * NAC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070013 * 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
Alexander Afanasyev9091d832018-04-18 17:21:08 -040017 * NAC, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070018 */
19
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070020#include "encrypt-params.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070021#include "error.hpp"
22#include <openssl/rand.h>
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070023
24namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040025namespace nac {
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070026namespace algo {
27
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070028EncryptParams::EncryptParams(tlv::AlgorithmTypeValue algorithm, uint8_t ivLength)
29 : m_algo(algorithm)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070030{
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070031 if (ivLength != 0) {
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070032 m_iv.resize(ivLength);
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070033 int result = RAND_bytes(m_iv.data(), m_iv.size());
34 if (result != 1) {
35 BOOST_THROW_EXCEPTION(Error("Cannot generate random IV"));
36 }
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070037 }
38}
39
40void
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070041EncryptParams::setIV(const uint8_t* iv, size_t ivLen)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070042{
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070043 m_iv = Buffer(iv, ivLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070044}
45
46void
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070047EncryptParams::setAlgorithmType(tlv::AlgorithmTypeValue algorithm)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070048{
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070049 m_algo = algorithm;
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070050}
51
52Buffer
53EncryptParams::getIV() const
54{
55 return m_iv;
56}
57
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070058tlv::AlgorithmTypeValue
59EncryptParams::getAlgorithmType() const
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070060{
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070061 return m_algo;
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070062}
63
64} // namespace algo
Alexander Afanasyev9091d832018-04-18 17:21:08 -040065} // namespace nac
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070066} // namespace ndn