psa_constant_names.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright The Mbed TLS Contributors
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <errno.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "psa/crypto.h"
  23. /* This block is present to support Visual Studio builds prior to 2015 */
  24. #if defined(_MSC_VER) && _MSC_VER < 1900
  25. #include <stdarg.h>
  26. int snprintf( char *s, size_t n, const char *fmt, ... )
  27. {
  28. int ret;
  29. va_list argp;
  30. /* Avoid calling the invalid parameter handler by checking ourselves */
  31. if( s == NULL || n == 0 || fmt == NULL )
  32. return( -1 );
  33. va_start( argp, fmt );
  34. #if defined(_TRUNCATE) && !defined(__MINGW32__)
  35. ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
  36. #else
  37. ret = _vsnprintf( s, n, fmt, argp );
  38. if( ret < 0 || (size_t) ret == n )
  39. {
  40. s[n-1] = '\0';
  41. ret = -1;
  42. }
  43. #endif
  44. va_end( argp );
  45. return( ret );
  46. }
  47. #endif
  48. /* There are different GET_HASH macros for different kinds of algorithms
  49. * built from hashes, but the values are all constructed on the
  50. * same model. */
  51. #define PSA_ALG_GET_HASH(alg) \
  52. (((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
  53. static void append(char **buffer, size_t buffer_size,
  54. size_t *required_size,
  55. const char *string, size_t length)
  56. {
  57. *required_size += length;
  58. if (*required_size < buffer_size) {
  59. memcpy(*buffer, string, length);
  60. *buffer += length;
  61. }
  62. }
  63. static void append_integer(char **buffer, size_t buffer_size,
  64. size_t *required_size,
  65. const char *format /*printf format for value*/,
  66. unsigned long value)
  67. {
  68. size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
  69. if (n < buffer_size - *required_size) *buffer += n;
  70. *required_size += n;
  71. }
  72. /* The code of these function is automatically generated and included below. */
  73. static const char *psa_ecc_family_name(psa_ecc_family_t curve);
  74. static const char *psa_dh_family_name(psa_dh_family_t group);
  75. static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
  76. static void append_with_curve(char **buffer, size_t buffer_size,
  77. size_t *required_size,
  78. const char *string, size_t length,
  79. psa_ecc_family_t curve)
  80. {
  81. const char *family_name = psa_ecc_family_name(curve);
  82. append(buffer, buffer_size, required_size, string, length);
  83. append(buffer, buffer_size, required_size, "(", 1);
  84. if (family_name != NULL) {
  85. append(buffer, buffer_size, required_size,
  86. family_name, strlen(family_name));
  87. } else {
  88. append_integer(buffer, buffer_size, required_size,
  89. "0x%02x", curve);
  90. }
  91. append(buffer, buffer_size, required_size, ")", 1);
  92. }
  93. static void append_with_group(char **buffer, size_t buffer_size,
  94. size_t *required_size,
  95. const char *string, size_t length,
  96. psa_dh_family_t group)
  97. {
  98. const char *group_name = psa_dh_family_name(group);
  99. append(buffer, buffer_size, required_size, string, length);
  100. append(buffer, buffer_size, required_size, "(", 1);
  101. if (group_name != NULL) {
  102. append(buffer, buffer_size, required_size,
  103. group_name, strlen(group_name));
  104. } else {
  105. append_integer(buffer, buffer_size, required_size,
  106. "0x%02x", group);
  107. }
  108. append(buffer, buffer_size, required_size, ")", 1);
  109. }
  110. typedef const char *(*psa_get_algorithm_name_func_ptr)(psa_algorithm_t alg);
  111. static void append_with_alg(char **buffer, size_t buffer_size,
  112. size_t *required_size,
  113. psa_get_algorithm_name_func_ptr get_name,
  114. psa_algorithm_t alg)
  115. {
  116. const char *name = get_name(alg);
  117. if (name != NULL) {
  118. append(buffer, buffer_size, required_size,
  119. name, strlen(name));
  120. } else {
  121. append_integer(buffer, buffer_size, required_size,
  122. "0x%08lx", alg);
  123. }
  124. }
  125. #include "psa_constant_names_generated.c"
  126. static int psa_snprint_status(char *buffer, size_t buffer_size,
  127. psa_status_t status)
  128. {
  129. const char *name = psa_strerror(status);
  130. if (name == NULL) {
  131. return snprintf(buffer, buffer_size, "%ld", (long) status);
  132. } else {
  133. size_t length = strlen(name);
  134. if (length < buffer_size) {
  135. memcpy(buffer, name, length + 1);
  136. return (int) length;
  137. } else {
  138. return (int) buffer_size;
  139. }
  140. }
  141. }
  142. static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
  143. psa_ecc_family_t curve)
  144. {
  145. const char *name = psa_ecc_family_name(curve);
  146. if (name == NULL) {
  147. return snprintf(buffer, buffer_size, "0x%02x", (unsigned) curve);
  148. } else {
  149. size_t length = strlen(name);
  150. if (length < buffer_size) {
  151. memcpy(buffer, name, length + 1);
  152. return (int) length;
  153. } else {
  154. return (int) buffer_size;
  155. }
  156. }
  157. }
  158. static int psa_snprint_dh_group(char *buffer, size_t buffer_size,
  159. psa_dh_family_t group)
  160. {
  161. const char *name = psa_dh_family_name(group);
  162. if (name == NULL) {
  163. return snprintf(buffer, buffer_size, "0x%02x", (unsigned) group);
  164. } else {
  165. size_t length = strlen(name);
  166. if (length < buffer_size) {
  167. memcpy(buffer, name, length + 1);
  168. return (int) length;
  169. } else {
  170. return (int) buffer_size;
  171. }
  172. }
  173. }
  174. static void usage(const char *program_name)
  175. {
  176. printf("Usage: %s TYPE VALUE [VALUE...]\n",
  177. program_name == NULL ? "psa_constant_names" : program_name);
  178. printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
  179. printf("Supported types (with = between aliases):\n");
  180. printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
  181. printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_family_t)\n");
  182. printf(" group=dh_group Diffie-Hellman group identifier (psa_dh_family_t)\n");
  183. printf(" type=key_type Key type (psa_key_type_t)\n");
  184. printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
  185. printf(" error=status Status code (psa_status_t)\n");
  186. }
  187. typedef enum {
  188. TYPE_STATUS,
  189. } signed_value_type;
  190. int process_signed(signed_value_type type, long min, long max, char **argp)
  191. {
  192. for (; *argp != NULL; argp++) {
  193. char buffer[200];
  194. char *end;
  195. long value = strtol(*argp, &end, 0);
  196. if (*end) {
  197. printf("Non-numeric value: %s\n", *argp);
  198. return EXIT_FAILURE;
  199. }
  200. if (value < min || (errno == ERANGE && value < 0)) {
  201. printf("Value too small: %s\n", *argp);
  202. return EXIT_FAILURE;
  203. }
  204. if (value > max || (errno == ERANGE && value > 0)) {
  205. printf("Value too large: %s\n", *argp);
  206. return EXIT_FAILURE;
  207. }
  208. switch (type) {
  209. case TYPE_STATUS:
  210. psa_snprint_status(buffer, sizeof(buffer),
  211. (psa_status_t) value);
  212. break;
  213. }
  214. puts(buffer);
  215. }
  216. return EXIT_SUCCESS;
  217. }
  218. typedef enum {
  219. TYPE_ALGORITHM,
  220. TYPE_ECC_CURVE,
  221. TYPE_DH_GROUP,
  222. TYPE_KEY_TYPE,
  223. TYPE_KEY_USAGE,
  224. } unsigned_value_type;
  225. int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
  226. {
  227. for (; *argp != NULL; argp++) {
  228. char buffer[200];
  229. char *end;
  230. unsigned long value = strtoul(*argp, &end, 0);
  231. if (*end) {
  232. printf("Non-numeric value: %s\n", *argp);
  233. return EXIT_FAILURE;
  234. }
  235. if (value > max || errno == ERANGE) {
  236. printf("Value out of range: %s\n", *argp);
  237. return EXIT_FAILURE;
  238. }
  239. switch (type) {
  240. case TYPE_ALGORITHM:
  241. psa_snprint_algorithm(buffer, sizeof(buffer),
  242. (psa_algorithm_t) value);
  243. break;
  244. case TYPE_ECC_CURVE:
  245. psa_snprint_ecc_curve(buffer, sizeof(buffer),
  246. (psa_ecc_family_t) value);
  247. break;
  248. case TYPE_DH_GROUP:
  249. psa_snprint_dh_group(buffer, sizeof(buffer),
  250. (psa_dh_family_t) value);
  251. break;
  252. case TYPE_KEY_TYPE:
  253. psa_snprint_key_type(buffer, sizeof(buffer),
  254. (psa_key_type_t) value);
  255. break;
  256. case TYPE_KEY_USAGE:
  257. psa_snprint_key_usage(buffer, sizeof(buffer),
  258. (psa_key_usage_t) value);
  259. break;
  260. }
  261. puts(buffer);
  262. }
  263. return EXIT_SUCCESS;
  264. }
  265. int main(int argc, char *argv[])
  266. {
  267. if (argc <= 1 ||
  268. !strcmp(argv[1], "help") ||
  269. !strcmp(argv[1], "--help"))
  270. {
  271. usage(argv[0]);
  272. return EXIT_FAILURE;
  273. }
  274. if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
  275. /* There's no way to obtain the actual range of a signed type,
  276. * so hard-code it here: psa_status_t is int32_t. */
  277. return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
  278. argv + 2);
  279. } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
  280. return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
  281. argv + 2);
  282. } else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
  283. return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_family_t) (-1),
  284. argv + 2);
  285. } else if (!strcmp(argv[1], "group") || !strcmp(argv[1], "dh_group")) {
  286. return process_unsigned(TYPE_DH_GROUP, (psa_dh_family_t) (-1),
  287. argv + 2);
  288. } else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
  289. return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
  290. argv + 2);
  291. } else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
  292. return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
  293. argv + 2);
  294. } else {
  295. printf("Unknown type: %s\n", argv[1]);
  296. return EXIT_FAILURE;
  297. }
  298. }