mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 10:38:33 -04:00
refactor: Allocations now use the temp_allocator more frequently.
This commit is contained in:
1
cli.odin
1
cli.odin
@@ -75,6 +75,7 @@ parse_args :: proc(args: []string, out: io.Stream, err: io.Stream) -> (cmd: Comm
|
|||||||
cmd.flags = make(map[string]string)
|
cmd.flags = make(map[string]string)
|
||||||
cmd.bool_set = make(map[string]bool)
|
cmd.bool_set = make(map[string]bool)
|
||||||
|
|
||||||
|
// TODO: Optimize loop?
|
||||||
i := 2
|
i := 2
|
||||||
for i < len(args) {
|
for i < len(args) {
|
||||||
arg := args[i]
|
arg := args[i]
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ cmd_sync :: proc(cmd: ^Command) {
|
|||||||
|
|
||||||
render_table(cmd.out, headers, table_rows[:])
|
render_table(cmd.out, headers, table_rows[:])
|
||||||
} else {
|
} else {
|
||||||
data, marshal_err := json.marshal(results[:])
|
data, marshal_err := json.marshal(results[:], allocator = context.temp_allocator)
|
||||||
if marshal_err != nil {
|
if marshal_err != nil {
|
||||||
fmt.wprintf(cmd.err, "Error marshaling JSON: %v\n", marshal_err, flush = false)
|
fmt.wprintf(cmd.err, "Error marshaling JSON: %v\n", marshal_err, flush = false)
|
||||||
return
|
return
|
||||||
|
|||||||
11
config.odin
11
config.odin
@@ -84,9 +84,9 @@ save_config :: proc(cfg: Config, force: bool = false) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if os.exists(cfg.config_path) && !force {
|
if os.exists(cfg.config_path) && !force {
|
||||||
info, stat_err := os.stat(cfg.config_path, context.allocator)
|
info, stat_err := os.stat(cfg.config_path, context.temp_allocator)
|
||||||
if stat_err == nil {
|
if stat_err == nil {
|
||||||
defer os.file_info_delete(info, context.allocator)
|
defer os.file_info_delete(info, context.temp_allocator)
|
||||||
if info.size > 0 {
|
if info.size > 0 {
|
||||||
fmt.println("Config file already exists. Run again with --force to reinitialize.")
|
fmt.println("Config file already exists. Run again with --force to reinitialize.")
|
||||||
return false
|
return false
|
||||||
@@ -94,12 +94,15 @@ save_config :: proc(cfg: Config, force: bool = false) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data, marshal_err := json.marshal(cfg, {pretty = true, use_spaces = true, spaces = 2})
|
data, marshal_err := json.marshal(
|
||||||
|
cfg,
|
||||||
|
{pretty = true, use_spaces = true, spaces = 2},
|
||||||
|
context.temp_allocator,
|
||||||
|
)
|
||||||
if marshal_err != nil {
|
if marshal_err != nil {
|
||||||
fmt.printf("Error marshaling config: %v\n", marshal_err)
|
fmt.printf("Error marshaling config: %v\n", marshal_err)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer delete(data)
|
|
||||||
|
|
||||||
write_err := os.write_entire_file(cfg.config_path, data)
|
write_err := os.write_entire_file(cfg.config_path, data)
|
||||||
if write_err != nil {
|
if write_err != nil {
|
||||||
|
|||||||
37
crypto.odin
37
crypto.odin
@@ -1,5 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "core:crypto/_fiat/field_p384r1"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
@@ -33,12 +34,12 @@ init_sodium :: proc "contextless" () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Optimize performance
|
||||||
encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: bool) {
|
encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: bool) {
|
||||||
x25519_pairs, pairs_ok := ssh_to_x25519(keys)
|
x25519_pairs, pairs_ok := ssh_to_x25519(keys, context.temp_allocator)
|
||||||
if !pairs_ok {
|
if !pairs_ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer delete(x25519_pairs)
|
|
||||||
|
|
||||||
sym_key: [CRYPTO_SECRETBOX_KEY_BYTES]u8
|
sym_key: [CRYPTO_SECRETBOX_KEY_BYTES]u8
|
||||||
randombytes_buf(&sym_key[0], CRYPTO_SECRETBOX_KEY_BYTES)
|
randombytes_buf(&sym_key[0], CRYPTO_SECRETBOX_KEY_BYTES)
|
||||||
@@ -47,7 +48,7 @@ encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: b
|
|||||||
randombytes_buf(&main_nonce[0], CRYPTO_SECRETBOX_NONCE_BYTES)
|
randombytes_buf(&main_nonce[0], CRYPTO_SECRETBOX_NONCE_BYTES)
|
||||||
|
|
||||||
ct_len := len(plaintext) + CRYPTO_SECRETBOX_MAC_BYTES
|
ct_len := len(plaintext) + CRYPTO_SECRETBOX_MAC_BYTES
|
||||||
secret_ct := make([]u8, ct_len)
|
secret_ct := make([]u8, ct_len, context.temp_allocator)
|
||||||
pt_ptr: [^]u8
|
pt_ptr: [^]u8
|
||||||
if len(plaintext) > 0 {
|
if len(plaintext) > 0 {
|
||||||
pt_ptr = &plaintext[0]
|
pt_ptr = &plaintext[0]
|
||||||
@@ -66,7 +67,7 @@ encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: b
|
|||||||
}
|
}
|
||||||
|
|
||||||
num_recipients := u32(len(x25519_pairs))
|
num_recipients := u32(len(x25519_pairs))
|
||||||
entries := make([]RecipientEntry, num_recipients)
|
entries := make([]RecipientEntry, num_recipients, context.temp_allocator)
|
||||||
|
|
||||||
for i in 0 ..< len(x25519_pairs) {
|
for i in 0 ..< len(x25519_pairs) {
|
||||||
for j in 0 ..< CRYPTO_BOX_PUBLICKEY_BYTES {
|
for j in 0 ..< CRYPTO_BOX_PUBLICKEY_BYTES {
|
||||||
@@ -126,8 +127,6 @@ encrypt :: proc(plaintext: []u8, keys: []SshKeyPair) -> (ciphertext: []u8, ok: b
|
|||||||
|
|
||||||
mem.copy(&ciphertext[pos], &secret_ct[0], ct_len)
|
mem.copy(&ciphertext[pos], &secret_ct[0], ct_len)
|
||||||
|
|
||||||
delete(entries)
|
|
||||||
delete(secret_ct)
|
|
||||||
ok = true
|
ok = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -176,11 +175,10 @@ decrypt :: proc(ciphertext: []u8, keys: []SshKeyPair) -> (plaintext: []u8, ok: b
|
|||||||
enc_nonce: [CRYPTO_BOX_NONCE_BYTES]u8
|
enc_nonce: [CRYPTO_BOX_NONCE_BYTES]u8
|
||||||
enc_pub: [CRYPTO_BOX_PUBLICKEY_BYTES]u8
|
enc_pub: [CRYPTO_BOX_PUBLICKEY_BYTES]u8
|
||||||
|
|
||||||
x25519_pairs, pairs_ok := ssh_to_x25519(keys)
|
x25519_pairs, pairs_ok := ssh_to_x25519(keys, context.temp_allocator)
|
||||||
if !pairs_ok {
|
if !pairs_ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer delete(x25519_pairs)
|
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
matched_pi := 0
|
matched_pi := 0
|
||||||
@@ -272,33 +270,39 @@ decrypt :: proc(ciphertext: []u8, keys: []SshKeyPair) -> (plaintext: []u8, ok: b
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ssh_to_x25519 :: proc(keys: []SshKeyPair) -> (pairs: []X25519Keypair, ok: bool) {
|
ssh_to_x25519 :: proc(
|
||||||
|
keys: []SshKeyPair,
|
||||||
|
allocator := context.temp_allocator,
|
||||||
|
) -> (
|
||||||
|
[]X25519Keypair,
|
||||||
|
bool,
|
||||||
|
) {
|
||||||
if len(keys) == 0 {
|
if len(keys) == 0 {
|
||||||
return
|
return {}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
pairs = make([]X25519Keypair, len(keys))
|
pairs := make([]X25519Keypair, len(keys), allocator)
|
||||||
|
|
||||||
for i in 0 ..< len(keys) {
|
for i in 0 ..< len(keys) {
|
||||||
ssh_kp, parse_ok := parse_ssh_private_key(keys[i].Private)
|
ssh_kp, parse_ok := parse_ssh_private_key(keys[i].Private)
|
||||||
if !parse_ok {
|
if !parse_ok {
|
||||||
fmt.printf("Error: failed to parse SSH private key: %s\n", keys[i].Private)
|
fmt.printf("Error: failed to parse SSH private key: %s\n", keys[i].Private)
|
||||||
delete(pairs)
|
delete(pairs)
|
||||||
return
|
return pairs, false
|
||||||
}
|
}
|
||||||
|
|
||||||
ssh_pub, pub_ok := parse_ssh_public_key(keys[i].Public)
|
ssh_pub, pub_ok := parse_ssh_public_key(keys[i].Public)
|
||||||
if !pub_ok {
|
if !pub_ok {
|
||||||
fmt.printf("Error: failed to parse SSH public key: %s\n", keys[i].Public)
|
fmt.printf("Error: failed to parse SSH public key: %s\n", keys[i].Public)
|
||||||
delete(pairs)
|
delete(pairs)
|
||||||
return
|
return pairs, false
|
||||||
}
|
}
|
||||||
|
|
||||||
pk_rc := crypto_sign_ed25519_pk_to_curve25519(&pairs[i].Public[0], &ssh_pub[0])
|
pk_rc := crypto_sign_ed25519_pk_to_curve25519(&pairs[i].Public[0], &ssh_pub[0])
|
||||||
if pk_rc != 0 {
|
if pk_rc != 0 {
|
||||||
fmt.println("Error: failed to convert ed25519 public key to curve25519")
|
fmt.println("Error: failed to convert ed25519 public key to curve25519")
|
||||||
delete(pairs)
|
delete(pairs)
|
||||||
return
|
return pairs, false
|
||||||
}
|
}
|
||||||
|
|
||||||
ed25519_sk: [64]u8
|
ed25519_sk: [64]u8
|
||||||
@@ -313,11 +317,10 @@ ssh_to_x25519 :: proc(keys: []SshKeyPair) -> (pairs: []X25519Keypair, ok: bool)
|
|||||||
if sk_rc != 0 {
|
if sk_rc != 0 {
|
||||||
fmt.println("Error: failed to convert ed25519 private key to curve25519")
|
fmt.println("Error: failed to convert ed25519 private key to curve25519")
|
||||||
delete(pairs)
|
delete(pairs)
|
||||||
return
|
return pairs, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = true
|
return pairs, true
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user