~ubuntu-branches/debian/squeeze/polarssl/squeeze

« back to all changes in this revision

Viewing changes to programs/pkey/rsa_sign.c

  • Committer: Package Import Robot
  • Author(s): Roland Stigge
  • Date: 2013-10-16 20:04:47 UTC
  • mfrom: (1.3.1) (3.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20131016200447-x0hfc8ysrgkjyji2
Tags: 1.2.9-1~deb6u1
* New upstream release
  - Fixes CVE-2013-5914 CVE-2013-5915 (Closes: #725359)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  RSA/SHA-1 signature creation program
3
3
 *
4
 
 *  Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
 
4
 *  Copyright (C) 2006-2011, Brainspark B.V.
 
5
 *
 
6
 *  This file is part of PolarSSL (http://www.polarssl.org)
 
7
 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
 
8
 *
5
9
 *  All rights reserved.
6
10
 *
7
 
 *  Joined copyright on original XySSL code with: Christophe Devine
8
 
 *
9
11
 *  This program is free software; you can redistribute it and/or modify
10
12
 *  it under the terms of the GNU General Public License as published by
11
13
 *  the Free Software Foundation; either version 2 of the License, or
28
30
#include <string.h>
29
31
#include <stdio.h>
30
32
 
 
33
#include "polarssl/config.h"
 
34
 
31
35
#include "polarssl/rsa.h"
32
36
#include "polarssl/sha1.h"
33
 
 
 
37
#include "polarssl/ctr_drbg.h"
 
38
#include "polarssl/entropy.h"
 
39
 
 
40
#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) ||  \
 
41
    !defined(POLARSSL_SHA1_C) || !defined(POLARSSL_FS_IO) ||    \
 
42
    !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
 
43
int main( int argc, char *argv[] )
 
44
{
 
45
    ((void) argc);
 
46
    ((void) argv);
 
47
 
 
48
    printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
 
49
           "POLARSSL_SHA1_C and/or POLARSSL_FS_IO "
 
50
           "and/or POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
 
51
           "not defined.\n");
 
52
    return( 0 );
 
53
}
 
54
#else
34
55
int main( int argc, char *argv[] )
35
56
{
36
57
    FILE *f;
37
 
    int ret, i;
 
58
    int ret;
 
59
    size_t i;
38
60
    rsa_context rsa;
 
61
    entropy_context entropy;
 
62
    ctr_drbg_context ctr_drbg;
39
63
    unsigned char hash[20];
40
 
    unsigned char buf[512];
 
64
    unsigned char buf[POLARSSL_MPI_MAX_SIZE];
 
65
    const char *pers = "rsa_decrypt";
41
66
 
42
67
    ret = 1;
43
68
 
45
70
    {
46
71
        printf( "usage: rsa_sign <filename>\n" );
47
72
 
48
 
#ifdef WIN32
 
73
#if defined(_WIN32)
49
74
        printf( "\n" );
50
75
#endif
51
76
 
52
77
        goto exit;
53
78
    }
54
79
 
 
80
    printf( "\n  . Seeding the random number generator..." );
 
81
    fflush( stdout );
 
82
 
 
83
    entropy_init( &entropy );
 
84
    if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
 
85
                               (const unsigned char *) pers,
 
86
                               strlen( pers ) ) ) != 0 )
 
87
    {
 
88
        printf( " failed\n  ! ctr_drbg_init returned %d\n", ret );
 
89
        goto exit;
 
90
    }
 
91
 
55
92
    printf( "\n  . Reading private key from rsa_priv.txt" );
56
93
    fflush( stdout );
57
94
 
63
100
        goto exit;
64
101
    }
65
102
 
66
 
    rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL );
 
103
    rsa_init( &rsa, RSA_PKCS_V15, 0 );
67
104
    
68
105
    if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
69
106
        ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
82
119
 
83
120
    fclose( f );
84
121
 
 
122
    printf( "\n  . Checking the private key" );
 
123
    fflush( stdout );
 
124
    if( ( ret = rsa_check_privkey( &rsa ) ) != 0 )
 
125
    {
 
126
        printf( " failed\n  ! rsa_check_privkey failed with -0x%0x\n", -ret );
 
127
        goto exit;
 
128
    }
 
129
 
85
130
    /*
86
131
     * Compute the SHA-1 hash of the input file,
87
132
     * then calculate the RSA signature of the hash.
95
140
        goto exit;
96
141
    }
97
142
 
98
 
    if( ( ret = rsa_pkcs1_sign( &rsa, RSA_PRIVATE, SIG_RSA_SHA1,
99
 
                                20, hash, buf ) ) != 0 )
 
143
    if( ( ret = rsa_pkcs1_sign( &rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE,
 
144
                                SIG_RSA_SHA1, 20, hash, buf ) ) != 0 )
100
145
    {
101
 
        printf( " failed\n  ! rsa_pkcs1_sign returned %d\n\n", ret );
 
146
        printf( " failed\n  ! rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
102
147
        goto exit;
103
148
    }
104
149
 
124
169
 
125
170
exit:
126
171
 
127
 
#ifdef WIN32
 
172
#if defined(_WIN32)
128
173
    printf( "  + Press Enter to exit this program.\n" );
129
174
    fflush( stdout ); getchar();
130
175
#endif
131
176
 
132
177
    return( ret );
133
178
}
 
179
#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
 
180
          POLARSSL_FS_IO */