1 /* 2 * Copyright (c) 2024, Altera Corporation. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef SOCFPGA_SHA_H 8 #define SOCFPGA_SHA_H 9 10 #include <stdlib.h> 11 12 13 #define SHA384_SUM_LEN 48 14 #define SHA384_DER_LEN 19 15 #define SHA512_SUM_LEN 64 16 #define SHA512_DER_LEN 19 17 #define SHA512_BLOCK_SIZE 128 18 19 20 /* MACRO Function */ 21 #define GET_UINT64_BE(n, b, i) { \ 22 (n) = ((unsigned long long) (b)[(i)] << 56) |\ 23 ((unsigned long long) (b)[(i) + 1] << 48) |\ 24 ((unsigned long long) (b)[(i) + 2] << 40) |\ 25 ((unsigned long long) (b)[(i) + 3] << 32) |\ 26 ((unsigned long long) (b)[(i) + 4] << 24) |\ 27 ((unsigned long long) (b)[(i) + 5] << 16) |\ 28 ((unsigned long long) (b)[(i) + 6] << 8) |\ 29 ((unsigned long long) (b)[(i) + 7]);\ 30 } 31 32 #define PUT_UINT64_BE(n, b, i) { \ 33 (b)[(i)] = (unsigned char) ((n) >> 56);\ 34 (b)[(i) + 1] = (unsigned char) ((n) >> 48);\ 35 (b)[(i) + 2] = (unsigned char) ((n) >> 40);\ 36 (b)[(i) + 3] = (unsigned char) ((n) >> 32);\ 37 (b)[(i) + 4] = (unsigned char) ((n) >> 24);\ 38 (b)[(i) + 5] = (unsigned char) ((n) >> 16);\ 39 (b)[(i) + 6] = (unsigned char) ((n) >> 8);\ 40 (b)[(i) + 7] = (unsigned char) ((n));\ 41 } 42 43 #define e0(x) (ror64(x, 28) ^ ror64(x, 34) ^ ror64(x, 39)) 44 #define e1(x) (ror64(x, 14) ^ ror64(x, 18) ^ ror64(x, 41)) 45 #define s0(x) (ror64(x, 1) ^ ror64(x, 8) ^ (x >> 7)) 46 #define s1(x) (ror64(x, 19) ^ ror64(x, 61) ^ (x >> 6)) 47 48 /* Inline Function Definitions */ 49 /* ror64() to rotate its right in 64 bits. */ 50 static inline uint64_t ror64(uint64_t input, unsigned int shift) 51 { 52 return (input >> (shift & 63)) | (input << ((-shift) & 63)); 53 } 54 55 static inline uint64_t Ch(uint64_t x, uint64_t y, uint64_t z) 56 { 57 return z ^ (x & (y ^ z)); 58 } 59 60 static inline uint64_t Maj(uint64_t x, uint64_t y, uint64_t z) 61 { 62 return (x & y) | (z & (x | y)); 63 } 64 65 static inline void LOAD_OP(int I, uint64_t *W, const uint8_t *input) 66 { 67 GET_UINT64_BE(W[I], input, I*8); 68 } 69 70 static inline void BLEND_OP(int I, uint64_t *W) 71 { 72 W[I & 15] += s1(W[(I-2) & 15]) + W[(I-7) & 15] + s0(W[(I-15) & 15]); 73 } 74 75 #if __BYTE_ORDER == __LITTLE_ENDIAN 76 inline uint32_t le32_to_cpue(const uint32_t *p) 77 { 78 return (uint32_t)*p; 79 } 80 #else 81 inline uint32_t le32_to_cpue(const uint32_t *p) 82 { 83 return swab32(*p); 84 } 85 #endif 86 87 static const uint64_t sha512_K[80] = { 88 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 89 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 90 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL, 91 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 92 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 93 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 94 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, 95 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, 96 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 97 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 98 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL, 99 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, 100 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 101 0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 102 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, 103 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, 104 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 105 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 106 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL, 107 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 108 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 109 0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 110 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 111 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, 112 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 113 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 114 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL, 115 }; 116 117 #define __cpu_to_le64(x) ((__force __le64)(__u64)(x)) 118 119 #define _uswap_64(x, sfx) \ 120 ((((x) & 0xff00000000000000##sfx) >> 56) |\ 121 (((x) & 0x00ff000000000000##sfx) >> 40) |\ 122 (((x) & 0x0000ff0000000000##sfx) >> 24) |\ 123 (((x) & 0x000000ff00000000##sfx) >> 8) |\ 124 (((x) & 0x00000000ff000000##sfx) << 8) |\ 125 (((x) & 0x0000000000ff0000##sfx) << 24) |\ 126 (((x) & 0x000000000000ff00##sfx) << 40) |\ 127 (((x) & 0x00000000000000ff##sfx) << 56)) 128 129 #if defined(__GNUC__) 130 #define uswap_64(x) _uswap_64(x, ull) 131 #else 132 #define uswap_64(x) _uswap_64(x) 133 #endif 134 135 #if __BYTE_ORDER == __LITTLE_ENDIAN 136 #define cpu_to_be64(x) uswap_64(x) 137 #else 138 #define cpu_to_be64(x) (x) 139 #endif 140 141 typedef struct { 142 uint64_t state[SHA512_SUM_LEN / 8]; 143 uint64_t count[2]; 144 uint8_t buf[SHA512_BLOCK_SIZE]; 145 } sha512_context; 146 147 /* Function Definitions */ 148 /* SHA384 Start Here */ 149 void sha384_init(sha512_context *ctx); 150 void sha384_update(sha512_context *ctx, const uint8_t *input, uint32_t length); 151 void sha384_finish(sha512_context *ctx, uint8_t digest[SHA384_SUM_LEN]); 152 void sha384_start(const unsigned char *input, unsigned int len, 153 unsigned char *output, unsigned int chunk_sz); 154 /* SHA512 Start Here */ 155 void sha512_init(sha512_context *ctx); 156 void sha512_update(sha512_context *ctx, const uint8_t *input, uint32_t length); 157 void sha512_finish(sha512_context *ctx, uint8_t digest[SHA512_SUM_LEN]); 158 void sha512_start(const unsigned char *input, unsigned int len, 159 unsigned char *output); 160 void sha512_transform(uint64_t *state, const uint8_t *input); 161 void sha512_block_fn(sha512_context *sst, const uint8_t *src, int blocks); 162 void sha512_base_do_finalize(sha512_context *sctx); 163 void sha512_base_do_update(sha512_context *sctx, const uint8_t *data, 164 unsigned int len); 165 166 #endif 167