• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Local implement of sha256.
3  *
4  * Copyright (C) 2013 Allwinner.
5  *
6  * This file is licensed under the terms of the GNU General Public
7  * License version 2.  This program is licensed "as is" without any
8  * warranty of any kind, whether express or implied.
9  */
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 
13 /****************************** MACROS ******************************/
14 #define ROTRIGHT(a, b) (((a) >> (b)) | ((a) << (32 - (b))))
15 #define CH(x, y, z)    (((x) & (y)) ^ (~(x) & (z)))
16 #define MAJ(x, y, z)   (((x) & (y)) ^  ((x) & (z)) ^ ((y) & (z)))
17 #define EP0(x)         (ROTRIGHT(x,  2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22))
18 #define EP1(x)         (ROTRIGHT(x,  6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25))
19 #define SIG0(x)        (ROTRIGHT(x,  7) ^ ROTRIGHT(x, 18) ^ ((x) >> 3))
20 #define SIG1(x)        (ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ ((x) >> 10))
21 
22 /**************************** VARIABLES *****************************/
23 static const uint32_t k[64] = {
24 	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
25 	0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
26 	0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
27 	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
28 	0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
29 	0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
30 	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
31 	0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
32 	0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
33 	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
34 	0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
35 	0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
36 	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
37 	0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
38 	0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
39 	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
40 };
41 
42 struct sha256_ctx {
43 	uint8_t data[64];   /* current 512-bit chunk of message data, just like a buffer */
44 	uint32_t datalen;   /* sign the data length of current chunk */
45 	uint64_t bitlen;    /* the bit length of the total message */
46 	uint32_t state[8];  /* store the middle state of hash abstract */
47 };
48 
49 /*********************** FUNCTION DEFINITIONS ***********************/
sha256_transform(struct sha256_ctx * ctx,const uint8_t * data)50 static void sha256_transform(struct sha256_ctx *ctx, const uint8_t *data)
51 {
52 	uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
53 
54 	/* initialization */
55 	for (i = 0, j = 0; i < 16; ++i, j += 4)
56 		m[i] = (data[j] << 24) | (data[j + 1] << 16) |
57 			(data[j + 2] << 8) | (data[j + 3]);
58 	for ( ; i < 64; ++i)
59 		m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
60 
61 	a = ctx->state[0];
62 	b = ctx->state[1];
63 	c = ctx->state[2];
64 	d = ctx->state[3];
65 	e = ctx->state[4];
66 	f = ctx->state[5];
67 	g = ctx->state[6];
68 	h = ctx->state[7];
69 
70 	for (i = 0; i < 64; ++i) {
71 		t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i];
72 		t2 = EP0(a) + MAJ(a, b, c);
73 		h = g;
74 		g = f;
75 		f = e;
76 		e = d + t1;
77 		d = c;
78 		c = b;
79 		b = a;
80 		a = t1 + t2;
81 	}
82 
83 	ctx->state[0] += a;
84 	ctx->state[1] += b;
85 	ctx->state[2] += c;
86 	ctx->state[3] += d;
87 	ctx->state[4] += e;
88 	ctx->state[5] += f;
89 	ctx->state[6] += g;
90 	ctx->state[7] += h;
91 }
92 
sha256_init(struct sha256_ctx * ctx)93 static void sha256_init(struct sha256_ctx *ctx)
94 {
95 	ctx->datalen = 0;
96 	ctx->bitlen = 0;
97 	ctx->state[0] = 0x6a09e667;
98 	ctx->state[1] = 0xbb67ae85;
99 	ctx->state[2] = 0x3c6ef372;
100 	ctx->state[3] = 0xa54ff53a;
101 	ctx->state[4] = 0x510e527f;
102 	ctx->state[5] = 0x9b05688c;
103 	ctx->state[6] = 0x1f83d9ab;
104 	ctx->state[7] = 0x5be0cd19;
105 }
106 
sha256_update(struct sha256_ctx * ctx,const uint8_t * data,size_t len)107 static void sha256_update(struct sha256_ctx *ctx, const uint8_t *data, size_t len)
108 {
109 	uint32_t i;
110 
111 	for (i = 0; i < len; ++i) {
112 		ctx->data[ctx->datalen] = data[i];
113 		ctx->datalen++;
114 		if (ctx->datalen == 64) {
115 			/* 64 byte = 512 bit  means the buffer ctx->data has
116 			 * fully stored one chunk of message,
117 			 * so do the sha256 hash map for the current chunk.
118 			 */
119 			sha256_transform(ctx, ctx->data);
120 			ctx->bitlen += 512;
121 			ctx->datalen = 0;
122 		}
123 	}
124 }
125 
sha256_final(struct sha256_ctx * ctx,uint8_t * hash)126 static void sha256_final(struct sha256_ctx *ctx, uint8_t *hash)
127 {
128 	uint32_t i;
129 
130 	i = ctx->datalen;
131 
132 	/* Pad whatever data is left in the buffer. */
133 	if (ctx->datalen < 56) {
134 		ctx->data[i++] = 0x80;  /* pad 10000000 = 0x80 */
135 		while (i < 56)
136 			ctx->data[i++] = 0x00;
137 	} else {
138 		ctx->data[i++] = 0x80;
139 		while (i < 64)
140 			ctx->data[i++] = 0x00;
141 		sha256_transform(ctx, ctx->data);
142 		memset(ctx->data, 0, 56);
143 	}
144 
145 	/* Append to the padding the total message's length in bits and transform. */
146 	ctx->bitlen += ctx->datalen * 8;
147 	ctx->data[63] = ctx->bitlen;
148 	ctx->data[62] = ctx->bitlen >> 8;
149 	ctx->data[61] = ctx->bitlen >> 16;
150 	ctx->data[60] = ctx->bitlen >> 24;
151 	ctx->data[59] = ctx->bitlen >> 32;
152 	ctx->data[58] = ctx->bitlen >> 40;
153 	ctx->data[57] = ctx->bitlen >> 48;
154 	ctx->data[56] = ctx->bitlen >> 56;
155 	sha256_transform(ctx, ctx->data);
156 
157 	/* copying the final state to the output hash(use big endian). */
158 	for (i = 0; i < 4; ++i) {
159 		hash[i]      = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
160 		hash[i + 4]  = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
161 		hash[i + 8]  = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
162 		hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
163 		hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
164 		hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
165 		hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
166 		hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
167 	}
168 }
169 
hmac_sha256(const uint8_t * plaintext,ssize_t psize,uint8_t * output)170 int hmac_sha256(const uint8_t *plaintext, ssize_t psize, uint8_t *output)
171 {
172 	struct sha256_ctx ctx;
173 
174 	sha256_init(&ctx);
175 	sha256_update(&ctx, plaintext, psize);
176 	sha256_final(&ctx, output);
177 	return 0;
178 }
179