• Eric Biggers's avatar
    crypto: testmgr - eliminate redundant decryption test vectors · 92a4c9fe
    Eric Biggers authored
    Currently testmgr has separate encryption and decryption test vectors
    for symmetric ciphers.  That's massively redundant, since with few
    exceptions (mostly mistakes, apparently), all decryption tests are
    identical to the encryption tests, just with the input/result flipped.
    Therefore, eliminate the redundancy by removing the decryption test
    vectors and updating testmgr to test both encryption and decryption
    using what used to be the encryption test vectors.  Naming is adjusted
    accordingly: each cipher_testvec now has a 'ptext' (plaintext), 'ctext'
    (ciphertext), and 'len' instead of an 'input', 'result', 'ilen', and
    'rlen'.  Note that it was always the case that 'ilen == rlen'.
    AES keywrap ("kw(aes)") is special because its IV is generated by the
    encryption.  Previously this was handled by specifying 'iv_out' for
    encryption and 'iv' for decryption.  To make it work cleanly with only
    one set of test vectors, put the IV in 'iv', remove 'iv_out', and add a
    boolean that indicates that the IV is generated by the encryption.
    In total, this removes over 10000 lines from testmgr.h, with no
    reduction in test coverage since prior patches already copied the few
    unique decryption test vectors into the encryption test vectors.
    This covers all algorithms that used 'struct cipher_testvec', e.g. any
    block cipher in the ECB, CBC, CTR, XTS, LRW, CTS-CBC, PCBC, OFB, or
    keywrap modes, and Salsa20 and ChaCha20.  No change is made to AEAD
    tests, though we probably can eliminate a similar redundancy there too.
    The testmgr.h portion of this patch was automatically generated using
    the following awk script, with some slight manual fixups on top (updated
    'struct cipher_testvec' definition, updated a few comments, and fixed up
    the AES keywrap test vectors):
        BEGIN { OTHER = 0; ENCVEC = 1; DECVEC = 2; DECVEC_TAIL = 3; mode = OTHER }
        /^static const struct cipher_testvec.*_enc_/ { sub("_enc", ""); mode = ENCVEC }
        /^static const struct cipher_testvec.*_dec_/ { mode = DECVEC }
        mode == ENCVEC && !/\.ilen[[:space:]]*=/ {
        	sub(/\.input[[:space:]]*=$/,    ".ptext =")
        	sub(/\.input[[:space:]]*=/,     ".ptext\t=")
        	sub(/\.result[[:space:]]*=$/,   ".ctext =")
        	sub(/\.result[[:space:]]*=/,    ".ctext\t=")
        	sub(/\.rlen[[:space:]]*=/,      ".len\t=")
        mode == DECVEC_TAIL && /[^[:space:]]/ { mode = OTHER }
        mode == OTHER                         { print }
        mode == ENCVEC && /^};/               { mode = OTHER }
        mode == DECVEC && /^};/               { mode = DECVEC_TAIL }
    Note that git's default diff algorithm gets confused by the testmgr.h
    portion of this patch, and reports too many lines added and removed.
    It's better viewed with 'git diff --minimal' (or 'git show --minimal'),
    which reports "2 files changed, 919 insertions(+), 11723 deletions(-)".
    Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
    Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>