Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 1 | /* -*- 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 Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 22 | #include "error.hpp" |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 23 | |
| 24 | namespace ndn { |
| 25 | namespace gep { |
| 26 | namespace algo { |
| 27 | |
| 28 | using namespace CryptoPP; |
| 29 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 30 | static Buffer |
| 31 | transform(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 Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 38 | |
| 39 | DecryptKey<Aes> |
| 40 | Aes::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 | |
| 45 | DecryptKey<Aes> decryptKey(std::move(Buffer(key.data(), key.size()))); |
| 46 | return decryptKey; |
| 47 | } |
| 48 | |
| 49 | EncryptKey<Aes> |
| 50 | Aes::deriveEncryptKey(const Buffer& keyBits) |
| 51 | { |
| 52 | Buffer copy = keyBits; |
| 53 | EncryptKey<Aes> encryptKey(std::move(copy)); |
| 54 | return encryptKey; |
| 55 | } |
| 56 | |
| 57 | Buffer |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 58 | Aes::decrypt(const uint8_t* key, size_t keyLen, |
| 59 | const uint8_t* payload, size_t payloadLen, |
| 60 | const EncryptParams& params) |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 61 | { |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 62 | switch (params.getAlgorithmType()) { |
| 63 | case tlv::AlgorithmAesEcb: { |
| 64 | ECB_Mode<AES>::Decryption ecbDecryption(key, keyLen); |
| 65 | return transform(&ecbDecryption, payload, payloadLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 66 | } |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 67 | case tlv::AlgorithmAesCbc: { |
| 68 | const Buffer& initVector = params.getIV(); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 69 | if (initVector.size() != static_cast<size_t>(AES::BLOCKSIZE)) |
| 70 | throw Error("incorrect initial vector size"); |
| 71 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 72 | CBC_Mode<AES>::Decryption cbcDecryption(key, keyLen, initVector.get()); |
| 73 | return transform(&cbcDecryption, payload, payloadLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 74 | } |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 75 | default: |
| 76 | throw Error("unsupported encryption mode"); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | Buffer |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 81 | Aes::encrypt(const uint8_t* key, size_t keyLen, |
| 82 | const uint8_t* payload, size_t payloadLen, |
| 83 | const EncryptParams& params) |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 84 | { |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 85 | switch (params.getAlgorithmType()) { |
| 86 | case tlv::AlgorithmAesEcb: { |
| 87 | ECB_Mode<AES>::Encryption ecbEncryption(key, keyLen); |
| 88 | return transform(&ecbEncryption, payload, payloadLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 89 | } |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 90 | case tlv::AlgorithmAesCbc: { |
| 91 | const Buffer& initVector = params.getIV(); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 92 | if (initVector.size() != static_cast<size_t>(AES::BLOCKSIZE)) |
| 93 | throw Error("incorrect initial vector size"); |
| 94 | |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 95 | CBC_Mode<AES>::Encryption cbcEncryption(key, keyLen, initVector.get()); |
| 96 | return transform(&cbcEncryption, payload, payloadLen); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 97 | } |
Prashanth Swaminathan | d5b3eae | 2015-07-09 15:37:05 -0700 | [diff] [blame^] | 98 | default: |
| 99 | throw Error("unsupported encryption mode"); |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Prashanth Swaminathan | c61cf19 | 2015-06-30 21:21:33 -0700 | [diff] [blame] | 103 | } // namespace algo |
| 104 | } // namespace gep |
| 105 | } // namespace ndn |