checksum: Refactor to remove loops and string operations
PATCH 1/9 checksum: Refactor to remove loops and string operations
Various checksum methods, like is_supported, get_digest_length, would look up the checksum data by building the algorithm name string and then iterating over all checksum_info structs to find the matching name. Change the checksum_info tables so that they're indexed by l_checksum_type and the lookups are cheap and simple. Also drop the copies of the algorithm name from struct l_checksum.
---
I've also tried changing l_checksum_get_digest_length to take a pointer to a checksum object instad of checksum type so it could be used equally for the primary algorithms and the HMAC and CMAC algorithms but iwd has one use case where it's convenient to have the digest size before creating the checksum object.
--- ell/checksum.c | 190 +++++++++++++++++++------------------------------ 1 file changed, 73 insertions(+), 117 deletions(-)
ell/checksum.c +73 -117
@@ -68,25 +68,42 @@ struct sockaddr_alg {#define SOL_ALG 279#endif -static struct { +struct checksum_info { +enum l_checksum_type type;const char *name;uint8_t digest_len;bool supported; +}; + +static struct checksum_info checksum_algs[] = { + [L_CHECKSUM_MD4 ] = { .name ="md4" , .digest_len =16 }, + [L_CHECKSUM_MD5 ] = { .name ="md5" , .digest_len =16 }, + [L_CHECKSUM_SHA1 ] = { .name ="sha1" , .digest_len =20 }, + [L_CHECKSUM_SHA256 ] = { .name ="sha256" , .digest_len =32 }, + [L_CHECKSUM_SHA384 ] = { .name ="sha384" , .digest_len =48 }, + [L_CHECKSUM_SHA512 ] = { .name ="sha512" , .digest_len =64 }, +}; + +static struct checksum_info checksum_cmac_aes_alg = + { .name ="cmac(aes)" , .digest_len =16 }; + +static struct checksum_info checksum_hmac_algs[] = { + [L_CHECKSUM_MD4 ] = { .name ="hmac(md4)" , .digest_len =16 }, + [L_CHECKSUM_MD5 ] = { .name ="hmac(md5)" , .digest_len =16 }, + [L_CHECKSUM_SHA1 ] = { .name ="hmac(sha1)" , .digest_len =20 }, + [L_CHECKSUM_SHA256 ] = { .name ="hmac(sha256)" , .digest_len =32 }, + [L_CHECKSUM_SHA384 ] = { .name ="hmac(sha384)" , .digest_len =48 }, + [L_CHECKSUM_SHA512 ] = { .name ="hmac(sha512)" , .digest_len =64 }, +}; + +static const struct { +struct checksum_info *list; +size_t n; } checksum_info_table[] = { - { .name ="md4" , .digest_len =16 }, - { .name ="md5" , .digest_len =16 }, - { .name ="sha1" , .digest_len =20 }, - { .name ="sha256" , .digest_len =32 }, - { .name ="sha384" , .digest_len =48 }, - { .name ="sha512" , .digest_len =64 }, - { .name ="cmac(aes)" , .digest_len =16 }, - { .name ="hmac(md4)" , .digest_len =16 }, - { .name ="hmac(md5)" , .digest_len =16 }, - { .name ="hmac(sha1)" , .digest_len =20 }, - { .name ="hmac(sha256)" , .digest_len =32 }, - { .name ="hmac(sha384)" , .digest_len =48 }, - { .name ="hmac(sha512)" , .digest_len =64 }, - { .name =NULL , .digest_len =0 }, + { checksum_algs,L_ARRAY_SIZE (checksum_algs) }, + { &checksum_cmac_aes_alg,1 }, + { checksum_hmac_algs,L_ARRAY_SIZE (checksum_hmac_algs) }, + {} };/**
@@ -96,17 +113,16 @@ static struct {* Checksum handling */ -#define is_valid_type (type) ((type) > L_CHECKSUM_NONE && \ - (type) <= L_CHECKSUM_SHA512) +#define is_valid_index (array, i) ((i) >= 0 && (i) < L_ARRAY_SIZE(array))/** * l_checksum: * -* Opague object representing the checksum. +* Opaque object representing the checksum. */ struct l_checksum {int sk; -char alg_name[sizeof (((struct sockaddr_alg *)0 )->salg_name)]; +const struct checksum_info *alg_info; };static int create_alg (const char *alg)
@@ -131,30 +147,6 @@ static int create_alg(const char *alg)return sk; } -static const char *checksum_type_to_name (enum l_checksum_type type) -{ -switch (type) { -case L_CHECKSUM_NONE : -return NULL ; -case L_CHECKSUM_MD4 : -return "md4" ; -case L_CHECKSUM_MD5 : -return "md5" ; -case L_CHECKSUM_SHA1 : -return "sha1" ; -case L_CHECKSUM_SHA224 : -return "sha224" ; -case L_CHECKSUM_SHA256 : -return "sha256" ; -case L_CHECKSUM_SHA384 : -return "sha384" ; -case L_CHECKSUM_SHA512 : -return "sha512" ; - } - -return NULL ; -} -/** * l_checksum_new: * @type: checksum type
@@ -166,17 +158,15 @@ static const char *checksum_type_to_name(enum l_checksum_type type) LIB_EXPORT struct l_checksum *l_checksum_new(enum l_checksum_type type) {struct l_checksum *checksum; -const char *name;int fd; -if (!is_valid_type (type)) +if (!is_valid_index (checksum_algs, type) || !checksum_algs[type].name)return NULL ; checksum = l_new(struct l_checksum, 1); + checksum->alg_info = &checksum_algs[type]; - name =checksum_type_to_name (type); - - fd =create_alg (name); + fd =create_alg (checksum->alg_info->name);if (fd <0 ) goto error;
@@ -186,8 +176,6 @@ LIB_EXPORT struct l_checksum *l_checksum_new(enum l_checksum_type type)if (checksum->sk <0 ) goto error; -strcpy (checksum->alg_name, name); -return checksum; error:
@@ -219,7 +207,7 @@ LIB_EXPORT struct l_checksum *l_checksum_new_cmac_aes(const void *key,return NULL ; } -strcpy (checksum->alg_name,"cmac(aes)" ); + checksum->alg_info = &checksum_cmac_aes_alg;return checksum; }
@@ -228,18 +216,12 @@ LIB_EXPORT struct l_checksum *l_checksum_new_hmac(enum l_checksum_type type, {struct l_checksum *checksum;int fd; -char name[sizeof (((struct sockaddr_alg *)0 )->salg_name)]; -unsigned int r; - -if (!is_valid_type (type)) -return NULL ; - r =snprintf (name,sizeof (name),"hmac(%s)" , -checksum_type_to_name (type)); -if (r >=sizeof (name)) +if (!is_valid_index (checksum_hmac_algs, type) || + !checksum_hmac_algs[type].name)return NULL ; - fd =create_alg (name); + fd =create_alg (checksum_hmac_algs[type].name);if (fd <0 )return NULL ;
@@ -257,7 +239,7 @@ LIB_EXPORT struct l_checksum *l_checksum_new_hmac(enum l_checksum_type type,return NULL ; } -strcpy (checksum->alg_name, name); + checksum->alg_info = &checksum_hmac_algs[type];return checksum; }
@@ -284,7 +266,7 @@ LIB_EXPORT struct l_checksum *l_checksum_clone(struct l_checksum *checksum)return NULL ; } -strcpy (clone->alg_name, checksum->alg_name); + clone->alg_info = checksum->alg_info;return clone; }
@@ -418,22 +400,13 @@ LIB_EXPORT ssize_t l_checksum_get_digest(struct l_checksum *checksum, LIB_EXPORT char *l_checksum_get_string(struct l_checksum *checksum) {unsigned char digest[64 ]; -unsigned int i;if (unlikely (!checksum))return NULL ;l_checksum_get_digest (checksum, digest,sizeof (digest)); -for (i =0 ; checksum_info_table[i].name; i++) { -if (strcmp (checksum_info_table[i].name, checksum->alg_name)) -continue ; - -return l_util_hexstring (digest, - checksum_info_table[i].digest_len); - } - -return NULL ; +return l_util_hexstring (digest, checksum->alg_info->digest_len); }static void init_supported ()
@@ -441,7 +414,7 @@ static void init_supported()static bool initialized = false;struct sockaddr_alg salg;int sk; -int i; +unsigned int i, j;if (likely (initialized))return ;
@@ -456,74 +429,57 @@ static void init_supported() salg.salg_family =AF_ALG ;strcpy ((char *) salg.salg_type,"hash" ); -for (i =0 ; checksum_info_table[i].name; i++) { -strcpy ((char *) salg.salg_name, checksum_info_table[i].name); - -if (bind (sk, (struct sockaddr *) &salg,sizeof (salg)) <0 ) -continue ; - - checksum_info_table[i].supported = true; - } +for (i =0 ; checksum_info_table[i].list; i++) +for (j =0 ; j < checksum_info_table[i].n; j++) { +struct checksum_info *info; -close (sk); -} + info = &checksum_info_table[i].list[j]; +if (!info->name) +continue ; -static inline bool is_supported (const char *alg) -{ -int i; +strcpy ((char *) salg.salg_name, info->name); -for (i =0 ; checksum_info_table[i].name; i++) { -if (strcmp (checksum_info_table[i].name, alg)) -continue ; +if (bind (sk, (struct sockaddr *) &salg, +sizeof (salg)) <0 ) +continue ; -return checksum_info_table[i].supported; - } + info->supported = true; + } -return false; +close (sk); } LIB_EXPORT bool l_checksum_is_supported(enum l_checksum_type type,bool check_hmac) { -const char *name; -char hmac[sizeof (((struct sockaddr_alg *)0 )->salg_name)]; +const struct checksum_info *list;init_supported (); - name =checksum_type_to_name (type); -if (!name) -return false; +if (!check_hmac) { +if (!is_valid_index (checksum_algs, type)) +return false; -if (!is_supported (name)) -return false; + list = checksum_algs; + }else { +if (!is_valid_index (checksum_hmac_algs, type)) +return false; -if (!check_hmac) -return true; + list = checksum_hmac_algs; + } -snprintf (hmac,sizeof (hmac) -1 ,"hmac(%s)" , name); -return is_supported (hmac); +return list[type].supported; } LIB_EXPORT bool l_checksum_cmac_aes_supported() {init_supported (); -return is_supported ("cmac(aes)" ); +return checksum_cmac_aes_alg.supported; } LIB_EXPORT ssize_t l_checksum_digest_length(enum l_checksum_type type) { -const char *name; -int i; - - name =checksum_type_to_name (type); -if (!name) -return -EINVAL ; - -for (i =0 ; checksum_info_table[i].name; i++) { -if (!strcmp (checksum_info_table[i].name, name)) -return checksum_info_table[i].digest_len; - } - -return 0 ; +return is_valid_index (checksum_algs, type) ? + checksum_algs[type].digest_len :0 ; }
Signature · 1 line
2.19.1
PATCH 2/9 checksum: Make l_checksum_get_digest more high-level
Check that the recv() in l_checksum_get_digest has received the expected number of bytes and update the comment to remove the reference to "bytes read" which implied this is a low-level wrapper for an fd read. Now the caller can pass a buffer longer than strictly needed for the hash, in which case the first digest_len bytes are used and digest_len is returned to inform the caller of the number of bytes written, or a shorter buffer to receive the initial part of the digest. The caller then only needs to check if the result is not < 0 instead of checking for short reads.
The normal usage now may be:
uint8_t a[64];size_t a_len;
a_len = l_checksum_get_digest(checksum, a, sizeof@);
--- ell/checksum.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
ell/checksum.c +9 -4
@@ -361,12 +361,14 @@ LIB_EXPORT bool l_checksum_updatev(struct l_checksum *checksum,/** * l_checksum_get_digest: * @checksum: checksum object -* @digest: digest data pointer -* @len: length of digest data +* @digest: output data buffer +* @len: length of output buffer * -* Gets the digest from @checksum as raw binary data. +* Writes the digest from @checksum as raw binary data into the provided +* buffer or, if the buffer is shorter, the initial @len bytes of the digest +* data. * -* Returns: Number of bytes read, or negative value if an error occurred. +* Returns: Number of bytes written, or negative value if an error occurred. **/ LIB_EXPORT ssize_t l_checksum_get_digest(struct l_checksum *checksum,void *digest,size_t len)
@@ -386,6 +388,9 @@ LIB_EXPORT ssize_t l_checksum_get_digest(struct l_checksum *checksum,if (result <0 )return -errno; +if ((size_t ) result < len && result < checksum->alg_info->digest_len) +return -EIO ; +return result; }
Signature · 1 line
2.19.1
PATCH 3/9 tls: Pass optional data parameter to sign/verify callbacks
In reply to Andrew ZaborowskiAdd parameters to optionally provide a buffer with the data to be signed/verified to the signature algorithm .sign and .verify methods, in addition to the current get_hash callback parameter. Currently .sign and .verify would receive the get_hash callback and call it when necessary to obtain the hash of the data being signed.
In the case of the handshake messages signature the hash data is part of the handshake state so there was no need for additional parameters. When we're hashing the ServerKeyExchange parameters though there's no need to keep a buffer of the data around for longer than we're building or processing the message so there's no point copying it to the handshake state structs. So instead we pass that buffer to .sign/.verify which then passes it to the get_hash callbacks.
This removes some duplication and in case of ECDHE gets rid of two instances of rebuilding the ServerECDHParams struct unnecessarily.
--- ell/tls-private.h | 13 +-- ell/tls-suites.c | 223 ++++++++++++++++++++-------------------------- ell/tls.c | 13 +-- 3 files changed, 113 insertions(+), 136 deletions(-)
ell/tls-private.h +8 -5
@@ -52,7 +52,8 @@ struct tls_hash_algorithm {extern const struct tls_hash_algorithm tls_handshake_hash_data[];typedef bool (*tls_get_hash_t )(struct l_tls *tls,uint8_t tls_id, -uint8_t *out,size_t *len, +const uint8_t *data,size_t data_len, +uint8_t *out,size_t *out_len,enum l_checksum_type *type);struct tls_key_exchange_algorithm {
@@ -72,10 +73,12 @@ struct tls_key_exchange_algorithm {void (*handle_client_key_exchange)(struct l_tls *tls,const uint8_t *buf,size_t len); -ssize_t (*sign)(struct l_tls *tls,uint8_t *out,size_t len, -tls_get_hash_t get_hash); -bool (*verify)(struct l_tls *tls,const uint8_t *in,size_t len, -tls_get_hash_t get_hash); +ssize_t (*sign)(struct l_tls *tls,uint8_t *out,size_t out_len, +tls_get_hash_t get_hash, +const uint8_t *data,size_t data_len); +bool (*verify)(struct l_tls *tls,const uint8_t *in,size_t in_len, +tls_get_hash_t get_hash, +const uint8_t *data,size_t data_len);void (*free_params)(struct l_tls *tls); };
ell/tls-suites.c +97 -126
375 lines not on this page · raw message
ell/tls.c +8 -5
@@ -1154,7 +1154,8 @@ static void tls_get_handshake_hash(struct l_tls *tls, }static bool tls_get_handshake_hash_by_id (struct l_tls *tls,uint8_t hash_id, -uint8_t *out,size_t *len, +const uint8_t *data,size_t data_len, +uint8_t *out,size_t *out_len,enum l_checksum_type *type) {enum handshake_hash_type hash;
@@ -1164,8 +1165,8 @@ static bool tls_get_handshake_hash_by_id(struct l_tls *tls, uint8_t hash_id, tls->handshake_hash[hash]) {tls_get_handshake_hash (tls, hash, out); -if (len) - *len = tls_handshake_hash_data[hash].length; +if (out_len) + *out_len = tls_handshake_hash_data[hash].length;if (type) *type = tls_handshake_hash_data[hash].l_id;
@@ -1187,7 +1188,7 @@ static bool tls_send_certificate_verify(struct l_tls *tls) sign_len = tls->pending.cipher_suite->key_xchg->sign (tls, buf +TLS_HANDSHAKE_HEADER_SIZE ,2048 -TLS_HANDSHAKE_HEADER_SIZE , - tls_get_handshake_hash_by_id); + tls_get_handshake_hash_by_id,NULL ,0 );if (sign_len <0 )return false;
@@ -2008,6 +2009,7 @@ static void tls_handle_server_hello_done(struct l_tls *tls, }static bool tls_get_prev_digest_by_id (struct l_tls *tls,uint8_t hash_id, +const uint8_t *data,size_t data_len,uint8_t *out,size_t *out_len,enum l_checksum_type *type) {
@@ -2038,7 +2040,8 @@ static void tls_handle_certificate_verify(struct l_tls *tls,int i;if (!tls->pending.cipher_suite->key_xchg->verify (tls, buf, len, - tls_get_prev_digest_by_id)) + tls_get_prev_digest_by_id, +NULL ,0 ))return ;/* Stop maintaining handshake message hashes other than the PRF hash */
Signature · 1 line
2.19.1
PATCH 4/9 tls: Simplify .sign/.verify get_hash callbacks
In reply to Andrew ZaborowskiPass the handshake_hash_type to the get_hash callbacks directly from the signature algorithm's .sign and .verify methods and drop the output hash type parameter. This simplifies both the get_hash callbacks and the callers and reduces the number of hash type lookups needed.
--- ell/tls-private.h | 44 +++++++++++++++--------------- ell/tls-suites.c | 69 +++++++++++++++++++++-------------------------- ell/tls.c | 58 +++++++++++++++------------------------ 3 files changed, 75 insertions(+), 96 deletions(-)
ell/tls-private.h +22 -22
@@ -49,12 +49,31 @@ struct tls_hash_algorithm {const char *name; }; +/* +* Support the minimum required set of handshake hash types for the +* Certificate Verify digital signature and the Finished PRF seed so we +* don't have to accumulate all of messages full contents until the +* Finished message. If we're sent a hash of a different type (in TLS 1.2+) +* and need to verify we'll give up. +* SHA1 and MD5 are explicitly required by versions < 1.2 and 1.2 requires +* that the Finished hash is the same as used for the PRF so we need to +* keep at least the hashes our supported cipher suites specify for the PRF. +*/ +enum handshake_hash_type { +HANDSHAKE_HASH_SHA384 , +HANDSHAKE_HASH_SHA256 , +HANDSHAKE_HASH_MD5 , +HANDSHAKE_HASH_SHA1 , + __HANDSHAKE_HASH_COUNT, +}; +#define HANDSHAKE_HASH_MAX_SIZE 48 +extern const struct tls_hash_algorithm tls_handshake_hash_data[]; -typedef bool (*tls_get_hash_t )(struct l_tls *tls,uint8_t tls_id, +typedef bool (*tls_get_hash_t )(struct l_tls *tls, +enum handshake_hash_type type,const uint8_t *data,size_t data_len, -uint8_t *out,size_t *out_len, -enum l_checksum_type *type); +uint8_t *out,size_t *out_len);struct tls_key_exchange_algorithm {uint8_t id;
@@ -179,25 +198,6 @@ enum tls_handshake_type { TLS_FINISHED = 20, }; -/* -* Support the minimum required set of handshake hash types for the -* Certificate Verify digital signature and the Finished PRF seed so we -* don't have to accumulate all of messages full contents until the -* Finished message. If we're sent a hash of a different type (in TLS 1.2+) -* and need to verify we'll give up. -* SHA1 and MD5 are explicitly required by versions < 1.2 and 1.2 requires -* that the Finished hash is the same as used for the PRF so we need to -* keep@least the hashes our supported cipher suites specify for the PRF. -*/ -enum handshake_hash_type { -HANDSHAKE_HASH_SHA384 , -HANDSHAKE_HASH_SHA256 , -HANDSHAKE_HASH_MD5 , -HANDSHAKE_HASH_SHA1 , - __HANDSHAKE_HASH_COUNT, -}; -#define HANDSHAKE_HASH_MAX_SIZE 48 -struct l_tls {bool server;
ell/tls-suites.c +31 -38
138 lines not on this page · raw message
ell/tls.c +22 -36
92 lines not on this page · raw message
Signature · 1 line
2.19.1
PATCH 5/9 tls: Drop the tls12_prf() hash_len parameter
In reply to Andrew Zaborowski--- ell/tls-private.h | 2 +- ell/tls.c | 16 ++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-)
ell/tls-private.h +1 -1
@@ -293,7 +293,7 @@ bool tls10_prf(const void *secret, size_t secret_len, const void *seed, size_t seed_len, uint8_t *out, size_t out_len); -bool tls12_prf(enum l_checksum_type type, size_t hash_len, +bool tls12_prf(enum l_checksum_type type, const void *secret, size_t secret_len, const char *label, const void *seed, size_t seed_len,
ell/tls.c +6 -10
@@ -62,8 +62,7 @@ bool tls10_prf(const void *secret, size_t secret_len,* the first byte of S2. */ -if (!tls12_prf (L_CHECKSUM_MD5 ,16 , - secret, l_s1, +if (!tls12_prf (L_CHECKSUM_MD5 , secret, l_s1, label, seed, seed_len, out, out_len))return false;
@@ -71,8 +70,7 @@ bool tls10_prf(const void *secret, size_t secret_len,if (secret_len >0 ) secret += secret_len - l_s1; -if (!tls12_prf (L_CHECKSUM_SHA1 ,20 , - secret, l_s1, +if (!tls12_prf (L_CHECKSUM_SHA1 , secret, l_s1, label, seed, seed_len, p_hash2, out_len))return false;
@@ -83,7 +81,7 @@ bool tls10_prf(const void *secret, size_t secret_len,return true; } -bool tls12_prf(enum l_checksum_type type, size_t hash_len, +bool tls12_prf(enum l_checksum_type type, const void *secret, size_t secret_len, const char *label, const void *seed, size_t seed_len,
@@ -107,16 +105,14 @@ bool tls12_prf(enum l_checksum_type type, size_t hash_len,/* Generate A(i) */ l_checksum_reset (hmac);l_checksum_update (hmac, a, a_len); -l_checksum_get_digest (hmac, a, hash_len); - a_len = hash_len; + a_len =l_checksum_get_digest (hmac, a,sizeof (a));/* Append seed & generate output */ memcpy (a + a_len, prfseed, prfseed_len);l_checksum_reset (hmac);l_checksum_update (hmac, a, a_len + prfseed_len); - chunk_len = out_len < hash_len ? out_len : hash_len; -l_checksum_get_digest (hmac, out, chunk_len); + chunk_len =l_checksum_get_digest (hmac, out, out_len); out += chunk_len; out_len -= chunk_len; }
@@ -132,7 +128,7 @@ static bool tls_prf_get_bytes(struct l_tls *tls, uint8_t *buf, size_t len) {if (tls->negotiated_version >=L_TLS_V12 ) -return tls12_prf (tls->prf_hmac->l_id, tls->prf_hmac->length, +return tls12_prf (tls->prf_hmac->l_id, secret, secret_len, label, seed, seed_len, buf, len);else
Signature · 1 line
2.19.1
PATCH 6/9 tls: Drop struct tls_hash_algorithm.length field
In reply to Andrew Zaborowski--- ell/tls-private.h | 1 - ell/tls.c | 16 ++++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-)
ell/tls-private.h +0 -1
@@ -45,7 +45,6 @@ struct tls_bulk_encryption_algorithm {struct tls_hash_algorithm {uint8_t tls_id;enum l_checksum_type l_id; -size_t length;const char *name; };
ell/tls.c +8 -8
@@ -556,10 +556,10 @@ static struct tls_compression_method *tls_find_compression_method( }const struct tls_hash_algorithm tls_handshake_hash_data[] = { - [HANDSHAKE_HASH_SHA384 ] = {5 ,L_CHECKSUM_SHA384 ,48 ,"SHA384" }, - [HANDSHAKE_HASH_SHA256 ] = {4 ,L_CHECKSUM_SHA256 ,32 ,"SHA256" }, - [HANDSHAKE_HASH_MD5 ] = {1 ,L_CHECKSUM_MD5 ,16 ,"MD5" }, - [HANDSHAKE_HASH_SHA1 ] = {2 ,L_CHECKSUM_SHA1 ,20 ,"SHA1" }, + [HANDSHAKE_HASH_SHA384 ] = {5 ,L_CHECKSUM_SHA384 ,"SHA384" }, + [HANDSHAKE_HASH_SHA256 ] = {4 ,L_CHECKSUM_SHA256 ,"SHA256" }, + [HANDSHAKE_HASH_MD5 ] = {1 ,L_CHECKSUM_MD5 ,"MD5" }, + [HANDSHAKE_HASH_SHA1 ] = {2 ,L_CHECKSUM_SHA1 ,"SHA1" }, };static bool tls_init_handshake_hash (struct l_tls *tls)
@@ -1144,8 +1144,8 @@ static void tls_get_handshake_hash(struct l_tls *tls,if (!hash)return ; -l_checksum_get_digest (hash, out, tls_handshake_hash_data[type].length); - +l_checksum_get_digest (hash, out,l_checksum_digest_length ( + tls_handshake_hash_data[type].l_id));l_checksum_free (hash); }
@@ -1217,7 +1217,7 @@ static void tls_send_finished(struct l_tls *tls)break ;tls_get_handshake_hash (tls, hash, seed); - seed_len = tls_handshake_hash_data[hash].length; + seed_len =l_checksum_digest_length (tls->prf_hmac->l_id); }else {tls_get_handshake_hash (tls,HANDSHAKE_HASH_MD5 , seed +0 );tls_get_handshake_hash (tls,HANDSHAKE_HASH_SHA1 , seed +16 );
@@ -1257,7 +1257,7 @@ static bool tls_verify_finished(struct l_tls *tls, const uint8_t *received,break ; seed = tls->prev_digest[hash]; - seed_len = tls_handshake_hash_data[hash].length; + seed_len =l_checksum_digest_length (tls->prf_hmac->l_id); }else { seed =alloca (36 );memcpy (seed +0 , tls->prev_digest[HANDSHAKE_HASH_MD5 ],16 );
Signature · 1 line
2.19.1
PATCH 7/9 tls: Separate signature algorithm and key exchange algorithm
In reply to Andrew ZaborowskiSeparate definitions of the key exchange algorithms from the signature algorithm. DHE and ECDHE can be used with different signature algorithms hopefully without any changes.
--- ell/tls-private.h | 24 +-- ell/tls-suites.c | 363 +++++++++++++++++++++++++--------------------- ell/tls.c | 43 +++--- 3 files changed, 234 insertions(+), 196 deletions(-)
ell/tls-private.h +13 -11
@@ -74,15 +74,23 @@ typedef bool (*tls_get_hash_t)(struct l_tls *tls, const uint8_t *data, size_t data_len, uint8_t *out, size_t *out_len); -struct tls_key_exchange_algorithm { +struct tls_signature_algorithm {uint8_t id; -bool certificate_check; +bool (*validate_cert_key_type)(struct l_cert *cert); + +ssize_t (*sign)(struct l_tls *tls,uint8_t *out,size_t out_len, +tls_get_hash_t get_hash, +const uint8_t *data,size_t data_len); +bool (*verify)(struct l_tls *tls,const uint8_t *in,size_t in_len, +tls_get_hash_t get_hash, +const uint8_t *data,size_t data_len); +}; + +struct tls_key_exchange_algorithm {bool need_ecc;bool need_ffdh; -bool (*validate_cert_key_type)(struct l_cert *cert); -bool (*send_server_key_exchange)(struct l_tls *tls);void (*handle_server_key_exchange)(struct l_tls *tls,const uint8_t *buf,size_t len);
@@ -91,13 +99,6 @@ struct tls_key_exchange_algorithm {void (*handle_client_key_exchange)(struct l_tls *tls,const uint8_t *buf,size_t len); -ssize_t (*sign)(struct l_tls *tls,uint8_t *out,size_t out_len, -tls_get_hash_t get_hash, -const uint8_t *data,size_t data_len); -bool (*verify)(struct l_tls *tls,const uint8_t *in,size_t in_len, -tls_get_hash_t get_hash, -const uint8_t *data,size_t data_len); -void (*free_params)(struct l_tls *tls); };
@@ -113,6 +114,7 @@ struct tls_cipher_suite {int verify_data_length;struct tls_bulk_encryption_algorithm *encryption; +struct tls_signature_algorithm *signature;struct tls_key_exchange_algorithm *key_xchg;struct tls_mac_algorithm *mac;enum l_checksum_type prf_hmac;
ell/tls-suites.c +197 -166
586 lines not on this page · raw message
ell/tls.c +24 -19
132 lines not on this page · raw message
Signature · 1 line
2.19.1
PATCH 8/9 tls: Use l_checksum_updatev in tls_get_dh_params_hash
In reply to Andrew ZaborowskiReduce the number of syscalls by using an iovec.
--- ell/tls-suites.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
ell/tls-suites.c +11 -7
@@ -347,19 +347,23 @@ static bool tls_get_dh_params_hash(struct l_tls *tls,struct l_checksum *checksum;ssize_t ret; - checksum =l_checksum_new (tls_handshake_hash_data[type].l_id); -if (!checksum) -return false; -/* * The ServerKeyExchange signature hash input format for RSA_sign is * not really specified in either RFC 8422 or RFC 5246 explicitly * but we use this format by analogy to DHE_RSA which uses RSA_sign * as well. Also matches ecdsa, ed25519 and ed448 formats. */ -l_checksum_update (checksum, tls->pending.client_random,32 ); -l_checksum_update (checksum, tls->pending.server_random,32 ); -l_checksum_update (checksum, data, data_len); +struct iovec iov[] = { + { .iov_base = tls->pending.client_random, .iov_len =32 }, + { .iov_base = tls->pending.server_random, .iov_len =32 }, + { .iov_base = (void *) data, .iov_len = data_len }, + }; + + checksum =l_checksum_new (tls_handshake_hash_data[type].l_id); +if (!checksum) +return false; + +l_checksum_updatev (checksum, iov,L_ARRAY_SIZE (iov)); ret =l_checksum_get_digest (checksum, out,HANDSHAKE_HASH_MAX_SIZE );l_checksum_free (checksum);
Signature · 1 line
2.19.1
PATCH 9/9 unit: Drop the hash lengths in test-tls
In reply to Andrew Zaborowski--- unit/test-tls.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
unit/test-tls.c +1 -5
@@ -81,7 +81,6 @@ static void test_tls10_prf(const void *data)/* https://www.ietf.org/mail-archive/web/tls/current/msg03416.html */ static struct tls12_prf_test { enum l_checksum_type hash; - size_t hash_len; const uint8_t *secret; size_t secret_len; const char *label;
@@ -91,7 +90,6 @@ static struct tls12_prf_test {size_t out_len; } tls12_prf_sha256_0 = { .hash = L_CHECKSUM_SHA256, - .hash_len = 32, .secret_len = 16, .secret = (const uint8_t []) { 0x9b, 0xbe, 0x43, 0x6b, 0xa9, 0x40, 0xf0, 0x17,
@@ -121,7 +119,6 @@ static struct tls12_prf_test { }, }, tls12_prf_sha384_0 = { .hash = L_CHECKSUM_SHA384, - .hash_len = 48, .secret_len = 16, .secret = (const uint8_t []) { 0xb8, 0x0b, 0x73, 0x3d, 0x6c, 0xee, 0xfc, 0xdc,
@@ -157,7 +154,6 @@ static struct tls12_prf_test { }, }, tls12_prf_sha512_0 = { .hash = L_CHECKSUM_SHA512, - .hash_len = 64, .secret_len = 16, .secret = (const uint8_t []) { 0xb0, 0x32, 0x35, 0x23, 0xc1, 0x85, 0x35, 0x99,
@@ -204,7 +200,7 @@ static void test_tls12_prf(const void *data)const struct tls12_prf_test *test = data;uint8_t out_buf[test->out_len]; -tls12_prf (test->hash, test->hash_len, test->secret, test->secret_len, +tls12_prf (test->hash, test->secret, test->secret_len, test->label, test->seed, test->seed_len, out_buf, test->out_len);
Signature · 1 line
2.19.1
Hi Andrew,
Quoted 12 lines · Andrew Zaborowski
Various checksum methods, like
is_supported,get_digest_length, would look up the checksum data by building the algorithm name string and then iterating over allchecksum_infostructs to find the matching name. Change thechecksum_infotables so that they're indexed byl_checksum_typeand the lookups are cheap and simple. Also drop the copies of the algorithm name from structl_checksum.
---
I've also tried changingl_checksum_get_digest_lengthto take a pointer to a checksum object instad of checksum type so it could be used equally for the primary algorithms and the HMAC and CMAC algorithms but iwd has one use case where it's convenient to have the digest size before creating the checksum object.
Quoted 4 lines
--- ell/checksum.c | 190 +++++++++++++++++++------------------------------ 1 file changed, 73 insertions(+), 117 deletions(-)
All applied, thanks.
Regards,
-Denis