rsa-public-key-lite.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_RSA_PUBLIC_KEY_LITE_HPP
23 #define NDN_RSA_PUBLIC_KEY_LITE_HPP
24 
25 #include "../util/blob-lite.hpp"
26 #include "../../c/errors.h"
27 #include "../../c/encrypt/algo/encrypt-params-types.h"
28 #include "../../c/security/rsa-public-key-types.h"
29 
30 namespace ndn {
31 
37 public:
42 
47 
56  ndn_Error
57  decode(const uint8_t* publicKeyDer, size_t publicKeyDerLength);
58 
66  ndn_Error
67  decode(const BlobLite& publicKeyDer)
68  {
69  return decode(publicKeyDer.buf(), publicKeyDer.size());
70  }
71 
83  ndn_Error
84  encode(uint8_t* encoding, size_t& encodingLength) const;
85 
94  bool
96  (const uint8_t* signature, size_t signatureLength, const uint8_t* data,
97  size_t dataLength) const;
98 
105  bool
106  verifyWithSha256(const BlobLite& signature, const BlobLite& data) const
107  {
108  return verifyWithSha256
109  (signature.buf(), signature.size(), data.buf(), data.size());
110  }
111 
125  static ndn_Error
127  (const uint8_t *signature, size_t signatureLength, const uint8_t *data,
128  size_t dataLength, const uint8_t *publicKeyDer, size_t publicKeyDerLength,
129  bool &verified);
130 
140  static ndn_Error
142  (const BlobLite& signature, const BlobLite& data,
143  const BlobLite& publicKeyDer, bool &verified)
144  {
146  (signature.buf(), signature.size(), data.buf(), data.size(),
147  publicKeyDer.buf(), publicKeyDer.size(), verified);
148  }
149 
163  ndn_Error
164  encrypt
165  (const uint8_t* plainData, size_t plainDataLength,
166  ndn_EncryptAlgorithmType algorithmType, uint8_t* encryptedData,
167  size_t& encryptedDataLength) const;
168 
181  ndn_Error
182  encrypt
183  (const BlobLite& plainData, ndn_EncryptAlgorithmType algorithmType,
184  uint8_t* encryptedData, size_t& encryptedDataLength) const
185  {
186  return encrypt
187  (plainData.buf(), plainData.size(), algorithmType, encryptedData,
188  encryptedDataLength);
189  }
190 
196  static RsaPublicKeyLite&
197  downCast(ndn_RsaPublicKey& blob) { return *(RsaPublicKeyLite*)&blob; }
198 
199  static const RsaPublicKeyLite&
200  downCast(const ndn_RsaPublicKey& blob) { return *(RsaPublicKeyLite*)&blob; }
201 
202 private:
203  // Don't allow copying since we don't reference count the allocated value.
204  RsaPublicKeyLite(const RsaPublicKeyLite& other);
205  RsaPublicKeyLite& operator=(const RsaPublicKeyLite& other);
206 };
207 
208 }
209 
210 #endif
static RsaPublicKeyLite & downCast(ndn_RsaPublicKey &blob)
Downcast the reference to the ndn_RsaPublicKey struct to a RsaPublicKeyLite.
Definition: rsa-public-key-lite.hpp:197
size_t size() const
Return size given to the constructor.
Definition: blob-lite.hpp:61
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
~RsaPublicKeyLite()
Finalize the RsaPublicKeyLite, freeing memory if needed.
Definition: rsa-public-key-lite.cpp:32
static ndn_Error verifySha256WithRsaSignature(const uint8_t *signature, size_t signatureLength, const uint8_t *data, size_t dataLength, const uint8_t *publicKeyDer, size_t publicKeyDerLength, bool &verified)
Verify the RSA signature of the data using the given public key.
Definition: rsa-public-key-lite.cpp:61
ndn_Error decode(const BlobLite &publicKeyDer)
Decode the publicKeyDer and set this RsaPublicKeyLite, allocating memory as needed.
Definition: rsa-public-key-lite.hpp:67
ndn_Error decode(const uint8_t *publicKeyDer, size_t publicKeyDerLength)
Decode the publicKeyDer and set this RsaPublicKeyLite, allocating memory as needed.
Definition: rsa-public-key-lite.cpp:39
An RsaPublicKeyLite holds a decoded RSA public key for use in crypto operations.
Definition: rsa-public-key-lite.hpp:36
bool verifyWithSha256(const uint8_t *signature, size_t signatureLength, const uint8_t *data, size_t dataLength) const
Use this public key to verify the data using RsaWithSha256.
Definition: rsa-public-key-lite.cpp:52
A struct ndn_RsaPublicKey holds a decoded RSA public key for use in crypto operations.
Definition: rsa-public-key-types.h:34
RsaPublicKeyLite()
Create an RsaPublicKeyLite with a null value.
Definition: rsa-public-key-lite.cpp:27
A BlobLite holds a pointer to an immutable pre-allocated buffer and its length This is like a JavaScr...
Definition: blob-lite.hpp:37
const uint8_t * buf() const
Return buf given to the constructor.
Definition: blob-lite.hpp:55
bool verifyWithSha256(const BlobLite &signature, const BlobLite &data) const
Use this public key to verify the data using RsaWithSha256.
Definition: rsa-public-key-lite.hpp:106
ndn_Error encrypt(const uint8_t *plainData, size_t plainDataLength, ndn_EncryptAlgorithmType algorithmType, uint8_t *encryptedData, size_t &encryptedDataLength) const
Use this public key to encrypt plainData according to the algorithmType.
Definition: rsa-public-key-lite.cpp:75
ndn_Error encode(uint8_t *encoding, size_t &encodingLength) const
Encode the DER-encoded SubjectPublicKeyInfo.
Definition: rsa-public-key-lite.cpp:45