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