1 /*-
2 * Copyright 2005-2016 Colin Percival
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #ifndef _SHA256_H_
28 #define _SHA256_H_
29
30 #include "crypt-port.h"
31
32 #include <stddef.h>
33 #include <stdint.h>
34
35 /*
36 * Use #defines in order to avoid namespace collisions with anyone else's
37 * SHA256 code (e.g., the code in OpenSSL).
38 */
39 #define SHA256_Init libcperciva_SHA256_Init
40 #define SHA256_Update libcperciva_SHA256_Update
41 #define SHA256_Final libcperciva_SHA256_Final
42 #define SHA256_Buf libcperciva_SHA256_Buf
43 #define SHA256_CTX libcperciva_SHA256_CTX
44 #define HMAC_SHA256_Init libcperciva_HMAC_SHA256_Init
45 #define HMAC_SHA256_Update libcperciva_HMAC_SHA256_Update
46 #define HMAC_SHA256_Final libcperciva_HMAC_SHA256_Final
47 #define HMAC_SHA256_Buf libcperciva_HMAC_SHA256_Buf
48 #define HMAC_SHA256_CTX libcperciva_HMAC_SHA256_CTX
49
50 /* Context structure for SHA256 operations. */
51 typedef struct {
52 uint32_t state[8];
53 uint64_t count;
54 uint8_t buf[64];
55 } SHA256_CTX;
56
57 /**
58 * SHA256_Init(ctx):
59 * Initialize the SHA256 context ${ctx}.
60 */
61 extern void SHA256_Init(SHA256_CTX *);
62
63 /**
64 * SHA256_Update(ctx, in, len):
65 * Input ${len} bytes from ${in} into the SHA256 context ${ctx}.
66 */
67 extern void SHA256_Update(SHA256_CTX *, const void *, size_t);
68
69 /**
70 * SHA256_Final(digest, ctx):
71 * Output the SHA256 hash of the data input to the context ${ctx} into the
72 * buffer ${digest}.
73 */
74 extern void SHA256_Final(uint8_t[32], SHA256_CTX *);
75
76 /**
77 * SHA256_Buf(in, len, digest):
78 * Compute the SHA256 hash of ${len} bytes from ${in} and write it to ${digest}.
79 */
80 extern void SHA256_Buf(const void *, size_t, uint8_t[32]);
81
82 /* Context structure for HMAC-SHA256 operations. */
83 typedef struct {
84 SHA256_CTX ictx;
85 SHA256_CTX octx;
86 } HMAC_SHA256_CTX;
87
88 /**
89 * HMAC_SHA256_Init(ctx, K, Klen):
90 * Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
91 * ${K}.
92 */
93 extern void HMAC_SHA256_Init(HMAC_SHA256_CTX *, const void *, size_t);
94
95 /**
96 * HMAC_SHA256_Update(ctx, in, len):
97 * Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
98 */
99 extern void HMAC_SHA256_Update(HMAC_SHA256_CTX *, const void *, size_t);
100
101 /**
102 * HMAC_SHA256_Final(digest, ctx):
103 * Output the HMAC-SHA256 of the data input to the context ${ctx} into the
104 * buffer ${digest}.
105 */
106 extern void HMAC_SHA256_Final(uint8_t[32], HMAC_SHA256_CTX *);
107
108 /**
109 * HMAC_SHA256_Buf(K, Klen, in, len, digest):
110 * Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
111 * length ${Klen}, and write the result to ${digest}.
112 */
113 extern void HMAC_SHA256_Buf(const void *, size_t, const void *, size_t, uint8_t[32]);
114
115 /**
116 * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
117 * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
118 * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
119 */
120 extern void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
121 uint64_t, uint8_t *, size_t);
122
123 #endif /* !_SHA256_H_ */