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

Subversion Repositories potato

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

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 19 skordal
static const uint32_t initial[8] =
15 13 skordal
{
16
                0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
17
                0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
18
};
19
 
20 19 skordal
static const uint32_t constants[64] =
21 13 skordal
{
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 19 skordal
static uint32_t rotate_right(uint32_t x, int n)
33 13 skordal
{
34 19 skordal
        return (x >> n) | (x << (32 - n));
35 13 skordal
}
36
 
37 19 skordal
static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
38 13 skordal
{
39
        return (x & y) ^ ((~x) & z);
40
}
41
 
42 19 skordal
static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
43 13 skordal
{
44
        return (x & y) ^ (x & z) ^ (y & z);
45
}
46
 
47 19 skordal
static uint32_t s0(uint32_t x)
48 13 skordal
{
49
        return rotate_right(x, 2) ^ rotate_right(x, 13) ^ rotate_right(x, 22);
50
}
51
 
52 19 skordal
static uint32_t s1(uint32_t x)
53 13 skordal
{
54
        return rotate_right(x, 6) ^ rotate_right(x, 11) ^ rotate_right(x, 25);
55
}
56
 
57 19 skordal
static uint32_t o0(uint32_t x)
58 13 skordal
{
59
        return rotate_right(x, 7) ^ rotate_right(x, 18) ^ (x >> 3);
60
}
61
 
62 19 skordal
static uint32_t o1(uint32_t x)
63 13 skordal
{
64
        return rotate_right(x, 17) ^ rotate_right(x, 19) ^ (x >> 10);
65
}
66
 
67
static uint32_t schedule(uint32_t input, const uint32_t * W, int i)
68
{
69
        if(i < 16)
70
                return input;
71 19 skordal
        else
72 13 skordal
                return o1(W[i - 2]) + W[i - 7] + o0(W[i - 15]) + W[i - 16];
73
}
74
 
75
static void compress(uint32_t * i, uint32_t W, uint32_t K)
76
{
77
        uint32_t a = i[0], b = i[1], c = i[2], d = i[3];
78
        uint32_t e = i[4], f = i[5], g = i[6], h = i[7];
79
 
80
        uint32_t t1 = h + s1(e) + Ch(e, f, g) + K + W;
81
        uint32_t t2 = s0(a) + Maj(a, b, c);
82
 
83
        h = g;
84
        g = f;
85
        f = e;
86
        e = d + t1;
87
        d = c;
88
        c = b;
89
        b = a;
90
        a = t1 + t2;
91
 
92
        i[0] = a;
93
        i[1] = b;
94
        i[2] = c;
95
        i[3] = d;
96
        i[4] = e;
97
        i[5] = f;
98
        i[6] = g;
99
        i[7] = h;
100
}
101
 
102
void sha256_reset(struct sha256_context * ctx)
103
{
104
        for(int i = 0; i < 8; ++i)
105
                ctx->intermediate[i] = initial[i];
106
}
107
 
108
void sha256_hash_block(struct sha256_context * ctx, const uint32_t * data)
109
{
110
        uint32_t W[64];
111
        uint32_t temp[8];
112
 
113
        for(int i = 0; i < 8; ++i)
114
                temp[i] = ctx->intermediate[i];
115
 
116
        for(int i = 0; i < 64; ++i)
117
        {
118
                uint32_t v = i < 16 ? data[i] : 0;
119
                W[i] = schedule(v, W, i);
120
                compress(temp, W[i], constants[i]);
121
        }
122
 
123
        for(int i = 0; i < 8; ++i)
124
                ctx->intermediate[i] += temp[i];
125
}
126
 
127
void sha256_pad_le_block(uint8_t * block, int block_length, uint64_t total_length)
128
{
129
        block[block_length] = 0x80; // Add a one to the end of the message;
130
        for(int i = block_length + 1; i < 64; ++i)
131
                block[i] = 0;
132
 
133
        ((uint32_t *) block)[14] = total_length * 8 >> 32;
134
        ((uint32_t *) block)[15] = total_length * 8 & 0xffffffff;
135
 
136
        // Convert the block to big-endian:
137
        for(int i = 0; i < 14; ++i)
138
                ((uint32_t *) block)[i] = htobe32(((uint32_t *) block)[i]);
139
}
140
 
141
void sha256_get_hash(const struct sha256_context * ctx, uint8_t * hash)
142
{
143
        for(int i = 0; i < 8; ++i)
144
        {
145
                // Return the hash in little-endian format:
146
                hash[i * 4 + 3] = (ctx->intermediate[i] >>  0) & 0xff;
147
                hash[i * 4 + 2] = (ctx->intermediate[i] >>  8) & 0xff;
148
                hash[i * 4 + 1] = (ctx->intermediate[i] >> 16) & 0xff;
149
                hash[i * 4 + 0] = (ctx->intermediate[i] >> 24) & 0xff;
150
        }
151
}
152
 
153
void sha256_format_hash(const uint8_t * hash, char * output)
154
{
155
        static const char * hex_digits = "0123456789abcdef";
156
        for(int i = 0; i < 32; i++)
157
        {
158
                uint8_t h = hash[i];
159
 
160
                output[i * 2 + 0] = hex_digits[(h >> 4) & 0xf];
161
                output[i * 2 + 1] = hex_digits[h & 0xf];
162
        }
163
 
164
        output[64] = 0;
165
}
166
 
167
 

powered by: WebSVN 2.1.0

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