├── .gitignore
├── gen-tables.sh
├── UNLICENSE
├── Makefile
├── blowfish.h
├── w32-compat
├── getopt.h
└── unistd.h
├── blowpipe.1
├── README.md
├── tests
├── vectors2.h
└── tests.c
├── blowpipe.c
└── blowfish.c
/.gitignore:
--------------------------------------------------------------------------------
1 | blowpipe
2 | tests/tests
3 | tests/key.dat
4 |
--------------------------------------------------------------------------------
/gen-tables.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | # This script computes the Blowfish initialization tables using POSIX
4 | # bc, or using the pi command if it is available (which is much faster
5 | # than GNU bc).
6 |
7 | set -e
8 |
9 | if $(command -v pi > /dev/null 2>&1); then
10 | pi=$(echo "obase=16; $(pi 10040)" | bc | tr -d '\n\\' | tail -c+3)
11 | else
12 | cmd='obase=16; scale=10040; a(1) * 4'
13 | pi="$(echo "$cmd" | bc -l | tr -d '\n\\' | tail -c+3)"
14 | fi
15 | ints="$(echo $pi | tr A-F a-f | head -c8336 | sed -r 's/.{8}/0x\0,\\n/g')"
16 |
17 | echo 'static const uint32_t blowfish_p[] = {'
18 | printf $ints | head -n18 | \
19 | paste -d ' ' - - - - | sed 's/^/ /g'
20 | echo '};'
21 | echo
22 |
23 | echo 'static const uint32_t blowfish_s[] = {'
24 | printf $ints | tail -n+19 | head -n1024 | \
25 | paste -d ' ' - - - - | sed 's/^/ /g'
26 | echo '};'
27 |
--------------------------------------------------------------------------------
/UNLICENSE:
--------------------------------------------------------------------------------
1 | This is free and unencumbered software released into the public domain.
2 |
3 | Anyone is free to copy, modify, publish, use, compile, sell, or
4 | distribute this software, either in source code form or as a compiled
5 | binary, for any purpose, commercial or non-commercial, and by any
6 | means.
7 |
8 | In jurisdictions that recognize copyright laws, the author or authors
9 | of this software dedicate any and all copyright interest in the
10 | software to the public domain. We make this dedication for the benefit
11 | of the public at large and to the detriment of our heirs and
12 | successors. We intend this dedication to be an overt act of
13 | relinquishment in perpetuity of all present and future rights to this
14 | software under copyright law.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | For more information, please refer to
25 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .POSIX:
2 | .SUFFIXES:
3 | CC = cc -std=c99
4 | CFLAGS = -Wall -Wextra -O3 -g3
5 | PREFIX = /usr/local
6 | EXEEXT =
7 |
8 | all: blowpipe$(EXEEXT)
9 |
10 | blowpipe$(EXEEXT): blowpipe.c blowfish.c blowfish.h w32-compat/unistd.h
11 | $(CC) $(LDFLAGS) $(CFLAGS) -o $@ blowpipe.c blowfish.c
12 |
13 | tests/tests$(EXEEXT): tests/tests.c blowfish.c blowfish.h tests/vectors2.h
14 | $(CC) $(LDFLAGS) $(CFLAGS) -o $@ tests/tests.c blowfish.c
15 |
16 | tests/key.dat:
17 | printf "helloworld" > $@
18 |
19 | blowpipe-cli.c: blowpipe.c blowfish.c blowfish.h w32-compat/unistd.h
20 | echo '#define _POSIX_C_SOURCE 200112L' | \
21 | cat - blowfish.h w32-compat/getopt.h w32-compat/unistd.h \
22 | blowfish.c blowpipe.c | \
23 | sed -r 's@^(#include +".+)@/* \1 */@g' > $@
24 |
25 | test: check
26 | check: tests/tests$(EXEEXT) tests/key.dat blowpipe$(EXEEXT)
27 | tests/tests$(EXEEXT)
28 | for len in $$(seq 0 10) $$(seq 65500 65600); do \
29 | head -c$$len /dev/urandom | \
30 | ./blowpipe$(EXEEXT) -E -c3 -ktests/key.dat | \
31 | ./blowpipe$(EXEEXT) -D -ktests/key.dat > /dev/null; \
32 | done
33 |
34 | amalgamation: blowpipe-cli.c
35 |
36 | install: blowpipe$(EXEEXT) blowpipe.1
37 | mkdir -p $(DESTDIR)$(PREFIX)/bin
38 | mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1
39 | cp -f blowpipe$(EXEEXT) $(DESTDIR)$(PREFIX)/bin
40 | gzip < blowpipe.1 > $(DESTDIR)$(PREFIX)/share/man/man1/blowpipe.1.gz
41 |
42 | clean:
43 | rm -f blowpipe$(EXEEXT) tests/tests$(EXEEXT)
44 | rm -f tests/key.dat blowpipe-cli.c
45 |
--------------------------------------------------------------------------------
/blowfish.h:
--------------------------------------------------------------------------------
1 | /* C99 Blowfish implementation
2 | *
3 | * This is free and unencumbered software released into the public domain.
4 | */
5 | #ifndef BLOWFISH_H
6 | #define BLOWFISH_H
7 |
8 | #include
9 |
10 | #define BLOWFISH_BLOCK_LENGTH 8
11 | #define BLOWFISH_SALT_LENGTH 16
12 | #define BLOWFISH_DIGEST_LENGTH 24
13 | #define BLOWFISH_MAX_KEY_LENGTH 72
14 | #define BLOWFISH_MAX_COST 63
15 |
16 | struct blowfish {
17 | uint32_t p[18];
18 | uint32_t s[4][256];
19 | };
20 |
21 | /* Initialize a cipher context with the given key.
22 | *
23 | * The maximum key length is 72 bytes. Generally the key length should
24 | * not exceed 56 bytes since the last 16 bytes do not affect every bit
25 | * of each subkey.
26 | */
27 | void blowfish_init(struct blowfish *, const void *key, int len);
28 |
29 | /* Encrypt a pair of 32-bit integers using the given context.
30 | */
31 | void blowfish_encrypt(const struct blowfish *, uint32_t *, uint32_t *);
32 |
33 | /* Decrypt a pair of 32-bit integers using the given context.
34 | */
35 | void blowfish_decrypt(const struct blowfish *, uint32_t *, uint32_t *);
36 |
37 | /* Compute the bcrypt digest for a given password.
38 | *
39 | * All inputs and outputs are raw bytes, not base-64 encoded.
40 | *
41 | * The digest must have space for 24 bytes (BLOWFISH_DIGEST_LENGTH).
42 | *
43 | * The maximum password length is 72 bytes. Note: As a convention,
44 | * OpenBSD's bcrypt() includes the null terminator byte in the key.
45 | *
46 | * The salt must be 16 bytes (BLOWFISH_SALT_LENGTH).
47 | *
48 | * The maximum cost is 63.
49 | */
50 | void blowfish_bcrypt(
51 | void *digest,
52 | const void *pwd,
53 | int len,
54 | const void *salt,
55 | int cost
56 | );
57 |
58 | #endif
59 |
--------------------------------------------------------------------------------
/w32-compat/getopt.h:
--------------------------------------------------------------------------------
1 | /* A minimal POSIX getopt() implementation in ANSI C
2 | *
3 | * This is free and unencumbered software released into the public domain.
4 | *
5 | * This implementation supports the convention of resetting the option
6 | * parser by assigning optind to 0. This resets the internal state
7 | * appropriately.
8 | *
9 | * Ref: http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html
10 | */
11 | #if !defined(GETOPT_H) && defined(_WIN32)
12 | #define GETOPT_H
13 |
14 | #include
15 | #include
16 | #include
17 |
18 | static int optind = 1;
19 | static int opterr = 1;
20 | static int optopt;
21 | static char *optarg;
22 |
23 | static int
24 | getopt(int argc, char * const argv[], const char *optstring)
25 | {
26 | static int optpos = 1;
27 | const char *arg;
28 | (void)argc;
29 |
30 | /* Reset? */
31 | if (optind == 0) {
32 | optind = 1;
33 | optpos = 1;
34 | }
35 |
36 | arg = argv[optind];
37 | if (arg && strcmp(arg, "--") == 0) {
38 | optind++;
39 | return -1;
40 | } else if (!arg || arg[0] != '-' || !isalnum(arg[1])) {
41 | return -1;
42 | } else {
43 | const char *opt = strchr(optstring, arg[optpos]);
44 | optopt = arg[optpos];
45 | if (!opt) {
46 | if (opterr && *optstring != ':')
47 | fprintf(stderr, "%s: illegal option: %c\n", argv[0], optopt);
48 | return '?';
49 | } else if (opt[1] == ':') {
50 | if (arg[optpos + 1]) {
51 | optarg = (char *)arg + optpos + 1;
52 | optind++;
53 | optpos = 1;
54 | return optopt;
55 | } else if (argv[optind + 1]) {
56 | optarg = (char *)argv[optind + 1];
57 | optind += 2;
58 | optpos = 1;
59 | return optopt;
60 | } else {
61 | if (opterr && *optstring != ':')
62 | fprintf(stderr,
63 | "%s: option requires an argument: %c\n",
64 | argv[0], optopt);
65 | return *optstring == ':' ? ':' : '?';
66 | }
67 | } else {
68 | if (!arg[++optpos]) {
69 | optind++;
70 | optpos = 1;
71 | }
72 | return optopt;
73 | }
74 | }
75 | }
76 |
77 | #endif
78 |
--------------------------------------------------------------------------------
/blowpipe.1:
--------------------------------------------------------------------------------
1 | .TH BLOWPIPE 1
2 | .SH NAME
3 | blowpipe \- create a Blowfish-encrypted, authenticated pipe
4 | .SH SYNOPSIS
5 | .B blowpipe
6 | [\-\fBD\fR|\-\fBE\fR]
7 | [\-\fBc\fR \fIcost\fR]
8 | [\-\fBk\fR \fIfile\fR]
9 | [\-\fBw\fR]
10 | outputfile
12 | .SH DESCRIPTION
13 | .B blowpipe
14 | is a \fItoy\fR that creates a Blowfish-encrypted, authenticated pipe.
15 | Each cryptographic primitive in use is built from the Blowfish cipher (bcrypt KDF, CTR mode Blowfish, CBC-MAC).
16 | .PP
17 | On decryption, the tool \fIonly\fR produces authenticated output.
18 | However, the overall output could still be truncated should something go wrong in the middle of a long stream.
19 | If the stream has been truncated, an error message will be produced and the exit status will reflect the error.
20 | .PP
21 | Since Blowfish is a 64-bit block cipher, it's only safe to encrypt up to a few tens of GBs at a time before birthday attacks become an issue.
22 | However, because of the initialization vector (IV), it's safe to reuse a password or key file to encrypt an arbitrary amount of data across separate runs.
23 | .SH OPTIONS
24 | Either of \fB-D\fR or \fB-E\fR must be specified.
25 | There is no default mode.
26 | .TP
27 | \fB\-D\fR
28 | Decrypt standard input to standard output.
29 | .TP
30 | \fB\-E\fB
31 | Encrypt standard input to standard output.
32 | .TP
33 | \fB\-c\fR \fIcost\fR
34 | In encryption mode (\fB\-E\fR), set the bcrypt cost.
35 | The cost is a power of two, so each increment doubles the total cost.
36 | In decryption mode, set the maximum permitted bcrypt cost.
37 | The default is low in order to prevent a malicious input from choosing a ridiculously large cost.
38 | .TP
39 | \fB\-k\fR \fIfile\fR
40 | Read key material from a file.
41 | By default little key stretching (\fB\-c\fR) is performed on this key.
42 | .TP
43 | \fB\-w\fR
44 | Wait for full chunks and don't flush chunks early.
45 | This increases latency in the pipe but decreases overhead.
46 | This option is meaningless if the input is a physical file.
47 | .SH EXAMPLES
48 | Encrypt/decrypt a file:
49 | .HP 2
50 | .nf
51 | $ blowpipe -E < data.gz > data.gz.enc
52 | $ blowpipe -D < data.gz.enc | gunzip > data.txt
53 | .fi
54 | .PP
55 | Encrypt/decrypt a TCP stream:
56 | .HP 2
57 | .nf
58 | ## receiver
59 | $ nc -lp 2000 | blowpipe -D -k keyfile > data.zip || rm -f data.zip
60 |
61 | ## sender
62 | $ blowpipe -E -k keyfile < data.zip | nc -N hostname 2000
63 | .fi
64 | .SH "SEE ALSO"
65 | .BR blowfish (3),
66 | .BR aepipe (1),
67 | .BR enchive (1)
68 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Blowpipe: authenticated Blowfish-encrypted pipe
2 |
3 | Blowpipe is a *toy* that creates an authenticated,
4 | [Blowfish][bfsh]-encrypted pipe. Each cryptographic primitive it uses is
5 | built entirely with the Blowfish cipher:
6 |
7 | * The key is derived from a passphrase using [bcrypt][bcrypt].
8 | * The stream is encrypted with Blowfish in [CTR mode][ctr].
9 | * The message authentication code ([MAC][mac]) is
10 | [Blowfish-CBC-MAC][cbcmac] with fixed-length messages.
11 |
12 | This tool is strictly stream oriented, processing standard input to
13 | standard output. It could be used to encrypt a file (prompted for a
14 | passphrase):
15 |
16 | $ blowpipe -E < data.gz > data.gz.enc
17 | $ blowpipe -D < data.gz.enc | gunzip > data.txt
18 |
19 | Or to securely transfer a file over a network:
20 |
21 | # receiver
22 | $ nc -lp 2000 | blowpipe -D -k keyfile > data.zip || rm -f data.zip
23 |
24 | # sender
25 | $ blowpipe -E -k keyfile < data.zip | nc -N hostname 2000
26 |
27 | On a modern desktop, Blowpipe has a throughput of about 75 MB/s.
28 |
29 | ![][img]
30 |
31 | On decryption, the tool *only* produces authenticated output. However,
32 | the overall output could still be truncated should something go wrong in
33 | the middle of a long stream. If the stream has been truncated, an error
34 | message will be produced and the exit status will reflect the error.
35 |
36 | Since Blowfish is a 64-bit block cipher, it's only safe to encrypt up to
37 | a few tens of GBs at a time before birthday attacks become an issue.
38 | However, because of the key salt, it's safe to reuse a password or key
39 | file to encrypt an arbitrary amount of data across separate runs. Other
40 | than the block size, Blowfish is still a solid cipher.
41 |
42 | Despite being a toy, Blowpipe is more secure than a few crypto tools
43 | typically packaged by a Linux distribution, such as the ["bcrypt" file
44 | encryption tool][bad] (unauthenticated, [ECB mode][ecb]) and
45 | [aespipe][aespipe] (unauthenticated).
46 |
47 | A proper alternative to Blowpipe is [aepipe][aepipe], which is built
48 | upon a stronger, newer cipher (AES).
49 |
50 | ## Options
51 |
52 | -D decrypt standard input to standard output
53 | -E encrypt standard input to standard output
54 | -c cost (encrypt) set the bcrypt cost
55 | (decrypt) set the maximum permitted bcrypt cost
56 | -k file read key material from given file
57 | -w wait for full chunks and don't flush early
58 |
59 | ## Installation
60 |
61 | Use the PREFIX variable to control the installation path. The default is
62 | to install under `/usr/local`.
63 |
64 | $ make PREFIX=$HOME/.local install
65 |
66 | To build with MinGW-w64, set CC and EXEEXT. On Linux you can even run
67 | the test suite (`make check`) if you have the Wine binfmt configured.
68 |
69 | $ make CC=x86_64-w64-mingw32-gcc EXEEXT=.exe
70 |
71 | To quickly build with MSVC:
72 |
73 | cl.exe -Ox blowpipe.c blowfish.c
74 |
75 | There's also an `amalgamation` target, which will combine all sources
76 | and headers into a single C source file, `blowpipe-cli.c`, ready to be
77 | compiled trivially anywhere by any C99 compiler.
78 |
79 | ## Wire format
80 |
81 | The overall wire format:
82 |
83 | * 16-byte random salt
84 | * 1-byte bcrypt cost: last salt byte + cost, modulo 256
85 | * one or more chunks
86 |
87 | The format is stream-oriented and data is processed in separate chunks
88 | up to 64kB in size, each with its own MAC. A special zero-length chunk
89 | marks the end of the stream, and any data following this chunk is
90 | ignored.
91 |
92 | The chunk format:
93 |
94 | * 8 byte MAC for this chunk (continued from previous chunk)
95 | * 2 byte big endian message length, encrypted and authenticated (`msglen`)
96 | * `msglen - 2` bytes of encrypted data
97 |
98 | The MAC is computed on the ciphertext. The last block in a chunk has its
99 | plaintext zero padded before encryption and authentication, but these
100 | bytes are not actually transmitted. The receiver must also zero-pad and
101 | encrypt the padding before authenticating.
102 |
103 | Since MACs are chained across chunks, it's not possible for an attacker
104 | to reorder individual chunks. The last chunk in the stream will have a
105 | `msglen` of 2, making it an empty chunk. Since this chunk and its
106 | `msglen` are authenticated, an attacker cannot prematurely terminate the
107 | stream without being detected.
108 |
109 | # C99 Public Domain Blowfish Cipher
110 |
111 | This repository includes a standalone Blowfish library (`blowfish.c`,
112 | `blowfish.h`), ready to use in another project. It is a strictly
113 | conforming C99 program with no platform-specific code.
114 |
115 | ~~~c
116 | void blowfish_init(struct blowfish *, const void *, int);
117 | void blowfish_encrypt(struct blowfish *, uint32_t *, uint32_t *);
118 | void blowfish_decrypt(struct blowfish *, uint32_t *, uint32_t *);
119 | void blowfish_bcrypt(void *digest, const void *pwd, int len, const void *salt, int cost);
120 | ~~~
121 |
122 | See `blowfish.h` for complete API documentation.
123 |
124 |
125 | [aepipe]: https://github.com/hashbrowncipher/keypipe
126 | [aespipe]: http://loop-aes.sourceforge.net/aespipe.README
127 | [bad]: http://bcrypt.sourceforge.net/
128 | [bcrypt]: https://en.wikipedia.org/wiki/Bcrypt
129 | [bfsh]: https://www.schneier.com/academic/blowfish/
130 | [cbcmac]: https://en.wikipedia.org/wiki/CBC-MAC
131 | [ctr]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#CTR
132 | [ecb]: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=700758
133 | [img]: https://upload.wikimedia.org/wikipedia/commons/e/ec/Blowpipe2_(PSF).jpg
134 | [mac]: https://en.wikipedia.org/wiki/Message_authentication_code
135 |
--------------------------------------------------------------------------------
/tests/vectors2.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Author : Randy L. Milbert
3 | * E-mail : rmilbert@mit.edu
4 | * Date : 18 Jun 97
5 | * Description: Eric Young's test vectors for Blowfish.
6 | */
7 |
8 | #include
9 |
10 | #define NUM_VARIABLE_KEY_TESTS 34
11 | #define NUM_SET_KEY_TESTS 24
12 |
13 | /* plaintext bytes -- left halves */
14 | uint32_t plaintext_l[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
15 | 0x00000000l, 0xFFFFFFFFl, 0x10000000l, 0x11111111l, 0x11111111l,
16 | 0x01234567l, 0x00000000l, 0x01234567l, 0x01A1D6D0l, 0x5CD54CA8l,
17 | 0x0248D438l, 0x51454B58l, 0x42FD4430l, 0x059B5E08l, 0x0756D8E0l,
18 | 0x762514B8l, 0x3BDD1190l, 0x26955F68l, 0x164D5E40l, 0x6B056E18l,
19 | 0x004BD6EFl, 0x480D3900l, 0x437540C8l, 0x072D43A0l, 0x02FE5577l,
20 | 0x1D9D5C50l, 0x30553228l, 0x01234567l, 0x01234567l, 0x01234567l,
21 | 0xFFFFFFFFl, 0x00000000l, 0x00000000l, 0xFFFFFFFFl, 0xFEDCBA98l,
22 | 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
23 | 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
24 | 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
25 | 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
26 | 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l
27 | };
28 |
29 | /* plaintext bytes -- right halves */
30 | uint32_t plaintext_r[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
31 | 0x00000000l, 0xFFFFFFFFl, 0x00000001l, 0x11111111l, 0x11111111l,
32 | 0x89ABCDEFl, 0x00000000l, 0x89ABCDEFl, 0x39776742l, 0x3DEF57DAl,
33 | 0x06F67172l, 0x2DDF440Al, 0x59577FA2l, 0x51CF143Al, 0x774761D2l,
34 | 0x29BF486Al, 0x49372802l, 0x35AF609Al, 0x4F275232l, 0x759F5CCAl,
35 | 0x09176062l, 0x6EE762F2l, 0x698F3CFAl, 0x77075292l, 0x8117F12Al,
36 | 0x18F728C2l, 0x6D6F295Al, 0x89ABCDEFl, 0x89ABCDEFl, 0x89ABCDEFl,
37 | 0xFFFFFFFFl, 0x00000000l, 0x00000000l, 0xFFFFFFFFl, 0x76543210l,
38 | 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
39 | 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
40 | 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
41 | 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
42 | 0x76543210l, 0x76543210l, 0x76543210l
43 | };
44 |
45 | /* key bytes for variable key tests */
46 | uint8_t variable_key[NUM_VARIABLE_KEY_TESTS][8] = {
47 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
48 | { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
49 | { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
50 | { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 },
51 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
52 | { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 },
53 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
54 | { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 },
55 | { 0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57 },
56 | { 0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E },
57 | { 0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86 },
58 | { 0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E },
59 | { 0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6 },
60 | { 0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE },
61 | { 0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6 },
62 | { 0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE },
63 | { 0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16 },
64 | { 0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F },
65 | { 0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46 },
66 | { 0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E },
67 | { 0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76 },
68 | { 0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07 },
69 | { 0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F },
70 | { 0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7 },
71 | { 0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF },
72 | { 0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6 },
73 | { 0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF },
74 | { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
75 | { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
76 | { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
77 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
78 | { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
79 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
80 | { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }
81 | };
82 |
83 | /* key bytes for set key tests */
84 | uint8_t set_key[24] = {
85 | 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87,
86 | 0x78, 0x69, 0x5A, 0x4B, 0x3C, 0x2D, 0x1E, 0x0F,
87 | 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
88 | };
89 |
90 | /* ciphertext bytes -- left halves */
91 | uint32_t ciphertext_l[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
92 | 0x4EF99745l, 0x51866FD5l, 0x7D856F9Al, 0x2466DD87l, 0x61F9C380l,
93 | 0x7D0CC630l, 0x4EF99745l, 0x0ACEAB0Fl, 0x59C68245l, 0xB1B8CC0Bl,
94 | 0x1730E577l, 0xA25E7856l, 0x353882B1l, 0x48F4D088l, 0x432193B7l,
95 | 0x13F04154l, 0x2EEDDA93l, 0xD887E039l, 0x5F99D04Fl, 0x4A057A3Bl,
96 | 0x452031C1l, 0x7555AE39l, 0x53C55F9Cl, 0x7A8E7BFAl, 0xCF9C5D7Al,
97 | 0xD1ABB290l, 0x55CB3774l, 0xFA34EC48l, 0xA7907951l, 0xC39E072Dl,
98 | 0x014933E0l, 0xF21E9A77l, 0x24594688l, 0x6B5C5A9Cl, 0xF9AD597Cl,
99 | 0xE91D21C1l, 0xE9C2B70Al, 0xBE1E6394l, 0xB39E4448l, 0x9457AA83l,
100 | 0x8BB77032l, 0xE87A244El, 0x15750E7Al, 0x122BA70Bl, 0x3A833C9Al,
101 | 0x9409DA87l, 0x884F8062l, 0x1F85031Cl, 0x79D9373Al, 0x93142887l,
102 | 0x03429E83l, 0xA4299E27l, 0xAFD5AED1l, 0x10851C0El, 0xE6F51ED7l,
103 | 0x64A6E14Al, 0x80C7D7D4l, 0x05044B62l
104 | };
105 |
106 | /* ciphertext bytes -- right halves */
107 | uint32_t ciphertext_r[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
108 | 0x6198DD78l, 0xB85ECB8Al, 0x613063F2l, 0x8B963C9Dl, 0x2281B096l,
109 | 0xAFDA1EC7l, 0x6198DD78l, 0xC6A0A28Dl, 0xEB05282Bl, 0x250F09A0l,
110 | 0x8BEA1DA4l, 0xCF2651EBl, 0x09CE8F1Al, 0x4C379918l, 0x8951FC98l,
111 | 0xD69D1AE5l, 0xFFD39C79l, 0x3C2DA6E3l, 0x5B163969l, 0x24D3977Bl,
112 | 0xE4FADA8El, 0xF59B87BDl, 0xB49FC019l, 0x937E89A3l, 0x4986ADB5l,
113 | 0x658BC778l, 0xD13EF201l, 0x47B268B2l, 0x08EA3CAEl, 0x9FAC631Dl,
114 | 0xCDAFF6E4l, 0xB71C49BCl, 0x5754369Al, 0x5D9E0A5Al, 0x49DB005El,
115 | 0xD961A6D6l, 0x1BC65CF3l, 0x08640F05l, 0x1BDB1E6El, 0xB1928C0Dl,
116 | 0xF960629Dl, 0x2CC85E82l, 0x4F4EC577l, 0x3AB64AE0l, 0xFFC537F6l,
117 | 0xA90F6BF2l, 0x5060B8B4l, 0x19E11968l, 0x714CA34Fl, 0xEE3BE15Cl,
118 | 0x8CE2D14Bl, 0x469FF67Bl, 0xC1BC96A8l, 0x3858DA9Fl, 0x9B9DB21Fl,
119 | 0xFD36B46Fl, 0x5A5479ADl, 0xFA52D080l
120 | };
121 |
--------------------------------------------------------------------------------
/w32-compat/unistd.h:
--------------------------------------------------------------------------------
1 | /* POSIX compatability layer for Blowpipe
2 | *
3 | * Only the tiny, tiny subset of POSIX needed by Blowpipe is implemented.
4 | */
5 | #if !defined(W32_COMPAT_H) && defined(_WIN32)
6 | #define W32_COMPAT_H
7 |
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | #include
15 |
16 | #ifdef _MSC_VER
17 | # pragma comment(lib, "advapi32.lib")
18 | #endif
19 |
20 | typedef SSIZE_T ssize_t;
21 |
22 | #define ECHO 1
23 | #define TCSANOW 0
24 | struct termios {
25 | int c_lflag;
26 | };
27 |
28 | #define O_RDONLY 1
29 | #define O_RDWR 2
30 |
31 | #define STDIN_FILENO 0
32 | #define STDOUT_FILENO 1
33 | #define URANDOM_FILENO 3
34 | #define TTY_FILENO 4
35 | #define FILE_FILENO 5
36 |
37 | static HANDLE compat_file;
38 | static HANDLE compat_conin;
39 | static HANDLE compat_conout;
40 | static HCRYPTPROV compat_crypt;
41 |
42 | static int
43 | open(const char *path, int flags)
44 | {
45 | if (strcmp(path, "/dev/urandom") == 0) {
46 | DWORD type = PROV_RSA_FULL;
47 | DWORD flags = CRYPT_VERIFYCONTEXT | CRYPT_SILENT;
48 | if (!CryptAcquireContext(&compat_crypt, 0, 0, type, flags)) {
49 | errno = EACCES;
50 | return -1;
51 | }
52 | return URANDOM_FILENO;
53 | } else if (strcmp(path, "/dev/tty") == 0) {
54 | DWORD access = GENERIC_READ | GENERIC_WRITE;
55 | DWORD disp = OPEN_EXISTING;
56 | DWORD flags = FILE_ATTRIBUTE_NORMAL;
57 | compat_conin = CreateFile("CONIN$", access, 0, 0, disp, flags, 0);
58 | if (compat_conin == INVALID_HANDLE_VALUE) {
59 | errno = ENOENT;
60 | return -1;
61 | }
62 | compat_conout = CreateFile("CONOUT$", access, 0, 0, disp, flags, 0);
63 | if (compat_conout == INVALID_HANDLE_VALUE) {
64 | CloseHandle(compat_conin);
65 | errno = ENOENT;
66 | return -1;
67 | }
68 | return TTY_FILENO;
69 | } else {
70 | assert(flags == O_RDONLY);
71 | DWORD access = GENERIC_READ;
72 | DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
73 | DWORD disp = OPEN_EXISTING;
74 | DWORD flags = FILE_ATTRIBUTE_NORMAL;
75 | compat_file = CreateFile(path, access, share, 0, disp, flags, 0);
76 | return compat_file == INVALID_HANDLE_VALUE ? -1 : FILE_FILENO;
77 | }
78 | }
79 |
80 | static int
81 | close(int fd)
82 | {
83 | switch (fd) {
84 | case URANDOM_FILENO:
85 | CryptReleaseContext(compat_crypt, 0);
86 | return 0;
87 | case TTY_FILENO:
88 | CloseHandle(compat_conin);
89 | CloseHandle(compat_conout);
90 | return 0;
91 | case FILE_FILENO:
92 | CloseHandle(compat_file);
93 | return 0;
94 | default:
95 | abort();
96 | }
97 | }
98 |
99 | static ssize_t
100 | read(int fd, void *buf, size_t len)
101 | {
102 | switch (fd) {
103 | case STDIN_FILENO: {
104 | static HANDLE in = INVALID_HANDLE_VALUE;
105 | static BOOL isconsole;
106 | if (in == INVALID_HANDLE_VALUE) {
107 | in = GetStdHandle(STD_INPUT_HANDLE);
108 | DWORD mode;
109 | isconsole = GetConsoleMode(in, &mode);
110 | }
111 | if (len > 0x77e8 && isconsole) {
112 | /* Undocumented behavior: Console reads are limited to 30696
113 | * bytes. Larger reads trigger ERROR_NOT_ENOUGH_MEMORY.
114 | * Y U do dis, Microsoft?
115 | */
116 | len = 0x77e8;
117 | }
118 | DWORD actual;
119 | BOOL r = ReadFile(in, buf, len, &actual, 0);
120 | if (!r) {
121 | DWORD error = GetLastError();
122 | if (error == ERROR_BROKEN_PIPE)
123 | return 0; // actually an EOF
124 | errno = EIO;
125 | return -1;
126 | }
127 | return actual;
128 | }
129 | case URANDOM_FILENO: {
130 | if (!CryptGenRandom(compat_crypt, len, buf)) {
131 | errno = EIO;
132 | return -1;
133 | }
134 | return len;
135 | }
136 | case TTY_FILENO: {
137 | DWORD actual;
138 | BOOL r = ReadConsole(compat_conin, buf, len, &actual, 0);
139 | if (!r) {
140 | errno = EIO;
141 | return -1;
142 | }
143 | return actual;
144 | }
145 | case FILE_FILENO: {
146 | DWORD actual;
147 | BOOL r = ReadFile(compat_file, buf, len, &actual, 0);
148 | if (!r) {
149 | errno = EIO;
150 | return -1;
151 | }
152 | return actual;
153 | }
154 | default:
155 | abort();
156 | }
157 | }
158 |
159 | static ssize_t
160 | write(int fd, const void *buf, size_t len)
161 | {
162 | switch (fd) {
163 | case STDOUT_FILENO: {
164 | HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
165 | DWORD actual;
166 | BOOL r = WriteFile(out, buf, len, &actual, 0);
167 | if (!r) {
168 | errno = EIO;
169 | return -1;
170 | }
171 | return actual;
172 | }
173 | case TTY_FILENO: {
174 | DWORD actual;
175 | BOOL r = WriteConsole(compat_conout, buf, len, &actual, 0);
176 | if (!r) {
177 | errno = EIO;
178 | return -1;
179 | }
180 | return actual;
181 | }
182 | default:
183 | abort();
184 | }
185 | }
186 |
187 |
188 | static int
189 | tcgetattr(int fd, struct termios *s)
190 | {
191 | assert(fd == TTY_FILENO);
192 | s->c_lflag = 1;
193 | return 0;
194 | }
195 |
196 | static int
197 | tcsetattr(int fd, int actions, struct termios *s)
198 | {
199 | assert(fd == TTY_FILENO);
200 | assert(actions == TCSANOW);
201 | DWORD access = GENERIC_READ | GENERIC_WRITE;
202 | DWORD disp = OPEN_EXISTING;
203 | DWORD flags = FILE_ATTRIBUTE_NORMAL;
204 | HANDLE console = CreateFile("CONIN$", access, 0, 0, disp, flags, 0);
205 | if (console != INVALID_HANDLE_VALUE) {
206 | DWORD orig;
207 | if (GetConsoleMode(console, &orig)) {
208 | if (s->c_lflag)
209 | SetConsoleMode(console, orig | ENABLE_ECHO_INPUT);
210 | else
211 | SetConsoleMode(console, orig & ~ENABLE_ECHO_INPUT);
212 | }
213 | }
214 | return 0;
215 | }
216 |
217 | #endif
218 |
--------------------------------------------------------------------------------
/tests/tests.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include "vectors2.h"
6 | #include "../blowfish.h"
7 |
8 | #define NUM_BCRYPT_VECTORS (sizeof(bcrypt_vectors)/sizeof(*bcrypt_vectors)/2)
9 | static const char bcrypt_vectors[][64] = {
10 | "twist", "$2a$04$mlr.PoDP3w4SzMh8A/td4O2LE5lJcM2/JSPEwYH0wXmT/Ai.Ip3GG",
11 | "sector", "$2a$04$4u14y0XgEKWfE2yS.VF6JuZhzI2JAcIsSM0ukKuSeFZDPH2Qfp2Ku",
12 | "cue", "$2a$05$B8Ld4qjLv/sMgtkdO6o8iO7Sm61jDwADJ9Y8fTXZyQqoG1qftwF82",
13 | "fading", "$2a$05$5d6JDfbFA3AZSBJpZmCcVuJQjo7QSgzePx3nS3PqTTsG.aqoDUkPW",
14 | "wedge", "$2a$06$LtzByA9pukm5I9XbW.6t6eZy3Qkg1pB17IuzfTfSCsky2X5eoFMua",
15 | "owns", "$2a$06$8wgs0GP1l0uT294gIj5STeFhpFAXsdChz8bdypccbpAQbevxWne9K",
16 | "cause", "$2a$07$IEtr9PejuMM8wSZpvQ4WL.rEdfz.2Y0R6ELEi2RaVKWXM5HbhUujq",
17 | "visual", "$2a$07$xXB.ianZO3Fzo4oG28uh1e.MoLZVezlYTOcm7kOKiIfp4L0MQFEBO",
18 | "thumbs", "$2a$08$vWI4Y9ApphxiDVlB.9wRYeoOlqEQa2q3wuQ8gQbhWFm5THOVYUc4K",
19 | "stare", "$2a$08$OEB986UEwkJLIw7SdoXKju/lgW7j/bVjsVaaaVEo461KdyUMxT/AO",
20 | "corpse", "$2a$09$MEKwC6Me6/hwiRMLoICLLOkIYIm8InZA39VlMRC0o9yHR1dW7P5q.",
21 | "taste", "$2a$09$f0rzicUe7fceAk7BcbEgZegPcX5fyAV0YmSJsGT0cgAoTJaJL9LBa",
22 | "loves", "$2a$10$WRpreT7IpmlwVcAUJWHPhea6nb2cEKMdxDdDbuNNJORVLuRNA4ofi",
23 | "slices", "$2a$10$JL73nWnZVBXZsV9/DZDQruS27YKRk.NnlqM0WJ8evHOcNfRZR67i6",
24 | "roots", "$2a$11$XRKrSKrCuUKnb5sllKpJ9.VscoH9O7ppsPeABflG20iPRGoKozv7a",
25 | "own", "$2a$11$P.Sp8zWnDMjRkSH5uc5./eVg4aZyTPajJGdGg.rs3/UX.1/dfFwsm"
26 | };
27 |
28 | static const char base64[] = {
29 | '.', '/', 'A', 'B', 'C', 'D', 'E', 'F',
30 | 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
31 | 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
32 | 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
33 | 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
34 | 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
35 | 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
36 | '2', '3', '4', '5', '6', '7', '8', '9',
37 | };
38 |
39 | static const unsigned char index64[] = {
40 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 | 0xff, 0xff, 0xff, 0xff, 0xff, 0x40, 0xff, 0xff,
45 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01,
46 | 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
47 | 0x3e, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
48 | 0xff, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
49 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
50 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
51 | 0x19, 0x1a, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff,
52 | 0xff, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22,
53 | 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a,
54 | 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32,
55 | 0x33, 0x34, 0x35, 0xff, 0xff, 0xff, 0xff, 0xff,
56 | };
57 |
58 | static void
59 | b64encode(char *dst, const void *src, size_t len)
60 | {
61 | int ocount = 0;
62 | unsigned overflow = 0;
63 | for (size_t i = 0; i < len; i++) {
64 | unsigned c = ((unsigned char *)src)[i];
65 | overflow = c | (overflow << 8);
66 | ocount += 8;
67 | while (ocount >= 6) {
68 | *dst++ = base64[(overflow >> (ocount - 6)) & 0x3f];
69 | ocount -= 6;
70 | }
71 | }
72 | if (ocount > 0)
73 | *dst++ = base64[(overflow << (6 - ocount)) & 0x3f];
74 | *dst = 0;
75 | }
76 |
77 | static void
78 | b64decode(void *dst, const char *src)
79 | {
80 | int c;
81 | int ocount = 0;
82 | unsigned overflow = 0;
83 | unsigned char *p = dst;
84 | while ((c = *src++)) {
85 | c = index64[c];
86 | overflow = (overflow << 6) | c;
87 | ocount += 6;
88 | while (ocount >= 8) {
89 | *p++ = (overflow >> (ocount - 8)) & 0xff;
90 | ocount -= 8;
91 | }
92 | }
93 | }
94 |
95 | struct bcrypt {
96 | uint8_t salt[BLOWFISH_SALT_LENGTH];
97 | uint8_t digest[BLOWFISH_DIGEST_LENGTH];
98 | int cost;
99 | };
100 |
101 | static void
102 | bcrypt_decode(struct bcrypt *bc, const char *str)
103 | {
104 | char salt[32];
105 | memcpy(salt, str + 7, 31);
106 | salt[31] = 0;
107 | b64decode(bc->salt, salt);
108 | b64decode(bc->digest, str + 28);
109 | bc->cost = (str[4] - '0') * 10 + str[5] - '0';
110 | }
111 |
112 | static void
113 | bcrypt_encode(struct bcrypt *bc, char *str)
114 | {
115 | str[0] = '$';
116 | str[1] = '2';
117 | str[2] = 'a';
118 | str[3] = '$';
119 | str[4] = '0' + (bc->cost / 10);
120 | str[5] = '0' + (bc->cost % 10);
121 | str[6] = '$';
122 | b64encode(str + 7, bc->salt, BLOWFISH_SALT_LENGTH);
123 | b64encode(str + 29, bc->digest, BLOWFISH_DIGEST_LENGTH - 1);
124 | }
125 |
126 | static int
127 | verify(uint32_t el, uint32_t er, uint32_t al, uint32_t ar)
128 | {
129 | if (er != ar || el != al) {
130 | printf("expect: %08lx%08lx\n", (unsigned long)el, (unsigned long)er);
131 | printf("actual: %08lx%08lx\n", (unsigned long)al, (unsigned long)ar);
132 | return 1;
133 | }
134 | return 0;
135 | }
136 |
137 | int
138 | main(void)
139 | {
140 | int failures = 0;
141 |
142 | /* bcrypt tests */
143 | for (size_t i = 0; i < NUM_BCRYPT_VECTORS; i++) {
144 | struct bcrypt bc[1];
145 | const char *pwd = bcrypt_vectors[i * 2 + 0];
146 | int pwdlen = strlen(pwd) + 1;
147 | const char *expect = bcrypt_vectors[i * 2 + 1];
148 |
149 | bcrypt_decode(bc, expect);
150 | memset(bc->digest, 0x5a, sizeof(bc->digest));
151 | blowfish_bcrypt(bc->digest, pwd, pwdlen, bc->salt, bc->cost);
152 |
153 | char actual[64];
154 | bcrypt_encode(bc, actual);
155 | if (strcmp(expect, actual) != 0) {
156 | printf("expect: %s\n", expect);
157 | printf("actual: %s\n", actual);
158 | failures++;
159 | }
160 | }
161 |
162 | /* variable key tests */
163 | for (int i = 0; i < NUM_VARIABLE_KEY_TESTS; i++) {
164 | struct blowfish ctx[1];
165 | blowfish_init(ctx, variable_key[i], 8);
166 |
167 | uint32_t xl = plaintext_l[i];
168 | uint32_t xr = plaintext_r[i];
169 | blowfish_encrypt(ctx, &xl, &xr);
170 | failures += verify(ciphertext_l[i], ciphertext_r[i], xl, xr);
171 |
172 | blowfish_decrypt(ctx, &xl, &xr);
173 | failures += verify(plaintext_l[i], plaintext_r[i], xl, xr);
174 | }
175 |
176 | /* set key tests */
177 | for (int z = 1; z <= NUM_SET_KEY_TESTS; z++) {
178 | struct blowfish ctx[1];
179 | blowfish_init(ctx, set_key, z);
180 |
181 | int i = NUM_VARIABLE_KEY_TESTS + (int)z - 1;
182 | uint32_t xl = plaintext_l[i];
183 | uint32_t xr = plaintext_r[i];
184 | blowfish_encrypt(ctx, &xl, &xr);
185 | failures += verify(ciphertext_l[i], ciphertext_r[i], xl, xr);
186 |
187 | blowfish_decrypt(ctx, &xl, &xr);
188 | failures += verify(plaintext_l[i], plaintext_r[i], xl, xr);
189 | }
190 |
191 | printf("%d failures\n", failures);
192 | return failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
193 | }
194 |
--------------------------------------------------------------------------------
/blowpipe.c:
--------------------------------------------------------------------------------
1 | /* Blowpipe --- authenticated Blowfish-encrypted pipe */
2 | #define _POSIX_C_SOURCE 200112L
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | #ifdef _WIN32
9 | #include "w32-compat/unistd.h"
10 | #include "w32-compat/getopt.h"
11 | #else
12 | #include
13 | #include
14 | #include
15 | #include
16 | #endif
17 |
18 | #include "blowfish.h"
19 |
20 | #define SALT_LENGTH 16
21 | #define CHUNK_SIZE (1UL << 16)
22 | #define CHUNK_SIZE_SIZE 2
23 | #define PASSPHRASE_COST 15
24 | #define KEYFILE_COST 0
25 | #define MAXIMUM_INPUT_COST 16
26 |
27 | #define DIE(...) \
28 | do { \
29 | fprintf(stderr, "blowcrypt: fatal: " __VA_ARGS__); \
30 | fputc('\n', stderr); \
31 | exit(EXIT_FAILURE); \
32 | } while (0)
33 |
34 | #define DIE_ERRNO(s) \
35 | do { \
36 | fprintf(stderr, "blowcrypt: fatal: %s: %s\n", s, strerror(errno)); \
37 | exit(EXIT_FAILURE); \
38 | } while (0)
39 |
40 | static ssize_t
41 | full_read(int fd, void *buf, size_t len)
42 | {
43 | size_t z = 0;
44 | while (z < len) {
45 | ssize_t r = read(fd, (char *)buf + z, len - z);
46 | if (r == -1)
47 | return -1;
48 | if (r == 0)
49 | break;
50 | z += r;
51 | }
52 | return z;
53 | }
54 |
55 | static void
56 | generate_salt(void *salt)
57 | {
58 | int fd = open("/dev/urandom", O_RDONLY);
59 | if (fd == -1)
60 | DIE_ERRNO("/dev/urandom");
61 | ssize_t z = full_read(fd, salt, SALT_LENGTH);
62 | if (z == -1)
63 | DIE_ERRNO("/dev/urandom");
64 | if (z != SALT_LENGTH)
65 | DIE("/dev/urandom salt generation failed");
66 | close(fd);
67 | }
68 |
69 | static void
70 | read_salt(int fd, void *salt)
71 | {
72 | ssize_t z = full_read(fd, salt, SALT_LENGTH + 1);
73 | if (z == -1)
74 | DIE_ERRNO("reading ciphertext");
75 | if (z < SALT_LENGTH)
76 | DIE("premature end of ciphertext");
77 | }
78 |
79 | static void
80 | passphrase_prompt(const char *prompt, char *buf)
81 | {
82 | int tty = open("/dev/tty", O_RDWR);
83 | if (tty == -1)
84 | DIE_ERRNO("/dev/tty");
85 |
86 | if (write(tty, prompt, strlen(prompt)) != (ssize_t)strlen(prompt))
87 | DIE_ERRNO("/dev/tty");
88 |
89 | struct termios old, new;
90 | if (tcgetattr(tty, &old) == -1)
91 | DIE_ERRNO("tcgetattr()");
92 | new = old;
93 | new.c_lflag &= ~ECHO;
94 | if (tcsetattr(tty, TCSANOW, &new) == -1)
95 | DIE_ERRNO("tcsetattr()");
96 |
97 | ssize_t z = read(tty, buf, BLOWFISH_MAX_KEY_LENGTH + 1);
98 | (void)tcsetattr(tty, TCSANOW, &old); // don't care if this fails
99 | (void)write(tty, "\n", 1); // don't care if this fails
100 |
101 | if (z == -1)
102 | DIE_ERRNO("/dev/tty");
103 | else if (z > BLOWFISH_MAX_KEY_LENGTH && (buf[z] != '\n' && buf[z] != '\r'))
104 | DIE("passphrase too long (maximum 56 bytes)");
105 | else if (z == 0) {
106 | buf[z] = 0;
107 | } else {
108 | buf[z] = 0;
109 | char *end;
110 | if ((end = strchr(buf, '\r')))
111 | *end = 0;
112 | else if ((end = strchr(buf, '\n')))
113 | *end = 0;
114 | }
115 | }
116 |
117 | static void
118 | passphrase_kdf(void *key, const void *salt, int cost, int verify)
119 | {
120 | char buf[2][BLOWFISH_MAX_KEY_LENGTH + 1];
121 | passphrase_prompt("Passphrase: ", buf[0]);
122 | if (verify) {
123 | passphrase_prompt("Passphrase (repeat): ", buf[1]);
124 | if (strcmp(buf[0], buf[1]) != 0)
125 | DIE("passphrases do not match");
126 | }
127 | blowfish_bcrypt(key, buf, strlen(buf[0]) + 1, salt, cost);
128 | }
129 |
130 | static void
131 | key_read(int fd, void *key, const void *salt, int cost)
132 | {
133 | /* Read one over the maximum key length. The extra character will
134 | * detect an overly-long key.
135 | */
136 | char buf[BLOWFISH_MAX_KEY_LENGTH + 1];
137 | ssize_t z = full_read(fd, buf, BLOWFISH_MAX_KEY_LENGTH + 1);
138 | if (z == -1)
139 | DIE_ERRNO("reading key");
140 | if (z > BLOWFISH_MAX_KEY_LENGTH)
141 | DIE("key is too long (maximum 72 bytes)");
142 | blowfish_bcrypt(key, buf, z, salt, cost);
143 | }
144 |
145 | static void
146 | encode_u32be(void *buf, uint32_t c)
147 | {
148 | uint8_t *p = buf;
149 | p[0] = (uint8_t)(c >> 24);
150 | p[1] = (uint8_t)(c >> 16);
151 | p[2] = (uint8_t)(c >> 8);
152 | p[3] = (uint8_t)(c >> 0);
153 | }
154 |
155 | static uint32_t
156 | decode_u32be(const void *buf)
157 | {
158 | const uint8_t *p = buf;
159 | return ((uint32_t)p[0] << 24) |
160 | ((uint32_t)p[1] << 16) |
161 | ((uint32_t)p[2] << 8) |
162 | ((uint32_t)p[3] << 0);
163 | }
164 |
165 | #define FLAG_WAIT (1u << 0)
166 |
167 | static void
168 | bp_encrypt(struct blowfish *crypt, struct blowfish *mac, unsigned flags)
169 | {
170 | int eof = 0;
171 | uint64_t ctr = 0;
172 | uint32_t macl = 0;
173 | uint32_t macr = 0;
174 | static uint8_t chunk[CHUNK_SIZE];
175 |
176 | for (;;) {
177 | ssize_t z;
178 | int headerlen = BLOWFISH_BLOCK_LENGTH + CHUNK_SIZE_SIZE;
179 | if (flags & FLAG_WAIT) {
180 | /* Read as much input as possible, but don't ever read()
181 | * again after getting 0 bytes on a read().
182 | */
183 | size_t avail = CHUNK_SIZE - headerlen;
184 | z = 0;
185 | while (!eof && avail) {
186 | ssize_t r = read(STDIN_FILENO, chunk + headerlen + z, avail);
187 | if (r == -1)
188 | break;
189 | if (r == 0)
190 | eof = 1;
191 | z += r;
192 | avail -= r;
193 | }
194 | } else {
195 | /* Read just the available data */
196 | size_t avail = CHUNK_SIZE - headerlen;
197 | z = read(STDIN_FILENO, chunk + headerlen, avail);
198 | }
199 | if (z == -1)
200 | DIE_ERRNO("reading plaintext");
201 |
202 | /* Zero-pad last block */
203 | size_t blocklen = BLOWFISH_BLOCK_LENGTH;
204 | size_t msglen = z + CHUNK_SIZE_SIZE;
205 | size_t nblocks = (msglen + blocklen - 1) / blocklen;
206 | size_t padding = nblocks * blocklen - msglen;
207 | memset(chunk + BLOWFISH_BLOCK_LENGTH + msglen, 0, padding);
208 |
209 | /* Write chunk length to chunk header */
210 | for (int i = 0; i < CHUNK_SIZE_SIZE; i++) {
211 | int shift = (CHUNK_SIZE_SIZE - 1 - i) * 8;
212 | chunk[BLOWFISH_BLOCK_LENGTH + i] = msglen >> shift;
213 | }
214 |
215 | /* Encrypt the buffer */
216 | for (size_t i = 0; i < nblocks; i++) {
217 | /* Compute CTR mode pad */
218 | uint32_t xl = ctr >> 32;
219 | uint32_t xr = ctr++;
220 | blowfish_encrypt(crypt, &xl, &xr);
221 |
222 | /* XOR plaintext with the pad */
223 | size_t off = (i + 1) * BLOWFISH_BLOCK_LENGTH;
224 | xl ^= decode_u32be(chunk + off + 0);
225 | xr ^= decode_u32be(chunk + off + 4);
226 |
227 | /* Compute the MAC */
228 | macl ^= xl;
229 | macr ^= xr;
230 | blowfish_encrypt(mac, &macl, ¯);
231 |
232 | /* Put ciphertext back into the buffer */
233 | encode_u32be(chunk + off + 0, xl);
234 | encode_u32be(chunk + off + 4, xr);
235 | }
236 | encode_u32be(chunk + 0, macl);
237 | encode_u32be(chunk + 4, macr);
238 |
239 | /* Write encrypted chunk */
240 | ssize_t len = msglen + BLOWFISH_BLOCK_LENGTH;
241 | ssize_t w = write(STDOUT_FILENO, chunk, len);
242 | if (w == -1)
243 | DIE_ERRNO("writing ciphertext");
244 | if (w != len)
245 | DIE("failed to write ciphertext");
246 |
247 | if (z == 0)
248 | break; // EOF
249 | }
250 |
251 | }
252 |
253 | static void
254 | bp_decrypt(struct blowfish *crypt, struct blowfish *mac)
255 | {
256 | size_t len = 0;
257 | uint64_t ctr = 0;
258 | uint32_t macl = 0;
259 | uint32_t macr = 0;
260 | static uint8_t chunk[CHUNK_SIZE];
261 |
262 | for (;;) {
263 | /* Read in at least the header */
264 | size_t headerlen = BLOWFISH_BLOCK_LENGTH + CHUNK_SIZE_SIZE;
265 | while (len < headerlen) {
266 | ssize_t z = read(STDIN_FILENO, chunk + len, CHUNK_SIZE - len);
267 | if (z == -1)
268 | DIE_ERRNO("reading ciphertext");
269 | if (z == 0)
270 | DIE("premature end of ciphertext");
271 | len += z;
272 | }
273 |
274 | /* Compute the first 8 bytes of the pad */
275 | uint8_t pad[BLOWFISH_BLOCK_LENGTH];
276 | uint32_t padl = ctr >> 32;
277 | uint32_t padr = ctr; // don't increment counter yet
278 | blowfish_encrypt(crypt, &padl, &padr);
279 | encode_u32be(pad + 0, padl);
280 | encode_u32be(pad + 4, padr);
281 |
282 | /* Decrypt the chunk length */
283 | size_t msglen = 0;
284 | for (int i = 0; i < CHUNK_SIZE_SIZE; i++) {
285 | msglen <<= 8;
286 | msglen |= chunk[BLOWFISH_BLOCK_LENGTH + i] ^ pad[i];
287 | }
288 | size_t msglen_min = CHUNK_SIZE_SIZE;
289 | size_t msglen_max = CHUNK_SIZE - BLOWFISH_BLOCK_LENGTH;
290 | if (msglen < msglen_min || msglen > msglen_max)
291 | DIE("ciphertext is damaged");
292 |
293 | /* Read remainder of chunk */
294 | size_t blocklen = BLOWFISH_BLOCK_LENGTH;
295 | size_t nblocks = (msglen + blocklen - 1) / blocklen;
296 | ssize_t remainder = msglen - len + blocklen;
297 | if (remainder > 0) {
298 | ssize_t z = full_read(STDIN_FILENO, chunk + len, remainder);
299 | if (z == -1)
300 | DIE_ERRNO("reading ciphertext");
301 | if (z != remainder)
302 | DIE("premature end of ciphertext");
303 | len += z;
304 | }
305 |
306 | /* Decrypt the buffer */
307 | for (size_t i = 0; i < nblocks; i++) {
308 | uint32_t pl = ctr >> 32;
309 | uint32_t pr = ctr++;
310 | blowfish_encrypt(crypt, &pl, &pr);
311 |
312 | /* Extract ciphertext */
313 | uint32_t cl, cr;
314 | size_t off = (i + 1) * BLOWFISH_BLOCK_LENGTH;
315 | if ((i + 1) * BLOWFISH_BLOCK_LENGTH > msglen) {
316 | /* Handle the final partial block in a temporary buffer
317 | * in order to pad it. The chunk buffer might have bytes
318 | * belonging to the next chunk immediately after this
319 | * partial block, so that space isn't available.
320 | */
321 | uint8_t tmp[BLOWFISH_BLOCK_LENGTH];
322 | int padding = (i + 1) * BLOWFISH_BLOCK_LENGTH - msglen;
323 | encode_u32be(tmp + 0, pl);
324 | encode_u32be(tmp + 4, pr);
325 | memcpy(tmp, chunk + off, 8 - padding);
326 | cl = decode_u32be(tmp + 0);
327 | cr = decode_u32be(tmp + 4);
328 | encode_u32be(tmp + 0, pl ^ cl);
329 | encode_u32be(tmp + 4, pr ^ cr);
330 | memcpy(chunk + off, tmp, 8 - padding);
331 | } else {
332 | cl = decode_u32be(chunk + off + 0);
333 | cr = decode_u32be(chunk + off + 4);
334 | encode_u32be(chunk + off + 0, pl ^ cl);
335 | encode_u32be(chunk + off + 4, pr ^ cr);
336 | }
337 |
338 | /* Compute MAC */
339 | macl ^= cl;
340 | macr ^= cr;
341 | blowfish_encrypt(mac, &macl, ¯);
342 | }
343 |
344 | /* Check the MAC */
345 | uint32_t cl = decode_u32be(chunk + 0);
346 | uint32_t cr = decode_u32be(chunk + 4);
347 | if (macl != cl || macr != cr)
348 | DIE("ciphertext is damaged");
349 |
350 | /* Quit after the empty chunk has been authenticated */
351 | if (msglen == CHUNK_SIZE_SIZE)
352 | break;
353 |
354 | /* Write out decrypted ciphertext */
355 | size_t outlen = msglen - CHUNK_SIZE_SIZE;
356 | ssize_t w = write(STDOUT_FILENO, chunk + headerlen, outlen);
357 | if (w == -1)
358 | DIE_ERRNO("writing plaintext");
359 | if ((size_t)w != outlen)
360 | DIE("failed to write plaintext");
361 |
362 | /* Move unprocessed bytes to beginning */
363 | size_t discard = msglen + BLOWFISH_BLOCK_LENGTH;
364 | memmove(chunk, chunk + discard, len - discard);
365 | len -= discard;
366 | }
367 | }
368 |
369 | static void
370 | usage(FILE *o)
371 | {
372 | fprintf(o, "usage: blowpipe [-D|-E] [-c cost] [-k file] [-w]\n");
373 | }
374 |
375 | int
376 | main(int argc, char **argv)
377 | {
378 | /* Options */
379 | const char *keyfile = 0;
380 | int cost = -1;
381 | unsigned eflags = 0;
382 | enum {MODE_ENCRYPT = 1, MODE_DECRYPT} mode = 0;
383 |
384 | int option;
385 | while ((option = getopt(argc, argv, "DEc:hk:w")) != -1) {
386 | switch (option) {
387 | case 'E':
388 | mode = MODE_ENCRYPT;
389 | break;
390 | case 'D':
391 | mode = MODE_DECRYPT;
392 | break;
393 | case 'c':
394 | cost = atoi(optarg);
395 | if (cost < 0 || cost > BLOWFISH_MAX_COST)
396 | DIE("invalid cost (must be 1 to 63)");
397 | break;
398 | case 'h':
399 | usage(stdout);
400 | exit(EXIT_SUCCESS);
401 | break;
402 | case 'k':
403 | keyfile = optarg;
404 | break;
405 | case 'w':
406 | eflags |= FLAG_WAIT;
407 | break;
408 | default:
409 | exit(EXIT_FAILURE);
410 | }
411 | }
412 |
413 | /* Check for invalid option combinations */
414 | if (mode == MODE_DECRYPT && (eflags & FLAG_WAIT))
415 | DIE("wait option (-w) is only for encryption (-E)");
416 |
417 | /* Make sure there are no more arguments */
418 | if (argv[optind])
419 | DIE("excess non-option command line arguments: %s", argv[optind]);
420 |
421 | char salt[SALT_LENGTH + 1];
422 | struct blowfish crypt[1];
423 | struct blowfish mac[1];
424 |
425 | /* Get the salt before asking for a password, in case it fails */
426 | switch (mode) {
427 | case MODE_ENCRYPT: {
428 | generate_salt(salt);
429 | } break;
430 | case MODE_DECRYPT: {
431 | read_salt(STDIN_FILENO, salt);
432 | int in_cost = salt[SALT_LENGTH] + 256;
433 | in_cost = (in_cost - salt[SALT_LENGTH - 1]) % 256;
434 | if (in_cost > BLOWFISH_MAX_COST)
435 | DIE("ciphertext is damaged");
436 | if (cost == -1)
437 | cost = MAXIMUM_INPUT_COST;
438 | if (in_cost > cost)
439 | DIE("bcrypt cost exceeds maximum (%d > %d), use -c to adjust",
440 | in_cost, cost);
441 | cost = in_cost;
442 | } break;
443 | default: {
444 | fputs("blowpipe: ", stderr);
445 | fputs("must select encrypt (-E) or decrypt (-D)\n", stderr);
446 | usage(stderr);
447 | exit(EXIT_FAILURE);
448 | }
449 | }
450 |
451 | /* Derive the key */
452 | char key[BLOWFISH_DIGEST_LENGTH];
453 | if (!keyfile) {
454 | int verify = mode == MODE_ENCRYPT;
455 | if (cost == -1)
456 | cost = PASSPHRASE_COST;
457 | passphrase_kdf(key, salt, cost, verify);
458 | } else {
459 | int fd = open(keyfile, O_RDONLY);
460 | if (fd == -1)
461 | DIE_ERRNO(keyfile);
462 | if (cost == -1)
463 | cost = KEYFILE_COST;
464 | key_read(fd, key, salt, cost);
465 | close(fd);
466 | }
467 | salt[SALT_LENGTH] = salt[SALT_LENGTH - 1] + cost;
468 | blowfish_init(crypt, key, BLOWFISH_DIGEST_LENGTH);
469 |
470 | /* Initialize the MAC by deriving another key */
471 | uint8_t zero[BLOWFISH_SALT_LENGTH] = {0};
472 | char mackey[BLOWFISH_DIGEST_LENGTH];
473 | blowfish_bcrypt(mackey, key, BLOWFISH_DIGEST_LENGTH, zero, 0);
474 | blowfish_init(mac, mackey, BLOWFISH_DIGEST_LENGTH);
475 |
476 | ssize_t z;
477 | switch (mode) {
478 | case MODE_ENCRYPT:
479 | z = write(STDOUT_FILENO, salt, SALT_LENGTH + 1);
480 | if (z == -1)
481 | DIE_ERRNO("writing ciphertext");
482 | if (z < SALT_LENGTH + 1)
483 | DIE("failed to write ciphertext");
484 | bp_encrypt(crypt, mac, eflags);
485 | break;
486 | case MODE_DECRYPT:
487 | bp_decrypt(crypt, mac);
488 | break;
489 | }
490 | return 0;
491 | }
492 |
--------------------------------------------------------------------------------
/blowfish.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "blowfish.h"
4 |
5 | static const uint32_t blowfish_p[] = {
6 | 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
7 | 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
8 | 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
9 | 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
10 | 0x9216d5d9, 0x8979fb1b,
11 | };
12 |
13 | static const uint32_t blowfish_s[] = {
14 | 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7,
15 | 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99,
16 | 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
17 | 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e,
18 | 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee,
19 | 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
20 | 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef,
21 | 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e,
22 | 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60,
23 | 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440,
24 | 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce,
25 | 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a,
26 | 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e,
27 | 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677,
28 | 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193,
29 | 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032,
30 | 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88,
31 | 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239,
32 | 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e,
33 | 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0,
34 | 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3,
35 | 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98,
36 | 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88,
37 | 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
38 | 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6,
39 | 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d,
40 | 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b,
41 | 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7,
42 | 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba,
43 | 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463,
44 | 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f,
45 | 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09,
46 | 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3,
47 | 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb,
48 | 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279,
49 | 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8,
50 | 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab,
51 | 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82,
52 | 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db,
53 | 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573,
54 | 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0,
55 | 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
56 | 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790,
57 | 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8,
58 | 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4,
59 | 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0,
60 | 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7,
61 | 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c,
62 | 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad,
63 | 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1,
64 | 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299,
65 | 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9,
66 | 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477,
67 | 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf,
68 | 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49,
69 | 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af,
70 | 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa,
71 | 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5,
72 | 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41,
73 | 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
74 | 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400,
75 | 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915,
76 | 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664,
77 | 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a,
78 | 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623,
79 | 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266,
80 | 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1,
81 | 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e,
82 | 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6,
83 | 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
84 | 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e,
85 | 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1,
86 | 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737,
87 | 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8,
88 | 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff,
89 | 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd,
90 | 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701,
91 | 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7,
92 | 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41,
93 | 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331,
94 | 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf,
95 | 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af,
96 | 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e,
97 | 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87,
98 | 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c,
99 | 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2,
100 | 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16,
101 | 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd,
102 | 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b,
103 | 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509,
104 | 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e,
105 | 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3,
106 | 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f,
107 | 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a,
108 | 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4,
109 | 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960,
110 | 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66,
111 | 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28,
112 | 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802,
113 | 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84,
114 | 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510,
115 | 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf,
116 | 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14,
117 | 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e,
118 | 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50,
119 | 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7,
120 | 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8,
121 | 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281,
122 | 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99,
123 | 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696,
124 | 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128,
125 | 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73,
126 | 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0,
127 | 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0,
128 | 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105,
129 | 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250,
130 | 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3,
131 | 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285,
132 | 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00,
133 | 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061,
134 | 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb,
135 | 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e,
136 | 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735,
137 | 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc,
138 | 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9,
139 | 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340,
140 | 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20,
141 | 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7,
142 | 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934,
143 | 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068,
144 | 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af,
145 | 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840,
146 | 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45,
147 | 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
148 | 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a,
149 | 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb,
150 | 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee,
151 | 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6,
152 | 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42,
153 | 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b,
154 | 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2,
155 | 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb,
156 | 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527,
157 | 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b,
158 | 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33,
159 | 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c,
160 | 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3,
161 | 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc,
162 | 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17,
163 | 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564,
164 | 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b,
165 | 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115,
166 | 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922,
167 | 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728,
168 | 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0,
169 | 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e,
170 | 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37,
171 | 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d,
172 | 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804,
173 | 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b,
174 | 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3,
175 | 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb,
176 | 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d,
177 | 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c,
178 | 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350,
179 | 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9,
180 | 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a,
181 | 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe,
182 | 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d,
183 | 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc,
184 | 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f,
185 | 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61,
186 | 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2,
187 | 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9,
188 | 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2,
189 | 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c,
190 | 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e,
191 | 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633,
192 | 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10,
193 | 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169,
194 | 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52,
195 | 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027,
196 | 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5,
197 | 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62,
198 | 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634,
199 | 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76,
200 | 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24,
201 | 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc,
202 | 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4,
203 | 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c,
204 | 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837,
205 | 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0,
206 | 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b,
207 | 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe,
208 | 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
209 | 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4,
210 | 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8,
211 | 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
212 | 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304,
213 | 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22,
214 | 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4,
215 | 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6,
216 | 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9,
217 | 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
218 | 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593,
219 | 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51,
220 | 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28,
221 | 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c,
222 | 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b,
223 | 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28,
224 | 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c,
225 | 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd,
226 | 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a,
227 | 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319,
228 | 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb,
229 | 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f,
230 | 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991,
231 | 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32,
232 | 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680,
233 | 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166,
234 | 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae,
235 | 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
236 | 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5,
237 | 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47,
238 | 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370,
239 | 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d,
240 | 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84,
241 | 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048,
242 | 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8,
243 | 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd,
244 | 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9,
245 | 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7,
246 | 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38,
247 | 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f,
248 | 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c,
249 | 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525,
250 | 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1,
251 | 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442,
252 | 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964,
253 | 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
254 | 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8,
255 | 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d,
256 | 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f,
257 | 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299,
258 | 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02,
259 | 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc,
260 | 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614,
261 | 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a,
262 | 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6,
263 | 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b,
264 | 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0,
265 | 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060,
266 | 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e,
267 | 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9,
268 | 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f,
269 | 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6,
270 | };
271 |
272 | static uint32_t
273 | blowfish_f(const uint32_t s[4][256], uint32_t x)
274 | {
275 | /* big endian */
276 | uint32_t b = 0xff;
277 | uint32_t h = s[0][(x >> 24) & b] + s[1][(x >> 16) & b];
278 | return (h ^ s[2][(x >> 8) & b]) + s[3][(x >> 0) & b];
279 | }
280 |
281 | static uint32_t
282 | blowfish_read(const uint8_t *p)
283 | {
284 | /* big endian */
285 | return ((uint32_t)p[0] << 24) |
286 | ((uint32_t)p[1] << 16) |
287 | ((uint32_t)p[2] << 8) |
288 | ((uint32_t)p[3] << 0);
289 | }
290 |
291 | static void
292 | blowfish_write(uint8_t *p, uint32_t v)
293 | {
294 | /* big endian */
295 | p[0] = (uint8_t)(v >> 24);
296 | p[1] = (uint8_t)(v >> 16);
297 | p[2] = (uint8_t)(v >> 8);
298 | p[3] = (uint8_t)(v >> 0);
299 | }
300 |
301 | void
302 | blowfish_encrypt(const struct blowfish *ctx, uint32_t *bl, uint32_t *br)
303 | {
304 | uint32_t xl = *bl;
305 | uint32_t xr = *br;
306 | for (int i = 0; i < 16; i += 2) {
307 | xl ^= ctx->p[i];
308 | xr ^= blowfish_f(ctx->s, xl);
309 | xr ^= ctx->p[i + 1];
310 | xl ^= blowfish_f(ctx->s, xr);
311 | }
312 | xl ^= ctx->p[16];
313 | xr ^= ctx->p[17];
314 | *bl = xr;
315 | *br = xl;
316 | }
317 |
318 | void
319 | blowfish_decrypt(const struct blowfish *ctx, uint32_t *bl, uint32_t *br)
320 | {
321 | uint32_t xl = *bl;
322 | uint32_t xr = *br;
323 | for (int i = 16; i > 0; i -= 2) {
324 | xl ^= ctx->p[i + 1];
325 | xr ^= blowfish_f(ctx->s, xl);
326 | xr ^= ctx->p[i];
327 | xl ^= blowfish_f(ctx->s, xr);
328 | }
329 | xl ^= ctx->p[1];
330 | xr ^= ctx->p[0];
331 | *bl = xr;
332 | *br = xl;
333 | }
334 |
335 | static void
336 | blowfish_expand(
337 | struct blowfish *ctx,
338 | const void *key,
339 | int len,
340 | const void *salt)
341 | {
342 | const uint8_t *k = key;
343 | for (int i = 0; i < 18; i++) {
344 | /* big endian */
345 | ctx->p[i] ^= (uint32_t)k[(i * 4 + 0) % len] << 24;
346 | ctx->p[i] ^= (uint32_t)k[(i * 4 + 1) % len] << 16;
347 | ctx->p[i] ^= (uint32_t)k[(i * 4 + 2) % len] << 8;
348 | ctx->p[i] ^= (uint32_t)k[(i * 4 + 3) % len] << 0;
349 | }
350 |
351 | uint32_t esalt[4];
352 | esalt[0] = blowfish_read((uint8_t *)salt + 0);
353 | esalt[1] = blowfish_read((uint8_t *)salt + 4);
354 | esalt[2] = blowfish_read((uint8_t *)salt + 8);
355 | esalt[3] = blowfish_read((uint8_t *)salt + 12);
356 |
357 | uint32_t ctext[2] = {0};
358 | for (int i = 0; i < 18; i += 2) {
359 | ctext[0] ^= esalt[(i + 0) % 4];
360 | ctext[1] ^= esalt[(i + 1) % 4];
361 | blowfish_encrypt(ctx, ctext, ctext + 1);
362 | ctx->p[i + 0] = ctext[0];
363 | ctx->p[i + 1] = ctext[1];
364 | }
365 |
366 | for (int i = 0; i < 4; i++) {
367 | for (int j = 0; j < 256; j += 2) {
368 | ctext[0] ^= esalt[(j + 2) % 4];
369 | ctext[1] ^= esalt[(j + 3) % 4];
370 | blowfish_encrypt(ctx, ctext, ctext + 1);
371 | ctx->s[i][j + 0] = ctext[0];
372 | ctx->s[i][j + 1] = ctext[1];
373 | }
374 | }
375 | }
376 |
377 | void
378 | blowfish_init(struct blowfish *ctx, const void *key, int len)
379 | {
380 | assert(len >= 0 && len <= BLOWFISH_MAX_KEY_LENGTH);
381 |
382 | unsigned char zero[BLOWFISH_SALT_LENGTH] = {0};
383 | if (!len) {
384 | key = zero;
385 | len = sizeof(zero);
386 | }
387 |
388 | memcpy(ctx->s, blowfish_s, sizeof(blowfish_s));
389 | memcpy(ctx->p, blowfish_p, sizeof(blowfish_p));
390 | blowfish_expand(ctx, key, len, &zero);
391 | }
392 |
393 | void
394 | blowfish_bcrypt(
395 | void *digest,
396 | const void *pwd,
397 | int len,
398 | const void *salt,
399 | int cost)
400 | {
401 | assert(len >= 0 && len <= BLOWFISH_MAX_KEY_LENGTH);
402 | assert(cost >= 0 && cost <= BLOWFISH_MAX_COST);
403 |
404 | unsigned char zero[BLOWFISH_SALT_LENGTH] = {0};
405 | if (!len) {
406 | pwd = zero;
407 | len = sizeof(zero);
408 | }
409 |
410 | struct blowfish ctx[1];
411 | memcpy(ctx->s, blowfish_s, sizeof(blowfish_s));
412 | memcpy(ctx->p, blowfish_p, sizeof(blowfish_p));
413 |
414 | blowfish_expand(ctx, pwd, len, salt);
415 |
416 | unsigned long long n = 1ULL << cost;
417 | for (unsigned long long i = 0; i < n; i++) {
418 | blowfish_expand(ctx, pwd, len, &zero);
419 | blowfish_expand(ctx, salt, BLOWFISH_SALT_LENGTH, &zero);
420 | }
421 |
422 | /* ctext = "OrpheanBeholderScryDoubt" */
423 | uint32_t ctext[6] = {
424 | 0x4f727068, 0x65616e42, 0x65686f6c,
425 | 0x64657253, 0x63727944, 0x6f756274,
426 | };
427 | for (int i = 0; i < 64; i++)
428 | for (int j = 0; j < 6; j += 2)
429 | blowfish_encrypt(ctx, ctext + j, ctext + j + 1);
430 | for (int i = 0; i < 6; i++)
431 | blowfish_write((uint8_t *)digest + i * 4, ctext[i]);
432 | }
433 |
--------------------------------------------------------------------------------