Skip to content

Commit

Permalink
Merge pull request #7 from awslabs/const_correctness
Browse files Browse the repository at this point in the history
Added const modifiers where it made sense.
  • Loading branch information
JonathanHenson authored Feb 14, 2019
2 parents 90eb144 + 9c6f727 commit 5bff919
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 37 deletions.
8 changes: 4 additions & 4 deletions include/aws/cal/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct aws_hash_vtable {
const char *alg_name;
const char *provider;
void (*destroy)(struct aws_hash *hash);
int (*update)(struct aws_hash *hash, struct aws_byte_cursor *buf);
int (*update)(struct aws_hash *hash, const struct aws_byte_cursor *buf);
int (*finalize)(struct aws_hash *hash, struct aws_byte_buf *out);
};

Expand Down Expand Up @@ -58,7 +58,7 @@ AWS_CAL_API void aws_hash_destroy(struct aws_hash *hash);
/**
* Updates the running hash with to_hash. this can be called multiple times.
*/
AWS_CAL_API int aws_hash_update(struct aws_hash *hash, struct aws_byte_cursor *to_hash);
AWS_CAL_API int aws_hash_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash);
/**
* Completes the hash computation and writes the final digest to output.
* Allocation of output is the caller's responsibility. If you specify
Expand All @@ -76,7 +76,7 @@ AWS_CAL_API int aws_hash_finalize(struct aws_hash *hash, struct aws_byte_buf *ou
*/
AWS_CAL_API int aws_md5_compute(
struct aws_allocator *allocator,
struct aws_byte_cursor *input,
const struct aws_byte_cursor *input,
struct aws_byte_buf *output,
size_t truncate_to);

Expand All @@ -90,7 +90,7 @@ AWS_CAL_API int aws_md5_compute(
*/
AWS_CAL_API int aws_sha256_compute(
struct aws_allocator *allocator,
struct aws_byte_cursor *input,
const struct aws_byte_cursor *input,
struct aws_byte_buf *output,
size_t truncate_to);

Expand Down
12 changes: 6 additions & 6 deletions include/aws/cal/hmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct aws_hmac_vtable {
const char *alg_name;
const char *provider;
void (*destroy)(struct aws_hmac *hmac);
int (*update)(struct aws_hmac *hmac, struct aws_byte_cursor *buf);
int (*update)(struct aws_hmac *hmac, const struct aws_byte_cursor *buf);
int (*finalize)(struct aws_hmac *hmac, struct aws_byte_buf *out);
};

Expand All @@ -39,14 +39,14 @@ struct aws_hmac {
void *impl;
};

typedef struct aws_hmac *(aws_hmac_new_fn)(struct aws_allocator *allocator, struct aws_byte_cursor *secret);
typedef struct aws_hmac *(aws_hmac_new_fn)(struct aws_allocator *allocator, const struct aws_byte_cursor *secret);

AWS_EXTERN_C_BEGIN
/**
* Allocates and initializes a sha256 hmac instance. Secret is the key to be
* used for the hmac process.
*/
AWS_CAL_API struct aws_hmac *aws_sha256_hmac_new(struct aws_allocator *allocator, struct aws_byte_cursor *secret);
AWS_CAL_API struct aws_hmac *aws_sha256_hmac_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret);

