1e1051a39Sopenharmony_ci#! /usr/bin/env perl 2e1051a39Sopenharmony_ci# Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci# 4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci# this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci 9e1051a39Sopenharmony_ci#Convert CCM CAVS test vectors to a format suitable for evp_test 10e1051a39Sopenharmony_ci 11e1051a39Sopenharmony_ciuse strict; 12e1051a39Sopenharmony_ciuse warnings; 13e1051a39Sopenharmony_ci 14e1051a39Sopenharmony_cimy $alg; 15e1051a39Sopenharmony_cimy $mode; 16e1051a39Sopenharmony_cimy $keylen; 17e1051a39Sopenharmony_cimy $key = ""; 18e1051a39Sopenharmony_cimy $iv = ""; 19e1051a39Sopenharmony_cimy $aad = ""; 20e1051a39Sopenharmony_cimy $ct = ""; 21e1051a39Sopenharmony_cimy $pt = ""; 22e1051a39Sopenharmony_cimy $tag = ""; 23e1051a39Sopenharmony_cimy $aadlen = 0; 24e1051a39Sopenharmony_cimy $ptlen = 0; 25e1051a39Sopenharmony_cimy $taglen = 0; 26e1051a39Sopenharmony_cimy $res = ""; 27e1051a39Sopenharmony_cimy $intest = 0; 28e1051a39Sopenharmony_cimy $fixediv = 0; 29e1051a39Sopenharmony_ci 30e1051a39Sopenharmony_ciwhile (<STDIN>) 31e1051a39Sopenharmony_ci{ 32e1051a39Sopenharmony_ci chomp; 33e1051a39Sopenharmony_ci 34e1051a39Sopenharmony_ci # Pull out the cipher mode from the comment at the beginning of the file 35e1051a39Sopenharmony_ci if(/^#\s*"([^-]+)-\w+" information/) { 36e1051a39Sopenharmony_ci $mode = lc($1); 37e1051a39Sopenharmony_ci # Pull out the key length from the comment at the beginning of the file 38e1051a39Sopenharmony_ci } elsif(/^#\s*(\w+) Keylen: (\d+)/) { 39e1051a39Sopenharmony_ci $alg = lc($1); 40e1051a39Sopenharmony_ci $keylen = $2; 41e1051a39Sopenharmony_ci # Some parameters common to many tests appear as a list in square brackets 42e1051a39Sopenharmony_ci # so parse these 43e1051a39Sopenharmony_ci } elsif(/\[(.*)\]/) { 44e1051a39Sopenharmony_ci my @pairs = split(/, /, $1); 45e1051a39Sopenharmony_ci foreach my $pair (@pairs) { 46e1051a39Sopenharmony_ci $pair =~ /(\w+)\s*=\s*(\d+)/; 47e1051a39Sopenharmony_ci # AAD Length 48e1051a39Sopenharmony_ci if ($1 eq "Alen") { 49e1051a39Sopenharmony_ci $aadlen = $2; 50e1051a39Sopenharmony_ci # Plaintext length 51e1051a39Sopenharmony_ci } elsif ($1 eq "Plen") { 52e1051a39Sopenharmony_ci $ptlen = $2; 53e1051a39Sopenharmony_ci # Tag length 54e1051a39Sopenharmony_ci } elsif ($1 eq "Tlen") { 55e1051a39Sopenharmony_ci $taglen = $2; 56e1051a39Sopenharmony_ci } 57e1051a39Sopenharmony_ci } 58e1051a39Sopenharmony_ci # Key/Value pair 59e1051a39Sopenharmony_ci } elsif (/^\s*(\w+)\s*=\s*(\S.*)\r/) { 60e1051a39Sopenharmony_ci if ($1 eq "Key") { 61e1051a39Sopenharmony_ci $key = $2; 62e1051a39Sopenharmony_ci } elsif ($1 eq "Nonce") { 63e1051a39Sopenharmony_ci $iv = $2; 64e1051a39Sopenharmony_ci if ($intest == 0) { 65e1051a39Sopenharmony_ci $fixediv = 1; 66e1051a39Sopenharmony_ci } else { 67e1051a39Sopenharmony_ci $fixediv = 0; 68e1051a39Sopenharmony_ci } 69e1051a39Sopenharmony_ci } elsif ($1 eq "Adata") { 70e1051a39Sopenharmony_ci $aad = $2; 71e1051a39Sopenharmony_ci } elsif ($1 eq "CT") { 72e1051a39Sopenharmony_ci $ct = substr($2, 0, length($2) - ($taglen * 2)); 73e1051a39Sopenharmony_ci $tag = substr($2, $taglen * -2); 74e1051a39Sopenharmony_ci } elsif ($1 eq "Payload") { 75e1051a39Sopenharmony_ci $pt = $2; 76e1051a39Sopenharmony_ci } elsif ($1 eq "Result") { 77e1051a39Sopenharmony_ci if ($2 =~ /Fail/) { 78e1051a39Sopenharmony_ci $res = "CIPHERUPDATE_ERROR"; 79e1051a39Sopenharmony_ci } 80e1051a39Sopenharmony_ci } elsif ($1 eq "Count") { 81e1051a39Sopenharmony_ci $intest = 1; 82e1051a39Sopenharmony_ci } elsif ($1 eq "Plen") { 83e1051a39Sopenharmony_ci $ptlen = $2; 84e1051a39Sopenharmony_ci } elsif ($1 eq "Tlen") { 85e1051a39Sopenharmony_ci $taglen = $2; 86e1051a39Sopenharmony_ci } elsif ($1 eq "Alen") { 87e1051a39Sopenharmony_ci $aadlen = $2; 88e1051a39Sopenharmony_ci } 89e1051a39Sopenharmony_ci # Something else - probably just a blank line 90e1051a39Sopenharmony_ci } elsif ($intest) { 91e1051a39Sopenharmony_ci print "Cipher = $alg-$keylen-$mode\n"; 92e1051a39Sopenharmony_ci print "Key = $key\n"; 93e1051a39Sopenharmony_ci print "IV = $iv\n"; 94e1051a39Sopenharmony_ci print "AAD ="; 95e1051a39Sopenharmony_ci if ($aadlen > 0) { 96e1051a39Sopenharmony_ci print " $aad"; 97e1051a39Sopenharmony_ci } 98e1051a39Sopenharmony_ci print "\nTag ="; 99e1051a39Sopenharmony_ci if ($taglen > 0) { 100e1051a39Sopenharmony_ci print " $tag"; 101e1051a39Sopenharmony_ci } 102e1051a39Sopenharmony_ci print "\nPlaintext ="; 103e1051a39Sopenharmony_ci if ($ptlen > 0) { 104e1051a39Sopenharmony_ci print " $pt"; 105e1051a39Sopenharmony_ci } 106e1051a39Sopenharmony_ci print "\nCiphertext = $ct\n"; 107e1051a39Sopenharmony_ci if ($res ne "") { 108e1051a39Sopenharmony_ci print "Operation = DECRYPT\n"; 109e1051a39Sopenharmony_ci print "Result = $res\n"; 110e1051a39Sopenharmony_ci } 111e1051a39Sopenharmony_ci print "\n"; 112e1051a39Sopenharmony_ci $res = ""; 113e1051a39Sopenharmony_ci if ($fixediv == 0) { 114e1051a39Sopenharmony_ci $iv = ""; 115e1051a39Sopenharmony_ci } 116e1051a39Sopenharmony_ci $aad = ""; 117e1051a39Sopenharmony_ci $tag = ""; 118e1051a39Sopenharmony_ci $pt = ""; 119e1051a39Sopenharmony_ci $intest = 0; 120e1051a39Sopenharmony_ci } 121e1051a39Sopenharmony_ci} 122