| 1 |
|
| MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::shorthash |
| 2 |
|
| |
| 3 |
|
| void _define_constants() |
| 4 |
|
| PREINIT: |
| 5 |
1 |
| HV *stash = gv_stashpv("Crypt::Sodium::XS::shorthash", 0); |
| 6 |
|
| |
| 7 |
|
| PPCODE: |
| 8 |
1 |
| newCONSTSUB(stash, "shorthash_BYTES", newSVuv(crypto_shorthash_BYTES)); |
| 9 |
1 |
| newCONSTSUB(stash, "shorthash_siphash24_BYTES", |
| 10 |
|
| newSVuv(crypto_shorthash_siphash24_BYTES)); |
| 11 |
1 |
| newCONSTSUB(stash, "shorthash_siphashx24_BYTES", |
| 12 |
|
| newSVuv(crypto_shorthash_siphashx24_BYTES)); |
| 13 |
1 |
| newCONSTSUB(stash, "shorthash_KEYBYTES", newSVuv(crypto_shorthash_KEYBYTES)); |
| 14 |
1 |
| newCONSTSUB(stash, "shorthash_siphash24_KEYBYTES", |
| 15 |
|
| newSVuv(crypto_shorthash_siphash24_KEYBYTES)); |
| 16 |
1 |
| newCONSTSUB(stash, "shorthash_siphashx24_KEYBYTES", |
| 17 |
|
| newSVuv(crypto_shorthash_siphashx24_KEYBYTES)); |
| 18 |
1 |
| newCONSTSUB(stash, "shorthash_PRIMITIVE", |
| 19 |
|
| newSVpvs(crypto_shorthash_PRIMITIVE)); |
| 20 |
|
| |
| 21 |
|
| SV * shorthash(SV * msg, SV * key) |
| 22 |
|
| |
| 23 |
|
| ALIAS: |
| 24 |
|
| shorthash_siphash24 = 1 |
| 25 |
|
| shorthash_siphashx24 = 2 |
| 26 |
|
| |
| 27 |
|
| PREINIT: |
| 28 |
48 |
| protmem *key_mv = NULL; |
| 29 |
|
| unsigned char *msg_buf, *key_buf, *out_buf; |
| 30 |
|
| STRLEN msg_len, key_len, key_req_len, out_len; |
| 31 |
|
| int (*func)(unsigned char *, const unsigned char *, |
| 32 |
|
| unsigned long long, const unsigned char *); |
| 33 |
|
| |
| 34 |
|
| CODE: |
| 35 |
48 |
| switch(ix) { |
| 36 |
16 |
| case 1: |
| 37 |
16 |
| key_req_len = crypto_shorthash_siphash24_KEYBYTES; |
| 38 |
16 |
| out_len = crypto_shorthash_siphash24_BYTES; |
| 39 |
16 |
| func = crypto_shorthash_siphash24; |
| 40 |
16 |
| break; |
| 41 |
16 |
| case 2: |
| 42 |
16 |
| key_req_len = crypto_shorthash_siphashx24_KEYBYTES; |
| 43 |
16 |
| out_len = crypto_shorthash_siphashx24_BYTES; |
| 44 |
16 |
| func = crypto_shorthash_siphashx24; |
| 45 |
16 |
| break; |
| 46 |
16 |
| default: |
| 47 |
16 |
| key_req_len = crypto_shorthash_KEYBYTES; |
| 48 |
16 |
| out_len = crypto_shorthash_BYTES; |
| 49 |
16 |
| func = crypto_shorthash; |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
48 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
48 |
0 |
48 |
|
| 53 |
48 |
| key_mv = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 54 |
48 |
| key_buf = key_mv->pm_ptr; |
| 55 |
48 |
| key_len = key_mv->size; |
| 56 |
|
| } |
| 57 |
|
| else |
| 58 |
0 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 59 |
48 |
▶ | if (key_len != key_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != key_req_len) |
0 |
48 |
48 |
|
| 60 |
0 |
| croak("shorthash: Invalid key length %lu", key_len); |
| 61 |
|
| |
| 62 |
48 |
| msg_buf = (unsigned char *)SvPVbyte(msg, msg_len); |
| 63 |
|
| |
| 64 |
48 |
| Newx(out_buf, out_len + 1, unsigned char); |
| 65 |
48 |
▶ | if (out_buf == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (out_buf == NULL) |
0 |
48 |
48 |
|
| 66 |
0 |
| croak("shorthash: Failed to allocate memory"); |
| 67 |
48 |
| out_buf[out_len] = '\0'; |
| 68 |
|
| |
| 69 |
48 |
▶ | if (key_mv && protmem_grant(aTHX_ key_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (key_mv && protmem_grant(aTHX_ key_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
48 |
0 |
48 |
| if (key_mv && protmem_grant(aTHX_ key_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
48 |
48 |
|
| 70 |
0 |
| Safefree(out_buf); |
| 71 |
0 |
| croak("shorthash: Failed to grant key protmem RO"); |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
48 |
| func(out_buf, msg_buf, msg_len, key_buf); |
| 75 |
|
| |
| 76 |
48 |
▶ | if (key_mv && protmem_release(aTHX_ key_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (key_mv && protmem_release(aTHX_ key_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) |
48 |
0 |
48 |
| if (key_mv && protmem_release(aTHX_ key_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
48 |
48 |
|
| 77 |
0 |
| croak("shorthash: Failed to release key protmem RO"); |
| 78 |
|
| |
| 79 |
48 |
| RETVAL = newSV(0); |
| 80 |
48 |
| sv_usepvn_flags(RETVAL, (char *)out_buf, out_len, SV_HAS_TRAILING_NUL); |
| 81 |
|
| |
| 82 |
|
| OUTPUT: |
| 83 |
|
| RETVAL |
| 84 |
|
| |
| 85 |
|
| SV * shorthash_keygen(SV * flags = &PL_sv_undef) |
| 86 |
|
| |
| 87 |
|
| ALIAS: |
| 88 |
|
| shorthash_siphash24_keygen = 1 |
| 89 |
|
| shorthash_siphashx24_keygen = 2 |
| 90 |
|
| |
| 91 |
|
| CODE: |
| 92 |
6 |
| switch(ix) { |
| 93 |
2 |
| case 1: |
| 94 |
2 |
| RETVAL = sv_keygen(aTHX_ crypto_shorthash_siphash24_KEYBYTES, flags); |
| 95 |
2 |
| break; |
| 96 |
2 |
| case 2: |
| 97 |
2 |
| RETVAL = sv_keygen(aTHX_ crypto_shorthash_siphashx24_KEYBYTES, flags); |
| 98 |
2 |
| break; |
| 99 |
2 |
| default: |
| 100 |
2 |
| RETVAL = sv_keygen(aTHX_ crypto_shorthash_KEYBYTES, flags); |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| OUTPUT: |
| 104 |
|
| RETVAL |