/**
* Cleans up and deallocates hmac.
Expand All @@ -56,7 +56,7 @@ AWS_CAL_API void aws_hmac_destroy(struct aws_hmac *hmac);
/**
* Updates the running hmac with to_hash. this can be called multiple times.
*/
AWS_CAL_API int aws_hmac_update(struct aws_hmac *hmac, struct aws_byte_cursor *to_hmac);
AWS_CAL_API int aws_hmac_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac);
/**
* Completes the hmac computation and writes the final digest to output.
* Allocation of output is the caller's responsibility. If you specify
Expand All @@ -76,8 +76,8 @@ AWS_CAL_API int aws_hmac_finalize(struct aws_hmac *hmac, struct aws_byte_buf *ou
*/
AWS_CAL_API int aws_sha256_hmac_compute(
struct aws_allocator *allocator,
struct aws_byte_cursor *secret,
struct aws_byte_cursor *to_hmac,
const struct aws_byte_cursor *secret,
const struct aws_byte_cursor *to_hmac,
struct aws_byte_buf *output,
size_t truncate_to);
/**
Expand Down
4 changes: 2 additions & 2 deletions source/bcrypt/bcrypt_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static size_t s_md5_obj_len = 0;
static aws_thread_once s_md5_once = AWS_THREAD_ONCE_STATIC_INIT;

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

static struct aws_hash_vtable s_sha256_vtable = {
Expand Down Expand Up @@ -136,7 +136,7 @@ static void s_destroy(struct aws_hash *hash) {
aws_mem_release(hash->allocator, ctx);
}

static int s_update(struct aws_hash *hash, struct aws_byte_cursor *to_hash) {
static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) {
if (!hash->good) {
return aws_raise_error(AWS_ERROR_INVALID_STATE);
}
Expand Down
6 changes: 3 additions & 3 deletions source/bcrypt/bcrypt_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static size_t s_sha256_hmac_obj_len = 0;
static aws_thread_once s_sha256_hmac_once = AWS_THREAD_ONCE_STATIC_INIT;

static void s_destroy(struct aws_hmac *hash);
static int s_update(struct aws_hmac *hash, struct aws_byte_cursor *to_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 = {
Expand Down Expand Up @@ -59,7 +59,7 @@ static void s_load_alg_handle(void) {
0);
}

struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, struct aws_byte_cursor *secret) {
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);

struct bcrypt_hmac_handle *bcrypt_hmac;
Expand Down Expand Up @@ -101,7 +101,7 @@ static void s_destroy(struct aws_hmac *hmac) {
aws_mem_release(hmac->allocator, ctx);
}

static int s_update(struct aws_hmac *hmac, struct aws_byte_cursor *to_hash) {
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);
}
Expand Down
6 changes: 3 additions & 3 deletions source/commoncrypto/commoncrypto_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <CommonCrypto/CommonHMAC.h>

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

static struct aws_hmac_vtable s_sha256_hmac_vtable = {
Expand All @@ -33,7 +33,7 @@ struct cc_hmac {
CCHmacContext cc_hmac_ctx;
};

struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, struct aws_byte_cursor *secret) {
struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
assert(secret->ptr);

struct cc_hmac *cc_hmac = aws_mem_acquire(allocator, sizeof(struct cc_hmac));
Expand All @@ -58,7 +58,7 @@ static void s_destroy(struct aws_hmac *hmac) {
aws_mem_release(hmac->allocator, ctx);
}

static int s_update(struct aws_hmac *hmac, struct aws_byte_cursor *to_hmac) {
static int s_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac) {
if (!hmac->good) {
return aws_raise_error(AWS_ERROR_INVALID_STATE);
}
Expand Down
4 changes: 2 additions & 2 deletions source/commoncrypto/commoncrypto_md5.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <CommonCrypto/CommonDigest.h>

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

static struct aws_hash_vtable s_vtable = {
Expand Down Expand Up @@ -54,7 +54,7 @@ static void s_destroy(struct aws_hash *hash) {
aws_mem_release(hash->allocator, ctx);
}

static int s_update(struct aws_hash *hash, struct aws_byte_cursor *to_hash) {
static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) {
if (!hash->good) {
return aws_raise_error(AWS_ERROR_INVALID_STATE);
}
Expand Down
4 changes: 2 additions & 2 deletions source/commoncrypto/commoncrypto_sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <CommonCrypto/CommonDigest.h>

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

static struct aws_hash_vtable s_vtable = {
Expand Down Expand Up @@ -55,7 +55,7 @@ static void s_destroy(struct aws_hash *hash) {
aws_mem_release(hash->allocator, ctx);
}

static int s_update(struct aws_hash *hash, struct aws_byte_cursor *to_hash) {
static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) {
if (!hash->good) {
return aws_raise_error(AWS_ERROR_INVALID_STATE);
}
Expand Down
8 changes: 4 additions & 4 deletions source/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void aws_hash_destroy(struct aws_hash *hash) {
hash->vtable->destroy(hash);
}

int aws_hash_update(struct aws_hash *hash, struct aws_byte_cursor *to_hash) {
int aws_hash_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) {
return hash->vtable->update(hash, to_hash);
}

Expand Down Expand Up @@ -82,7 +82,7 @@ int aws_hash_finalize(struct aws_hash *hash, struct aws_byte_buf *output, size_t

static inline int compute_hash(
struct aws_hash *hash,
struct aws_byte_cursor *input,
const struct aws_byte_cursor *input,
struct aws_byte_buf *output,
size_t truncate_to) {
if (!hash) {
Expand All @@ -105,15 +105,15 @@ static inline int compute_hash(

int aws_md5_compute(
struct aws_allocator *allocator,
struct aws_byte_cursor *input,
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,
struct aws_byte_cursor *input,
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);
Expand Down
14 changes: 8 additions & 6 deletions source/hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#include <aws/cal/hmac.h>

#ifndef AWS_BYO_CRYPTO
extern struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, struct aws_byte_cursor *secret);
extern struct aws_hmac *aws_sha256_hmac_default_new(
struct aws_allocator *allocator,
const struct aws_byte_cursor *secret);
static aws_hmac_new_fn *s_sha256_hmac_new_fn = aws_sha256_hmac_default_new;
#else
static struct aws_hmac *aws_hmac_new_abort(struct aws_allocator *allocator, struct aws_byte_cursor *secret) {
static struct aws_hmac *aws_hmac_new_abort(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
(void)allocator;
(void)secret;
abort();
Expand All @@ -27,7 +29,7 @@ static struct aws_hmac *aws_hmac_new_abort(struct aws_allocator *allocator, stru
static aws_hmac_new_fn *s_sha256_hmac_new_fn = aws_hmac_new_abort;
#endif

struct aws_hmac *aws_sha256_hmac_new(struct aws_allocator *allocator, struct aws_byte_cursor *secret) {
struct aws_hmac *aws_sha256_hmac_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
return s_sha256_hmac_new_fn(allocator, secret);
}

Expand All @@ -39,7 +41,7 @@ void aws_hmac_destroy(struct aws_hmac *hmac) {
hmac->vtable->destroy(hmac);
}

int aws_hmac_update(struct aws_hmac *hmac, struct aws_byte_cursor *to_hmac) {
int aws_hmac_update(struct aws_hmac *hmac, const struct aws_byte_cursor *to_hmac) {
return hmac->vtable->update(hmac, to_hmac);
}

Expand Down Expand Up @@ -70,8 +72,8 @@ int aws_hmac_finalize(struct aws_hmac *hmac, struct aws_byte_buf *output, size_t

int aws_sha256_hmac_compute(
struct aws_allocator *allocator,
struct aws_byte_cursor *secret,
struct aws_byte_cursor *to_hmac,
const struct aws_byte_cursor *secret,
const struct aws_byte_cursor *to_hmac,
struct aws_byte_buf *output,
size_t truncate_to) {
struct aws_hmac *hmac = aws_sha256_hmac_new(allocator, secret);
Expand Down
4 changes: 2 additions & 2 deletions source/opensslcrypto/opensslcrypto_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <openssl/sha.h>

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

static struct aws_hash_vtable s_md5_vtable = {
Expand Down Expand Up @@ -103,7 +103,7 @@ static void s_destroy(struct aws_hash *hash) {
aws_mem_release(hash->allocator, hash);
}

static int s_update(struct aws_hash *hash, struct aws_byte_cursor *to_hash) {
static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) {
if (!hash->good) {
return aws_raise_error(AWS_ERROR_INVALID_STATE);
}
Expand Down
6 changes: 3 additions & 3 deletions source/opensslcrypto/opensslcrypto_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define OPENSSL_VERSION_LESS_1_1 (OPENSSL_VERSION_NUMBER < 0x10100003L)

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

static struct aws_hmac_vtable s_sha256_hmac_vtable = {
Expand All @@ -41,7 +41,7 @@ static struct aws_hmac_vtable s_sha256_hmac_vtable = {
.provider = "OpenSSL Compatible libcrypto",
};

struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, struct aws_byte_cursor *secret) {
struct aws_hmac *aws_sha256_hmac_default_new(struct aws_allocator *allocator, const struct aws_byte_cursor *secret) {
assert(secret->ptr);

struct aws_hmac *hmac = aws_mem_acquire(allocator, sizeof(struct aws_hmac));
Expand Down Expand Up @@ -100,7 +100,7 @@ static void s_destroy(struct aws_hmac *hmac) {
aws_mem_release(hmac->allocator, hmac);
}

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

0 comments on commit 5bff919

Please sign in to comment.