sizes.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include "tomcrypt.h"
  4. #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
  5. #include <libgen.h>
  6. #else
  7. #define basename(x) x
  8. #endif
  9. /**
  10. @file demo_crypt_sizes.c
  11. Demo how to get various sizes to dynamic languages
  12. like Python - Larry Bugbee, February 2013
  13. */
  14. static void s_print_line(const char* cmd, const char* desc)
  15. {
  16. printf(" %-16s - %s\n", cmd, desc);
  17. }
  18. int main(int argc, char **argv)
  19. {
  20. if (argc == 1) {
  21. /* given a specific size name, get and print its size */
  22. char name[] = "ltc_hash_descriptor";
  23. unsigned int size;
  24. char *sizes_list;
  25. unsigned int sizes_list_len;
  26. if (crypt_get_size(name, &size) != 0) exit(EXIT_FAILURE);
  27. printf("\n size of '%s' is %u \n\n", name, size);
  28. /* get and print the length of the names (and sizes) list */
  29. if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  30. printf(" need to allocate %u bytes \n\n", sizes_list_len);
  31. /* get and print the names (and sizes) list */
  32. if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
  33. if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  34. printf(" supported sizes:\n\n%s\n\n", sizes_list);
  35. free(sizes_list);
  36. } else if (argc == 2) {
  37. if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
  38. char* base = strdup(basename(argv[0]));
  39. printf("Usage: %s [-a] [-s name]\n\n", base);
  40. s_print_line("<no argument>", "The old behavior of the demo");
  41. s_print_line("-a", "Only lists all sizes");
  42. s_print_line("-s name", "List a single size given as argument");
  43. s_print_line("-h", "The help you're looking at");
  44. free(base);
  45. } else if (strcmp(argv[1], "-a") == 0) {
  46. char *sizes_list;
  47. unsigned int sizes_list_len;
  48. /* get and print the length of the names (and sizes) list */
  49. if (crypt_list_all_sizes(NULL, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  50. /* get and print the names (and sizes) list */
  51. if ((sizes_list = malloc(sizes_list_len)) == NULL) exit(EXIT_FAILURE);
  52. if (crypt_list_all_sizes(sizes_list, &sizes_list_len) != 0) exit(EXIT_FAILURE);
  53. printf("%s\n", sizes_list);
  54. free(sizes_list);
  55. }
  56. } else if (argc == 3) {
  57. if (strcmp(argv[1], "-s") == 0) {
  58. unsigned int size;
  59. if (crypt_get_size(argv[2], &size) != 0) exit(EXIT_FAILURE);
  60. printf("%s,%u\n", argv[2], size);
  61. }
  62. }
  63. return 0;
  64. }