Crypt-OpenSSL-AES

 view release on metacpan or  search on metacpan

AES.xs  view on Meta::CPAN

    if (!options) return NULL;
    if (hv_exists(options, name, strlen(name))) {
        svp = hv_fetch(options, name, strlen(name), 0);
        value = *svp;
        return SvPV_nolen(value);
    }

    return NULL;
}

#if OPENSSL_VERSION_NUMBER >= 0x30000000L

EVP_CIPHER * get_cipher(pTHX_ HV * options, STRLEN keysize) {
    char *name = get_option_svalue(aTHX_ options, "cipher");
    char *props = get_option_svalue(aTHX_ options, "provider_props"); /* e.g. "fips=yes" */
    char cipher_name[32];

    if (name == NULL) {
        if      (keysize == 16) snprintf(cipher_name, sizeof(cipher_name), "AES-128-ECB");
        else if (keysize == 24) snprintf(cipher_name, sizeof(cipher_name), "AES-192-ECB");
        else if (keysize == 32) snprintf(cipher_name, sizeof(cipher_name), "AES-256-ECB");
        else croak("Unsupported keysize");
        name = cipher_name;
    }

    /* Validate keysize matches the cipher name prefix */
    int cipher_bits = 0;
    if (sscanf(name, "AES-%d-", &cipher_bits) == 1) {
        if ((int)keysize * 8 != cipher_bits)
            croak("You specified an unsupported cipher for this keysize");
    } else {
        croak("You specified an unsupported cipher");
    }

    EVP_CIPHER *cipher = EVP_CIPHER_fetch(NULL, name, props);
    if (!cipher)
        croak("You specified an unsupported cipher: %s", name);

    return cipher;  /* caller must EVP_CIPHER_free() */
}

#else
#if OPENSSL_VERSION_NUMBER >= 0x00908000L
#ifdef LIBRESSL_VERSION_NUMBER
const EVP_CIPHER * get_cipher(pTHX_ HV * options, STRLEN keysize) {
#else
EVP_CIPHER * get_cipher(pTHX_ HV * options, STRLEN keysize) {
#endif
    char * name = get_option_svalue(aTHX_ options, "cipher");
    char * props = get_option_svalue(aTHX_ options, "provider_props"); /* e.g. "fips=yes" */

    if (props != NULL) {
        croak ("provider_props fips=yes only supported on OpenSSL 3.0+");
    }

    if (keysize == 16) {
        if (name == NULL)
            return (EVP_CIPHER * ) EVP_aes_128_ecb();
        else if (strcmp(name, "AES-128-ECB") == 0)
            return (EVP_CIPHER * ) EVP_aes_128_ecb();
        else if (strcmp(name, "AES-128-CBC") == 0)
            return (EVP_CIPHER * ) EVP_aes_128_cbc();
        else if (strcmp(name, "AES-128-CFB") == 0)
            return (EVP_CIPHER * ) EVP_aes_128_cfb();
        else if (strcmp(name, "AES-128-CTR") == 0)
#if OPENSSL_VERSION_NUMBER >=  0x10001000L
            return (EVP_CIPHER * ) EVP_aes_128_ctr();
#else
            croak ("CTR ciphers not supported on this version of OpenSSL");
#endif
        else if (strcmp(name, "AES-128-OFB") == 0)
            return (EVP_CIPHER * ) EVP_aes_128_ofb();
        else
            croak ("You specified an unsupported cipher for this keysize: 16");
    } else if (keysize == 24) {
        if (name == NULL)
            return (EVP_CIPHER * ) EVP_aes_192_ecb();
        else if (strcmp(name, "AES-192-ECB") == 0)
            return (EVP_CIPHER * ) EVP_aes_192_ecb();
        else if (strcmp(name, "AES-192-CBC") == 0)
            return (EVP_CIPHER * ) EVP_aes_192_cbc();
        else if (strcmp(name, "AES-192-CFB") == 0)
            return (EVP_CIPHER * ) EVP_aes_192_cfb();
        else if (strcmp(name, "AES-192-CTR") == 0)
#if OPENSSL_VERSION_NUMBER >=  0x10001000L
            return (EVP_CIPHER * ) EVP_aes_192_ctr();
#else
            croak ("CTR ciphers not supported on this version of OpenSSL");
#endif
        else if (strcmp(name, "AES-192-OFB") == 0)
            return (EVP_CIPHER * ) EVP_aes_192_ofb();
        else
            croak ("You specified an unsupported cipher for this keysize: 24");
    } else if (keysize == 32) {
        if (name == NULL)
            return (EVP_CIPHER * ) EVP_aes_256_ecb();
        else if (strcmp(name, "AES-256-ECB") == 0)
            return (EVP_CIPHER * ) EVP_aes_256_ecb();
        else if (strcmp(name, "AES-256-CBC") == 0)
            return (EVP_CIPHER * ) EVP_aes_256_cbc();
        else if (strcmp(name, "AES-256-CFB") == 0)
            return (EVP_CIPHER * ) EVP_aes_256_cfb();
        else if (strcmp(name, "AES-256-CTR") == 0)
#if OPENSSL_VERSION_NUMBER >=  0x10001000L
            return (EVP_CIPHER * ) EVP_aes_256_ctr();
#else
        croak ("CTR ciphers not supported on this version of OpenSSL");
#endif
        else if (strcmp(name, "AES-256-OFB") == 0)
            return (EVP_CIPHER * ) EVP_aes_256_ofb();
        else
            croak ("You specified an unsupported cipher for this keysize: 32");
    }
    else
        croak ("You specified an unsupported keysize (16, 24 or 32 bytes only)");
}
#endif
#endif

char * get_cipher_name (pTHX_ HV * options, STRLEN keysize) {
    char * value = get_option_svalue(aTHX_ options, "cipher");
    if (value == NULL) {
        if (keysize == 16)
            return "AES-128-ECB";
        else if (keysize == 24)
            return "AES-192-ECB";
        else if (keysize == 32)
            return "AES-256-ECB";
        else
            croak ("get_cipher_name - Unsupported Key Size");
    }

    return value;
}

unsigned char * get_iv(pTHX_ HV * options, STRLEN *len) {
    SV **svp;
    if (options && hv_exists(options, "iv", 2 /* strlen("iv") */)) {
        svp = hv_fetch(options, "iv", 2 /* strlen("iv") */, 0);
        return (unsigned char *) SvPV(*svp, *len);
    }
    *len = 0;
    return NULL;
}

int get_padding(pTHX_ HV * options) {
    SV **svp;

    if (!options) return 0;

    if (hv_exists(options, "padding", 7 /* strlen("padding") */)) {
        svp = hv_fetch(options, "padding", 7 /* strlen("padding") */, 0);
        if (SvTRUE(*svp))
            return 1;
        else
            return 0;
    }
    return 0;
}



( run in 1.508 second using v1.01-cache-2.11-cpan-e1769b4cff6 )