summaryrefslogtreecommitdiff
path: root/libs/libssh2/src/blowfish.c
diff options
context:
space:
mode:
Diffstat (limited to 'libs/libssh2/src/blowfish.c')
-rw-r--r--libs/libssh2/src/blowfish.c168
1 files changed, 111 insertions, 57 deletions
diff --git a/libs/libssh2/src/blowfish.c b/libs/libssh2/src/blowfish.c
index 4aefc66ac7..5de54f5963 100644
--- a/libs/libssh2/src/blowfish.c
+++ b/libs/libssh2/src/blowfish.c
@@ -1,7 +1,8 @@
/* $OpenBSD: blowfish.c,v 1.18 2004/11/02 17:23:26 hshoexer Exp $ */
/*
- * Blowfish block cipher for OpenBSD
- * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
+ * Blowfish for OpenBSD - a fast block cipher designed by Bruce Schneier
+ *
+ * Copyright (C) Niels Provos <provos@physnet.uni-hamburg.de>
* All rights reserved.
*
* Implementation advice by David Mazieres <dm@lcs.mit.edu>.
@@ -14,10 +15,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by Niels Provos.
- * 4. The name of the author may not be used to endorse or promote products
+ * 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
@@ -30,6 +28,8 @@
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
*/
/*
@@ -39,38 +39,81 @@
* Bruce Schneier.
*/
+#if defined(LIBSSH2_BCRYPT_PBKDF_C) || defined(_DEBUG_BLOWFISH)
#if !defined(HAVE_BCRYPT_PBKDF) && (!defined(HAVE_BLOWFISH_INITSTATE) || \
!defined(HAVE_BLOWFISH_EXPAND0STATE) || \
!defined(HAVE_BLF_ENC))
-#if 0
-#include <stdio.h> /* used for debugging */
+#ifdef _DEBUG_BLOWFISH
+#include <stdio.h>
#include <string.h>
+#include <inttypes.h>
+#endif
+
+/* Schneier specifies a maximum key length of 56 bytes.
+ * This ensures that every key bit affects every cipher
+ * bit. However, the subkeys can hold up to 72 bytes.
+ * Warning: For normal blowfish encryption only 56 bytes
+ * of the key affect all cipherbits.
+ */
+
+#define BLF_N 16 /* Number of Subkeys */
+#define BLF_MAXKEYLEN ((BLF_N-2)*4) /* 448 bits */
+#define BLF_MAXUTILIZED ((BLF_N + 2)*4) /* 576 bits */
+
+/* Blowfish context */
+typedef struct BlowfishContext {
+ uint32_t S[4][256]; /* S-Boxes */
+ uint32_t P[BLF_N + 2]; /* Subkeys */
+} blf_ctx;
+
+/* Raw access to customized Blowfish
+ * blf_key is just:
+ * Blowfish_initstate( state )
+ * Blowfish_expand0state( state, key, keylen )
+ */
+
+static void Blowfish_encipher(blf_ctx *, uint32_t *, uint32_t *);
+#ifdef _DEBUG_BLOWFISH
+static void Blowfish_decipher(blf_ctx *, uint32_t *, uint32_t *);
+#endif
+static void Blowfish_initstate(blf_ctx *);
+static void Blowfish_expand0state(blf_ctx *, const uint8_t *, uint16_t);
+static void Blowfish_expandstate
+(blf_ctx *, const uint8_t *, uint16_t, const uint8_t *, uint16_t);
+
+/* Standard Blowfish */
+
+#ifdef _DEBUG_BLOWFISH
+static void blf_key(blf_ctx *, const uint8_t *, uint16_t);
+#endif
+static void blf_enc(blf_ctx *, uint32_t *, uint16_t);
+#ifdef _DEBUG_BLOWFISH
+static void blf_dec(blf_ctx *, uint32_t *, uint16_t);
#endif
-#include <sys/types.h>
+#if 0
+static void blf_ecb_encrypt(blf_ctx *, uint8_t *, uint32_t);
+static void blf_ecb_decrypt(blf_ctx *, uint8_t *, uint32_t);
-#include "libssh2.h"
-#include "blf.h"
+static void blf_cbc_encrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t);
+static void blf_cbc_decrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t);
+#endif
-#undef inline
-#ifdef __GNUC__
-#define inline __inline
-#else /* !__GNUC__ */
-#define inline
-#endif /* !__GNUC__ */
+/* Converts uint8_t to uint32_t */
+static uint32_t Blowfish_stream2word(const uint8_t *, uint16_t, uint16_t *);
/* Function for Feistel Networks */
-#define F(s, x) ((((s)[ (((x)>>24)&0xFF)] \
- + (s)[0x100 + (((x)>>16)&0xFF)]) \
- ^ (s)[0x200 + (((x)>> 8)&0xFF)]) \
- + (s)[0x300 + ( (x) &0xFF)])
+#define F(s, x) ((((s)[ (((x) >> 24) & 0xFF)] \
+ + (s)[0x100 + (((x) >> 16) & 0xFF)]) \
+ ^ (s)[0x200 + (((x) >> 8) & 0xFF)]) \
+ + (s)[0x300 + ( (x) & 0xFF)])
#define BLFRND(s,p,i,j,n) (i ^= F(s,j) ^ (p)[n])
-void
+static void
Blowfish_encipher(blf_ctx *c, uint32_t *xl, uint32_t *xr)
{
uint32_t Xl;
@@ -95,7 +138,8 @@ Blowfish_encipher(blf_ctx *c, uint32_t *xl, uint32_t *xr)
*xr = Xl;
}
-void
+#ifdef _DEBUG_BLOWFISH
+static void
Blowfish_decipher(blf_ctx *c, uint32_t *xl, uint32_t *xr)
{
uint32_t Xl;
@@ -119,8 +163,9 @@ Blowfish_decipher(blf_ctx *c, uint32_t *xl, uint32_t *xr)
*xl = Xr ^ p[0];
*xr = Xl;
}
+#endif
-void
+static void
Blowfish_initstate(blf_ctx *c)
{
/* P-box and S-box tables initialized with digits of Pi */
@@ -399,7 +444,7 @@ Blowfish_initstate(blf_ctx *c)
*c = initstate;
}
-uint32_t
+static uint32_t
Blowfish_stream2word(const uint8_t *data, uint16_t databytes,
uint16_t *current)
{
@@ -420,12 +465,12 @@ Blowfish_stream2word(const uint8_t *data, uint16_t databytes,
return temp;
}
-void
+static void
Blowfish_expand0state(blf_ctx *c, const uint8_t *key, uint16_t keybytes)
{
- uint16_t i;
+ int i;
+ int k;
uint16_t j;
- uint16_t k;
uint32_t temp;
uint32_t datal;
uint32_t datar;
@@ -457,14 +502,13 @@ Blowfish_expand0state(blf_ctx *c, const uint8_t *key, uint16_t keybytes)
}
}
-
-void
+static void
Blowfish_expandstate(blf_ctx *c, const uint8_t *data, uint16_t databytes,
const uint8_t *key, uint16_t keybytes)
{
- uint16_t i;
+ int i;
+ int k;
uint16_t j;
- uint16_t k;
uint32_t temp;
uint32_t datal;
uint32_t datar;
@@ -501,7 +545,8 @@ Blowfish_expandstate(blf_ctx *c, const uint8_t *data, uint16_t databytes,
}
-void
+#ifdef _DEBUG_BLOWFISH
+static void
blf_key(blf_ctx *c, const uint8_t *k, uint16_t len)
{
/* Initialize S-boxes and subkeys with Pi */
@@ -510,8 +555,9 @@ blf_key(blf_ctx *c, const uint8_t *k, uint16_t len)
/* Transform S-boxes and subkeys with key */
Blowfish_expand0state(c, k, len);
}
+#endif
-void
+static void
blf_enc(blf_ctx *c, uint32_t *data, uint16_t blocks)
{
uint32_t *d;
@@ -524,7 +570,8 @@ blf_enc(blf_ctx *c, uint32_t *data, uint16_t blocks)
}
}
-void
+#ifdef _DEBUG_BLOWFISH
+static void
blf_dec(blf_ctx *c, uint32_t *data, uint16_t blocks)
{
uint32_t *d;
@@ -536,8 +583,10 @@ blf_dec(blf_ctx *c, uint32_t *data, uint16_t blocks)
d += 2;
}
}
+#endif
-void
+#if 0
+static void
blf_ecb_encrypt(blf_ctx *c, uint8_t *data, uint32_t len)
{
uint32_t l, r;
@@ -547,11 +596,11 @@ blf_ecb_encrypt(blf_ctx *c, uint8_t *data, uint32_t len)
l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
Blowfish_encipher(c, &l, &r);
- data[0] = l >> 24 & 0xff;
+ data[0] = (uint8_t)(l >> 24 & 0xff);
data[1] = l >> 16 & 0xff;
data[2] = l >> 8 & 0xff;
data[3] = l & 0xff;
- data[4] = r >> 24 & 0xff;
+ data[4] = (uint8_t)(r >> 24 & 0xff);
data[5] = r >> 16 & 0xff;
data[6] = r >> 8 & 0xff;
data[7] = r & 0xff;
@@ -559,7 +608,7 @@ blf_ecb_encrypt(blf_ctx *c, uint8_t *data, uint32_t len)
}
}
-void
+static void
blf_ecb_decrypt(blf_ctx *c, uint8_t *data, uint32_t len)
{
uint32_t l, r;
@@ -569,11 +618,11 @@ blf_ecb_decrypt(blf_ctx *c, uint8_t *data, uint32_t len)
l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
Blowfish_decipher(c, &l, &r);
- data[0] = l >> 24 & 0xff;
+ data[0] = (uint8_t)(l >> 24 & 0xff);
data[1] = l >> 16 & 0xff;
data[2] = l >> 8 & 0xff;
data[3] = l & 0xff;
- data[4] = r >> 24 & 0xff;
+ data[4] = (uint8_t)(r >> 24 & 0xff);
data[5] = r >> 16 & 0xff;
data[6] = r >> 8 & 0xff;
data[7] = r & 0xff;
@@ -581,7 +630,7 @@ blf_ecb_decrypt(blf_ctx *c, uint8_t *data, uint32_t len)
}
}
-void
+static void
blf_cbc_encrypt(blf_ctx *c, uint8_t *iv, uint8_t *data, uint32_t len)
{
uint32_t l, r;
@@ -593,11 +642,11 @@ blf_cbc_encrypt(blf_ctx *c, uint8_t *iv, uint8_t *data, uint32_t len)
l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
Blowfish_encipher(c, &l, &r);
- data[0] = l >> 24 & 0xff;
+ data[0] = (uint8_t)(l >> 24 & 0xff);
data[1] = l >> 16 & 0xff;
data[2] = l >> 8 & 0xff;
data[3] = l & 0xff;
- data[4] = r >> 24 & 0xff;
+ data[4] = (uint8_t)(r >> 24 & 0xff);
data[5] = r >> 16 & 0xff;
data[6] = r >> 8 & 0xff;
data[7] = r & 0xff;
@@ -606,7 +655,7 @@ blf_cbc_encrypt(blf_ctx *c, uint8_t *iv, uint8_t *data, uint32_t len)
}
}
-void
+static void
blf_cbc_decrypt(blf_ctx *c, uint8_t *iva, uint8_t *data, uint32_t len)
{
uint32_t l, r;
@@ -619,11 +668,11 @@ blf_cbc_decrypt(blf_ctx *c, uint8_t *iva, uint8_t *data, uint32_t len)
l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
Blowfish_decipher(c, &l, &r);
- data[0] = l >> 24 & 0xff;
+ data[0] = (uint8_t)(l >> 24 & 0xff);
data[1] = l >> 16 & 0xff;
data[2] = l >> 8 & 0xff;
data[3] = l & 0xff;
- data[4] = r >> 24 & 0xff;
+ data[4] = (uint8_t)(r >> 24 & 0xff);
data[5] = r >> 16 & 0xff;
data[6] = r >> 8 & 0xff;
data[7] = r & 0xff;
@@ -635,31 +684,31 @@ blf_cbc_decrypt(blf_ctx *c, uint8_t *iva, uint8_t *data, uint32_t len)
l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7];
Blowfish_decipher(c, &l, &r);
- data[0] = l >> 24 & 0xff;
+ data[0] = (uint8_t)(l >> 24 & 0xff);
data[1] = l >> 16 & 0xff;
data[2] = l >> 8 & 0xff;
data[3] = l & 0xff;
- data[4] = r >> 24 & 0xff;
+ data[4] = (uint8_t)(r >> 24 & 0xff);
data[5] = r >> 16 & 0xff;
data[6] = r >> 8 & 0xff;
data[7] = r & 0xff;
for(j = 0; j < 8; j++)
data[j] ^= iva[j];
}
+#endif
-#if 0
-void
+#ifdef _DEBUG_BLOWFISH
+static void
report(uint32_t data[], uint16_t len)
{
- uint16_t i;
+ int i;
for(i = 0; i < len; i += 2)
- printf("Block %0hd: %08lx %08lx.\n",
- i / 2, data[i], data[i + 1]);
+ printf("Block %d: 0x%08lx 0x%08lx.\n",
+ i / 2, (unsigned long)data[i], (unsigned long)data[i + 1]);
}
-void
+int
main(void)
{
-
blf_ctx c;
char key[] = "AAAAA";
char key2[] = "abcdefghijklmnopqrstuvwxyz";
@@ -682,12 +731,15 @@ main(void)
report(data, 10);
/* Second test */
- blf_key(&c, (uint8_t *) key2, strlen(key2));
+ blf_key(&c, (uint8_t *) key2, (uint16_t)strlen(key2));
blf_enc(&c, data2, 1);
printf("\nShould read as: 0x324ed0fe 0xf413a203.\n");
report(data2, 2);
blf_dec(&c, data2, 1);
+ printf("\nShould read as: 0x424c4f57 0x46495348.\n");
report(data2, 2);
+
+ return 0;
}
#endif
@@ -695,3 +747,5 @@ main(void)
(!defined(HAVE_BLOWFISH_INITSTATE) || \
!defined(HAVE_BLOWFISH_EXPAND0STATE) || \
'!defined(HAVE_BLF_ENC)) */
+
+#endif /* defined(LIBSSH2_BCRYPT_PBKDF_C) || defined(_DEBUG_BLOWFISH) */