Skip to content

Commit 2f0aba1

Browse files
committed
Updated libssh2 to 1.9.0.
1 parent e4cd203 commit 2f0aba1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+11244
-3866
lines changed

src/utility/libssh2/agent.c

+121-72
Large diffs are not rendered by default.

src/utility/libssh2/bcrypt_pbkdf.c

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/* $OpenBSD: bcrypt_pbkdf.c,v 1.4 2013/07/29 00:55:53 tedu Exp $ */
2+
/*
3+
* Copyright (c) 2013 Ted Unangst <[email protected]>
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+
19+
#ifndef HAVE_BCRYPT_PBKDF
20+
21+
#include "libssh2_priv.h"
22+
#include <stdlib.h>
23+
#include <sys/types.h>
24+
#ifdef HAVE_SYS_PARAM_H
25+
#include <sys/param.h>
26+
#endif
27+
28+
#include "blf.h"
29+
30+
#define MINIMUM(a,b) (((a) < (b)) ? (a) : (b))
31+
32+
/*
33+
* pkcs #5 pbkdf2 implementation using the "bcrypt" hash
34+
*
35+
* The bcrypt hash function is derived from the bcrypt password hashing
36+
* function with the following modifications:
37+
* 1. The input password and salt are preprocessed with SHA512.
38+
* 2. The output length is expanded to 256 bits.
39+
* 3. Subsequently the magic string to be encrypted is lengthened and modifed
40+
* to "OxychromaticBlowfishSwatDynamite"
41+
* 4. The hash function is defined to perform 64 rounds of initial state
42+
* expansion. (More rounds are performed by iterating the hash.)
43+
*
44+
* Note that this implementation pulls the SHA512 operations into the caller
45+
* as a performance optimization.
46+
*
47+
* One modification from official pbkdf2. Instead of outputting key material
48+
* linearly, we mix it. pbkdf2 has a known weakness where if one uses it to
49+
* generate (i.e.) 512 bits of key material for use as two 256 bit keys, an
50+
* attacker can merely run once through the outer loop below, but the user
51+
* always runs it twice. Shuffling output bytes requires computing the
52+
* entirety of the key material to assemble any subkey. This is something a
53+
* wise caller could do; we just do it for you.
54+
*/
55+
56+
#define BCRYPT_BLOCKS 8
57+
#define BCRYPT_HASHSIZE (BCRYPT_BLOCKS * 4)
58+
59+
static void
60+
bcrypt_hash(uint8_t *sha2pass, uint8_t *sha2salt, uint8_t *out)
61+
{
62+
blf_ctx state;
63+
uint8_t ciphertext[BCRYPT_HASHSIZE] =
64+
"OxychromaticBlowfishSwatDynamite";
65+
uint32_t cdata[BCRYPT_BLOCKS];
66+
int i;
67+
uint16_t j;
68+
size_t shalen = SHA512_DIGEST_LENGTH;
69+
70+
/* key expansion */
71+
Blowfish_initstate(&state);
72+
Blowfish_expandstate(&state, sha2salt, shalen, sha2pass, shalen);
73+
for(i = 0; i < 64; i++) {
74+
Blowfish_expand0state(&state, sha2salt, shalen);
75+
Blowfish_expand0state(&state, sha2pass, shalen);
76+
}
77+
78+
/* encryption */
79+
j = 0;
80+
for(i = 0; i < BCRYPT_BLOCKS; i++)
81+
cdata[i] = Blowfish_stream2word(ciphertext, sizeof(ciphertext),
82+
&j);
83+
for(i = 0; i < 64; i++)
84+
blf_enc(&state, cdata, sizeof(cdata) / sizeof(uint64_t));
85+
86+
/* copy out */
87+
for(i = 0; i < BCRYPT_BLOCKS; i++) {
88+
out[4 * i + 3] = (cdata[i] >> 24) & 0xff;
89+
out[4 * i + 2] = (cdata[i] >> 16) & 0xff;
90+
out[4 * i + 1] = (cdata[i] >> 8) & 0xff;
91+
out[4 * i + 0] = cdata[i] & 0xff;
92+
}
93+
94+
/* zap */
95+
_libssh2_explicit_zero(ciphertext, sizeof(ciphertext));
96+
_libssh2_explicit_zero(cdata, sizeof(cdata));
97+
_libssh2_explicit_zero(&state, sizeof(state));
98+
}
99+
100+
int
101+
bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt,
102+
size_t saltlen,
103+
uint8_t *key, size_t keylen, unsigned int rounds)
104+
{
105+
uint8_t sha2pass[SHA512_DIGEST_LENGTH];
106+
uint8_t sha2salt[SHA512_DIGEST_LENGTH];
107+
uint8_t out[BCRYPT_HASHSIZE];
108+
uint8_t tmpout[BCRYPT_HASHSIZE];
109+
uint8_t *countsalt;
110+
size_t i, j, amt, stride;
111+
uint32_t count;
112+
size_t origkeylen = keylen;
113+
libssh2_sha512_ctx ctx;
114+
115+
/* nothing crazy */
116+
if(rounds < 1)
117+
return -1;
118+
if(passlen == 0 || saltlen == 0 || keylen == 0 ||
119+
keylen > sizeof(out) * sizeof(out) || saltlen > 1<<20)
120+
return -1;
121+
countsalt = calloc(1, saltlen + 4);
122+
if(countsalt == NULL)
123+
return -1;
124+
stride = (keylen + sizeof(out) - 1) / sizeof(out);
125+
amt = (keylen + stride - 1) / stride;
126+
127+
memcpy(countsalt, salt, saltlen);
128+
129+
/* collapse password */
130+
libssh2_sha512_init(&ctx);
131+
libssh2_sha512_update(ctx, pass, passlen);
132+
libssh2_sha512_final(ctx, sha2pass);
133+
134+
/* generate key, sizeof(out) at a time */
135+
for(count = 1; keylen > 0; count++) {
136+
countsalt[saltlen + 0] = (count >> 24) & 0xff;
137+
countsalt[saltlen + 1] = (count >> 16) & 0xff;
138+
countsalt[saltlen + 2] = (count >> 8) & 0xff;
139+
countsalt[saltlen + 3] = count & 0xff;
140+
141+
/* first round, salt is salt */
142+
libssh2_sha512_init(&ctx);
143+
libssh2_sha512_update(ctx, countsalt, saltlen + 4);
144+
libssh2_sha512_final(ctx, sha2salt);
145+
146+
bcrypt_hash(sha2pass, sha2salt, tmpout);
147+
memcpy(out, tmpout, sizeof(out));
148+
149+
for(i = 1; i < rounds; i++) {
150+
/* subsequent rounds, salt is previous output */
151+
libssh2_sha512_init(&ctx);
152+
libssh2_sha512_update(ctx, tmpout, sizeof(tmpout));
153+
libssh2_sha512_final(ctx, sha2salt);
154+
155+
bcrypt_hash(sha2pass, sha2salt, tmpout);
156+
for(j = 0; j < sizeof(out); j++)
157+
out[j] ^= tmpout[j];
158+
}
159+
160+
/*
161+
* pbkdf2 deviation: ouput the key material non-linearly.
162+
*/
163+
amt = MINIMUM(amt, keylen);
164+
for(i = 0; i < amt; i++) {
165+
size_t dest = i * stride + (count - 1);
166+
if(dest >= origkeylen) {
167+
break;
168+
}
169+
key[dest] = out[i];
170+
}
171+
keylen -= i;
172+
}
173+
174+
/* zap */
175+
_libssh2_explicit_zero(out, sizeof(out));
176+
free(countsalt);
177+
178+
return 0;
179+
}
180+
#endif /* HAVE_BCRYPT_PBKDF */

