aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-cal/source/hash.c
blob: b4a9163fc4b95a99530b8125ac0d46376d244643 (plain) (blame)
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
/** 
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 
 * SPDX-License-Identifier: Apache-2.0. 
 */ 
#include <aws/cal/hash.h> 
 
#ifndef AWS_BYO_CRYPTO 
extern struct aws_hash *aws_sha256_default_new(struct aws_allocator *allocator); 
extern struct aws_hash *aws_md5_default_new(struct aws_allocator *allocator); 
 
static aws_hash_new_fn *s_sha256_new_fn = aws_sha256_default_new; 
static aws_hash_new_fn *s_md5_new_fn = aws_md5_default_new; 
#else 
static struct aws_hash *aws_hash_new_abort(struct aws_allocator *allocator) { 
    (void)allocator; 
    abort(); 
} 
 
static aws_hash_new_fn *s_sha256_new_fn = aws_hash_new_abort; 
static aws_hash_new_fn *s_md5_new_fn = aws_hash_new_abort; 
#endif 
 
struct aws_hash *aws_sha256_new(struct aws_allocator *allocator) { 
    return s_sha256_new_fn(allocator); 
} 
 
struct aws_hash *aws_md5_new(struct aws_allocator *allocator) { 
    return s_md5_new_fn(allocator); 
} 
 
void aws_set_md5_new_fn(aws_hash_new_fn *fn) { 
    s_md5_new_fn = fn; 
} 
 
void aws_set_sha256_new_fn(aws_hash_new_fn *fn) { 
    s_sha256_new_fn = fn; 
} 
 
void aws_hash_destroy(struct aws_hash *hash) { 
    hash->vtable->destroy(hash); 
} 
 
int aws_hash_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) { 
    return hash->vtable->update(hash, to_hash); 
} 
 
int aws_hash_finalize(struct aws_hash *hash, struct aws_byte_buf *output, size_t truncate_to) { 
 
    if (truncate_to && truncate_to < hash->digest_size) { 
        size_t available_buffer = output->capacity - output->len; 
        if (available_buffer < truncate_to) { 
            return aws_raise_error(AWS_ERROR_SHORT_BUFFER); 
        } 
 
        uint8_t tmp_output[128] = {0}; 
        AWS_ASSERT(sizeof(tmp_output) >= hash->digest_size); 
 
        struct aws_byte_buf tmp_out_buf = aws_byte_buf_from_array(tmp_output, sizeof(tmp_output)); 
        tmp_out_buf.len = 0; 
 
        if (hash->vtable->finalize(hash, &tmp_out_buf)) { 
            return AWS_OP_ERR; 
        } 
 
        memcpy(output->buffer + output->len, tmp_output, truncate_to); 
        output->len += truncate_to; 
        return AWS_OP_SUCCESS; 
    } 
 
    return hash->vtable->finalize(hash, output); 
} 
 
static inline int compute_hash( 
    struct aws_hash *hash, 
    const struct aws_byte_cursor *input, 
    struct aws_byte_buf *output, 
    size_t truncate_to) { 
    if (!hash) { 
        return AWS_OP_ERR; 
    } 
 
    if (aws_hash_update(hash, input)) { 
        aws_hash_destroy(hash); 
        return AWS_OP_ERR; 
    } 
 
    if (aws_hash_finalize(hash, output, truncate_to)) { 
        aws_hash_destroy(hash); 
        return AWS_OP_ERR; 
    } 
 
    aws_hash_destroy(hash); 
    return AWS_OP_SUCCESS; 
} 
 
int aws_md5_compute( 
    struct aws_allocator *allocator, 
    const struct aws_byte_cursor *input, 
    struct aws_byte_buf *output, 
    size_t truncate_to) { 
    return compute_hash(aws_md5_new(allocator), input, output, truncate_to); 
} 
 
int aws_sha256_compute( 
    struct aws_allocator *allocator, 
    const struct aws_byte_cursor *input, 
    struct aws_byte_buf *output, 
    size_t truncate_to) { 
    return compute_hash(aws_sha256_new(allocator), input, output, truncate_to); 
}