summaryrefslogtreecommitdiff
path: root/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/hmac_hmacsha256.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxencryptsave/crypto_pwhash_scryptsalsa208sha256/hmac_hmacsha256.c')
-rw-r--r--toxencryptsave/crypto_pwhash_scryptsalsa208sha256/hmac_hmacsha256.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/hmac_hmacsha256.c b/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/hmac_hmacsha256.c
new file mode 100644
index 00000000..2a268075
--- /dev/null
+++ b/toxencryptsave/crypto_pwhash_scryptsalsa208sha256/hmac_hmacsha256.c
@@ -0,0 +1,115 @@
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4#ifdef VANILLA_NACL /* toxcore only uses this when libsodium is unavailable */
5
6/*-
7 * Copyright 2005,2007,2009 Colin Percival
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33//#include "api.h"
34#include "crypto_auth_hmacsha256.h"
35#include "crypto_hash_sha256.h"
36#include "utils.h"
37
38#include <sys/types.h>
39
40#include <stdint.h>
41#include <string.h>
42
43int
44crypto_auth_hmacsha256_init(crypto_auth_hmacsha256_state *state,
45 const unsigned char *key,
46 size_t keylen)
47{
48 unsigned char pad[64];
49 unsigned char khash[32];
50 size_t i;
51
52 if (keylen > 64) {
53 crypto_hash_sha256_init(&state->ictx);
54 crypto_hash_sha256_update(&state->ictx, key, keylen);
55 crypto_hash_sha256_final(&state->ictx, khash);
56 key = khash;
57 keylen = 32;
58 }
59 crypto_hash_sha256_init(&state->ictx);
60 memset(pad, 0x36, 64);
61 for (i = 0; i < keylen; i++) {
62 pad[i] ^= key[i];
63 }
64 crypto_hash_sha256_update(&state->ictx, pad, 64);
65
66 crypto_hash_sha256_init(&state->octx);
67 memset(pad, 0x5c, 64);
68 for (i = 0; i < keylen; i++) {
69 pad[i] ^= key[i];
70 }
71 crypto_hash_sha256_update(&state->octx, pad, 64);
72
73 sodium_memzero((void *) khash, sizeof khash);
74
75 return 0;
76}
77
78int
79crypto_auth_hmacsha256_update(crypto_auth_hmacsha256_state *state,
80 const unsigned char *in,
81 unsigned long long inlen)
82{
83 crypto_hash_sha256_update(&state->ictx, in, inlen);
84
85 return 0;
86}
87
88int
89crypto_auth_hmacsha256_final(crypto_auth_hmacsha256_state *state,
90 unsigned char *out)
91{
92 unsigned char ihash[32];
93
94 crypto_hash_sha256_final(&state->ictx, ihash);
95 crypto_hash_sha256_update(&state->octx, ihash, 32);
96 crypto_hash_sha256_final(&state->octx, out);
97
98 sodium_memzero((void *) ihash, sizeof ihash);
99
100 return 0;
101}
102
103int
104crypto_auth(unsigned char *out, const unsigned char *in,
105 unsigned long long inlen, const unsigned char *k)
106{
107 crypto_auth_hmacsha256_state state;
108
109 crypto_auth_hmacsha256_init(&state, k, crypto_auth_hmacsha256_KEYBYTES);
110 crypto_auth_hmacsha256_update(&state, in, inlen);
111 crypto_auth_hmacsha256_final(&state, out);
112
113 return 0;
114}
115#endif