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