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