-
-int tools_read_mk(const char *file, char **key, int keysize)
-{
- int fd;
-
- if (!keysize || !key)
- return -EINVAL;
-
- *key = crypt_safe_alloc(keysize);
- if (!*key)
- return -ENOMEM;
-
- fd = open(file, O_RDONLY);
- if (fd == -1) {
- log_err(_("Cannot read keyfile %s."), file);
- goto fail;
- }
-
- if (read_buffer(fd, *key, keysize) != keysize) {
- log_err(_("Cannot read %d bytes from keyfile %s."), keysize, file);
- close(fd);
- goto fail;
- }
- close(fd);
- return 0;
-fail:
- crypt_safe_free(*key);
- *key = NULL;
- return -EINVAL;
-}
-
-int tools_write_mk(const char *file, const char *key, int keysize)
-{
- int fd, r = -EINVAL;
-
- fd = open(file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
- if (fd < 0) {
- log_err(_("Cannot open keyfile %s for write."), file);
- return r;
- }
-
- if (write_buffer(fd, key, keysize) == keysize)
- r = 0;
- else
- log_err(_("Cannot write to keyfile %s."), file);
-
- close(fd);
- return r;
-}