Skip to content

Commit

Permalink
lib-dcrypt: Use cpu32_to_be instead of htonl
Browse files Browse the repository at this point in the history
prefer our own conversion functions over htonl
which is intended for networking related numbers.
  • Loading branch information
cmouse authored and Timo Sirainen committed Oct 3, 2017
1 parent a943ed0 commit 32340fe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/lib-dcrypt/istream-decrypt.c
Expand Up @@ -449,7 +449,7 @@ ssize_t i_stream_decrypt_key(struct decrypt_istream *stream, const char *malg, u
hash->result(hctx, hres);

for(int i = 1; i < 2049; i++) {
uint32_t i_msb = htonl(i);
uint32_t i_msb = cpu32_to_be(i);

hash->init(hctx);
hash->loop(hctx, hres, sizeof(hres));
Expand Down
16 changes: 8 additions & 8 deletions src/lib-dcrypt/ostream-encrypt.c
Expand Up @@ -108,20 +108,20 @@ int o_stream_encrypt_send_header_v2(struct encrypt_ostream *stream)
buffer_append(values, IOSTREAM_CRYPT_MAGIC, sizeof(IOSTREAM_CRYPT_MAGIC));
c = 2;
buffer_append(values, &c, 1);
i = htonl(stream->flags);
i = cpu32_to_be(stream->flags);
buffer_append(values, &i, 4);
/* store total length of header
9 = version + flags + length
8 = rounds + key data length
*/
i = htonl(sizeof(IOSTREAM_CRYPT_MAGIC) + 9 + stream->cipher_oid->used + stream->mac_oid->used + 8 + stream->key_data_len);
i = cpu32_to_be(sizeof(IOSTREAM_CRYPT_MAGIC) + 9 + stream->cipher_oid->used + stream->mac_oid->used + 8 + stream->key_data_len);
buffer_append(values, &i, 4);

buffer_append_buf(values, stream->cipher_oid, 0, (size_t)-1);
buffer_append_buf(values, stream->mac_oid, 0, (size_t)-1);
i = htonl(IO_STREAM_ENCRYPT_ROUNDS);
i = cpu32_to_be(IO_STREAM_ENCRYPT_ROUNDS);
buffer_append(values, &i, 4);
i = htonl(stream->key_data_len);
i = cpu32_to_be(stream->key_data_len);
buffer_append(values, &i, 4);
buffer_append(values, stream->key_data, stream->key_data_len);
i_free_and_null(stream->key_data);
Expand Down Expand Up @@ -313,11 +313,11 @@ int o_stream_encrypt_key_for_pubkey_v2(struct encrypt_ostream *stream, const cha
/* store hash of public key as ID */
dcrypt_key_id_public(stream->pub, "sha256", res, NULL);
/* store ephemeral key (if present) */
unsigned int val = htonl(ephemeral_key->used);
unsigned int val = cpu32_to_be(ephemeral_key->used);
buffer_append(res, &val, 4);
buffer_append_buf(res, ephemeral_key, 0, (size_t)-1);
/* store encrypted key */
val = htonl(encrypted_key->used);
val = cpu32_to_be(encrypted_key->used);
buffer_append(res, &val, 4);
buffer_append_buf(res, encrypted_key, 0, (size_t)-1);

Expand Down Expand Up @@ -378,7 +378,7 @@ int o_stream_encrypt_keydata_create_v2(struct encrypt_ostream *stream, const cha
hash->result(hctx, hres);

for(int i = 1; i < 2049; i++) {
uint32_t i_msb = htonl(i);
uint32_t i_msb = cpu32_to_be(i);

hash->init(hctx);
hash->loop(hctx, hres, sizeof(hres));
Expand All @@ -387,7 +387,7 @@ int o_stream_encrypt_keydata_create_v2(struct encrypt_ostream *stream, const cha
}

/* store key data hash */
val = htonl(sizeof(hres));
val = cpu32_to_be(sizeof(hres));
buffer_append(res, &val, 4);
buffer_append(res, hres, sizeof(hres));

Expand Down

0 comments on commit 32340fe

Please sign in to comment.