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