pwd.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. *******************************************************************************
  3. \file pwd.c
  4. \brief Generate and manage passwords
  5. \project bee2/cmd
  6. \created 2022.06.23
  7. \version 2024.06.14
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include "../cmd.h"
  13. #include <bee2/core/blob.h>
  14. #include <bee2/core/dec.h>
  15. #include <bee2/core/err.h>
  16. #include <bee2/core/hex.h>
  17. #include <bee2/core/mem.h>
  18. #include <bee2/core/prng.h>
  19. #include <bee2/core/rng.h>
  20. #include <bee2/core/str.h>
  21. #include <bee2/core/util.h>
  22. #include <bee2/crypto/bels.h>
  23. #include <bee2/crypto/belt.h>
  24. #include <bee2/crypto/bign.h>
  25. #include <bee2/crypto/bpki.h>
  26. #include <bee2/crypto/brng.h>
  27. #include <stdio.h>
  28. /*
  29. *******************************************************************************
  30. Утилита pwd
  31. Функционал:
  32. - построение пароля по заданной схеме;
  33. - проверочное определение ранее построенного пароля;
  34. - печать ранее построенного пароля.
  35. Допустимые схемы построения паролей определены в модуле cmd.h при описании
  36. функций cmdPwdGen(), cmdPwdRead().
  37. Пример:
  38. bee2cmd pwd gen share:"-l256 -t3 -crc -pass pass:zed s1 s2 s3 s4 s5"
  39. bee2cmd pwd gen \
  40. share:"-l192 -pass share:\"-crc -pass pass:zed s1 s2 s3\" ss1 ss2 ss3"
  41. bee2cmd pwd val share:"-pass share:\"-pass pass:zed s2 s4 s1\" ss3 ss1"
  42. bee2cmd pwd print share:"-pass share:\"-pass pass:zed s2 s4 s1\" ss3 ss1"
  43. *******************************************************************************
  44. */
  45. static const char _name[] = "pwd";
  46. static const char _descr[] = "generate and manage passwords";
  47. static int pwdUsage()
  48. {
  49. printf(
  50. "bee2cmd/%s: %s\n"
  51. "Usage:\n"
  52. " pwd gen <schema>\n"
  53. " generate a password according to <schema>\n"
  54. " pwd val <schema>\n"
  55. " validate a password built by <schema>\n"
  56. " pwd print <schema>\n"
  57. " print a password built by <schema>\n"
  58. " schemas:\n"
  59. " pass:<pwd> -- direct password\n"
  60. " env:<name> -- password in environment variable <name>\n"
  61. " share:\"[options] <share1> <share2> ...\" -- shared password\n"
  62. " options:\n"
  63. " -t<nn> --- threshold (2 <= <nn> <= 16, 2 by default)\n"
  64. " -l<mmm> --- password bitlen: 128, 192 or 256 (by default)\n"
  65. " -crc --- the password contains 64-bit crc (<mmm> != 128)\n"
  66. " -pass <schema> --- password to protect shares\n"
  67. ,
  68. _name, _descr
  69. );
  70. return -1;
  71. }
  72. /*
  73. *******************************************************************************
  74. Самотестирование
  75. *******************************************************************************
  76. */
  77. static err_t pwdSelfTest()
  78. {
  79. const char pwd[] = "B194BAC80A08F53B";
  80. octet state[1024];
  81. octet buf[5 * (32 + 1)];
  82. octet buf1[32];
  83. // bels-share: разделение и сборка
  84. if (belsShare3(buf, 5, 3, 32, beltH()) != ERR_OK)
  85. return ERR_SELFTEST;
  86. if (belsRecover2(buf1, 1, 32, buf) != ERR_OK ||
  87. memEq(buf1, beltH(), 32))
  88. return ERR_SELFTEST;
  89. if (belsRecover2(buf1, 2, 32, buf) != ERR_OK ||
  90. memEq(buf1, beltH(), 32))
  91. return ERR_SELFTEST;
  92. if (belsRecover2(buf1, 3, 32, buf) != ERR_OK ||
  93. !memEq(buf1, beltH(), 32))
  94. return ERR_SELFTEST;
  95. // brng-ctr: тест Б.2
  96. ASSERT(sizeof(state) >= brngCTR_keep());
  97. memCopy(buf, beltH(), 96);
  98. brngCTRStart(state, beltH() + 128, beltH() + 128 + 64);
  99. brngCTRStepR(buf, 96, state);
  100. if (!hexEq(buf,
  101. "1F66B5B84B7339674533F0329C74F218"
  102. "34281FED0732429E0C79235FC273E269"
  103. "4C0E74B2CD5811AD21F23DE7E0FA742C"
  104. "3ED6EC483C461CE15C33A77AA308B7D2"
  105. "0F51D91347617C20BD4AB07AEF4F26A1"
  106. "AD1362A8F9A3D42FBE1B8E6F1C88AAD5"))
  107. return ERR_SELFTEST;
  108. // pbkdf2 тест E.5
  109. beltPBKDF2(buf, (const octet*)"B194BAC80A08F53B", strLen(pwd), 10000,
  110. beltH() + 128 + 64, 8);
  111. if (!hexEq(buf,
  112. "3D331BBBB1FBBB40E4BF22F6CB9A689E"
  113. "F13A77DC09ECF93291BFE42439A72E7D"))
  114. return FALSE;
  115. // belt-kwp: тест A.21
  116. ASSERT(sizeof(state) >= beltKWP_keep());
  117. beltKWPStart(state, beltH() + 128, 32);
  118. memCopy(buf, beltH(), 32);
  119. memCopy(buf + 32, beltH() + 32, 16);
  120. beltKWPStepE(buf, 48, state);
  121. if (!hexEq(buf,
  122. "49A38EE108D6C742E52B774F00A6EF98"
  123. "B106CBD13EA4FB0680323051BC04DF76"
  124. "E487B055C69BCF541176169F1DC9F6C8"))
  125. return FALSE;
  126. // все нормально
  127. return ERR_OK;
  128. }
  129. /*
  130. *******************************************************************************
  131. Генерация пароля
  132. pwd gen <schema>
  133. *******************************************************************************
  134. */
  135. static err_t pwdGen(int argc, char* argv[])
  136. {
  137. err_t code = ERR_OK;
  138. cmd_pwd_t pwd = 0;
  139. // верное число параметров?
  140. if (argc != 1)
  141. return ERR_CMD_PARAMS;
  142. // самотестирование
  143. code = pwdSelfTest();
  144. ERR_CALL_CHECK(code);
  145. // генерировать пароль
  146. code = cmdPwdGen(&pwd, *argv);
  147. cmdPwdClose(pwd);
  148. return code;
  149. }
  150. /*
  151. *******************************************************************************
  152. Проверка пароля
  153. pwd val <schema>
  154. *******************************************************************************
  155. */
  156. static err_t pwdVal(int argc, char* argv[])
  157. {
  158. err_t code = ERR_OK;
  159. cmd_pwd_t pwd = 0;
  160. // верное число параметров?
  161. if (argc != 1)
  162. return ERR_CMD_PARAMS;
  163. // самотестирование
  164. code = pwdSelfTest();
  165. ERR_CALL_CHECK(code);
  166. // определить пароль (с одновременной проверкой)
  167. code = cmdPwdRead(&pwd, *argv);
  168. cmdPwdClose(pwd);
  169. return code;
  170. }
  171. /*
  172. *******************************************************************************
  173. Печать пароля
  174. pwd print <schema>
  175. *******************************************************************************
  176. */
  177. static err_t pwdPrint(int argc, char* argv[])
  178. {
  179. err_t code = ERR_OK;
  180. cmd_pwd_t pwd = 0;
  181. // верное число параметров?
  182. if (argc != 1)
  183. return ERR_CMD_PARAMS;
  184. // определить пароль
  185. code = cmdPwdRead(&pwd, *argv);
  186. ERR_CALL_CHECK(code);
  187. // печатать пароль
  188. printf("%s\n", pwd);
  189. cmdPwdClose(pwd);
  190. return code;
  191. }
  192. /*
  193. *******************************************************************************
  194. Главная функция
  195. *******************************************************************************
  196. */
  197. int pwdMain(int argc, char* argv[])
  198. {
  199. err_t code;
  200. // справка
  201. if (argc < 3)
  202. return pwdUsage();
  203. // разбор
  204. ++argv, --argc;
  205. if (strEq(argv[0], "gen"))
  206. code = pwdGen(argc - 1, argv + 1);
  207. else if (strEq(argv[0], "val"))
  208. code = pwdVal(argc - 1, argv + 1);
  209. else if (strEq(argv[0], "print"))
  210. code = pwdPrint(argc - 1, argv + 1);
  211. else
  212. code = ERR_CMD_NOT_FOUND;
  213. // завершить
  214. if (code != ERR_OK || strEq(argv[0], "val"))
  215. printf("bee2cmd/%s: %s\n", _name, errMsg(code));
  216. return code != ERR_OK ? -1 : 0;
  217. }
  218. /*
  219. *******************************************************************************
  220. Инициализация
  221. *******************************************************************************
  222. */
  223. err_t pwdInit()
  224. {
  225. return cmdReg(_name, _descr, pwdMain);
  226. }