| 1 |
|
| SV * |
| 2 |
|
| seal(key, nonce, plaintext, ...) |
| 3 |
|
| SV *key |
| 4 |
|
| SV *nonce |
| 5 |
|
| SV *plaintext |
| 6 |
|
| PREINIT: |
| 7 |
157 |
| STRLEN keylen, noncelen, len, adlen = 0; |
| 8 |
157 |
| const uint8_t *k, *n, *in, *ad = NULL; |
| 9 |
157 |
| SV *bound = NULL; |
| 10 |
|
| uint8_t *out; |
| 11 |
|
| int code; |
| 12 |
|
| CODE: |
| 13 |
157 |
▶ | if (items > 4) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 4) |
1 |
156 |
157 |
|
| 14 |
1 |
| croak("Usage: Crypt::Symmetric::Primitives::seal(key, nonce, plaintext, data)"); |
| 15 |
156 |
| key = bytes_of(aTHX_ key); |
| 16 |
155 |
| nonce = bytes_of(aTHX_ nonce); |
| 17 |
154 |
| plaintext = bytes_of(aTHX_ plaintext); |
| 18 |
152 |
▶ | if (items > 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 3) { |
143 |
9 |
152 |
|
| 19 |
143 |
| bound = settled(aTHX_ ST(3)); |
| 20 |
142 |
▶ | bound = SvOK(bound) ? bytes_of(aTHX_ bound) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| bound = SvOK(bound) ? bytes_of(aTHX_ bound) : NULL; |
128 |
14 |
142 |
|
| 21 |
|
| } |
| 22 |
151 |
| k = bytes_at(aTHX_ key, &keylen); |
| 23 |
149 |
▶ | if (keylen != SYMPRIM_AEAD_KEY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (keylen != SYMPRIM_AEAD_KEY) |
2 |
147 |
149 |
|
| 24 |
2 |
| croak("a seal key is 32 bytes"); |
| 25 |
147 |
| n = bytes_at(aTHX_ nonce, &noncelen); |
| 26 |
147 |
▶ | if (noncelen != SYMPRIM_AEAD_NONCE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (noncelen != SYMPRIM_AEAD_NONCE) |
2 |
145 |
147 |
|
| 27 |
2 |
| croak("a seal nonce is 24 bytes"); |
| 28 |
145 |
▶ | if (bound) |
Branch: 1 branch
| Branch | True | False | Total |
| if (bound) |
127 |
18 |
145 |
|
| 29 |
127 |
| ad = bytes_at(aTHX_ bound, &adlen); |
| 30 |
145 |
| in = bytes_at(aTHX_ plaintext, &len); |
| 31 |
145 |
| RETVAL = newSV(len + SYMPRIM_AEAD_TAG); |
| 32 |
145 |
| SvPOK_only(RETVAL); |
| 33 |
145 |
| out = (uint8_t *)SvPVX(RETVAL); |
| 34 |
145 |
| code = symprim_aead_seal(out, out + len, in, len, ad, adlen, n, k); |
| 35 |
145 |
| engine_agreed(aTHX_ code, RETVAL, NULL, 0); |
| 36 |
145 |
| out[len + SYMPRIM_AEAD_TAG] = '\0'; |
| 37 |
145 |
| SvCUR_set(RETVAL, len + SYMPRIM_AEAD_TAG); |
| 38 |
|
| OUTPUT: |
| 39 |
|
| RETVAL |
| 40 |
|
| |
| 41 |
|
| SV * |
| 42 |
|
| open_sealed(key, nonce, sealed, ...) |
| 43 |
|
| SV *key |
| 44 |
|
| SV *nonce |
| 45 |
|
| SV *sealed |
| 46 |
|
| PREINIT: |
| 47 |
1548 |
| STRLEN keylen, noncelen, len, adlen = 0; |
| 48 |
1548 |
| const uint8_t *k, *n, *in, *ad = NULL; |
| 49 |
1548 |
| SV *bound = NULL; |
| 50 |
|
| uint8_t *out; |
| 51 |
|
| int code; |
| 52 |
|
| CODE: |
| 53 |
1548 |
▶ | if (items > 4) |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 4) |
1 |
1547 |
1548 |
|
| 54 |
1 |
| croak("Usage: Crypt::Symmetric::Primitives::open_sealed(key, nonce, sealed, data)"); |
| 55 |
1547 |
| key = bytes_of(aTHX_ key); |
| 56 |
1546 |
| nonce = bytes_of(aTHX_ nonce); |
| 57 |
1545 |
| sealed = bytes_of(aTHX_ sealed); |
| 58 |
1544 |
▶ | if (items > 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 3) { |
1540 |
4 |
1544 |
|
| 59 |
1540 |
| bound = settled(aTHX_ ST(3)); |
| 60 |
1539 |
▶ | bound = SvOK(bound) ? bytes_of(aTHX_ bound) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| bound = SvOK(bound) ? bytes_of(aTHX_ bound) : NULL; |
1526 |
13 |
1539 |
|
| 61 |
|
| } |
| 62 |
1543 |
| k = bytes_at(aTHX_ key, &keylen); |
| 63 |
1543 |
▶ | if (keylen != SYMPRIM_AEAD_KEY) |
Branch: 1 branch
| Branch | True | False | Total |
| if (keylen != SYMPRIM_AEAD_KEY) |
1 |
1542 |
1543 |
|
| 64 |
1 |
| croak("a seal key is 32 bytes"); |
| 65 |
1542 |
| n = bytes_at(aTHX_ nonce, &noncelen); |
| 66 |
1542 |
▶ | if (noncelen != SYMPRIM_AEAD_NONCE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (noncelen != SYMPRIM_AEAD_NONCE) |
1 |
1541 |
1542 |
|
| 67 |
1 |
| croak("a seal nonce is 24 bytes"); |
| 68 |
1541 |
▶ | if (bound) |
Branch: 1 branch
| Branch | True | False | Total |
| if (bound) |
1526 |
15 |
1541 |
|
| 69 |
1526 |
| ad = bytes_at(aTHX_ bound, &adlen); |
| 70 |
1541 |
| in = bytes_at(aTHX_ sealed, &len); |
| 71 |
1541 |
▶ | if (len < SYMPRIM_AEAD_TAG) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < SYMPRIM_AEAD_TAG) { |
2 |
1539 |
1541 |
|
| 72 |
2 |
| RETVAL = &PL_sv_undef; |
| 73 |
|
| } else { |
| 74 |
1539 |
| len -= SYMPRIM_AEAD_TAG; |
| 75 |
1539 |
▶ | RETVAL = newSV(len ? len : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = newSV(len ? len : 1); |
1532 |
7 |
1539 |
|
| 76 |
1539 |
| SvPOK_only(RETVAL); |
| 77 |
1539 |
| out = (uint8_t *)SvPVX(RETVAL); |
| 78 |
1539 |
| code = symprim_aead_open(out, in, len, in + len, ad, adlen, n, k); |
| 79 |
1539 |
▶ | if (code == SYMPRIM_E_AUTH) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code == SYMPRIM_E_AUTH) { |
1477 |
62 |
1539 |
|
| 80 |
1477 |
| SvREFCNT_dec(RETVAL); |
| 81 |
1477 |
| RETVAL = &PL_sv_undef; |
| 82 |
|
| } else { |
| 83 |
62 |
| engine_agreed(aTHX_ code, RETVAL, NULL, 0); |
| 84 |
62 |
| out[len] = '\0'; |
| 85 |
62 |
| SvCUR_set(RETVAL, len); |
| 86 |
|
| } |
| 87 |
|
| } |
| 88 |
1541 |
▶ | if (RETVAL == &PL_sv_undef) |
Branch: 1 branch
| Branch | True | False | Total |
| if (RETVAL == &PL_sv_undef) |
1479 |
62 |
1541 |
|
| 89 |
1479 |
| SvREFCNT_inc_simple_void_NN(RETVAL); |
| 90 |
|
| OUTPUT: |
| 91 |
|
| RETVAL |
| 92 |
|
| |
| 93 |
|
| SV * |
| 94 |
|
| _chacha20(key, nonce, counter, data) |
| 95 |
|
| SV *key |
| 96 |
|
| SV *nonce |
| 97 |
|
| SV *counter |
| 98 |
|
| SV *data |
| 99 |
|
| PREINIT: |
| 100 |
|
| STRLEN keylen, noncelen, len; |
| 101 |
|
| const uint8_t *k, *n, *in; |
| 102 |
|
| uint32_t start; |
| 103 |
|
| int code; |
| 104 |
|
| CODE: |
| 105 |
64 |
| key = bytes_of(aTHX_ key); |
| 106 |
63 |
| nonce = bytes_of(aTHX_ nonce); |
| 107 |
62 |
| data = bytes_of(aTHX_ data); |
| 108 |
61 |
| start = number_between(aTHX_ "_chacha20", "the counter", settled(aTHX_ counter), 0, 0xFFFFFFFFUL); |
| 109 |
59 |
| k = bytes_at(aTHX_ key, &keylen); |
| 110 |
59 |
| n = bytes_at(aTHX_ nonce, &noncelen); |
| 111 |
59 |
| in = bytes_at(aTHX_ data, &len); |
| 112 |
59 |
▶ | if (keylen != 32 || noncelen != 12) |
Branch: 2 branches
| Branch | True | False | Total |
| if (keylen != 32 || noncelen != 12) |
58 |
1 |
59 |
| if (keylen != 32 || noncelen != 12) |
1 |
57 |
58 |
|
| 113 |
2 |
| croak("_chacha20 takes a 32 byte key and a 12 byte nonce"); |
| 114 |
57 |
▶ | RETVAL = newSV(len ? len : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = newSV(len ? len : 1); |
56 |
1 |
57 |
|
| 115 |
57 |
| SvPOK_only(RETVAL); |
| 116 |
57 |
| code = symprim_chacha20((uint8_t *)SvPVX(RETVAL), in, len, k, n, start); |
| 117 |
57 |
▶ | if (code != SYMPRIM_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (code != SYMPRIM_OK) { |
2 |
55 |
57 |
|
| 118 |
2 |
| SvREFCNT_dec(RETVAL); |
| 119 |
2 |
| croak("%s", symprim_error_message(code)); |
| 120 |
|
| } |
| 121 |
55 |
| SvPVX(RETVAL)[len] = '\0'; |
| 122 |
55 |
| SvCUR_set(RETVAL, len); |
| 123 |
|
| OUTPUT: |
| 124 |
|
| RETVAL |
| 125 |
|
| |
| 126 |
|
| SV * |
| 127 |
|
| _hchacha20(key, nonce) |
| 128 |
|
| SV *key |
| 129 |
|
| SV *nonce |
| 130 |
|
| PREINIT: |
| 131 |
|
| STRLEN keylen, noncelen; |
| 132 |
|
| const uint8_t *k, *n; |
| 133 |
|
| uint8_t out[32]; |
| 134 |
|
| CODE: |
| 135 |
48 |
| key = bytes_of(aTHX_ key); |
| 136 |
47 |
| nonce = bytes_of(aTHX_ nonce); |
| 137 |
46 |
| k = bytes_at(aTHX_ key, &keylen); |
| 138 |
46 |
| n = bytes_at(aTHX_ nonce, &noncelen); |
| 139 |
46 |
▶ | if (keylen != 32 || noncelen != 16) |
Branch: 2 branches
| Branch | True | False | Total |
| if (keylen != 32 || noncelen != 16) |
45 |
1 |
46 |
| if (keylen != 32 || noncelen != 16) |
1 |
44 |
45 |
|
| 140 |
2 |
| croak("_hchacha20 takes a 32 byte key and a 16 byte nonce"); |
| 141 |
44 |
| symprim_hchacha20(out, k, n); |
| 142 |
44 |
| RETVAL = newSVpvn((const char *)out, sizeof out); |
| 143 |
44 |
| symprim_wipe(out, sizeof out); |
| 144 |
|
| OUTPUT: |
| 145 |
|
| RETVAL |
| 146 |
|
| |
| 147 |
|
| SV * |
| 148 |
|
| _poly1305(key, ...) |
| 149 |
|
| SV *key |
| 150 |
|
| PREINIT: |
| 151 |
|
| STRLEN keylen, len; |
| 152 |
|
| const uint8_t *k, *in; |
| 153 |
|
| symprim_poly1305_state s; |
| 154 |
|
| uint8_t tag[16]; |
| 155 |
|
| int i; |
| 156 |
|
| CODE: |
| 157 |
|
| /* Every piece is read before the key is taken up, so nothing that holds |
| 158 |
|
| * the key is in hand when a piece cannot be read. */ |
| 159 |
3586 |
| key = bytes_of(aTHX_ key); |
| 160 |
10748 |
▶ | for (i = 1; i < items; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) |
7165 |
3583 |
10748 |
|
| 161 |
7165 |
| ST(i) = bytes_of(aTHX_ ST(i)); |
| 162 |
3583 |
| k = bytes_at(aTHX_ key, &keylen); |
| 163 |
3583 |
▶ | if (keylen != 32) |
Branch: 1 branch
| Branch | True | False | Total |
| if (keylen != 32) |
1 |
3582 |
3583 |
|
| 164 |
1 |
| croak("_poly1305 takes a 32 byte key"); |
| 165 |
3582 |
| symprim_poly1305_init(&s, k); |
| 166 |
10739 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
7159 |
3580 |
10739 |
|
| 167 |
7159 |
| in = bytes_at_or_null(ST(i), &len); |
| 168 |
7159 |
▶ | if (!in) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in) { |
2 |
7157 |
7159 |
|
| 169 |
2 |
| symprim_wipe(&s, sizeof s); |
| 170 |
2 |
| croak("an argument was changed while the others were being read"); |
| 171 |
|
| } |
| 172 |
7157 |
| symprim_poly1305_update(&s, in, len); |
| 173 |
|
| } |
| 174 |
3580 |
| symprim_poly1305_final(&s, tag); |
| 175 |
3580 |
| symprim_wipe(&s, sizeof s); |
| 176 |
3580 |
| RETVAL = newSVpvn((const char *)tag, sizeof tag); |
| 177 |
|
| OUTPUT: |
| 178 |
|
| RETVAL |
| 179 |
|
| |
| 180 |
|
| SV * |
| 181 |
|
| _chacha20poly1305(key, nonce, plaintext, data) |
| 182 |
|
| SV *key |
| 183 |
|
| SV *nonce |
| 184 |
|
| SV *plaintext |
| 185 |
|
| SV *data |
| 186 |
|
| PREINIT: |
| 187 |
|
| STRLEN keylen, noncelen, len, adlen; |
| 188 |
|
| const uint8_t *k, *n, *in, *ad; |
| 189 |
|
| uint8_t *out; |
| 190 |
|
| CODE: |
| 191 |
117 |
| key = bytes_of(aTHX_ key); |
| 192 |
116 |
| nonce = bytes_of(aTHX_ nonce); |
| 193 |
115 |
| plaintext = bytes_of(aTHX_ plaintext); |
| 194 |
114 |
| data = bytes_of(aTHX_ data); |
| 195 |
113 |
| k = bytes_at(aTHX_ key, &keylen); |
| 196 |
113 |
| n = bytes_at(aTHX_ nonce, &noncelen); |
| 197 |
113 |
| ad = bytes_at(aTHX_ data, &adlen); |
| 198 |
113 |
| in = bytes_at(aTHX_ plaintext, &len); |
| 199 |
113 |
▶ | if (keylen != 32 || noncelen != 12) |
Branch: 2 branches
| Branch | True | False | Total |
| if (keylen != 32 || noncelen != 12) |
112 |
1 |
113 |
| if (keylen != 32 || noncelen != 12) |
1 |
111 |
112 |
|
| 200 |
2 |
| croak("_chacha20poly1305 takes a 32 byte key and a 12 byte nonce"); |
| 201 |
111 |
| RETVAL = newSV(len + 16); |
| 202 |
111 |
| SvPOK_only(RETVAL); |
| 203 |
111 |
| out = (uint8_t *)SvPVX(RETVAL); |
| 204 |
111 |
| symprim_chacha20poly1305_seal(out, out + len, in, len, ad, adlen, n, k); |
| 205 |
111 |
| out[len + 16] = '\0'; |
| 206 |
111 |
| SvCUR_set(RETVAL, len + 16); |
| 207 |
|
| OUTPUT: |
| 208 |
|
| RETVAL |