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" |
| 22 | |
| 23 | namespace ndn { |
| 24 | namespace gep { |
| 25 | namespace algo { |
| 26 | |
| 27 | using namespace CryptoPP; |
| 28 | |
| 29 | Buffer |
| 30 | crypt(CipherModeBase* cipher, const Buffer& data); |
| 31 | |
| 32 | DecryptKey<Aes> |
| 33 | Aes::generateKey(RandomNumberGenerator& rng, AesKeyParams& params) |
| 34 | { |
| 35 | SecByteBlock key(0x00, params.getKeySize() >> 3); // Converting key bit-size to byte-size. |
| 36 | rng.GenerateBlock(key.data(), key.size()); |
| 37 | |
| 38 | DecryptKey<Aes> decryptKey(std::move(Buffer(key.data(), key.size()))); |
| 39 | return decryptKey; |
| 40 | } |
| 41 | |
| 42 | EncryptKey<Aes> |
| 43 | Aes::deriveEncryptKey(const Buffer& keyBits) |
| 44 | { |
| 45 | Buffer copy = keyBits; |
| 46 | EncryptKey<Aes> encryptKey(std::move(copy)); |
| 47 | return encryptKey; |
| 48 | } |
| 49 | |
| 50 | Buffer |
| 51 | Aes::decrypt(const Buffer& keyBits, const Buffer& encryptedData, const EncryptParams& params) |
| 52 | { |
| 53 | switch (params.getEncryptMode()) { |
| 54 | case ENCRYPT_MODE_ECB_AES: |
| 55 | { |
| 56 | ECB_Mode<AES>::Decryption ecbDecryption(keyBits.get(), keyBits.size()); |
| 57 | return crypt(&ecbDecryption, encryptedData); |
| 58 | } |
| 59 | |
| 60 | case ENCRYPT_MODE_CBC_AES: |
| 61 | { |
| 62 | Buffer initVector = params.getIV(); |
| 63 | if (initVector.size() != static_cast<size_t>(AES::BLOCKSIZE)) |
| 64 | throw Error("incorrect initial vector size"); |
| 65 | |
| 66 | CBC_Mode<AES>::Decryption cbcDecryption(keyBits.get(), keyBits.size(), initVector.get()); |
| 67 | return crypt(&cbcDecryption, encryptedData); |
| 68 | } |
| 69 | |
| 70 | default: |
| 71 | throw Error("unsupported encryption mode"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | Buffer |
| 76 | Aes::encrypt(const Buffer& keyBits, const Buffer& plainData, const EncryptParams& params) |
| 77 | { |
| 78 | switch (params.getEncryptMode()) { |
| 79 | case ENCRYPT_MODE_ECB_AES: |
| 80 | { |
| 81 | ECB_Mode<AES>::Encryption ecbEncryption(keyBits.get(), keyBits.size()); |
| 82 | return crypt(&ecbEncryption, plainData); |
| 83 | } |
| 84 | |
| 85 | case ENCRYPT_MODE_CBC_AES: |
| 86 | { |
| 87 | Buffer initVector = params.getIV(); |
| 88 | if (initVector.size() != static_cast<size_t>(AES::BLOCKSIZE)) |
| 89 | throw Error("incorrect initial vector size"); |
| 90 | |
| 91 | CBC_Mode<AES>::Encryption cbcEncryption(keyBits.get(), keyBits.size(), initVector.get()); |
| 92 | return crypt(&cbcEncryption, plainData); |
| 93 | } |
| 94 | |
| 95 | default: |
| 96 | throw Error("unsupported encryption mode"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | Buffer |
| 101 | crypt(CipherModeBase* cipher, const Buffer& data) |
| 102 | { |
| 103 | OBufferStream obuf; |
| 104 | StringSource pipe(data.get(), data.size(), true, |
| 105 | new StreamTransformationFilter(*cipher, new FileSink(obuf))); |
| 106 | return *(obuf.buf()); |
| 107 | } |
| 108 | |
| 109 | } // namespace algo |
| 110 | } // namespace gep |
| 111 | } // namespace ndn |