| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "openssl/opensslv.h" |
| 6 |
|
| |
| 7 |
|
| #include <openssl/aes.h> |
| 8 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 9 |
|
| #include <openssl/evp.h> |
| 10 |
|
| #endif |
| 11 |
|
| |
| 12 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 13 |
|
| #include <openssl/rand.h> |
| 14 |
|
| #endif |
| 15 |
|
| |
| 16 |
|
| #include "ppport.h" |
| 17 |
|
| |
| 18 |
|
| /* |
| 19 |
|
| * Copyright (C) 2006-2024 DelTel, Inc. |
| 20 |
|
| * |
| 21 |
|
| * This library is free software; you can redistribute it and/or modify |
| 22 |
|
| * it under the same terms as Perl itself, either Perl version 5.8.5 or, |
| 23 |
|
| * at your option, any later version of Perl 5 you may have available. |
| 24 |
|
| */ |
| 25 |
|
| |
| 26 |
|
| typedef struct state { |
| 27 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 28 |
|
| EVP_CIPHER_CTX *enc_ctx; |
| 29 |
|
| EVP_CIPHER_CTX *dec_ctx; |
| 30 |
|
| int padding; |
| 31 |
|
| #ifdef USE_ITHREADS |
| 32 |
|
| tTHX tid; |
| 33 |
|
| #endif |
| 34 |
|
| #else |
| 35 |
|
| AES_KEY enc_key; |
| 36 |
|
| AES_KEY dec_key; |
| 37 |
|
| int padding; |
| 38 |
|
| #endif |
| 39 |
|
| } *Crypt__OpenSSL__AES; |
| 40 |
|
| |
| 41 |
|
| #define THROW(p_result) if (!(p_result)) { error = 1; goto err; } |
| 42 |
|
| |
| 43 |
265 |
| char * get_option_svalue (pTHX_ HV * options, char * name) { |
| 44 |
|
| SV **svp; |
| 45 |
|
| SV * value; |
| 46 |
|
| |
| 47 |
265 |
▶ | if (!options) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!options) return NULL; |
18 |
247 |
265 |
|
| 48 |
247 |
▶ | if (hv_exists(options, name, strlen(name))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(options, name, strlen(name))) { |
161 |
86 |
247 |
|
| 49 |
161 |
| svp = hv_fetch(options, name, strlen(name), 0); |
| 50 |
161 |
| value = *svp; |
| 51 |
161 |
| return SvPV_nolen(value); |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
86 |
| return NULL; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 58 |
|
| |
| 59 |
88 |
| EVP_CIPHER * get_cipher(pTHX_ HV * options, STRLEN keysize) { |
| 60 |
88 |
| char *name = get_option_svalue(aTHX_ options, "cipher"); |
| 61 |
88 |
| char *props = get_option_svalue(aTHX_ options, "provider_props"); /* e.g. "fips=yes" */ |
| 62 |
|
| char cipher_name[32]; |
| 63 |
|
| |
| 64 |
88 |
▶ | if (name == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (name == NULL) { |
8 |
80 |
88 |
|
| 65 |
8 |
▶ | if (keysize == 16) snprintf(cipher_name, sizeof(cipher_name), "AES-128-ECB"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (keysize == 16) snprintf(cipher_name, sizeof(cipher_name), "AES-128-ECB"); |
3 |
5 |
8 |
|
| 66 |
5 |
▶ | else if (keysize == 24) snprintf(cipher_name, sizeof(cipher_name), "AES-192-ECB"); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (keysize == 24) snprintf(cipher_name, sizeof(cipher_name), "AES-192-ECB"); |
1 |
4 |
5 |
|
| 67 |
4 |
▶ | else if (keysize == 32) snprintf(cipher_name, sizeof(cipher_name), "AES-256-ECB"); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (keysize == 32) snprintf(cipher_name, sizeof(cipher_name), "AES-256-ECB"); |
4 |
0 |
4 |
|
| 68 |
0 |
| else croak("Unsupported keysize"); |
| 69 |
8 |
| name = cipher_name; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| /* Validate keysize matches the cipher name prefix */ |
| 73 |
88 |
| int cipher_bits = 0; |
| 74 |
88 |
▶ | if (sscanf(name, "AES-%d-", &cipher_bits) == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sscanf(name, "AES-%d-", &cipher_bits) == 1) { |
88 |
0 |
88 |
|
| 75 |
88 |
▶ | if ((int)keysize * 8 != cipher_bits) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((int)keysize * 8 != cipher_bits) |
32 |
56 |
88 |
|
| 76 |
32 |
| croak("You specified an unsupported cipher for this keysize"); |
| 77 |
|
| } else { |
| 78 |
0 |
| croak("You specified an unsupported cipher"); |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
56 |
| EVP_CIPHER *cipher = EVP_CIPHER_fetch(NULL, name, props); |
| 82 |
56 |
▶ | if (!cipher) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cipher) |
0 |
56 |
56 |
|
| 83 |
0 |
| croak("You specified an unsupported cipher: %s", name); |
| 84 |
|
| |
| 85 |
56 |
| return cipher; /* caller must EVP_CIPHER_free() */ |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| #else |
| 89 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 90 |
|
| #ifdef LIBRESSL_VERSION_NUMBER |
| 91 |
|
| const EVP_CIPHER * get_cipher(pTHX_ HV * options, STRLEN keysize) { |
| 92 |
|
| #else |
| 93 |
|
| EVP_CIPHER * get_cipher(pTHX_ HV * options, STRLEN keysize) { |
| 94 |
|
| #endif |
| 95 |
|
| char * name = get_option_svalue(aTHX_ options, "cipher"); |
| 96 |
|
| char * props = get_option_svalue(aTHX_ options, "provider_props"); /* e.g. "fips=yes" */ |
| 97 |
|
| |
| 98 |
|
| if (props != NULL) { |
| 99 |
|
| croak ("provider_props fips=yes only supported on OpenSSL 3.0+"); |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| if (keysize == 16) { |
| 103 |
|
| if (name == NULL) |
| 104 |
|
| return (EVP_CIPHER * ) EVP_aes_128_ecb(); |
| 105 |
|
| else if (strcmp(name, "AES-128-ECB") == 0) |
| 106 |
|
| return (EVP_CIPHER * ) EVP_aes_128_ecb(); |
| 107 |
|
| else if (strcmp(name, "AES-128-CBC") == 0) |
| 108 |
|
| return (EVP_CIPHER * ) EVP_aes_128_cbc(); |
| 109 |
|
| else if (strcmp(name, "AES-128-CFB") == 0) |
| 110 |
|
| return (EVP_CIPHER * ) EVP_aes_128_cfb(); |
| 111 |
|
| else if (strcmp(name, "AES-128-CTR") == 0) |
| 112 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x10001000L |
| 113 |
|
| return (EVP_CIPHER * ) EVP_aes_128_ctr(); |
| 114 |
|
| #else |
| 115 |
|
| croak ("CTR ciphers not supported on this version of OpenSSL"); |
| 116 |
|
| #endif |
| 117 |
|
| else if (strcmp(name, "AES-128-OFB") == 0) |
| 118 |
|
| return (EVP_CIPHER * ) EVP_aes_128_ofb(); |
| 119 |
|
| else |
| 120 |
|
| croak ("You specified an unsupported cipher for this keysize: 16"); |
| 121 |
|
| } else if (keysize == 24) { |
| 122 |
|
| if (name == NULL) |
| 123 |
|
| return (EVP_CIPHER * ) EVP_aes_192_ecb(); |
| 124 |
|
| else if (strcmp(name, "AES-192-ECB") == 0) |
| 125 |
|
| return (EVP_CIPHER * ) EVP_aes_192_ecb(); |
| 126 |
|
| else if (strcmp(name, "AES-192-CBC") == 0) |
| 127 |
|
| return (EVP_CIPHER * ) EVP_aes_192_cbc(); |
| 128 |
|
| else if (strcmp(name, "AES-192-CFB") == 0) |
| 129 |
|
| return (EVP_CIPHER * ) EVP_aes_192_cfb(); |
| 130 |
|
| else if (strcmp(name, "AES-192-CTR") == 0) |
| 131 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x10001000L |
| 132 |
|
| return (EVP_CIPHER * ) EVP_aes_192_ctr(); |
| 133 |
|
| #else |
| 134 |
|
| croak ("CTR ciphers not supported on this version of OpenSSL"); |
| 135 |
|
| #endif |
| 136 |
|
| else if (strcmp(name, "AES-192-OFB") == 0) |
| 137 |
|
| return (EVP_CIPHER * ) EVP_aes_192_ofb(); |
| 138 |
|
| else |
| 139 |
|
| croak ("You specified an unsupported cipher for this keysize: 24"); |
| 140 |
|
| } else if (keysize == 32) { |
| 141 |
|
| if (name == NULL) |
| 142 |
|
| return (EVP_CIPHER * ) EVP_aes_256_ecb(); |
| 143 |
|
| else if (strcmp(name, "AES-256-ECB") == 0) |
| 144 |
|
| return (EVP_CIPHER * ) EVP_aes_256_ecb(); |
| 145 |
|
| else if (strcmp(name, "AES-256-CBC") == 0) |
| 146 |
|
| return (EVP_CIPHER * ) EVP_aes_256_cbc(); |
| 147 |
|
| else if (strcmp(name, "AES-256-CFB") == 0) |
| 148 |
|
| return (EVP_CIPHER * ) EVP_aes_256_cfb(); |
| 149 |
|
| else if (strcmp(name, "AES-256-CTR") == 0) |
| 150 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x10001000L |
| 151 |
|
| return (EVP_CIPHER * ) EVP_aes_256_ctr(); |
| 152 |
|
| #else |
| 153 |
|
| croak ("CTR ciphers not supported on this version of OpenSSL"); |
| 154 |
|
| #endif |
| 155 |
|
| else if (strcmp(name, "AES-256-OFB") == 0) |
| 156 |
|
| return (EVP_CIPHER * ) EVP_aes_256_ofb(); |
| 157 |
|
| else |
| 158 |
|
| croak ("You specified an unsupported cipher for this keysize: 32"); |
| 159 |
|
| } |
| 160 |
|
| else |
| 161 |
|
| croak ("You specified an unsupported keysize (16, 24 or 32 bytes only)"); |
| 162 |
|
| } |
| 163 |
|
| #endif |
| 164 |
|
| #endif |
| 165 |
|
| |
| 166 |
89 |
| char * get_cipher_name (pTHX_ HV * options, STRLEN keysize) { |
| 167 |
89 |
| char * value = get_option_svalue(aTHX_ options, "cipher"); |
| 168 |
89 |
▶ | if (value == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value == NULL) { |
8 |
81 |
89 |
|
| 169 |
8 |
▶ | if (keysize == 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if (keysize == 16) |
3 |
5 |
8 |
|
| 170 |
3 |
| return "AES-128-ECB"; |
| 171 |
5 |
▶ | else if (keysize == 24) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (keysize == 24) |
1 |
4 |
5 |
|
| 172 |
1 |
| return "AES-192-ECB"; |
| 173 |
4 |
▶ | else if (keysize == 32) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (keysize == 32) |
4 |
0 |
4 |
|
| 174 |
4 |
| return "AES-256-ECB"; |
| 175 |
|
| else |
| 176 |
0 |
| croak ("get_cipher_name - Unsupported Key Size"); |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
81 |
| return value; |
| 180 |
|
| } |
| 181 |
|
| |
| 182 |
56 |
| unsigned char * get_iv(pTHX_ HV * options, STRLEN *len) { |
| 183 |
|
| SV **svp; |
| 184 |
56 |
▶ | if (options && hv_exists(options, "iv", 2 /* strlen("iv") */)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (options && hv_exists(options, "iv", 2 /* strlen("iv") */)) { |
50 |
6 |
56 |
| if (options && hv_exists(options, "iv", 2 /* strlen("iv") */)) { |
24 |
26 |
50 |
|
| 185 |
24 |
| svp = hv_fetch(options, "iv", 2 /* strlen("iv") */, 0); |
| 186 |
48 |
| return (unsigned char *) SvPV(*svp, *len); |
| 187 |
|
| } |
| 188 |
32 |
| *len = 0; |
| 189 |
32 |
| return NULL; |
| 190 |
|
| } |
| 191 |
|
| |
| 192 |
89 |
| int get_padding(pTHX_ HV * options) { |
| 193 |
|
| SV **svp; |
| 194 |
|
| |
| 195 |
89 |
▶ | if (!options) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!options) return 0; |
6 |
83 |
89 |
|
| 196 |
|
| |
| 197 |
83 |
▶ | if (hv_exists(options, "padding", 7 /* strlen("padding") */)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(options, "padding", 7 /* strlen("padding") */)) { |
67 |
16 |
83 |
|
| 198 |
67 |
| svp = hv_fetch(options, "padding", 7 /* strlen("padding") */, 0); |
| 199 |
67 |
▶ | if (SvTRUE(*svp)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTRUE(*svp)) |
60 |
7 |
67 |
|
| 200 |
60 |
| return 1; |
| 201 |
|
| else |
| 202 |
7 |
| return 0; |
| 203 |
|
| } |
| 204 |
16 |
| return 0; |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| /* Taken from p5-Git-Raw */ |
| 208 |
83 |
| STATIC HV *ensure_hv(pTHX_ SV *sv, const char *identifier) { |
| 209 |
83 |
▶ | if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) |
83 |
0 |
83 |
| if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) |
0 |
83 |
83 |
|
| 210 |
0 |
| croak("Invalid type for '%s', expected a hash", identifier); |
| 211 |
|
| |
| 212 |
83 |
| return (HV *) SvRV(sv); |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
|
| MODULE = Crypt::OpenSSL::AES PACKAGE = Crypt::OpenSSL::AES |
| 216 |
|
| |
| 217 |
|
| PROTOTYPES: ENABLE |
| 218 |
|
| |
| 219 |
|
| BOOT: |
| 220 |
|
| { |
| 221 |
13 |
| HV *stash = gv_stashpv("Crypt::OpenSSL::AES", 0); |
| 222 |
|
| |
| 223 |
13 |
| newCONSTSUB (stash, "keysize", newSViv (32)); |
| 224 |
13 |
| newCONSTSUB (stash, "blocksize", newSViv (AES_BLOCK_SIZE)); |
| 225 |
|
| } |
| 226 |
|
| |
| 227 |
|
| Crypt::OpenSSL::AES |
| 228 |
|
| new(class, key_sv, ...) |
| 229 |
|
| SV * class |
| 230 |
|
| SV * key_sv |
| 231 |
|
| CODE: |
| 232 |
|
| { |
| 233 |
|
| PERL_UNUSED_ARG(class); |
| 234 |
|
| STRLEN keysize; |
| 235 |
|
| unsigned char * key; |
| 236 |
89 |
| HV * options = NULL; |
| 237 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 238 |
|
| #ifdef LIBRESSL_VERSION_NUMBER |
| 239 |
|
| const EVP_CIPHER * cipher; |
| 240 |
|
| #else |
| 241 |
|
| EVP_CIPHER * cipher; |
| 242 |
|
| #endif |
| 243 |
89 |
| unsigned char * iv = NULL; |
| 244 |
89 |
| STRLEN iv_len = 0; |
| 245 |
89 |
| char * cipher_name = NULL; |
| 246 |
|
| #endif |
| 247 |
89 |
▶ | if (items > 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 2) |
83 |
6 |
89 |
|
| 248 |
83 |
| options = ensure_hv(aTHX_ ST(2), "options"); |
| 249 |
|
| |
| 250 |
89 |
▶ | if (!SvPOK (key_sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvPOK (key_sv)) |
0 |
89 |
89 |
|
| 251 |
0 |
| croak("Key must be a scalar"); |
| 252 |
|
| |
| 253 |
89 |
| key = (unsigned char *) SvPVbyte_nolen(key_sv); |
| 254 |
89 |
| keysize = SvCUR(key_sv); |
| 255 |
|
| |
| 256 |
89 |
▶ | if (keysize != 16 && keysize != 24 && keysize != 32) |
Branch: 3 branches
| Branch | True | False | Total |
| if (keysize != 16 && keysize != 24 && keysize != 32) |
61 |
28 |
89 |
| if (keysize != 16 && keysize != 24 && keysize != 32) |
38 |
23 |
61 |
| if (keysize != 16 && keysize != 24 && keysize != 32) |
0 |
38 |
38 |
|
| 257 |
0 |
| croak ("The key must be 128, 192 or 256 bits long"); |
| 258 |
|
| |
| 259 |
89 |
| Newxz(RETVAL, 1, struct state); |
| 260 |
89 |
| RETVAL->padding = get_padding(aTHX_ options); |
| 261 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 262 |
89 |
| cipher_name = get_cipher_name(aTHX_ options, keysize); |
| 263 |
89 |
▶ | if ((strcmp(cipher_name, "AES-128-ECB") == 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| if ((strcmp(cipher_name, "AES-128-ECB") == 0 || |
69 |
20 |
89 |
|
| 264 |
69 |
▶ | strcmp(cipher_name, "AES-192-ECB") == 0 || |
Branch: 1 branch
| Branch | True | False | Total |
| strcmp(cipher_name, "AES-192-ECB") == 0 || |
50 |
19 |
69 |
|
| 265 |
50 |
▶ | strcmp(cipher_name, "AES-256-ECB") == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| strcmp(cipher_name, "AES-256-ECB") == 0) |
25 |
25 |
50 |
|
| 266 |
64 |
▶ | && (options && hv_exists(options, "iv", strlen("iv")))) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (options && hv_exists(options, "iv", strlen("iv")))) { |
58 |
6 |
64 |
| && (options && hv_exists(options, "iv", strlen("iv")))) { |
1 |
57 |
58 |
|
| 267 |
1 |
| Safefree(RETVAL); |
| 268 |
1 |
| croak ("%s does not use IV", cipher_name); |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
88 |
| cipher = get_cipher(aTHX_ options, keysize); |
| 272 |
56 |
| iv = get_iv(aTHX_ options, &iv_len); |
| 273 |
|
| |
| 274 |
56 |
| int cipher_iv_len = EVP_CIPHER_iv_length(cipher); |
| 275 |
56 |
▶ | if (cipher_iv_len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cipher_iv_len > 0) { |
24 |
32 |
56 |
|
| 276 |
24 |
▶ | if (!iv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!iv) { |
0 |
24 |
24 |
|
| 277 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 278 |
0 |
| EVP_CIPHER_free(cipher); |
| 279 |
|
| #endif |
| 280 |
0 |
| Safefree(RETVAL); |
| 281 |
0 |
| croak("Cipher %s requires an IV of %d bytes, but none was provided", cipher_name, cipher_iv_len); |
| 282 |
|
| } |
| 283 |
24 |
▶ | if (iv_len != (STRLEN)cipher_iv_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (iv_len != (STRLEN)cipher_iv_len) { |
2 |
22 |
24 |
|
| 284 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 285 |
2 |
| EVP_CIPHER_free(cipher); |
| 286 |
|
| #endif |
| 287 |
2 |
| Safefree(RETVAL); |
| 288 |
2 |
| croak("Invalid IV length for %s: expected %d bytes, got %d", |
| 289 |
|
| cipher_name, cipher_iv_len, (int)iv_len); |
| 290 |
|
| } |
| 291 |
|
| } |
| 292 |
|
| |
| 293 |
|
| /* Create and initialise the context */ |
| 294 |
54 |
▶ | if(!(RETVAL->enc_ctx = EVP_CIPHER_CTX_new())) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(RETVAL->enc_ctx = EVP_CIPHER_CTX_new())) { |
0 |
54 |
54 |
|
| 295 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 296 |
0 |
| EVP_CIPHER_free(cipher); |
| 297 |
|
| #endif |
| 298 |
0 |
| Safefree(RETVAL); |
| 299 |
0 |
| croak ("EVP_CIPHER_CTX_new failed for enc_ctx"); |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
54 |
▶ | if(!(RETVAL->dec_ctx = EVP_CIPHER_CTX_new())) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!(RETVAL->dec_ctx = EVP_CIPHER_CTX_new())) { |
0 |
54 |
54 |
|
| 303 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 304 |
0 |
| EVP_CIPHER_free(cipher); |
| 305 |
|
| #endif |
| 306 |
0 |
| EVP_CIPHER_CTX_free(RETVAL->enc_ctx); |
| 307 |
0 |
| Safefree(RETVAL); |
| 308 |
0 |
| croak ("EVP_CIPHER_CTX_new failed for dec_ctx"); |
| 309 |
|
| } |
| 310 |
|
| |
| 311 |
54 |
▶ | if(1 != EVP_EncryptInit_ex(RETVAL->enc_ctx, cipher, |
Branch: 1 branch
| Branch | True | False | Total |
| if(1 != EVP_EncryptInit_ex(RETVAL->enc_ctx, cipher, |
0 |
54 |
54 |
|
| 312 |
|
| NULL, key, iv)) { |
| 313 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 314 |
0 |
| EVP_CIPHER_free(cipher); |
| 315 |
|
| #endif |
| 316 |
0 |
| EVP_CIPHER_CTX_free(RETVAL->enc_ctx); |
| 317 |
0 |
| EVP_CIPHER_CTX_free(RETVAL->dec_ctx); |
| 318 |
0 |
| Safefree(RETVAL); |
| 319 |
0 |
| croak ("EVP_EncryptInit_ex failed"); |
| 320 |
|
| } |
| 321 |
|
| |
| 322 |
54 |
▶ | if(1 != EVP_DecryptInit_ex(RETVAL->dec_ctx, cipher, |
Branch: 1 branch
| Branch | True | False | Total |
| if(1 != EVP_DecryptInit_ex(RETVAL->dec_ctx, cipher, |
0 |
54 |
54 |
|
| 323 |
|
| NULL, key, iv)) { |
| 324 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 325 |
0 |
| EVP_CIPHER_free(cipher); |
| 326 |
|
| #endif |
| 327 |
0 |
| EVP_CIPHER_CTX_free(RETVAL->enc_ctx); |
| 328 |
0 |
| EVP_CIPHER_CTX_free(RETVAL->dec_ctx); |
| 329 |
0 |
| Safefree(RETVAL); |
| 330 |
0 |
| croak ("EVP_DecryptInit_ex failed"); |
| 331 |
|
| } |
| 332 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 333 |
54 |
| EVP_CIPHER_free(cipher); |
| 334 |
|
| #endif |
| 335 |
|
| #else |
| 336 |
|
| AES_set_encrypt_key(key,keysize*8,&RETVAL->enc_key); |
| 337 |
|
| AES_set_decrypt_key(key,keysize*8,&RETVAL->dec_key); |
| 338 |
|
| #endif |
| 339 |
|
| #if defined(USE_ITHREADS) && (OPENSSL_VERSION_NUMBER >= 0x00908000L) |
| 340 |
|
| /* Store the creating thread's ID so DESTROY can warn on misuse */ |
| 341 |
|
| RETVAL->tid = aTHX; |
| 342 |
|
| #endif |
| 343 |
|
| } |
| 344 |
|
| OUTPUT: |
| 345 |
|
| RETVAL |
| 346 |
|
| |
| 347 |
|
| SV * |
| 348 |
|
| encrypt(self, data) |
| 349 |
|
| Crypt::OpenSSL::AES self |
| 350 |
|
| SV *data |
| 351 |
|
| CODE: |
| 352 |
|
| { |
| 353 |
|
| int error; |
| 354 |
|
| STRLEN size; |
| 355 |
44 |
| unsigned char * plaintext = (unsigned char *) SvPVbyte(data,size); |
| 356 |
|
| const char * ciphertext; |
| 357 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 358 |
44 |
| int out_len = 0; |
| 359 |
44 |
| int ciphertext_len = 0; |
| 360 |
44 |
| int block_size = EVP_CIPHER_CTX_block_size(self->enc_ctx); |
| 361 |
|
| #else |
| 362 |
|
| int block_size = AES_BLOCK_SIZE; |
| 363 |
|
| #endif |
| 364 |
|
| #if defined(USE_ITHREADS) && (OPENSSL_VERSION_NUMBER >= 0x00908000L) |
| 365 |
|
| if (self->tid != aTHX) |
| 366 |
|
| croak("Crypt::OpenSSL::AES: encrypt() called from a different " |
| 367 |
|
| "thread than the object was created in -- " |
| 368 |
|
| "EVP_CIPHER_CTX is not thread-safe"); |
| 369 |
|
| #endif |
| 370 |
44 |
| error = 0; |
| 371 |
44 |
▶ | if((size % block_size != 0) && self->padding != 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if((size % block_size != 0) && self->padding != 1) { |
5 |
39 |
44 |
| if((size % block_size != 0) && self->padding != 1) { |
2 |
3 |
5 |
|
| 372 |
2 |
| croak("AES: Data size must be multiple of blocksize (%d bytes)", block_size); |
| 373 |
|
| } |
| 374 |
42 |
| Newxc(ciphertext, size + block_size, unsigned char, const char); |
| 375 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 376 |
42 |
▶ | if (1 != EVP_EncryptInit_ex(self->enc_ctx, NULL, NULL, NULL, NULL)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (1 != EVP_EncryptInit_ex(self->enc_ctx, NULL, NULL, NULL, NULL)) { |
0 |
42 |
42 |
|
| 377 |
0 |
| Safefree(ciphertext); |
| 378 |
0 |
| croak("EVP_EncryptInit_ex re-init failed"); |
| 379 |
|
| } |
| 380 |
|
| |
| 381 |
42 |
| EVP_CIPHER_CTX_set_padding(self->enc_ctx, self->padding); |
| 382 |
|
| |
| 383 |
42 |
▶ | THROW(EVP_EncryptUpdate(self->enc_ctx, (unsigned char *) ciphertext , &out_len, plaintext, size)); |
Branch: 1 branch
| Branch | True | False | Total |
| THROW(EVP_EncryptUpdate(self->enc_ctx, (unsigned char *) ciphertext , &out_len, plaintext, size)); |
0 |
42 |
42 |
|
| 384 |
|
| |
| 385 |
42 |
| ciphertext_len += out_len; |
| 386 |
|
| |
| 387 |
42 |
▶ | THROW(EVP_EncryptFinal_ex(self->enc_ctx, (unsigned char *) ciphertext + ciphertext_len, &out_len)); |
Branch: 1 branch
| Branch | True | False | Total |
| THROW(EVP_EncryptFinal_ex(self->enc_ctx, (unsigned char *) ciphertext + ciphertext_len, &out_len)); |
0 |
42 |
42 |
|
| 388 |
|
| |
| 389 |
42 |
| ciphertext_len += out_len; |
| 390 |
|
| |
| 391 |
42 |
| RETVAL = newSVpvn(ciphertext, ciphertext_len); |
| 392 |
|
| #else |
| 393 |
|
| AES_encrypt(plaintext, ciphertext, &self->enc_key); |
| 394 |
|
| RETVAL = newSVpvn((const unsigned char *) ciphertext, size); |
| 395 |
|
| #endif |
| 396 |
|
| /* Cleanup both branches and error case */ |
| 397 |
42 |
| err: |
| 398 |
42 |
▶ | if (ciphertext != NULL) Safefree(ciphertext); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ciphertext != NULL) Safefree(ciphertext); |
42 |
0 |
42 |
|
| 399 |
42 |
▶ | if(error) |
Branch: 1 branch
| Branch | True | False | Total |
| if(error) |
0 |
42 |
42 |
|
| 400 |
0 |
| croak("Unable to Encrypt"); |
| 401 |
|
| } |
| 402 |
|
| OUTPUT: |
| 403 |
|
| RETVAL |
| 404 |
|
| |
| 405 |
|
| SV * |
| 406 |
|
| decrypt(self, data) |
| 407 |
|
| Crypt::OpenSSL::AES self |
| 408 |
|
| SV *data |
| 409 |
|
| CODE: |
| 410 |
|
| { |
| 411 |
|
| #if defined(USE_ITHREADS) && (OPENSSL_VERSION_NUMBER >= 0x00908000L) |
| 412 |
|
| if (self->tid != aTHX) |
| 413 |
|
| croak("Crypt::OpenSSL::AES: decrypt() called from a different " |
| 414 |
|
| "thread than the object was created in -- " |
| 415 |
|
| "EVP_CIPHER_CTX is not thread-safe"); |
| 416 |
|
| #endif |
| 417 |
|
| int error; |
| 418 |
|
| STRLEN size; |
| 419 |
42 |
| unsigned char * ciphertext = (unsigned char *) SvPVbyte(data,size); |
| 420 |
|
| const char * plaintext; |
| 421 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 422 |
42 |
| int out_len = 0; |
| 423 |
42 |
| int plaintext_len = 0; |
| 424 |
42 |
| int block_size = EVP_CIPHER_CTX_block_size(self->dec_ctx); |
| 425 |
|
| #else |
| 426 |
|
| int block_size = AES_BLOCK_SIZE; |
| 427 |
|
| #endif |
| 428 |
42 |
| error = 0; |
| 429 |
42 |
▶ | if ((size % block_size != 0) && self->padding != 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((size % block_size != 0) && self->padding != 1) { |
0 |
42 |
42 |
| if ((size % block_size != 0) && self->padding != 1) { |
0 |
0 |
0 |
|
| 430 |
0 |
| croak("AES: Data size must be multiple of blocksize (%d bytes)", block_size); |
| 431 |
|
| } |
| 432 |
42 |
| Newxc(plaintext, size + block_size, const unsigned char, const char); |
| 433 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 434 |
42 |
▶ | if (1 != EVP_DecryptInit_ex(self->dec_ctx, NULL, NULL, NULL, NULL)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (1 != EVP_DecryptInit_ex(self->dec_ctx, NULL, NULL, NULL, NULL)) { |
0 |
42 |
42 |
|
| 435 |
0 |
| Safefree(plaintext); |
| 436 |
0 |
| croak("EVP_DecryptInit_ex re-init failed"); |
| 437 |
|
| } |
| 438 |
|
| |
| 439 |
42 |
| EVP_CIPHER_CTX_set_padding(self->dec_ctx, self->padding); |
| 440 |
|
| |
| 441 |
42 |
▶ | THROW(EVP_DecryptUpdate(self->dec_ctx, (unsigned char *) plaintext, &out_len, ciphertext, size)); |
Branch: 1 branch
| Branch | True | False | Total |
| THROW(EVP_DecryptUpdate(self->dec_ctx, (unsigned char *) plaintext, &out_len, ciphertext, size)); |
0 |
42 |
42 |
|
| 442 |
|
| |
| 443 |
42 |
| plaintext_len += out_len; |
| 444 |
|
| |
| 445 |
42 |
▶ | THROW(EVP_DecryptFinal_ex(self->dec_ctx, (unsigned char *) plaintext + plaintext_len, &out_len)); |
Branch: 1 branch
| Branch | True | False | Total |
| THROW(EVP_DecryptFinal_ex(self->dec_ctx, (unsigned char *) plaintext + plaintext_len, &out_len)); |
1 |
41 |
42 |
|
| 446 |
|
| |
| 447 |
41 |
| plaintext_len += out_len; |
| 448 |
|
| |
| 449 |
41 |
| RETVAL = newSVpvn(plaintext, plaintext_len); |
| 450 |
|
| #else |
| 451 |
|
| AES_decrypt(ciphertext, plaintext, &self->dec_key); |
| 452 |
|
| RETVAL = newSVpvn((const unsigned char *) plaintext, size); |
| 453 |
|
| #endif |
| 454 |
|
| /* Cleanup both branches and error case */ |
| 455 |
42 |
| err: |
| 456 |
42 |
▶ | if(plaintext != NULL) Safefree(plaintext); |
Branch: 1 branch
| Branch | True | False | Total |
| if(plaintext != NULL) Safefree(plaintext); |
42 |
0 |
42 |
|
| 457 |
42 |
▶ | if(error) |
Branch: 1 branch
| Branch | True | False | Total |
| if(error) |
1 |
41 |
42 |
|
| 458 |
1 |
| croak("Unable to Decrypt"); |
| 459 |
|
| } |
| 460 |
|
| OUTPUT: |
| 461 |
|
| RETVAL |
| 462 |
|
| |
| 463 |
|
| int |
| 464 |
|
| fips_mode() |
| 465 |
|
| CODE: |
| 466 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 467 |
0 |
| RETVAL = EVP_default_properties_is_fips_enabled(NULL); |
| 468 |
|
| #elif OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 469 |
|
| RETVAL = FIPS_mode(); |
| 470 |
|
| #else |
| 471 |
|
| RETVAL = 0; |
| 472 |
|
| #endif |
| 473 |
|
| OUTPUT: |
| 474 |
|
| RETVAL |
| 475 |
|
| |
| 476 |
|
| void |
| 477 |
|
| post_fork_init() |
| 478 |
|
| CODE: |
| 479 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 480 |
0 |
| RAND_poll(); /* re-seed PRNG from OS entropy */ |
| 481 |
|
| #endif |
| 482 |
|
| |
| 483 |
|
| void |
| 484 |
|
| DESTROY(self) |
| 485 |
|
| Crypt::OpenSSL::AES self |
| 486 |
|
| CODE: |
| 487 |
|
| #if defined(USE_ITHREADS) && (OPENSSL_VERSION_NUMBER >= 0x00908000L) |
| 488 |
|
| if (self->tid != aTHX) |
| 489 |
|
| warn("Crypt::OpenSSL::AES: object destroyed in a different thread " |
| 490 |
|
| "than it was created in -- EVP_CIPHER_CTX is not thread-safe"); |
| 491 |
|
| #endif |
| 492 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x00908000L |
| 493 |
54 |
| EVP_CIPHER_CTX_free(self->enc_ctx); |
| 494 |
54 |
| EVP_CIPHER_CTX_free(self->dec_ctx); |
| 495 |
|
| #endif |
| 496 |
54 |
| Safefree(self); |