Logo Search packages:      
Sourcecode: yapet version File versions  Download package

Key::Key ( const char *  password  )  throw (YAPETException)

Initializes the key.

Initializes the key and the initialization vector. Make sure you securely destroy the password provided to this method.

Parameters:
password a pointer to the location the password is stored. The password has to be zero-terminated.

Definition at line 57 of file key.cc.

References cleanup(), IVec, IVECLENGTH, key, KEYLENGTH, MD5_LEN, RIPEMD160_LEN, and SHA1_LEN.

                                                   {
    // Sentinel variable to check the size of the key
    uint8_t eff_keylength;

    //
    // First run (sha1)
    //
    const EVP_MD* md = EVP_sha1();
    if (md == NULL)
      throw YAPETException(_("Run 1: Unable to initialize the EVP_MD structure"));

    EVP_MD_CTX mdctx;
    EVP_MD_CTX_init(&mdctx);

    int retval = EVP_DigestInit_ex(&mdctx, md, NULL);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      throw YAPETException(_("Run 1: Unable to initialize the digest"));
    }

    retval = EVP_DigestUpdate(&mdctx, password, strlen(password));
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      throw YAPETException(_("Run 1: Unable to update the digest"));
    }

    unsigned int tmplen;
    retval = EVP_DigestFinal_ex(&mdctx, key, &tmplen);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 1: Unable to finalize the digest"));
    }

    if (tmplen != SHA1_LEN) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 1: Digest does not have expected length"));
    }

    eff_keylength = tmplen;
    EVP_MD_CTX_cleanup(&mdctx);

    //
    // Second run (md5)
    //
    md = EVP_md5();
    if (md == NULL) {
      cleanup();
      throw YAPETException(_("Run 2: Unable to initialize the EVP_MD structure"));
    }

    EVP_MD_CTX_init(&mdctx);
    retval = EVP_DigestInit_ex(&mdctx, md, NULL);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 2: Unable to initialize the digest"));
    }

    retval = EVP_DigestUpdate(&mdctx, key, SHA1_LEN);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 2: Unable to update the digest"));
    }

    retval = EVP_DigestFinal_ex(&mdctx, key + SHA1_LEN, &tmplen);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 2: Unable to finalize the digest"));
    }

    if (tmplen != MD5_LEN) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 2: Digest does not have expected length"));
    }

    eff_keylength += tmplen;
    EVP_MD_CTX_cleanup(&mdctx);

    //
    // Third run (ripemd160)
    //
    md = EVP_ripemd160();
    if (md == NULL) {
      cleanup();
      throw YAPETException(_("Run 3: Unable to initialize the EVP_MD structure"));
    }

    EVP_MD_CTX_init(&mdctx);
    retval = EVP_DigestInit_ex(&mdctx, md, NULL);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 3: Unable to initialize the digest"));
    }

    retval = EVP_DigestUpdate(&mdctx, key, SHA1_LEN + MD5_LEN);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 3: Unable to update the digest"));
    }

    retval = EVP_DigestFinal_ex(&mdctx, key + SHA1_LEN + MD5_LEN, &tmplen);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 3: Unable to finalize the digest"));
    }

    if (tmplen != RIPEMD160_LEN) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("Run 3: Digest does not have expected length"));
    }

    eff_keylength += tmplen;
    EVP_MD_CTX_cleanup(&mdctx);

    if (eff_keylength != KEYLENGTH) {
      cleanup();
      char tmp[100];
      snprintf(tmp,
             100,
             _("Effective key length of %d does not match expected key length %d"),
             eff_keylength,
             KEYLENGTH);
      throw YAPETException(tmp);
    }

    //
    // The initialization vector
    //
    uint8_t ivec_hash_buf[MD5_LEN];
    md = EVP_md5();
    if (md == NULL) {
      cleanup();
      throw YAPETException(_("IVec: Unable to initialize the EVP_MD structure"));
    }

    EVP_MD_CTX_init(&mdctx);
    retval = EVP_DigestInit_ex(&mdctx, md, NULL);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("IVec: Unable to initialize the digest"));
    }

    retval = EVP_DigestUpdate(&mdctx, key, SHA1_LEN + MD5_LEN + RIPEMD160_LEN);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("IVec: Unable to update the digest"));
    }

    retval = EVP_DigestFinal_ex(&mdctx, ivec_hash_buf, &tmplen);
    if (retval == 0) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("IVec: Unable to finalize the digest"));
    }

    if (tmplen != MD5_LEN) {
      EVP_MD_CTX_cleanup(&mdctx);
      cleanup();
      throw YAPETException(_("IVec: Digest does not have expected length"));
    }

    EVP_MD_CTX_cleanup(&mdctx);

    memcpy(IVec, ivec_hash_buf, IVECLENGTH);
    memset(ivec_hash_buf, 0, MD5_LEN);
}


Generated by  Doxygen 1.6.0   Back to index