Debian Codegraph @

crt/aws-c-cal/source/hmac.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/cal/hmac.h>
6
7 #ifndef BYO_CRYPTO
8 extern struct aws_hmac *aws_sha256_hmac_default_new(
9 struct aws_allocator *allocator,
10 const struct aws_byte_cursor *secret);
11 static aws_hmac_new_fn *s_sha256_hmac_new_fn = aws_sha256_hmac_default_new;
12
13 extern struct aws_hmac *aws_sha512_hmac_default_new(
14 struct aws_allocator *allocator,
15 const struct aws_byte_cursor *secret);
16 static aws_hmac_new_fn *s_sha512_hmac_new_fn = aws_sha512_hmac_default_new;
17 #else
18 static struct aws_hmac *aws_hmac_new_abort(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
19 (void)allocator;
20 (void)secret;
21 abort();
22 }
23
24 static aws_hmac_new_fn *s_sha256_hmac_new_fn = aws_hmac_new_abort;
25 static aws_hmac_new_fn *s_sha512_hmac_new_fn = aws_hmac_new_abort;
26 #endif
27
28 struct aws_hmac *aws_sha256_hmac_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
29 return s_sha256_hmac_new_fn(allocator, secret);
30 }
31
32 void aws_set_sha256_hmac_new_fn(aws_hmac_new_fn *fn) {
33 s_sha256_hmac_new_fn = fn;
34 }
35
36 struct aws_hmac *aws_sha512_hmac_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
37 return s_sha512_hmac_new_fn(allocator, secret);
38 }
39
40 void aws_set_sha512_hmac_new_fn(aws_hmac_new_fn *fn) {
41 s_sha512_hmac_new_fn = fn;
42 }
43
44 void aws_hmac_destroy(struct aws_hmac *hmac) {
45 hmac->vtable->destroy(hmac);
46 }
47
48 int aws_hmac_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac) {
49 return hmac->vtable->update(hmac, to_hmac);
50 }
51
52 int aws_hmac_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output, size_t truncate_to) {
53 if (truncate_to && truncate_to < hmac->digest_size) {
54 size_t available_buffer = output->capacity - output->len;
55 if (available_buffer < truncate_to) {
56 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
57 }
58
59 uint8_t tmp_output[128] = {0};
60 AWS_ASSERT(sizeof(tmp_output) >= hmac->digest_size);
61
62 struct aws_byte_buf tmp_out_buf = aws_byte_buf_from_array(tmp_output, sizeof(tmp_output));
63 tmp_out_buf.len = 0;
64
65 if (hmac->vtable->finalize(hmac, &tmp_out_buf)) {
66 return AWS_OP_ERR;
67 }
68
69 memcpy(output->buffer + output->len, tmp_output, truncate_to);
70 output->len += truncate_to;
71 return AWS_OP_SUCCESS;
72 }
73
74 return hmac->vtable->finalize(hmac, output);
75 }
76
77 int aws_sha256_hmac_compute(
78 struct aws_allocator *allocator,
79 const struct aws_byte_cursor *secret,
80 const struct aws_byte_cursor *to_hmac,
81 struct aws_byte_buf *output,
82 size_t truncate_to) {
83 struct aws_hmac *hmac = aws_sha256_hmac_new(allocator, secret);
84
85 if (!hmac) {
86 return AWS_OP_ERR;
87 }
88
89 if (aws_hmac_update(hmac, to_hmac)) {
90 aws_hmac_destroy(hmac);
91 return AWS_OP_ERR;
92 }
93
94 if (aws_hmac_finalize(hmac, output, truncate_to)) {
95 aws_hmac_destroy(hmac);
96 return AWS_OP_ERR;
97 }
98
99 aws_hmac_destroy(hmac);
100 return AWS_OP_SUCCESS;
101 }
102
103 int aws_sha512_hmac_compute(
104 struct aws_allocator *allocator,
105 const struct aws_byte_cursor *secret,
106 const struct aws_byte_cursor *to_hmac,
107 struct aws_byte_buf *output,
108 size_t truncate_to) {
109 struct aws_hmac *hmac = aws_sha512_hmac_new(allocator, secret);
110
111 if (!hmac) {
112 return AWS_OP_ERR;
113 }
114
115 if (aws_hmac_update(hmac, to_hmac)) {
116 aws_hmac_destroy(hmac);
117 return AWS_OP_ERR;
118 }
119
120 if (aws_hmac_finalize(hmac, output, truncate_to)) {
121 aws_hmac_destroy(hmac);
122 return AWS_OP_ERR;
123 }
124
125 aws_hmac_destroy(hmac);
126 return AWS_OP_SUCCESS;
127 }