* git://git.kernel.org/pub/scm/linux/kernel/git/davem/net: net: Compute protocol sequence numbers and fragment IDs using MD5. crypto: Move md5_transform to lib/md5.c
···2121#include <linux/module.h>2222#include <linux/string.h>2323#include <linux/types.h>2424+#include <linux/cryptohash.h>2425#include <asm/byteorder.h>2525-2626-#define F1(x, y, z) (z ^ (x & (y ^ z)))2727-#define F2(x, y, z) F1(z, x, y)2828-#define F3(x, y, z) (x ^ y ^ z)2929-#define F4(x, y, z) (y ^ (x | ~z))3030-3131-#define MD5STEP(f, w, x, y, z, in, s) \3232- (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)3333-3434-static void md5_transform(u32 *hash, u32 const *in)3535-{3636- u32 a, b, c, d;3737-3838- a = hash[0];3939- b = hash[1];4040- c = hash[2];4141- d = hash[3];4242-4343- MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);4444- MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);4545- MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);4646- MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);4747- MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);4848- MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);4949- MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);5050- MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);5151- MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);5252- MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);5353- MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);5454- MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);5555- MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);5656- MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);5757- MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);5858- MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);5959-6060- MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);6161- MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);6262- MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);6363- MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);6464- MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);6565- MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);6666- MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);6767- MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);6868- MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);6969- MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);7070- MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);7171- MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);7272- MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);7373- MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);7474- MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);7575- MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);7676-7777- MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);7878- MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);7979- MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);8080- MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);8181- MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);8282- MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);8383- MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);8484- MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);8585- MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);8686- MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);8787- MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);8888- MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);8989- MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);9090- MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);9191- MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);9292- MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);9393-9494- MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);9595- MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);9696- MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);9797- MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);9898- MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);9999- MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);100100- MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);101101- MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);102102- MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);103103- MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);104104- MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);105105- MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);106106- MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);107107- MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);108108- MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);109109- MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);110110-111111- hash[0] += a;112112- hash[1] += b;113113- hash[2] += c;114114- hash[3] += d;115115-}1162611727/* XXX: this stuff can be optimized */11828static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
+8-341
drivers/char/random.c
···13001300};13011301#endif /* CONFIG_SYSCTL */1302130213031303-/********************************************************************13041304- *13051305- * Random functions for networking13061306- *13071307- ********************************************************************/13031303+static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned;1308130413091309-/*13101310- * TCP initial sequence number picking. This uses the random number13111311- * generator to pick an initial secret value. This value is hashed13121312- * along with the TCP endpoint information to provide a unique13131313- * starting point for each pair of TCP endpoints. This defeats13141314- * attacks which rely on guessing the initial TCP sequence number.13151315- * This algorithm was suggested by Steve Bellovin.13161316- *13171317- * Using a very strong hash was taking an appreciable amount of the total13181318- * TCP connection establishment time, so this is a weaker hash,13191319- * compensated for by changing the secret periodically.13201320- */13211321-13221322-/* F, G and H are basic MD4 functions: selection, majority, parity */13231323-#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))13241324-#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))13251325-#define H(x, y, z) ((x) ^ (y) ^ (z))13261326-13271327-/*13281328- * The generic round function. The application is so specific that13291329- * we don't bother protecting all the arguments with parens, as is generally13301330- * good macro practice, in favor of extra legibility.13311331- * Rotation is separate from addition to prevent recomputation13321332- */13331333-#define ROUND(f, a, b, c, d, x, s) \13341334- (a += f(b, c, d) + x, a = (a << s) | (a >> (32 - s)))13351335-#define K1 013361336-#define K2 013240474631UL13371337-#define K3 015666365641UL13381338-13391339-#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)13401340-13411341-static __u32 twothirdsMD4Transform(__u32 const buf[4], __u32 const in[12])13051305+static int __init random_int_secret_init(void)13421306{13431343- __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];13441344-13451345- /* Round 1 */13461346- ROUND(F, a, b, c, d, in[ 0] + K1, 3);13471347- ROUND(F, d, a, b, c, in[ 1] + K1, 7);13481348- ROUND(F, c, d, a, b, in[ 2] + K1, 11);13491349- ROUND(F, b, c, d, a, in[ 3] + K1, 19);13501350- ROUND(F, a, b, c, d, in[ 4] + K1, 3);13511351- ROUND(F, d, a, b, c, in[ 5] + K1, 7);13521352- ROUND(F, c, d, a, b, in[ 6] + K1, 11);13531353- ROUND(F, b, c, d, a, in[ 7] + K1, 19);13541354- ROUND(F, a, b, c, d, in[ 8] + K1, 3);13551355- ROUND(F, d, a, b, c, in[ 9] + K1, 7);13561356- ROUND(F, c, d, a, b, in[10] + K1, 11);13571357- ROUND(F, b, c, d, a, in[11] + K1, 19);13581358-13591359- /* Round 2 */13601360- ROUND(G, a, b, c, d, in[ 1] + K2, 3);13611361- ROUND(G, d, a, b, c, in[ 3] + K2, 5);13621362- ROUND(G, c, d, a, b, in[ 5] + K2, 9);13631363- ROUND(G, b, c, d, a, in[ 7] + K2, 13);13641364- ROUND(G, a, b, c, d, in[ 9] + K2, 3);13651365- ROUND(G, d, a, b, c, in[11] + K2, 5);13661366- ROUND(G, c, d, a, b, in[ 0] + K2, 9);13671367- ROUND(G, b, c, d, a, in[ 2] + K2, 13);13681368- ROUND(G, a, b, c, d, in[ 4] + K2, 3);13691369- ROUND(G, d, a, b, c, in[ 6] + K2, 5);13701370- ROUND(G, c, d, a, b, in[ 8] + K2, 9);13711371- ROUND(G, b, c, d, a, in[10] + K2, 13);13721372-13731373- /* Round 3 */13741374- ROUND(H, a, b, c, d, in[ 3] + K3, 3);13751375- ROUND(H, d, a, b, c, in[ 7] + K3, 9);13761376- ROUND(H, c, d, a, b, in[11] + K3, 11);13771377- ROUND(H, b, c, d, a, in[ 2] + K3, 15);13781378- ROUND(H, a, b, c, d, in[ 6] + K3, 3);13791379- ROUND(H, d, a, b, c, in[10] + K3, 9);13801380- ROUND(H, c, d, a, b, in[ 1] + K3, 11);13811381- ROUND(H, b, c, d, a, in[ 5] + K3, 15);13821382- ROUND(H, a, b, c, d, in[ 9] + K3, 3);13831383- ROUND(H, d, a, b, c, in[ 0] + K3, 9);13841384- ROUND(H, c, d, a, b, in[ 4] + K3, 11);13851385- ROUND(H, b, c, d, a, in[ 8] + K3, 15);13861386-13871387- return buf[1] + b; /* "most hashed" word */13881388- /* Alternative: return sum of all words? */13891389-}13901390-#endif13911391-13921392-#undef ROUND13931393-#undef F13941394-#undef G13951395-#undef H13961396-#undef K113971397-#undef K213981398-#undef K313991399-14001400-/* This should not be decreased so low that ISNs wrap too fast. */14011401-#define REKEY_INTERVAL (300 * HZ)14021402-/*14031403- * Bit layout of the tcp sequence numbers (before adding current time):14041404- * bit 24-31: increased after every key exchange14051405- * bit 0-23: hash(source,dest)14061406- *14071407- * The implementation is similar to the algorithm described14081408- * in the Appendix of RFC 1185, except that14091409- * - it uses a 1 MHz clock instead of a 250 kHz clock14101410- * - it performs a rekey every 5 minutes, which is equivalent14111411- * to a (source,dest) tulple dependent forward jump of the14121412- * clock by 0..2^(HASH_BITS+1)14131413- *14141414- * Thus the average ISN wraparound time is 68 minutes instead of14151415- * 4.55 hours.14161416- *14171417- * SMP cleanup and lock avoidance with poor man's RCU.14181418- * Manfred Spraul <manfred@colorfullife.com>14191419- *14201420- */14211421-#define COUNT_BITS 814221422-#define COUNT_MASK ((1 << COUNT_BITS) - 1)14231423-#define HASH_BITS 2414241424-#define HASH_MASK ((1 << HASH_BITS) - 1)14251425-14261426-static struct keydata {14271427- __u32 count; /* already shifted to the final position */14281428- __u32 secret[12];14291429-} ____cacheline_aligned ip_keydata[2];14301430-14311431-static unsigned int ip_cnt;14321432-14331433-static void rekey_seq_generator(struct work_struct *work);14341434-14351435-static DECLARE_DELAYED_WORK(rekey_work, rekey_seq_generator);14361436-14371437-/*14381438- * Lock avoidance:14391439- * The ISN generation runs lockless - it's just a hash over random data.14401440- * State changes happen every 5 minutes when the random key is replaced.14411441- * Synchronization is performed by having two copies of the hash function14421442- * state and rekey_seq_generator always updates the inactive copy.14431443- * The copy is then activated by updating ip_cnt.14441444- * The implementation breaks down if someone blocks the thread14451445- * that processes SYN requests for more than 5 minutes. Should never14461446- * happen, and even if that happens only a not perfectly compliant14471447- * ISN is generated, nothing fatal.14481448- */14491449-static void rekey_seq_generator(struct work_struct *work)14501450-{14511451- struct keydata *keyptr = &ip_keydata[1 ^ (ip_cnt & 1)];14521452-14531453- get_random_bytes(keyptr->secret, sizeof(keyptr->secret));14541454- keyptr->count = (ip_cnt & COUNT_MASK) << HASH_BITS;14551455- smp_wmb();14561456- ip_cnt++;14571457- schedule_delayed_work(&rekey_work,14581458- round_jiffies_relative(REKEY_INTERVAL));14591459-}14601460-14611461-static inline struct keydata *get_keyptr(void)14621462-{14631463- struct keydata *keyptr = &ip_keydata[ip_cnt & 1];14641464-14651465- smp_rmb();14661466-14671467- return keyptr;14681468-}14691469-14701470-static __init int seqgen_init(void)14711471-{14721472- rekey_seq_generator(NULL);13071307+ get_random_bytes(random_int_secret, sizeof(random_int_secret));14731308 return 0;14741309}14751475-late_initcall(seqgen_init);14761476-14771477-#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)14781478-__u32 secure_tcpv6_sequence_number(__be32 *saddr, __be32 *daddr,14791479- __be16 sport, __be16 dport)14801480-{14811481- __u32 seq;14821482- __u32 hash[12];14831483- struct keydata *keyptr = get_keyptr();14841484-14851485- /* The procedure is the same as for IPv4, but addresses are longer.14861486- * Thus we must use twothirdsMD4Transform.14871487- */14881488-14891489- memcpy(hash, saddr, 16);14901490- hash[4] = ((__force u16)sport << 16) + (__force u16)dport;14911491- memcpy(&hash[5], keyptr->secret, sizeof(__u32) * 7);14921492-14931493- seq = twothirdsMD4Transform((const __u32 *)daddr, hash) & HASH_MASK;14941494- seq += keyptr->count;14951495-14961496- seq += ktime_to_ns(ktime_get_real());14971497-14981498- return seq;14991499-}15001500-EXPORT_SYMBOL(secure_tcpv6_sequence_number);15011501-#endif15021502-15031503-/* The code below is shamelessly stolen from secure_tcp_sequence_number().15041504- * All blames to Andrey V. Savochkin <saw@msu.ru>.15051505- */15061506-__u32 secure_ip_id(__be32 daddr)15071507-{15081508- struct keydata *keyptr;15091509- __u32 hash[4];15101510-15111511- keyptr = get_keyptr();15121512-15131513- /*15141514- * Pick a unique starting offset for each IP destination.15151515- * The dest ip address is placed in the starting vector,15161516- * which is then hashed with random data.15171517- */15181518- hash[0] = (__force __u32)daddr;15191519- hash[1] = keyptr->secret[9];15201520- hash[2] = keyptr->secret[10];15211521- hash[3] = keyptr->secret[11];15221522-15231523- return half_md4_transform(hash, keyptr->secret);15241524-}15251525-15261526-__u32 secure_ipv6_id(const __be32 daddr[4])15271527-{15281528- const struct keydata *keyptr;15291529- __u32 hash[4];15301530-15311531- keyptr = get_keyptr();15321532-15331533- hash[0] = (__force __u32)daddr[0];15341534- hash[1] = (__force __u32)daddr[1];15351535- hash[2] = (__force __u32)daddr[2];15361536- hash[3] = (__force __u32)daddr[3];15371537-15381538- return half_md4_transform(hash, keyptr->secret);15391539-}15401540-15411541-#ifdef CONFIG_INET15421542-15431543-__u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,15441544- __be16 sport, __be16 dport)15451545-{15461546- __u32 seq;15471547- __u32 hash[4];15481548- struct keydata *keyptr = get_keyptr();15491549-15501550- /*15511551- * Pick a unique starting offset for each TCP connection endpoints15521552- * (saddr, daddr, sport, dport).15531553- * Note that the words are placed into the starting vector, which is15541554- * then mixed with a partial MD4 over random data.15551555- */15561556- hash[0] = (__force u32)saddr;15571557- hash[1] = (__force u32)daddr;15581558- hash[2] = ((__force u16)sport << 16) + (__force u16)dport;15591559- hash[3] = keyptr->secret[11];15601560-15611561- seq = half_md4_transform(hash, keyptr->secret) & HASH_MASK;15621562- seq += keyptr->count;15631563- /*15641564- * As close as possible to RFC 793, which15651565- * suggests using a 250 kHz clock.15661566- * Further reading shows this assumes 2 Mb/s networks.15671567- * For 10 Mb/s Ethernet, a 1 MHz clock is appropriate.15681568- * For 10 Gb/s Ethernet, a 1 GHz clock should be ok, but15691569- * we also need to limit the resolution so that the u32 seq15701570- * overlaps less than one time per MSL (2 minutes).15711571- * Choosing a clock of 64 ns period is OK. (period of 274 s)15721572- */15731573- seq += ktime_to_ns(ktime_get_real()) >> 6;15741574-15751575- return seq;15761576-}15771577-15781578-/* Generate secure starting point for ephemeral IPV4 transport port search */15791579-u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)15801580-{15811581- struct keydata *keyptr = get_keyptr();15821582- u32 hash[4];15831583-15841584- /*15851585- * Pick a unique starting offset for each ephemeral port search15861586- * (saddr, daddr, dport) and 48bits of random data.15871587- */15881588- hash[0] = (__force u32)saddr;15891589- hash[1] = (__force u32)daddr;15901590- hash[2] = (__force u32)dport ^ keyptr->secret[10];15911591- hash[3] = keyptr->secret[11];15921592-15931593- return half_md4_transform(hash, keyptr->secret);15941594-}15951595-EXPORT_SYMBOL_GPL(secure_ipv4_port_ephemeral);15961596-15971597-#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)15981598-u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,15991599- __be16 dport)16001600-{16011601- struct keydata *keyptr = get_keyptr();16021602- u32 hash[12];16031603-16041604- memcpy(hash, saddr, 16);16051605- hash[4] = (__force u32)dport;16061606- memcpy(&hash[5], keyptr->secret, sizeof(__u32) * 7);16071607-16081608- return twothirdsMD4Transform((const __u32 *)daddr, hash);16091609-}16101610-#endif16111611-16121612-#if defined(CONFIG_IP_DCCP) || defined(CONFIG_IP_DCCP_MODULE)16131613-/* Similar to secure_tcp_sequence_number but generate a 48 bit value16141614- * bit's 32-47 increase every key exchange16151615- * 0-31 hash(source, dest)16161616- */16171617-u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,16181618- __be16 sport, __be16 dport)16191619-{16201620- u64 seq;16211621- __u32 hash[4];16221622- struct keydata *keyptr = get_keyptr();16231623-16241624- hash[0] = (__force u32)saddr;16251625- hash[1] = (__force u32)daddr;16261626- hash[2] = ((__force u16)sport << 16) + (__force u16)dport;16271627- hash[3] = keyptr->secret[11];16281628-16291629- seq = half_md4_transform(hash, keyptr->secret);16301630- seq |= ((u64)keyptr->count) << (32 - HASH_BITS);16311631-16321632- seq += ktime_to_ns(ktime_get_real());16331633- seq &= (1ull << 48) - 1;16341634-16351635- return seq;16361636-}16371637-EXPORT_SYMBOL(secure_dccp_sequence_number);16381638-#endif16391639-16401640-#endif /* CONFIG_INET */16411641-13101310+late_initcall(random_int_secret_init);1642131116431312/*16441313 * Get a random word for internal kernel use only. Similar to urandom but···13151646 * value is not cryptographically secure but for several uses the cost of13161647 * depleting entropy is too high13171648 */13181318-DEFINE_PER_CPU(__u32 [4], get_random_int_hash);16491649+DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash);13191650unsigned int get_random_int(void)13201651{13211321- struct keydata *keyptr;13221652 __u32 *hash = get_cpu_var(get_random_int_hash);13231323- int ret;16531653+ unsigned int ret;1324165413251325- keyptr = get_keyptr();13261655 hash[0] += current->pid + jiffies + get_cycles();13271327-13281328- ret = half_md4_transform(hash, keyptr->secret);16561656+ md5_transform(hash, random_int_secret);16571657+ ret = hash[0];13291658 put_cpu_var(get_random_int_hash);1330165913311660 return ret;
···1919#include <linux/net.h>2020#include <net/ip.h>2121#include <net/inetpeer.h>2222+#include <net/secure_seq.h>22232324/*2425 * Theory of operations.