File: bcrypt_hmac.c

package info (click to toggle)
aws-crt-python 0.28.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,428 kB
  • sloc: ansic: 437,955; python: 27,657; makefile: 5,855; sh: 4,289; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (191 lines) | stat: -rw-r--r-- 6,017 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <aws/cal/cal.h>
#include <aws/cal/hmac.h>

#include <aws/common/thread.h>

#include <windows.h>

#include <bcrypt.h>
#include <winerror.h>

static BCRYPT_ALG_HANDLE s_sha256_hmac_alg = NULL;
static BCRYPT_ALG_HANDLE s_sha512_hmac_alg = NULL;
static size_t s_sha256_hmac_obj_len = 0;
static size_t s_sha512_hmac_obj_len = 0;

static aws_thread_once s_sha256_hmac_once = AWS_THREAD_ONCE_STATIC_INIT;
static aws_thread_once s_sha512_hmac_once = AWS_THREAD_ONCE_STATIC_INIT;

static void s_destroy(struct aws_hmac *hash);
static int s_update(struct aws_hmac *hash, const struct aws_byte_cursor *to_hash);
static int s_finalize(struct aws_hmac *hash, struct aws_byte_buf *output);

static struct aws_hmac_vtable s_sha256_hmac_vtable = {
    .destroy = s_destroy,
    .update = s_update,
    .finalize = s_finalize,
    .alg_name = "SHA256 HMAC",
    .provider = "Windows CNG",
};

static struct aws_hmac_vtable s_sha512_hmac_vtable = {
    .destroy = s_destroy,
    .update = s_update,
    .finalize = s_finalize,
    .alg_name = "SHA512 HMAC",
    .provider = "Windows CNG",
};

struct bcrypt_hmac_handle {
    struct aws_hmac hmac;
    BCRYPT_HASH_HANDLE hash_handle;
    uint8_t *hash_obj;
};

static void s_load_alg_handle_sha256(void *user_data) {
    (void)user_data;
    /* this function is incredibly slow, LET IT LEAK*/
    BCryptOpenAlgorithmProvider(
        &s_sha256_hmac_alg, BCRYPT_SHA256_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
    AWS_ASSERT(s_sha256_hmac_alg);
    DWORD result_length = 0;
    BCryptGetProperty(
        s_sha256_hmac_alg,
        BCRYPT_OBJECT_LENGTH,
        (PBYTE)&s_sha256_hmac_obj_len,
        sizeof(s_sha256_hmac_obj_len),
        &result_length,
        0);
}

static void s_load_alg_handle_sha512(void *user_data) {
    (void)user_data;
    /* this function is incredibly slow, LET IT LEAK*/
    BCryptOpenAlgorithmProvider(
        &s_sha512_hmac_alg, BCRYPT_SHA512_ALGORITHM, MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
    AWS_ASSERT(s_sha512_hmac_alg);
    DWORD result_length = 0;
    BCryptGetProperty(
        s_sha512_hmac_alg,
        BCRYPT_OBJECT_LENGTH,
        (PBYTE)&s_sha512_hmac_obj_len,
        sizeof(s_sha512_hmac_obj_len),
        &result_length,
        0);
}

struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
    aws_thread_call_once(&s_sha256_hmac_once, s_load_alg_handle_sha256, NULL);

    struct bcrypt_hmac_handle *bcrypt_hmac;
    uint8_t *hash_obj;
    aws_mem_acquire_many(
        allocator, 2, &bcrypt_hmac, sizeof(struct bcrypt_hmac_handle), &hash_obj, s_sha256_hmac_obj_len);

    AWS_ZERO_STRUCT(*bcrypt_hmac);
    bcrypt_hmac->hmac.allocator = allocator;
    bcrypt_hmac->hmac.vtable = &s_sha256_hmac_vtable;
    bcrypt_hmac->hmac.impl = bcrypt_hmac;
    bcrypt_hmac->hmac.digest_size = AWS_SHA256_HMAC_LEN;
    bcrypt_hmac->hmac.good = true;
    bcrypt_hmac->hash_obj = hash_obj;
    NTSTATUS status = BCryptCreateHash(
        s_sha256_hmac_alg,
        &bcrypt_hmac->hash_handle,
        bcrypt_hmac->hash_obj,
        (ULONG)s_sha256_hmac_obj_len,
        secret->ptr,
        (ULONG)secret->len,
        0);

    if (((NTSTATUS)status) < 0) {
        aws_mem_release(allocator, bcrypt_hmac);
        aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
        return NULL;
    }

    return &bcrypt_hmac->hmac;
}

struct aws_hmac *aws_sha512_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
    aws_thread_call_once(&s_sha512_hmac_once, s_load_alg_handle_sha512, NULL);

    struct bcrypt_hmac_handle *bcrypt_hmac;
    uint8_t *hash_obj;
    aws_mem_acquire_many(
        allocator, 2, &bcrypt_hmac, sizeof(struct bcrypt_hmac_handle), &hash_obj, s_sha512_hmac_obj_len);

    AWS_ZERO_STRUCT(*bcrypt_hmac);
    bcrypt_hmac->hmac.allocator = allocator;
    bcrypt_hmac->hmac.vtable = &s_sha512_hmac_vtable;
    bcrypt_hmac->hmac.impl = bcrypt_hmac;
    bcrypt_hmac->hmac.digest_size = AWS_SHA512_HMAC_LEN;
    bcrypt_hmac->hmac.good = true;
    bcrypt_hmac->hash_obj = hash_obj;
    NTSTATUS status = BCryptCreateHash(
        s_sha512_hmac_alg,
        &bcrypt_hmac->hash_handle,
        bcrypt_hmac->hash_obj,
        (ULONG)s_sha512_hmac_obj_len,
        secret->ptr,
        (ULONG)secret->len,
        0);

    if (((NTSTATUS)status) < 0) {
        aws_mem_release(allocator, bcrypt_hmac);
        aws_raise_error(AWS_ERROR_CAL_CRYPTO_OPERATION_FAILED);
        return NULL;
    }

    return &bcrypt_hmac->hmac;
}

static void s_destroy(struct aws_hmac *hmac) {
    struct bcrypt_hmac_handle *ctx = hmac->impl;
    BCryptDestroyHash(ctx->hash_handle);
    aws_mem_release(hmac->allocator, ctx);
}

static int s_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hash) {
    if (!hmac->good) {
        return aws_raise_error(AWS_ERROR_INVALID_STATE);
    }

    struct bcrypt_hmac_handle *ctx = hmac->impl;
    NTSTATUS status = BCryptHashData(ctx->hash_handle, to_hash->ptr, (ULONG)to_hash->len, 0);

    if (((NTSTATUS)status) < 0) {
        hmac->good = false;
        return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
    }

    return AWS_OP_SUCCESS;
}

static int s_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output) {
    if (!hmac->good) {
        return aws_raise_error(AWS_ERROR_INVALID_STATE);
    }

    struct bcrypt_hmac_handle *ctx = hmac->impl;

    size_t buffer_len = output->capacity - output->len;

    if (buffer_len < hmac->digest_size) {
        return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
    }

    NTSTATUS status = BCryptFinishHash(ctx->hash_handle, output->buffer + output->len, (ULONG)hmac->digest_size, 0);

    hmac->good = false;
    if (((NTSTATUS)status) < 0) {
        return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
    }

    output->len += hmac->digest_size;
    return AWS_OP_SUCCESS;
}