|
| 1 | +/* LibTomCrypt, modular cryptographic library -- Tom St Denis |
| 2 | + * |
| 3 | + * LibTomCrypt is a library that provides various cryptographic |
| 4 | + * algorithms in a highly modular and flexible manner. |
| 5 | + * |
| 6 | + * The library is free for all purposes without any express |
| 7 | + * guarantee it works. |
| 8 | + */ |
| 9 | +#include "tomcrypt_private.h" |
| 10 | + |
| 11 | +/** |
| 12 | + @file bcrypt.c |
| 13 | + bcrypt pbkdf, Steffen Jaeckel |
| 14 | +*/ |
| 15 | +#ifdef LTC_BCRYPT |
| 16 | + |
| 17 | +#define BCRYPT_WORDS 8 |
| 18 | +#define BCRYPT_HASHSIZE (BCRYPT_WORDS * 4) |
| 19 | + |
| 20 | +static int _bcrypt_hash(const unsigned char *pt, |
| 21 | + const unsigned char *pass, unsigned long passlen, |
| 22 | + const unsigned char *salt, unsigned long saltlen, |
| 23 | + unsigned char *out, unsigned long *outlen) |
| 24 | +{ |
| 25 | + symmetric_key key; |
| 26 | + int err, n; |
| 27 | + ulong32 ct[BCRYPT_WORDS]; |
| 28 | + |
| 29 | + if ((err = blowfish_setup_with_data(pass, passlen, salt, saltlen, &key)) != CRYPT_OK) { |
| 30 | + return err; |
| 31 | + } |
| 32 | + for (n = 0; n < 64; ++n) { |
| 33 | + if ((err = blowfish_expand(salt, saltlen, NULL, 0, &key)) != CRYPT_OK) { |
| 34 | + return err; |
| 35 | + } |
| 36 | + if ((err = blowfish_expand(pass, passlen, NULL, 0, &key)) != CRYPT_OK) { |
| 37 | + return err; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + for (n = 0; n < BCRYPT_WORDS; ++n) { |
| 42 | + LOAD32H(ct[n], &pt[n*4]); |
| 43 | + } |
| 44 | + |
| 45 | + for (n = 0; n < 64; ++n) { |
| 46 | + blowfish_enc(ct, BCRYPT_WORDS/2, &key); |
| 47 | + } |
| 48 | + |
| 49 | + for (n = 0; n < BCRYPT_WORDS; ++n) { |
| 50 | + STORE32L(ct[n], &out[4 * n]); |
| 51 | + } |
| 52 | + *outlen = sizeof(ct); |
| 53 | +#ifdef LTC_CLEAN_STACK |
| 54 | + zeromem(&key, sizeof(key)); |
| 55 | + zeromem(ct, sizeof(ct)); |
| 56 | +#endif |
| 57 | + |
| 58 | + return CRYPT_OK; |
| 59 | +} |
| 60 | + |
| 61 | +static int _bcrypt_pbkdf_hash(const unsigned char *pass, unsigned long passlen, |
| 62 | + const unsigned char *salt, unsigned long saltlen, |
| 63 | + unsigned char *out, unsigned long *outlen) |
| 64 | +{ |
| 65 | + const unsigned char pt[] = "OxychromaticBlowfishSwatDynamite"; |
| 66 | + return _bcrypt_hash(pt, pass, passlen, salt, saltlen, out, outlen); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + Compatible to bcrypt_pbkdf() as provided in OpenBSD |
| 71 | + @param password The input password (or key) |
| 72 | + @param password_len The length of the password (octets) |
| 73 | + @param salt The salt (or nonce) |
| 74 | + @param salt_len The length of the salt (octets) |
| 75 | + @param rounds # of iterations desired [read specs for more] |
| 76 | + @param hash_idx The index of the hash desired |
| 77 | + @param out [out] The destination for this algorithm |
| 78 | + @param outlen [in/out] The desired size of the algorithm output |
| 79 | + @return CRYPT_OK if successful |
| 80 | +*/ |
| 81 | +int bcrypt_pbkdf_openbsd(const char *password, unsigned long password_len, |
| 82 | + const unsigned char *salt, unsigned long salt_len, |
| 83 | + unsigned int rounds, int hash_idx, |
| 84 | + unsigned char *out, unsigned long *outlen) |
| 85 | +{ |
| 86 | + int err; |
| 87 | + ulong32 blkno; |
| 88 | + unsigned long left, itts, x, y, hashed_pass_len, step_size, steps, dest, used_rounds; |
| 89 | + unsigned char *buf[3], blkbuf[4]; |
| 90 | + unsigned char *hashed_pass; |
| 91 | + |
| 92 | + LTC_ARGCHK(password != NULL); |
| 93 | + LTC_ARGCHK(salt != NULL); |
| 94 | + LTC_ARGCHK(out != NULL); |
| 95 | + LTC_ARGCHK(outlen != NULL); |
| 96 | + |
| 97 | + if ((password_len == 0) || (salt_len == 0) || (*outlen == 0)) { |
| 98 | + return CRYPT_INVALID_ARG; |
| 99 | + } |
| 100 | + /* test hash IDX */ |
| 101 | + if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
| 102 | + return err; |
| 103 | + } |
| 104 | + /* set default value for rounds if not given */ |
| 105 | + if (rounds == 0) { |
| 106 | + used_rounds = LTC_BCRYPT_DEFAULT_ROUNDS; |
| 107 | + } else { |
| 108 | + used_rounds = rounds; |
| 109 | + } |
| 110 | + |
| 111 | + buf[0] = XMALLOC(MAXBLOCKSIZE * 3); |
| 112 | + hashed_pass = XMALLOC(MAXBLOCKSIZE); |
| 113 | + if (buf[0] == NULL || hashed_pass == NULL) { |
| 114 | + if (hashed_pass != NULL) { |
| 115 | + XFREE(hashed_pass); |
| 116 | + } |
| 117 | + if (buf[0] != NULL) { |
| 118 | + XFREE(buf[0]); |
| 119 | + } |
| 120 | + return CRYPT_MEM; |
| 121 | + } |
| 122 | + /* buf[1] points to the second block of MAXBLOCKSIZE bytes */ |
| 123 | + buf[1] = buf[0] + MAXBLOCKSIZE; |
| 124 | + buf[2] = buf[1] + MAXBLOCKSIZE; |
| 125 | + |
| 126 | + step_size = (*outlen + BCRYPT_HASHSIZE - 1) / BCRYPT_HASHSIZE; |
| 127 | + steps = (*outlen + step_size - 1) / step_size; |
| 128 | + |
| 129 | + hashed_pass_len = MAXBLOCKSIZE; |
| 130 | + if ((err = hash_memory(hash_idx, (unsigned char*)password, password_len, hashed_pass, &hashed_pass_len)) != CRYPT_OK) { |
| 131 | + goto LBL_ERR; |
| 132 | + } |
| 133 | + |
| 134 | + left = *outlen; |
| 135 | + blkno = 0; |
| 136 | + while (left != 0) { |
| 137 | + /* increment and store current block number */ |
| 138 | + ++blkno; |
| 139 | + STORE32H(blkno, blkbuf); |
| 140 | + |
| 141 | + /* process block number blkno */ |
| 142 | + zeromem(buf[0], MAXBLOCKSIZE*2); |
| 143 | + |
| 144 | + x = MAXBLOCKSIZE; |
| 145 | + if ((err = hash_memory_multi(hash_idx, buf[0], &x, |
| 146 | + salt, salt_len, |
| 147 | + blkbuf, 4, |
| 148 | + NULL, 0)) != CRYPT_OK) { |
| 149 | + goto LBL_ERR; |
| 150 | + } |
| 151 | + y = MAXBLOCKSIZE; |
| 152 | + if ((err = _bcrypt_pbkdf_hash(hashed_pass, hashed_pass_len, buf[0], x, buf[1], &y)) != CRYPT_OK) { |
| 153 | + goto LBL_ERR; |
| 154 | + } |
| 155 | + XMEMCPY(buf[2], buf[1], y); |
| 156 | + |
| 157 | + /* now compute repeated and XOR it in buf[2] */ |
| 158 | + for (itts = 1; itts < used_rounds; ++itts) { |
| 159 | + x = MAXBLOCKSIZE; |
| 160 | + if ((err = hash_memory(hash_idx, buf[1], y, buf[0], &x)) != CRYPT_OK) { |
| 161 | + goto LBL_ERR; |
| 162 | + } |
| 163 | + y = MAXBLOCKSIZE; |
| 164 | + if ((err = _bcrypt_pbkdf_hash(hashed_pass, hashed_pass_len, buf[0], x, buf[1], &y)) != CRYPT_OK) { |
| 165 | + goto LBL_ERR; |
| 166 | + } |
| 167 | + for (x = 0; x < y; x++) { |
| 168 | + buf[2][x] ^= buf[1][x]; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /* now emit upto `steps` bytes of buf[2] to output */ |
| 173 | + steps = MIN(steps, left); |
| 174 | + for (y = 0; y < steps; ++y) { |
| 175 | + dest = y * step_size + (blkno - 1); |
| 176 | + if (dest >= *outlen) |
| 177 | + break; |
| 178 | + out[dest] = buf[2][y]; |
| 179 | + } |
| 180 | + left -= y; |
| 181 | + } |
| 182 | + |
| 183 | + err = CRYPT_OK; |
| 184 | +LBL_ERR: |
| 185 | +#ifdef LTC_CLEAN_STACK |
| 186 | + zeromem(buf[0], MAXBLOCKSIZE*3); |
| 187 | + zeromem(hashed_pass, MAXBLOCKSIZE); |
| 188 | +#endif |
| 189 | + |
| 190 | + XFREE(hashed_pass); |
| 191 | + XFREE(buf[0]); |
| 192 | + |
| 193 | + return err; |
| 194 | +} |
| 195 | + |
| 196 | +#endif |
| 197 | + |
| 198 | + |
| 199 | +/* ref: $Format:%D$ */ |
| 200 | +/* git commit: $Format:%H$ */ |
| 201 | +/* commit time: $Format:%ai$ */ |
0 commit comments