bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk/* Lengths and limits for some crypt() algorithms. */
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi#define CRYPT_BLF_PREFIX_LEN (7+22+1) /* $2.$nn$ + salt */
605c40c77fc3851cb2845da1c5319e32c791592aSergey Kitovcrypt_generate_des(const char *plaintext, const struct password_generate_params *params ATTR_UNUSED,
54a1b3574acab5f778843f7f1e04d2d26d61a852Timo Sirainen const unsigned char **raw_password_r, size_t *size_r)
54a1b3574acab5f778843f7f1e04d2d26d61a852Timo Sirainen salt = password_generate_salt(CRYPT_SALT_LEN);
54a1b3574acab5f778843f7f1e04d2d26d61a852Timo Sirainen password = t_strdup(mycrypt(plaintext, salt));
54a1b3574acab5f778843f7f1e04d2d26d61a852Timo Sirainen *raw_password_r = (const unsigned char *)password;
58d21a174d971834cfcd6d363349222749a54650Aki Tuomicrypt_generate_blowfish(const char *plaintext, const struct password_generate_params *params,
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk const unsigned char **raw_password_r, size_t *size_r)
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi if (crypt_gensalt_blowfish_rn(CRYPT_BLF_PREFIX, rounds,
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi if (crypt_blowfish_rn(plaintext, magic_salt, password,
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi *raw_password_r = (const unsigned char *)t_strdup(password);
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomicrypt_verify_blowfish(const char *plaintext, const struct password_generate_params *params ATTR_UNUSED,
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi const char **error_r)
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi /* the default mycrypt() handler would return match */
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi if (crypt_blowfish_rn(plaintext, salt, crypted, CRYPT_BLF_BUFFER_LEN) == NULL) {
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi /* really shouldn't happen unless the system is broken */
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi *error_r = t_strdup_printf("crypt_blowfish_rn failed: %m");
605c40c77fc3851cb2845da1c5319e32c791592aSergey Kitovcrypt_generate_sha256(const char *plaintext, const struct password_generate_params *params,
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk const unsigned char **raw_password_r, size_t *size_r)
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk salt = password_generate_salt(CRYPT_SHA2_SALT_LEN);
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk magic_salt = t_strdup_printf("$5$rounds=%u$%s", rounds, salt);
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk password = t_strdup(mycrypt(plaintext, magic_salt));
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk *raw_password_r = (const unsigned char *)password;
605c40c77fc3851cb2845da1c5319e32c791592aSergey Kitovcrypt_generate_sha512(const char *plaintext, const struct password_generate_params *params,
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk const unsigned char **raw_password_r, size_t *size_r)
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk salt = password_generate_salt(CRYPT_SHA2_SALT_LEN);
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk magic_salt = t_strdup_printf("$6$rounds=%u$%s", rounds, salt);
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk password = t_strdup(mycrypt(plaintext, magic_salt));
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk *raw_password_r = (const unsigned char *)password;
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk/* keep in sync with the crypt_schemes struct below */
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volkstatic const struct {
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk { "08/15!test~4711", "$5$rounds=1000$0123456789abcdef",
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk "$5$rounds=1000$0123456789abcdef$K/DksR0DT01hGc8g/kt"
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk "9McEgrbFMKi9qrb1jehe7hn4" },
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk { "08/15!test~4711", "$6$rounds=1000$0123456789abcdef",
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk "$6$rounds=1000$0123456789abcdef$ZIAd5WqfyLkpvsVCVUU1GrvqaZTq"
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk "vhJoouxdSqJO71l9Ld3tVrfOatEjarhghvEYADkq//LpDnTeO90tcbtHR1" }
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk/* keep in sync with the sample struct above */
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volkstatic const struct password_scheme crypt_schemes[] = {
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk { "SHA256-CRYPT", PW_ENCODING_NONE, 0, crypt_verify,
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk { "SHA512-CRYPT", PW_ENCODING_NONE, 0, crypt_verify,
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomistatic const struct password_scheme blf_crypt_scheme = {
1ebb6094e5105ba7ef521a0177c42d3ea81243f0Aki Tuomi "BLF-CRYPT", PW_ENCODING_NONE, 0, crypt_verify_blowfish,
cfb22f2f9b28d5888ba00ad910e47c9a490ca673Aki Tuomistatic const struct password_scheme default_crypt_scheme = {
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk unsigned int i;
c6d1318ff2db67719a803bada0323b38a078c98bAki Tuomi i_assert(N_ELEMENTS(crypt_schemes) == N_ELEMENTS(sample));
42fb278a57f1c6d7d5d0c7bd2318edb721dc0ec0Pascal Volk for (i = 0; i < N_ELEMENTS(crypt_schemes); i++) {