aboutsummaryrefslogtreecommitdiff
path: root/utils.c
blob: 1b0fcb61ee163339fa6f5b8bf33bbf9aa4c97a70 (plain)
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
/*
  This file is part of msr.

  msr is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  msr is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  License for more details.

  You should have received a copy of the GNU General Public License
  along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/

#include "utils.h"

void* catch_malloc(size_t size, const char * const err) {
  errno = 0;
  void *m = malloc(size);
  if (m == NULL || errno != 0) {
    fprintf(stderr,"Error: %s\n",err);
    exit(1);
  }
  return m;
}


void* catch_smalloc(size_t size, const char * const err) {
  errno = 0;
  void* m = sodium_malloc(size);
  if (m == NULL || errno != 0) {
    fprintf(stderr,"Error: %s\n",err);
    exit(1);
  }
  return m;
}

const char * error_to_str(const Error e) {
  switch (e) {
    case SUCCESS: return NULL;
    case E_NULL: return "Null argument.";
    case E_CHECKSUM: return "Secret key checksum failed.";
    case E_ALG_CHOICE: return "Unsupported algorithm choice.";
    case E_ALREADY_ENCRYPTED: return "Secret key already encrypted.";
    case E_ALREADY_DECRYPTED: return "Secret key already decrypted.";
    case E_KDF_FAIL: return "Internal KDF error.";
    case E_BAD_PASS: return "Incorrect password for secret key.";
    case E_VERIFY: return "Verification failed";
    case E_WRONG_PUBKEY: return "Public key ID mismatch.";
    case E_SIGN: return "Internal signing error.";
    case E_SMALL_BUFF: return "Buffer too small.";
    case E_INVALID_LEN: return "Invalid buffer length.";
    case E_B64_BAD_STR: return "Invalid characters found in string.";
    case E_PASS_MISMATCH: return "Passwords do not match.";
    case E_BAD_FILE: return "Unable to open file.";
    case E_BAD_PUBKEY_DAT: return "Invalid public key data.";
    case E_BAD_SECKEY_DAT: return "Invalid secret key data.";
    case E_ENCRYPTED: return "Secret key is encrypted and so unusable.";
    case E_INVALID_ARG: return "Invalid argument.";
    default: return "Unreachable.";
  }
}