src/utility/libssh2/blf.h

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* $OpenBSD: blf.h,v 1.7 2007/03/14 17:59:41 grunk Exp $ */
2+
/*
3+
* Blowfish - a fast block cipher designed by Bruce Schneier
4+
*
5+
* Copyright 1997 Niels Provos <[email protected]>
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
* 3. All advertising materials mentioning features or use of this software
17+
* must display the following acknowledgement:
18+
* This product includes software developed by Niels Provos.
19+
* 4. The name of the author may not be used to endorse or promote products
20+
* derived from this software without specific prior written permission.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
*/
33+
34+
#ifndef _BLF_H_
35+
#define _BLF_H_
36+
37+
#if !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H)
38+
39+
/* Schneier specifies a maximum key length of 56 bytes.
40+
* This ensures that every key bit affects every cipher
41+
* bit. However, the subkeys can hold up to 72 bytes.
42+
* Warning: For normal blowfish encryption only 56 bytes
43+
* of the key affect all cipherbits.
44+
*/
45+
46+
#define BLF_N 16 /* Number of Subkeys */
47+
#define BLF_MAXKEYLEN ((BLF_N-2)*4) /* 448 bits */
48+
#define BLF_MAXUTILIZED ((BLF_N + 2)*4) /* 576 bits */
49+
50+
/* Blowfish context */
51+
typedef struct BlowfishContext {
52+
uint32_t S[4][256]; /* S-Boxes */
53+
uint32_t P[BLF_N + 2]; /* Subkeys */
54+
} blf_ctx;
55+
56+
/* Raw access to customized Blowfish
57+
* blf_key is just:
58+
* Blowfish_initstate( state )
59+
* Blowfish_expand0state( state, key, keylen )
60+
*/
61+
62+
void Blowfish_encipher(blf_ctx *, uint32_t *, uint32_t *);
63+
void Blowfish_decipher(blf_ctx *, uint32_t *, uint32_t *);
64+
void Blowfish_initstate(blf_ctx *);
65+
void Blowfish_expand0state(blf_ctx *, const uint8_t *, uint16_t);
66+
void Blowfish_expandstate
67+
(blf_ctx *, const uint8_t *, uint16_t, const uint8_t *, uint16_t);
68+
69+
/* Standard Blowfish */
70+
71+
void blf_key(blf_ctx *, const uint8_t *, uint16_t);
72+
void blf_enc(blf_ctx *, uint32_t *, uint16_t);
73+
void blf_dec(blf_ctx *, uint32_t *, uint16_t);
74+
75+
void blf_ecb_encrypt(blf_ctx *, uint8_t *, uint32_t);
76+
void blf_ecb_decrypt(blf_ctx *, uint8_t *, uint32_t);
77+
78+
void blf_cbc_encrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t);
79+
void blf_cbc_decrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t);
80+
81+
/* Converts uint8_t to uint32_t */
82+
uint32_t Blowfish_stream2word(const uint8_t *, uint16_t, uint16_t *);
83+
84+
/* bcrypt with pbkd */
85+
int bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt,
86+
size_t saltlen,
87+
uint8_t *key, size_t keylen, unsigned int rounds);
88+
89+
#endif /* !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H) */
90+
#endif /* _BLF_H */

0 commit comments

Comments
 (0)