Now works as OpenPGPcard v1

This commit is contained in:
NIIBE Yutaka
2010-08-23 16:44:02 +09:00
parent c4f1bc576b
commit 8ee8d3eb27
6 changed files with 126 additions and 11 deletions

View File

@@ -61,16 +61,15 @@ include $(CHIBIOS)/os/hal/platforms/STM32/platform.mk
include $(CHIBIOS)/os/hal/hal.mk
include $(CHIBIOS)/os/ports/GCC/ARMCMx/STM32F10x/port.mk
include $(CHIBIOS)/os/kernel/kernel.mk
include $(CHIBIOS)/test/test.mk
include stmusb.mk
include usbcdc.mk
include rsa.mk
include ../boards/OLIMEX_STM32_H103/board.mk
# C sources that can be compiled in ARM or THUMB mode depending on the global
# setting.
CSRC = $(PORTSRC) \
$(KERNSRC) \
$(TESTSRC) \
$(HALSRC) \
$(PLATFORMSRC) \
$(BOARDSRC) \
@@ -78,13 +77,14 @@ CSRC = $(PORTSRC) \
$(CHIBIOS)/os/various/syscalls.c \
$(STMUSBSRC) \
$(USBCDCSRC) \
$(RSASRC) \
main.c hw_config.c usb_lld.c usb_desc.c usb_prop.c usb-icc.c gpg.c
# List ASM source files here
ASMSRC = $(PORTASM) \
$(CHIBIOS)/os/ports/GCC/ARMCMx/STM32F10x/vectors.s gpg-data.s
INCDIR = $(STMUSBINCDIR) $(USBCDCDIR) \
INCDIR = $(RSAINCDIR) $(STMUSBINCDIR) $(USBCDCDIR) \
$(PORTINC) $(KERNINC) $(TESTINC) \
$(HALINC) $(PLATFORMINC) $(BOARDINC) \
$(CHIBIOS)/os/various

View File

@@ -1,6 +1,7 @@
extern Thread *blinker_thread;
extern void put_byte (uint8_t b);
extern void put_string (const char *s);
extern void _write (const char *, int);

View File

