Coverage Report

Created: 2024-11-21 07:03

/src/trezor-firmware/crypto/hmac.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * Copyright (c) 2013-2014 Tomas Dzetkulic
3
 * Copyright (c) 2013-2014 Pavol Rusnak
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining
6
 * a copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
 * OTHER DEALINGS IN THE SOFTWARE.
22
 */
23
24
#include <string.h>
25
26
#include "hmac.h"
27
#include "memzero.h"
28
#include "options.h"
29
30
void hmac_sha256_Init(HMAC_SHA256_CTX *hctx, const uint8_t *key,
31
29
                      const uint32_t keylen) {
32
29
  static CONFIDENTIAL uint8_t i_key_pad[SHA256_BLOCK_LENGTH];
33
29
  memzero(i_key_pad, SHA256_BLOCK_LENGTH);
34
29
  if (keylen > SHA256_BLOCK_LENGTH) {
35
12
    sha256_Raw(key, keylen, i_key_pad);
36
17
  } else {
37
17
    memcpy(i_key_pad, key, keylen);
38
17
  }
39
1.88k
  for (int i = 0; i < SHA256_BLOCK_LENGTH; i++) {
40
1.85k
    hctx->o_key_pad[i] = i_key_pad[i] ^ 0x5c;
41
1.85k
    i_key_pad[i] ^= 0x36;
42
1.85k
  }
43
29
  sha256_Init(&(hctx->ctx));
44
29
  sha256_Update(&(hctx->ctx), i_key_pad, SHA256_BLOCK_LENGTH);
45
29
  memzero(i_key_pad, sizeof(i_key_pad));
46
29
}
47
48
void hmac_sha256_Update(HMAC_SHA256_CTX *hctx, const uint8_t *msg,
49
13.2k
                        const uint32_t msglen) {
50
13.2k
  sha256_Update(&(hctx->ctx), msg, msglen);
51
13.2k
}
52
53
29
void hmac_sha256_Final(HMAC_SHA256_CTX *hctx, uint8_t *hmac) {
54
29
  sha256_Final(&(hctx->ctx), hmac);
55
29
  sha256_Init(&(hctx->ctx));
56
29
  sha256_Update(&(hctx->ctx), hctx->o_key_pad, SHA256_BLOCK_LENGTH);
57
29
  sha256_Update(&(hctx->ctx), hmac, SHA256_DIGEST_LENGTH);
58
29
  sha256_Final(&(hctx->ctx), hmac);
59
29
  memzero(hctx, sizeof(HMAC_SHA256_CTX));
60
29
}
61
62
void hmac_sha256(const uint8_t *key, const uint32_t keylen, const uint8_t *msg,
63
0
                 const uint32_t msglen, uint8_t *hmac) {
64
0
  static CONFIDENTIAL HMAC_SHA256_CTX hctx;
65
0
  hmac_sha256_Init(&hctx, key, keylen);
66
0
  hmac_sha256_Update(&hctx, msg, msglen);
67
0
  hmac_sha256_Final(&hctx, hmac);
68
0
}
69
70
void hmac_sha256_prepare(const uint8_t *key, const uint32_t keylen,
71
1.01k
                         uint32_t *opad_digest, uint32_t *ipad_digest) {
72
1.01k
  static CONFIDENTIAL uint32_t key_pad[SHA256_BLOCK_LENGTH / sizeof(uint32_t)];
73
74
1.01k
  memzero(key_pad, sizeof(key_pad));
75
1.01k
  if (keylen > SHA256_BLOCK_LENGTH) {
76
706
    static CONFIDENTIAL SHA256_CTX context;
77
706
    sha256_Init(&context);
78
706
    sha256_Update(&context, key, keylen);
79
706
    sha256_Final(&context, (uint8_t *)key_pad);
80
706
  } else {
81
308
    memcpy(key_pad, key, keylen);
82
308
  }
83
84
  /* compute o_key_pad and its digest */
85
17.2k
  for (int i = 0; i < SHA256_BLOCK_LENGTH / (int)sizeof(uint32_t); i++) {
86
16.2k
    uint32_t data = 0;
87
16.2k
#if BYTE_ORDER == LITTLE_ENDIAN
88
16.2k
    REVERSE32(key_pad[i], data);
89
#else
90
    data = key_pad[i];
91
#endif
92
16.2k
    key_pad[i] = data ^ 0x5c5c5c5c;
93
16.2k
  }
94
1.01k
  sha256_Transform(sha256_initial_hash_value, key_pad, opad_digest);
95
96
  /* convert o_key_pad to i_key_pad and compute its digest */
97
17.2k
  for (int i = 0; i < SHA256_BLOCK_LENGTH / (int)sizeof(uint32_t); i++) {
98
16.2k
    key_pad[i] = key_pad[i] ^ 0x5c5c5c5c ^ 0x36363636;
99
16.2k
  }
100
1.01k
  sha256_Transform(sha256_initial_hash_value, key_pad, ipad_digest);
101
1.01k
  memzero(key_pad, sizeof(key_pad));
102
1.01k
}
103
104
void hmac_sha512_Init(HMAC_SHA512_CTX *hctx, const uint8_t *key,
105
51
                      const uint32_t keylen) {
106
51
  static CONFIDENTIAL uint8_t i_key_pad[SHA512_BLOCK_LENGTH];
107
51
  memzero(i_key_pad, SHA512_BLOCK_LENGTH);
108
51
  if (keylen > SHA512_BLOCK_LENGTH) {
109
25
    sha512_Raw(key, keylen, i_key_pad);
110
26
  } else {
111
26
    memcpy(i_key_pad, key, keylen);
112
26
  }
113
6.57k
  for (int i = 0; i < SHA512_BLOCK_LENGTH; i++) {
114
6.52k
    hctx->o_key_pad[i] = i_key_pad[i] ^ 0x5c;
115
6.52k
    i_key_pad[i] ^= 0x36;
116
6.52k
  }
117
51
  sha512_Init(&(hctx->ctx));
118
51
  sha512_Update(&(hctx->ctx), i_key_pad, SHA512_BLOCK_LENGTH);
119
51
  memzero(i_key_pad, sizeof(i_key_pad));
120
51
}
121
122
void hmac_sha512_Update(HMAC_SHA512_CTX *hctx, const uint8_t *msg,
123
15.7k
                        const uint32_t msglen) {
124
15.7k
  sha512_Update(&(hctx->ctx), msg, msglen);
125
15.7k
}
126
127
51
void hmac_sha512_Final(HMAC_SHA512_CTX *hctx, uint8_t *hmac) {
128
51
  sha512_Final(&(hctx->ctx), hmac);
129
51
  sha512_Init(&(hctx->ctx));
130
51
  sha512_Update(&(hctx->ctx), hctx->o_key_pad, SHA512_BLOCK_LENGTH);
131
51
  sha512_Update(&(hctx->ctx), hmac, SHA512_DIGEST_LENGTH);
132
51
  sha512_Final(&(hctx->ctx), hmac);
133
51
  memzero(hctx, sizeof(HMAC_SHA512_CTX));
134
51
}
135
136
void hmac_sha512(const uint8_t *key, const uint32_t keylen, const uint8_t *msg,
137
0
                 const uint32_t msglen, uint8_t *hmac) {
138
0
  HMAC_SHA512_CTX hctx = {0};
139
0
  hmac_sha512_Init(&hctx, key, keylen);
140
0
  hmac_sha512_Update(&hctx, msg, msglen);
141
0
  hmac_sha512_Final(&hctx, hmac);
142
0
}
143
144
void hmac_sha512_prepare(const uint8_t *key, const uint32_t keylen,
145
547
                         uint64_t *opad_digest, uint64_t *ipad_digest) {
146
547
  static CONFIDENTIAL uint64_t key_pad[SHA512_BLOCK_LENGTH / sizeof(uint64_t)];
147
148
547
  memzero(key_pad, sizeof(key_pad));
149
547
  if (keylen > SHA512_BLOCK_LENGTH) {
150
361
    static CONFIDENTIAL SHA512_CTX context;
151
361
    sha512_Init(&context);
152
361
    sha512_Update(&context, key, keylen);
153
361
    sha512_Final(&context, (uint8_t *)key_pad);
154
361
  } else {
155
186
    memcpy(key_pad, key, keylen);
156
186
  }
157
158
  /* compute o_key_pad and its digest */
159
9.29k
  for (int i = 0; i < SHA512_BLOCK_LENGTH / (int)sizeof(uint64_t); i++) {
160
8.75k
    uint64_t data = 0;
161
8.75k
#if BYTE_ORDER == LITTLE_ENDIAN
162
8.75k
    REVERSE64(key_pad[i], data);
163
#else
164
    data = key_pad[i];
165
#endif
166
8.75k
    key_pad[i] = data ^ 0x5c5c5c5c5c5c5c5c;
167
8.75k
  }
168
547
  sha512_Transform(sha512_initial_hash_value, key_pad, opad_digest);
169
170
  /* convert o_key_pad to i_key_pad and compute its digest */
171
9.29k
  for (int i = 0; i < SHA512_BLOCK_LENGTH / (int)sizeof(uint64_t); i++) {
172
8.75k
    key_pad[i] = key_pad[i] ^ 0x5c5c5c5c5c5c5c5c ^ 0x3636363636363636;
173
8.75k
  }
174
547
  sha512_Transform(sha512_initial_hash_value, key_pad, ipad_digest);
175
547
  memzero(key_pad, sizeof(key_pad));
176
547
}