Debian Codegraph @

crt/aws-c-cal/source/darwin/commoncrypto_sha1.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
6 #include <aws/cal/hash.h>
7
8 #include <CommonCrypto/CommonDigest.h>
9
10 static void s_destroy(struct aws_hash *hash);
11 static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash);
12 static int s_finalize(struct aws_hash *hash, struct aws_byte_buf *output);
13
14 static struct aws_hash_vtable s_vtable = {
15 .destroy = s_destroy,
16 .update = s_update,
17 .finalize = s_finalize,
18 .alg_name = "SHA1",
19 .provider = "CommonCrypto",
20 };
21
22 struct cc_sha1_hash {
23 struct aws_hash hash;
24 CC_SHA1_CTX cc_hash;
25 };
26
27 struct aws_hash *aws_sha1_default_new(struct aws_allocator *allocator) {
28 struct cc_sha1_hash *sha1_hash = aws_mem_acquire(allocator, sizeof(struct cc_sha1_hash));
29
30 sha1_hash->hash.allocator = allocator;
31 sha1_hash->hash.vtable = &s_vtable;
32 sha1_hash->hash.impl = sha1_hash;
33 sha1_hash->hash.digest_size = AWS_SHA1_LEN;
34 sha1_hash->hash.good = true;
35
36 CC_SHA1_Init(&sha1_hash->cc_hash);
37 return &sha1_hash->hash;
38 }
39
40 static void s_destroy(struct aws_hash *hash) {
41 struct cc_sha1_hash *ctx = hash->impl;
42 aws_mem_release(hash->allocator, ctx);
43 }
44
45 static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) {
46 if (!hash->good) {
47 return aws_raise_error(AWS_ERROR_INVALID_STATE);
48 }
49
50 struct cc_sha1_hash *ctx = hash->impl;
51
52 CC_SHA1_Update(&ctx->cc_hash, to_hash->ptr, (CC_LONG)to_hash->len);
53 return AWS_OP_SUCCESS;
54 }
55
56 static int s_finalize(struct aws_hash *hash, struct aws_byte_buf *output) {
57 if (!hash->good) {
58 return aws_raise_error(AWS_ERROR_INVALID_STATE);
59 }
60
61 struct cc_sha1_hash *ctx = hash->impl;
62
63 size_t buffer_len = output->capacity - output->len;
64
65 if (buffer_len < hash->digest_size) {
66 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
67 }
68
69 CC_SHA1_Final(output->buffer + output->len, &ctx->cc_hash);
70 hash->good = false;
71 output->len += hash->digest_size;
72 return AWS_OP_SUCCESS;
73 }