| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2026 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 3 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "extensions.h" | ||
| 19 | #include "lbm_c_interop.h" | ||
| 20 | #include "crypto_extensions.h" | ||
| 21 | #include "crypto.h" | ||
| 22 | #include <string.h> | ||
| 23 | |||
| 24 | #ifdef LBM_OPT_CRYPTO_EXTENSIONS_SIZE | ||
| 25 | #pragma GCC optimize ("-Os") | ||
| 26 | #endif | ||
| 27 | #ifdef LBM_OPT_CRYPTO_EXTENSIONS_SIZE_AGGRESSIVE | ||
| 28 | #pragma GCC optimize ("-Oz") | ||
| 29 | #endif | ||
| 30 | |||
| 31 | 15 | static lbm_value ext_sha256_str(lbm_value *args, lbm_uint argn) { | |
| 32 | 15 | lbm_value res = ENC_SYM_TERROR; | |
| 33 |
2/4✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
|
15 | if (argn == 1 && lbm_is_array_r(args[0])) { |
| 34 |
1/2✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
|
15 | if(lbm_create_array(&res, 32)) { |
| 35 | 15 | lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(res); | |
| 36 | 15 | lbm_array_header_t *in_arr = (lbm_array_header_t*)lbm_car(args[0]); | |
| 37 | 15 | char *str = (char*)in_arr->data; | |
| 38 | 15 | uint32_t n = (uint32_t)strlen_max(str, in_arr->size); | |
| 39 | 15 | crypto_sha256((uint8_t*)arr->data, (uint8_t*)str, n); | |
| 40 | } | ||
| 41 | } | ||
| 42 | 15 | return res; | |
| 43 | } | ||
| 44 | |||
| 45 | 30 | static lbm_value ext_sha256(lbm_value *args, lbm_uint argn) { | |
| 46 | 30 | lbm_value res = ENC_SYM_TERROR; | |
| 47 |
2/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 30 times.
✗ Branch 4 not taken.
|
30 | if (argn == 1 && lbm_is_array_r(args[0])) { |
| 48 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | if(lbm_create_array(&res, 32)) { |
| 49 | 30 | lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(res); | |
| 50 | 30 | lbm_array_header_t *in_arr = (lbm_array_header_t*)lbm_car(args[0]); | |
| 51 | 30 | crypto_sha256((uint8_t*)arr->data, (uint8_t*)in_arr->data, (uint32_t)in_arr->size); | |
| 52 | } | ||
| 53 | } | ||
| 54 | 30 | return res; | |
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | 12 | static lbm_value ext_aes128_enc(lbm_value *args, lbm_uint argn) { | |
| 60 | 12 | lbm_value res = ENC_SYM_TERROR; | |
| 61 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
24 | if (argn == 2 && |
| 62 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
24 | lbm_is_array_r(args[0]) && |
| 63 | 12 | lbm_is_array_r(args[1])) { | |
| 64 | |||
| 65 | 12 | lbm_array_header_t *key = lbm_dec_array_r(args[0]); | |
| 66 | 12 | lbm_array_header_t *data = lbm_dec_array_r(args[1]); | |
| 67 | |||
| 68 | // encodes one block of 16 bytes. | ||
| 69 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
24 | if (key->size == 16 && data->size == 16) { |
| 70 | uint8_t key_expanded[176]; | ||
| 71 | 12 | crypto_aes_key_expansion((uint8_t*)key->data, key_expanded, CRYPTO_AES128_NUM_WORDS_PER_KEY); | |
| 72 | // If this failed res is MERROR. | ||
| 73 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | if (lbm_create_array(&res, 16)) { |
| 74 | 12 | lbm_array_header_t *out = (lbm_array_header_t*)lbm_car(res); | |
| 75 | 12 | memcpy(out->data, data->data, 16); | |
| 76 | 12 | crypto_aes_cipher((uint8_t*)out->data, key_expanded, CRYPTO_AES128_NUM_ROUNDS); | |
| 77 | } | ||
| 78 | } else { | ||
| 79 | ✗ | res = ENC_SYM_EERROR; | |
| 80 | } | ||
| 81 | } | ||
| 82 | 12 | return res; | |
| 83 | } | ||
| 84 | |||
| 85 | 3 | static lbm_value ext_aes128_dec(lbm_value *args, lbm_uint argn) { | |
| 86 | 3 | lbm_value res = ENC_SYM_TERROR; | |
| 87 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | if (argn == 2 && |
| 88 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | lbm_is_array_r(args[0]) && |
| 89 | 3 | lbm_is_array_r(args[1])) { | |
| 90 | |||
| 91 | 3 | lbm_array_header_t *key = lbm_dec_array_r(args[0]); | |
| 92 | 3 | lbm_array_header_t *data = lbm_dec_array_r(args[1]); | |
| 93 | |||
| 94 | // decodes one block of 16 bytes. | ||
| 95 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | if (key->size == 16 && data->size == 16) { |
| 96 | uint8_t key_expanded[176]; | ||
| 97 | 3 | crypto_aes_key_expansion((uint8_t*)key->data, key_expanded, CRYPTO_AES128_NUM_WORDS_PER_KEY); | |
| 98 | // If this failed res is MERROR. | ||
| 99 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if (lbm_create_array(&res, 16)) { |
| 100 | 3 | lbm_array_header_t *out = (lbm_array_header_t*)lbm_car(res); | |
| 101 | 3 | memcpy(out->data, data->data, 16); | |
| 102 | 3 | crypto_aes_inv_cipher((uint8_t*)out->data, key_expanded, CRYPTO_AES128_NUM_ROUNDS); | |
| 103 | } | ||
| 104 | } else { | ||
| 105 | ✗ | res = ENC_SYM_EERROR; | |
| 106 | } | ||
| 107 | } | ||
| 108 | 3 | return res; | |
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | 12 | static lbm_value ext_aes256_enc(lbm_value *args, lbm_uint argn) { | |
| 113 | 12 | lbm_value res = ENC_SYM_TERROR; | |
| 114 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
24 | if (argn == 2 && |
| 115 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
24 | lbm_is_array_r(args[0]) && |
| 116 | 12 | lbm_is_array_r(args[1])) { | |
| 117 | |||
| 118 | 12 | lbm_array_header_t *key = lbm_dec_array_r(args[0]); | |
| 119 | 12 | lbm_array_header_t *data = lbm_dec_array_r(args[1]); | |
| 120 | |||
| 121 | // encodes one block of 16 bytes. | ||
| 122 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
24 | if (key->size == 32 && data->size == 16) { |
| 123 | uint8_t key_expanded[240]; | ||
| 124 | 12 | crypto_aes_key_expansion((uint8_t*)key->data, key_expanded, CRYPTO_AES256_NUM_WORDS_PER_KEY); | |
| 125 | // If this failed res is MERROR. | ||
| 126 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | if (lbm_create_array(&res, 16)) { |
| 127 | 12 | lbm_array_header_t *out = (lbm_array_header_t*)lbm_car(res); | |
| 128 | 12 | memcpy(out->data, data->data, 16); | |
| 129 | 12 | crypto_aes_cipher((uint8_t*)out->data, key_expanded, CRYPTO_AES256_NUM_ROUNDS); | |
| 130 | } | ||
| 131 | } else { | ||
| 132 | ✗ | res = ENC_SYM_EERROR; | |
| 133 | } | ||
| 134 | } | ||
| 135 | 12 | return res; | |
| 136 | } | ||
| 137 | |||
| 138 | 3 | static lbm_value ext_aes256_dec(lbm_value *args, lbm_uint argn) { | |
| 139 | 3 | lbm_value res = ENC_SYM_TERROR; | |
| 140 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | if (argn == 2 && |
| 141 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | lbm_is_array_r(args[0]) && |
| 142 | 3 | lbm_is_array_r(args[1])) { | |
| 143 | |||
| 144 | 3 | lbm_array_header_t *key = lbm_dec_array_r(args[0]); | |
| 145 | 3 | lbm_array_header_t *data = lbm_dec_array_r(args[1]); | |
| 146 | |||
| 147 | // decodes one block of 16 bytes. | ||
| 148 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | if (key->size == 32 && data->size == 16) { |
| 149 | uint8_t key_expanded[240]; | ||
| 150 | 3 | crypto_aes_key_expansion((uint8_t*)key->data, key_expanded, CRYPTO_AES256_NUM_WORDS_PER_KEY); | |
| 151 | // If this failed res is MERROR. | ||
| 152 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if (lbm_create_array(&res, 16)) { |
| 153 | 3 | lbm_array_header_t *out = (lbm_array_header_t*)lbm_car(res); | |
| 154 | 3 | memcpy(out->data, data->data, 16); | |
| 155 | 3 | crypto_aes_inv_cipher((uint8_t*)out->data, key_expanded, CRYPTO_AES256_NUM_ROUNDS); | |
| 156 | } | ||
| 157 | } else { | ||
| 158 | ✗ | res = ENC_SYM_EERROR; | |
| 159 | } | ||
| 160 | } | ||
| 161 | 3 | return res; | |
| 162 | } | ||
| 163 | |||
| 164 | // //////////////////////////////////////////////////////////// | ||
| 165 | // Utilities | ||
| 166 | // | ||
| 167 | |||
| 168 | 99 | static lbm_value ext_bytes_to_hex(lbm_value *args, lbm_uint argn) { | |
| 169 | 99 | lbm_value res = ENC_SYM_TERROR; | |
| 170 |
2/4✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 99 times.
✗ Branch 4 not taken.
|
99 | if (argn == 1 && lbm_is_array_r(args[0])) { |
| 171 | 99 | lbm_array_header_t *in_arr = (lbm_array_header_t*)lbm_car(args[0]); | |
| 172 |
1/2✓ Branch 1 taken 99 times.
✗ Branch 2 not taken.
|
99 | if (lbm_create_array(&res, in_arr->size * 2 + 1)) { |
| 173 | 99 | lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(res); | |
| 174 | 99 | crypto_bytes_to_hex((uint8_t*)in_arr->data, (uint32_t)in_arr->size, (char*)arr->data); | |
| 175 | } | ||
| 176 | } | ||
| 177 | 99 | return res; | |
| 178 | } | ||
| 179 | |||
| 180 | 9 | static lbm_value ext_hex_to_bytes(lbm_value *args, lbm_uint argn) { | |
| 181 | 9 | lbm_value res = ENC_SYM_TERROR; | |
| 182 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
|
9 | if (argn == 1 && lbm_is_array_r(args[0])) { |
| 183 | 9 | lbm_array_header_t *in_arr = (lbm_array_header_t*)lbm_car(args[0]); | |
| 184 | 9 | res = ENC_SYM_EERROR; | |
| 185 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | if (in_arr->size % 2 == 1) { // null-terminated: size odd means even number of hex chars |
| 186 | 6 | lbm_uint bytes_len = (in_arr->size - 1) / 2; | |
| 187 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | if (lbm_create_array(&res, bytes_len)) { |
| 188 | 6 | lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(res); | |
| 189 | 6 | crypto_hex_to_bytes((uint8_t*)in_arr->data, (uint32_t)(in_arr->size - 1), (uint8_t*)arr->data); | |
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | 9 | return res; | |
| 194 | } | ||
| 195 | |||
| 196 | // //////////////////////////////////////////////////////////// | ||
| 197 | // Bignum | ||
| 198 | // | ||
| 199 | |||
| 200 | 21 | static lbm_value ext_bn_divmod(lbm_value *args, lbm_uint argn) { | |
| 201 | 21 | lbm_value res = ENC_SYM_TERROR; | |
| 202 |
3/6✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 21 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 21 times.
✗ Branch 7 not taken.
|
21 | if (argn == 2 && lbm_is_array_r(args[0]) && lbm_is_array_r(args[1])) { |
| 203 | 21 | lbm_array_header_t *a = lbm_dec_array_r(args[0]); | |
| 204 | 21 | lbm_array_header_t *b = lbm_dec_array_r(args[1]); | |
| 205 | 21 | uint32_t *ap = (uint32_t*)a->data; | |
| 206 | 21 | uint32_t *bp = (uint32_t*)b->data; | |
| 207 | 21 | uint32_t alen = crypto_bn_normalize_len(ap, (uint32_t)(a->size / sizeof(uint32_t))); | |
| 208 | 21 | uint32_t blen = crypto_bn_normalize_len(bp, (uint32_t)(b->size / sizeof(uint32_t))); | |
| 209 | |||
| 210 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 15 times.
|
21 | if (crypto_bn_cmp(ap, alen, bp, blen) < 0) { |
| 211 | // a < b: q = 0, r = a | ||
| 212 | lbm_value qv, rv; | ||
| 213 |
2/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
|
12 | if (lbm_heap_allocate_array(&qv, sizeof(uint32_t)) && |
| 214 | 6 | lbm_heap_allocate_array(&rv, alen * sizeof(uint32_t))) { | |
| 215 | 6 | ((uint32_t*)lbm_dec_array_r(qv)->data)[0] = 0; | |
| 216 | 6 | memcpy(lbm_dec_array_r(rv)->data, ap, alen * sizeof(uint32_t)); | |
| 217 | 6 | res = lbm_cons(qv, rv); | |
| 218 | } else { | ||
| 219 | ✗ | res = ENC_SYM_MERROR; | |
| 220 | } | ||
| 221 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
|
15 | } else if (blen == 1) { |
| 222 | lbm_value qv, rv; | ||
| 223 |
2/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
24 | if (lbm_heap_allocate_array(&qv, alen * sizeof(uint32_t)) && |
| 224 | 12 | lbm_heap_allocate_array(&rv, sizeof(uint32_t))) { | |
| 225 | 12 | crypto_bn_divmod_single(ap, alen, bp[0], | |
| 226 | 12 | (uint32_t*)lbm_dec_array_r(qv)->data, | |
| 227 | 12 | (uint32_t*)lbm_dec_array_r(rv)->data); | |
| 228 | 12 | res = lbm_cons(qv, rv); | |
| 229 | } else { | ||
| 230 | ✗ | res = ENC_SYM_MERROR; | |
| 231 | } | ||
| 232 | } else { | ||
| 233 | // General case: allocate q, r, and scratch | ||
| 234 | lbm_value qv, rv, s_an, s_bn; | ||
| 235 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
|
6 | if (lbm_heap_allocate_array(&qv, alen * sizeof(uint32_t)) && |
| 236 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | lbm_heap_allocate_array(&rv, blen * sizeof(uint32_t)) && |
| 237 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | lbm_heap_allocate_array(&s_an, (alen + 1) * sizeof(uint32_t)) && |
| 238 | 3 | lbm_heap_allocate_array(&s_bn, blen * sizeof(uint32_t))) { | |
| 239 | 3 | crypto_bn_divmod(ap, alen, bp, blen, | |
| 240 | 3 | (uint32_t*)lbm_dec_array_r(qv)->data, | |
| 241 | 3 | (uint32_t*)lbm_dec_array_r(rv)->data, | |
| 242 | 3 | (uint32_t*)lbm_dec_array_r(s_an)->data, | |
| 243 | 3 | (uint32_t*)lbm_dec_array_r(s_bn)->data); | |
| 244 | 3 | res = lbm_cons(qv, rv); | |
| 245 | } else { | ||
| 246 | ✗ | res = ENC_SYM_MERROR; | |
| 247 | } | ||
| 248 | } | ||
| 249 | } | ||
| 250 | 21 | return res; | |
| 251 | } | ||
| 252 | |||
| 253 | // //////////////////////////////////////////////////////////// | ||
| 254 | // Extensions | ||
| 255 | |||
| 256 | 21 | static lbm_value ext_bn_cmp(lbm_value *args, lbm_uint argn) { | |
| 257 | 21 | lbm_value r = ENC_SYM_TERROR; | |
| 258 |
3/6✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 21 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 21 times.
✗ Branch 7 not taken.
|
21 | if (argn == 2 && lbm_is_array_r(args[0]) && lbm_is_array_r(args[1])) { |
| 259 | 21 | lbm_array_header_t *a = lbm_dec_array_r(args[0]); | |
| 260 | 21 | lbm_array_header_t *b = lbm_dec_array_r(args[1]); | |
| 261 | 21 | uint32_t *ap = (uint32_t*)a->data; | |
| 262 | 21 | uint32_t *bp = (uint32_t*)b->data; | |
| 263 | 21 | uint32_t alen = crypto_bn_normalize_len(ap, (uint32_t)(a->size / sizeof(uint32_t))); | |
| 264 | 21 | uint32_t blen = crypto_bn_normalize_len(bp, (uint32_t)(b->size / sizeof(uint32_t))); | |
| 265 | 21 | r = lbm_enc_i(crypto_bn_cmp(ap, alen, bp, blen)); | |
| 266 | } | ||
| 267 | 21 | return r; | |
| 268 | } | ||
| 269 | |||
| 270 | 15 | static lbm_value ext_bn_add(lbm_value *args, lbm_uint argn) { | |
| 271 | 15 | lbm_value r = ENC_SYM_TERROR; | |
| 272 |
3/6✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
|
15 | if (argn == 2 && lbm_is_array_r(args[0]) && lbm_is_array_r(args[1])) { |
| 273 | 15 | lbm_array_header_t *a = lbm_dec_array_r(args[0]); | |
| 274 | 15 | lbm_array_header_t *b = lbm_dec_array_r(args[1]); | |
| 275 | 15 | uint32_t *ap = (uint32_t*)a->data; | |
| 276 | 15 | uint32_t *bp = (uint32_t*)b->data; | |
| 277 | 15 | uint32_t alen = crypto_bn_normalize_len(ap, (uint32_t)(a->size / sizeof(uint32_t))); | |
| 278 | 15 | uint32_t blen = crypto_bn_normalize_len(bp, (uint32_t)(b->size / sizeof(uint32_t))); | |
| 279 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (alen < blen) { // ensure alen >= blen |
| 280 | ✗ | uint32_t *tmp = ap; ap = bp; bp = tmp; | |
| 281 | ✗ | uint32_t tl = alen; alen = blen; blen = tl; | |
| 282 | } | ||
| 283 | lbm_value res; | ||
| 284 |
1/2✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
|
15 | if (lbm_heap_allocate_array(&res, (alen + 1) * sizeof(uint32_t))) { |
| 285 | 15 | lbm_array_header_t *arr = lbm_dec_array_r(res); | |
| 286 | 15 | crypto_bn_add(ap, alen, bp, blen, (uint32_t*)arr->data); | |
| 287 | 15 | r = res; | |
| 288 | } else { | ||
| 289 | ✗ | r = ENC_SYM_MERROR; | |
| 290 | } | ||
| 291 | } | ||
| 292 | 15 | return r; | |
| 293 | } | ||
| 294 | |||
| 295 | 9 | static lbm_value ext_bn_sub(lbm_value *args, lbm_uint argn) { | |
| 296 | 9 | lbm_value r = ENC_SYM_TERROR; | |
| 297 |
3/6✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
|
9 | if (argn == 2 && lbm_is_array_r(args[0]) && lbm_is_array_r(args[1])) { |
| 298 | 9 | lbm_array_header_t *a = lbm_dec_array_r(args[0]); | |
| 299 | 9 | lbm_array_header_t *b = lbm_dec_array_r(args[1]); | |
| 300 | 9 | uint32_t *ap = (uint32_t*)a->data; | |
| 301 | 9 | uint32_t *bp = (uint32_t*)b->data; | |
| 302 | 9 | uint32_t alen = crypto_bn_normalize_len(ap, (uint32_t)(a->size / sizeof(uint32_t))); | |
| 303 | 9 | uint32_t blen = crypto_bn_normalize_len(bp, (uint32_t)(b->size / sizeof(uint32_t))); | |
| 304 | 9 | r = ENC_SYM_EERROR; | |
| 305 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3 times.
|
9 | if (crypto_bn_cmp(ap, alen, bp, blen) >= 0) { // a >= b required |
| 306 | lbm_value res; | ||
| 307 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | if (lbm_heap_allocate_array(&res, alen * sizeof(uint32_t))) { |
| 308 | 6 | lbm_array_header_t *arr = lbm_dec_array_r(res); | |
| 309 | 6 | crypto_bn_sub(ap, alen, bp, blen, (uint32_t*)arr->data); | |
| 310 | 6 | r = res; | |
| 311 | } else { | ||
| 312 | ✗ | r = ENC_SYM_MERROR; | |
| 313 | } | ||
| 314 | } | ||
| 315 | } | ||
| 316 | 9 | return r; | |
| 317 | } | ||
| 318 | |||
| 319 | 12 | static lbm_value ext_bn_mul(lbm_value *args, lbm_uint argn) { | |
| 320 | 12 | lbm_value r = ENC_SYM_TERROR; | |
| 321 |
3/6✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 12 times.
✗ Branch 7 not taken.
|
12 | if (argn == 2 && lbm_is_array_r(args[0]) && lbm_is_array_r(args[1])) { |
| 322 | 12 | lbm_array_header_t *a = lbm_dec_array_r(args[0]); | |
| 323 | 12 | lbm_array_header_t *b = lbm_dec_array_r(args[1]); | |
| 324 | 12 | uint32_t *ap = (uint32_t*)a->data; | |
| 325 | 12 | uint32_t *bp = (uint32_t*)b->data; | |
| 326 | 12 | uint32_t alen = crypto_bn_normalize_len(ap, (uint32_t)(a->size / sizeof(uint32_t))); | |
| 327 | 12 | uint32_t blen = crypto_bn_normalize_len(bp, (uint32_t)(b->size / sizeof(uint32_t))); | |
| 328 | lbm_value res; | ||
| 329 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | if (lbm_heap_allocate_array(&res, (alen + blen) * sizeof(uint32_t))) { |
| 330 | 12 | lbm_array_header_t *arr = lbm_dec_array_r(res); | |
| 331 | 12 | crypto_bn_mul(ap, alen, bp, blen, (uint32_t*)arr->data); | |
| 332 | 12 | r = res; | |
| 333 | } else { | ||
| 334 | ✗ | r = ENC_SYM_MERROR; | |
| 335 | } | ||
| 336 | } | ||
| 337 | 12 | return r; | |
| 338 | } | ||
| 339 | |||
| 340 | // Convert bignum to u32. Returns EERROR if bignum is too large to fit. | ||
| 341 | 99 | static lbm_value ext_bn_to_u32(lbm_value *args, lbm_uint argn) { | |
| 342 | 99 | lbm_value r = ENC_SYM_TERROR; | |
| 343 |
2/4✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 99 times.
✗ Branch 4 not taken.
|
99 | if (argn == 1 && lbm_is_array_r(args[0])) { |
| 344 | 99 | lbm_array_header_t *arr = lbm_dec_array_r(args[0]); | |
| 345 | 99 | uint32_t *ap = (uint32_t*)arr->data; | |
| 346 | 99 | uint32_t alen = crypto_bn_normalize_len(ap, (uint32_t)(arr->size / sizeof(uint32_t))); | |
| 347 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 3 times.
|
99 | if (alen == 1) { |
| 348 | 96 | r = lbm_enc_u32(ap[0]); | |
| 349 | } else { | ||
| 350 | 3 | r = ENC_SYM_EERROR; | |
| 351 | } | ||
| 352 | } | ||
| 353 | 99 | return r; | |
| 354 | } | ||
| 355 | |||
| 356 | //convert u32 to bignum | ||
| 357 | 210 | static lbm_value ext_bn_from_u32(lbm_value *args, lbm_uint argn) { | |
| 358 | 210 | lbm_value r = ENC_SYM_TERROR; | |
| 359 |
2/4✓ Branch 0 taken 210 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 210 times.
✗ Branch 4 not taken.
|
210 | if (argn == 1 && lbm_is_number(args[0])) { |
| 360 | lbm_value bn; | ||
| 361 |
1/2✓ Branch 1 taken 210 times.
✗ Branch 2 not taken.
|
210 | if (lbm_heap_allocate_array(&bn, sizeof(uint32_t))) { |
| 362 | // dec_array_r is a bit misleading as we are writing. | ||
| 363 | // But it is ok and the _r variant should be slightly cheaper. | ||
| 364 | // We know it is writable because we just allocated it! | ||
| 365 | 210 | lbm_array_header_t *arr = lbm_dec_array_r(bn); | |
| 366 | 210 | uint32_t *vp = (uint32_t*)&arr->data[0]; | |
| 367 | 210 | *vp = lbm_dec_as_u32(args[0]); | |
| 368 | 210 | r = bn; | |
| 369 | } else { | ||
| 370 | ✗ | r = ENC_SYM_MERROR; | |
| 371 | } | ||
| 372 | } | ||
| 373 | 210 | return r; | |
| 374 | } | ||
| 375 | |||
| 376 | 36 | static lbm_value ext_bn_from_bytes(lbm_value *args, lbm_uint argn) { | |
| 377 | 36 | lbm_value r = ENC_SYM_TERROR; | |
| 378 |
2/4✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
|
36 | if (argn == 1 && lbm_is_array_r(args[0])) { |
| 379 | 36 | lbm_array_header_t *byte_array = lbm_dec_array_r(args[0]); | |
| 380 | 36 | r = ENC_SYM_EERROR; | |
| 381 |
2/2✓ Branch 0 taken 33 times.
✓ Branch 1 taken 3 times.
|
36 | if (byte_array->size % sizeof(uint32_t) == 0) { |
| 382 | 33 | uint32_t num_bytes = (uint32_t)byte_array->size; | |
| 383 | lbm_value bn ; | ||
| 384 |
1/2✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
|
33 | if (lbm_heap_allocate_array(&bn, num_bytes)) { |
| 385 | 33 | lbm_array_header_t *arr = lbm_dec_array_r(bn); | |
| 386 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 33 times.
|
249 | for (uint32_t i = 0; i < num_bytes ; i ++) { |
| 387 | 216 | ((uint8_t*)arr->data)[i] = ((uint8_t*)byte_array->data)[num_bytes-i-1]; | |
| 388 | } | ||
| 389 | 33 | r = bn; | |
| 390 | } else { | ||
| 391 | ✗ | r = ENC_SYM_MERROR; | |
| 392 | } | ||
| 393 | } | ||
| 394 | } | ||
| 395 | 36 | return r; | |
| 396 | } | ||
| 397 | |||
| 398 | |||
| 399 | 21 | static lbm_value ext_bn_to_bytes(lbm_value *args, lbm_uint argn) { | |
| 400 | 21 | lbm_value r = ENC_SYM_TERROR; | |
| 401 |
2/4✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 21 times.
✗ Branch 4 not taken.
|
21 | if (argn == 1 && lbm_is_array_r(args[0])) { |
| 402 | 21 | lbm_array_header_t *bn = lbm_dec_array_r(args[0]); | |
| 403 | 21 | uint32_t num_bytes = (uint32_t)bn->size; | |
| 404 | lbm_value byte_array; | ||
| 405 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | if (lbm_heap_allocate_array(&byte_array, num_bytes)) { |
| 406 | 21 | lbm_array_header_t *arr = lbm_dec_array_r(byte_array); | |
| 407 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 21 times.
|
165 | for (uint32_t i = 0; i < num_bytes; i++) { |
| 408 | 144 | ((uint8_t*)arr->data)[i] = ((uint8_t*)bn->data)[num_bytes - i - 1]; | |
| 409 | } | ||
| 410 | 21 | r = byte_array; | |
| 411 | } else { | ||
| 412 | ✗ | r = ENC_SYM_MERROR; | |
| 413 | } | ||
| 414 | } | ||
| 415 | 21 | return r; | |
| 416 | } | ||
| 417 | |||
| 418 | // Allocates a bunch of memory which is bad from | ||
| 419 | // a fragmentation point of view. | ||
| 420 | // Possibly look at making defrag_mem an option. | ||
| 421 | 36 | static lbm_value ext_bn_modexp(lbm_value *args, lbm_uint argn) { | |
| 422 | 36 | lbm_value r = ENC_SYM_TERROR; | |
| 423 |
2/4✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
|
72 | if (argn == 3 && |
| 424 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
72 | lbm_is_array_r(args[0]) && |
| 425 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
72 | lbm_is_array_r(args[1]) && |
| 426 | 36 | lbm_is_array_r(args[2])) { | |
| 427 | 36 | lbm_array_header_t *base_arr = lbm_dec_array_r(args[0]); | |
| 428 | 36 | lbm_array_header_t *exp_arr = lbm_dec_array_r(args[1]); | |
| 429 | 36 | lbm_array_header_t *n_arr = lbm_dec_array_r(args[2]); | |
| 430 | 36 | uint32_t *base = (uint32_t*)base_arr->data; | |
| 431 | 36 | uint32_t *exp = (uint32_t*)exp_arr->data; | |
| 432 | 36 | uint32_t *n = (uint32_t*)n_arr->data; | |
| 433 | 36 | uint32_t blen = crypto_bn_normalize_len(base, (uint32_t)(base_arr->size / sizeof(uint32_t))); | |
| 434 | 36 | uint32_t elen = crypto_bn_normalize_len(exp, (uint32_t)(exp_arr->size / sizeof(uint32_t))); | |
| 435 | 36 | uint32_t nlen = crypto_bn_normalize_len(n, (uint32_t)(n_arr->size / sizeof(uint32_t))); | |
| 436 | |||
| 437 | lbm_value result, tmp_mul, scratch_q, scratch_an, scratch_bn; | ||
| 438 | // TODO: We can allocate and free the scratchpads immediately | ||
| 439 | // using lbm_malloc and lbm_free so we dont have to wait for GC to reclaim | ||
| 440 | // these memory areas. | ||
| 441 |
2/4✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
✗ Branch 4 not taken.
|
72 | if (lbm_heap_allocate_array(&result, nlen * sizeof(uint32_t)) && |
| 442 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
72 | lbm_heap_allocate_array(&tmp_mul, 2*nlen * sizeof(uint32_t)) && |
| 443 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
72 | lbm_heap_allocate_array(&scratch_q, 2*nlen * sizeof(uint32_t)) && |
| 444 |
1/2✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
|
72 | lbm_heap_allocate_array(&scratch_an,(2*nlen + 1) * sizeof(uint32_t)) && |
| 445 | 36 | lbm_heap_allocate_array(&scratch_bn, nlen * sizeof(uint32_t))) { | |
| 446 | 36 | crypto_bn_modexp(base, blen, exp, elen, n, nlen, | |
| 447 | 36 | (uint32_t*)lbm_dec_array_r(result)->data, | |
| 448 | 36 | (uint32_t*)lbm_dec_array_r(tmp_mul)->data, | |
| 449 | 36 | (uint32_t*)lbm_dec_array_r(scratch_q)->data, | |
| 450 | 36 | (uint32_t*)lbm_dec_array_r(scratch_an)->data, | |
| 451 | 36 | (uint32_t*)lbm_dec_array_r(scratch_bn)->data); | |
| 452 | 36 | r = result; | |
| 453 | } else { | ||
| 454 | ✗ | r = ENC_SYM_MERROR; | |
| 455 | } | ||
| 456 | } | ||
| 457 | 36 | return r; | |
| 458 | } | ||
| 459 | |||
| 460 | // //////////////////////////////////////////////////////////// | ||
| 461 | // Init | ||
| 462 | // | ||
| 463 | |||
| 464 | 711 | void lbm_crypto_extensions_init(void) { | |
| 465 | 711 | lbm_add_extension("sha256-str", ext_sha256_str); | |
| 466 | 711 | lbm_add_extension("sha256", ext_sha256); | |
| 467 | |||
| 468 | // aes on blocks of 16bytes using 16 or 32 byte keys. | ||
| 469 | 711 | lbm_add_extension("aes128-enc", ext_aes128_enc); | |
| 470 | 711 | lbm_add_extension("aes128-dec", ext_aes128_dec); | |
| 471 | 711 | lbm_add_extension("aes256-enc", ext_aes256_enc); | |
| 472 | 711 | lbm_add_extension("aes256-dec", ext_aes256_dec); | |
| 473 | |||
| 474 | 711 | lbm_add_extension("bytes-to-hex", ext_bytes_to_hex); | |
| 475 | 711 | lbm_add_extension("hex-to-bytes", ext_hex_to_bytes); | |
| 476 | |||
| 477 | // Bignum library | ||
| 478 | 711 | lbm_add_extension("bn-add", ext_bn_add); | |
| 479 | 711 | lbm_add_extension("bn-cmp", ext_bn_cmp); | |
| 480 | 711 | lbm_add_extension("bn-divmod", ext_bn_divmod); | |
| 481 | 711 | lbm_add_extension("bn-from-bytes", ext_bn_from_bytes); | |
| 482 | 711 | lbm_add_extension("bn-from-u32", ext_bn_from_u32); | |
| 483 | 711 | lbm_add_extension("bn-modexp", ext_bn_modexp); | |
| 484 | 711 | lbm_add_extension("bn-mul", ext_bn_mul); | |
| 485 | 711 | lbm_add_extension("bn-sub", ext_bn_sub); | |
| 486 | 711 | lbm_add_extension("bn-to-bytes", ext_bn_to_bytes); | |
| 487 | 711 | lbm_add_extension("bn-to-u32", ext_bn_to_u32); | |
| 488 | 711 | } | |
| 489 |