/src/hpn-ssh/openbsd-compat/bcrypt_pbkdf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: bcrypt_pbkdf.c,v 1.16 2020/08/02 18:35:48 tb Exp $ */ |
2 | | /* |
3 | | * Copyright (c) 2013 Ted Unangst <tedu@openbsd.org> |
4 | | * |
5 | | * Permission to use, copy, modify, and distribute this software for any |
6 | | * purpose with or without fee is hereby granted, provided that the above |
7 | | * copyright notice and this permission notice appear in all copies. |
8 | | * |
9 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
10 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
11 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
12 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
13 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
14 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
15 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
16 | | */ |
17 | | |
18 | | /* OPENBSD ORIGINAL: lib/libutil/bcrypt_pbkdf.c */ |
19 | | |
20 | | /* This version has been modified to use SHA512 from SUPERCOP */ |
21 | | |
22 | | #include "includes.h" |
23 | | |
24 | | #ifndef HAVE_BCRYPT_PBKDF |
25 | | |
26 | | #include <sys/types.h> |
27 | | |
28 | | #ifdef HAVE_STDLIB_H |
29 | | # include <stdlib.h> |
30 | | #endif |
31 | | #include <string.h> |
32 | | |
33 | | #ifdef HAVE_BLF_H |
34 | | # include <blf.h> |
35 | | #endif |
36 | | |
37 | | #include "crypto_api.h" |
38 | | #ifdef SHA512_DIGEST_LENGTH |
39 | | # undef SHA512_DIGEST_LENGTH |
40 | | #endif |
41 | 3.76k | #define SHA512_DIGEST_LENGTH crypto_hash_sha512_BYTES |
42 | | |
43 | 3.76k | #define MINIMUM(a,b) (((a) < (b)) ? (a) : (b)) |
44 | | |
45 | | /* |
46 | | * pkcs #5 pbkdf2 implementation using the "bcrypt" hash |
47 | | * |
48 | | * The bcrypt hash function is derived from the bcrypt password hashing |
49 | | * function with the following modifications: |
50 | | * 1. The input password and salt are preprocessed with SHA512. |
51 | | * 2. The output length is expanded to 256 bits. |
52 | | * 3. Subsequently the magic string to be encrypted is lengthened and modifed |
53 | | * to "OxychromaticBlowfishSwatDynamite" |
54 | | * 4. The hash function is defined to perform 64 rounds of initial state |
55 | | * expansion. (More rounds are performed by iterating the hash.) |
56 | | * |
57 | | * Note that this implementation pulls the SHA512 operations into the caller |
58 | | * as a performance optimization. |
59 | | * |
60 | | * One modification from official pbkdf2. Instead of outputting key material |
61 | | * linearly, we mix it. pbkdf2 has a known weakness where if one uses it to |
62 | | * generate (e.g.) 512 bits of key material for use as two 256 bit keys, an |
63 | | * attacker can merely run once through the outer loop, but the user |
64 | | * always runs it twice. Shuffling output bytes requires computing the |
65 | | * entirety of the key material to assemble any subkey. This is something a |
66 | | * wise caller could do; we just do it for you. |
67 | | */ |
68 | | |
69 | 308k | #define BCRYPT_WORDS 8 |
70 | | #define BCRYPT_HASHSIZE (BCRYPT_WORDS * 4) |
71 | | |
72 | | static void |
73 | | bcrypt_hash(uint8_t *sha2pass, uint8_t *sha2salt, uint8_t *out) |
74 | 3.76k | { |
75 | 3.76k | blf_ctx state; |
76 | 3.76k | uint8_t ciphertext[BCRYPT_HASHSIZE] = |
77 | 3.76k | "OxychromaticBlowfishSwatDynamite"; |
78 | 3.76k | uint32_t cdata[BCRYPT_WORDS]; |
79 | 3.76k | int i; |
80 | 3.76k | uint16_t j; |
81 | 3.76k | size_t shalen = SHA512_DIGEST_LENGTH; |
82 | | |
83 | | /* key expansion */ |
84 | 3.76k | Blowfish_initstate(&state); |
85 | 3.76k | Blowfish_expandstate(&state, sha2salt, shalen, sha2pass, shalen); |
86 | 244k | for (i = 0; i < 64; i++) { |
87 | 240k | Blowfish_expand0state(&state, sha2salt, shalen); |
88 | 240k | Blowfish_expand0state(&state, sha2pass, shalen); |
89 | 240k | } |
90 | | |
91 | | /* encryption */ |
92 | 3.76k | j = 0; |
93 | 33.8k | for (i = 0; i < BCRYPT_WORDS; i++) |
94 | 30.0k | cdata[i] = Blowfish_stream2word(ciphertext, sizeof(ciphertext), |
95 | 30.0k | &j); |
96 | 244k | for (i = 0; i < 64; i++) |
97 | 240k | blf_enc(&state, cdata, BCRYPT_WORDS / 2); |
98 | | |
99 | | /* copy out */ |
100 | 33.8k | for (i = 0; i < BCRYPT_WORDS; i++) { |
101 | 30.0k | out[4 * i + 3] = (cdata[i] >> 24) & 0xff; |
102 | 30.0k | out[4 * i + 2] = (cdata[i] >> 16) & 0xff; |
103 | 30.0k | out[4 * i + 1] = (cdata[i] >> 8) & 0xff; |
104 | 30.0k | out[4 * i + 0] = cdata[i] & 0xff; |
105 | 30.0k | } |
106 | | |
107 | | /* zap */ |
108 | 3.76k | explicit_bzero(ciphertext, sizeof(ciphertext)); |
109 | 3.76k | explicit_bzero(cdata, sizeof(cdata)); |
110 | 3.76k | explicit_bzero(&state, sizeof(state)); |
111 | 3.76k | } |
112 | | |
113 | | int |
114 | | bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt, size_t saltlen, |
115 | | uint8_t *key, size_t keylen, unsigned int rounds) |
116 | 3.76k | { |
117 | 3.76k | uint8_t sha2pass[SHA512_DIGEST_LENGTH]; |
118 | 3.76k | uint8_t sha2salt[SHA512_DIGEST_LENGTH]; |
119 | 3.76k | uint8_t out[BCRYPT_HASHSIZE]; |
120 | 3.76k | uint8_t tmpout[BCRYPT_HASHSIZE]; |
121 | 3.76k | uint8_t *countsalt; |
122 | 3.76k | size_t i, j, amt, stride; |
123 | 3.76k | uint32_t count; |
124 | 3.76k | size_t origkeylen = keylen; |
125 | | |
126 | | /* nothing crazy */ |
127 | 3.76k | if (rounds < 1) |
128 | 0 | goto bad; |
129 | 3.76k | if (passlen == 0 || saltlen == 0 || keylen == 0 || |
130 | 3.76k | keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20) |
131 | 0 | goto bad; |
132 | 3.76k | if ((countsalt = calloc(1, saltlen + 4)) == NULL) |
133 | 0 | goto bad; |
134 | 3.76k | stride = (keylen + sizeof(out) - 1) / sizeof(out); |
135 | 3.76k | amt = (keylen + stride - 1) / stride; |
136 | | |
137 | 3.76k | memcpy(countsalt, salt, saltlen); |
138 | | |
139 | | /* collapse password */ |
140 | 3.76k | crypto_hash_sha512(sha2pass, pass, passlen); |
141 | | |
142 | | /* generate key, sizeof(out) at a time */ |
143 | 7.52k | for (count = 1; keylen > 0; count++) { |
144 | 3.76k | countsalt[saltlen + 0] = (count >> 24) & 0xff; |
145 | 3.76k | countsalt[saltlen + 1] = (count >> 16) & 0xff; |
146 | 3.76k | countsalt[saltlen + 2] = (count >> 8) & 0xff; |
147 | 3.76k | countsalt[saltlen + 3] = count & 0xff; |
148 | | |
149 | | /* first round, salt is salt */ |
150 | 3.76k | crypto_hash_sha512(sha2salt, countsalt, saltlen + 4); |
151 | | |
152 | 3.76k | bcrypt_hash(sha2pass, sha2salt, tmpout); |
153 | 3.76k | memcpy(out, tmpout, sizeof(out)); |
154 | | |
155 | 3.76k | for (i = 1; i < rounds; i++) { |
156 | | /* subsequent rounds, salt is previous output */ |
157 | 0 | crypto_hash_sha512(sha2salt, tmpout, sizeof(tmpout)); |
158 | 0 | bcrypt_hash(sha2pass, sha2salt, tmpout); |
159 | 0 | for (j = 0; j < sizeof(out); j++) |
160 | 0 | out[j] ^= tmpout[j]; |
161 | 0 | } |
162 | | |
163 | | /* |
164 | | * pbkdf2 deviation: output the key material non-linearly. |
165 | | */ |
166 | 3.76k | amt = MINIMUM(amt, keylen); |
167 | 124k | for (i = 0; i < amt; i++) { |
168 | 120k | size_t dest = i * stride + (count - 1); |
169 | 120k | if (dest >= origkeylen) |
170 | 0 | break; |
171 | 120k | key[dest] = out[i]; |
172 | 120k | } |
173 | 3.76k | keylen -= i; |
174 | 3.76k | } |
175 | | |
176 | | /* zap */ |
177 | 3.76k | freezero(countsalt, saltlen + 4); |
178 | 3.76k | explicit_bzero(out, sizeof(out)); |
179 | 3.76k | explicit_bzero(tmpout, sizeof(tmpout)); |
180 | | |
181 | 3.76k | return 0; |
182 | | |
183 | 0 | bad: |
184 | | /* overwrite with random in case caller doesn't check return code */ |
185 | 0 | arc4random_buf(key, keylen); |
186 | 0 | return -1; |
187 | 3.76k | } |
188 | | #endif /* HAVE_BCRYPT_PBKDF */ |