Skip to contentSkip to conversation

crypto: assert cipher algorithm is always valid

PATCH In review P J P 2 participants 2 messages Feb 20, 2017 Crypto API

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH 1/1 crypto: assert cipher algorithm is always valid

From: Prasad J Pandit <[email protected]>

Crypto routines 'qcrypto_cipher_get_block_len' and 'qcrypto_cipher_get_key_len' return non-zero cipher block and key lengths from static arrays 'alg_block_len[]' and 'alg_key_len[]' respectively. Returning 'zero(0)' value from either of them would likely lead to an error condition.

---
 crypto/cipher.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
crypto/cipher.c +2 -6
index 9ecaff7..5a96489 100644
@@ -63,18 +63,14 @@ static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
 
 size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
 {
-    if (alg >= G_N_ELEMENTS(alg_key_len)) {
-        return 0;
-    }
+    assert(alg < G_N_ELEMENTS(alg_key_len));
     return alg_block_len[alg];
 }
 
 
 size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
 {
-    if (alg >= G_N_ELEMENTS(alg_key_len)) {
-        return 0;
-    }
+    assert(alg < G_N_ELEMENTS(alg_key_len));
     return alg_key_len[alg];
 }
 
Signature · 1 line
2.9.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · P J P

From: Prasad J Pandit <[email protected]>

Crypto routines 'qcrypto_cipher_get_block_len' and 'qcrypto_cipher_get_key_len' return non-zero cipher block and key lengths from static arrays 'alg_block_len[]' and 'alg_key_len[]' respectively. Returning 'zero(0)' value from either of them would likely lead to an error condition.

Well callers are supposed to check for 0 condition and report an error really. In practice none of them do, and the alg parameters they pass in all come from constants. So we're not validating user input here - we're catching programming bugs and thus assert makes sense.

Quoted 5 lines
---
 crypto/cipher.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
Quoted 15 lines
crypto/cipher.c +1 -3
index 9ecaff7..5a96489 100644
@@ -63,18 +63,14 @@ static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
 
 size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
 {
-    if (alg >= G_N_ELEMENTS(alg_key_len)) {
-        return 0;
-    }
+    assert(alg < G_N_ELEMENTS(alg_key_len));
     return alg_block_len[alg];
 }

crypto/cipher.c static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = { · line 75

Quoted 10 lines
crypto/cipher.c +1 -3
 
 
 size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
 {
-    if (alg >= G_N_ELEMENTS(alg_key_len)) {
-        return 0;
-    }
+    assert(alg < G_N_ELEMENTS(alg_key_len));
     return alg_key_len[alg];
 }

Adding to the crypto/ queue

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|