blob: ff6c31d6f88c2f0f85b747793cd2524639b9df29 [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 "rsa.hpp"
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070021#include "error.hpp"
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070022#include <ndn-cxx/encoding/buffer-stream.hpp>
23#include <ndn-cxx/security/transform/private-key.hpp>
24#include <ndn-cxx/security/transform/public-key.hpp>
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070025
26namespace ndn {
Alexander Afanasyev9091d832018-04-18 17:21:08 -040027namespace nac {
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070028namespace algo {
29
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070030DecryptKey<Rsa>
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070031Rsa::generateKey(RsaKeyParams& params)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070032{
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070033 auto privateKey = security::transform::generatePrivateKey(params);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070034
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070035 OBufferStream os;
36 privateKey->savePkcs1(os);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070037
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070038 DecryptKey<Rsa> decryptKey(std::move(*os.buf()));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070039 return decryptKey;
40}
41
42EncryptKey<Rsa>
43Rsa::deriveEncryptKey(const Buffer& keyBits)
44{
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070045 security::transform::PrivateKey sKey;
46 sKey.loadPkcs1(keyBits.get<uint8_t>(), keyBits.size());
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070047
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070048 ConstBufferPtr pKeyBits = sKey.derivePublicKey();
49 security::transform::PublicKey pKey;
50 pKey.loadPkcs8(pKeyBits->data(), pKeyBits->size());
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070051
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070052 OBufferStream os;
53 pKey.savePkcs8(os);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070054
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070055 EncryptKey<Rsa> encryptKey(std::move(*os.buf()));
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070056 return encryptKey;
57}
58
59Buffer
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070060Rsa::decrypt(const uint8_t* key, size_t keyLen,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070061 const uint8_t* payload, size_t payloadLen)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070062{
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070063
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070064 security::transform::PrivateKey sKey;
65 sKey.loadPkcs1(key, keyLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070066
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070067 auto decrypted = sKey.decrypt(payload, payloadLen);
68 return *decrypted;
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070069}
70
71Buffer
Prashanth Swaminathand5b3eae2015-07-09 15:37:05 -070072Rsa::encrypt(const uint8_t* key, size_t keyLen,
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070073 const uint8_t* payload, size_t payloadLen)
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070074{
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070075 security::transform::PublicKey pKey;
76 pKey.loadPkcs8(key, keyLen);
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070077
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070078 auto cipherText = pKey.encrypt(payload, payloadLen);
79 return *cipherText;
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070080}
81
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070082} // namespace algo
Alexander Afanasyev9091d832018-04-18 17:21:08 -040083} // namespace nac
Prashanth Swaminathanc61cf192015-06-30 21:21:33 -070084} // namespace ndn