crypto/weakcrypto/test/tasymmetric/script_gen/rsa_test.c
changeset 0 2c201484c85f
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Generates RSA test vectors.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 /**
       
    26  @file
       
    27 */
       
    28 
       
    29 #include <stdio.h>
       
    30 #include <string.h>
       
    31 
       
    32 #include "openssl/e_os.h"
       
    33 
       
    34 #include <openssl/crypto.h>
       
    35 #include <openssl/rsa.h>
       
    36 
       
    37 #include "utils.h"
       
    38 #include "keys.h"
       
    39 
       
    40 void printPublicKey(RSA* key)
       
    41     {    
       
    42     printf("\t\t<modulus>");
       
    43     printBN(key->n);
       
    44     printf("</modulus>\n");
       
    45     printf("\t\t<publicExponent>");
       
    46     printBN(key->e);
       
    47     printf("</publicExponent>\n");
       
    48     }
       
    49 
       
    50 void printPrivateKey(RSA* key)
       
    51     {    
       
    52     printf("\t\t<modulus>");
       
    53     printBN(key->n);
       
    54     printf("</modulus>\n");
       
    55     printf("\t\t<privateExponent>");
       
    56     printBN(key->d);
       
    57     printf("</privateExponent>\n");
       
    58     }
       
    59 
       
    60 /**
       
    61  * Generate encrypt and decrypt vectors for a plaintext.
       
    62  */
       
    63 
       
    64 static void generateEncryptionVector(RSA* key, unsigned char* ptext_ex, int plen, BOOL passes)
       
    65     {
       
    66     unsigned char ctext[RSA_size(key)];
       
    67     int num;
       
    68 
       
    69     setOurRandom();
       
    70 	num = RSA_public_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_PADDING);
       
    71     if (num == -1)
       
    72         processError();
       
    73 
       
    74     if (!passes)
       
    75         scramble(ctext, num);
       
    76 
       
    77     printActionHeader("RSA test vector", "RSAEncryptVector");
       
    78     printPublicKey(key);
       
    79     printHexElement("plaintext", ptext_ex, plen);
       
    80     printHexElement("ciphertext", ctext, num);
       
    81     printActionFooter(passes);
       
    82 
       
    83     printActionHeader("RSA test vector", "RSADecryptVector");
       
    84     printPrivateKey(key);
       
    85     printHexElement("ciphertext", ctext, num);
       
    86     printHexElement("plaintext", ptext_ex, plen);
       
    87     printActionFooter(passes);
       
    88     }
       
    89 
       
    90 /**
       
    91  * Sign a digest - the digest is unformatted, ie we're not dealing with
       
    92  * algotrithm identifiers here.
       
    93  */
       
    94 
       
    95 static void generateSignatureVector(RSA* key, unsigned char* ptext_ex, int plen, BOOL passes)
       
    96     {
       
    97     unsigned char ctext[RSA_size(key)];
       
    98     int num;
       
    99 
       
   100 	num = RSA_private_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_PADDING);
       
   101     if (num == -1)
       
   102         processError();
       
   103 
       
   104     if (!passes)
       
   105         scramble(ctext, num);
       
   106 
       
   107     printActionHeader("RSA test vector", "RSASignVector");
       
   108     printPrivateKey(key);
       
   109     printHexElement("digestInfo", ptext_ex, plen);
       
   110     printHexElement("signature", ctext, num);
       
   111     printActionFooter(passes);
       
   112 
       
   113     printActionHeader("RSA test vector", "RSAVerifyVector");
       
   114     printPublicKey(key);
       
   115     printHexElement("digestInfo", ptext_ex, plen);
       
   116     printHexElement("signature", ctext, num);
       
   117     printActionFooter(passes);
       
   118     }
       
   119 
       
   120 /* Plaintext from openssl test code. */
       
   121 static unsigned char ptext1[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
       
   122 static int plen1 = sizeof(ptext1) - 1;
       
   123 
       
   124 /* 16 byte random plaintext. */
       
   125 static unsigned char ptext2[] =
       
   126         "\x47\xab\x92\x76\x09\xfd\x75\xa7\xe2\x08\x85\xeb\x7e\x4c\xff\x0a";
       
   127 static int plen2 = sizeof(ptext2) - 1;
       
   128 
       
   129 /* 32 byte random plaintext. */
       
   130 static unsigned char ptext3[] =
       
   131         "\x0b\x0a\x7c\xeb\x6c\x17\x45\x53\x1d\xa7\x24\xad\x43\x8b\xf7\x46"
       
   132         "\x89\xc3\x9f\x09\x5e\x88\x3e\xd8\x8e\x04\x36\x38\x49\xc0\x0f\x41";
       
   133 static int plen3 = sizeof(ptext3) - 1;
       
   134 
       
   135 /* One byte plaintext. */
       
   136 static unsigned char short_ptext[] = "\x23";
       
   137 static int short_plen = sizeof(short_ptext) - 1;
       
   138 
       
   139 /* Longest possible plaintexts, one for each key. */
       
   140 static unsigned char long_ptext1[] =
       
   141         "\x66\x79\xf3\x84\x82\x06\x99\x06\xcd\xf1\xdf\x3f\xdd\xb5\x37\x74"
       
   142         "\x46\x76\xba\x0d\xb8\xd6\x82\xb6\x82\x6f\x31\xb1\xd8\x23\x0c\xca"
       
   143         "\x4e\x39\x28\x77\x05\x3f\xac\x5a\x13\xff\x3a\x39\x35\x2e\xaf\xb1"
       
   144         "\x85\xe4\xd0\x60\xf4";
       
   145 
       
   146 static int long_plen1 = sizeof(long_ptext1) - 1;
       
   147 
       
   148 static unsigned char long_ptext2[] =
       
   149         "\xcd\xa2\x2c\x4b\x6a\x20\x00\x0e\xad\xad\x74\xbd\xb3\x04\xbd\xc5"
       
   150         "\x72\x73\x02\x11\x9d\x6d\x37\x75\x66\x5a\xf2\xe6\x47\x65\x79\x80"
       
   151         "\x7c\x92\xec\x09\xf5\x33\xea";
       
   152 
       
   153 static int long_plen2 = sizeof(long_ptext2) - 1;
       
   154 
       
   155 static unsigned char long_ptext3[] =
       
   156         "\x0e\x25\x61\xaf\x55\xeb\x9c\x10\x90\x4f\xd4\x27\xfd\x0d\x1d\xf4"
       
   157         "\x38\xbd\x9e\xd0\xc7\x1c\x48\x0b\x50\xa1\xd3\xf1\xb4\xdb\xba\x2d"
       
   158         "\x00\x81\x59\x6e\x61\x43\x35\x50\xf9\x5f\x70\x20\xb2\x47\x48\x7f"
       
   159         "\x32\xf7\xe8\x2e\x50\xc1\x80\x45\x4b\x5c\xf8\x45\x6a\xa0\x0f\x33"
       
   160         "\xf1\xec\x9a\xb1\x79\xf5\xcc\x92\x1c\x30\x12\xb0\x55\x7b\x49\x06"
       
   161         "\x93\xa8\x30\x5a\x68\x79\x8a\x21\x9a\xd7\x68\x70\xf8\xa1\xf1\x0a"
       
   162         "\x52\x85\x75\xf9\x2d\x26\xd3\x1b\x37\xdc\xdc\x60\x87\x77\xcb\x97"
       
   163         "\x57\x00\x4f\xf1\x81";
       
   164 
       
   165 static int long_plen3 = sizeof(long_ptext3) - 1;
       
   166 
       
   167 int main(int argc, char *argv[])
       
   168     {
       
   169     initKeys();
       
   170 
       
   171     setOurRandom();
       
   172     testOurRandom();
       
   173 
       
   174     /** Public encryption: */
       
   175 
       
   176     /** Encrypt openssl test plaintext with each key. */
       
   177     generateEncryptionVector(key1, ptext1, plen1, TRUE);
       
   178     generateEncryptionVector(key2, ptext1, plen1, TRUE);
       
   179     generateEncryptionVector(key3, ptext1, plen1, TRUE);
       
   180 
       
   181     /** Encrypt 16 byte test plaintext with each key. */
       
   182     generateEncryptionVector(key1, ptext2, plen2, TRUE);
       
   183     generateEncryptionVector(key2, ptext2, plen2, TRUE);
       
   184     generateEncryptionVector(key3, ptext2, plen2, TRUE);
       
   185 
       
   186     /** Encrypt 32 byte test plaintext with each key. */
       
   187     generateEncryptionVector(key1, ptext3, plen3, TRUE);
       
   188     generateEncryptionVector(key2, ptext3, plen3, TRUE);
       
   189     generateEncryptionVector(key3, ptext3, plen3, TRUE);
       
   190 
       
   191     /** Encypt one byte plaintext with each key. */
       
   192     generateEncryptionVector(key1, short_ptext, short_plen, TRUE);
       
   193     generateEncryptionVector(key2, short_ptext, short_plen, TRUE);
       
   194     generateEncryptionVector(key3, short_ptext, short_plen, TRUE);
       
   195 
       
   196     /** Encrypt longest possible plaintext for each key. */
       
   197     generateEncryptionVector(key1, long_ptext1, long_plen1, TRUE);
       
   198     generateEncryptionVector(key2, long_ptext2, long_plen2, TRUE);
       
   199     generateEncryptionVector(key3, long_ptext3, long_plen3, TRUE);
       
   200 
       
   201     /** Negative encryption vectors. */
       
   202     generateEncryptionVector(key1, ptext1, plen1, FALSE);
       
   203     generateEncryptionVector(key2, ptext1, plen1, FALSE);
       
   204     generateEncryptionVector(key3, ptext1, plen1, FALSE);
       
   205 
       
   206     /** Signing: */
       
   207 
       
   208     /** Sign openssl test plaintext with each key. */
       
   209     generateSignatureVector(key1, ptext1, plen1, TRUE);
       
   210     generateSignatureVector(key2, ptext1, plen1, TRUE);
       
   211     generateSignatureVector(key3, ptext1, plen1, TRUE);
       
   212 
       
   213     /** Sign 16 byte digest with each key. */
       
   214     generateSignatureVector(key1, ptext2, plen2, TRUE);
       
   215     generateSignatureVector(key2, ptext2, plen2, TRUE);
       
   216     generateSignatureVector(key3, ptext2, plen2, TRUE);
       
   217 
       
   218     /** Sign 32 byte digest with each key. */
       
   219     generateSignatureVector(key1, ptext3, plen3, TRUE);
       
   220     generateSignatureVector(key2, ptext3, plen3, TRUE);
       
   221     generateSignatureVector(key3, ptext3, plen3, TRUE);
       
   222 
       
   223     /** Sign one byte digest with each key. */
       
   224     generateSignatureVector(key1, short_ptext, short_plen, TRUE);
       
   225     generateSignatureVector(key2, short_ptext, short_plen, TRUE);
       
   226     generateSignatureVector(key3, short_ptext, short_plen, TRUE);
       
   227 
       
   228     /** Sign longest possible digests for each key. */
       
   229     generateSignatureVector(key1, long_ptext1, long_plen1, TRUE);
       
   230     generateSignatureVector(key2, long_ptext2, long_plen2, TRUE);
       
   231     generateSignatureVector(key3, long_ptext3, long_plen3, TRUE);
       
   232 
       
   233     /** Negative signature vectors. */
       
   234     generateSignatureVector(key1, ptext1, plen1, FALSE);
       
   235     generateSignatureVector(key2, ptext1, plen1, FALSE);
       
   236     generateSignatureVector(key3, ptext1, plen1, FALSE);
       
   237 
       
   238     return 0;
       
   239     }