cavs-to-evptest.pl 3.1 KB

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