| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #define NO_XSLOCKS |
| 3 |
|
| |
| 4 |
|
| #include "EXTERN.h" |
| 5 |
|
| #include "perl.h" |
| 6 |
|
| #include "XSUB.h" |
| 7 |
|
| #include "patchlevel.h" |
| 8 |
|
| #include "ppport.h" |
| 9 |
|
| |
| 10 |
|
| #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__HAIKU__) |
| 11 |
|
| #include <sys/types.h> |
| 12 |
|
| #endif |
| 13 |
|
| #include <limits.h> |
| 14 |
|
| #include <stdio.h> |
| 15 |
|
| #include <string.h> |
| 16 |
|
| #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__HAIKU__) |
| 17 |
|
| #include <fcntl.h> |
| 18 |
|
| #include <stdint.h> |
| 19 |
|
| #include <stdlib.h> |
| 20 |
|
| #include <sys/stat.h> |
| 21 |
|
| #include <termios.h> |
| 22 |
|
| #include <unistd.h> |
| 23 |
|
| #elif defined(_WIN32) |
| 24 |
|
| /* windows is completely untested. */ |
| 25 |
|
| #include <windows.h> |
| 26 |
|
| #endif |
| 27 |
|
| #ifndef TCSAFLUSH |
| 28 |
|
| #define TCSAFLUSH 0 |
| 29 |
|
| #endif |
| 30 |
|
| |
| 31 |
|
| #include <sodium.h> |
| 32 |
|
| |
| 33 |
|
| #define CSXS_MIN(a,b) (((a) <= (b)) ? (a) : (b)) |
| 34 |
|
| #define CSXS_MAX(a,b) (((a) >= (b)) ? (a) : (b)) |
| 35 |
|
| |
| 36 |
|
| /* likely needs backing off and preprocessor around supported features. "for |
| 37 |
|
| * now"... */ |
| 38 |
|
| /* 1.0.18: major: 10 minor: 3 */ |
| 39 |
|
| #if (SODIUM_LIBRARY_VERSION_MAJOR < 10U) |
| 40 |
|
| #error "libsodium library is not compatible (version too old)" |
| 41 |
|
| #endif |
| 42 |
|
| #if (SODIUM_LIBRARY_VERSION_MAJOR == 10U) && (SODIUM_LIBRARY_VERSION_MINOR < 3U) |
| 43 |
|
| #error "libsodium library is not compatible (version too old)" |
| 44 |
|
| #endif |
| 45 |
|
| #undef SODIUM_HAS_RISTRETTO255 |
| 46 |
|
| #if (SODIUM_LIBRARY_VERSION_MAJOR > 10U) || \ |
| 47 |
|
| ((SODIUM_LIBRARY_VERSION_MAJOR == 10U) && (SODIUM_LIBRARY_VERSION_MINOR >= 3U)) |
| 48 |
|
| #define SODIUM_HAS_RISTRETTO255 1U |
| 49 |
|
| #endif |
| 50 |
|
| |
| 51 |
|
| /* 1.0.19: major: 26 minor: 1 */ |
| 52 |
|
| #undef SODIUM_HAS_AEGIS |
| 53 |
|
| #if (SODIUM_LIBRARY_VERSION_MAJOR > 26U) || \ |
| 54 |
|
| ((SODIUM_LIBRARY_VERSION_MAJOR == 26U) && (SODIUM_LIBRARY_VERSION_MINOR >= 1U)) |
| 55 |
|
| #define SODIUM_HAS_AEGIS 1U |
| 56 |
|
| #endif |
| 57 |
|
| |
| 58 |
|
| #undef SODIUM_HAS_HKDF |
| 59 |
|
| #if (SODIUM_LIBRARY_VERSION_MAJOR > 26U) || \ |
| 60 |
|
| ((SODIUM_LIBRARY_VERSION_MAJOR == 26U) && (SODIUM_LIBRARY_VERSION_MINOR >= 1U)) |
| 61 |
|
| #define SODIUM_HAS_HKDF 1U |
| 62 |
|
| #endif |
| 63 |
|
| |
| 64 |
|
| /* 1.0.21: major: 26 minor: 3 */ |
| 65 |
|
| #undef SODIUM_HAS_IPCODECS |
| 66 |
|
| #undef SODIUM_HAS_IPCRYPT |
| 67 |
|
| #undef SODIUM_HAS_XOF |
| 68 |
|
| #if (SODIUM_LIBRARY_VERSION_MAJOR > 26U) || \ |
| 69 |
|
| ((SODIUM_LIBRARY_VERSION_MAJOR == 26U) && (SODIUM_LIBRARY_VERSION_MINOR >= 3U)) |
| 70 |
|
| #define SODIUM_HAS_IPCODECS 1U |
| 71 |
|
| #define SODIUM_HAS_IPCRYPT 1U |
| 72 |
|
| #define SODIUM_HAS_XOF 1U |
| 73 |
|
| #endif |
| 74 |
|
| |
| 75 |
|
| /* 1.0.22: major: 26 minor: 4 */ |
| 76 |
|
| #undef SODIUM_HAS_KEM |
| 77 |
|
| #undef SODIUM_HAS_SHA3 |
| 78 |
|
| #if (SODIUM_LIBRARY_VERSION_MAJOR > 26U) || \ |
| 79 |
|
| ((SODIUM_LIBRARY_VERSION_MAJOR == 26U) && (SODIUM_LIBRARY_VERSION_MINOR >= 4U)) |
| 80 |
|
| #define SODIUM_HAS_KEM 1U |
| 81 |
|
| #define SODIUM_HAS_SHA3 1U |
| 82 |
|
| #endif |
| 83 |
|
| |
| 84 |
|
| /* 00000011 */ |
| 85 |
|
| #define PROTMEM_FLAG_MPROTECT_MASK 0x3U |
| 86 |
|
| #define PROTMEM_FLAG_MPROTECT_NOACCESS 0U |
| 87 |
|
| #define PROTMEM_FLAG_MPROTECT_RO 0x1U |
| 88 |
|
| /* WR unused (no sodium_mprotect_writeonly), just for completeness */ |
| 89 |
|
| #define PROTMEM_FLAG_MPROTECT_WR 0x2U |
| 90 |
|
| #define PROTMEM_FLAG_MPROTECT_RW 0x3U |
| 91 |
|
| |
| 92 |
|
| /* 00000100 reserved */ |
| 93 |
|
| |
| 94 |
|
| /* 00011000 */ |
| 95 |
|
| #define PROTMEM_FLAG_MLOCK_MASK 0x24U |
| 96 |
|
| #define PROTMEM_FLAG_MLOCK_STRICT 0U |
| 97 |
|
| #define PROTMEM_FLAG_MLOCK_PERMISSIVE 0x8U |
| 98 |
|
| #define PROTMEM_FLAG_MLOCK_NONE 0x24U |
| 99 |
|
| |
| 100 |
|
| /* 00100000 */ |
| 101 |
|
| #define PROTMEM_FLAG_LOCK_MASK 0x20U |
| 102 |
|
| #define PROTMEM_FLAG_LOCK_LOCKED 0U |
| 103 |
|
| #define PROTMEM_FLAG_LOCK_UNLOCKED 0x20U |
| 104 |
|
| |
| 105 |
|
| /* 01000000 */ |
| 106 |
|
| #define PROTMEM_FLAG_MEMZERO_MASK 0x40U |
| 107 |
|
| #define PROTMEM_FLAG_MEMZERO_ENABLED 0U |
| 108 |
|
| #define PROTMEM_FLAG_MEMZERO_DISABLED 0x40U |
| 109 |
|
| |
| 110 |
|
| /* 10000000 */ |
| 111 |
|
| #define PROTMEM_FLAG_MALLOC_MASK 0x80U |
| 112 |
|
| #define PROTMEM_FLAG_MALLOC_SODIUM 0U |
| 113 |
|
| #define PROTMEM_FLAG_MALLOC_PLAIN 0x80U |
| 114 |
|
| |
| 115 |
|
| #define PROTMEM_FLAG_ALL_DISABLED 0xffffffffU |
| 116 |
|
| #define PROTMEM_FLAG_ALL_ENABLED 0U |
| 117 |
|
| |
| 118 |
|
| #define MEMVAULT_READ_BUFSIZE 4096U |
| 119 |
|
| #define MEMVAULT_WRITE_BUFSIZE 4096U |
| 120 |
|
| |
| 121 |
|
| #define MEMVAULT_CLASS ("Crypt::Sodium::XS::MemVault") |
| 122 |
|
| |
| 123 |
|
| #if IVSIZE < 8 |
| 124 |
|
| #define LESSTHAN64BITINT |
| 125 |
|
| #endif |
| 126 |
|
| |
| 127 |
|
| /* fallback for pre-5.31.4 */ |
| 128 |
|
| #ifndef SvPVbyte_nomg |
| 129 |
|
| #ifdef PERL_STATIC_INLINE |
| 130 |
|
| PERL_STATIC_INLINE char * SvPVbyte_nomg(pTHX_ SV *sv, STRLEN *len) |
| 131 |
|
| #else |
| 132 |
|
| static char * SvPVbyte_nomg(pTHX_ SV *sv, STRLEN *len) |
| 133 |
|
| #endif |
| 134 |
|
| { |
| 135 |
|
| char *buf = SvPV_nomg(sv, *len); |
| 136 |
|
| if (!SvUTF8(sv)) |
| 137 |
|
| return buf; |
| 138 |
|
| if (SvGMAGICAL(sv)) { |
| 139 |
|
| sv = sv_2mortal(newSVpvn(buf, *len)); |
| 140 |
|
| SvUTF8_on(sv); |
| 141 |
|
| } |
| 142 |
|
| return SvPVbyte(sv, *len); |
| 143 |
|
| } |
| 144 |
|
| #define SvPVbyte_nomg(sv, len) SvPVbyte_nomg(aTHX_ (sv), &(len)) |
| 145 |
|
| #endif |
| 146 |
|
| |
| 147 |
|
| |
| 148 |
|
| static U32 g_protmem_default_flags_state = 0; |
| 149 |
|
| static U32 g_protmem_default_flags_memvault = 0; |
| 150 |
|
| static U32 g_protmem_default_flags_key = 0; |
| 151 |
|
| static U32 g_protmem_default_flags_decrypt = 0; |
| 152 |
|
| |
| 153 |
|
| static int has_aes256gcm; |
| 154 |
|
| |
| 155 |
|
| typedef struct { |
| 156 |
|
| void *pm_ptr; |
| 157 |
|
| size_t size; |
| 158 |
|
| unsigned int flags; |
| 159 |
|
| } protmem; |
| 160 |
|
| |
| 161 |
7237 |
| static void protmem_free(pTHX_ protmem *pm) { |
| 162 |
7237 |
▶ | if (pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pm == NULL) |
0 |
7237 |
7237 |
|
| 163 |
0 |
| return; |
| 164 |
7237 |
▶ | if (pm->flags & PROTMEM_FLAG_MALLOC_PLAIN) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pm->flags & PROTMEM_FLAG_MALLOC_PLAIN) { |
14 |
7223 |
7237 |
|
| 165 |
14 |
▶ | if (!(pm->flags & PROTMEM_FLAG_MEMZERO_DISABLED)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(pm->flags & PROTMEM_FLAG_MEMZERO_DISABLED)) |
0 |
14 |
14 |
|
| 166 |
0 |
| sodium_memzero(pm->pm_ptr, pm->size); |
| 167 |
14 |
| safefree(pm->pm_ptr); |
| 168 |
|
| } |
| 169 |
|
| else |
| 170 |
7223 |
| sodium_free(pm->pm_ptr); |
| 171 |
7237 |
| safefree(pm); |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
2584 |
| static protmem * protmem_clone(pTHX_ protmem *cur_pm, size_t new_size) { |
| 175 |
|
| protmem *new_pm; |
| 176 |
|
| |
| 177 |
2584 |
▶ | if (cur_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cur_pm == NULL) |
0 |
2584 |
2584 |
|
| 178 |
0 |
| return NULL; |
| 179 |
2584 |
| new_pm = safemalloc(sizeof(protmem)); |
| 180 |
2584 |
▶ | if (new_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_pm == NULL) |
0 |
2584 |
2584 |
|
| 181 |
0 |
| return NULL; |
| 182 |
2584 |
▶ | if (cur_pm->flags & PROTMEM_FLAG_MALLOC_PLAIN) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cur_pm->flags & PROTMEM_FLAG_MALLOC_PLAIN) |
0 |
2584 |
2584 |
|
| 183 |
0 |
| new_pm->pm_ptr = safemalloc(new_size); |
| 184 |
|
| else |
| 185 |
2584 |
| new_pm->pm_ptr = sodium_malloc(new_size); |
| 186 |
2584 |
▶ | if (new_pm->pm_ptr == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_pm->pm_ptr == NULL) { |
0 |
2584 |
2584 |
|
| 187 |
0 |
| safefree(new_pm); |
| 188 |
0 |
| return NULL; |
| 189 |
|
| } |
| 190 |
2584 |
| new_pm->size = new_size; |
| 191 |
2584 |
| new_pm->flags = cur_pm->flags; |
| 192 |
|
| /* flags is set, but caller must still mpstate_access_release! */ |
| 193 |
2584 |
▶ | if (!(new_pm->flags & PROTMEM_FLAG_MLOCK_PERMISSIVE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(new_pm->flags & PROTMEM_FLAG_MLOCK_PERMISSIVE) |
0 |
2584 |
2584 |
|
| 194 |
0 |
▶ | && !(new_pm->flags & PROTMEM_FLAG_MALLOC_PLAIN)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && !(new_pm->flags & PROTMEM_FLAG_MALLOC_PLAIN)) { |
0 |
0 |
0 |
|
| 195 |
0 |
▶ | if (sodium_mlock(new_pm->pm_ptr, new_size) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sodium_mlock(new_pm->pm_ptr, new_size) != 0) { |
0 |
0 |
0 |
|
| 196 |
0 |
| protmem_free(aTHX_ new_pm); |
| 197 |
0 |
| warn("protmem_clone: mlock failed\n"); |
| 198 |
0 |
| return NULL; |
| 199 |
|
| } |
| 200 |
|
| } |
| 201 |
2584 |
▶ | if (new_pm->flags & PROTMEM_FLAG_MLOCK_NONE |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_pm->flags & PROTMEM_FLAG_MLOCK_NONE |
2013 |
571 |
2584 |
|
| 202 |
2013 |
▶ | && !(new_pm->flags & PROTMEM_FLAG_MALLOC_PLAIN)) |
Branch: 1 branch
| Branch | True | False | Total |
| && !(new_pm->flags & PROTMEM_FLAG_MALLOC_PLAIN)) |
2013 |
0 |
2013 |
|
| 203 |
2013 |
| sodium_munlock(new_pm->pm_ptr, new_size); |
| 204 |
|
| /* ignoring failure */ |
| 205 |
2584 |
| memcpy(new_pm->pm_ptr, cur_pm->pm_ptr, |
| 206 |
2584 |
| new_size > cur_pm->size ? cur_pm->size : new_size); |
| 207 |
|
| |
| 208 |
2584 |
| return new_pm; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
48261 |
| static int protmem_grant(pTHX_ protmem *pm, int flags) { |
| 212 |
|
| int pm_flags; |
| 213 |
48261 |
▶ | if (pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pm == NULL) |
0 |
48261 |
48261 |
|
| 214 |
0 |
| return -1; |
| 215 |
48261 |
| pm_flags = pm->flags; |
| 216 |
48261 |
▶ | if (pm_flags & PROTMEM_FLAG_MALLOC_PLAIN) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pm_flags & PROTMEM_FLAG_MALLOC_PLAIN) |
13 |
48248 |
48261 |
|
| 217 |
13 |
| return 0; |
| 218 |
48248 |
| pm_flags &= PROTMEM_FLAG_MPROTECT_MASK; |
| 219 |
48248 |
| flags &= PROTMEM_FLAG_MPROTECT_MASK; |
| 220 |
48248 |
▶ | if (flags <= pm_flags) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags <= pm_flags) |
0 |
48248 |
48248 |
|
| 221 |
0 |
| return 0; |
| 222 |
48248 |
| switch(flags) { |
| 223 |
30784 |
| case PROTMEM_FLAG_MPROTECT_RW: |
| 224 |
30784 |
| return sodium_mprotect_readwrite(pm->pm_ptr); |
| 225 |
17464 |
| case PROTMEM_FLAG_MPROTECT_RO: |
| 226 |
17464 |
| return sodium_mprotect_readonly(pm->pm_ptr); |
| 227 |
|
| } |
| 228 |
0 |
| return -1; |
| 229 |
|
| } |
| 230 |
|
| |
| 231 |
55479 |
| static int protmem_release(pTHX_ protmem *pm, int flags) { |
| 232 |
|
| int pm_flags; |
| 233 |
55479 |
▶ | if (pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pm == NULL) |
0 |
55479 |
55479 |
|
| 234 |
0 |
| return -1; |
| 235 |
55479 |
| pm_flags = pm->flags; |
| 236 |
55479 |
▶ | if (pm_flags & PROTMEM_FLAG_MALLOC_PLAIN) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pm_flags & PROTMEM_FLAG_MALLOC_PLAIN) |
27 |
55452 |
55479 |
|
| 237 |
27 |
| return 0; |
| 238 |
55452 |
| pm_flags &= PROTMEM_FLAG_MPROTECT_MASK; |
| 239 |
55452 |
| flags &= PROTMEM_FLAG_MPROTECT_MASK; |
| 240 |
55452 |
▶ | if (flags <= pm_flags) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags <= pm_flags) |
0 |
55452 |
55452 |
|
| 241 |
0 |
| return 0; |
| 242 |
55452 |
| switch(pm_flags) { |
| 243 |
0 |
| case PROTMEM_FLAG_MPROTECT_RO: |
| 244 |
0 |
| return sodium_mprotect_readonly(pm->pm_ptr); |
| 245 |
55452 |
| case PROTMEM_FLAG_MPROTECT_NOACCESS: |
| 246 |
55452 |
| return sodium_mprotect_noaccess(pm->pm_ptr); |
| 247 |
|
| } |
| 248 |
0 |
| return -1; |
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
|
| #if defined(USE_ITHREADS) && defined(MGf_DUP) |
| 252 |
|
| STATIC int dup_protmem(pTHX_ MAGIC *mg, CLONE_PARAMS *params) { |
| 253 |
|
| protmem *new_pm; |
| 254 |
|
| protmem *cur_pm; |
| 255 |
|
| PERL_UNUSED_VAR(params); |
| 256 |
|
| cur_pm = (protmem *)mg->mg_ptr; |
| 257 |
|
| |
| 258 |
|
| if (protmem_grant(aTHX_ cur_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
| 259 |
|
| croak("Failed to grant protmem RO"); |
| 260 |
|
| |
| 261 |
|
| new_pm = protmem_clone(aTHX_ cur_pm, cur_pm->size); |
| 262 |
|
| if (new_pm == NULL) |
| 263 |
|
| croak("Failed to clone protmem"); |
| 264 |
|
| |
| 265 |
|
| if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
| 266 |
|
| protmem_free(aTHX_ new_pm); |
| 267 |
|
| croak("Failed to release new protmem RW"); |
| 268 |
|
| } |
| 269 |
|
| |
| 270 |
|
| if (protmem_release(aTHX_ cur_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
| 271 |
|
| protmem_free(aTHX_ new_pm); |
| 272 |
|
| croak("Failed to release protmem RO"); |
| 273 |
|
| } |
| 274 |
|
| |
| 275 |
|
| mg->mg_ptr = (char *)new_pm; |
| 276 |
|
| return 0; |
| 277 |
|
| } |
| 278 |
|
| #endif |
| 279 |
|
| STATIC MGVTBL vtbl_protmem = { |
| 280 |
|
| NULL, /* get */ |
| 281 |
|
| NULL, /* set */ |
| 282 |
|
| NULL, /* len */ |
| 283 |
|
| NULL, /* clear */ |
| 284 |
|
| NULL, /* free */ |
| 285 |
|
| #ifdef MGf_COPY |
| 286 |
|
| NULL, /* copy */ |
| 287 |
|
| #endif |
| 288 |
|
| #ifdef MGf_DUP |
| 289 |
|
| # ifdef USE_ITHREADS |
| 290 |
|
| dup_protmem, |
| 291 |
|
| # else |
| 292 |
|
| NULL, /* dup */ |
| 293 |
|
| # endif |
| 294 |
|
| #endif |
| 295 |
|
| #ifdef MGf_LOCAL |
| 296 |
|
| NULL /* local */ |
| 297 |
|
| #endif |
| 298 |
|
| }; |
| 299 |
|
| |
| 300 |
|
| /* returns NULL on failure */ |
| 301 |
4657 |
| static protmem * protmem_init(pTHX_ STRLEN size, int flags) { |
| 302 |
|
| protmem *pm; |
| 303 |
|
| |
| 304 |
4657 |
| pm = safemalloc(sizeof(protmem)); |
| 305 |
4657 |
▶ | if (pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (pm == NULL) |
0 |
4657 |
4657 |
|
| 306 |
0 |
| return NULL; |
| 307 |
|
| |
| 308 |
4657 |
| pm->flags = flags; |
| 309 |
4657 |
▶ | if (flags & PROTMEM_FLAG_MALLOC_PLAIN) |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & PROTMEM_FLAG_MALLOC_PLAIN) |
14 |
4643 |
4657 |
|
| 310 |
14 |
| pm->pm_ptr = safemalloc(size); |
| 311 |
|
| else |
| 312 |
4643 |
| pm->pm_ptr = sodium_malloc(size); |
| 313 |
4657 |
▶ | if (pm->pm_ptr == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pm->pm_ptr == NULL) { |
0 |
4657 |
4657 |
|
| 314 |
0 |
| safefree(pm); |
| 315 |
0 |
| return NULL; |
| 316 |
|
| } |
| 317 |
4657 |
| pm->size = size; |
| 318 |
4657 |
▶ | if (!(flags & PROTMEM_FLAG_MLOCK_PERMISSIVE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(flags & PROTMEM_FLAG_MLOCK_PERMISSIVE) |
20 |
4637 |
4657 |
|
| 319 |
20 |
▶ | && !(flags & PROTMEM_FLAG_MALLOC_PLAIN)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && !(flags & PROTMEM_FLAG_MALLOC_PLAIN)) { |
20 |
0 |
20 |
|
| 320 |
20 |
▶ | if (sodium_mlock(pm->pm_ptr, size) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sodium_mlock(pm->pm_ptr, size) != 0) { |
0 |
20 |
20 |
|
| 321 |
0 |
| warn("protmem_init: mlock failed.\n"); |
| 322 |
0 |
| protmem_free(aTHX_ pm); |
| 323 |
0 |
| return NULL; |
| 324 |
|
| } |
| 325 |
|
| } |
| 326 |
4657 |
▶ | if (flags & PROTMEM_FLAG_MLOCK_NONE && !(flags & PROTMEM_FLAG_MALLOC_PLAIN)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (flags & PROTMEM_FLAG_MLOCK_NONE && !(flags & PROTMEM_FLAG_MALLOC_PLAIN)) |
97 |
4560 |
4657 |
| if (flags & PROTMEM_FLAG_MLOCK_NONE && !(flags & PROTMEM_FLAG_MALLOC_PLAIN)) |
83 |
14 |
97 |
|
| 327 |
83 |
| sodium_munlock(pm->pm_ptr, size); |
| 328 |
|
| /* ignoring failure */ |
| 329 |
|
| |
| 330 |
4657 |
| return pm; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
|
| /* NB: croaks on failure */ |
| 334 |
59177 |
| static protmem * protmem_get(pTHX_ SV *sv, const char *sv_pkg) { |
| 335 |
|
| MAGIC *mg; |
| 336 |
|
| |
| 337 |
59177 |
▶ | if (!sv_derived_from(sv, sv_pkg)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_derived_from(sv, sv_pkg)) |
0 |
59177 |
59177 |
|
| 338 |
0 |
| croak("Not a reference to a %s object", sv_pkg); |
| 339 |
|
| |
| 340 |
59177 |
▶ | for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(SvRV(sv)); mg; mg = mg->mg_moremagic) |
59177 |
0 |
59177 |
|
| 341 |
59177 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_protmem) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_protmem) |
59177 |
0 |
59177 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vtbl_protmem) |
59177 |
0 |
59177 |
|
| 342 |
59177 |
| return (protmem *)mg->mg_ptr; |
| 343 |
|
| |
| 344 |
0 |
| croak("Failed to get %s pointer", sv_pkg); |
| 345 |
|
| return NULL; |
| 346 |
|
| } |
| 347 |
|
| |
| 348 |
7134 |
| static SV * protmem_to_sv(pTHX_ protmem *pm, const char *sv_pkg) { |
| 349 |
7134 |
| SV *sv = newSV(0); |
| 350 |
7134 |
| SV *obj = newRV_noinc(sv); |
| 351 |
|
| #ifdef USE_ITHREADS |
| 352 |
|
| MAGIC *mg; |
| 353 |
|
| #endif |
| 354 |
|
| |
| 355 |
7134 |
| sv_bless(obj, gv_stashpv(sv_pkg, GV_ADD)); |
| 356 |
|
| |
| 357 |
|
| #ifdef USE_ITHREADS |
| 358 |
|
| mg = |
| 359 |
|
| #endif |
| 360 |
7134 |
| sv_magicext(sv, NULL, PERL_MAGIC_ext, &vtbl_protmem, (const char *)pm, 0); |
| 361 |
|
| |
| 362 |
|
| #if defined(USE_ITHREADS) && defined(MGf_DUP) |
| 363 |
|
| mg->mg_flags |= MGf_DUP; |
| 364 |
|
| #endif |
| 365 |
|
| |
| 366 |
7134 |
| return obj; |
| 367 |
|
| } |
| 368 |
|
| |
| 369 |
|
| /* NB: croaks on failure */ |
| 370 |
|
| /* XXX: does this really need to be able to clone into a different package? */ |
| 371 |
2568 |
| static SV * protmem_clone_sv(pTHX_ SV *pm_sv, const char *sv_pkg) { |
| 372 |
|
| protmem *new_pm; |
| 373 |
2568 |
| protmem *cur_pm = protmem_get(aTHX_ pm_sv, sv_pkg); |
| 374 |
|
| |
| 375 |
2568 |
▶ | if (protmem_grant(aTHX_ cur_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_grant(aTHX_ cur_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
2568 |
2568 |
|
| 376 |
0 |
| croak("Failed to grant protmem RO"); |
| 377 |
|
| |
| 378 |
2568 |
| new_pm = protmem_clone(aTHX_ cur_pm, cur_pm->size); |
| 379 |
2568 |
▶ | if (new_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_pm == NULL) |
0 |
2568 |
2568 |
|
| 380 |
0 |
| croak("Failed to clone protmem"); |
| 381 |
|
| |
| 382 |
2568 |
▶ | if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ new_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
2568 |
2568 |
|
| 383 |
0 |
| protmem_free(aTHX_ new_pm); |
| 384 |
0 |
| protmem_release(aTHX_ cur_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 385 |
0 |
| croak("Failed to release new protmem RW"); |
| 386 |
|
| } |
| 387 |
|
| |
| 388 |
2568 |
▶ | if (protmem_release(aTHX_ cur_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ cur_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
2568 |
2568 |
|
| 389 |
0 |
| protmem_free(aTHX_ new_pm); |
| 390 |
0 |
| croak("Failed to release protmem RO"); |
| 391 |
|
| } |
| 392 |
|
| |
| 393 |
2568 |
| return protmem_to_sv(aTHX_ new_pm, sv_pkg); |
| 394 |
|
| } |
| 395 |
|
| |
| 396 |
38 |
| static SV * nonce_generate(pTHX_ STRLEN out_len, SV *base) { |
| 397 |
|
| SV *ret; |
| 398 |
|
| STRLEN base_len; |
| 399 |
|
| unsigned char *base_buf; |
| 400 |
|
| unsigned char *nonce_buf; |
| 401 |
|
| |
| 402 |
38 |
| Newxz(nonce_buf, out_len + 1, unsigned char); |
| 403 |
38 |
▶ | if (nonce_buf == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_buf == NULL) |
0 |
38 |
38 |
|
| 404 |
0 |
| croak("Failed to allocate memory"); |
| 405 |
|
| |
| 406 |
38 |
| SvGETMAGIC(base); |
| 407 |
38 |
▶ | if (SvOK(base)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(base)) { |
4 |
34 |
38 |
|
| 408 |
4 |
| base_buf = (unsigned char *)SvPVbyte_nomg(base, base_len); |
| 409 |
4 |
▶ | if (base_len > out_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base_len > out_len) { |
0 |
4 |
4 |
|
| 410 |
0 |
| Safefree(nonce_buf); |
| 411 |
0 |
| croak("Invalid nonce length (too long): %lu", base_len); |
| 412 |
|
| } |
| 413 |
|
| |
| 414 |
4 |
| memcpy(nonce_buf, base_buf, base_len); |
| 415 |
|
| } |
| 416 |
|
| else |
| 417 |
34 |
| randombytes_buf(nonce_buf, out_len); |
| 418 |
|
| |
| 419 |
38 |
| ret = newSV(0); |
| 420 |
38 |
| sv_usepvn_flags(ret, (char *)nonce_buf, out_len, SV_HAS_TRAILING_NUL); |
| 421 |
|
| |
| 422 |
38 |
| return ret; |
| 423 |
|
| } |
| 424 |
|
| |
| 425 |
|
| /* NB: croaks on failure */ |
| 426 |
578 |
| static SV * sv_keygen(pTHX_ STRLEN size, SV * flags) { |
| 427 |
|
| protmem *key_pm; |
| 428 |
578 |
| unsigned int mv_flags = g_protmem_default_flags_key; |
| 429 |
|
| |
| 430 |
578 |
| SvGETMAGIC(flags); |
| 431 |
578 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
578 |
578 |
|
| 432 |
0 |
| mv_flags = SvUV_nomg(flags); |
| 433 |
|
| |
| 434 |
578 |
| key_pm = protmem_init(aTHX_ size, mv_flags); |
| 435 |
578 |
▶ | if (key_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_pm == NULL) |
0 |
578 |
578 |
|
| 436 |
0 |
| croak("sv_keygen: Failed to allocate protmem"); |
| 437 |
|
| |
| 438 |
578 |
| randombytes_buf(key_pm->pm_ptr, key_pm->size); |
| 439 |
|
| |
| 440 |
578 |
▶ | if (protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
578 |
578 |
|
| 441 |
0 |
| protmem_free(aTHX_ key_pm); |
| 442 |
0 |
| croak("sv_keygen: Failed to release key protmem RW"); |
| 443 |
|
| } |
| 444 |
|
| |
| 445 |
578 |
| return protmem_to_sv(aTHX_ key_pm, MEMVAULT_CLASS); |
| 446 |
|
| } |
| 447 |
|
| |
| 448 |
|
| =for TODO |
| 449 |
|
| |
| 450 |
|
| there is a metric boatload (or two) of error handling in this code that needs |
| 451 |
|
| cleaning up. the whole thing could do with a re-factor already. very much |
| 452 |
|
| duplicated long-form code that could be abstracted (CAREFULLY). might not be |
| 453 |
|
| worth the added complexity. this'll be a beast to maintain as-is, though. |
| 454 |
|
| |
| 455 |
|
| also necessary all over the place is handling older versions of libsodium. |
| 456 |
|
| there are version definitions usable from pre-processor that should be used to |
| 457 |
|
| protect stuff only in the newer versions, and throw otherwise. |
| 458 |
|
| |
| 459 |
|
| also also, should be keeping track of whether the used libsodium is a "minimal" |
| 460 |
|
| build. availablility of a number of algorithms and such are dependent on being |
| 461 |
|
| a not-minimal build and should be guarded. |
| 462 |
|
| |
| 463 |
|
| =cut |
| 464 |
|
| |
| 465 |
|
| MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS |
| 466 |
|
| |
| 467 |
|
| BOOT: |
| 468 |
26 |
▶ | if (sodium_init() != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sodium_init() != 0) |
0 |
26 |
26 |
|
| 469 |
0 |
| croak("Failed to initialze library"); |
| 470 |
26 |
| has_aes256gcm = crypto_aead_aes256gcm_is_available(); |
| 471 |
|
| |
| 472 |
|
| PROTOTYPES: ENABLE |
| 473 |
|
| |
| 474 |
|
| void _define_constants() |
| 475 |
|
| PREINIT: |
| 476 |
26 |
| HV *stash = gv_stashpv("Crypt::Sodium::XS", 0); |
| 477 |
|
| |
| 478 |
|
| PPCODE: |
| 479 |
26 |
| newCONSTSUB(stash, "SODIUM_VERSION_STRING", newSVpvs(SODIUM_VERSION_STRING)); |
| 480 |
26 |
| newCONSTSUB(stash, "SODIUM_LIBRARY_VERSION_MAJOR", |
| 481 |
|
| newSVuv(SODIUM_LIBRARY_VERSION_MAJOR)); |
| 482 |
26 |
| newCONSTSUB(stash, "SODIUM_LIBRARY_VERSION_MINOR", |
| 483 |
|
| newSVuv(SODIUM_LIBRARY_VERSION_MINOR)); |
| 484 |
26 |
| XSRETURN_YES; |
| 485 |
|
| |
| 486 |
|
| const char * |
| 487 |
|
| sodium_version_string() |
| 488 |
|
| |
| 489 |
|
| INCLUDE: xs/base64.xs |
| 490 |
|
| |
| 491 |
|
| INCLUDE: xs/util.xs |
| 492 |
|
| |
| 493 |
|
| INCLUDE: xs/protmem.xs |
| 494 |
|
| |
| 495 |
|
| INCLUDE: xs/memvault.xs |
| 496 |
|
| |
| 497 |
|
| INCLUDE: xs/core.xs |
| 498 |
|
| |
| 499 |
|
| INCLUDE: xs/curve25519.xs |
| 500 |
|
| |
| 501 |
|
| INCLUDE: xs/kx.xs |
| 502 |
|
| |
| 503 |
|
| INCLUDE: xs/kdf.xs |
| 504 |
|
| |
| 505 |
|
| INCLUDE: xs/hkdf.xs |
| 506 |
|
| |
| 507 |
|
| INCLUDE: xs/kem.xs |
| 508 |
|
| |
| 509 |
|
| INCLUDE: xs/secretbox.xs |
| 510 |
|
| |
| 511 |
|
| INCLUDE: xs/box.xs |
| 512 |
|
| |
| 513 |
|
| INCLUDE: xs/sign.xs |
| 514 |
|
| |
| 515 |
|
| INCLUDE: xs/secretstream.xs |
| 516 |
|
| |
| 517 |
|
| INCLUDE: xs/aead.xs |
| 518 |
|
| |
| 519 |
|
| INCLUDE: xs/stream.xs |
| 520 |
|
| |
| 521 |
|
| INCLUDE: xs/shorthash.xs |
| 522 |
|
| |
| 523 |
|
| INCLUDE: xs/generichash.xs |
| 524 |
|
| |
| 525 |
|
| INCLUDE: xs/pwhash.xs |
| 526 |
|
| |
| 527 |
|
| INCLUDE: xs/hash.xs |
| 528 |
|
| |
| 529 |
|
| INCLUDE: xs/auth.xs |
| 530 |
|
| |
| 531 |
|
| INCLUDE: xs/onetimeauth.xs |
| 532 |
|
| |
| 533 |
|
| INCLUDE: xs/scalarmult.xs |
| 534 |
|
| |
| 535 |
|
| INCLUDE: xs/ipcrypt.xs |
| 536 |
|
| |
| 537 |
|
| INCLUDE: xs/xof.xs |