1 module crypto.blake2.impl; 2 3 import core.stdc..string: memcpy; 4 5 6 package: 7 pure nothrow @nogc: 8 9 10 ushort load16( const void *src ) 11 { 12 version (LittleEndian) 13 { 14 ushort w; 15 memcpy(&w, src, w.sizeof); 16 return w; 17 } 18 else // BigEndian 19 { 20 auto p = cast(const(ubyte)*)src; 21 return cast(ushort)(( cast(uint) ( p[0] ) << 0) | 22 ( cast(uint) ( p[1] ) << 8)) ; 23 } 24 } 25 26 uint load32( const void *src ) 27 { 28 version (LittleEndian) 29 { 30 uint w; 31 memcpy(&w, src, w.sizeof); 32 return w; 33 } 34 else 35 { 36 auto p = cast(const(ubyte)*)src; 37 return 38 ( cast(uint) ( p[0] ) << 0) | 39 ( cast(uint) ( p[1] ) << 8) | 40 ( cast(uint) ( p[2] ) << 16) | 41 ( cast(uint) ( p[3] ) << 24) ; 42 } 43 } 44 45 ulong load64( const void *src ) 46 { 47 version (LittleEndian) 48 { 49 ulong w; 50 memcpy(&w, src, w.sizeof); 51 return w; 52 } 53 else 54 { 55 auto p = cast(const(ubyte)*)src; 56 return 57 ( cast(ulong) ( p[0] ) << 0) | 58 ( cast(ulong) ( p[1] ) << 8) | 59 ( cast(ulong) ( p[2] ) << 16) | 60 ( cast(ulong) ( p[3] ) << 24) | 61 ( cast(ulong) ( p[4] ) << 32) | 62 ( cast(ulong) ( p[5] ) << 40) | 63 ( cast(ulong) ( p[6] ) << 48) | 64 ( cast(ulong) ( p[7] ) << 56) ; 65 } 66 } 67 68 ulong load48( const void *src ) 69 { 70 auto p = cast(const(ubyte)*)src; 71 return 72 ( cast(ulong) ( p[0] ) << 0) | 73 ( cast(ulong) ( p[1] ) << 8) | 74 ( cast(ulong) ( p[2] ) << 16) | 75 ( cast(ulong) ( p[3] ) << 24) | 76 ( cast(ulong) ( p[4] ) << 32) | 77 ( cast(ulong) ( p[5] ) << 40) ; 78 } 79 80 81 void store16( void *dst, in ushort w ) 82 { 83 version (LittleEndian) 84 { 85 memcpy(dst, &w, w.sizeof); 86 } 87 else 88 { 89 auto p = cast(ubyte*)dst; 90 *p++ = cast(ubyte) w; 91 w >>= 8; 92 *p++ = cast(ubyte) w; 93 } 94 } 95 96 void store32( void *dst, in uint w ) 97 { 98 version (LittleEndian) 99 { 100 memcpy(dst, &w, w.sizeof); 101 } 102 else 103 { 104 auto p = cast(ubyte*)dst; 105 p[0] = cast(ubyte)(w >> 0); 106 p[1] = cast(ubyte)(w >> 8); 107 p[2] = cast(ubyte)(w >> 16); 108 p[3] = cast(ubyte)(w >> 24); 109 } 110 } 111 112 void store48( void *dst, in ulong w ) 113 { 114 auto p = cast(ubyte*)dst; 115 p[0] = cast(ubyte)(w >> 0); 116 p[1] = cast(ubyte)(w >> 8); 117 p[2] = cast(ubyte)(w >> 16); 118 p[3] = cast(ubyte)(w >> 24); 119 p[4] = cast(ubyte)(w >> 32); 120 p[5] = cast(ubyte)(w >> 40); 121 } 122 123 void store64( void *dst, in ulong w ) 124 { 125 version (LittleEndian) 126 { 127 memcpy(dst, &w, w.sizeof); 128 } 129 else 130 { 131 auto p = cast(ubyte*)dst; 132 p[0] = cast(ubyte)(w >> 0); 133 p[1] = cast(ubyte)(w >> 8); 134 p[2] = cast(ubyte)(w >> 16); 135 p[3] = cast(ubyte)(w >> 24); 136 p[4] = cast(ubyte)(w >> 32); 137 p[5] = cast(ubyte)(w >> 40); 138 p[6] = cast(ubyte)(w >> 48); 139 p[7] = cast(ubyte)(w >> 56); 140 } 141 } 142 143 uint rotr32( in uint w, in uint c ) 144 { 145 return ( w >> c ) | ( w << ( 32 - c ) ); 146 } 147 148 ulong rotr64( in ulong w, in uint c ) 149 { 150 return ( w >> c ) | ( w << ( 64 - c ) ); 151 }