<commit_before>/* $OpenBSD: ip_blf.c,v 1.2 1998/03/02 15:24:21 provos Exp $ */
/*
 * Blowfish block cipher for OpenBSD
 * Copyright 1997 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * Implementation advice by <NAME> <<EMAIL>>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 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 <NAME>.
 * 4. 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
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * 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.
 */

/*
 * This code is derived from section 14.3 and the given source
 * in section V of Applied Cryptography, second edition.
 * Blowfish is an unpatented fast block cipher designed by
 * Bruce Schneier.
 */

#include <sys/param.h>
#include <sys/systm.h>

#include <netinet/ip_blf.h>

#undef inline
#ifdef __GNUC__
#define inline __inline
#else				/* !__GNUC__ */
#define inline
#endif				/* !__GNUC__ */

/* Function for Feistel Networks */

#define F(bc, x) ((((bc)->S[0][((x) & 0xFF000000) >> 24] \
		    + (bc)->S[1][((x) &0xFF0000 ) >> 16]) \
		   ^ (bc)->S[2][((x) & 0xFF00) >> 8]) \
		  + (bc)->S[3][(x) & 0x00FF])

#define BLFRND(bc,i,j,n) (i ^= F(bc,j) ^ (bc)->P[n])

void
Blowfish_encipher(c, xl, xr)
	blf_ctx *c;
	u_int32_t *xl;
	u_int32_t *xr;
{
	u_int32_t Xl;
	u_int32_t Xr;

	Xl = *xl;
	Xr = *xr;

	Xl ^= c->P[0];
	BLFRND(c, Xr, Xl, 1); BLFRND(c, Xl, Xr, 2);
	BLFRND(c, Xr, Xl, 3); BLFRND(c, Xl, Xr, 4);
	BLFRND(c, Xr, Xl, 5); BLFRND(c, Xl, Xr, 6);
	BLFRND(c, Xr, Xl, 7); BLFRND(c, Xl, Xr, 8);
	BLFRND(c, Xr, Xl, 9); BLFRND(c, Xl, Xr, 10);
	BLFRND(c, Xr, Xl, 11); BLFRND(c, Xl, Xr, 12);
	BLFRND(c, Xr, Xl, 13); BLFRND(c, Xl, Xr, 14);
	BLFRND(c, Xr, Xl, 15); BLFRND(c, Xl, Xr, 16);

	*xl = Xr ^ c->P[17];
	*xr = Xl;
}

void
Blowfish_decipher(c, xl, xr)
	blf_ctx *c;
	u_int32_t *xl;
	u_int32_t *xr;
{
	u_int32_t Xl;
	u_int32_t Xr;

	Xl = *xl;
	Xr = *xr;

	Xl ^= c->P[17];
	BLFRND(c, Xr, Xl, 16); BLFRND(c, Xl, Xr, 15);
	BLFRND(c, Xr, Xl, 14); BLFRND(c, Xl, Xr, 13);
	BLFRND(c, Xr, Xl, 12); BLFRND(c, Xl, Xr, 11);
	BLFRND(c, Xr, Xl, 10); BLFRND(c, Xl, Xr, 9);
	BLFRND(c, Xr, Xl, 8); BLFRND(c, Xl, Xr, 7);
	BLFRND(c, Xr, Xl, 6); BLFRND(c, Xl, Xr, 5);
	BLFRND(c, Xr, Xl, 4); BLFRND(c, Xl, Xr, 3);
	BLFRND(c, Xr, Xl, 2); BLFRND(c, Xl, Xr, 1);
<commit_msg>Fix an endianness problem (not an issue in libc).<commit_after>/* $OpenBSD: ip_blf.c,v 1.3 1999/02/23 02:44:20 angelos Exp $ */
/*
 * Blowfish block cipher for OpenBSD
 * Copyright 1997 <NAME> <<EMAIL>>
 * All rights reserved.
 *
 * Implementation advice by <NAME> <<EMAIL>>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 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 <NAME>.
 * 4. 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
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * 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.
 */

/*
 * This code is derived from section 14.3 and the given source
 * in section V of Applied Cryptography, second edition.
 * Blowfish is an unpatented fast block cipher designed by
 * <NAME>.
 */

#include <sys/param.h>
#include <sys/systm.h>

#include <netinet/ip_blf.h>

#undef inline
#ifdef __GNUC__
#define inline __inline
#else				/* !__GNUC__ */
#define inline
#endif				/* !__GNUC__ */

/* Function for Feistel Networks */

#define F(bc, x) ((((bc)->S[0][((x) & 0xFF000000) >> 24] \
		    + (bc)->S[1][((x) &0xFF0000 ) >> 16]) \
		   ^ (bc)->S[2][((x) & 0xFF00) >> 8]) \
		  + (bc)->S[3][(x) & 0x00FF])

#define BLFRND(bc,i,j,n) (i ^= F(bc,j) ^ (bc)->P[n])

void
Blowfish_encipher(c, xl, xr)
	blf_ctx *c;
	u_int32_t *xl;
	u_int32_t *xr;
{
	u_int32_t Xl;
	u_int32_t Xr;

	Xl = ntohl(*xl);
	Xr = ntohl(*xr);

	Xl ^= c->P[0];
	BLFRND(c, Xr, Xl, 1); BLFRND(c, Xl, Xr, 2);
	BLFRND(c, Xr, Xl, 3); BLFRND(c, Xl, Xr, 4);
	BLFRND(c, Xr, Xl, 5); BLFRND(c, Xl, Xr, 6);
	BLFRND(c, Xr, Xl, 7); BLFRND(c, Xl, Xr, 8);
	BLFRND(c, Xr, Xl, 9); BLFRND(c, Xl, Xr, 10);
	BLFRND(c, Xr, Xl, 11); BLFRND(c, Xl, Xr, 12);
	BLFRND(c, Xr, Xl, 13); BLFRND(c, Xl, Xr, 14);
	BLFRND(c, Xr, Xl, 15); BLFRND(c, Xl, Xr, 16);

	*xl = Xr ^ c->P[17];
	*xr = Xl;
}

void
Blowfish_decipher(c, xl, xr)
	blf_ctx *c;
	u_int32_t *xl;
	u_int32_t *xr;
{
	u_int32_t Xl;
	u_int32_t Xr;

	Xl = htonl(*xl);
	Xr = htonl(*xr);

	Xl ^= c->P[17];
	BLFRND(c, Xr, Xl, 16); BLFRND(c, Xl, Xr, 15);
	BLFRND(c, Xr, Xl, 14); BLFRND(c, Xl, Xr, 13);
	BLFRND(c, Xr, Xl, 12); BLFRND(c, Xl, Xr, 11);
	BLFRND(c, Xr, Xl, 10); BLFRND(c, Xl, Xr, 9);
	BLFRND(c, Xr, Xl, 8); BLFRND(c, Xl, Xr, 7);
	BLFRND(c, Xr, Xl, 6); BLFRND(c, Xl, Xr, 5);
	BLFRND(c, Xr, Xl, 4); BLFRND(c, Xl, Xr, 3);
	BLFRND(c, Xr, Xl, 2); BLFRND(c, Xl, Xr, 1);

{'fraction_non_alphanumeric': 0.1417486490231398, 'fraction_numerical': 0.03353193847859221, 'mean_word_length': 3.82809364548495, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2799042', 'n_tokens_mistral': 3186, 'n_tokens_neox': 2850, 'n_words': 999}