| 1 |
|
| MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::curve25519 |
| 2 |
|
| |
| 3 |
|
| void |
| 4 |
|
| _define_constants() |
| 5 |
|
| PREINIT: |
| 6 |
1 |
| HV *stash = gv_stashpv("Crypt::Sodium::XS::curve25519", 0); |
| 7 |
|
| |
| 8 |
|
| PPCODE: |
| 9 |
1 |
| newCONSTSUB(stash, "core_ed25519_BYTES", |
| 10 |
|
| newSVuv(crypto_core_ed25519_BYTES)); |
| 11 |
1 |
| newCONSTSUB(stash, "core_ed25519_HASHBYTES", |
| 12 |
|
| newSVuv(crypto_core_ed25519_HASHBYTES)); |
| 13 |
1 |
| newCONSTSUB(stash, "core_ed25519_NONREDUCEDSCALARBYTES", |
| 14 |
|
| newSVuv(crypto_core_ed25519_NONREDUCEDSCALARBYTES)); |
| 15 |
1 |
| newCONSTSUB(stash, "core_ed25519_SCALARBYTES", |
| 16 |
|
| newSVuv(crypto_core_ed25519_SCALARBYTES)); |
| 17 |
1 |
| newCONSTSUB(stash, "core_ed25519_UNIFORMBYTES", |
| 18 |
|
| newSVuv(crypto_core_ed25519_UNIFORMBYTES)); |
| 19 |
|
| #ifdef SODIUM_HAS_RISTRETTO255 |
| 20 |
1 |
| newCONSTSUB(stash, "core_ristretto255_BYTES", |
| 21 |
|
| newSVuv(crypto_core_ristretto255_BYTES)); |
| 22 |
1 |
| newCONSTSUB(stash, "core_ristretto255_HASHBYTES", |
| 23 |
|
| newSVuv(crypto_core_ristretto255_HASHBYTES)); |
| 24 |
1 |
| newCONSTSUB(stash, "core_ristretto255_NONREDUCEDSCALARBYTES", |
| 25 |
|
| newSVuv(crypto_core_ristretto255_NONREDUCEDSCALARBYTES)); |
| 26 |
1 |
| newCONSTSUB(stash, "core_ristretto255_SCALARBYTES", |
| 27 |
|
| newSVuv(crypto_core_ristretto255_SCALARBYTES)); |
| 28 |
1 |
| newCONSTSUB(stash, "core_ristretto255_available", &PL_sv_yes); |
| 29 |
|
| #else |
| 30 |
|
| newCONSTSUB(stash, "core_ristretto255_available", &PL_sv_no); |
| 31 |
|
| #endif |
| 32 |
|
| |
| 33 |
|
| SV * core_ed25519_add(SV * p, SV * q) |
| 34 |
|
| |
| 35 |
|
| ALIAS: |
| 36 |
|
| core_ed25519_sub = 1 |
| 37 |
|
| core_ristretto255_add = 2 |
| 38 |
|
| core_ristretto255_sub = 3 |
| 39 |
|
| |
| 40 |
|
| PREINIT: |
| 41 |
4 |
▶ | protmem *p_pm = NULL, *q_pm = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| protmem *p_pm = NULL, *q_pm = NULL; |
2 |
2 |
4 |
|
| 42 |
|
| int ret; |
| 43 |
|
| unsigned char *a_buf, *p_buf, *q_buf; |
| 44 |
|
| STRLEN a_len, p_len, p_req_len, q_len, q_req_len; |
| 45 |
|
| |
| 46 |
|
| CODE: |
| 47 |
4 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
2 |
2 |
4 |
|
| 48 |
|
| /* case 1 is same as default */ |
| 49 |
2 |
| case 2: |
| 50 |
|
| /* fallthrough */ |
| 51 |
|
| case 3: |
| 52 |
2 |
| a_len = crypto_core_ristretto255_BYTES; |
| 53 |
2 |
| p_req_len = crypto_core_ristretto255_BYTES; |
| 54 |
2 |
| q_req_len = crypto_core_ristretto255_BYTES; |
| 55 |
2 |
| break; |
| 56 |
2 |
| default: |
| 57 |
2 |
| a_len = crypto_core_ed25519_BYTES; |
| 58 |
2 |
| p_req_len = crypto_core_ed25519_BYTES; |
| 59 |
2 |
| q_req_len = crypto_core_ed25519_BYTES; |
| 60 |
|
| } |
| 61 |
4 |
▶ | if (sv_derived_from(p, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(p, MEMVAULT_CLASS)) { |
2 |
2 |
4 |
|
| 62 |
2 |
| p_pm = protmem_get(aTHX_ p, MEMVAULT_CLASS); |
| 63 |
2 |
| p_buf = p_pm->pm_ptr; |
| 64 |
2 |
| p_len = p_pm->size; |
| 65 |
|
| } |
| 66 |
|
| else |
| 67 |
2 |
| p_buf = (unsigned char *)SvPVbyte(p, p_len); |
| 68 |
4 |
▶ | if (p_len != p_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p_len != p_req_len) |
0 |
4 |
4 |
|
| 69 |
0 |
| croak("ed25519|ristretto255_add|sub: Invalid p length"); |
| 70 |
|
| |
| 71 |
4 |
▶ | if (sv_derived_from(q, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(q, MEMVAULT_CLASS)) { |
2 |
2 |
4 |
|
| 72 |
2 |
| q_pm = protmem_get(aTHX_ q, MEMVAULT_CLASS); |
| 73 |
2 |
| q_buf = q_pm->pm_ptr; |
| 74 |
2 |
| q_len = q_pm->size; |
| 75 |
|
| } |
| 76 |
|
| else |
| 77 |
2 |
| q_buf = (unsigned char *)SvPVbyte(q, q_len); |
| 78 |
4 |
▶ | if (q_len != q_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (q_len != q_req_len) |
0 |
4 |
4 |
|
| 79 |
0 |
| croak("ed25519|ristretto255_add|sub: Invalid q length"); |
| 80 |
|
| |
| 81 |
|
| /* ok to not use a memvault for output? */ |
| 82 |
4 |
| Newx(a_buf, a_len + 1, unsigned char); |
| 83 |
4 |
▶ | if (a_buf == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a_buf == NULL) |
0 |
4 |
4 |
|
| 84 |
0 |
| croak("ed25519|ristretto255_add|sub: Failed to allocate memory"); |
| 85 |
4 |
| a_buf[a_len] = '\0'; |
| 86 |
|
| |
| 87 |
4 |
▶ | if (p_pm && protmem_grant(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p_pm && protmem_grant(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
2 |
2 |
4 |
| if (p_pm && protmem_grant(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
2 |
2 |
|
| 88 |
0 |
| Safefree(a_buf); |
| 89 |
0 |
| croak("ed25519|ristretto255_add|sub: Failed to grant p protmem RO"); |
| 90 |
|
| } |
| 91 |
4 |
▶ | if (q_pm && protmem_grant(aTHX_ q_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q_pm && protmem_grant(aTHX_ q_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
2 |
2 |
4 |
| if (q_pm && protmem_grant(aTHX_ q_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
2 |
2 |
|
| 92 |
0 |
| protmem_release(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 93 |
0 |
| Safefree(a_buf); |
| 94 |
0 |
| croak("ed25519|ristretto255_add|sub: Failed to grant q protmem RO"); |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
4 |
| switch(ix) { |
| 98 |
0 |
| case 1: |
| 99 |
0 |
| ret = crypto_core_ed25519_sub(a_buf, p_buf, q_buf); |
| 100 |
0 |
| break; |
| 101 |
2 |
| case 2: |
| 102 |
2 |
| ret = crypto_core_ristretto255_add(a_buf, p_buf, q_buf); |
| 103 |
2 |
| break; |
| 104 |
0 |
| case 3: |
| 105 |
0 |
| ret = crypto_core_ristretto255_sub(a_buf, p_buf, q_buf); |
| 106 |
0 |
| break; |
| 107 |
2 |
| default: |
| 108 |
2 |
| ret = crypto_core_ed25519_add(a_buf, p_buf, q_buf); |
| 109 |
|
| } |
| 110 |
|
| |
| 111 |
4 |
▶ | if (q_pm && protmem_release(aTHX_ q_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q_pm && protmem_release(aTHX_ q_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
2 |
2 |
4 |
| if (q_pm && protmem_release(aTHX_ q_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
2 |
2 |
|
| 112 |
0 |
| protmem_release(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 113 |
0 |
| Safefree(a_buf); |
| 114 |
0 |
| croak("ed25519|ristretto255_add|sub: Failed to release q protmem RO"); |
| 115 |
|
| } |
| 116 |
4 |
▶ | if (p_pm && protmem_release(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p_pm && protmem_release(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
2 |
2 |
4 |
| if (p_pm && protmem_release(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
2 |
2 |
|
| 117 |
0 |
| Safefree(a_buf); |
| 118 |
0 |
| croak("ed25519|ristretto255_add|sub: Failed to release p protmem RO"); |
| 119 |
|
| } |
| 120 |
4 |
▶ | if (ret != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret != 0) { |
0 |
4 |
4 |
|
| 121 |
0 |
| Safefree(a_buf); |
| 122 |
0 |
| croak("ed25519|ristretto255_add|sub: Failed to add or sub: p or q were not valid points"); |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
4 |
| RETVAL = newSV(0); |
| 126 |
4 |
| sv_usepvn_flags(RETVAL, (char *)a_buf, a_len, SV_HAS_TRAILING_NUL); |
| 127 |
|
| |
| 128 |
|
| OUTPUT: |
| 129 |
|
| RETVAL |
| 130 |
|
| |
| 131 |
|
| SV * core_ed25519_from_uniform(SV * r) |
| 132 |
|
| |
| 133 |
|
| ALIAS: |
| 134 |
|
| core_ristretto255_from_hash = 1 |
| 135 |
|
| |
| 136 |
|
| PREINIT: |
| 137 |
|
| STRLEN p_len, r_len, r_req_len; |
| 138 |
|
| unsigned char *p_buf, *r_buf; |
| 139 |
|
| |
| 140 |
|
| CODE: |
| 141 |
0 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
0 |
0 |
0 |
|
| 142 |
0 |
| case 1: |
| 143 |
0 |
| p_len = crypto_core_ristretto255_BYTES; |
| 144 |
0 |
| r_req_len = crypto_core_ristretto255_BYTES; |
| 145 |
0 |
| break; |
| 146 |
0 |
| default: |
| 147 |
0 |
| p_len = crypto_core_ed25519_BYTES; |
| 148 |
0 |
| r_req_len = crypto_core_ed25519_BYTES; |
| 149 |
|
| } |
| 150 |
0 |
| r_buf = (unsigned char *)SvPVbyte(r, r_len); |
| 151 |
0 |
▶ | if (r_len != r_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r_len != r_req_len) |
0 |
0 |
0 |
|
| 152 |
0 |
| croak("ed25519_from_uniform: invalid vector length"); |
| 153 |
|
| |
| 154 |
0 |
| Newx(p_buf, p_len + 1, unsigned char); |
| 155 |
0 |
▶ | if (p_buf == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p_buf == NULL) |
0 |
0 |
0 |
|
| 156 |
0 |
| croak("ed25519_from_uniform: Failed to allocate memory"); |
| 157 |
0 |
| p_buf[p_len] = '\0'; |
| 158 |
|
| |
| 159 |
0 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
0 |
0 |
0 |
|
| 160 |
0 |
| case 1: |
| 161 |
0 |
| crypto_core_ristretto255_from_hash(p_buf, r_buf); |
| 162 |
0 |
| break; |
| 163 |
0 |
| default: |
| 164 |
0 |
| crypto_core_ed25519_from_uniform(p_buf, r_buf); |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
0 |
| RETVAL = newSV(0); |
| 168 |
0 |
| sv_usepvn_flags(RETVAL, (char *)p_buf, p_len, SV_HAS_TRAILING_NUL); |
| 169 |
|
| |
| 170 |
|
| OUTPUT: |
| 171 |
|
| RETVAL |
| 172 |
|
| |
| 173 |
|
| SV * core_ed25519_is_valid_point(SV * p) |
| 174 |
|
| |
| 175 |
|
| ALIAS: |
| 176 |
|
| core_ristretto255_is_valid_point = 1 |
| 177 |
|
| |
| 178 |
|
| PREINIT: |
| 179 |
2 |
▶ | protmem *p_pm = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| protmem *p_pm = NULL; |
1 |
1 |
2 |
|
| 180 |
|
| int ret; |
| 181 |
|
| unsigned char *p_buf; |
| 182 |
|
| STRLEN p_len, p_req_len; |
| 183 |
|
| |
| 184 |
|
| PPCODE: |
| 185 |
|
| PERL_UNUSED_VAR(RETVAL); |
| 186 |
2 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
1 |
1 |
2 |
|
| 187 |
1 |
| case 1: |
| 188 |
1 |
| p_req_len = crypto_core_ristretto255_BYTES; |
| 189 |
1 |
| break; |
| 190 |
1 |
| default: |
| 191 |
1 |
| p_req_len = crypto_core_ed25519_BYTES; |
| 192 |
|
| } |
| 193 |
2 |
▶ | if (sv_derived_from(p, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(p, MEMVAULT_CLASS)) { |
0 |
2 |
2 |
|
| 194 |
0 |
| p_pm = protmem_get(aTHX_ p, MEMVAULT_CLASS); |
| 195 |
0 |
| p_buf = p_pm->pm_ptr; |
| 196 |
0 |
| p_len = p_pm->size; |
| 197 |
|
| } |
| 198 |
|
| else |
| 199 |
2 |
| p_buf = (unsigned char *)SvPVbyte(p, p_len); |
| 200 |
2 |
▶ | if (p_len != p_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p_len != p_req_len) |
0 |
2 |
2 |
|
| 201 |
0 |
| croak("ed25519_is_valid_point: Invalid point length"); |
| 202 |
|
| |
| 203 |
2 |
▶ | if (p_pm && protmem_grant(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (p_pm && protmem_grant(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
2 |
2 |
| if (p_pm && protmem_grant(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
0 |
0 |
|
| 204 |
0 |
| croak("ed25519_is_valid_point: Failed to grant protmem RO"); |
| 205 |
|
| |
| 206 |
2 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
1 |
1 |
2 |
|
| 207 |
1 |
| case 1: |
| 208 |
1 |
| ret = crypto_core_ristretto255_is_valid_point(p_buf); |
| 209 |
1 |
| break; |
| 210 |
1 |
| default: |
| 211 |
1 |
| ret = crypto_core_ed25519_is_valid_point(p_buf); |
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
2 |
▶ | if (p_pm && protmem_release(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (p_pm && protmem_release(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
2 |
2 |
| if (p_pm && protmem_release(aTHX_ p_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
0 |
0 |
|
| 215 |
0 |
| croak("ed25519_is_valid_point: Failed to release protmem RO"); |
| 216 |
|
| |
| 217 |
2 |
▶ | if (ret == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 1) |
2 |
0 |
2 |
|
| 218 |
2 |
| XSRETURN_YES; |
| 219 |
0 |
| XSRETURN_NO; |
| 220 |
|
| |
| 221 |
|
| SV * core_ed25519_random() |
| 222 |
|
| |
| 223 |
|
| ALIAS: |
| 224 |
|
| core_ristretto255_random = 1 |
| 225 |
|
| |
| 226 |
|
| PREINIT: |
| 227 |
|
| unsigned char *p_buf; |
| 228 |
|
| STRLEN p_len; |
| 229 |
|
| |
| 230 |
|
| CODE: |
| 231 |
2 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
1 |
1 |
2 |
|
| 232 |
1 |
| case 1: |
| 233 |
1 |
| p_len = crypto_core_ristretto255_BYTES; |
| 234 |
1 |
| break; |
| 235 |
1 |
| default: |
| 236 |
1 |
| p_len = crypto_core_ed25519_BYTES; |
| 237 |
|
| } |
| 238 |
2 |
| Newx(p_buf, p_len + 1, unsigned char); |
| 239 |
2 |
▶ | if (p_buf == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p_buf == NULL) |
0 |
2 |
2 |
|
| 240 |
0 |
| croak("ed25519_scalar_random: Failed to allocate memory"); |
| 241 |
2 |
| p_buf[p_len] = '\0'; |
| 242 |
|
| |
| 243 |
2 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
1 |
1 |
2 |
|
| 244 |
1 |
| case 1: |
| 245 |
1 |
| crypto_core_ristretto255_random(p_buf); |
| 246 |
1 |
| break; |
| 247 |
1 |
| default: |
| 248 |
1 |
| crypto_core_ed25519_random(p_buf); |
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
2 |
| RETVAL = newSV(0); |
| 252 |
2 |
| sv_usepvn_flags(RETVAL, (char *)p_buf, p_len, SV_HAS_TRAILING_NUL); |
| 253 |
|
| |
| 254 |
|
| OUTPUT: |
| 255 |
|
| RETVAL |
| 256 |
|
| |
| 257 |
|
| SV * core_ed25519_scalar_add(SV * x, SV * y, SV * flags = &PL_sv_undef) |
| 258 |
|
| |
| 259 |
|
| ALIAS: |
| 260 |
|
| core_ed25519_scalar_sub = 1 |
| 261 |
|
| core_ed25519_scalar_mul = 2 |
| 262 |
|
| core_ristretto255_scalar_add = 3 |
| 263 |
|
| core_ristretto255_scalar_sub = 4 |
| 264 |
|
| core_ristretto255_scalar_mul = 5 |
| 265 |
|
| |
| 266 |
|
| PREINIT: |
| 267 |
0 |
| protmem *new_pm, *x_pm = NULL, *y_pm = NULL; |
| 268 |
0 |
▶ | unsigned int new_flags = g_protmem_default_flags_key; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int new_flags = g_protmem_default_flags_key; |
0 |
0 |
0 |
|
| 269 |
|
| STRLEN new_len, x_len, x_req_len, y_len, y_req_len; |
| 270 |
|
| unsigned char *x_buf, *y_buf; |
| 271 |
|
| |
| 272 |
|
| CODE: |
| 273 |
0 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
0 |
0 |
0 |
|
| 274 |
|
| /* case 1 and 2 are same as default */ |
| 275 |
0 |
| case 3: |
| 276 |
|
| /* fallthrough */ |
| 277 |
|
| case 4: |
| 278 |
|
| /* fallthrough */ |
| 279 |
|
| case 5: |
| 280 |
0 |
| new_len = crypto_core_ristretto255_SCALARBYTES; |
| 281 |
0 |
| x_req_len = crypto_core_ristretto255_SCALARBYTES; |
| 282 |
0 |
| y_req_len = crypto_core_ristretto255_SCALARBYTES; |
| 283 |
0 |
| break; |
| 284 |
0 |
| default: |
| 285 |
0 |
| new_len = crypto_core_ed25519_SCALARBYTES; |
| 286 |
0 |
| x_req_len = crypto_core_ed25519_SCALARBYTES; |
| 287 |
0 |
| y_req_len = crypto_core_ed25519_SCALARBYTES; |
| 288 |
|
| } |
| 289 |
0 |
| SvGETMAGIC(flags); |
| 290 |
0 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
0 |
0 |
|
| 291 |
0 |
| new_flags = SvUV_nomg(flags); |
| 292 |
|
| |
| 293 |
0 |
▶ | if (sv_derived_from(x, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(x, MEMVAULT_CLASS)) { |
0 |
0 |
0 |
|
| 294 |
0 |
| x_pm = protmem_get(aTHX_ x, MEMVAULT_CLASS); |
| 295 |
0 |
| x_buf = x_pm->pm_ptr; |
| 296 |
0 |
| x_len = x_pm->size; |
| 297 |
|
| } |
| 298 |
|
| else |
| 299 |
0 |
| x_buf = (unsigned char *)SvPVbyte(x, x_len); |
| 300 |
0 |
▶ | if (x_len != x_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (x_len != x_req_len) |
0 |
0 |
0 |
|
| 301 |
0 |
| croak("ed25519_scalar_add|sub|mul: Invalid x length"); |
| 302 |
|
| |
| 303 |
0 |
▶ | if (sv_derived_from(y, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(y, MEMVAULT_CLASS)) { |
0 |
0 |
0 |
|
| 304 |
0 |
| y_pm = protmem_get(aTHX_ y, MEMVAULT_CLASS); |
| 305 |
0 |
| y_buf = y_pm->pm_ptr; |
| 306 |
0 |
| y_len = y_pm->size; |
| 307 |
|
| } |
| 308 |
|
| else |
| 309 |
0 |
| y_buf = (unsigned char *)SvPVbyte(y, y_len); |
| 310 |
0 |
▶ | if (y_len != y_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (y_len != y_req_len) |
0 |
0 |
0 |
|
| 311 |
0 |
| croak("ed25519_scalar_add|sub|mul: Invalid y length"); |
| 312 |
|
| |
| 313 |
0 |
| new_pm = protmem_init(aTHX_ new_len, new_flags); |
| 314 |
0 |
▶ | if (new_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_pm == NULL) |
0 |
0 |
0 |
|
| 315 |
0 |
| croak("ed25519_scalar_add|sub|mul: Failed to allocate protmem"); |
| 316 |
|
| |
| 317 |
0 |
▶ | if (x_pm && protmem_grant(aTHX_ x_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (x_pm && protmem_grant(aTHX_ x_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
| if (x_pm && protmem_grant(aTHX_ x_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
|
| 318 |
0 |
| protmem_free(aTHX_ new_pm); |
| 319 |
0 |
| croak("ed25519_scalar_add|sub|mul: Failed to grant x protmem RO"); |
| 320 |
|
| } |
| 321 |
0 |
▶ | if (y_pm && protmem_grant(aTHX_ y_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (y_pm && protmem_grant(aTHX_ y_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
| if (y_pm && protmem_grant(aTHX_ y_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
|
| 322 |
0 |
| protmem_free(aTHX_ new_pm); |
| 323 |
0 |
| protmem_release(aTHX_ x_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 324 |
0 |
| croak("ed25519_scalar_add|sub|mul: Failed to grant y protmem RO"); |
| 325 |
|
| } |
| 326 |
|
| |
| 327 |
0 |
| switch(ix) { |
| 328 |
0 |
| case 1: |
| 329 |
0 |
| crypto_core_ed25519_scalar_sub(new_pm->pm_ptr, x_buf, y_buf); |
| 330 |
0 |
| break; |
| 331 |
0 |
| case 2: |
| 332 |
0 |
| crypto_core_ed25519_scalar_mul(new_pm->pm_ptr, x_buf, y_buf); |
| 333 |
0 |
| break; |
| 334 |
0 |
| case 3: |
| 335 |
0 |
| crypto_core_ristretto255_scalar_add(new_pm->pm_ptr, x_buf, y_buf); |
| 336 |
0 |
| break; |
| 337 |
0 |
| case 4: |
| 338 |
0 |
| crypto_core_ristretto255_scalar_sub(new_pm->pm_ptr, x_buf, y_buf); |
| 339 |
0 |
| break; |
| 340 |
0 |
| case 5: |
| 341 |
0 |
| crypto_core_ristretto255_scalar_mul(new_pm->pm_ptr, x_buf, y_buf); |
| 342 |
0 |
| break; |
| 343 |
0 |
| default: |
| 344 |
0 |
| crypto_core_ed25519_scalar_add(new_pm->pm_ptr, x_buf, y_buf); |
| 345 |
|
| } |
| 346 |
|
| |
| 347 |
0 |
▶ | if (y_pm && protmem_release(aTHX_ y_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (y_pm && protmem_release(aTHX_ y_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
| if (y_pm && protmem_release(aTHX_ y_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
|
| 348 |
0 |
| protmem_free(aTHX_ new_pm); |
| 349 |
0 |
| protmem_release(aTHX_ x_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 350 |
0 |
| croak("ed25519_scalar_add|sub: Failed to release y protmem RO"); |
| 351 |
|
| } |
| 352 |
0 |
▶ | if (x_pm && protmem_release(aTHX_ x_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (x_pm && protmem_release(aTHX_ x_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
| if (x_pm && protmem_release(aTHX_ x_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
|
| 353 |
0 |
| protmem_free(aTHX_ new_pm); |
| 354 |
0 |
| croak("ed25519_scalar_add|sub: Failed to release x protmem RO"); |
| 355 |
|
| } |
| 356 |
0 |
▶ | 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 |
0 |
0 |
|
| 357 |
0 |
| protmem_free(aTHX_ new_pm); |
| 358 |
0 |
| croak("ed25519_scalar_add|sub: Failed to release protmem RO"); |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
0 |
| RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS); |
| 362 |
|
| |
| 363 |
|
| OUTPUT: |
| 364 |
|
| RETVAL |
| 365 |
|
| |
| 366 |
|
| SV * core_ed25519_scalar_complement(SV * s, SV * flags = &PL_sv_undef) |
| 367 |
|
| |
| 368 |
|
| ALIAS: |
| 369 |
|
| core_ed25519_scalar_invert = 1 |
| 370 |
|
| core_ed25519_scalar_negate = 2 |
| 371 |
|
| core_ed25519_scalar_reduce = 3 |
| 372 |
|
| core_ristretto255_scalar_complement = 4 |
| 373 |
|
| core_ristretto255_scalar_invert = 5 |
| 374 |
|
| core_ristretto255_scalar_negate = 6 |
| 375 |
|
| core_ristretto255_scalar_reduce = 7 |
| 376 |
|
| |
| 377 |
|
| PREINIT: |
| 378 |
2 |
| protmem *new_pm, *s_pm = NULL; |
| 379 |
2 |
▶ | unsigned int new_flags = g_protmem_default_flags_key; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int new_flags = g_protmem_default_flags_key; |
2 |
0 |
2 |
|
| 380 |
|
| STRLEN new_len, s_len, s_req_len; |
| 381 |
|
| unsigned char *s_buf; |
| 382 |
|
| |
| 383 |
|
| CODE: |
| 384 |
2 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
1 |
1 |
2 |
|
| 385 |
1 |
| case 4: |
| 386 |
|
| /* fallthrough */ |
| 387 |
|
| case 5: |
| 388 |
|
| /* fallthrough */ |
| 389 |
|
| case 6: |
| 390 |
1 |
| new_len = crypto_core_ristretto255_SCALARBYTES; |
| 391 |
1 |
| s_req_len = crypto_core_ristretto255_SCALARBYTES; |
| 392 |
1 |
| break; |
| 393 |
1 |
| default: |
| 394 |
1 |
| new_len = crypto_core_ed25519_SCALARBYTES; |
| 395 |
1 |
| s_req_len = crypto_core_ed25519_SCALARBYTES; |
| 396 |
|
| } |
| 397 |
2 |
| SvGETMAGIC(flags); |
| 398 |
2 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
2 |
2 |
|
| 399 |
0 |
| new_flags = SvUV(flags); |
| 400 |
|
| |
| 401 |
2 |
▶ | if (sv_derived_from(s, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(s, MEMVAULT_CLASS)) { |
2 |
0 |
2 |
|
| 402 |
2 |
| s_pm = protmem_get(aTHX_ s, MEMVAULT_CLASS); |
| 403 |
2 |
| s_buf = s_pm->pm_ptr; |
| 404 |
2 |
| s_len = s_pm->size; |
| 405 |
|
| } |
| 406 |
|
| else |
| 407 |
0 |
| s_buf = (unsigned char *)SvPVbyte(s, s_len); |
| 408 |
2 |
▶ | if (s_len != s_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (s_len != s_req_len) |
0 |
2 |
2 |
|
| 409 |
0 |
| croak("ed25519_scalar_complement|invert|negate|reduce: Invalid x length"); |
| 410 |
|
| |
| 411 |
2 |
| new_pm = protmem_init(aTHX_ new_len, new_flags); |
| 412 |
2 |
▶ | if (new_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_pm == NULL) |
0 |
2 |
2 |
|
| 413 |
0 |
| croak("ed25519_scalar_add|sub|mul: Failed to allocate protmem"); |
| 414 |
|
| |
| 415 |
2 |
▶ | if (s_pm && protmem_grant(aTHX_ s_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s_pm && protmem_grant(aTHX_ s_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
2 |
0 |
2 |
| if (s_pm && protmem_grant(aTHX_ s_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
2 |
2 |
|
| 416 |
0 |
| protmem_free(aTHX_ new_pm); |
| 417 |
0 |
| croak("ed25519_scalar_complement|invert|negate|reduce: Failed to grant s protmem RO"); |
| 418 |
|
| } |
| 419 |
|
| |
| 420 |
2 |
| switch(ix) { |
| 421 |
0 |
| case 1: |
| 422 |
0 |
| crypto_core_ed25519_scalar_invert(new_pm->pm_ptr, s_buf); |
| 423 |
0 |
| break; |
| 424 |
1 |
| case 2: |
| 425 |
1 |
| crypto_core_ed25519_scalar_negate(new_pm->pm_ptr, s_buf); |
| 426 |
1 |
| break; |
| 427 |
0 |
| case 3: |
| 428 |
0 |
| crypto_core_ed25519_scalar_reduce(new_pm->pm_ptr, s_buf); |
| 429 |
0 |
| break; |
| 430 |
0 |
| case 4: |
| 431 |
0 |
| crypto_core_ristretto255_scalar_complement(new_pm->pm_ptr, s_buf); |
| 432 |
0 |
| break; |
| 433 |
0 |
| case 5: |
| 434 |
0 |
| crypto_core_ristretto255_scalar_invert(new_pm->pm_ptr, s_buf); |
| 435 |
0 |
| break; |
| 436 |
1 |
| case 6: |
| 437 |
1 |
| crypto_core_ristretto255_scalar_negate(new_pm->pm_ptr, s_buf); |
| 438 |
1 |
| break; |
| 439 |
0 |
| case 7: |
| 440 |
0 |
| crypto_core_ristretto255_scalar_reduce(new_pm->pm_ptr, s_buf); |
| 441 |
0 |
| break; |
| 442 |
0 |
| default: |
| 443 |
0 |
| crypto_core_ed25519_scalar_complement(new_pm->pm_ptr, s_buf); |
| 444 |
|
| } |
| 445 |
|
| |
| 446 |
2 |
▶ | if (s_pm && protmem_release(aTHX_ s_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s_pm && protmem_release(aTHX_ s_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
2 |
0 |
2 |
| if (s_pm && protmem_release(aTHX_ s_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
2 |
2 |
|
| 447 |
0 |
| protmem_free(aTHX_ new_pm); |
| 448 |
0 |
| croak("ed25519_scalar_complement|invert|negate|reduce: Failed to release s protmem RO"); |
| 449 |
|
| } |
| 450 |
2 |
▶ | 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 |
2 |
2 |
|
| 451 |
0 |
| protmem_free(aTHX_ new_pm); |
| 452 |
0 |
| croak("ed25519_scalar_complement|invert|negate|reduce: Failed to release protmem RO"); |
| 453 |
|
| } |
| 454 |
|
| |
| 455 |
2 |
| RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS); |
| 456 |
|
| |
| 457 |
|
| OUTPUT: |
| 458 |
|
| RETVAL |
| 459 |
|
| |
| 460 |
|
| SV * core_ed25519_scalar_random(SV * flags = &PL_sv_undef) |
| 461 |
|
| |
| 462 |
|
| ALIAS: |
| 463 |
|
| core_ristretto255_scalar_random = 1 |
| 464 |
|
| |
| 465 |
|
| PREINIT: |
| 466 |
|
| protmem *new_pm; |
| 467 |
4 |
▶ | unsigned int new_flags = g_protmem_default_flags_key; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int new_flags = g_protmem_default_flags_key; |
4 |
0 |
4 |
|
| 468 |
|
| STRLEN new_len; |
| 469 |
|
| |
| 470 |
|
| CODE: |
| 471 |
4 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
2 |
2 |
4 |
|
| 472 |
2 |
| case 1: |
| 473 |
2 |
| new_len = crypto_core_ristretto255_SCALARBYTES; |
| 474 |
2 |
| break; |
| 475 |
2 |
| default: |
| 476 |
2 |
| new_len = crypto_core_ed25519_SCALARBYTES; |
| 477 |
|
| } |
| 478 |
4 |
| SvGETMAGIC(flags); |
| 479 |
4 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
4 |
4 |
|
| 480 |
0 |
| new_flags = SvUV(flags); |
| 481 |
4 |
| new_pm = protmem_init(aTHX_ new_len, new_flags); |
| 482 |
4 |
▶ | if (new_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_pm == NULL) |
0 |
4 |
4 |
|
| 483 |
0 |
| croak("ed25519_scalar_random: Failed to allocate protmem"); |
| 484 |
4 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
2 |
2 |
4 |
|
| 485 |
2 |
| case 1: |
| 486 |
2 |
| crypto_core_ristretto255_scalar_random(new_pm->pm_ptr); |
| 487 |
2 |
| break; |
| 488 |
2 |
| default: |
| 489 |
2 |
| crypto_core_ed25519_scalar_random(new_pm->pm_ptr); |
| 490 |
|
| } |
| 491 |
4 |
▶ | 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 |
4 |
4 |
|
| 492 |
0 |
| croak("ed25519_scalar_random: Failed to release protmem RW"); |
| 493 |
|
| } |
| 494 |
|
| |
| 495 |
4 |
| RETVAL = protmem_to_sv(aTHX_ new_pm, MEMVAULT_CLASS); |
| 496 |
|
| |
| 497 |
|
| OUTPUT: |
| 498 |
|
| RETVAL |