1#!/usr/bin/env perl
2#
3# Copyright The Mbed TLS Contributors
4# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18use strict;
19
20my $file = shift;
21
22open(TEST_DATA, "$file") or die "Opening test cases '$file': $!";
23
24sub get_val($$)
25{
26    my $str = shift;
27    my $name = shift;
28    my $val = "";
29
30    while(my $line = <TEST_DATA>)
31    {
32        next if($line !~ /^# $str/);
33        last;
34    }
35
36    while(my $line = <TEST_DATA>)
37    {
38        last if($line eq "\r\n");
39        $val .= $line;
40    }
41
42    $val =~ s/[ \r\n]//g;
43
44    return $val;
45}
46
47my $state = 0;
48my $val_n = "";
49my $val_e = "";
50my $val_p = "";
51my $val_q = "";
52my $mod = 0;
53my $cnt = 1;
54while (my $line = <TEST_DATA>)
55{
56    next if ($line !~ /^# Example/);
57
58    ( $mod ) = ($line =~ /A (\d+)/);
59    $val_n = get_val("RSA modulus n", "N");
60    $val_e = get_val("RSA public exponent e", "E");
61    $val_p = get_val("Prime p", "P");
62    $val_q = get_val("Prime q", "Q");
63
64    for(my $i = 1; $i <= 6; $i++)
65    {
66        my $val_m = get_val("Message to be", "M");
67        my $val_salt = get_val("Salt", "Salt");
68        my $val_sig = get_val("Signature", "Sig");
69
70        print("RSASSA-PSS Signature Example ${cnt}_${i}\n");
71        print("pkcs1_rsassa_pss_sign:$mod:16:\"$val_p\":16:\"$val_q\":16:\"$val_n\":16:\"$val_e\":SIG_RSA_SHA1:MBEDTLS_MD_SHA1");
72        print(":\"$val_m\"");
73        print(":\"$val_salt\"");
74        print(":\"$val_sig\":0");
75        print("\n\n");
76
77        print("RSASSA-PSS Signature Example ${cnt}_${i} (verify)\n");
78        print("pkcs1_rsassa_pss_verify:$mod:16:\"$val_n\":16:\"$val_e\":SIG_RSA_SHA1:MBEDTLS_MD_SHA1");
79        print(":\"$val_m\"");
80        print(":\"$val_salt\"");
81        print(":\"$val_sig\":0");
82        print("\n\n");
83    }
84    $cnt++;
85}
86close(TEST_DATA);
87