blob: 6a7d2e07f4ff9d202b17e9e4911191901b269dde [file] [log] [blame]
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California
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
20#include <ndn-cxx/encoding/buffer-stream.hpp>
21#include "aes.hpp"
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070022#include "error.hpp"
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070023
24namespace ndn {
25namespace gep {
26namespace algo {
27
28using namespace CryptoPP;
29
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070030static Buffer
31transform(CipherModeBase* cipher, const uint8_t* data, size_t dataLen)
32{
33 OBufferStream obuf;
34 StringSource pipe(data, dataLen, true,
35 new StreamTransformationFilter(*cipher, new FileSink(obuf)));
36 return *(obuf.buf());
37}
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070038
39DecryptKey<Aes>
40Aes::generateKey(RandomNumberGenerator& rng, AesKeyParams& params)
41{
42 SecByteBlock key(0x00, params.getKeySize() >> 3); // Converting key bit-size to byte-size.
43 rng.GenerateBlock(key.data(), key.size());
44
Alexander Afanasyev867228e2016-10-17 16:54:55 -070045 DecryptKey<Aes> decryptKey(Buffer(key.data(), key.size()));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070046 return decryptKey;
47}
48
49EncryptKey<Aes>
50Aes::deriveEncryptKey(const Buffer& keyBits)
51{
52 Buffer copy = keyBits;
53 EncryptKey<Aes> encryptKey(std::move(copy));
54 return encryptKey;
55}
56
57Buffer
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070058Aes::decrypt(const uint8_t* key, size_t keyLen,
59 const uint8_t* payload, size_t payloadLen,
60 const EncryptParams& params)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070061{
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070062 switch (params.getAlgorithmType()) {
63 case tlv::AlgorithmAesEcb: {
64 ECB_Mode<AES>::Decryption ecbDecryption(key, keyLen);
65 return transform(&ecbDecryption, payload, payloadLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070066 }
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070067 case tlv::AlgorithmAesCbc: {
68 const Buffer& initVector = params.getIV();
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070069 if (initVector.size() != static_cast<size_t>(AES::BLOCKSIZE))
70 throw Error("incorrect initial vector size");
71
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070072 CBC_Mode<AES>::Decryption cbcDecryption(key, keyLen, initVector.get());
73 return transform(&cbcDecryption, payload, payloadLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070074 }
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070075 default:
76 throw Error("unsupported encryption mode");
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070077 }
78}
79
80Buffer
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070081Aes::encrypt(const uint8_t* key, size_t keyLen,
82 const uint8_t* payload, size_t payloadLen,
83 const EncryptParams& params)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070084{
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070085 switch (params.getAlgorithmType()) {
86 case tlv::AlgorithmAesEcb: {
87 ECB_Mode<AES>::Encryption ecbEncryption(key, keyLen);
88 return transform(&ecbEncryption, payload, payloadLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070089 }
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070090 case tlv::AlgorithmAesCbc: {
91 const Buffer& initVector = params.getIV();
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070092 if (initVector.size() != static_cast<size_t>(AES::BLOCKSIZE))
93 throw Error("incorrect initial vector size");
94
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070095 CBC_Mode<AES>::Encryption cbcEncryption(key, keyLen, initVector.get());
96 return transform(&cbcEncryption, payload, payloadLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070097 }
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070098 default:
99 throw Error("unsupported encryption mode");
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700100 }
101}
102
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -0700103} // namespace algo
104} // namespace gep
105} // namespace ndn