pem-info.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. /* print all PEM related infos */
  4. #include "tomcrypt_private.h"
  5. #if defined(LTC_PEM_SSH)
  6. extern const struct blockcipher_info pem_dek_infos[];
  7. extern const unsigned long pem_dek_infos_num;
  8. extern const struct blockcipher_info ssh_ciphers[];
  9. extern const unsigned long ssh_ciphers_num;
  10. static const struct {
  11. const char *is, *should;
  12. } cipher_name_map[] = {
  13. { "", "none" },
  14. { "aes", "AES" },
  15. { "blowfish", "Blowfish" },
  16. { "c20p1305", "ChaCha20Poly1305" },
  17. { "camellia", "Camellia" },
  18. { "cast5", "CAST5" },
  19. { "chacha20", "ChaCha20" },
  20. { "3des", "3DES (EDE)" },
  21. { "des", "DES" },
  22. { "desx", "DES-X" },
  23. { "idea", "IDEA" },
  24. { "rc5", "RC5" },
  25. { "rc2", "RC2" },
  26. { "seed", "SEED" },
  27. { "serpent", "Serpent" },
  28. { "twofish", "Twofish" },
  29. };
  30. static const char *s_map_cipher(const char *name)
  31. {
  32. unsigned long n;
  33. for (n = 0; n < LTC_ARRAY_SIZE(cipher_name_map); ++n) {
  34. if (strcmp(name, cipher_name_map[n].is) == 0)
  35. return cipher_name_map[n].should;
  36. }
  37. fprintf(stderr, "Error: Can't map %s\n", name);
  38. exit(1);
  39. }
  40. static const struct {
  41. enum cipher_mode mode;
  42. const char *name;
  43. } cipher_mode_map[] = {
  44. { cm_none, "none", },
  45. { cm_cbc, "CBC", },
  46. { cm_cfb, "CFB", },
  47. { cm_cfb1, "CFB1", },
  48. { cm_cfb8, "CFB8", },
  49. { cm_ctr, "CTR", },
  50. { cm_ofb, "OFB", },
  51. { cm_stream, "STREAM", },
  52. { cm_gcm, "GCM", },
  53. };
  54. static const char *s_map_mode(enum cipher_mode mode)
  55. {
  56. size_t n;
  57. mode &= cm_modes | cm_1bit | cm_8bit;
  58. for (n = 0; n < LTC_ARRAY_SIZE(cipher_mode_map); ++n) {
  59. if (cipher_mode_map[n].mode == mode)
  60. return cipher_mode_map[n].name;
  61. }
  62. fprintf(stderr, "Error: Can't map cipher_mode %d\n", mode);
  63. exit(1);
  64. }
  65. static void LTC_NORETURN die(int status)
  66. {
  67. FILE* o = status == EXIT_SUCCESS ? stdout : stderr;
  68. fprintf(o,
  69. "Usage: pem-info [<-h>]\n\n"
  70. "Generate LaTeX tables from the supported PEM resp. SSH ciphers.\n\n"
  71. "\t-h\tThe help you're looking at.\n"
  72. );
  73. exit(status);
  74. }
  75. int main(int argc, char **argv)
  76. {
  77. unsigned long n;
  78. if (argc > 1 && strstr(argv[1], "-h"))
  79. die(0);
  80. printf("PEM ciphers:\n\n");
  81. for (n = 0; n < pem_dek_infos_num; ++n) {
  82. char nbuf[32] = {0};
  83. size_t nlen = strlen(pem_dek_infos[n].name);
  84. memcpy(nbuf, pem_dek_infos[n].name, nlen);
  85. nbuf[nlen-1] = '}';
  86. printf("\\hline \\texttt{%-18s & %-15s & %-25ld & %-6s \\\\\n",
  87. nbuf, s_map_cipher(pem_dek_infos[n].algo),
  88. pem_dek_infos[n].keylen * 8,
  89. s_map_mode(pem_dek_infos[n].mode));
  90. }
  91. printf("\nSSH ciphers:\n\n");
  92. for (n = 0; n < ssh_ciphers_num; ++n) {
  93. char nbuf[32] = {0};
  94. size_t nlen = strlen(ssh_ciphers[n].name);
  95. memcpy(nbuf, ssh_ciphers[n].name, nlen);
  96. nbuf[nlen] = '}';
  97. printf("\\hline \\texttt{%-30s & %-16s & %-24ld & %-6s \\\\\n",
  98. nbuf, s_map_cipher(ssh_ciphers[n].algo),
  99. ssh_ciphers[n].keylen * 8,
  100. s_map_mode(ssh_ciphers[n].mode));
  101. }
  102. return 0;
  103. }
  104. #else
  105. int main(void) { return EXIT_FAILURE; }
  106. #endif