os/security/crypto/weakcryptospi/test/tasymmetric/script_gen/rsa_test.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tasymmetric/script_gen/rsa_test.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,236 @@
     1.4 +/*
     1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* Generates RSA test vectors.
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +
    1.24 +
    1.25 +/**
    1.26 + @file
    1.27 +*/
    1.28 +
    1.29 +#include <stdio.h>
    1.30 +#include <string.h>
    1.31 +
    1.32 +#include "openssl/e_os.h"
    1.33 +
    1.34 +#include <openssl/crypto.h>
    1.35 +#include <openssl/rsa.h>
    1.36 +
    1.37 +#include "utils.h"
    1.38 +#include "keys.h"
    1.39 +
    1.40 +void printPublicKey(RSA* key)
    1.41 +    {    
    1.42 +    printf("\t\t<modulus>");
    1.43 +    printBN(key->n);
    1.44 +    printf("</modulus>\n");
    1.45 +    printf("\t\t<publicExponent>");
    1.46 +    printBN(key->e);
    1.47 +    printf("</publicExponent>\n");
    1.48 +    }
    1.49 +
    1.50 +void printPrivateKey(RSA* key)
    1.51 +    {    
    1.52 +    printf("\t\t<modulus>");
    1.53 +    printBN(key->n);
    1.54 +    printf("</modulus>\n");
    1.55 +    printf("\t\t<privateExponent>");
    1.56 +    printBN(key->d);
    1.57 +    printf("</privateExponent>\n");
    1.58 +    }
    1.59 +
    1.60 +/**
    1.61 + * Generate encrypt and decrypt vectors for a plaintext.
    1.62 + */
    1.63 +
    1.64 +static void generateEncryptionVector(RSA* key, unsigned char* ptext_ex, int plen, BOOL passes)
    1.65 +    {
    1.66 +    unsigned char ctext[RSA_size(key)];
    1.67 +    int num;
    1.68 +
    1.69 +    setOurRandom();
    1.70 +	num = RSA_public_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_PADDING);
    1.71 +    if (num == -1)
    1.72 +        processError();
    1.73 +
    1.74 +    if (!passes)
    1.75 +        scramble(ctext, num);
    1.76 +
    1.77 +    printActionHeader("RSA test vector", "RSAEncryptVector");
    1.78 +    printPublicKey(key);
    1.79 +    printHexElement("plaintext", ptext_ex, plen);
    1.80 +    printHexElement("ciphertext", ctext, num);
    1.81 +    printActionFooter(passes);
    1.82 +
    1.83 +    printActionHeader("RSA test vector", "RSADecryptVector");
    1.84 +    printPrivateKey(key);
    1.85 +    printHexElement("ciphertext", ctext, num);
    1.86 +    printHexElement("plaintext", ptext_ex, plen);
    1.87 +    printActionFooter(passes);
    1.88 +    }
    1.89 +
    1.90 +/**
    1.91 + * Sign a digest - the digest is unformatted, ie we're not dealing with
    1.92 + * algotrithm identifiers here.
    1.93 + */
    1.94 +
    1.95 +static void generateSignatureVector(RSA* key, unsigned char* ptext_ex, int plen, BOOL passes)
    1.96 +    {
    1.97 +    unsigned char ctext[RSA_size(key)];
    1.98 +    int num;
    1.99 +
   1.100 +	num = RSA_private_encrypt(plen, ptext_ex, ctext, key, RSA_PKCS1_PADDING);
   1.101 +    if (num == -1)
   1.102 +        processError();
   1.103 +
   1.104 +    if (!passes)
   1.105 +        scramble(ctext, num);
   1.106 +
   1.107 +    printActionHeader("RSA test vector", "RSASignVector");
   1.108 +    printPrivateKey(key);
   1.109 +    printHexElement("digestInfo", ptext_ex, plen);
   1.110 +    printHexElement("signature", ctext, num);
   1.111 +    printActionFooter(passes);
   1.112 +
   1.113 +    printActionHeader("RSA test vector", "RSAVerifyVector");
   1.114 +    printPublicKey(key);
   1.115 +    printHexElement("digestInfo", ptext_ex, plen);
   1.116 +    printHexElement("signature", ctext, num);
   1.117 +    printActionFooter(passes);
   1.118 +    }
   1.119 +
   1.120 +/* Plaintext from openssl test code. */
   1.121 +static unsigned char ptext1[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
   1.122 +static int plen1 = sizeof(ptext1) - 1;
   1.123 +
   1.124 +/* 16 byte random plaintext. */
   1.125 +static unsigned char ptext2[] =
   1.126 +        "\x47\xab\x92\x76\x09\xfd\x75\xa7\xe2\x08\x85\xeb\x7e\x4c\xff\x0a";
   1.127 +static int plen2 = sizeof(ptext2) - 1;
   1.128 +
   1.129 +/* 32 byte random plaintext. */
   1.130 +static unsigned char ptext3[] =
   1.131 +        "\x0b\x0a\x7c\xeb\x6c\x17\x45\x53\x1d\xa7\x24\xad\x43\x8b\xf7\x46"
   1.132 +        "\x89\xc3\x9f\x09\x5e\x88\x3e\xd8\x8e\x04\x36\x38\x49\xc0\x0f\x41";
   1.133 +static int plen3 = sizeof(ptext3) - 1;
   1.134 +
   1.135 +/* One byte plaintext. */
   1.136 +static unsigned char short_ptext[] = "\x23";
   1.137 +static int short_plen = sizeof(short_ptext) - 1;
   1.138 +
   1.139 +/* Longest possible plaintexts, one for each key. */
   1.140 +static unsigned char long_ptext1[] =
   1.141 +        "\x66\x79\xf3\x84\x82\x06\x99\x06\xcd\xf1\xdf\x3f\xdd\xb5\x37\x74"
   1.142 +        "\x46\x76\xba\x0d\xb8\xd6\x82\xb6\x82\x6f\x31\xb1\xd8\x23\x0c\xca"
   1.143 +        "\x4e\x39\x28\x77\x05\x3f\xac\x5a\x13\xff\x3a\x39\x35\x2e\xaf\xb1"
   1.144 +        "\x85\xe4\xd0\x60\xf4";
   1.145 +
   1.146 +static int long_plen1 = sizeof(long_ptext1) - 1;
   1.147 +
   1.148 +static unsigned char long_ptext2[] =
   1.149 +        "\xcd\xa2\x2c\x4b\x6a\x20\x00\x0e\xad\xad\x74\xbd\xb3\x04\xbd\xc5"
   1.150 +        "\x72\x73\x02\x11\x9d\x6d\x37\x75\x66\x5a\xf2\xe6\x47\x65\x79\x80"
   1.151 +        "\x7c\x92\xec\x09\xf5\x33\xea";
   1.152 +
   1.153 +static int long_plen2 = sizeof(long_ptext2) - 1;
   1.154 +
   1.155 +static unsigned char long_ptext3[] =
   1.156 +        "\x0e\x25\x61\xaf\x55\xeb\x9c\x10\x90\x4f\xd4\x27\xfd\x0d\x1d\xf4"
   1.157 +        "\x38\xbd\x9e\xd0\xc7\x1c\x48\x0b\x50\xa1\xd3\xf1\xb4\xdb\xba\x2d"
   1.158 +        "\x00\x81\x59\x6e\x61\x43\x35\x50\xf9\x5f\x70\x20\xb2\x47\x48\x7f"
   1.159 +        "\x32\xf7\xe8\x2e\x50\xc1\x80\x45\x4b\x5c\xf8\x45\x6a\xa0\x0f\x33"
   1.160 +        "\xf1\xec\x9a\xb1\x79\xf5\xcc\x92\x1c\x30\x12\xb0\x55\x7b\x49\x06"
   1.161 +        "\x93\xa8\x30\x5a\x68\x79\x8a\x21\x9a\xd7\x68\x70\xf8\xa1\xf1\x0a"
   1.162 +        "\x52\x85\x75\xf9\x2d\x26\xd3\x1b\x37\xdc\xdc\x60\x87\x77\xcb\x97"
   1.163 +        "\x57\x00\x4f\xf1\x81";
   1.164 +
   1.165 +static int long_plen3 = sizeof(long_ptext3) - 1;
   1.166 +
   1.167 +int main(int argc, char *argv[])
   1.168 +    {
   1.169 +    initKeys();
   1.170 +
   1.171 +    setOurRandom();
   1.172 +    testOurRandom();
   1.173 +
   1.174 +    /** Public encryption: */
   1.175 +
   1.176 +    /** Encrypt openssl test plaintext with each key. */
   1.177 +    generateEncryptionVector(key1, ptext1, plen1, TRUE);
   1.178 +    generateEncryptionVector(key2, ptext1, plen1, TRUE);
   1.179 +    generateEncryptionVector(key3, ptext1, plen1, TRUE);
   1.180 +
   1.181 +    /** Encrypt 16 byte test plaintext with each key. */
   1.182 +    generateEncryptionVector(key1, ptext2, plen2, TRUE);
   1.183 +    generateEncryptionVector(key2, ptext2, plen2, TRUE);
   1.184 +    generateEncryptionVector(key3, ptext2, plen2, TRUE);
   1.185 +
   1.186 +    /** Encrypt 32 byte test plaintext with each key. */
   1.187 +    generateEncryptionVector(key1, ptext3, plen3, TRUE);
   1.188 +    generateEncryptionVector(key2, ptext3, plen3, TRUE);
   1.189 +    generateEncryptionVector(key3, ptext3, plen3, TRUE);
   1.190 +
   1.191 +    /** Encypt one byte plaintext with each key. */
   1.192 +    generateEncryptionVector(key1, short_ptext, short_plen, TRUE);
   1.193 +    generateEncryptionVector(key2, short_ptext, short_plen, TRUE);
   1.194 +    generateEncryptionVector(key3, short_ptext, short_plen, TRUE);
   1.195 +
   1.196 +    /** Encrypt longest possible plaintext for each key. */
   1.197 +    generateEncryptionVector(key1, long_ptext1, long_plen1, TRUE);
   1.198 +    generateEncryptionVector(key2, long_ptext2, long_plen2, TRUE);
   1.199 +    generateEncryptionVector(key3, long_ptext3, long_plen3, TRUE);
   1.200 +
   1.201 +    /** Negative encryption vectors. */
   1.202 +    generateEncryptionVector(key1, ptext1, plen1, FALSE);
   1.203 +    generateEncryptionVector(key2, ptext1, plen1, FALSE);
   1.204 +    generateEncryptionVector(key3, ptext1, plen1, FALSE);
   1.205 +
   1.206 +    /** Signing: */
   1.207 +
   1.208 +    /** Sign openssl test plaintext with each key. */
   1.209 +    generateSignatureVector(key1, ptext1, plen1, TRUE);
   1.210 +    generateSignatureVector(key2, ptext1, plen1, TRUE);
   1.211 +    generateSignatureVector(key3, ptext1, plen1, TRUE);
   1.212 +
   1.213 +    /** Sign 16 byte digest with each key. */
   1.214 +    generateSignatureVector(key1, ptext2, plen2, TRUE);
   1.215 +    generateSignatureVector(key2, ptext2, plen2, TRUE);
   1.216 +    generateSignatureVector(key3, ptext2, plen2, TRUE);
   1.217 +
   1.218 +    /** Sign 32 byte digest with each key. */
   1.219 +    generateSignatureVector(key1, ptext3, plen3, TRUE);
   1.220 +    generateSignatureVector(key2, ptext3, plen3, TRUE);
   1.221 +    generateSignatureVector(key3, ptext3, plen3, TRUE);
   1.222 +
   1.223 +    /** Sign one byte digest with each key. */
   1.224 +    generateSignatureVector(key1, short_ptext, short_plen, TRUE);
   1.225 +    generateSignatureVector(key2, short_ptext, short_plen, TRUE);
   1.226 +    generateSignatureVector(key3, short_ptext, short_plen, TRUE);
   1.227 +
   1.228 +    /** Sign longest possible digests for each key. */
   1.229 +    generateSignatureVector(key1, long_ptext1, long_plen1, TRUE);
   1.230 +    generateSignatureVector(key2, long_ptext2, long_plen2, TRUE);
   1.231 +    generateSignatureVector(key3, long_ptext3, long_plen3, TRUE);
   1.232 +
   1.233 +    /** Negative signature vectors. */
   1.234 +    generateSignatureVector(key1, ptext1, plen1, FALSE);
   1.235 +    generateSignatureVector(key2, ptext1, plen1, FALSE);
   1.236 +    generateSignatureVector(key3, ptext1, plen1, FALSE);
   1.237 +
   1.238 +    return 0;
   1.239 +    }