comparison libtomcrypt/demos/constants.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children e9dba7abd939
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9 #include "tomcrypt.h"
10
11 #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
12 #include <libgen.h>
13 #else
14 #define basename(x) x
15 #endif
16
17 /**
18 @file demo_crypt_constants.c
19
20 Demo how to get various constants to dynamic languages
21 like Python
22
23 Larry Bugbee, February 2013
24 */
25
26 static void _print_line(const char* cmd, const char* desc)
27 {
28 printf(" %-16s - %s\n", cmd, desc);
29 }
30
31 int main(int argc, char **argv)
32 {
33 if (argc == 1) {
34 /* given a specific constant name, get and print its value */
35 char name[] = "CTR_COUNTER_BIG_ENDIAN";
36 int value;
37 char *names_list;
38 unsigned int names_list_len;
39
40 if (crypt_get_constant(name, &value) != 0) exit(EXIT_FAILURE);
41 printf("\n %s is %d \n\n", name, value);
42
43 /* get and print the length of the names (and values) list */
44
45 if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE);
46 printf(" need to allocate %u bytes \n\n", names_list_len);
47
48 /* get and print the names (and values) list */
49 if ((names_list = malloc(names_list_len)) == NULL) exit(EXIT_FAILURE);
50 if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE);
51 printf(" supported constants:\n\n%s\n\n", names_list);
52 free(names_list);
53 } else if (argc == 2) {
54 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
55 char* base = strdup(basename(argv[0]));
56 printf("Usage: %s [-a] [-s name]\n\n", base);
57 _print_line("<no argument>", "The old behavior of the demo");
58 _print_line("-a", "Only lists all constants");
59 _print_line("-s name", "List a single constant given as argument");
60 _print_line("-h", "The help you're looking at");
61 free(base);
62 } else if (strcmp(argv[1], "-a") == 0) {
63 char *names_list;
64 unsigned int names_list_len;
65 /* get and print the length of the names (and values) list */
66 if (crypt_list_all_constants(NULL, &names_list_len) != 0) exit(EXIT_FAILURE);
67 /* get and print the names (and values) list */
68 names_list = malloc(names_list_len);
69 if (crypt_list_all_constants(names_list, &names_list_len) != 0) exit(EXIT_FAILURE);
70 printf("%s\n", names_list);
71 }
72 } else if (argc == 3) {
73 if (strcmp(argv[1], "-s") == 0) {
74 int value;
75 if (crypt_get_constant(argv[2], &value) != 0) exit(EXIT_FAILURE);
76 printf("%s,%u\n", argv[2], value);
77 }
78 }
79
80 return 0;
81 }
82
83
84 /* ref: $Format:%D$ */
85 /* git commit: $Format:%H$ */
86 /* commit time: $Format:%ai$ */