+ OPENSSL_strlcat(out_buf, tmp_buf, sizeof(out_buf));
+ OPENSSL_strlcat(out_buf, ascii_dollar, sizeof(out_buf));
+ }
+ OPENSSL_strlcat(out_buf, ascii_salt, sizeof(out_buf));
+
+ /* assert "$5$rounds=999999999$......salt......" */
+ if (strlen(out_buf) > 3 + 17 * rounds_custom + salt_len )
+ goto err;
+
+ md = EVP_MD_CTX_new();
+ if (md == NULL
+ || !EVP_DigestInit_ex(md, sha, NULL)
+ || !EVP_DigestUpdate(md, passwd, passwd_len)
+ || !EVP_DigestUpdate(md, ascii_salt, salt_len))
+ goto err;
+
+ md2 = EVP_MD_CTX_new();
+ if (md2 == NULL
+ || !EVP_DigestInit_ex(md2, sha, NULL)
+ || !EVP_DigestUpdate(md2, passwd, passwd_len)
+ || !EVP_DigestUpdate(md2, ascii_salt, salt_len)
+ || !EVP_DigestUpdate(md2, passwd, passwd_len)
+ || !EVP_DigestFinal_ex(md2, buf, NULL))
+ goto err;
+
+ for (n = passwd_len; n > buf_size; n -= buf_size) {
+ if (!EVP_DigestUpdate(md, buf, buf_size))
+ goto err;
+ }
+ if (!EVP_DigestUpdate(md, buf, n))
+ goto err;
+
+ n = passwd_len;
+ while (n) {
+ if (!EVP_DigestUpdate(md,
+ (n & 1) ? buf : (unsigned const char *)passwd,
+ (n & 1) ? buf_size : passwd_len))
+ goto err;
+ n >>= 1;
+ }
+ if (!EVP_DigestFinal_ex(md, buf, NULL))
+ return NULL;
+
+ /* P sequence */
+ if (!EVP_DigestInit_ex(md2, sha, NULL))
+ goto err;
+
+ for (n = passwd_len; n > 0; n--)
+ if (!EVP_DigestUpdate(md2, passwd, passwd_len))
+ goto err;
+
+ if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
+ return NULL;
+
+ if ((p_bytes = OPENSSL_zalloc(passwd_len)) == NULL)
+ goto err;
+ for (cp = p_bytes, n = passwd_len; n > buf_size; n -= buf_size, cp += buf_size)
+ memcpy(cp, temp_buf, buf_size);
+ memcpy(cp, temp_buf, n);
+
+ /* S sequence */
+ if (!EVP_DigestInit_ex(md2, sha, NULL))
+ goto err;
+
+ for (n = 16 + buf[0]; n > 0; n--)
+ if (!EVP_DigestUpdate(md2, ascii_salt, salt_len))
+ goto err;
+
+ if (!EVP_DigestFinal_ex(md2, temp_buf, NULL))
+ return NULL;
+
+ if ((s_bytes = OPENSSL_zalloc(salt_len)) == NULL)
+ goto err;
+ for (cp = s_bytes, n = salt_len; n > buf_size; n -= buf_size, cp += buf_size)
+ memcpy(cp, temp_buf, buf_size);
+ memcpy(cp, temp_buf, n);
+
+ for (n = 0; n < rounds; n++) {
+ if (!EVP_DigestInit_ex(md2, sha, NULL))
+ goto err;
+ if (!EVP_DigestUpdate(md2,
+ (n & 1) ? (unsigned const char *)p_bytes : buf,
+ (n & 1) ? passwd_len : buf_size))
+ goto err;
+ if (n % 3) {
+ if (!EVP_DigestUpdate(md2, s_bytes, salt_len))
+ goto err;
+ }
+ if (n % 7) {
+ if (!EVP_DigestUpdate(md2, p_bytes, passwd_len))
+ goto err;
+ }
+ if (!EVP_DigestUpdate(md2,
+ (n & 1) ? buf : (unsigned const char *)p_bytes,
+ (n & 1) ? buf_size : passwd_len))
+ goto err;
+ if (!EVP_DigestFinal_ex(md2, buf, NULL))
+ goto err;
+ }
+ EVP_MD_CTX_free(md2);
+ EVP_MD_CTX_free(md);
+ md2 = NULL;
+ md = NULL;
+ OPENSSL_free(p_bytes);
+ OPENSSL_free(s_bytes);
+ p_bytes = NULL;
+ s_bytes = NULL;
+
+ cp = out_buf + strlen(out_buf);
+ *cp++ = ascii_dollar[0];
+
+# define b64_from_24bit(B2, B1, B0, N) \
+ do { \
+ unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \
+ int i = (N); \
+ while (i-- > 0) \
+ { \
+ *cp++ = cov_2char[w & 0x3f]; \
+ w >>= 6; \
+ } \
+ } while (0)
+
+ switch (magic[0]) {
+ case '5':
+ b64_from_24bit (buf[0], buf[10], buf[20], 4);
+ b64_from_24bit (buf[21], buf[1], buf[11], 4);
+ b64_from_24bit (buf[12], buf[22], buf[2], 4);
+ b64_from_24bit (buf[3], buf[13], buf[23], 4);
+ b64_from_24bit (buf[24], buf[4], buf[14], 4);
+ b64_from_24bit (buf[15], buf[25], buf[5], 4);
+ b64_from_24bit (buf[6], buf[16], buf[26], 4);
+ b64_from_24bit (buf[27], buf[7], buf[17], 4);
+ b64_from_24bit (buf[18], buf[28], buf[8], 4);
+ b64_from_24bit (buf[9], buf[19], buf[29], 4);
+ b64_from_24bit (0, buf[31], buf[30], 3);
+ break;
+ case '6':
+ b64_from_24bit (buf[0], buf[21], buf[42], 4);
+ b64_from_24bit (buf[22], buf[43], buf[1], 4);
+ b64_from_24bit (buf[44], buf[2], buf[23], 4);
+ b64_from_24bit (buf[3], buf[24], buf[45], 4);
+ b64_from_24bit (buf[25], buf[46], buf[4], 4);
+ b64_from_24bit (buf[47], buf[5], buf[26], 4);
+ b64_from_24bit (buf[6], buf[27], buf[48], 4);
+ b64_from_24bit (buf[28], buf[49], buf[7], 4);
+ b64_from_24bit (buf[50], buf[8], buf[29], 4);
+ b64_from_24bit (buf[9], buf[30], buf[51], 4);
+ b64_from_24bit (buf[31], buf[52], buf[10], 4);
+ b64_from_24bit (buf[53], buf[11], buf[32], 4);
+ b64_from_24bit (buf[12], buf[33], buf[54], 4);
+ b64_from_24bit (buf[34], buf[55], buf[13], 4);
+ b64_from_24bit (buf[56], buf[14], buf[35], 4);
+ b64_from_24bit (buf[15], buf[36], buf[57], 4);
+ b64_from_24bit (buf[37], buf[58], buf[16], 4);
+ b64_from_24bit (buf[59], buf[17], buf[38], 4);
+ b64_from_24bit (buf[18], buf[39], buf[60], 4);
+ b64_from_24bit (buf[40], buf[61], buf[19], 4);
+ b64_from_24bit (buf[62], buf[20], buf[41], 4);
+ b64_from_24bit (0, 0, buf[63], 2);
+ break;
+ default:
+ goto err;
+ }
+ *cp = '\0';
+#ifdef CHARSET_EBCDIC
+ ascii2ebcdic(out_buf, out_buf, strlen(out_buf));