<commit_before>#include <cstdlib>
#include "pkcs11test.h"

using namespace std;  // So sue me

namespace pkcs11 {
namespace test {

class SecretKeyTest : public ReadOnlySessionTest {
 public:
  static const int kNumBlocks = 4;
  SecretKeyTest(CK_MECHANISM_TYPE keygen_mechanism,
                CK_MECHANISM_TYPE mode,
                int blocksize,
                bool emits_iv,
                int keylen = -1)
    : attrs_({CKA_ENCRYPT, CKA_DECRYPT}),
      key_(session_, attrs_, keygen_mechanism, keylen),
      mode_(mode),
      blocksize_(blocksize),
      emits_iv_(emits_iv),
      iv_(randmalloc(blocksize_)),
      plaintext_(randmalloc(kNumBlocks * blocksize_)),
      mechanism_({mode_, (emits_iv_ ? iv_.get() : NULL_PTR), (emits_iv_ ? blocksize_ : 0)}) {
    if (g_verbose && emits_iv_) cout << "IV: " << hex_data(iv_.get(), blocksize_) << endl;
    if (g_verbose) cout << "PT: " << hex_data(plaintext_.get(), kNumBlocks * blocksize_) << endl;
  }

  void TestEncryptDecrypt() {
    // First encrypt the data.
    CK_RV rv = g_fns->C_EncryptInit(session_, &mechanism_, key_.handle());
    EXPECT_CKR_OK(rv);
    if (rv != CKR_OK) return;

    CK_BYTE ciphertext[1024];
    CK_ULONG ciphertext_len = sizeof(ciphertext);
    rv = g_fns->C_Encrypt(session_, plaintext_.get(), kNumBlocks * blocksize_, ciphertext, &ciphertext_len);
    EXPECT_CKR_OK(rv);
    EXPECT_EQ(kNumBlocks * blocksize_, ciphertext_len);
    if (rv != CKR_OK) return;
    if (g_verbose) cout << "CT: " << hex_data(ciphertext, ciphertext_len) << endl;

    // Now decrypt the data.
    rv = g_fns->C_DecryptInit(session_, &mechanism_, key_.handle());
    EXPECT_CKR_OK(rv);
    if (rv != CKR_OK) return;

    CK_BYTE recovered_plaintext[1024];
    CK_ULONG recovered_plaintext_len = sizeof(recovered_plaintext);
    rv = g_fns->C_Decrypt(session_, ciphertext, ciphertext_len, recovered_plaintext, &recovered_plaintext_len);
    if (g_verbose) cout << "PT: " << hex_data(recovered_plaintext, recovered_plaintext_len) << endl;
    EXPECT_CKR_OK(rv);
    EXPECT_EQ(kNumBlocks * blocksize_, recovered_plaintext_len);
    EXPECT_EQ(0, memcmp(plaintext_.get(), recovered_plaintext, recovered_plaintext_len));
  }<commit_msg>Fix compiler warning (about int narrowing)<commit_after>#include <cstdlib>
#include "pkcs11test.h"

using namespace std;  // So sue me

namespace pkcs11 {
namespace test {

class SecretKeyTest : public ReadOnlySessionTest {
 public:
  static const int kNumBlocks = 4;
  SecretKeyTest(CK_MECHANISM_TYPE keygen_mechanism,
                CK_MECHANISM_TYPE mode,
                int blocksize,
                bool emits_iv,
                int keylen = -1)
    : attrs_({CKA_ENCRYPT, CKA_DECRYPT}),
      key_(session_, attrs_, keygen_mechanism, keylen),
      mode_(mode),
      blocksize_(blocksize),
      emits_iv_(emits_iv),
      iv_(randmalloc(blocksize_)),
      plaintext_(randmalloc(kNumBlocks * blocksize_)),
      mechanism_({mode_, (emits_iv_ ? iv_.get() : NULL_PTR), (emits_iv_ ? (CK_ULONG)blocksize_ : 0)}) {
    if (g_verbose && emits_iv_) cout << "IV: " << hex_data(iv_.get(), blocksize_) << endl;
    if (g_verbose) cout << "PT: " << hex_data(plaintext_.get(), kNumBlocks * blocksize_) << endl;
  }

  void TestEncryptDecrypt() {
    // First encrypt the data.
    CK_RV rv = g_fns->C_EncryptInit(session_, &mechanism_, key_.handle());
    EXPECT_CKR_OK(rv);
    if (rv != CKR_OK) return;

    CK_BYTE ciphertext[1024];
    CK_ULONG ciphertext_len = sizeof(ciphertext);
    rv = g_fns->C_Encrypt(session_, plaintext_.get(), kNumBlocks * blocksize_, ciphertext, &ciphertext_len);
    EXPECT_CKR_OK(rv);
    EXPECT_EQ(kNumBlocks * blocksize_, ciphertext_len);
    if (rv != CKR_OK) return;
    if (g_verbose) cout << "CT: " << hex_data(ciphertext, ciphertext_len) << endl;

    // Now decrypt the data.
    rv = g_fns->C_DecryptInit(session_, &mechanism_, key_.handle());
    EXPECT_CKR_OK(rv);
    if (rv != CKR_OK) return;

    CK_BYTE recovered_plaintext[1024];
    CK_ULONG recovered_plaintext_len = sizeof(recovered_plaintext);
    rv = g_fns->C_Decrypt(session_, ciphertext, ciphertext_len, recovered_plaintext, &recovered_plaintext_len);
    if (g_verbose) cout << "PT: " << hex_data(recovered_plaintext, recovered_plaintext_len) << endl;
    EXPECT_CKR_OK(rv);
    EXPECT_EQ(kNumBlocks * blocksize_, recovered_plaintext_len);
    EXPECT_EQ(0, memcmp(plaintext_.get(), recovered_plaintext, recovered_plaintext_len));
  }
{'fraction_non_alphanumeric': 0.12288993923024984, 'fraction_numerical': 0.007202340760747243, 'mean_word_length': 3.9487750556792873, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5947004', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1559, 'n_words': 309}