1e5b75505Sopenharmony_ci/* 2e5b75505Sopenharmony_ci * SHA-256 hash implementation and interface functions 3e5b75505Sopenharmony_ci * Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi> 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 "sha256.h" 13e5b75505Sopenharmony_ci#include "sha256_i.h" 14e5b75505Sopenharmony_ci#include "crypto.h" 15e5b75505Sopenharmony_ci 16e5b75505Sopenharmony_ci 17e5b75505Sopenharmony_ci/** 18e5b75505Sopenharmony_ci * sha256_vector - SHA256 hash for data vector 19e5b75505Sopenharmony_ci * @num_elem: Number of elements in the data vector 20e5b75505Sopenharmony_ci * @addr: Pointers to the data areas 21e5b75505Sopenharmony_ci * @len: Lengths of the data blocks 22e5b75505Sopenharmony_ci * @mac: Buffer for the hash 23e5b75505Sopenharmony_ci * Returns: 0 on success, -1 of failure 24e5b75505Sopenharmony_ci */ 25e5b75505Sopenharmony_ciint sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len, 26e5b75505Sopenharmony_ci u8 *mac) 27e5b75505Sopenharmony_ci{ 28e5b75505Sopenharmony_ci struct sha256_state ctx; 29e5b75505Sopenharmony_ci size_t i; 30e5b75505Sopenharmony_ci 31e5b75505Sopenharmony_ci if (TEST_FAIL()) 32e5b75505Sopenharmony_ci return -1; 33e5b75505Sopenharmony_ci 34e5b75505Sopenharmony_ci sha256_init(&ctx); 35e5b75505Sopenharmony_ci for (i = 0; i < num_elem; i++) 36e5b75505Sopenharmony_ci if (sha256_process(&ctx, addr[i], len[i])) 37e5b75505Sopenharmony_ci return -1; 38e5b75505Sopenharmony_ci if (sha256_done(&ctx, mac)) 39e5b75505Sopenharmony_ci return -1; 40e5b75505Sopenharmony_ci return 0; 41e5b75505Sopenharmony_ci} 42e5b75505Sopenharmony_ci 43e5b75505Sopenharmony_ci 44e5b75505Sopenharmony_ci/* ===== start - public domain SHA256 implementation ===== */ 45e5b75505Sopenharmony_ci 46e5b75505Sopenharmony_ci/* This is based on SHA256 implementation in LibTomCrypt that was released into 47e5b75505Sopenharmony_ci * public domain by Tom St Denis. */ 48e5b75505Sopenharmony_ci 49e5b75505Sopenharmony_ci/* the K array */ 50e5b75505Sopenharmony_cistatic const unsigned long K[64] = { 51e5b75505Sopenharmony_ci 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 52e5b75505Sopenharmony_ci 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, 53e5b75505Sopenharmony_ci 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 54e5b75505Sopenharmony_ci 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 55e5b75505Sopenharmony_ci 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL, 56e5b75505Sopenharmony_ci 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, 57e5b75505Sopenharmony_ci 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 58e5b75505Sopenharmony_ci 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, 59e5b75505Sopenharmony_ci 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, 60e5b75505Sopenharmony_ci 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL, 61e5b75505Sopenharmony_ci 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 62e5b75505Sopenharmony_ci 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 63e5b75505Sopenharmony_ci 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL 64e5b75505Sopenharmony_ci}; 65e5b75505Sopenharmony_ci 66e5b75505Sopenharmony_ci 67e5b75505Sopenharmony_ci/* Various logical functions */ 68e5b75505Sopenharmony_ci#define RORc(x, y) \ 69e5b75505Sopenharmony_ci( ((((unsigned long) (x) & 0xFFFFFFFFUL) >> (unsigned long) ((y) & 31)) | \ 70e5b75505Sopenharmony_ci ((unsigned long) (x) << (unsigned long) (32 - ((y) & 31)))) & 0xFFFFFFFFUL) 71e5b75505Sopenharmony_ci#define Ch(x,y,z) (z ^ (x & (y ^ z))) 72e5b75505Sopenharmony_ci#define Maj(x,y,z) (((x | y) & z) | (x & y)) 73e5b75505Sopenharmony_ci#define S(x, n) RORc((x), (n)) 74e5b75505Sopenharmony_ci#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n)) 75e5b75505Sopenharmony_ci#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) 76e5b75505Sopenharmony_ci#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) 77e5b75505Sopenharmony_ci#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) 78e5b75505Sopenharmony_ci#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) 79e5b75505Sopenharmony_ci#ifndef MIN 80e5b75505Sopenharmony_ci#define MIN(x, y) (((x) < (y)) ? (x) : (y)) 81e5b75505Sopenharmony_ci#endif 82e5b75505Sopenharmony_ci 83e5b75505Sopenharmony_ci/* compress 512-bits */ 84e5b75505Sopenharmony_cistatic int sha256_compress(struct sha256_state *md, unsigned char *buf) 85e5b75505Sopenharmony_ci{ 86e5b75505Sopenharmony_ci u32 S[8], W[64], t0, t1; 87e5b75505Sopenharmony_ci u32 t; 88e5b75505Sopenharmony_ci int i; 89e5b75505Sopenharmony_ci 90e5b75505Sopenharmony_ci /* copy state into S */ 91e5b75505Sopenharmony_ci for (i = 0; i < 8; i++) { 92e5b75505Sopenharmony_ci S[i] = md->state[i]; 93e5b75505Sopenharmony_ci } 94e5b75505Sopenharmony_ci 95e5b75505Sopenharmony_ci /* copy the state into 512-bits into W[0..15] */ 96e5b75505Sopenharmony_ci for (i = 0; i < 16; i++) 97e5b75505Sopenharmony_ci W[i] = WPA_GET_BE32(buf + (4 * i)); 98e5b75505Sopenharmony_ci 99e5b75505Sopenharmony_ci /* fill W[16..63] */ 100e5b75505Sopenharmony_ci for (i = 16; i < 64; i++) { 101e5b75505Sopenharmony_ci W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + 102e5b75505Sopenharmony_ci W[i - 16]; 103e5b75505Sopenharmony_ci } 104e5b75505Sopenharmony_ci 105e5b75505Sopenharmony_ci /* Compress */ 106e5b75505Sopenharmony_ci#define RND(a,b,c,d,e,f,g,h,i) \ 107e5b75505Sopenharmony_ci t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ 108e5b75505Sopenharmony_ci t1 = Sigma0(a) + Maj(a, b, c); \ 109e5b75505Sopenharmony_ci d += t0; \ 110e5b75505Sopenharmony_ci h = t0 + t1; 111e5b75505Sopenharmony_ci 112e5b75505Sopenharmony_ci for (i = 0; i < 64; ++i) { 113e5b75505Sopenharmony_ci RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i); 114e5b75505Sopenharmony_ci t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4]; 115e5b75505Sopenharmony_ci S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t; 116e5b75505Sopenharmony_ci } 117e5b75505Sopenharmony_ci 118e5b75505Sopenharmony_ci /* feedback */ 119e5b75505Sopenharmony_ci for (i = 0; i < 8; i++) { 120e5b75505Sopenharmony_ci md->state[i] = md->state[i] + S[i]; 121e5b75505Sopenharmony_ci } 122e5b75505Sopenharmony_ci return 0; 123e5b75505Sopenharmony_ci} 124e5b75505Sopenharmony_ci 125e5b75505Sopenharmony_ci 126e5b75505Sopenharmony_ci/* Initialize the hash state */ 127e5b75505Sopenharmony_civoid sha256_init(struct sha256_state *md) 128e5b75505Sopenharmony_ci{ 129e5b75505Sopenharmony_ci md->curlen = 0; 130e5b75505Sopenharmony_ci md->length = 0; 131e5b75505Sopenharmony_ci md->state[0] = 0x6A09E667UL; 132e5b75505Sopenharmony_ci md->state[1] = 0xBB67AE85UL; 133e5b75505Sopenharmony_ci md->state[2] = 0x3C6EF372UL; 134e5b75505Sopenharmony_ci md->state[3] = 0xA54FF53AUL; 135e5b75505Sopenharmony_ci md->state[4] = 0x510E527FUL; 136e5b75505Sopenharmony_ci md->state[5] = 0x9B05688CUL; 137e5b75505Sopenharmony_ci md->state[6] = 0x1F83D9ABUL; 138e5b75505Sopenharmony_ci md->state[7] = 0x5BE0CD19UL; 139e5b75505Sopenharmony_ci} 140e5b75505Sopenharmony_ci 141e5b75505Sopenharmony_ci/** 142e5b75505Sopenharmony_ci Process a block of memory though the hash 143e5b75505Sopenharmony_ci @param md The hash state 144e5b75505Sopenharmony_ci @param in The data to hash 145e5b75505Sopenharmony_ci @param inlen The length of the data (octets) 146e5b75505Sopenharmony_ci @return CRYPT_OK if successful 147e5b75505Sopenharmony_ci*/ 148e5b75505Sopenharmony_ciint sha256_process(struct sha256_state *md, const unsigned char *in, 149e5b75505Sopenharmony_ci unsigned long inlen) 150e5b75505Sopenharmony_ci{ 151e5b75505Sopenharmony_ci unsigned long n; 152e5b75505Sopenharmony_ci 153e5b75505Sopenharmony_ci if (md->curlen >= sizeof(md->buf)) 154e5b75505Sopenharmony_ci return -1; 155e5b75505Sopenharmony_ci 156e5b75505Sopenharmony_ci while (inlen > 0) { 157e5b75505Sopenharmony_ci if (md->curlen == 0 && inlen >= SHA256_BLOCK_SIZE) { 158e5b75505Sopenharmony_ci if (sha256_compress(md, (unsigned char *) in) < 0) 159e5b75505Sopenharmony_ci return -1; 160e5b75505Sopenharmony_ci md->length += SHA256_BLOCK_SIZE * 8; 161e5b75505Sopenharmony_ci in += SHA256_BLOCK_SIZE; 162e5b75505Sopenharmony_ci inlen -= SHA256_BLOCK_SIZE; 163e5b75505Sopenharmony_ci } else { 164e5b75505Sopenharmony_ci n = MIN(inlen, (SHA256_BLOCK_SIZE - md->curlen)); 165e5b75505Sopenharmony_ci os_memcpy(md->buf + md->curlen, in, n); 166e5b75505Sopenharmony_ci md->curlen += n; 167e5b75505Sopenharmony_ci in += n; 168e5b75505Sopenharmony_ci inlen -= n; 169e5b75505Sopenharmony_ci if (md->curlen == SHA256_BLOCK_SIZE) { 170e5b75505Sopenharmony_ci if (sha256_compress(md, md->buf) < 0) 171e5b75505Sopenharmony_ci return -1; 172e5b75505Sopenharmony_ci md->length += 8 * SHA256_BLOCK_SIZE; 173e5b75505Sopenharmony_ci md->curlen = 0; 174e5b75505Sopenharmony_ci } 175e5b75505Sopenharmony_ci } 176e5b75505Sopenharmony_ci } 177e5b75505Sopenharmony_ci 178e5b75505Sopenharmony_ci return 0; 179e5b75505Sopenharmony_ci} 180e5b75505Sopenharmony_ci 181e5b75505Sopenharmony_ci 182e5b75505Sopenharmony_ci/** 183e5b75505Sopenharmony_ci Terminate the hash to get the digest 184e5b75505Sopenharmony_ci @param md The hash state 185e5b75505Sopenharmony_ci @param out [out] The destination of the hash (32 bytes) 186e5b75505Sopenharmony_ci @return CRYPT_OK if successful 187e5b75505Sopenharmony_ci*/ 188e5b75505Sopenharmony_ciint sha256_done(struct sha256_state *md, unsigned char *out) 189e5b75505Sopenharmony_ci{ 190e5b75505Sopenharmony_ci int i; 191e5b75505Sopenharmony_ci 192e5b75505Sopenharmony_ci if (md->curlen >= sizeof(md->buf)) 193e5b75505Sopenharmony_ci return -1; 194e5b75505Sopenharmony_ci 195e5b75505Sopenharmony_ci /* increase the length of the message */ 196e5b75505Sopenharmony_ci md->length += md->curlen * 8; 197e5b75505Sopenharmony_ci 198e5b75505Sopenharmony_ci /* append the '1' bit */ 199e5b75505Sopenharmony_ci md->buf[md->curlen++] = (unsigned char) 0x80; 200e5b75505Sopenharmony_ci 201e5b75505Sopenharmony_ci /* if the length is currently above 56 bytes we append zeros 202e5b75505Sopenharmony_ci * then compress. Then we can fall back to padding zeros and length 203e5b75505Sopenharmony_ci * encoding like normal. 204e5b75505Sopenharmony_ci */ 205e5b75505Sopenharmony_ci if (md->curlen > 56) { 206e5b75505Sopenharmony_ci while (md->curlen < SHA256_BLOCK_SIZE) { 207e5b75505Sopenharmony_ci md->buf[md->curlen++] = (unsigned char) 0; 208e5b75505Sopenharmony_ci } 209e5b75505Sopenharmony_ci sha256_compress(md, md->buf); 210e5b75505Sopenharmony_ci md->curlen = 0; 211e5b75505Sopenharmony_ci } 212e5b75505Sopenharmony_ci 213e5b75505Sopenharmony_ci /* pad up to 56 bytes of zeroes */ 214e5b75505Sopenharmony_ci while (md->curlen < 56) { 215e5b75505Sopenharmony_ci md->buf[md->curlen++] = (unsigned char) 0; 216e5b75505Sopenharmony_ci } 217e5b75505Sopenharmony_ci 218e5b75505Sopenharmony_ci /* store length */ 219e5b75505Sopenharmony_ci WPA_PUT_BE64(md->buf + 56, md->length); 220e5b75505Sopenharmony_ci sha256_compress(md, md->buf); 221e5b75505Sopenharmony_ci 222e5b75505Sopenharmony_ci /* copy output */ 223e5b75505Sopenharmony_ci for (i = 0; i < 8; i++) 224e5b75505Sopenharmony_ci WPA_PUT_BE32(out + (4 * i), md->state[i]); 225e5b75505Sopenharmony_ci 226e5b75505Sopenharmony_ci return 0; 227e5b75505Sopenharmony_ci} 228e5b75505Sopenharmony_ci 229e5b75505Sopenharmony_ci/* ===== end - public domain SHA256 implementation ===== */ 230