@@ -46,7 +46,7 @@ put_byte (uint8_t b)
_write ("\r\n", 2);
}
static void
void
put_string (const char *s)
{
_write (s, strlen (s));
@@ -406,11 +406,8 @@ INS_VERIFY
put_string (" wrong length\r\n");
else
{
#if 0
r = rsa_sign (&cmd_APDU[5]);
write_res_apdu (NULL, RSA_SIGNATURE_LENGTH, 0x90, 0x00);
#endif
write_res_apdu (NULL, 0, 0x64, 0x00);
unsigned char * r = rsa_sign (&cmd_APDU[5]);
write_res_apdu (r, RSA_SIGNATURE_LENGTH, 0x90, 0x00);
}
put_string ("done.\r\n");

View File

@@ -26,7 +26,6 @@
#include "ch.h"
#include "hal.h"
#include "test.h"
#include "usb_lld.h"
#include "usb_lib.h"
@@ -167,7 +166,7 @@ static msg_t Thread2 (void *arg)
static WORKING_AREA(waUSBthread, 128*2);
extern msg_t USBthread (void *arg);
static WORKING_AREA(waGPGthread, 128*4);
static WORKING_AREA(waGPGthread, 128*16);
extern msg_t GPGthread (void *arg);
/*

4
src/rsa.mk Normal file
View File

@@ -0,0 +1,4 @@
RSADIR = ../../polarssl-0.13.1
RSASRCDIR = $(RSADIR)/library
RSAINCDIR = $(RSADIR)/include
RSASRC = $(RSASRCDIR)/bignum.c $(RSASRCDIR)/rsa.c rsa_sign.c

114
src/rsa_sign.c Normal file
View File

@@ -0,0 +1,114 @@
#include "ch.h"
#include "gnuk.h"
#include <polarssl/config.h>
#include <polarssl/rsa.h>
static unsigned char output[256];
/* big endian */
static const unsigned char p[64] = {
0xe3,0xec,0x42,0x6c,0x23,0x37,0x1f,0x96,
0x55,0x2f,0x1d,0x2a,0x37,0xd2,0x66,0x43,
0x3c,0xb1,0xa5,0x24,0x47,0x8e,0x29,0x86,
0xee,0xec,0xa9,0x86,0x1e,0x05,0xfb,0xfa,
0x00,0x0e,0x69,0xbc,0x60,0x47,0x1c,0x00,
0x96,0xdc,0x30,0x00,0x05,0xfb,0xe0,0x00,
0xae,0x05,0x38,0x38,0xf9,0x7e,0xa6,0xae,
0xf6,0x90,0x68,0x48,0xf5,0x23,0x7f,0x3d,
};
static const unsigned char q[64] = {
0xf2,0x8c,0xa0,0x24,0x38,0x7c,0x6e,0x66,
0x9c,0x39,0xe9,0x4d,0x2b,0xe3,0x94,0xd1,
0x93,0x3f,0x17,0xcb,0x3f,0x4e,0x8d,0xa4,
0xdd,0x1f,0xee,0x00,0x7d,0x8c,0x5e,0xbb,
0xc7,0x80,0x1a,0xf5,0x79,0x80,0x63,0x5a,
0xc7,0xa2,0xb7,0x92,0x75,0x26,0xda,0xd4,
0xc9,0x9f,0x83,0x58,0x81,0x0a,0x53,0xa3,
0xe4,0x0b,0xd1,0xde,0xe2,0x33,0xf2,0x9b,
};
/* dq = 1/e mod (p - 1) */
static const unsigned char dp[64] = {
0x44,0x60,0x63,0x88,0xe7,0x83,0xa9,0x01,
0x43,0xaa,0xef,0xb7,0xf3,0xa9,0x36,0x2e,
0xa3,0xad,0xb3,0x9a,0x4f,0xb1,0x31,0x7a,
0x1e,0x81,0x77,0x56,0x19,0xf9,0xb7,0x9f,
0x47,0x97,0x0b,0x57,0x8d,0x16,0xa8,0x2a,
0xf1,0x17,0x3b,0x7b,0x36,0x50,0x5a,0xe2,
0x39,0x52,0xad,0x8c,0x42,0x25,0x21,0xc2,
0x49,0x8c,0x07,0x0d,0x24,0x25,0xb3,0x0d,
};
/* dq = 1/e mod (q - 1) */
static const unsigned char dq[64] = {
0x6c,0xbe,0xbd,0xe1,0xdf,0x6d,0x72,0x76,
0x10,0xdf,0x2a,0x09,0x8e,0x7e,0xe7,0xaf,
0x1a,0xcc,0x73,0xd7,0x8d,0x57,0xa3,0xcb,
0xe1,0xd4,0x39,0xfa,0x00,0x50,0x19,0xfd,
0x28,0xd1,0xf7,0xc4,0x4d,0x28,0x51,0xe3,
0x56,0x94,0x4e,0xa7,0x9c,0x5c,0xc2,0x4a,
0x1a,0xe7,0xa8,0x94,0xda,0xae,0x0d,0x97,
0xf2,0x5b,0x96,0x49,0xce,0xb3,0x29,0xb7,
};
/* q_inv = 1/q mod p */
static const unsigned char q_inv[64] = {
0x19,0x77,0x6a,0xc8,0xd7,0x2b,0x9f,0x82,
0x2d,0xbd,0x05,0x89,0xd8,0x0f,0x1e,0xb6,
0x19,0x82,0x58,0x2f,0x7f,0xfd,0xee,0x50,
0x00,0x75,0x85,0x69,0x64,0x37,0x32,0xa0,
0x62,0x55,0x5b,0x13,0x71,0x9e,0x4e,0x1f,
0x93,0x4c,0xa2,0x77,0x6c,0x70,0x07,0xa6,
0xfb,0xd5,0x4e,0x94,0xab,0xbd,0xa0,0x0e,
0x0d,0x42,0x07,0x9d,0xbb,0x5d,0xbc,0x75,
};
unsigned char *
rsa_sign (unsigned char *raw_message)
{
rsa_context ctx;
mpi P1, Q1, H;
mpi_init( &P1, &Q1, &H, NULL );
rsa_init( &ctx, RSA_PKCS_V15, 0, NULL, NULL );
#if 0
ctx.len = 2048 / 8;
#else
ctx.len = 1024 / 8;
mpi_read_string( &ctx.E, 16, "10001" );
mpi_read_binary (&ctx.P, p, ctx.len / 2);
mpi_read_binary (&ctx.Q, q, ctx.len / 2);
mpi_mul_mpi( &ctx.N, &ctx.P, &ctx.Q );
mpi_sub_int( &P1, &ctx.P, 1 );
mpi_sub_int( &Q1, &ctx.Q, 1 );
mpi_mul_mpi( &H, &P1, &Q1 );
mpi_inv_mod( &ctx.D , &ctx.E, &H );
mpi_read_binary (&ctx.DP, dp, ctx.len / 2);
mpi_read_binary (&ctx.DQ, dq, ctx.len / 2);
mpi_read_binary (&ctx.QP, q_inv, ctx.len / 2);
#endif
put_string ("RSA...");
{
int r;
if ((r = rsa_check_privkey( &ctx )) == 0)
put_string ("ok...");
else
{
put_string ("failed.\r\n");
put_byte (r);
return output;
}
}
#if 0
rsa_pkcs1_sign( &ctx, RSA_PRIVATE, SIG_RSA_SHA256, 0, raw_message, output);
#else
rsa_pkcs1_sign( &ctx, RSA_PRIVATE, SIG_RSA_RAW, 35, raw_message, output );
put_string ("done.\r\n");
return output;
#endif
}