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