예제 #1
0
void
Name::get(NameLite& nameLite) const
{
  nameLite.clear();
  for (size_t i = 0; i < components_.size(); ++i) {
    ndn_Error error;
    NameLite::Component component;
    components_[i].get(component);
    if ((error = nameLite.append(component)))
      throw runtime_error(ndn_getErrorString(error));
  }
}
예제 #2
0
// Imitate Name::get(NameLite& nameLite).
static void
toNameLite(PyObject* name, NameLite& nameLite)
{
  nameLite.clear();
  PyObjectRef components(PyObject_GetAttr(name, str._components));
  for (size_t i = 0; i < PyList_GET_SIZE(components.obj); ++i) {
    ndn_Error error;
    NameLite::Component componentLite;
    toNameComponentLite(PyList_GET_ITEM(components.obj, i), componentLite);
    if ((error = nameLite.append(componentLite)))
      // TODO: Handle the error!
      return;
  }
}
/**
 * Loop to encode a data packet nIterations times using C.
 * @param nIterations The number of iterations.
 * @param useComplex If true, use a large name, large content and all fields.  If false, use a small name, small content
 * and only required fields.
 * @param useCrypto If true, sign the data packet.  If false, use a blank signature.
 * @param encoding Output buffer for the wire encoding.
 * @param maxEncodingLength The size of the encoding buffer.
 * @param encodingLength Return the number of output bytes in encoding.
 * @return The number of seconds for all iterations.
 */
static double
benchmarkEncodeDataSecondsC
  (int nIterations, bool useComplex, bool useCrypto, uint8_t* encoding, size_t maxEncodingLength, size_t *encodingLength)
{
  ndn_Error error;
  NameLite::Component finalBlockId((uint8_t*)"\x00", 1);

  ndn_NameComponent nameComponents[7];
  NameLite name(nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]));
  const size_t complexContentSize = 1115;
  char contentString[complexContentSize + 10];
  BlobLite content;
  if (useComplex) {
    // Use a large name and content.
    name.append("ndn");
    name.append("ucla.edu");
    name.append("apps");
    name.append("lwndn-test");
    name.append("numbers.txt");
    name.append("\xFD\x05\x05\xE8\x0C\xCE\x1D");
    if ((error = name.append(finalBlockId))) {
      cout << "Error in name.append: " << ndn_getErrorString(error) << endl;
      return 0;
    }

    int count = 1;
    sprintf(contentString, "%d", count++);
    while (strlen(contentString) < complexContentSize)
      sprintf(contentString + strlen(contentString), " %d", count++);
    content = BlobLite((uint8_t*)contentString, strlen(contentString));
  }
  else {
    // Use a small name and content.
    name.append("test");
    content = BlobLite((uint8_t*)"abc", 3);
  }

  ndn_NameComponent certificateNameComponents[5];
  NameLite certificateName
    (certificateNameComponents, sizeof(certificateNameComponents) / sizeof(certificateNameComponents[0]));
  certificateName.append("testname");
  certificateName.append("KEY");
  certificateName.append("DSK-123");
  certificateName.append("ID-CERT");
  if ((error = certificateName.append("0"))) {
    cout << "Error in certificateName.append: " << ndn_getErrorString(error) << endl;
    return 0;
  }

  uint8_t signatureBitsArray[256];
  memset(signatureBitsArray, 0, sizeof(signatureBitsArray));

  // Set up the private key now in case useCrypto is true.
  // Use a temporary pointer since d2i updates it.
  const uint8_t *privateKeyDerPointer = DEFAULT_RSA_PRIVATE_KEY_DER;
  RSA *privateKey = d2i_RSAPrivateKey(NULL, &privateKeyDerPointer, sizeof(DEFAULT_RSA_PRIVATE_KEY_DER));
  if (!privateKey) {
    // Don't expect this to happen.
    cout << "Error decoding private key DER" << endl;
    return 0;
  }

  double start = getNowSeconds();
  for (int i = 0; i < nIterations; ++i) {
    // Since we aren't going to change the names, we can re-use the arrays.
    DataLite data
      (nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
       certificateNameComponents,
       sizeof(certificateNameComponents) / sizeof(certificateNameComponents[0]));

    if ((error = data.setName(name))) {
      cout << "Error in data.setName: " << ndn_getErrorString(error) << endl;
      return 0;
    }
    data.setContent(content);
    if (useComplex) {
      data.getMetaInfo().setFreshnessPeriod(1000);
      data.getMetaInfo().setFinalBlockId(finalBlockId);
    }

    // Assume the encoding buffer is big enough so we don't need to dynamically reallocate.
    DynamicUInt8ArrayLite output(encoding, maxEncodingLength, 0);
    size_t signedPortionBeginOffset, signedPortionEndOffset;

    data.getSignature().getKeyLocator().setType(ndn_KeyLocatorType_KEYNAME);
    if ((error = data.getSignature().getKeyLocator().setKeyName(certificateName))) {
      cout << "Error in data.setKeyName: " << ndn_getErrorString(error) << endl;
      return 0;
    }
    if (useCrypto) {
      data.getSignature().setType(ndn_SignatureType_Sha256WithRsaSignature);

      // Encode once to get the signed portion.
      size_t dummyEncodingLength;
      if ((error = Tlv0_1_1WireFormatLite::encodeData
           (data, &signedPortionBeginOffset, &signedPortionEndOffset,
            output, &dummyEncodingLength))) {
        cout << "Error in ndn_encodeTlvData: " << ndn_getErrorString(error) << endl;
        return 0;
      }

      // Imitate MemoryPrivateKeyStorage::sign.
      uint8_t digest[SHA256_DIGEST_LENGTH];
      ndn_digestSha256(encoding + signedPortionBeginOffset, signedPortionEndOffset - signedPortionBeginOffset, digest);
      unsigned int signatureBitsLength;
      if (!RSA_sign(NID_sha256, digest, sizeof(digest), signatureBitsArray, &signatureBitsLength, privateKey)) {
        // Don't expect this to happen.
        cout << "Error in RSA_sign" << endl;
        return 0;
      }

      data.getSignature().setSignature(BlobLite(signatureBitsArray, signatureBitsLength));
    }
    else {
      // Set up the signature, but don't sign.
      data.getSignature().setSignature(BlobLite(signatureBitsArray, sizeof(signatureBitsArray)));
      data.getSignature().setType(ndn_SignatureType_Sha256WithRsaSignature);
    }

    if ((error = Tlv0_1_1WireFormatLite::encodeData
         (data, &signedPortionBeginOffset, &signedPortionEndOffset,
          output, encodingLength))) {
      cout << "Error in ndn_encodeTlvData: " << ndn_getErrorString(error) << endl;
      return 0;
    }
  }
  double finish = getNowSeconds();

  if (privateKey)
    RSA_free(privateKey);

  return finish - start;
}