Port crypt package

This commit is contained in:
2026-01-21 19:05:46 -05:00
parent a72be0393c
commit d24202f63b
7 changed files with 144 additions and 34 deletions
+38 -18
View File
@@ -12,23 +12,40 @@ to export to C.
All relevant exported libmutton functions have C functions of the same name.
Functions with multiple return values all have CGO-generated structs to store the return values.
These structs are named `<FunctionName>_return`, as per CGO.
All functions continue to perform the same basic operations, with a couple caveats:
All functions perform similar basic operations, with a couple caveats:
- Anything that would normally return a Go error now returns a null-terminated *C.char containing the error string.
These are safe to null-terminate because their values are much more predictable than other strings.
Errors are always the *first* return value, so they can always be referenced with `<result>.r0`.
- Anything that would normally return a Go string or byte slice now returns a C.PascalString struct.
- Many things that would normally require/return a Go string or byte slice now require/return a C.PascalString struct.
This is to avoid bugs with null-terminated strings.
Additionally, since comments tend to fall out of date, please rely on the [Go documentation for libmutton](https://pkg.go.dev/github.com/rwinkhart/libmutton). Documentation for the CGO bindings present in this repo only specify return values.
### Example (libmutton's global.DirInit)
### Example (decrypting and printing lines of a libmutton entry)
Build the following example with `gcc <filename> ./cmutton.a`.
```c
#include <stdio.h>
#include <string.h>
#include "cmutton.h" // import cmutton
static void read_input(const char *prompt, char *buffer, size_t size) {
printf("%s", prompt);
fflush(stdout);
if (fgets(buffer, size, stdin) == NULL) {
fprintf(stderr, "Error reading user input\n");
exit(1);
}
// remove trailing newline
buffer[strlen(buffer)-1] = '\0';
}
int main() {
char vanityPath[256];
char password[256];
read_input("Enter vanity path: ", vanityPath, sizeof(vanityPath));
read_input("Enter password: ", password, sizeof(password));
// use CGO-generated struct to get multiple return values
struct DirInit_return result = DirInit(1);
struct DecryptFileToSlice_return result = DecryptFileToSlice(GetRealPath(vanityPath), GetPascalStringFromCString(password));
// familiar error handling pattern
if (result.r0 != NULL) {
@@ -36,17 +53,19 @@ int main() {
exit(1); // result.r0 not freed since program exits
}
// print Pascal string using "%.*s" with printf and supplying both length and data
printf("Old device ID: %.*s\n", result.r1.len, result.r1.data);
for (int i = 0; i < result.r2; i++) {
// print Pascal string using "%.*s" with printf and supplying both length and data
printf("%.*s\n", result.r1[i].len, result.r1[i].data);
}
// be sure to free the data!
free(result.r1.data);
FreeArray(result.r1, result.r2);
}
```
# Progress
- [ ] age
- [ ] AllPasswordEntries(forceReage bool) error
- [ ] AllPasswordEntries(forceReage bool, rcwPassword []byte) error
- [ ] Entry(vanityPath string, timestamp int64) error
- [ ] TranslateAgeTimestamp(timestamp *int64) uint8
- [ ] clip
@@ -65,25 +84,26 @@ int main() {
- [ ] EntryIsNotEmpty(entryData []string) bool
- [ ] EntryRefresh(oldRCWPassword, newRCWPassword []byte, removeOldDir bool) error
- [ ] GenTOTP(secret string, time time.Time) (string, error)
- [ ] GetOldEntryData(realPath string, field int) ([]string, error)
- [ ] GetOldEntryData(realPath string, field int, rcwPassword []byte) ([]string, error)
- [ ] LibmuttonInit(inputCB func(prompt string) string, rcwPassword []byte, ...) error
- [ ] RCWSanityCheckGen(password []byte) error
- [ ] WriteEntry(realPath string, decSlice []string, passwordIsNew bool) error
- [ ] WriteEntry(realPath string, decSlice []string, passwordIsNew bool, rcwPassword []byte) error
- [ ] crypt
- [ ] DecryptFileToSlice(realPath string) ([]string, error)
- [ ] EncryptBytes(decBytes []byte) []byte
- [ ] RCWDArgument()
- [ ] VAR: Daemonize bool
- [ ] VAR: RetryPassword bool
- [ ] Address TODOs
- [X] ~~VAR: RetryPassword bool~~ (RCWD not supported)
- [X] DecryptFileToSlice(realPath string) ([]string, error)
- [X] EncryptBytes(decBytes []byte) []byte
- [X] ~~RCWDArgument()~~ (RCWD not supported)
- [ ] global
- [ ] Address TODOs
- [X] ~~VAR (CB func): GetPassword~~ (RCWD not supported)
- [X] DirInit(preserveOldCfgDir bool) (string, error)
- [ ] ~~GenDeviceIDList() ([]fs.DirEntry, error)~~ (not for use outside of libmutton)
- [X] ~~GenDeviceIDList() ([]fs.DirEntry, error)~~ (not for use outside of libmutton)
- [X] GetCurrentDeviceID() (string, error)
- [X] GetRealAgePath(vanityPath string) string
- [X] GetRealPath(vanityPath string) string
- [ ] ~~GetSysProcAttr() *syscall.SysProcAttr~~ (not for use outside of libmutton)
- [X] ~~GetSysProcAttr() *syscall.SysProcAttr~~ (not for use outside of libmutton)
- [X] GetVanityPath(realPath string) string
- [ ] VAR (CB func): GetPassword
- [ ] syncclient
- [ ] AddFolderRemote(vanityPath string) error
- [ ] GenDeviceID(oldDeviceID, prefix string) (string, string, bool, error)