/*
* contrib / pgcrypto / crypt - blowfish . c
*
* This code comes from John the Ripper password cracker , with reentrant
* and crypt ( 3 ) interfaces added , but optimizations specific to password
* cracking removed .
*
* Written by Solar Designer < solar at openwall . com > in 1998 - 2002 and
* placed in the public domain .
*
* There ' s absolutely no warranty .
*
* It is my intent that you should be able to use this on your system ,
* as a part of a software package , or anywhere else to improve security ,
* ensure compatibility , or for any other purpose . I would appreciate
* it if you give credit where it is due and keep your modifications in
* the public domain as well , but I don ' t require that in order to let
* you place this code and any modifications you make under a license
* of your choice .
*
* This implementation is compatible with OpenBSD bcrypt . c ( version 2 a )
* by Niels Provos < provos at citi . umich . edu > , and uses some of his
* ideas . The password hashing algorithm was designed by David Mazieres
* < dm at lcs . mit . edu > .
*
* There ' s a paper on the algorithm that explains its design decisions :
*
* http : //www.usenix.org/events/usenix99/provos.html
*
* Some of the tricks in BF_ROUND might be inspired by Eric Young ' s
* Blowfish library ( I can ' t be sure if I would think of something if I
* hadn ' t seen his code ) .
*/
#include "postgres.h"
#include "miscadmin.h"
#include "px-crypt.h"
#include "px.h"
#ifdef __i386__
#define BF_ASM 0 /* 1 */
#define BF_SCALE 1
#elif defined (__x86_64__)
#define BF_ASM 0
#define BF_SCALE 1
#else
#define BF_ASM 0
#define BF_SCALE 0
#endif
typedef unsigned int BF_word;
typedef signed int BF_word_signed;
/* Number of Blowfish rounds, this is also hardcoded into a few places */
#define BF_N 16
typedef BF_word BF_key[BF_N + 2 ];
typedef struct
{
BF_word S[4 ][0 x100];
BF_key P;
} BF_ctx;
/*
* Magic IV for 64 Blowfish encryptions that we do at the end .
* The string is " OrpheanBeholderScryDoubt " on big - endian .
*/
static BF_word BF_magic_w[6 ] = {
0 x4F727068, 0 x65616E42, 0 x65686F6C,
0 x64657253, 0 x63727944, 0 x6F756274
};
/*
* P - box and S - box tables initialized with digits of Pi .
*/
static BF_ctx BF_init_state = {
{
{
0 xd1310ba6, 0 x98dfb5ac, 0 x2ffd72db, 0 xd01adfb7,
0 xb8e1afed, 0 x6a267e96, 0 xba7c9045, 0 xf12c7f99,
0 x24a19947, 0 xb3916cf7, 0 x0801f2e2, 0 x858efc16,
0 x636920d8, 0 x71574e69, 0 xa458fea3, 0 xf4933d7e,
0 x0d95748f, 0 x728eb658, 0 x718bcd58, 0 x82154aee,
0 x7b54a41d, 0 xc25a59b5, 0 x9c30d539, 0 x2af26013,
0 xc5d1b023, 0 x286085f0, 0 xca417918, 0 xb8db38ef,
0 x8e79dcb0, 0 x603a180e, 0 x6c9e0e8b, 0 xb01e8a3e,
0 xd71577c1, 0 xbd314b27, 0 x78af2fda, 0 x55605c60,
0 xe65525f3, 0 xaa55ab94, 0 x57489862, 0 x63e81440,
0 x55ca396a, 0 x2aab10b6, 0 xb4cc5c34, 0 x1141e8ce,
0 xa15486af, 0 x7c72e993, 0 xb3ee1411, 0 x636fbc2a,
0 x2ba9c55d, 0 x741831f6, 0 xce5c3e16, 0 x9b87931e,
0 xafd6ba33, 0 x6c24cf5c, 0 x7a325381, 0 x28958677,
0 x3b8f4898, 0 x6b4bb9af, 0 xc4bfe81b, 0 x66282193,
0 x61d809cc, 0 xfb21a991, 0 x487cac60, 0 x5dec8032,
0 xef845d5d, 0 xe98575b1, 0 xdc262302, 0 xeb651b88,
0 x23893e81, 0 xd396acc5, 0 x0f6d6ff3, 0 x83f44239,
0 x2e0b4482, 0 xa4842004, 0 x69c8f04a, 0 x9e1f9b5e,
0 x21c66842, 0 xf6e96c9a, 0 x670c9c61, 0 xabd388f0,
0 x6a51a0d2, 0 xd8542f68, 0 x960fa728, 0 xab5133a3,
0 x6eef0b6c, 0 x137a3be4, 0 xba3bf050, 0 x7efb2a98,
0 xa1f1651d, 0 x39af0176, 0 x66ca593e, 0 x82430e88,
0 x8cee8619, 0 x456f9fb4, 0 x7d84a5c3, 0 x3b8b5ebe,
0 xe06f75d8, 0 x85c12073, 0 x401a449f, 0 x56c16aa6,
0 x4ed3aa62, 0 x363f7706, 0 x1bfedf72, 0 x429b023d,
0 x37d0d724, 0 xd00a1248, 0 xdb0fead3, 0 x49f1c09b,
0 x075372c9, 0 x80991b7b, 0 x25d479d8, 0 xf6e8def7,
0 xe3fe501a, 0 xb6794c3b, 0 x976ce0bd, 0 x04c006ba,
0 xc1a94fb6, 0 x409f60c4, 0 x5e5c9ec2, 0 x196a2463,
0 x68fb6faf, 0 x3e6c53b5, 0 x1339b2eb, 0 x3b52ec6f,
0 x6dfc511f, 0 x9b30952c, 0 xcc814544, 0 xaf5ebd09,
0 xbee3d004, 0 xde334afd, 0 x660f2807, 0 x192e4bb3,
0 xc0cba857, 0 x45c8740f, 0 xd20b5f39, 0 xb9d3fbdb,
0 x5579c0bd, 0 x1a60320a, 0 xd6a100c6, 0 x402c7279,
0 x679f25fe, 0 xfb1fa3cc, 0 x8ea5e9f8, 0 xdb3222f8,
0 x3c7516df, 0 xfd616b15, 0 x2f501ec8, 0 xad0552ab,
0 x323db5fa, 0 xfd238760, 0 x53317b48, 0 x3e00df82,
0 x9e5c57bb, 0 xca6f8ca0, 0 x1a87562e, 0 xdf1769db,
0 xd542a8f6, 0 x287effc3, 0 xac6732c6, 0 x8c4f5573,
0 x695b27b0, 0 xbbca58c8, 0 xe1ffa35d, 0 xb8f011a0,
0 x10fa3d98, 0 xfd2183b8, 0 x4afcb56c, 0 x2dd1d35b,
0 x9a53e479, 0 xb6f84565, 0 xd28e49bc, 0 x4bfb9790,
0 xe1ddf2da, 0 xa4cb7e33, 0 x62fb1341, 0 xcee4c6e8,
0 xef20cada, 0 x36774c01, 0 xd07e9efe, 0 x2bf11fb4,
0 x95dbda4d, 0 xae909198, 0 xeaad8e71, 0 x6b93d5a0,
0 xd08ed1d0, 0 xafc725e0, 0 x8e3c5b2f, 0 x8e7594b7,
0 x8ff6e2fb, 0 xf2122b64, 0 x8888b812, 0 x900df01c,
0 x4fad5ea0, 0 x688fc31c, 0 xd1cff191, 0 xb3a8c1ad,
0 x2f2f2218, 0 xbe0e1777, 0 xea752dfe, 0 x8b021fa1,
0 xe5a0cc0f, 0 xb56f74e8, 0 x18acf3d6, 0 xce89e299,
0 xb4a84fe0, 0 xfd13e0b7, 0 x7cc43b81, 0 xd2ada8d9,
0 x165fa266, 0 x80957705, 0 x93cc7314, 0 x211a1477,
0 xe6ad2065, 0 x77b5fa86, 0 xc75442f5, 0 xfb9d35cf,
0 xebcdaf0c, 0 x7b3e89a0, 0 xd6411bd3, 0 xae1e7e49,
0 x00250e2d, 0 x2071b35e, 0 x226800bb, 0 x57b8e0af,
0 x2464369b, 0 xf009b91e, 0 x5563911d, 0 x59dfa6aa,
0 x78c14389, 0 xd95a537f, 0 x207d5ba2, 0 x02e5b9c5,
0 x83260376, 0 x6295cfa9, 0 x11c81968, 0 x4e734a41,
0 xb3472dca, 0 x7b14a94a, 0 x1b510052, 0 x9a532915,
0 xd60f573f, 0 xbc9bc6e4, 0 x2b60a476, 0 x81e67400,
0 x08ba6fb5, 0 x571be91f, 0 xf296ec6b, 0 x2a0dd915,
0 xb6636521, 0 xe7b9f9b6, 0 xff34052e, 0 xc5855664,
0 x53b02d5d, 0 xa99f8fa1, 0 x08ba4799, 0 x6e85076a
}, {
0 x4b7a70e9, 0 xb5b32944, 0 xdb75092e, 0 xc4192623,
0 xad6ea6b0, 0 x49a7df7d, 0 x9cee60b8, 0 x8fedb266,
0 xecaa8c71, 0 x699a17ff, 0 x5664526c, 0 xc2b19ee1,
0 x193602a5, 0 x75094c29, 0 xa0591340, 0 xe4183a3e,
0 x3f54989a, 0 x5b429d65, 0 x6b8fe4d6, 0 x99f73fd6,
0 xa1d29c07, 0 xefe830f5, 0 x4d2d38e6, 0 xf0255dc1,
0 x4cdd2086, 0 x8470eb26, 0 x6382e9c6, 0 x021ecc5e,
0 x09686b3f, 0 x3ebaefc9, 0 x3c971814, 0 x6b6a70a1,
0 x687f3584, 0 x52a0e286, 0 xb79c5305, 0 xaa500737,
0 x3e07841c, 0 x7fdeae5c, 0 x8e7d44ec, 0 x5716f2b8,
0 xb03ada37, 0 xf0500c0d, 0 xf01c1f04, 0 x0200b3ff,
0 xae0cf51a, 0 x3cb574b2, 0 x25837a58, 0 xdc0921bd,
0 xd19113f9, 0 x7ca92ff6, 0 x94324773, 0 x22f54701,
0 x3ae5e581, 0 x37c2dadc, 0 xc8b57634, 0 x9af3dda7,
0 xa9446146, 0 x0fd0030e, 0 xecc8c73e, 0 xa4751e41,
0 xe238cd99, 0 x3bea0e2f, 0 x3280bba1, 0 x183eb331,
0 x4e548b38, 0 x4f6db908, 0 x6f420d03, 0 xf60a04bf,
0 x2cb81290, 0 x24977c79, 0 x5679b072, 0 xbcaf89af,
0 xde9a771f, 0 xd9930810, 0 xb38bae12, 0 xdccf3f2e,
0 x5512721f, 0 x2e6b7124, 0 x501adde6, 0 x9f84cd87,
0 x7a584718, 0 x7408da17, 0 xbc9f9abc, 0 xe94b7d8c,
0 xec7aec3a, 0 xdb851dfa, 0 x63094366, 0 xc464c3d2,
0 xef1c1847, 0 x3215d908, 0 xdd433b37, 0 x24c2ba16,
0 x12a14d43, 0 x2a65c451, 0 x50940002, 0 x133ae4dd,
0 x71dff89e, 0 x10314e55, 0 x81ac77d6, 0 x5f11199b,
0 x043556f1, 0 xd7a3c76b, 0 x3c11183b, 0 x5924a509,
0 xf28fe6ed, 0 x97f1fbfa, 0 x9ebabf2c, 0 x1e153c6e,
0 x86e34570, 0 xeae96fb1, 0 x860e5e0a, 0 x5a3e2ab3,
0 x771fe71c, 0 x4e3d06fa, 0 x2965dcb9, 0 x99e71d0f,
0 x803e89d6, 0 x5266c825, 0 x2e4cc978, 0 x9c10b36a,
0 xc6150eba, 0 x94e2ea78, 0 xa5fc3c53, 0 x1e0a2df4,
0 xf2f74ea7, 0 x361d2b3d, 0 x1939260f, 0 x19c27960,
0 x5223a708, 0 xf71312b6, 0 xebadfe6e, 0 xeac31f66,
0 xe3bc4595, 0 xa67bc883, 0 xb17f37d1, 0 x018cff28,
0 xc332ddef, 0 xbe6c5aa5, 0 x65582185, 0 x68ab9802,
0 xeecea50f, 0 xdb2f953b, 0 x2aef7dad, 0 x5b6e2f84,
0 x1521b628, 0 x29076170, 0 xecdd4775, 0 x619f1510,
0 x13cca830, 0 xeb61bd96, 0 x0334fe1e, 0 xaa0363cf,
0 xb5735c90, 0 x4c70a239, 0 xd59e9e0b, 0 xcbaade14,
0 xeecc86bc, 0 x60622ca7, 0 x9cab5cab, 0 xb2f3846e,
0 x648b1eaf, 0 x19bdf0ca, 0 xa02369b9, 0 x655abb50,
0 x40685a32, 0 x3c2ab4b3, 0 x319ee9d5, 0 xc021b8f7,
0 x9b540b19, 0 x875fa099, 0 x95f7997e, 0 x623d7da8,
0 xf837889a, 0 x97e32d77, 0 x11ed935f, 0 x16681281,
0 x0e358829, 0 xc7e61fd6, 0 x96dedfa1, 0 x7858ba99,
0 x57f584a5, 0 x1b227263, 0 x9b83c3ff, 0 x1ac24696,
0 xcdb30aeb, 0 x532e3054, 0 x8fd948e4, 0 x6dbc3128,
0 x58ebf2ef, 0 x34c6ffea, 0 xfe28ed61, 0 xee7c3c73,
0 x5d4a14d9, 0 xe864b7e3, 0 x42105d14, 0 x203e13e0,
0 x45eee2b6, 0 xa3aaabea, 0 xdb6c4f15, 0 xfacb4fd0,
0 xc742f442, 0 xef6abbb5, 0 x654f3b1d, 0 x41cd2105,
0 xd81e799e, 0 x86854dc7, 0 xe44b476a, 0 x3d816250,
0 xcf62a1f2, 0 x5b8d2646, 0 xfc8883a0, 0 xc1c7b6a3,
0 x7f1524c3, 0 x69cb7492, 0 x47848a0b, 0 x5692b285,
0 x095bbf00, 0 xad19489d, 0 x1462b174, 0 x23820e00,
0 x58428d2a, 0 x0c55f5ea, 0 x1dadf43e, 0 x233f7061,
0 x3372f092, 0 x8d937e41, 0 xd65fecf1, 0 x6c223bdb,
0 x7cde3759, 0 xcbee7460, 0 x4085f2a7, 0 xce77326e,
0 xa6078084, 0 x19f8509e, 0 xe8efd855, 0 x61d99735,
0 xa969a7aa, 0 xc50c06c2, 0 x5a04abfc, 0 x800bcadc,
0 x9e447a2e, 0 xc3453484, 0 xfdd56705, 0 x0e1e9ec9,
0 xdb73dbd3, 0 x105588cd, 0 x675fda79, 0 xe3674340,
0 xc5c43465, 0 x713e38d8, 0 x3d28f89e, 0 xf16dff20,
0 x153e21e7, 0 x8fb03d4a, 0 xe6e39f2b, 0 xdb83adf7
}, {
0 xe93d5a68, 0 x948140f7, 0 xf64c261c, 0 x94692934,
0 x411520f7, 0 x7602d4f7, 0 xbcf46b2e, 0 xd4a20068,
0 xd4082471, 0 x3320f46a, 0 x43b7d4b7, 0 x500061af,
0 x1e39f62e, 0 x97244546, 0 x14214f74, 0 xbf8b8840,
0 x4d95fc1d, 0 x96b591af, 0 x70f4ddd3, 0 x66a02f45,
0 xbfbc09ec, 0 x03bd9785, 0 x7fac6dd0, 0 x31cb8504,
0 x96eb27b3, 0 x55fd3941, 0 xda2547e6, 0 xabca0a9a,
0 x28507825, 0 x530429f4, 0 x0a2c86da, 0 xe9b66dfb,
0 x68dc1462, 0 xd7486900, 0 x680ec0a4, 0 x27a18dee,
0 x4f3ffea2, 0 xe887ad8c, 0 xb58ce006, 0 x7af4d6b6,
0 xaace1e7c, 0 xd3375fec, 0 xce78a399, 0 x406b2a42,
0 x20fe9e35, 0 xd9f385b9, 0 xee39d7ab, 0 x3b124e8b,
0 x1dc9faf7, 0 x4b6d1856, 0 x26a36631, 0 xeae397b2,
0 x3a6efa74, 0 xdd5b4332, 0 x6841e7f7, 0 xca7820fb,
0 xfb0af54e, 0 xd8feb397, 0 x454056ac, 0 xba489527,
0 x55533a3a, 0 x20838d87, 0 xfe6ba9b7, 0 xd096954b,
0 x55a867bc, 0 xa1159a58, 0 xcca92963, 0 x99e1db33,
0 xa62a4a56, 0 x3f3125f9, 0 x5ef47e1c, 0 x9029317c,
0 xfdf8e802, 0 x04272f70, 0 x80bb155c, 0 x05282ce3,
0 x95c11548, 0 xe4c66d22, 0 x48c1133f, 0 xc70f86dc,
0 x07f9c9ee, 0 x41041f0f, 0 x404779a4, 0 x5d886e17,
0 x325f51eb, 0 xd59bc0d1, 0 xf2bcc18f, 0 x41113564,
0 x257b7834, 0 x602a9c60, 0 xdff8e8a3, 0 x1f636c1b,
0 x0e12b4c2, 0 x02e1329e, 0 xaf664fd1, 0 xcad18115,
0 x6b2395e0, 0 x333e92e1, 0 x3b240b62, 0 xeebeb922,
0 x85b2a20e, 0 xe6ba0d99, 0 xde720c8c, 0 x2da2f728,
0 xd0127845, 0 x95b794fd, 0 x647d0862, 0 xe7ccf5f0,
0 x5449a36f, 0 x877d48fa, 0 xc39dfd27, 0 xf33e8d1e,
0 x0a476341, 0 x992eff74, 0 x3a6f6eab, 0 xf4f8fd37,
0 xa812dc60, 0 xa1ebddf8, 0 x991be14c, 0 xdb6e6b0d,
0 xc67b5510, 0 x6d672c37, 0 x2765d43b, 0 xdcd0e804,
0 xf1290dc7, 0 xcc00ffa3, 0 xb5390f92, 0 x690fed0b,
0 x667b9ffb, 0 xcedb7d9c, 0 xa091cf0b, 0 xd9155ea3,
0 xbb132f88, 0 x515bad24, 0 x7b9479bf, 0 x763bd6eb,
0 x37392eb3, 0 xcc115979, 0 x8026e297, 0 xf42e312d,
0 x6842ada7, 0 xc66a2b3b, 0 x12754ccc, 0 x782ef11c,
0 x6a124237, 0 xb79251e7, 0 x06a1bbe6, 0 x4bfb6350,
0 x1a6b1018, 0 x11caedfa, 0 x3d25bdd8, 0 xe2e1c3c9,
0 x44421659, 0 x0a121386, 0 xd90cec6e, 0 xd5abea2a,
0 x64af674e, 0 xda86a85f, 0 xbebfe988, 0 x64e4c3fe,
0 x9dbc8057, 0 xf0f7c086, 0 x60787bf8, 0 x6003604d,
0 xd1fd8346, 0 xf6381fb0, 0 x7745ae04, 0 xd736fccc,
0 x83426b33, 0 xf01eab71, 0 xb0804187, 0 x3c005e5f,
0 x77a057be, 0 xbde8ae24, 0 x55464299, 0 xbf582e61,
0 x4e58f48f, 0 xf2ddfda2, 0 xf474ef38, 0 x8789bdc2,
0 x5366f9c3, 0 xc8b38e74, 0 xb475f255, 0 x46fcd9b9,
0 x7aeb2661, 0 x8b1ddf84, 0 x846a0e79, 0 x915f95e2,
0 x466e598e, 0 x20b45770, 0 x8cd55591, 0 xc902de4c,
0 xb90bace1, 0 xbb8205d0, 0 x11a86248, 0 x7574a99e,
0 xb77f19b6, 0 xe0a9dc09, 0 x662d09a1, 0 xc4324633,
0 xe85a1f02, 0 x09f0be8c, 0 x4a99a025, 0 x1d6efe10,
0 x1ab93d1d, 0 x0ba5a4df, 0 xa186f20f, 0 x2868f169,
0 xdcb7da83, 0 x573906fe, 0 xa1e2ce9b, 0 x4fcd7f52,
0 x50115e01, 0 xa70683fa, 0 xa002b5c4, 0 x0de6d027,
0 x9af88c27, 0 x773f8641, 0 xc3604c06, 0 x61a806b5,
0 xf0177a28, 0 xc0f586e0, 0 x006058aa, 0 x30dc7d62,
0 x11e69ed7, 0 x2338ea63, 0 x53c2dd94, 0 xc2c21634,
0 xbbcbee56, 0 x90bcb6de, 0 xebfc7da1, 0 xce591d76,
0 x6f05e409, 0 x4b7c0188, 0 x39720a3d, 0 x7c927c24,
0 x86e3725f, 0 x724d9db9, 0 x1ac15bb4, 0 xd39eb8fc,
0 xed545578, 0 x08fca5b5, 0 xd83d7cd3, 0 x4dad0fc4,
0 x1e50ef5e, 0 xb161e6f8, 0 xa28514d9, 0 x6c51133c,
0 x6fd5c7e7, 0 x56e14ec4, 0 x362abfce, 0 xddc6c837,
0 xd79a3234, 0 x92638212, 0 x670efa8e, 0 x406000e0
}, {
0 x3a39ce37, 0 xd3faf5cf, 0 xabc27737, 0 x5ac52d1b,
0 x5cb0679e, 0 x4fa33742, 0 xd3822740, 0 x99bc9bbe,
0 xd5118e9d, 0 xbf0f7315, 0 xd62d1c7e, 0 xc700c47b,
0 xb78c1b6b, 0 x21a19045, 0 xb26eb1be, 0 x6a366eb4,
0 x5748ab2f, 0 xbc946e79, 0 xc6a376d2, 0 x6549c2c8,
0 x530ff8ee, 0 x468dde7d, 0 xd5730a1d, 0 x4cd04dc6,
0 x2939bbdb, 0 xa9ba4650, 0 xac9526e8, 0 xbe5ee304,
0 xa1fad5f0, 0 x6a2d519a, 0 x63ef8ce2, 0 x9a86ee22,
0 xc089c2b8, 0 x43242ef6, 0 xa51e03aa, 0 x9cf2d0a4,
0 x83c061ba, 0 x9be96a4d, 0 x8fe51550, 0 xba645bd6,
0 x2826a2f9, 0 xa73a3ae1, 0 x4ba99586, 0 xef5562e9,
0 xc72fefd3, 0 xf752f7da, 0 x3f046f69, 0 x77fa0a59,
0 x80e4a915, 0 x87b08601, 0 x9b09e6ad, 0 x3b3ee593,
0 xe990fd5a, 0 x9e34d797, 0 x2cf0b7d9, 0 x022b8b51,
0 x96d5ac3a, 0 x017da67d, 0 xd1cf3ed6, 0 x7c7d2d28,
0 x1f9f25cf, 0 xadf2b89b, 0 x5ad6b472, 0 x5a88f54c,
0 xe029ac71, 0 xe019a5e6, 0 x47b0acfd, 0 xed93fa9b,
0 xe8d3c48d, 0 x283b57cc, 0 xf8d56629, 0 x79132e28,
0 x785f0191, 0 xed756055, 0 xf7960e44, 0 xe3d35e8c,
0 x15056dd4, 0 x88f46dba, 0 x03a16125, 0 x0564f0bd,
0 xc3eb9e15, 0 x3c9057a2, 0 x97271aec, 0 xa93a072a,
0 x1b3f6d9b, 0 x1e6321f5, 0 xf59c66fb, 0 x26dcf319,
0 x7533d928, 0 xb155fdf5, 0 x03563482, 0 x8aba3cbb,
0 x28517711, 0 xc20ad9f8, 0 xabcc5167, 0 xccad925f,
0 x4de81751, 0 x3830dc8e, 0 x379d5862, 0 x9320f991,
0 xea7a90c2, 0 xfb3e7bce, 0 x5121ce64, 0 x774fbe32,
0 xa8b6e37e, 0 xc3293d46, 0 x48de5369, 0 x6413e680,
0 xa2ae0810, 0 xdd6db224, 0 x69852dfd, 0 x09072166,
0 xb39a460a, 0 x6445c0dd, 0 x586cdecf, 0 x1c20c8ae,
0 x5bbef7dd, 0 x1b588d40, 0 xccd2017f, 0 x6bb4e3bb,
0 xdda26a7e, 0 x3a59ff45, 0 x3e350a44, 0 xbcb4cdd5,
0 x72eacea8, 0 xfa6484bb, 0 x8d6612ae, 0 xbf3c6f47,
0 xd29be463, 0 x542f5d9e, 0 xaec2771b, 0 xf64e6370,
0 x740e0d8d, 0 xe75b1357, 0 xf8721671, 0 xaf537d5d,
0 x4040cb08, 0 x4eb4e2cc, 0 x34d2466a, 0 x0115af84,
0 xe1b00428, 0 x95983a1d, 0 x06b89fb4, 0 xce6ea048,
0 x6f3f3b82, 0 x3520ab82, 0 x011a1d4b, 0 x277227f8,
0 x611560b1, 0 xe7933fdc, 0 xbb3a792b, 0 x344525bd,
0 xa08839e1, 0 x51ce794b, 0 x2f32c9b7, 0 xa01fbac9,
0 xe01cc87e, 0 xbcc7d1f6, 0 xcf0111c3, 0 xa1e8aac7,
0 x1a908749, 0 xd44fbd9a, 0 xd0dadecb, 0 xd50ada38,
0 x0339c32a, 0 xc6913667, 0 x8df9317c, 0 xe0b12b4f,
0 xf79e59b7, 0 x43f5bb3a, 0 xf2d519ff, 0 x27d9459c,
0 xbf97222c, 0 x15e6fc2a, 0 x0f91fc71, 0 x9b941525,
0 xfae59361, 0 xceb69ceb, 0 xc2a86459, 0 x12baa8d1,
0 xb6c1075e, 0 xe3056a0c, 0 x10d25065, 0 xcb03a442,
0 xe0ec6e0e, 0 x1698db3b, 0 x4c98a0be, 0 x3278e964,
0 x9f1f9532, 0 xe0d392df, 0 xd3a0342b, 0 x8971f21e,
0 x1b0a7441, 0 x4ba3348c, 0 xc5be7120, 0 xc37632d8,
0 xdf359f8d, 0 x9b992f2e, 0 xe60b6f47, 0 x0fe3f11d,
0 xe54cda54, 0 x1edad891, 0 xce6279cf, 0 xcd3e7e6f,
0 x1618b166, 0 xfd2c1d05, 0 x848fd2c5, 0 xf6fb2299,
0 xf523f357, 0 xa6327623, 0 x93a83531, 0 x56cccd02,
0 xacf08162, 0 x5a75ebb5, 0 x6e163697, 0 x88d273cc,
0 xde966292, 0 x81b949d0, 0 x4c50901b, 0 x71c65614,
0 xe6c6c7bd, 0 x327a140a, 0 x45e1d006, 0 xc3f27b9a,
0 xc9aa53fd, 0 x62a80f00, 0 xbb25bfe2, 0 x35bdd2f6,
0 x71126905, 0 xb2040222, 0 xb6cbcf7c, 0 xcd769c2b,
0 x53113ec0, 0 x1640e3d3, 0 x38abbd60, 0 x2547adf0,
0 xba38209c, 0 xf746ce76, 0 x77afa1c5, 0 x20756060,
0 x85cbfe4e, 0 x8ae88dd8, 0 x7aaaf9b0, 0 x4cf9aa7e,
0 x1948c25c, 0 x02fb8a8c, 0 x01c36ae4, 0 xd6ebe1f9,
0 x90d4f869, 0 xa65cdea0, 0 x3f09252d, 0 xc208e69f,
0 xb74e6132, 0 xce77e25b, 0 x578fdfe3, 0 x3ac372e6
}
}, {
0 x243f6a88, 0 x85a308d3, 0 x13198a2e, 0 x03707344,
0 xa4093822, 0 x299f31d0, 0 x082efa98, 0 xec4e6c89,
0 x452821e6, 0 x38d01377, 0 xbe5466cf, 0 x34e90c6c,
0 xc0ac29b7, 0 xc97c50dd, 0 x3f84d5b5, 0 xb5470917,
0 x9216d5d9, 0 x8979fb1b
}
};
static unsigned char BF_itoa64[64 + 1 ] =
"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" ;
static unsigned char BF_atoi64[0 x60] = {
64 , 64 , 64 , 64 , 64 , 64 , 64 , 64 , 64 , 64 , 64 , 64 , 64 , 64 , 0 , 1 ,
54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 62 , 63 , 64 , 64 , 64 , 64 , 64 , 64 ,
64 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 ,
17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 64 , 64 , 64 , 64 , 64 ,
64 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 ,
43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 52 , 53 , 64 , 64 , 64 , 64 , 64
};
#define BF_safe_atoi64(dst, src) \
do { \
tmp = (unsigned char )(src); \
if ((unsigned int )(tmp -= 0 x20) >= 0 x60) return -1 ; \
tmp = BF_atoi64[tmp]; \
if (tmp > 63 ) return -1 ; \
(dst) = tmp; \
} while (0 )
static int
BF_decode(BF_word *dst, const char *src, int size)
{
unsigned char *dptr = (unsigned char *) dst;
unsigned char *end = dptr + size;
const unsigned char *sptr = (const unsigned char *) src;
unsigned int tmp,
c1,
c2,
c3,
c4;
do
{
BF_safe_atoi64(c1, *sptr++);
BF_safe_atoi64(c2, *sptr++);
*dptr++ = (c1 << 2 ) | ((c2 & 0 x30) >> 4 );
if (dptr >= end)
break ;
BF_safe_atoi64(c3, *sptr++);
*dptr++ = ((c2 & 0 x0F) << 4 ) | ((c3 & 0 x3C) >> 2 );
if (dptr >= end)
break ;
BF_safe_atoi64(c4, *sptr++);
*dptr++ = ((c3 & 0 x03) << 6 ) | c4;
} while (dptr < end);
return 0 ;
}
static void
BF_encode(char *dst, const BF_word *src, int size)
{
const unsigned char *sptr = (const unsigned char *) src;
const unsigned char *end = sptr + size;
unsigned char *dptr = (unsigned char *) dst;
unsigned int c1,
c2;
do
{
c1 = *sptr++;
*dptr++ = BF_itoa64[c1 >> 2 ];
c1 = (c1 & 0 x03) << 4 ;
if (sptr >= end)
{
*dptr++ = BF_itoa64[c1];
break ;
}
c2 = *sptr++;
c1 |= c2 >> 4 ;
*dptr++ = BF_itoa64[c1];
c1 = (c2 & 0 x0f) << 2 ;
if (sptr >= end)
{
*dptr++ = BF_itoa64[c1];
break ;
}
c2 = *sptr++;
c1 |= c2 >> 6 ;
*dptr++ = BF_itoa64[c1];
*dptr++ = BF_itoa64[c2 & 0 x3f];
} while (sptr < end);
}
static void
BF_swap(BF_word *x, int count)
{
/* Swap on little-endian hardware, else do nothing */
#ifndef WORDS_BIGENDIAN
BF_word tmp;
do
{
tmp = *x;
tmp = (tmp << 16 ) | (tmp >> 16 );
*x++ = ((tmp & 0 x00FF00FF) << 8 ) | ((tmp >> 8 ) & 0 x00FF00FF);
} while (--count);
#endif
}
#if BF_SCALE
/* Architectures which can shift addresses left by 2 bits with no extra cost */
#define BF_ROUND(L, R, N) \
tmp1 = (L) & 0 xFF; \
tmp2 = (L) >> 8 ; \
tmp2 &= 0 xFF; \
tmp3 = (L) >> 16 ; \
tmp3 &= 0 xFF; \
tmp4 = (L) >> 24 ; \
tmp1 = data.ctx.S[3 ][tmp1]; \
tmp2 = data.ctx.S[2 ][tmp2]; \
tmp3 = data.ctx.S[1 ][tmp3]; \
tmp3 += data.ctx.S[0 ][tmp4]; \
tmp3 ^= tmp2; \
(R) ^= data.ctx.P[(N) + 1 ]; \
tmp3 += tmp1; \
(R) ^= tmp3
#else
/* Architectures with no complicated addressing modes supported */
#define BF_INDEX(S, i) \
(*((BF_word *)(((unsigned char *)(S)) + (i))))
#define BF_ROUND(L, R, N) \
tmp1 = (L) & 0 xFF; \
tmp1 <<= 2 ; \
tmp2 = (L) >> 6 ; \
tmp2 &= 0 x3FC; \
tmp3 = (L) >> 14 ; \
tmp3 &= 0 x3FC; \
tmp4 = (L) >> 22 ; \
tmp4 &= 0 x3FC; \
tmp1 = BF_INDEX(data.ctx.S[3 ], tmp1); \
tmp2 = BF_INDEX(data.ctx.S[2 ], tmp2); \
tmp3 = BF_INDEX(data.ctx.S[1 ], tmp3); \
tmp3 += BF_INDEX(data.ctx.S[0 ], tmp4); \
tmp3 ^= tmp2; \
(R) ^= data.ctx.P[(N) + 1 ]; \
tmp3 += tmp1; \
(R) ^= tmp3
#endif
/*
* Encrypt one block , BF_N is hardcoded here .
*/
#define BF_ENCRYPT \
L ^= data.ctx.P[0 ]; \
BF_ROUND(L, R, 0 ); \
BF_ROUND(R, L, 1 ); \
BF_ROUND(L, R, 2 ); \
BF_ROUND(R, L, 3 ); \
BF_ROUND(L, R, 4 ); \
BF_ROUND(R, L, 5 ); \
BF_ROUND(L, R, 6 ); \
BF_ROUND(R, L, 7 ); \
BF_ROUND(L, R, 8 ); \
BF_ROUND(R, L, 9 ); \
BF_ROUND(L, R, 10 ); \
BF_ROUND(R, L, 11 ); \
BF_ROUND(L, R, 12 ); \
BF_ROUND(R, L, 13 ); \
BF_ROUND(L, R, 14 ); \
BF_ROUND(R, L, 15 ); \
tmp4 = R; \
R = L; \
L = tmp4 ^ data.ctx.P[BF_N + 1 ]
#if BF_ASM
extern void _BF_body_r(BF_ctx *ctx);
#define BF_body() \
_BF_body_r(&data.ctx)
#else
#define BF_body() \
do { \
L = R = 0 ; \
ptr = data.ctx.P; \
do { \
ptr += 2 ; \
BF_ENCRYPT; \
*(ptr - 2 ) = L; \
*(ptr - 1 ) = R; \
} while (ptr < &data.ctx.P[BF_N + 2 ]); \
\
ptr = data.ctx.S[0 ]; \
do { \
ptr += 2 ; \
BF_ENCRYPT; \
*(ptr - 2 ) = L; \
*(ptr - 1 ) = R; \
} while (ptr < &data.ctx.S[3 ][0 xFF]); \
} while (0 )
#endif
static void
BF_set_key(const char *key, BF_key expanded, BF_key initial,
int sign_extension_bug)
{
const char *ptr = key;
int i,
j;
BF_word tmp;
for (i = 0 ; i < BF_N + 2 ; i++)
{
tmp = 0 ;
for (j = 0 ; j < 4 ; j++)
{
tmp <<= 8 ;
if (sign_extension_bug)
tmp |= (BF_word_signed) (signed char ) *ptr;
else
tmp |= (unsigned char ) *ptr;
if (!*ptr)
ptr = key;
else
ptr++;
}
expanded[i] = tmp;
initial[i] = BF_init_state.P[i] ^ tmp;
}
}
char *
_crypt_blowfish_rn(const char *key, const char *setting,
char *output, int size)
{
struct
{
BF_ctx ctx;
BF_key expanded_key;
union
{
BF_word salt[4 ];
BF_word output[6 ];
} binary;
} data;
BF_word L,
R;
BF_word tmp1,
tmp2,
tmp3,
tmp4;
BF_word *ptr;
BF_word count;
int i;
if (size < 7 + 22 + 31 + 1 )
return NULL;
/*
* Blowfish salt value must be formatted as follows : " $ 2 a $ " or " $ 2 x $ " , a
* two digit cost parameter , " $ " , and 22 digits from the alphabet
* " . / 0 - 9 A - Za - z " . - - from the PHP crypt docs . Apparently we enforce a few
* more restrictions on the count in the salt as well .
*/
if (strlen(setting) < 29 )
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid salt" )));
if (setting[0 ] != '$' ||
setting[1 ] != '2' ||
(setting[2 ] != 'a' && setting[2 ] != 'x' ) ||
setting[3 ] != '$' ||
setting[4 ] < '0' || setting[4 ] > '3' ||
setting[5 ] < '0' || setting[5 ] > '9' ||
(setting[4 ] == '3' && setting[5 ] > '1' ) ||
setting[6 ] != '$' )
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid salt" )));
}
count = (BF_word) 1 << ((setting[4 ] - '0' ) * 10 + (setting[5 ] - '0' ));
if (count < 16 || BF_decode(data.binary.salt, &setting[7 ], 16 ))
{
px_memset(data.binary.salt, 0 , sizeof (data.binary.salt));
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid salt" )));
}
BF_swap(data.binary.salt, 4 );
BF_set_key(key, data.expanded_key, data.ctx.P, setting[2 ] == 'x' );
memcpy(data.ctx.S, BF_init_state.S, sizeof (data.ctx.S));
L = R = 0 ;
for (i = 0 ; i < BF_N + 2 ; i += 2 )
{
L ^= data.binary.salt[i & 2 ];
R ^= data.binary.salt[(i & 2 ) + 1 ];
BF_ENCRYPT;
data.ctx.P[i] = L;
data.ctx.P[i + 1 ] = R;
}
ptr = data.ctx.S[0 ];
do
{
ptr += 4 ;
L ^= data.binary.salt[(BF_N + 2 ) & 3 ];
R ^= data.binary.salt[(BF_N + 3 ) & 3 ];
BF_ENCRYPT;
*(ptr - 4 ) = L;
*(ptr - 3 ) = R;
L ^= data.binary.salt[(BF_N + 4 ) & 3 ];
R ^= data.binary.salt[(BF_N + 5 ) & 3 ];
BF_ENCRYPT;
*(ptr - 2 ) = L;
*(ptr - 1 ) = R;
} while (ptr < &data.ctx.S[3 ][0 xFF]);
do
{
CHECK_FOR_INTERRUPTS();
data.ctx.P[0 ] ^= data.expanded_key[0 ];
data.ctx.P[1 ] ^= data.expanded_key[1 ];
data.ctx.P[2 ] ^= data.expanded_key[2 ];
data.ctx.P[3 ] ^= data.expanded_key[3 ];
data.ctx.P[4 ] ^= data.expanded_key[4 ];
data.ctx.P[5 ] ^= data.expanded_key[5 ];
data.ctx.P[6 ] ^= data.expanded_key[6 ];
data.ctx.P[7 ] ^= data.expanded_key[7 ];
data.ctx.P[8 ] ^= data.expanded_key[8 ];
data.ctx.P[9 ] ^= data.expanded_key[9 ];
data.ctx.P[10 ] ^= data.expanded_key[10 ];
data.ctx.P[11 ] ^= data.expanded_key[11 ];
data.ctx.P[12 ] ^= data.expanded_key[12 ];
data.ctx.P[13 ] ^= data.expanded_key[13 ];
data.ctx.P[14 ] ^= data.expanded_key[14 ];
data.ctx.P[15 ] ^= data.expanded_key[15 ];
data.ctx.P[16 ] ^= data.expanded_key[16 ];
data.ctx.P[17 ] ^= data.expanded_key[17 ];
BF_body();
tmp1 = data.binary.salt[0 ];
tmp2 = data.binary.salt[1 ];
tmp3 = data.binary.salt[2 ];
tmp4 = data.binary.salt[3 ];
data.ctx.P[0 ] ^= tmp1;
data.ctx.P[1 ] ^= tmp2;
data.ctx.P[2 ] ^= tmp3;
data.ctx.P[3 ] ^= tmp4;
data.ctx.P[4 ] ^= tmp1;
data.ctx.P[5 ] ^= tmp2;
data.ctx.P[6 ] ^= tmp3;
data.ctx.P[7 ] ^= tmp4;
data.ctx.P[8 ] ^= tmp1;
data.ctx.P[9 ] ^= tmp2;
data.ctx.P[10 ] ^= tmp3;
data.ctx.P[11 ] ^= tmp4;
data.ctx.P[12 ] ^= tmp1;
data.ctx.P[13 ] ^= tmp2;
data.ctx.P[14 ] ^= tmp3;
data.ctx.P[15 ] ^= tmp4;
data.ctx.P[16 ] ^= tmp1;
data.ctx.P[17 ] ^= tmp2;
BF_body();
} while (--count);
for (i = 0 ; i < 6 ; i += 2 )
{
L = BF_magic_w[i];
R = BF_magic_w[i + 1 ];
count = 64 ;
do
{
BF_ENCRYPT;
} while (--count);
data.binary.output[i] = L;
data.binary.output[i + 1 ] = R;
}
memcpy(output, setting, 7 + 22 - 1 );
output[7 + 22 - 1 ] = BF_itoa64[(int )
BF_atoi64[(int ) setting[7 + 22 - 1 ] - 0 x20] & 0 x30];
/* This has to be bug-compatible with the original implementation, so
* only encode 23 of the 24 bytes. :-) */
BF_swap(data.binary.output, 6 );
BF_encode(&output[7 + 22 ], data.binary.output, 23 );
output[7 + 22 + 31 ] = '\0' ;
/* Overwrite the most obvious sensitive data we have on the stack. Note
* that this does not guarantee there ' s no sensitive data left on the
* stack and/or in registers; I'm not aware of portable code that does. */
px_memset(&data, 0 , sizeof (data));
return output;
}
Messung V0.5 in Prozent C=97 H=85 G=91
¤ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet am 2026-08-08)
¤
*© Formatika GbR, Deutschland