Exemplo n.º 1
0
X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher,
                                  const unsigned char *salt, int saltlen,
                                  unsigned char *aiv, uint64_t N, uint64_t r,
                                  uint64_t p)
{
    X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
    int alg_nid;
    size_t keylen = 0;
    EVP_CIPHER_CTX ctx;
    unsigned char iv[EVP_MAX_IV_LENGTH];
    PBE2PARAM *pbe2 = NULL;
    ASN1_OBJECT *obj;

    if (!cipher) {
        ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_PASSED_NULL_PARAMETER);
        goto err;
    }

    if (EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
        ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
                ASN1_R_INVALID_SCRYPT_PARAMETERS);
        goto err;
    }

    alg_nid = EVP_CIPHER_type(cipher);
    if (alg_nid == NID_undef) {
        ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
                ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
        goto err;
    }
    obj = OBJ_nid2obj(alg_nid);
    pbe2 = PBE2PARAM_new();
    if (pbe2 == NULL)
        goto merr;

    /* Setup the AlgorithmIdentifier for the encryption scheme */
    scheme = pbe2->encryption;

    scheme->algorithm = obj;
    scheme->parameter = ASN1_TYPE_new();
    if (scheme->parameter == NULL)
        goto merr;

    /* Create random IV */
    if (EVP_CIPHER_iv_length(cipher)) {
        if (aiv)
            memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
        else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0)
            goto err;
    }

    EVP_CIPHER_CTX_init(&ctx);

    /* Dummy cipherinit to just setup the IV */
    if (EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0) == 0)
        goto err;
    if (EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) {
        ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
                ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
        EVP_CIPHER_CTX_cleanup(&ctx);
        goto err;
    }
    EVP_CIPHER_CTX_cleanup(&ctx);

    /* If its RC2 then we'd better setup the key length */

    if (alg_nid == NID_rc2_cbc)
        keylen = EVP_CIPHER_key_length(cipher);

    /* Setup keyfunc */

    X509_ALGOR_free(pbe2->keyfunc);

    pbe2->keyfunc = pkcs5_scrypt_set(salt, saltlen, keylen, N, r, p);

    if (pbe2->keyfunc == NULL)
        goto merr;

    /* Now set up top level AlgorithmIdentifier */

    ret = X509_ALGOR_new();
    if (ret == NULL)
        goto merr;

    ret->algorithm = OBJ_nid2obj(NID_pbes2);

    /* Encode PBE2PARAM into parameter */

    if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
                                &ret->parameter) == NULL)
        goto merr;

    PBE2PARAM_free(pbe2);
    pbe2 = NULL;

    return ret;

 merr:
    ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_MALLOC_FAILURE);

 err:
    PBE2PARAM_free(pbe2);
    X509_ALGOR_free(kalg);
    X509_ALGOR_free(ret);

    return NULL;

}
Exemplo n.º 2
0
static void
test_libscrypt_eq_openssl(void *arg)
{
  uint8_t buf1[64];
  uint8_t buf2[64];

  uint64_t N, r, p;
  uint64_t maxmem = 0; // --> SCRYPT_MAX_MEM in OpenSSL.

  int libscrypt_retval, openssl_retval;

  size_t dk_len = 64;

  (void)arg;

  memset(buf1,0,64);
  memset(buf2,0,64);

  N = 1;
  r = 16;
  p = 1;

  libscrypt_retval =
  libscrypt_scrypt((const uint8_t *)"", 0, (const uint8_t *)"", 0,
                   r, N, p, buf1, dk_len);
  openssl_retval =
  EVP_PBE_scrypt((const char *)"", 0, (const unsigned char *)"", 0,
                  r, N, p, maxmem, buf2, dk_len);

  tt_int_op(libscrypt_retval, ==, 0);
  tt_int_op(openssl_retval, ==, 1);

  tt_mem_op(buf1, ==, buf2, 64);

  memset(buf1,0,64);
  memset(buf2,0,64);

  N = 8;
  r = 1024;
  p = 16;

  libscrypt_retval =
  libscrypt_scrypt((const uint8_t *)"password", 0,
                   (const uint8_t *)"NaCl", 0,
                   r, N, p, buf1, dk_len);
  openssl_retval =
  EVP_PBE_scrypt((const char *)"password", 0,
                 (const unsigned char *)"NaCl", 0,
                  r, N, p, maxmem, buf2, dk_len);

  tt_int_op(libscrypt_retval, ==, 0);
  tt_int_op(openssl_retval, ==, 1);

  tt_mem_op(buf1, ==, buf2, 64);

  memset(buf1,0,64);
  memset(buf2,0,64);

  N = 8;
  r = 16384;
  p = 1;

  libscrypt_retval =
  libscrypt_scrypt((const uint8_t *)"pleaseletmein", 0,
                   (const uint8_t *)"SodiumChloride", 0,
                   N, r, p, buf1, dk_len);
  openssl_retval =
  EVP_PBE_scrypt((const char *)"pleaseletmein", 0,
                 (const unsigned char *)"SodiumChloride", 0,
                  N, r, p, maxmem, buf2, dk_len);

  tt_int_op(libscrypt_retval, ==, 0);
  tt_int_op(openssl_retval, ==, 1);

  tt_mem_op(buf1, ==, buf2, 64);

#if 0
  memset(buf1,0,64);
  memset(buf2,0,64);

  r = 1048576;

  libscrypt_retval =
  libscrypt_scrypt((const uint8_t *)"pleaseletmein", 0,
                   (const uint8_t *)"SodiumChloride", 0,
                   N, r, p, buf1, dk_len);
  openssl_retval =
  EVP_PBE_scrypt((const char *)"pleaseletmein", 0,
                 (const unsigned char *)"SodiumChloride", 0,
                  N, r, p, maxmem, buf2, dk_len);

  tt_int_op(libscrypt_retval, ==, 0);
  tt_int_op(openssl_retval, ==, 1);

  tt_mem_op(buf1, ==, buf2, 64);
#endif

  done:
  return;
}
Exemplo n.º 3
0
int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
                             int passlen, ASN1_TYPE *param,
                             const EVP_CIPHER *c, const EVP_MD *md, int en_de)
{
    unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
    uint64_t p, r, N;
    size_t saltlen;
    size_t keylen = 0;
    int rv = 0;
    SCRYPT_PARAMS *sparam = NULL;

    if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
        EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_NO_CIPHER_SET);
        goto err;
    }

    /* Decode parameter */

    sparam = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), param);

    if (sparam == NULL) {
        EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_DECODE_ERROR);
        goto err;
    }

    keylen = EVP_CIPHER_CTX_key_length(ctx);

    /* Now check the parameters of sparam */

    if (sparam->keyLength) {
        uint64_t spkeylen;
        if ((ASN1_INTEGER_get_uint64(&spkeylen, sparam->keyLength) == 0)
            || (spkeylen != keylen)) {
            EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
                   EVP_R_UNSUPPORTED_KEYLENGTH);
            goto err;
        }
    }
    /* Check all parameters fit in uint64_t and are acceptable to scrypt */
    if (ASN1_INTEGER_get_uint64(&N, sparam->costParameter) == 0
        || ASN1_INTEGER_get_uint64(&r, sparam->blockSize) == 0
        || ASN1_INTEGER_get_uint64(&p, sparam->parallelizationParameter) == 0
        || EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
        EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
               EVP_R_ILLEGAL_SCRYPT_PARAMETERS);
        goto err;
    }

    /* it seems that its all OK */

    salt = sparam->salt->data;
    saltlen = sparam->salt->length;
    if (EVP_PBE_scrypt(pass, passlen, salt, saltlen, N, r, p, 0, key, keylen)
        == 0)
        goto err;
    rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
 err:
    if (keylen)
        OPENSSL_cleanse(key, keylen);
    SCRYPT_PARAMS_free(sparam);
    return rv;
}
Exemplo n.º 4
0
static void
test_libscrypt_eq_openssl(void *arg)
{
  uint8_t buf1[64];
  uint8_t buf2[64];

  uint64_t N;
  uint32_t r, p;
  uint64_t maxmem = 0; // --> SCRYPT_MAX_MEM in OpenSSL.

  int libscrypt_retval, openssl_retval;

  size_t dk_len = 64;

  (void)arg;

  memset(buf1,0,64);
  memset(buf2,0,64);

  /* NOTE: we're using N,r the way OpenSSL and libscrypt define them,
   * not the way draft-josefsson-scrypt-kdf-00.txt define them.
   */
  N = 16;
  r = 1;
  p = 1;

  libscrypt_retval =
  libscrypt_scrypt((const uint8_t *)"", 0, (const uint8_t *)"", 0,
                   N, r, p, buf1, dk_len);
  openssl_retval =
  EVP_PBE_scrypt((const char *)"", 0, (const unsigned char *)"", 0,
                  N, r, p, maxmem, buf2, dk_len);

  tt_int_op(libscrypt_retval, OP_EQ, 0);
  tt_int_op(openssl_retval, OP_EQ, 1);

  tt_mem_op(buf1, OP_EQ, buf2, 64);

  memset(buf1,0,64);
  memset(buf2,0,64);

  N = 1024;
  r = 8;
  p = 16;

  libscrypt_retval =
  libscrypt_scrypt((const uint8_t *)"password", strlen("password"),
                   (const uint8_t *)"NaCl", strlen("NaCl"),
                   N, r, p, buf1, dk_len);
  openssl_retval =
  EVP_PBE_scrypt((const char *)"password", strlen("password"),
                 (const unsigned char *)"NaCl", strlen("NaCl"),
                 N, r, p, maxmem, buf2, dk_len);

  tt_int_op(libscrypt_retval, OP_EQ, 0);
  tt_int_op(openssl_retval, OP_EQ, 1);

  tt_mem_op(buf1, OP_EQ, buf2, 64);

  memset(buf1,0,64);
  memset(buf2,0,64);

  N = 16384;
  r = 8;
  p = 1;

  libscrypt_retval =
  libscrypt_scrypt((const uint8_t *)"pleaseletmein",
                   strlen("pleaseletmein"),
                   (const uint8_t *)"SodiumChloride",
                   strlen("SodiumChloride"),
                   N, r, p, buf1, dk_len);
  openssl_retval =
  EVP_PBE_scrypt((const char *)"pleaseletmein",
                 strlen("pleaseletmein"),
                 (const unsigned char *)"SodiumChloride",
                 strlen("SodiumChloride"),
                 N, r, p, maxmem, buf2, dk_len);

  tt_int_op(libscrypt_retval, OP_EQ, 0);
  tt_int_op(openssl_retval, OP_EQ, 1);

  tt_mem_op(buf1, OP_EQ, buf2, 64);

  memset(buf1,0,64);
  memset(buf2,0,64);

  N = 1048576;
  maxmem = 2 * 1024 * 1024 * (uint64_t)1024; // 2 GB

  libscrypt_retval =
  libscrypt_scrypt((const uint8_t *)"pleaseletmein",
                   strlen("pleaseletmein"),
                   (const uint8_t *)"SodiumChloride",
                   strlen("SodiumChloride"),
                   N, r, p, buf1, dk_len);
  openssl_retval =
  EVP_PBE_scrypt((const char *)"pleaseletmein",
                 strlen("pleaseletmein"),
                 (const unsigned char *)"SodiumChloride",
                 strlen("SodiumChloride"),
                 N, r, p, maxmem, buf2, dk_len);

  tt_int_op(libscrypt_retval, OP_EQ, 0);
  tt_int_op(openssl_retval, OP_EQ, 1);

  tt_mem_op(buf1, OP_EQ, buf2, 64);

  done:
  return;
}