1e5b75505Sopenharmony_ci/*
2e5b75505Sopenharmony_ci * SHA-512 hash implementation and interface functions
3e5b75505Sopenharmony_ci * Copyright (c) 2015, Pali Rohár <pali.rohar@gmail.com>
4e5b75505Sopenharmony_ci *
5e5b75505Sopenharmony_ci * This software may be distributed under the terms of the BSD license.
6e5b75505Sopenharmony_ci * See README for more details.
7e5b75505Sopenharmony_ci */
8e5b75505Sopenharmony_ci
9e5b75505Sopenharmony_ci#include "includes.h"
10e5b75505Sopenharmony_ci
11e5b75505Sopenharmony_ci#include "common.h"
12e5b75505Sopenharmony_ci#include "sha512_i.h"
13e5b75505Sopenharmony_ci#include "crypto.h"
14e5b75505Sopenharmony_ci
15e5b75505Sopenharmony_ci
16e5b75505Sopenharmony_ci/**
17e5b75505Sopenharmony_ci * sha512_vector - SHA512 hash for data vector
18e5b75505Sopenharmony_ci * @num_elem: Number of elements in the data vector
19e5b75505Sopenharmony_ci * @addr: Pointers to the data areas
20e5b75505Sopenharmony_ci * @len: Lengths of the data blocks
21e5b75505Sopenharmony_ci * @mac: Buffer for the hash
22e5b75505Sopenharmony_ci * Returns: 0 on success, -1 of failure
23e5b75505Sopenharmony_ci */
24e5b75505Sopenharmony_ciint sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
25e5b75505Sopenharmony_ci		  u8 *mac)
26e5b75505Sopenharmony_ci{
27e5b75505Sopenharmony_ci	struct sha512_state ctx;
28e5b75505Sopenharmony_ci	size_t i;
29e5b75505Sopenharmony_ci
30e5b75505Sopenharmony_ci	sha512_init(&ctx);
31e5b75505Sopenharmony_ci	for (i = 0; i < num_elem; i++)
32e5b75505Sopenharmony_ci		if (sha512_process(&ctx, addr[i], len[i]))
33e5b75505Sopenharmony_ci			return -1;
34e5b75505Sopenharmony_ci	if (sha512_done(&ctx, mac))
35e5b75505Sopenharmony_ci		return -1;
36e5b75505Sopenharmony_ci	return 0;
37e5b75505Sopenharmony_ci}
38e5b75505Sopenharmony_ci
39e5b75505Sopenharmony_ci
40e5b75505Sopenharmony_ci/* ===== start - public domain SHA512 implementation ===== */
41e5b75505Sopenharmony_ci
42e5b75505Sopenharmony_ci/* This is based on SHA512 implementation in LibTomCrypt that was released into
43e5b75505Sopenharmony_ci * public domain by Tom St Denis. */
44e5b75505Sopenharmony_ci
45e5b75505Sopenharmony_ci#define CONST64(n) n ## ULL
46e5b75505Sopenharmony_ci
47e5b75505Sopenharmony_ci/* the K array */
48e5b75505Sopenharmony_cistatic const u64 K[80] = {
49e5b75505Sopenharmony_ci	CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd),
50e5b75505Sopenharmony_ci	CONST64(0xb5c0fbcfec4d3b2f), CONST64(0xe9b5dba58189dbbc),
51e5b75505Sopenharmony_ci	CONST64(0x3956c25bf348b538), CONST64(0x59f111f1b605d019),
52e5b75505Sopenharmony_ci	CONST64(0x923f82a4af194f9b), CONST64(0xab1c5ed5da6d8118),
53e5b75505Sopenharmony_ci	CONST64(0xd807aa98a3030242), CONST64(0x12835b0145706fbe),
54e5b75505Sopenharmony_ci	CONST64(0x243185be4ee4b28c), CONST64(0x550c7dc3d5ffb4e2),
55e5b75505Sopenharmony_ci	CONST64(0x72be5d74f27b896f), CONST64(0x80deb1fe3b1696b1),
56e5b75505Sopenharmony_ci	CONST64(0x9bdc06a725c71235), CONST64(0xc19bf174cf692694),
57e5b75505Sopenharmony_ci	CONST64(0xe49b69c19ef14ad2), CONST64(0xefbe4786384f25e3),
58e5b75505Sopenharmony_ci	CONST64(0x0fc19dc68b8cd5b5), CONST64(0x240ca1cc77ac9c65),
59e5b75505Sopenharmony_ci	CONST64(0x2de92c6f592b0275), CONST64(0x4a7484aa6ea6e483),
60e5b75505Sopenharmony_ci	CONST64(0x5cb0a9dcbd41fbd4), CONST64(0x76f988da831153b5),
61e5b75505Sopenharmony_ci	CONST64(0x983e5152ee66dfab), CONST64(0xa831c66d2db43210),
62e5b75505Sopenharmony_ci	CONST64(0xb00327c898fb213f), CONST64(0xbf597fc7beef0ee4),
63e5b75505Sopenharmony_ci	CONST64(0xc6e00bf33da88fc2), CONST64(0xd5a79147930aa725),
64e5b75505Sopenharmony_ci	CONST64(0x06ca6351e003826f), CONST64(0x142929670a0e6e70),
65e5b75505Sopenharmony_ci	CONST64(0x27b70a8546d22ffc), CONST64(0x2e1b21385c26c926),
66e5b75505Sopenharmony_ci	CONST64(0x4d2c6dfc5ac42aed), CONST64(0x53380d139d95b3df),
67e5b75505Sopenharmony_ci	CONST64(0x650a73548baf63de), CONST64(0x766a0abb3c77b2a8),
68e5b75505Sopenharmony_ci	CONST64(0x81c2c92e47edaee6), CONST64(0x92722c851482353b),
69e5b75505Sopenharmony_ci	CONST64(0xa2bfe8a14cf10364), CONST64(0xa81a664bbc423001),
70e5b75505Sopenharmony_ci	CONST64(0xc24b8b70d0f89791), CONST64(0xc76c51a30654be30),
71e5b75505Sopenharmony_ci	CONST64(0xd192e819d6ef5218), CONST64(0xd69906245565a910),
72e5b75505Sopenharmony_ci	CONST64(0xf40e35855771202a), CONST64(0x106aa07032bbd1b8),
73e5b75505Sopenharmony_ci	CONST64(0x19a4c116b8d2d0c8), CONST64(0x1e376c085141ab53),
74e5b75505Sopenharmony_ci	CONST64(0x2748774cdf8eeb99), CONST64(0x34b0bcb5e19b48a8),
75e5b75505Sopenharmony_ci	CONST64(0x391c0cb3c5c95a63), CONST64(0x4ed8aa4ae3418acb),
76e5b75505Sopenharmony_ci	CONST64(0x5b9cca4f7763e373), CONST64(0x682e6ff3d6b2b8a3),
77e5b75505Sopenharmony_ci	CONST64(0x748f82ee5defb2fc), CONST64(0x78a5636f43172f60),
78e5b75505Sopenharmony_ci	CONST64(0x84c87814a1f0ab72), CONST64(0x8cc702081a6439ec),
79e5b75505Sopenharmony_ci	CONST64(0x90befffa23631e28), CONST64(0xa4506cebde82bde9),
80e5b75505Sopenharmony_ci	CONST64(0xbef9a3f7b2c67915), CONST64(0xc67178f2e372532b),
81e5b75505Sopenharmony_ci	CONST64(0xca273eceea26619c), CONST64(0xd186b8c721c0c207),
82e5b75505Sopenharmony_ci	CONST64(0xeada7dd6cde0eb1e), CONST64(0xf57d4f7fee6ed178),
83e5b75505Sopenharmony_ci	CONST64(0x06f067aa72176fba), CONST64(0x0a637dc5a2c898a6),
84e5b75505Sopenharmony_ci	CONST64(0x113f9804bef90dae), CONST64(0x1b710b35131c471b),
85e5b75505Sopenharmony_ci	CONST64(0x28db77f523047d84), CONST64(0x32caab7b40c72493),
86e5b75505Sopenharmony_ci	CONST64(0x3c9ebe0a15c9bebc), CONST64(0x431d67c49c100d4c),
87e5b75505Sopenharmony_ci	CONST64(0x4cc5d4becb3e42b6), CONST64(0x597f299cfc657e2a),
88e5b75505Sopenharmony_ci	CONST64(0x5fcb6fab3ad6faec), CONST64(0x6c44198c4a475817)
89e5b75505Sopenharmony_ci};
90e5b75505Sopenharmony_ci
91e5b75505Sopenharmony_ci/* Various logical functions */
92e5b75505Sopenharmony_ci#define Ch(x,y,z)       (z ^ (x & (y ^ z)))
93e5b75505Sopenharmony_ci#define Maj(x,y,z)      (((x | y) & z) | (x & y))
94e5b75505Sopenharmony_ci#define S(x, n)         ROR64c(x, n)
95e5b75505Sopenharmony_ci#define R(x, n)         (((x) & CONST64(0xFFFFFFFFFFFFFFFF)) >> ((u64) n))
96e5b75505Sopenharmony_ci#define Sigma0(x)       (S(x, 28) ^ S(x, 34) ^ S(x, 39))
97e5b75505Sopenharmony_ci#define Sigma1(x)       (S(x, 14) ^ S(x, 18) ^ S(x, 41))
98e5b75505Sopenharmony_ci#define Gamma0(x)       (S(x, 1) ^ S(x, 8) ^ R(x, 7))
99e5b75505Sopenharmony_ci#define Gamma1(x)       (S(x, 19) ^ S(x, 61) ^ R(x, 6))
100e5b75505Sopenharmony_ci#ifndef MIN
101e5b75505Sopenharmony_ci#define MIN(x, y) (((x) < (y)) ? (x) : (y))
102e5b75505Sopenharmony_ci#endif
103e5b75505Sopenharmony_ci
104e5b75505Sopenharmony_ci#define ROR64c(x, y) \
105e5b75505Sopenharmony_ci    ( ((((x) & CONST64(0xFFFFFFFFFFFFFFFF)) >> ((u64) (y) & CONST64(63))) | \
106e5b75505Sopenharmony_ci      ((x) << ((u64) (64 - ((y) & CONST64(63)))))) & \
107e5b75505Sopenharmony_ci      CONST64(0xFFFFFFFFFFFFFFFF))
108e5b75505Sopenharmony_ci
109e5b75505Sopenharmony_ci/* compress 1024-bits */
110e5b75505Sopenharmony_cistatic int sha512_compress(struct sha512_state *md, unsigned char *buf)
111e5b75505Sopenharmony_ci{
112e5b75505Sopenharmony_ci	u64 S[8], t0, t1;
113e5b75505Sopenharmony_ci	u64 *W;
114e5b75505Sopenharmony_ci	int i;
115e5b75505Sopenharmony_ci
116e5b75505Sopenharmony_ci	W = os_malloc(80 * sizeof(u64));
117e5b75505Sopenharmony_ci	if (!W)
118e5b75505Sopenharmony_ci		return -1;
119e5b75505Sopenharmony_ci
120e5b75505Sopenharmony_ci	/* copy state into S */
121e5b75505Sopenharmony_ci	for (i = 0; i < 8; i++) {
122e5b75505Sopenharmony_ci		S[i] = md->state[i];
123e5b75505Sopenharmony_ci	}
124e5b75505Sopenharmony_ci
125e5b75505Sopenharmony_ci	/* copy the state into 1024-bits into W[0..15] */
126e5b75505Sopenharmony_ci	for (i = 0; i < 16; i++)
127e5b75505Sopenharmony_ci		W[i] = WPA_GET_BE64(buf + (8 * i));
128e5b75505Sopenharmony_ci
129e5b75505Sopenharmony_ci	/* fill W[16..79] */
130e5b75505Sopenharmony_ci	for (i = 16; i < 80; i++) {
131e5b75505Sopenharmony_ci		W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) +
132e5b75505Sopenharmony_ci			W[i - 16];
133e5b75505Sopenharmony_ci	}
134e5b75505Sopenharmony_ci
135e5b75505Sopenharmony_ci	/* Compress */
136e5b75505Sopenharmony_ci	for (i = 0; i < 80; i++) {
137e5b75505Sopenharmony_ci		t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i];
138e5b75505Sopenharmony_ci		t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
139e5b75505Sopenharmony_ci		S[7] = S[6];
140e5b75505Sopenharmony_ci		S[6] = S[5];
141e5b75505Sopenharmony_ci		S[5] = S[4];
142e5b75505Sopenharmony_ci		S[4] = S[3] + t0;
143e5b75505Sopenharmony_ci		S[3] = S[2];
144e5b75505Sopenharmony_ci		S[2] = S[1];
145e5b75505Sopenharmony_ci		S[1] = S[0];
146e5b75505Sopenharmony_ci		S[0] = t0 + t1;
147e5b75505Sopenharmony_ci	}
148e5b75505Sopenharmony_ci
149e5b75505Sopenharmony_ci	/* feedback */
150e5b75505Sopenharmony_ci	for (i = 0; i < 8; i++) {
151e5b75505Sopenharmony_ci		md->state[i] = md->state[i] + S[i];
152e5b75505Sopenharmony_ci	}
153e5b75505Sopenharmony_ci
154e5b75505Sopenharmony_ci	os_free(W);
155e5b75505Sopenharmony_ci	return 0;
156e5b75505Sopenharmony_ci}
157e5b75505Sopenharmony_ci
158e5b75505Sopenharmony_ci
159e5b75505Sopenharmony_ci/**
160e5b75505Sopenharmony_ci   Initialize the hash state
161e5b75505Sopenharmony_ci   @param md   The hash state you wish to initialize
162e5b75505Sopenharmony_ci   @return CRYPT_OK if successful
163e5b75505Sopenharmony_ci*/
164e5b75505Sopenharmony_civoid sha512_init(struct sha512_state *md)
165e5b75505Sopenharmony_ci{
166e5b75505Sopenharmony_ci	md->curlen = 0;
167e5b75505Sopenharmony_ci	md->length = 0;
168e5b75505Sopenharmony_ci	md->state[0] = CONST64(0x6a09e667f3bcc908);
169e5b75505Sopenharmony_ci	md->state[1] = CONST64(0xbb67ae8584caa73b);
170e5b75505Sopenharmony_ci	md->state[2] = CONST64(0x3c6ef372fe94f82b);
171e5b75505Sopenharmony_ci	md->state[3] = CONST64(0xa54ff53a5f1d36f1);
172e5b75505Sopenharmony_ci	md->state[4] = CONST64(0x510e527fade682d1);
173e5b75505Sopenharmony_ci	md->state[5] = CONST64(0x9b05688c2b3e6c1f);
174e5b75505Sopenharmony_ci	md->state[6] = CONST64(0x1f83d9abfb41bd6b);
175e5b75505Sopenharmony_ci	md->state[7] = CONST64(0x5be0cd19137e2179);
176e5b75505Sopenharmony_ci}
177e5b75505Sopenharmony_ci
178e5b75505Sopenharmony_ci
179e5b75505Sopenharmony_ci/**
180e5b75505Sopenharmony_ci   Process a block of memory though the hash
181e5b75505Sopenharmony_ci   @param md     The hash state
182e5b75505Sopenharmony_ci   @param in     The data to hash
183e5b75505Sopenharmony_ci   @param inlen  The length of the data (octets)
184e5b75505Sopenharmony_ci   @return CRYPT_OK if successful
185e5b75505Sopenharmony_ci*/
186e5b75505Sopenharmony_ciint sha512_process(struct sha512_state *md, const unsigned char *in,
187e5b75505Sopenharmony_ci		   unsigned long inlen)
188e5b75505Sopenharmony_ci{
189e5b75505Sopenharmony_ci	unsigned long n;
190e5b75505Sopenharmony_ci
191e5b75505Sopenharmony_ci	if (md->curlen >= sizeof(md->buf))
192e5b75505Sopenharmony_ci		return -1;
193e5b75505Sopenharmony_ci
194e5b75505Sopenharmony_ci	while (inlen > 0) {
195e5b75505Sopenharmony_ci		if (md->curlen == 0 && inlen >= SHA512_BLOCK_SIZE) {
196e5b75505Sopenharmony_ci			if (sha512_compress(md, (unsigned char *) in) < 0)
197e5b75505Sopenharmony_ci				return -1;
198e5b75505Sopenharmony_ci			md->length += SHA512_BLOCK_SIZE * 8;
199e5b75505Sopenharmony_ci			in += SHA512_BLOCK_SIZE;
200e5b75505Sopenharmony_ci			inlen -= SHA512_BLOCK_SIZE;
201e5b75505Sopenharmony_ci		} else {
202e5b75505Sopenharmony_ci			n = MIN(inlen, (SHA512_BLOCK_SIZE - md->curlen));
203e5b75505Sopenharmony_ci			os_memcpy(md->buf + md->curlen, in, n);
204e5b75505Sopenharmony_ci			md->curlen += n;
205e5b75505Sopenharmony_ci			in += n;
206e5b75505Sopenharmony_ci			inlen -= n;
207e5b75505Sopenharmony_ci			if (md->curlen == SHA512_BLOCK_SIZE) {
208e5b75505Sopenharmony_ci				if (sha512_compress(md, md->buf) < 0)
209e5b75505Sopenharmony_ci					return -1;
210e5b75505Sopenharmony_ci				md->length += 8 * SHA512_BLOCK_SIZE;
211e5b75505Sopenharmony_ci				md->curlen = 0;
212e5b75505Sopenharmony_ci			}
213e5b75505Sopenharmony_ci		}
214e5b75505Sopenharmony_ci	}
215e5b75505Sopenharmony_ci
216e5b75505Sopenharmony_ci	return 0;
217e5b75505Sopenharmony_ci}
218e5b75505Sopenharmony_ci
219e5b75505Sopenharmony_ci
220e5b75505Sopenharmony_ci/**
221e5b75505Sopenharmony_ci   Terminate the hash to get the digest
222e5b75505Sopenharmony_ci   @param md  The hash state
223e5b75505Sopenharmony_ci   @param out [out] The destination of the hash (64 bytes)
224e5b75505Sopenharmony_ci   @return CRYPT_OK if successful
225e5b75505Sopenharmony_ci*/
226e5b75505Sopenharmony_ciint sha512_done(struct sha512_state *md, unsigned char *out)
227e5b75505Sopenharmony_ci{
228e5b75505Sopenharmony_ci	int i;
229e5b75505Sopenharmony_ci
230e5b75505Sopenharmony_ci	if (md->curlen >= sizeof(md->buf))
231e5b75505Sopenharmony_ci		return -1;
232e5b75505Sopenharmony_ci
233e5b75505Sopenharmony_ci	/* increase the length of the message */
234e5b75505Sopenharmony_ci	md->length += md->curlen * CONST64(8);
235e5b75505Sopenharmony_ci
236e5b75505Sopenharmony_ci	/* append the '1' bit */
237e5b75505Sopenharmony_ci	md->buf[md->curlen++] = (unsigned char) 0x80;
238e5b75505Sopenharmony_ci
239e5b75505Sopenharmony_ci	/* if the length is currently above 112 bytes we append zeros
240e5b75505Sopenharmony_ci	 * then compress.  Then we can fall back to padding zeros and length
241e5b75505Sopenharmony_ci	 * encoding like normal.
242e5b75505Sopenharmony_ci	 */
243e5b75505Sopenharmony_ci	if (md->curlen > 112) {
244e5b75505Sopenharmony_ci		while (md->curlen < 128) {
245e5b75505Sopenharmony_ci			md->buf[md->curlen++] = (unsigned char) 0;
246e5b75505Sopenharmony_ci		}
247e5b75505Sopenharmony_ci		sha512_compress(md, md->buf);
248e5b75505Sopenharmony_ci		md->curlen = 0;
249e5b75505Sopenharmony_ci	}
250e5b75505Sopenharmony_ci
251e5b75505Sopenharmony_ci	/* pad up to 120 bytes of zeroes
252e5b75505Sopenharmony_ci	 * note: that from 112 to 120 is the 64 MSB of the length.  We assume
253e5b75505Sopenharmony_ci	 * that you won't hash > 2^64 bits of data... :-)
254e5b75505Sopenharmony_ci	 */
255e5b75505Sopenharmony_ci	while (md->curlen < 120) {
256e5b75505Sopenharmony_ci		md->buf[md->curlen++] = (unsigned char) 0;
257e5b75505Sopenharmony_ci	}
258e5b75505Sopenharmony_ci
259e5b75505Sopenharmony_ci	/* store length */
260e5b75505Sopenharmony_ci	WPA_PUT_BE64(md->buf + 120, md->length);
261e5b75505Sopenharmony_ci	sha512_compress(md, md->buf);
262e5b75505Sopenharmony_ci
263e5b75505Sopenharmony_ci	/* copy output */
264e5b75505Sopenharmony_ci	for (i = 0; i < 8; i++)
265e5b75505Sopenharmony_ci		WPA_PUT_BE64(out + (8 * i), md->state[i]);
266e5b75505Sopenharmony_ci
267e5b75505Sopenharmony_ci	return 0;
268e5b75505Sopenharmony_ci}
269e5b75505Sopenharmony_ci
270e5b75505Sopenharmony_ci/* ===== end - public domain SHA512 implementation ===== */
271