1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/*
* Copyright (c) 2015 Paul Fariello <paul@fariello.eu>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#ifndef __linux__
#include <sys/endian.h>
#else
#include <endian.h>
#endif
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <sodium.h>
#include <locale.h>
#include <string.h>
#include <unistd.h>
#include "kickpass.h"
#include "safe.h"
#include "storage.h"
static uint16_t kp_storage_version = 0x0001;
#ifndef betoh16
#define betoh16 be16toh
#endif
#ifndef betoh64
#define betoh64 be64toh
#endif
#define KP_STORAGE_SALT_SIZE crypto_pwhash_scryptsalsa208sha256_SALTBYTES
#define KP_STORAGE_NONCE_SIZE crypto_aead_chacha20poly1305_NPUBBYTES
#define KP_STORAGE_HEADER_SIZE (2+2+8+8+KP_STORAGE_SALT_SIZE+KP_STORAGE_NONCE_SIZE)
struct kp_storage_header {
uint16_t version;
uint16_t sodium_version;
uint64_t opslimit;
uint64_t memlimit;
unsigned char salt[KP_STORAGE_SALT_SIZE];
unsigned char nonce[KP_STORAGE_NONCE_SIZE];
};
#define KP_STORAGE_HEADER_INIT { 0, 0, 0, 0, { 0 }, { 0 } }
static void kp_storage_header_pack(const struct kp_storage_header *,
unsigned char *);
static void kp_storage_header_unpack(struct kp_storage_header *,
const unsigned char *);
static kp_error_t kp_storage_encrypt(struct kp_ctx *,
struct kp_storage_header *,
const unsigned char *, unsigned long long,
const unsigned char *, unsigned long long,
unsigned char *, unsigned long long *);
static kp_error_t kp_storage_decrypt(struct kp_ctx *,
struct kp_storage_header *,
const unsigned char *, unsigned long long,
unsigned char *, unsigned long long *,
const unsigned char *, unsigned long long);
#define READ_HEADER(s, packed, field) do {\
memcpy(&(field), (packed), (s)/8);\
(field) = betoh##s(field);\
(packed) = (packed) + (s)/8;\
} while(0)
#define WRITE_HEADER(s, packed, field) do {\
memcpy((packed), &(field), (s)/8);\
*(uint##s##_t *)(packed) = htobe##s(*(uint##s##_t *)(packed));\
(packed) = (packed) + (s)/8;\
} while(0)
static void
kp_storage_header_pack(const struct kp_storage_header *header,
unsigned char *packed)
{
assert(header);
assert(packed);
WRITE_HEADER(16, packed, header->version);
WRITE_HEADER(16, packed, header->sodium_version);
WRITE_HEADER(64, packed, header->opslimit);
WRITE_HEADER(64, packed, header->memlimit);
memcpy(packed, header->salt, KP_STORAGE_SALT_SIZE);
packed = packed + KP_STORAGE_SALT_SIZE;
memcpy(packed, header->nonce, KP_STORAGE_NONCE_SIZE);
}
static void
kp_storage_header_unpack(struct kp_storage_header *header,
const unsigned char *packed)
{
assert(header);
assert(packed);
READ_HEADER(16, packed, header->version);
READ_HEADER(16, packed, header->sodium_version);
READ_HEADER(64, packed, header->opslimit);
READ_HEADER(64, packed, header->memlimit);
memcpy(header->salt, packed, KP_STORAGE_SALT_SIZE);
packed = packed + KP_STORAGE_SALT_SIZE;
memcpy(header->nonce, packed, KP_STORAGE_NONCE_SIZE);
}
static kp_error_t
kp_storage_encrypt(struct kp_ctx *ctx, struct kp_storage_header *header,
const unsigned char *packed_header, unsigned long long header_size,
const unsigned char *plain, unsigned long long plain_size,
unsigned char *cipher, unsigned long long *cipher_size)
{
unsigned char key[crypto_aead_chacha20poly1305_KEYBYTES];
if (crypto_pwhash_scryptsalsa208sha256(key,
crypto_aead_chacha20poly1305_KEYBYTES,
ctx->password, strlen(ctx->password),
header->salt, header->opslimit, header->memlimit) != 0) {
errno = ENOMEM;
return KP_ERRNO;
}
if (crypto_aead_chacha20poly1305_encrypt(cipher, cipher_size,
plain, plain_size,
packed_header, header_size,
NULL, header->nonce, key) != 0) {
return KP_EENCRYPT;
}
return KP_SUCCESS;
}
static kp_error_t
kp_storage_decrypt(struct kp_ctx *ctx, struct kp_storage_header *header,
const unsigned char *packed_header, unsigned long long header_size,
unsigned char *plain, unsigned long long *plain_size,
const unsigned char *cipher, unsigned long long cipher_size)
{
unsigned char key[crypto_aead_chacha20poly1305_KEYBYTES];
if (crypto_pwhash_scryptsalsa208sha256(key,
crypto_aead_chacha20poly1305_KEYBYTES,
ctx->password, strlen(ctx->password),
header->salt, header->opslimit, header->memlimit) != 0) {
errno = ENOMEM;
return KP_ERRNO;
}
if (crypto_aead_chacha20poly1305_decrypt(plain, plain_size,
NULL, cipher, cipher_size,
packed_header, header_size,
header->nonce, key) != 0) {
return KP_EDECRYPT;
}
return KP_SUCCESS;
}
kp_error_t
kp_storage_save(struct kp_ctx *ctx, struct kp_safe *safe)
{
kp_error_t ret = KP_SUCCESS;
unsigned char *cipher = NULL, *plain = NULL;
unsigned long long cipher_size, plain_size;
struct kp_storage_header header = KP_STORAGE_HEADER_INIT;
unsigned char packed_header[KP_STORAGE_HEADER_SIZE];
char path[PATH_MAX];
size_t password_len, metadata_len;
assert(ctx);
assert(safe);
assert(safe->open == true);
password_len = strlen(safe->password);
assert(password_len < KP_PASSWORD_MAX_LEN);
metadata_len = strlen(safe->metadata);
assert(metadata_len < KP_METADATA_MAX_LEN);
/* Ensure we are at the beginning of the safe */
if (lseek(safe->cipher, 0, SEEK_SET) != 0) {
return errno;
}
if (ftruncate(safe->cipher, 0) != 0) {
return errno;
}
if ((ret = kp_safe_get_path(ctx, safe, path, PATH_MAX)) != KP_SUCCESS) {
return ret;
}
/* construct full plain */
/* plain is password + '\0' + metadata + '\0' */
plain_size = password_len + metadata_len + 2;
plain = sodium_malloc(plain_size);
strncpy((char *)plain, (char *)safe->password, password_len);
plain[password_len] = '\0';
strncpy((char *)&plain[password_len+1], (char *)safe->metadata, metadata_len);
plain[plain_size-1] = '\0';
/* alloc cipher to max size */
cipher = malloc(plain_size+crypto_aead_chacha20poly1305_ABYTES);
if (!cipher) {
ret = ENOMEM;
goto out;
}
header.version = kp_storage_version;
header.sodium_version = SODIUM_LIBRARY_VERSION_MAJOR << 8 |
SODIUM_LIBRARY_VERSION_MINOR;
header.opslimit = ctx->cfg.opslimit;
header.memlimit = ctx->cfg.memlimit;
randombytes_buf(header.salt, KP_STORAGE_SALT_SIZE);
randombytes_buf(header.nonce, KP_STORAGE_NONCE_SIZE);
kp_storage_header_pack(&header, packed_header);
if ((ret = kp_storage_encrypt(ctx, &header,
packed_header, KP_STORAGE_HEADER_SIZE,
plain, plain_size,
cipher, &cipher_size))
!= KP_SUCCESS) {
goto out;
}
if (write(safe->cipher, packed_header, KP_STORAGE_HEADER_SIZE)
< KP_STORAGE_HEADER_SIZE) {
ret = errno;
goto out;
}
if (write(safe->cipher, cipher, cipher_size)
< cipher_size) {
ret = errno;
goto out;
}
out:
sodium_free(plain);
free(cipher);
return ret;
}
kp_error_t
kp_storage_open(struct kp_ctx *ctx, struct kp_safe *safe)
{
kp_error_t ret = KP_SUCCESS;
unsigned char *cipher = NULL, *plain = NULL;
unsigned long long cipher_size, plain_size;
struct kp_storage_header header = KP_STORAGE_HEADER_INIT;
unsigned char packed_header[KP_STORAGE_HEADER_SIZE];
size_t password_len;
assert(ctx);
assert(safe);
assert(safe->open == false);
errno = 0;
if (read(safe->cipher, packed_header, KP_STORAGE_HEADER_SIZE)
!= KP_STORAGE_HEADER_SIZE) {
if (errno != 0) {
ret = errno;
} else {
ret = KP_INVALID_STORAGE;
}
goto out;
}
kp_storage_header_unpack(&header, packed_header);
/* alloc plain to max size */
plain = sodium_malloc(KP_PLAIN_MAX_SIZE);
/* alloc cipher to max size */
cipher = malloc(KP_PLAIN_MAX_SIZE+crypto_aead_chacha20poly1305_ABYTES);
cipher_size = read(safe->cipher, cipher,
KP_PLAIN_MAX_SIZE+crypto_aead_chacha20poly1305_ABYTES);
if (cipher_size <= crypto_aead_chacha20poly1305_ABYTES) {
ret = KP_INVALID_STORAGE;
goto out;
}
if (cipher_size - crypto_aead_chacha20poly1305_ABYTES
> KP_PLAIN_MAX_SIZE) {
ret = ENOMEM;
goto out;
}
if ((ret = kp_storage_decrypt(ctx, &header,
packed_header, KP_STORAGE_HEADER_SIZE,
plain, &plain_size,
cipher, cipher_size))
!= KP_SUCCESS) {
goto out;
}
strncpy((char *)safe->password, (char *)plain, KP_PASSWORD_MAX_LEN);
/* ensure null termination */
safe->password[KP_PASSWORD_MAX_LEN-1] = '\0';
password_len = strlen((char *)safe->password);
strncpy((char *)safe->metadata, (char *)&plain[password_len+1], KP_METADATA_MAX_LEN);
/* ensure null termination */
safe->metadata[KP_METADATA_MAX_LEN-1] = '\0';
safe->open = true;
out:
sodium_free(plain);
free(cipher);
return ret;
}
|