static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)
{
    /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */

    unsigned char digest[SHA_DIGEST_LENGTH];
    unsigned char *tmp = NULL;
    EVP_MD_CTX *ctxt = NULL;
    int longg;
    int longN = BN_num_bytes(N);
    BIGNUM *res = NULL;

    if (BN_ucmp(g, N) >= 0)
        return NULL;

    ctxt = EVP_MD_CTX_new();
    if (ctxt == NULL)
        return NULL;
    if ((tmp = OPENSSL_malloc(longN)) == NULL)
        goto err;
    BN_bn2bin(N, tmp);

    if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
        || !EVP_DigestUpdate(ctxt, tmp, longN))
        goto err;

    memset(tmp, 0, longN);
    longg = BN_bn2bin(g, tmp);
    /* use the zeros behind to pad on left */
    if (!EVP_DigestUpdate(ctxt, tmp + longg, longN - longg)
        || !EVP_DigestUpdate(ctxt, tmp, longg))
        goto err;

    if (!EVP_DigestFinal_ex(ctxt, digest, NULL))
        goto err;
    res = BN_bin2bn(digest, sizeof(digest), NULL);
 err:
    OPENSSL_free(tmp);
    EVP_MD_CTX_free(ctxt);
    return res;
}