Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(32569)

Delta Between Two Patch Sets: Modules/_blake2/impl/blake2-impl.h

Issue 26798: add BLAKE2 to hashlib
Left Patch Set: Created 3 years, 4 months ago
Right Patch Set: Created 3 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Modules/_blake2/impl/blake2.h ('k') | Modules/_blake2/impl/blake2s.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 BLAKE2 reference source code package - reference C implementations 2 BLAKE2 reference source code package - optimized C implementations
3 3
4 Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the 4 Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
5 terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at 5 terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
6 your option. The terms of these licenses can be found at: 6 your option. The terms of these licenses can be found at:
7 7
8 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 8 - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
9 - OpenSSL license : https://www.openssl.org/source/license.html 9 - OpenSSL license : https://www.openssl.org/source/license.html
10 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 10 - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
11 11
12 More information about the BLAKE2 hash function can be found at 12 More information about the BLAKE2 hash function can be found at
13 https://blake2.net. 13 https://blake2.net.
14 */ 14 */
15 #pragma once 15 #pragma once
16 #ifndef __BLAKE2_IMPL_H__ 16 #ifndef __BLAKE2_IMPL_H__
17 #define __BLAKE2_IMPL_H__ 17 #define __BLAKE2_IMPL_H__
18 18
19 #include <stdint.h> 19 #include <stdint.h>
20 #include <string.h> 20 #include <string.h>
21 21
22 Py_LOCAL_INLINE(uint32_t) load32( const void *src ) 22 BLAKE2_LOCAL_INLINE(uint32_t) load32( const void *src )
23 { 23 {
24 #if defined(NATIVE_LITTLE_ENDIAN) 24 #if defined(NATIVE_LITTLE_ENDIAN)
25 uint32_t w; 25 uint32_t w;
26 memcpy(&w, src, sizeof w); 26 memcpy(&w, src, sizeof w);
27 return w; 27 return w;
28 #else 28 #else
29 const uint8_t *p = ( const uint8_t * )src; 29 const uint8_t *p = ( const uint8_t * )src;
30 uint32_t w = *p++; 30 uint32_t w = *p++;
31 w |= ( uint32_t )( *p++ ) << 8; 31 w |= ( uint32_t )( *p++ ) << 8;
32 w |= ( uint32_t )( *p++ ) << 16; 32 w |= ( uint32_t )( *p++ ) << 16;
33 w |= ( uint32_t )( *p++ ) << 24; 33 w |= ( uint32_t )( *p++ ) << 24;
34 return w; 34 return w;
35 #endif 35 #endif
36 } 36 }
37 37
38 Py_LOCAL_INLINE(uint64_t) load64( const void *src ) 38 BLAKE2_LOCAL_INLINE(uint64_t) load64( const void *src )
39 { 39 {
40 #if defined(NATIVE_LITTLE_ENDIAN) 40 #if defined(NATIVE_LITTLE_ENDIAN)
41 uint64_t w; 41 uint64_t w;
42 memcpy(&w, src, sizeof w); 42 memcpy(&w, src, sizeof w);
43 return w; 43 return w;
44 #else 44 #else
45 const uint8_t *p = ( const uint8_t * )src; 45 const uint8_t *p = ( const uint8_t * )src;
46 uint64_t w = *p++; 46 uint64_t w = *p++;
47 w |= ( uint64_t )( *p++ ) << 8; 47 w |= ( uint64_t )( *p++ ) << 8;
48 w |= ( uint64_t )( *p++ ) << 16; 48 w |= ( uint64_t )( *p++ ) << 16;
49 w |= ( uint64_t )( *p++ ) << 24; 49 w |= ( uint64_t )( *p++ ) << 24;
50 w |= ( uint64_t )( *p++ ) << 32; 50 w |= ( uint64_t )( *p++ ) << 32;
51 w |= ( uint64_t )( *p++ ) << 40; 51 w |= ( uint64_t )( *p++ ) << 40;
52 w |= ( uint64_t )( *p++ ) << 48; 52 w |= ( uint64_t )( *p++ ) << 48;
53 w |= ( uint64_t )( *p++ ) << 56; 53 w |= ( uint64_t )( *p++ ) << 56;
54 return w; 54 return w;
55 #endif 55 #endif
56 } 56 }
57 57
58 Py_LOCAL_INLINE(void) store32( void *dst, uint32_t w ) 58 BLAKE2_LOCAL_INLINE(void) store32( void *dst, uint32_t w )
59 { 59 {
60 #if defined(NATIVE_LITTLE_ENDIAN) 60 #if defined(NATIVE_LITTLE_ENDIAN)
61 memcpy(dst, &w, sizeof w); 61 memcpy(dst, &w, sizeof w);
62 #else 62 #else
63 uint8_t *p = ( uint8_t * )dst; 63 uint8_t *p = ( uint8_t * )dst;
64 *p++ = ( uint8_t )w; w >>= 8; 64 *p++ = ( uint8_t )w; w >>= 8;
65 *p++ = ( uint8_t )w; w >>= 8; 65 *p++ = ( uint8_t )w; w >>= 8;
66 *p++ = ( uint8_t )w; w >>= 8; 66 *p++ = ( uint8_t )w; w >>= 8;
67 *p++ = ( uint8_t )w; 67 *p++ = ( uint8_t )w;
68 #endif 68 #endif
69 } 69 }
70 70
71 Py_LOCAL_INLINE(void) store64( void *dst, uint64_t w ) 71 BLAKE2_LOCAL_INLINE(void) store64( void *dst, uint64_t w )
72 { 72 {
73 #if defined(NATIVE_LITTLE_ENDIAN) 73 #if defined(NATIVE_LITTLE_ENDIAN)
74 memcpy(dst, &w, sizeof w); 74 memcpy(dst, &w, sizeof w);
75 #else 75 #else
76 uint8_t *p = ( uint8_t * )dst; 76 uint8_t *p = ( uint8_t * )dst;
77 *p++ = ( uint8_t )w; w >>= 8; 77 *p++ = ( uint8_t )w; w >>= 8;
78 *p++ = ( uint8_t )w; w >>= 8; 78 *p++ = ( uint8_t )w; w >>= 8;
79 *p++ = ( uint8_t )w; w >>= 8; 79 *p++ = ( uint8_t )w; w >>= 8;
80 *p++ = ( uint8_t )w; w >>= 8; 80 *p++ = ( uint8_t )w; w >>= 8;
81 *p++ = ( uint8_t )w; w >>= 8; 81 *p++ = ( uint8_t )w; w >>= 8;
82 *p++ = ( uint8_t )w; w >>= 8; 82 *p++ = ( uint8_t )w; w >>= 8;
83 *p++ = ( uint8_t )w; w >>= 8; 83 *p++ = ( uint8_t )w; w >>= 8;
84 *p++ = ( uint8_t )w; 84 *p++ = ( uint8_t )w;
85 #endif 85 #endif
86 } 86 }
87 87
88 Py_LOCAL_INLINE(uint64_t) load48( const void *src ) 88 BLAKE2_LOCAL_INLINE(uint64_t) load48( const void *src )
89 { 89 {
90 const uint8_t *p = ( const uint8_t * )src; 90 const uint8_t *p = ( const uint8_t * )src;
91 uint64_t w = *p++; 91 uint64_t w = *p++;
92 w |= ( uint64_t )( *p++ ) << 8; 92 w |= ( uint64_t )( *p++ ) << 8;
93 w |= ( uint64_t )( *p++ ) << 16; 93 w |= ( uint64_t )( *p++ ) << 16;
94 w |= ( uint64_t )( *p++ ) << 24; 94 w |= ( uint64_t )( *p++ ) << 24;
95 w |= ( uint64_t )( *p++ ) << 32; 95 w |= ( uint64_t )( *p++ ) << 32;
96 w |= ( uint64_t )( *p++ ) << 40; 96 w |= ( uint64_t )( *p++ ) << 40;
97 return w; 97 return w;
98 } 98 }
99 99
100 Py_LOCAL_INLINE(void) store48( void *dst, uint64_t w ) 100 BLAKE2_LOCAL_INLINE(void) store48( void *dst, uint64_t w )
101 { 101 {
102 uint8_t *p = ( uint8_t * )dst; 102 uint8_t *p = ( uint8_t * )dst;
103 *p++ = ( uint8_t )w; w >>= 8; 103 *p++ = ( uint8_t )w; w >>= 8;
104 *p++ = ( uint8_t )w; w >>= 8; 104 *p++ = ( uint8_t )w; w >>= 8;
105 *p++ = ( uint8_t )w; w >>= 8; 105 *p++ = ( uint8_t )w; w >>= 8;
106 *p++ = ( uint8_t )w; w >>= 8; 106 *p++ = ( uint8_t )w; w >>= 8;
107 *p++ = ( uint8_t )w; w >>= 8; 107 *p++ = ( uint8_t )w; w >>= 8;
108 *p++ = ( uint8_t )w; 108 *p++ = ( uint8_t )w;
109 } 109 }
110 110
111 Py_LOCAL_INLINE(uint32_t) rotl32( const uint32_t w, const unsigned c ) 111 BLAKE2_LOCAL_INLINE(uint32_t) rotl32( const uint32_t w, const unsigned c )
112 { 112 {
113 return ( w << c ) | ( w >> ( 32 - c ) ); 113 return ( w << c ) | ( w >> ( 32 - c ) );
114 } 114 }
115 115
116 Py_LOCAL_INLINE(uint64_t) rotl64( const uint64_t w, const unsigned c ) 116 BLAKE2_LOCAL_INLINE(uint64_t) rotl64( const uint64_t w, const unsigned c )
117 { 117 {
118 return ( w << c ) | ( w >> ( 64 - c ) ); 118 return ( w << c ) | ( w >> ( 64 - c ) );
119 } 119 }
120 120
121 Py_LOCAL_INLINE(uint32_t) rotr32( const uint32_t w, const unsigned c ) 121 BLAKE2_LOCAL_INLINE(uint32_t) rotr32( const uint32_t w, const unsigned c )
122 { 122 {
123 return ( w >> c ) | ( w << ( 32 - c ) ); 123 return ( w >> c ) | ( w << ( 32 - c ) );
124 } 124 }
125 125
126 Py_LOCAL_INLINE(uint64_t) rotr64( const uint64_t w, const unsigned c ) 126 BLAKE2_LOCAL_INLINE(uint64_t) rotr64( const uint64_t w, const unsigned c )
127 { 127 {
128 return ( w >> c ) | ( w << ( 64 - c ) ); 128 return ( w >> c ) | ( w << ( 64 - c ) );
129 } 129 }
130 130
131 /* prevents compiler optimizing out memset() */ 131 /* prevents compiler optimizing out memset() */
132 Py_LOCAL_INLINE(void) secure_zero_memory(void *v, size_t n) 132 BLAKE2_LOCAL_INLINE(void) secure_zero_memory(void *v, size_t n)
133 { 133 {
134 static void *(*const volatile memset_v)(void *, int, size_t) = &memset; 134 static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
135 memset_v(v, 0, n); 135 memset_v(v, 0, n);
136 } 136 }
137 137
138 #endif 138 #endif
139 139
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+