OpenCores
URL https://opencores.org/ocsvn/potato/potato/trunk

Subversion Repositories potato

[/] [potato/] [trunk/] [benchmarks/] [sha256/] [sha256.c] - Blame information for rev 14

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 skordal
// The Potato Processor Benchmark Applications
2
// (c) Kristian Klomsten Skordal 2015 <kristian.skordal@wafflemail.net>
3
// Report bugs and issues on <http://opencores.org/project,potato,bugtracker>
4
 
5
#include "platform.h"
6
#include "gpio.h"
7
 
8
#include "sha256.h"
9
 
10
#define htobe32(n)      ((uint32_t) ((n << 24) | ((n << 8) & 0xff0000) | ((n >> 8) & 0xff00) | (n >> 24)))
11
 
12
// Software SHA256 module
13
 
14
static uint32_t initial[] =
15
{
16
                0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
17
                0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
18
};
19
 
20
static uint32_t constants[] =
21
{
22
        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
23
        0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
24
        0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
25
        0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
26
        0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
27
        0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
28
        0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
29
        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
30
};
31
 
32
static inline uint32_t rotate_right(uint32_t x, int n)
33
{
34
        uint32_t retval = 0;
35
 
36
        retval = x >> n;
37
        retval |= x << (32 - n);
38
 
39
        return retval;
40
}
41
 
42
static inline uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
43
{
44
        return (x & y) ^ ((~x) & z);
45
}
46
 
47
static inline uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
48
{
49
        return (x & y) ^ (x & z) ^ (y & z);
50
}
51
 
52
static inline uint32_t s0(uint32_t x)
53
{
54
        return rotate_right(x, 2) ^ rotate_right(x, 13) ^ rotate_right(x, 22);
55
}
56
 
57
static inline uint32_t s1(uint32_t x)
58
{
59
        return rotate_right(x, 6) ^ rotate_right(x, 11) ^ rotate_right(x, 25);
60
}
61
 
62
static inline uint32_t o0(uint32_t x)
63
{
64
        return rotate_right(x, 7) ^ rotate_right(x, 18) ^ (x >> 3);
65
}
66
 
67
static inline uint32_t o1(uint32_t x)
68
{
69
        return rotate_right(x, 17) ^ rotate_right(x, 19) ^ (x >> 10);
70
}
71
 
72
static uint32_t schedule(uint32_t input, const uint32_t * W, int i)
73
{
74
        if(i < 16)
75
                return input;
76
        else {
77
                return o1(W[i - 2]) + W[i - 7] + o0(W[i - 15]) + W[i - 16];
78
        }
79
}
80
 
81
static void compress(uint32_t * i, uint32_t W, uint32_t K)
82
{
83
        uint32_t a = i[0], b = i[1], c = i[2], d = i[3];
84
        uint32_t e = i[4], f = i[5], g = i[6], h = i[7];
85
 
86
        uint32_t t1 = h + s1(e) + Ch(e, f, g) + K + W;
87
        uint32_t t2 = s0(a) + Maj(a, b, c);
88
 
89
        h = g;
90
        g = f;
91
        f = e;
92
        e = d + t1;
93
        d = c;
94
        c = b;
95
        b = a;
96
        a = t1 + t2;
97
 
98
        i[0] = a;
99
        i[1] = b;
100
        i[2] = c;
101
        i[3] = d;
102
        i[4] = e;
103
        i[5] = f;
104
        i[6] = g;
105
        i[7] = h;
106
}
107
 
108
void sha256_reset(struct sha256_context * ctx)
109
{
110
        for(int i = 0; i < 8; ++i)
111
                ctx->intermediate[i] = initial[i];
112
}
113
 
114
void sha256_hash_block(struct sha256_context * ctx, const uint32_t * data)
115
{
116
        uint32_t W[64];
117
        uint32_t temp[8];
118
 
119
        for(int i = 0; i < 8; ++i)
120
                temp[i] = ctx->intermediate[i];
121
 
122
        for(int i = 0; i < 64; ++i)
123
        {
124
                uint32_t v = i < 16 ? data[i] : 0;
125
                W[i] = schedule(v, W, i);
126
                compress(temp, W[i], constants[i]);
127
        }
128
 
129
        for(int i = 0; i < 8; ++i)
130
                ctx->intermediate[i] += temp[i];
131
}
132
 
133
void sha256_pad_le_block(uint8_t * block, int block_length, uint64_t total_length)
134
{
135
        block[block_length] = 0x80; // Add a one to the end of the message;
136
        for(int i = block_length + 1; i < 64; ++i)
137
                block[i] = 0;
138
 
139
        ((uint32_t *) block)[14] = total_length * 8 >> 32;
140
        ((uint32_t *) block)[15] = total_length * 8 & 0xffffffff;
141
 
142
        // Convert the block to big-endian:
143
        for(int i = 0; i < 14; ++i)
144
                ((uint32_t *) block)[i] = htobe32(((uint32_t *) block)[i]);
145
}
146
 
147
void sha256_get_hash(const struct sha256_context * ctx, uint8_t * hash)
148
{
149
        for(int i = 0; i < 8; ++i)
150
        {
151
                // Return the hash in little-endian format:
152
                hash[i * 4 + 3] = (ctx->intermediate[i] >>  0) & 0xff;
153
                hash[i * 4 + 2] = (ctx->intermediate[i] >>  8) & 0xff;
154
                hash[i * 4 + 1] = (ctx->intermediate[i] >> 16) & 0xff;
155
                hash[i * 4 + 0] = (ctx->intermediate[i] >> 24) & 0xff;
156
        }
157
}
158
 
159
void sha256_format_hash(const uint8_t * hash, char * output)
160
{
161
        static const char * hex_digits = "0123456789abcdef";
162
        for(int i = 0; i < 32; i++)
163
        {
164
                uint8_t h = hash[i];
165
 
166
                output[i * 2 + 0] = hex_digits[(h >> 4) & 0xf];
167
                output[i * 2 + 1] = hex_digits[h & 0xf];
168
        }
169
 
170
        output[64] = 0;
171
}
172
 
173
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.