]>
Commit | Line | Data |
---|---|---|
2d0b4412 | 1 | /* |
2d0b4412 | 2 | * Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. |
208527a7 | 3 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. |
2d0b4412 | 4 | * |
e0a65194 | 5 | * Licensed under the OpenSSL licenses (the "License"); |
208527a7 KR |
6 | * you may not use this file except in compliance with the License. |
7 | * You may obtain a copy of the License at | |
8 | * https://www.openssl.org/source/license.html | |
9 | * or in the file LICENSE in the source distribution. | |
2d0b4412 BC |
10 | */ |
11 | ||
208527a7 KR |
12 | /* |
13 | * Derived from the BLAKE2 reference implementation written by Samuel Neves. | |
14 | * More information about the BLAKE2 hash function and its implementations | |
15 | * can be found at https://blake2.net. | |
16 | */ | |
2d0b4412 | 17 | |
2d0b4412 | 18 | #include <string.h> |
208527a7 | 19 | #include "e_os.h" |
2d0b4412 | 20 | |
a5741089 | 21 | static ossl_inline uint32_t load32(const uint8_t *src) |
2d0b4412 | 22 | { |
208527a7 KR |
23 | const union { |
24 | long one; | |
25 | char little; | |
26 | } is_endian = { 1 }; | |
27 | ||
28 | if (is_endian.little) { | |
29 | uint32_t w; | |
30 | memcpy(&w, src, sizeof(w)); | |
31 | return w; | |
32 | } else { | |
1fab06a6 AP |
33 | uint32_t w = ((uint32_t)src[0]) |
34 | | ((uint32_t)src[1] << 8) | |
35 | | ((uint32_t)src[2] << 16) | |
36 | | ((uint32_t)src[3] << 24); | |
208527a7 KR |
37 | return w; |
38 | } | |
2d0b4412 BC |
39 | } |
40 | ||
a5741089 | 41 | static ossl_inline uint64_t load64(const uint8_t *src) |
2d0b4412 | 42 | { |
208527a7 KR |
43 | const union { |
44 | long one; | |
45 | char little; | |
46 | } is_endian = { 1 }; | |
47 | ||
48 | if (is_endian.little) { | |
49 | uint64_t w; | |
50 | memcpy(&w, src, sizeof(w)); | |
51 | return w; | |
52 | } else { | |
1fab06a6 AP |
53 | uint64_t w = ((uint64_t)src[0]) |
54 | | ((uint64_t)src[1] << 8) | |
55 | | ((uint64_t)src[2] << 16) | |
56 | | ((uint64_t)src[3] << 24) | |
57 | | ((uint64_t)src[4] << 32) | |
58 | | ((uint64_t)src[5] << 40) | |
59 | | ((uint64_t)src[6] << 48) | |
60 | | ((uint64_t)src[7] << 56); | |
208527a7 KR |
61 | return w; |
62 | } | |
2d0b4412 BC |
63 | } |
64 | ||
a5741089 | 65 | static ossl_inline void store32(uint8_t *dst, uint32_t w) |
2d0b4412 | 66 | { |
208527a7 KR |
67 | const union { |
68 | long one; | |
69 | char little; | |
70 | } is_endian = { 1 }; | |
71 | ||
72 | if (is_endian.little) { | |
73 | memcpy(dst, &w, sizeof(w)); | |
74 | } else { | |
75 | uint8_t *p = (uint8_t *)dst; | |
76 | int i; | |
77 | ||
78 | for (i = 0; i < 4; i++) | |
79 | p[i] = (uint8_t)(w >> (8 * i)); | |
80 | } | |
2d0b4412 BC |
81 | } |
82 | ||
a5741089 | 83 | static ossl_inline void store64(uint8_t *dst, uint64_t w) |
2d0b4412 | 84 | { |
208527a7 KR |
85 | const union { |
86 | long one; | |
87 | char little; | |
88 | } is_endian = { 1 }; | |
89 | ||
90 | if (is_endian.little) { | |
91 | memcpy(dst, &w, sizeof(w)); | |
92 | } else { | |
93 | uint8_t *p = (uint8_t *)dst; | |
94 | int i; | |
95 | ||
96 | for (i = 0; i < 8; i++) | |
97 | p[i] = (uint8_t)(w >> (8 * i)); | |
98 | } | |
2d0b4412 BC |
99 | } |
100 | ||
a5741089 | 101 | static ossl_inline uint64_t load48(const uint8_t *src) |
2d0b4412 | 102 | { |
1fab06a6 AP |
103 | uint64_t w = ((uint64_t)src[0]) |
104 | | ((uint64_t)src[1] << 8) | |
105 | | ((uint64_t)src[2] << 16) | |
106 | | ((uint64_t)src[3] << 24) | |
107 | | ((uint64_t)src[4] << 32) | |
108 | | ((uint64_t)src[5] << 40); | |
2d0b4412 BC |
109 | return w; |
110 | } | |
111 | ||
a5741089 | 112 | static ossl_inline void store48(uint8_t *dst, uint64_t w) |
2d0b4412 BC |
113 | { |
114 | uint8_t *p = (uint8_t *)dst; | |
1fab06a6 AP |
115 | p[0] = (uint8_t)w; |
116 | p[1] = (uint8_t)(w>>8); | |
117 | p[2] = (uint8_t)(w>>16); | |
118 | p[3] = (uint8_t)(w>>24); | |
119 | p[4] = (uint8_t)(w>>32); | |
120 | p[5] = (uint8_t)(w>>40); | |
2d0b4412 BC |
121 | } |
122 | ||
53a51674 | 123 | static ossl_inline uint32_t rotr32(const uint32_t w, const unsigned int c) |
2d0b4412 BC |
124 | { |
125 | return (w >> c) | (w << (32 - c)); | |
126 | } | |
127 | ||
53a51674 | 128 | static ossl_inline uint64_t rotr64(const uint64_t w, const unsigned int c) |
2d0b4412 BC |
129 | { |
130 | return (w >> c) | (w << (64 - c)); | |
131 | } |