1 /*
2 * Copyright (c) 2014-2018, Andrew Romanenko <melanhit@gmail.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <errno.h>
35
36 #include "../akmos.h"
37 #include "cli.h"
38
39 typedef enum {
40 AKMOS_CLI_DIGEST,
41 AKMOS_CLI_CIPHER_ENC,
42 AKMOS_CLI_CIPHER_DEC,
43 AKMOS_CLI_MAC,
44 AKMOS_CLI_BASE64,
45 AKMOS_CLI_HELP,
46 AKMOS_CLI_UNKNOWN
47 } akmos_cli_id;
48
amalloc(uint8_t ** ptr,size_t len)49 int amalloc(uint8_t **ptr, size_t len)
50 {
51 *ptr = NULL;
52
53 *ptr = malloc(len);
54 if(*ptr == NULL) {
55 fprintf(stderr, "%s\n", strerror(errno));
56 return errno;
57 }
58
59 return EXIT_SUCCESS;
60 }
61
akmos_cli_help()62 int akmos_cli_help() {
63 printf("Usage: akmos <command> <options>\n"
64 "Available commands:\n"
65 " dgst - make digest (hash)\n"
66 " base64 - binary to text\n"
67 " enc - encrypt\n"
68 " dec - decrypt\n"
69 " mac - compute MAC\n"
70 " help - print help\n");
71
72 return EXIT_SUCCESS;
73 }
74
akmos_cli_version()75 int akmos_cli_version()
76 {
77 const char *buf;
78
79 buf = akmos_version();
80 printf("%s\n", buf);
81
82 return EXIT_SUCCESS;
83 }
84
main(int argc,char ** argv)85 int main(int argc, char **argv)
86 {
87 int opt;
88
89 if(argc < 2)
90 return akmos_cli_help();
91
92 if(strcmp(argv[1], "dgst") == 0)
93 opt = AKMOS_CLI_DIGEST;
94 else if(strcmp(argv[1], "enc") == 0)
95 opt = AKMOS_CLI_CIPHER_ENC;
96 else if(strcmp(argv[1], "dec") == 0)
97 opt = AKMOS_CLI_CIPHER_DEC;
98 else if(strcmp(argv[1], "mac") == 0)
99 opt = AKMOS_CLI_MAC;
100 else if(strcmp(argv[1], "base64") == 0)
101 opt = AKMOS_CLI_BASE64;
102 else if(strcmp(argv[1], "help") == 0)
103 opt = AKMOS_CLI_HELP;
104 else
105 opt = AKMOS_CLI_UNKNOWN;
106
107 switch(opt) {
108 case AKMOS_CLI_DIGEST:
109 return akmos_cli_digest(--argc, ++argv);
110
111 case AKMOS_CLI_CIPHER_ENC:
112 return akmos_cli_cipher(--argc, ++argv, AKMOS_MODE_ENCRYPT);
113
114 case AKMOS_CLI_CIPHER_DEC:
115 return akmos_cli_cipher(--argc, ++argv, AKMOS_MODE_DECRYPT);
116
117 case AKMOS_CLI_MAC:
118 return akmos_cli_mac(--argc, ++argv);
119
120 case AKMOS_CLI_BASE64:
121 return akmos_cli_base64(--argc, ++argv);
122
123 case AKMOS_CLI_HELP:
124 return akmos_cli_help();
125
126 case AKMOS_CLI_UNKNOWN:
127 fprintf(stderr, "Unknown action '%s'\n", argv[1]);
128 return EXIT_FAILURE;
129 }
130
131 return EXIT_SUCCESS;
132 }
133