int ctr_generate(RAND_DRBG *drbg,
                 unsigned char *out, size_t outlen,
                 const unsigned char *adin, size_t adinlen)
{
    RAND_DRBG_CTR *ctr = &drbg->ctr;
    if (adin != NULL && adinlen != 0) {
        ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0);
        if (drbg->flags & RAND_DRBG_FLAG_CTR_USE_DF) {
            adin = NULL;
            adinlen = 1;
        }
    } else {
        adinlen = 0;
    }
    for ( ; ; ) {
        inc_128(ctr);
        if (outlen < 16) {
            AES_encrypt(ctr->V, ctr->K, &ctr->ks);
            memcpy(out, ctr->K, outlen);
            break;
        }
        AES_encrypt(ctr->V, out, &ctr->ks);
        out += 16;
        outlen -= 16;
        if (outlen == 0)
            break;
    }
    ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0);
    return 1;
}
['static int tls_construct_cke_gost(SSL *s, WPACKET *pkt)\n{\n#ifndef OPENSSL_NO_GOST\n    EVP_PKEY_CTX *pkey_ctx = NULL;\n    X509 *peer_cert;\n    size_t msglen;\n    unsigned int md_len;\n    unsigned char shared_ukm[32], tmp[256];\n    EVP_MD_CTX *ukm_hash = NULL;\n    int dgst_nid = NID_id_GostR3411_94;\n    unsigned char *pms = NULL;\n    size_t pmslen = 0;\n    if ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0)\n        dgst_nid = NID_id_GostR3411_2012_256;\n    peer_cert = s->session->peer;\n    if (!peer_cert) {\n        SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CONSTRUCT_CKE_GOST,\n               SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);\n        return 0;\n    }\n    pkey_ctx = EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert), NULL);\n    if (pkey_ctx == NULL) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,\n                 ERR_R_MALLOC_FAILURE);\n        return 0;\n    }\n    pmslen = 32;\n    pms = OPENSSL_malloc(pmslen);\n    if (pms == NULL) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,\n                 ERR_R_MALLOC_FAILURE);\n        goto err;\n    }\n    if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0\n        || ssl_randbytes(s, pms, (int)pmslen) <= 0) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,\n                 ERR_R_INTERNAL_ERROR);\n        goto err;\n    };\n    ukm_hash = EVP_MD_CTX_new();\n    if (ukm_hash == NULL\n        || EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0\n        || EVP_DigestUpdate(ukm_hash, s->s3->client_random,\n                            SSL3_RANDOM_SIZE) <= 0\n        || EVP_DigestUpdate(ukm_hash, s->s3->server_random,\n                            SSL3_RANDOM_SIZE) <= 0\n        || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,\n                 ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    EVP_MD_CTX_free(ukm_hash);\n    ukm_hash = NULL;\n    if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT,\n                          EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,\n                 SSL_R_LIBRARY_BUG);\n        goto err;\n    }\n    msglen = 255;\n    if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,\n                 SSL_R_LIBRARY_BUG);\n        goto err;\n    }\n    if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)\n            || (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81))\n            || !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) {\n        SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,\n                 ERR_R_INTERNAL_ERROR);\n        goto err;\n    }\n    EVP_PKEY_CTX_free(pkey_ctx);\n    s->s3->tmp.pms = pms;\n    s->s3->tmp.pmslen = pmslen;\n    return 1;\n err:\n    EVP_PKEY_CTX_free(pkey_ctx);\n    OPENSSL_clear_free(pms, pmslen);\n    EVP_MD_CTX_free(ukm_hash);\n    return 0;\n#else\n    SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CKE_GOST,\n             ERR_R_INTERNAL_ERROR);\n    return 0;\n#endif\n}', 'int ssl_randbytes(SSL *s, unsigned char *rnd, size_t size)\n{\n    if (s->drbg != NULL) {\n         return RAND_DRBG_generate(s->drbg, rnd, size, 0, NULL, 0);\n    }\n    return RAND_bytes(rnd, (int)size);\n}', 'int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,\n                       int prediction_resistance,\n                       const unsigned char *adin, size_t adinlen)\n{\n    int reseed_required = 0;\n    if (drbg->state != DRBG_READY) {\n        rand_drbg_restart(drbg, NULL, 0, 0);\n        if (drbg->state == DRBG_ERROR) {\n            RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);\n            return 0;\n        }\n        if (drbg->state == DRBG_UNINITIALISED) {\n            RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);\n            return 0;\n        }\n    }\n    if (outlen > drbg->max_request) {\n        RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);\n        return 0;\n    }\n    if (adinlen > drbg->max_adinlen) {\n        RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);\n        return 0;\n    }\n    if (drbg->fork_count != rand_fork_count) {\n        drbg->fork_count = rand_fork_count;\n        reseed_required = 1;\n    }\n    if (drbg->reseed_counter >= drbg->reseed_interval)\n        reseed_required = 1;\n    if (reseed_required || prediction_resistance) {\n        if (!RAND_DRBG_reseed(drbg, adin, adinlen)) {\n            RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR);\n            return 0;\n        }\n        adin = NULL;\n        adinlen = 0;\n    }\n    if (!ctr_generate(drbg, out, outlen, adin, adinlen)) {\n        drbg->state = DRBG_ERROR;\n        RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR);\n        return 0;\n    }\n    drbg->reseed_counter++;\n    return 1;\n}', 'int ctr_generate(RAND_DRBG *drbg,\n                 unsigned char *out, size_t outlen,\n                 const unsigned char *adin, size_t adinlen)\n{\n    RAND_DRBG_CTR *ctr = &drbg->ctr;\n    if (adin != NULL && adinlen != 0) {\n        ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0);\n        if (drbg->flags & RAND_DRBG_FLAG_CTR_USE_DF) {\n            adin = NULL;\n            adinlen = 1;\n        }\n    } else {\n        adinlen = 0;\n    }\n    for ( ; ; ) {\n        inc_128(ctr);\n        if (outlen < 16) {\n            AES_encrypt(ctr->V, ctr->K, &ctr->ks);\n            memcpy(out, ctr->K, outlen);\n            break;\n        }\n        AES_encrypt(ctr->V, out, &ctr->ks);\n        out += 16;\n        outlen -= 16;\n        if (outlen == 0)\n            break;\n    }\n    ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0);\n    return 1;\n}']