encoding.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Argon2 reference source code package - reference C implementations
  3. *
  4. * Copyright 2015
  5. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  6. *
  7. * You may use this work under the terms of a Creative Commons CC0 1.0
  8. * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  9. * these licenses can be found at:
  10. *
  11. * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  12. * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * You should have received a copy of both of these licenses along with this
  15. * software. If not, they may be obtained at the above URLs.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include "encoding.h"
  22. #include "core.h"
  23. /*
  24. * Example code for a decoder and encoder of "hash strings", with Argon2
  25. * parameters.
  26. *
  27. * This code comprises three sections:
  28. *
  29. * -- The first section contains generic Base64 encoding and decoding
  30. * functions. It is conceptually applicable to any hash function
  31. * implementation that uses Base64 to encode and decode parameters,
  32. * salts and outputs. It could be made into a library, provided that
  33. * the relevant functions are made public (non-static) and be given
  34. * reasonable names to avoid collisions with other functions.
  35. *
  36. * -- The second section is specific to Argon2. It encodes and decodes
  37. * the parameters, salts and outputs. It does not compute the hash
  38. * itself.
  39. *
  40. * The code was originally written by Thomas Pornin <pornin@bolet.org>,
  41. * to whom comments and remarks may be sent. It is released under what
  42. * should amount to Public Domain or its closest equivalent; the
  43. * following mantra is supposed to incarnate that fact with all the
  44. * proper legal rituals:
  45. *
  46. * ---------------------------------------------------------------------
  47. * This file is provided under the terms of Creative Commons CC0 1.0
  48. * Public Domain Dedication. To the extent possible under law, the
  49. * author (Thomas Pornin) has waived all copyright and related or
  50. * neighboring rights to this file. This work is published from: Canada.
  51. * ---------------------------------------------------------------------
  52. *
  53. * Copyright (c) 2015 Thomas Pornin
  54. */
  55. /* ==================================================================== */
  56. /*
  57. * Common code; could be shared between different hash functions.
  58. *
  59. * Note: the Base64 functions below assume that uppercase letters (resp.
  60. * lowercase letters) have consecutive numerical codes, that fit on 8
  61. * bits. All modern systems use ASCII-compatible charsets, where these
  62. * properties are true. If you are stuck with a dinosaur of a system
  63. * that still defaults to EBCDIC then you already have much bigger
  64. * interoperability issues to deal with.
  65. */
  66. /*
  67. * Some macros for constant-time comparisons. These work over values in
  68. * the 0..255 range. Returned value is 0x00 on "false", 0xFF on "true".
  69. */
  70. #define EQ(x, y) ((((0U - ((unsigned)(x) ^ (unsigned)(y))) >> 8) & 0xFF) ^ 0xFF)
  71. #define GT(x, y) ((((unsigned)(y) - (unsigned)(x)) >> 8) & 0xFF)
  72. #define GE(x, y) (GT(y, x) ^ 0xFF)
  73. #define LT(x, y) GT(y, x)
  74. #define LE(x, y) GE(y, x)
  75. /*
  76. * Convert value x (0..63) to corresponding Base64 character.
  77. */
  78. static int b64_byte_to_char(unsigned x) {
  79. return (LT(x, 26) & (x + 'A')) |
  80. (GE(x, 26) & LT(x, 52) & (x + ('a' - 26))) |
  81. (GE(x, 52) & LT(x, 62) & (x + ('0' - 52))) | (EQ(x, 62) & '+') |
  82. (EQ(x, 63) & '/');
  83. }
  84. /*
  85. * Convert character c to the corresponding 6-bit value. If character c
  86. * is not a Base64 character, then 0xFF (255) is returned.
  87. */
  88. static unsigned b64_char_to_byte(int c) {
  89. unsigned x;
  90. x = (GE(c, 'A') & LE(c, 'Z') & (c - 'A')) |
  91. (GE(c, 'a') & LE(c, 'z') & (c - ('a' - 26))) |
  92. (GE(c, '0') & LE(c, '9') & (c - ('0' - 52))) | (EQ(c, '+') & 62) |
  93. (EQ(c, '/') & 63);
  94. return x | (EQ(x, 0) & (EQ(c, 'A') ^ 0xFF));
  95. }
  96. /*
  97. * Convert some bytes to Base64. 'dst_len' is the length (in characters)
  98. * of the output buffer 'dst'; if that buffer is not large enough to
  99. * receive the result (including the terminating 0), then (size_t)-1
  100. * is returned. Otherwise, the zero-terminated Base64 string is written
  101. * in the buffer, and the output length (counted WITHOUT the terminating
  102. * zero) is returned.
  103. */
  104. static size_t to_base64(char *dst, size_t dst_len, const void *src,
  105. size_t src_len) {
  106. size_t olen;
  107. const unsigned char *buf;
  108. unsigned acc, acc_len;
  109. olen = (src_len / 3) << 2;
  110. switch (src_len % 3) {
  111. case 2:
  112. olen++;
  113. /* fall through */
  114. case 1:
  115. olen += 2;
  116. break;
  117. }
  118. if (dst_len <= olen) {
  119. return (size_t)-1;
  120. }
  121. acc = 0;
  122. acc_len = 0;
  123. buf = (const unsigned char *)src;
  124. while (src_len-- > 0) {
  125. acc = (acc << 8) + (*buf++);
  126. acc_len += 8;
  127. while (acc_len >= 6) {
  128. acc_len -= 6;
  129. *dst++ = (char)b64_byte_to_char((acc >> acc_len) & 0x3F);
  130. }
  131. }
  132. if (acc_len > 0) {
  133. *dst++ = (char)b64_byte_to_char((acc << (6 - acc_len)) & 0x3F);
  134. }
  135. *dst++ = 0;
  136. return olen;
  137. }
  138. /*
  139. * Decode Base64 chars into bytes. The '*dst_len' value must initially
  140. * contain the length of the output buffer '*dst'; when the decoding
  141. * ends, the actual number of decoded bytes is written back in
  142. * '*dst_len'.
  143. *
  144. * Decoding stops when a non-Base64 character is encountered, or when
  145. * the output buffer capacity is exceeded. If an error occurred (output
  146. * buffer is too small, invalid last characters leading to unprocessed
  147. * buffered bits), then NULL is returned; otherwise, the returned value
  148. * points to the first non-Base64 character in the source stream, which
  149. * may be the terminating zero.
  150. */
  151. static const char *from_base64(void *dst, size_t *dst_len, const char *src) {
  152. size_t len;
  153. unsigned char *buf;
  154. unsigned acc, acc_len;
  155. buf = (unsigned char *)dst;
  156. len = 0;
  157. acc = 0;
  158. acc_len = 0;
  159. for (;;) {
  160. unsigned d;
  161. d = b64_char_to_byte(*src);
  162. if (d == 0xFF) {
  163. break;
  164. }
  165. src++;
  166. acc = (acc << 6) + d;
  167. acc_len += 6;
  168. if (acc_len >= 8) {
  169. acc_len -= 8;
  170. if ((len++) >= *dst_len) {
  171. return NULL;
  172. }
  173. *buf++ = (acc >> acc_len) & 0xFF;
  174. }
  175. }
  176. /*
  177. * If the input length is equal to 1 modulo 4 (which is
  178. * invalid), then there will remain 6 unprocessed bits;
  179. * otherwise, only 0, 2 or 4 bits are buffered. The buffered
  180. * bits must also all be zero.
  181. */
  182. if (acc_len > 4 || (acc & (((unsigned)1 << acc_len) - 1)) != 0) {
  183. return NULL;
  184. }
  185. *dst_len = len;
  186. return src;
  187. }
  188. /*
  189. * Decode decimal integer from 'str'; the value is written in '*v'.
  190. * Returned value is a pointer to the next non-decimal character in the
  191. * string. If there is no digit at all, or the value encoding is not
  192. * minimal (extra leading zeros), or the value does not fit in an
  193. * 'unsigned long', then NULL is returned.
  194. */
  195. static const char *decode_decimal(const char *str, unsigned long *v) {
  196. const char *orig;
  197. unsigned long acc;
  198. acc = 0;
  199. for (orig = str;; str++) {
  200. int c;
  201. c = *str;
  202. if (c < '0' || c > '9') {
  203. break;
  204. }
  205. c -= '0';
  206. if (acc > (ULONG_MAX / 10)) {
  207. return NULL;
  208. }
  209. acc *= 10;
  210. if ((unsigned long)c > (ULONG_MAX - acc)) {
  211. return NULL;
  212. }
  213. acc += (unsigned long)c;
  214. }
  215. if (str == orig || (*orig == '0' && str != (orig + 1))) {
  216. return NULL;
  217. }
  218. *v = acc;
  219. return str;
  220. }
  221. /* ==================================================================== */
  222. /*
  223. * Code specific to Argon2.
  224. *
  225. * The code below applies the following format:
  226. *
  227. * $argon2<T>[$v=<num>]$m=<num>,t=<num>,p=<num>$<bin>$<bin>
  228. *
  229. * where <T> is either 'd', 'id', or 'i', <num> is a decimal integer (positive,
  230. * fits in an 'unsigned long'), and <bin> is Base64-encoded data (no '=' padding
  231. * characters, no newline or whitespace).
  232. *
  233. * The last two binary chunks (encoded in Base64) are, in that order,
  234. * the salt and the output. Both are required. The binary salt length and the
  235. * output length must be in the allowed ranges defined in argon2.h.
  236. *
  237. * The ctx struct must contain buffers large enough to hold the salt and pwd
  238. * when it is fed into decode_string.
  239. */
  240. int decode_string(argon2_context_t *ctx, const char *str, argon2_type_t type) {
  241. /* check for prefix */
  242. #define CC(prefix) \
  243. do { \
  244. size_t cc_len = strlen(prefix); \
  245. if (strncmp(str, prefix, cc_len) != 0) { \
  246. return ARGON2_DECODING_FAIL; \
  247. } \
  248. str += cc_len; \
  249. } while ((void)0, 0)
  250. /* optional prefix checking with supplied code */
  251. #define CC_opt(prefix, code) \
  252. do { \
  253. size_t cc_len = strlen(prefix); \
  254. if (strncmp(str, prefix, cc_len) == 0) { \
  255. str += cc_len; \
  256. { code; } \
  257. } \
  258. } while ((void)0, 0)
  259. /* Decoding prefix into decimal */
  260. #define DECIMAL(x) \
  261. do { \
  262. unsigned long dec_x; \
  263. str = decode_decimal(str, &dec_x); \
  264. if (str == NULL) { \
  265. return ARGON2_DECODING_FAIL; \
  266. } \
  267. (x) = dec_x; \
  268. } while ((void)0, 0)
  269. /* Decoding prefix into uint32_t decimal */
  270. #define DECIMAL_U32(x) \
  271. do { \
  272. unsigned long dec_x; \
  273. str = decode_decimal(str, &dec_x); \
  274. if (str == NULL || dec_x > UINT32_MAX) { \
  275. return ARGON2_DECODING_FAIL; \
  276. } \
  277. (x) = (uint32_t)dec_x; \
  278. } while ((void)0, 0)
  279. /* Decoding base64 into a binary buffer */
  280. #define BIN(buf, max_len, len) \
  281. do { \
  282. size_t bin_len = (max_len); \
  283. str = from_base64(buf, &bin_len, str); \
  284. if (str == NULL || bin_len > UINT32_MAX) { \
  285. return ARGON2_DECODING_FAIL; \
  286. } \
  287. (len) = (uint32_t)bin_len; \
  288. } while ((void)0, 0)
  289. size_t maxsaltlen = ctx->saltlen;
  290. size_t maxoutlen = ctx->outlen;
  291. int validation_result;
  292. const char* type_string;
  293. /* We should start with the argon2_type we are using */
  294. type_string = kp_argon2_type2string(type, 0);
  295. if (!type_string) {
  296. return ARGON2_INCORRECT_TYPE;
  297. }
  298. CC("$");
  299. CC(type_string);
  300. /* Reading the version number if the default is suppressed */
  301. ctx->version = ARGON2_VERSION_10;
  302. CC_opt("$v=", DECIMAL_U32(ctx->version));
  303. CC("$m=");
  304. DECIMAL_U32(ctx->m_cost);
  305. CC(",t=");
  306. DECIMAL_U32(ctx->t_cost);
  307. CC(",p=");
  308. DECIMAL_U32(ctx->lanes);
  309. ctx->threads = ctx->lanes;
  310. CC("$");
  311. BIN(ctx->salt, maxsaltlen, ctx->saltlen);
  312. CC("$");
  313. BIN(ctx->out, maxoutlen, ctx->outlen);
  314. /* The rest of the fields get the default values */
  315. ctx->secret = NULL;
  316. ctx->secretlen = 0;
  317. ctx->ad = NULL;
  318. ctx->adlen = 0;
  319. ctx->allocate_cbk = NULL;
  320. ctx->free_cbk = NULL;
  321. ctx->flags = ARGON2_DEFAULT_FLAGS;
  322. /* On return, must have valid context */
  323. validation_result = validate_inputs(ctx);
  324. if (validation_result != ARGON2_OK) {
  325. return validation_result;
  326. }
  327. /* Can't have any additional characters */
  328. if (*str == 0) {
  329. return ARGON2_OK;
  330. } else {
  331. return ARGON2_DECODING_FAIL;
  332. }
  333. #undef CC
  334. #undef CC_opt
  335. #undef DECIMAL
  336. #undef BIN
  337. }
  338. int encode_string(char *dst, size_t dst_len, argon2_context_t *ctx,
  339. argon2_type_t type) {
  340. #define SS(str) \
  341. do { \
  342. size_t pp_len = strlen(str); \
  343. if (pp_len >= dst_len) { \
  344. return ARGON2_ENCODING_FAIL; \
  345. } \
  346. memcpy(dst, str, pp_len + 1); \
  347. dst += pp_len; \
  348. dst_len -= pp_len; \
  349. } while ((void)0, 0)
  350. #define SX(x) \
  351. do { \
  352. char tmp[30]; \
  353. sprintf(tmp, "%lu", (unsigned long)(x)); \
  354. SS(tmp); \
  355. } while ((void)0, 0)
  356. #define SB(buf, len) \
  357. do { \
  358. size_t sb_len = to_base64(dst, dst_len, buf, len); \
  359. if (sb_len == (size_t)-1) { \
  360. return ARGON2_ENCODING_FAIL; \
  361. } \
  362. dst += sb_len; \
  363. dst_len -= sb_len; \
  364. } while ((void)0, 0)
  365. const char* type_string = kp_argon2_type2string(type, 0);
  366. int validation_result = validate_inputs(ctx);
  367. if (!type_string) {
  368. return ARGON2_ENCODING_FAIL;
  369. }
  370. if (validation_result != ARGON2_OK) {
  371. return validation_result;
  372. }
  373. SS("$");
  374. SS(type_string);
  375. SS("$v=");
  376. SX(ctx->version);
  377. SS("$m=");
  378. SX(ctx->m_cost);
  379. SS(",t=");
  380. SX(ctx->t_cost);
  381. SS(",p=");
  382. SX(ctx->lanes);
  383. SS("$");
  384. SB(ctx->salt, ctx->saltlen);
  385. SS("$");
  386. SB(ctx->out, ctx->outlen);
  387. return ARGON2_OK;
  388. #undef SS
  389. #undef SX
  390. #undef SB
  391. }
  392. size_t b64len(uint32_t len) {
  393. size_t olen = ((size_t)len / 3) << 2;
  394. switch (len % 3) {
  395. case 2:
  396. olen++;
  397. /* fall through */
  398. case 1:
  399. olen += 2;
  400. break;
  401. }
  402. return olen;
  403. }
  404. size_t numlen(uint32_t num) {
  405. size_t len = 1;
  406. while (num >= 10) {
  407. ++len;
  408. num = num / 10;
  409. }
  410. return len;
  411. }