~voidraven/scry

30e8d2d5f2980d922d4b636a3f39f5c5f0d24639 — lotus 11 months ago efd3a80
Expanded option flags, added more runtime checks, modified tests, updated README
7 files changed, 68 insertions(+), 38 deletions(-)

M README.md
M compile_commands.json
M include/main.hpp
M include/scry.hpp
M main.cpp
M scry.cpp
M tests/test_scry.cpp
M README.md => README.md +3 -0
@@ 39,6 39,9 @@ SuperSecretPassword [enter]

Flags:
-s      # provide your own salt (aka nonce)
-n      # cpu/memory cost (default 4096)
-r      # block size (default 8)
-p      # parallelism (default 2)
-h      # shows this help info
-v      # shows the version info
```

M compile_commands.json => compile_commands.json +19 -0
@@ 13,6 13,25 @@
            "tests", 
            "-I", 
            "/usr/local/include", 
            "scry.cpp"
        ], 
        "directory": "/home/neb/git/scry", 
        "file": "scry.cpp"
    }, 
    {
        "arguments": [
            "c++", 
            "-c", 
            "--std=c++14", 
            "-pedantic", 
            "-Wall", 
            "-Wextra", 
            "-I", 
            "include", 
            "-I", 
            "tests", 
            "-I", 
            "/usr/local/include", 
            "main.cpp"
        ], 
        "directory": "/home/neb/git/scry", 

M include/main.hpp => include/main.hpp +16 -12
@@ 11,10 11,11 @@

struct Params {
  std::string salt;
  uint8_t n;
  uint8_t ram;
  uint8_t cpu;
  bool option_passed;
  uint64_t cost;
  uint8_t block;
  uint64_t parallel;
  bool s_option_passed;
  bool nrp_option_passed;
  const std::string VERSION;
};



@@ 40,27 41,30 @@ bool check_for_ascii(std::string val) {

/* r, p, and buflen
 * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32.
 * N must be a power of 2 greater than 1.
 * N must be a power of 2 greater than 1 and less than 2^(128 * r / 8).
 */
bool check_args(CLI::App& app, Params& p) {
  if (app.count("-v")) {
    print_version(p.VERSION);
    return false;
  }
  if (app.count("-s")) {
    if(!check_for_ascii(p.salt)) return false;
    p.s_option_passed = true;
  }
  if (app.count("-r")) {
    // check range
    p.option_passed = true;
    p.nrp_option_passed = true;
    return true;
  }
  if (app.count("-p")) {
    // check range
    p.option_passed = true;
    // The parallelization parameter p
    // is a positive integer less than or equal
    // to ((2^32-1) * 32) / (128 * r)
    if(!(p.parallel <= ((2^32-1) * 32 / (128 * p.block)))) return false;
    p.nrp_option_passed = true;
    return true;
  }
  if (app.count("-s")) {
    if(!check_for_ascii(p.salt)) return false;
    p.option_passed = true;
  }
 
  return true;
}

M include/scry.hpp => include/scry.hpp +2 -2
@@ 17,8 17,8 @@

class Scry {
public:
  int make_key(const char* password) const;
  int make_key(const char* salt, const char* password, int N=1, int r=8, int p=1) const;
  int make_key(const char* password, uint64_t n, uint8_t r, uint64_t p) const;
  int make_key(const char* salt, const char* password, uint64_t n, uint8_t r, uint64_t p) const;
};

#endif // MY_SCRY

M main.cpp => main.cpp +19 -12
@@ 1,4 1,5 @@


/*  Main ('Runner') implementation file
 *  author: lotus
 *  license: ISC


@@ 8,7 9,7 @@

int main(int argc, char* argv[]) {
  const std::string VERSION = "0.1";
  Params p{ "", 1, 8, 1, false,VERSION };
  Params p{ "", 4096, 8, 2, false, false, VERSION };
  int rv = 1;
  
  // parse the user input flags & options


@@ 16,8 17,9 @@ int main(int argc, char* argv[]) {
  app.ignore_case();

  app.add_option("-s, --salt", p.salt, "Specify your own salt (default behavior uses /dev/urandom)");
  app.add_option("-r, --ram", p.ram, "Change the standard ram cost");
  app.add_option("-p, --cpu", p.cpu, "Change the standard cpu cost");
  app.add_option("-n, --cpucost", p.cost, "The CPU/Memory cost (default 4096)");
  app.add_option("-r, --blocksize", p.block, "Change the standard blocksize (default 8)");
  app.add_option("-p, --parallelism", p.parallel, "How much parallelism to use (default 2)");
  app.add_flag("-v,--version", "Display the version of scry");

  CLI11_PARSE(app, argc, argv);


@@ 31,23 33,28 @@ int main(int argc, char* argv[]) {
  }
  if (args_ok) {
    // good input, let's get to work
    auto sp_sobj = std::make_unique<Scry>();
    auto scry_obj = std::make_unique<Scry>();
    std::string line;
    std::string str_pass;
    while (std::getline(std::cin, line) && !line.empty()) {
      std::cout << "line = "<< line << "\n";
      str_pass = line;
    }
    bool pass_ok = str_pass.length() < 512;
    if (p.option_passed && pass_ok) {
    bool pass_ok = str_pass.length() < 513;
    if (p.s_option_passed && pass_ok) {
      // call other function.
      const char *password = line.c_str();
      // rv = sp_sobj->make_key(p.salt, password, p.n, p.ram, p.cpu);
      const char* password = line.c_str();
      //rv = scry_obj->make_key(p.salt, password, p.cost, p.block, p.parallel);
    }
    else if (!p.s_option_passed && p.nrp_option_passed) {
      // salt was not provided, but the -n, -r, or -p flag was used
      // so we have to grab a random salt from /dev/urandom
    }
    else if (pass_ok) {
      std::cout << "str_pass = " << str_pass << "\n";
      const char *password = line.c_str();
      rv = sp_sobj->make_key(password);
      const char* password = line.c_str();
      rv = scry_obj->make_key(password, p.cost, p.block, p.parallel);
    }
    else {
      std::cout << "There was an issue reading your input. Was your password too long?\n";
    }
  }
  

M scry.cpp => scry.cpp +4 -7
@@ 11,13 11,11 @@

/* --- Member Functions --- */

int Scry::make_key(const char* password) const {
  int n = 2;
  int r = 8;
  int p = 2;

int Scry::make_key(const char* password, uint64_t n, uint8_t r, uint64_t p) const {
  char dest[125];
  try {
    int rv = libscrypt_hash(&dest[0], password, n, r, p);
    libscrypt_hash(&dest[0], password, n, r, p);
  }
  catch(...) {
    std::cout << "Caught exception during key creation!\n";


@@ 27,8 25,7 @@ int Scry::make_key(const char* password) const {
  return 1;
}


int Scry::make_key(const char* salt, const char* password, int n, int r, int p) const {
int Scry::make_key(const char* salt, const char* password, uint64_t n, uint8_t r, uint64_t p) const {
  int i = 0;
  return i;
}

M tests/test_scry.cpp => tests/test_scry.cpp +5 -5
@@ 4,25 4,25 @@

TEST_CASE( "Empty password given", "[empty-pass]" ) {
  auto sp = std::make_unique<Scry>();
  REQUIRE( sp->make_key("") > 0 );
  REQUIRE( sp->make_key("", 4096, 8, 2) > 0 );
}

TEST_CASE( "Short password given", "[short-pass]" ) {
  auto sp = std::make_unique<Scry>();
  REQUIRE( sp->make_key("a") > 0 );
  REQUIRE( sp->make_key("a", 4096, 8, 2) > 0 );
}

TEST_CASE( "Medium password given", "[medium-pass]" ) {
  auto sp = std::make_unique<Scry>();
  REQUIRE( sp->make_key("aaaaaaaaaa12341234") > 0 );
  REQUIRE( sp->make_key("aaaaaaaaaa12341234", 4096, 8, 2) > 0 );
}

TEST_CASE( "Long password given", "[long-pass]" ) {
  auto sp = std::make_unique<Scry>();
  REQUIRE( sp->make_key("aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341!@#$%^&*()-=+\\|`~[]{};<>?") > 0 );
  REQUIRE( sp->make_key("aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341!@#$%^&*()-=+\\|`~[]{};<>?", 4096, 8, 2) > 0 );
}

TEST_CASE( "Very long password given", "[short-pass]" ) {
  auto sp = std::make_unique<Scry>();
  REQUIRE( sp->make_key("aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341!@#$%^&*()-=\\+|`~[]{};<>?aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341!@#$%^&*()-=+|`~[]{};<>?") > 0 );
  REQUIRE( sp->make_key("aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341!@#$%^&*()-=\\+|`~[]{};<>?aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341234aaaaaaaaaa12341!@#$%^&*()-=+|`~[]{};<>?", 4096, 8, 2) > 0 );
}