From a53a5b1a38f599012955e463329207b3258ee296 Mon Sep 17 00:00:00 2001 From: Sean Parkinson Date: Wed, 7 Nov 2018 09:30:35 +1000 Subject: [PATCH] Add simple RSA sign and verify examples --- .gitignore | 6 + signature/README.md | 16 +++ signature/rsa_buffer/Makefile | 36 ++++++ signature/rsa_buffer/README | 28 +++++ signature/rsa_buffer/rsa_priv_2048.h | 151 +++++++++++++++++++++++ signature/rsa_buffer/rsa_pub_2048.h | 68 +++++++++++ signature/rsa_buffer/sign.c | 176 +++++++++++++++++++++++++++ signature/rsa_buffer/sign_vfy.sh | 10 ++ signature/rsa_buffer/signature.h | 40 ++++++ signature/rsa_buffer/verify.c | 116 ++++++++++++++++++ 10 files changed, 647 insertions(+) create mode 100644 signature/rsa_buffer/Makefile create mode 100644 signature/rsa_buffer/README create mode 100644 signature/rsa_buffer/rsa_priv_2048.h create mode 100644 signature/rsa_buffer/rsa_pub_2048.h create mode 100644 signature/rsa_buffer/sign.c create mode 100755 signature/rsa_buffer/sign_vfy.sh create mode 100644 signature/rsa_buffer/signature.h create mode 100644 signature/rsa_buffer/verify.c diff --git a/.gitignore b/.gitignore index 876cc717..a3105355 100644 --- a/.gitignore +++ b/.gitignore @@ -100,6 +100,8 @@ btle/ecc-server signature/sigtest/wolfsigtest signature/sigtest/opensigtest signature/firmware_sign/eccsign +signature/rsa_buffer/sign +signature/rsa_buffer/verify ecc/ecc-key-decode ecc/ecc-sign @@ -109,3 +111,7 @@ pkcs7/pkcs7-verify *.dSYM certmanager/certloadverifybuffer certmanager/certverify + +rsa/sign +rsa/verify + diff --git a/signature/README.md b/signature/README.md index 487d19db..a5ea72c7 100644 --- a/signature/README.md +++ b/signature/README.md @@ -37,4 +37,20 @@ formatted files. importing the public and private keys. App will then sign a msg with the private key and verify that signature using the public key +------------------ UPDATE ----------------- +November 13 2018: + +Added rsa_buffer directory + +rsa_buffer directory contains: + +1. App "sign.c" to generate a signature using a DER encoded private key into a + buffer. Prints out a hex encoding of signature. + +2. App "verify.c" to verify a signature using a DER encoded public key in and + binary signature in buffers. + +3. Script sign_vfy.sh builds sign, creates a signature with "sign", builds + verify with message and signature generated and verifies the signature with + "verify". diff --git a/signature/rsa_buffer/Makefile b/signature/rsa_buffer/Makefile new file mode 100644 index 00000000..10d68236 --- /dev/null +++ b/signature/rsa_buffer/Makefile @@ -0,0 +1,36 @@ +# RSA Examples Makefile +CC = gcc +LIB_PATH = /usr/local +CFLAGS = -Wall -I$(LIB_PATH)/include +LIBS = -L$(LIB_PATH)/lib -lm + +# option variables +DYN_LIB = -lwolfssl +STATIC_LIB = $(LIB_PATH)/lib/libwolfssl.a +DEBUG_FLAGS = -g -DDEBUG +DEBUG_INC_PATHS = -MD +OPTIMIZE = -Os + +# Options +#CFLAGS+=$(DEBUG_FLAGS) +CFLAGS+=$(OPTIMIZE) +#LIBS+=$(STATIC_LIB) +LIBS+=$(DYN_LIB) + +# build targets +SRC=$(wildcard *.c) +TARGETS=$(patsubst %.c, %, $(SRC)) + +.PHONY: clean all + +all: $(TARGETS) + +debug: CFLAGS+=$(DEBUG_FLAGS) +debug: all + +# build template +%: %.c + $(CC) -o $@ $< $(CFLAGS) $(LIBS) + +clean: + rm -f $(TARGETS) diff --git a/signature/rsa_buffer/README b/signature/rsa_buffer/README new file mode 100644 index 00000000..704fe0d7 --- /dev/null +++ b/signature/rsa_buffer/README @@ -0,0 +1,28 @@ +Configure and install wolfSSL with these options: + +./configure +make +make install + +(if any build issues due to previous installations please run 'ldconfig`) + +To compile without Makefile: + +gcc -o sign sign.c -lwolfssl +./sign > signature.h +gcc -o verify verify.c -lwolfssl + + +To sign a message: + +./sign + +To verify the signature with the message: + +./verify + + +Best wishes in all your testing! + +- The wolfSSL Team + diff --git a/signature/rsa_buffer/rsa_priv_2048.h b/signature/rsa_buffer/rsa_priv_2048.h new file mode 100644 index 00000000..dcdce22f --- /dev/null +++ b/signature/rsa_buffer/rsa_priv_2048.h @@ -0,0 +1,151 @@ +/* rsa_priv_2048.h + * + * Copyright (C) 2006-2018 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* This file contains an RSA 2048-bit private key. + * It is the private counterpart to "rsa_pub_2048.h" + */ + +/* RSA private key to sign with. + * Key is PKCS#1 formatted and DER encoded. + */ +static const unsigned char private_key_2048[] = { + 0x30, 0x82, 0x04, 0xA4, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, + 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE, 0x39, 0xA4, 0x32, + 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74, 0x9A, + 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, + 0x07, 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, + 0x9E, 0xD4, 0x81, 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, + 0xBB, 0xA1, 0x75, 0xC8, 0x36, 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, + 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC, 0xF1, 0x81, 0x1E, + 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F, 0x65, + 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, + 0xC5, 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 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0x42, 0x46, 0xA1, 0x9F, 0xCD, + 0xF5, 0xBF +}; + diff --git a/signature/rsa_buffer/rsa_pub_2048.h b/signature/rsa_buffer/rsa_pub_2048.h new file mode 100644 index 00000000..a5432acb --- /dev/null +++ b/signature/rsa_buffer/rsa_pub_2048.h @@ -0,0 +1,68 @@ +/* rsa_pub_2048.h + * + * Copyright (C) 2006-2018 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* This file contains an RSA 2048-bit public key. + * It is the public counterpart to "rsa_priv_2048.h" + */ + +/* RSA public key to verify with. + * Key is PKCS#1 formatted and DER encoded. + */ +static const unsigned char public_key_2048[] = { + 0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, + 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, + 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, + 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, + 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE, 0x39, 0xA4, + 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, + 0x7C, 0x74, 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, + 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07, 0x32, 0x8E, + 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, + 0xD4, 0x81, 0x48, 0xFD, 0x2D, 0x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, + 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, + 0xB5, 0x72, 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, + 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7, 0x7F, 0xC0, + 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, + 0xD3, 0x02, 0x03, 0x01, 0x00, 0x01 +}; + diff --git a/signature/rsa_buffer/sign.c b/signature/rsa_buffer/sign.c new file mode 100644 index 00000000..ead71e5f --- /dev/null +++ b/signature/rsa_buffer/sign.c @@ -0,0 +1,176 @@ +/* sign.c + * + * Copyright (C) 2006-2018 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* This file shows how to sign a message with an RSA private key. + * The signature is PKCS#1.5 formatted. + * Key and data are held in buffers. + * The output of this program can be used with "verify.c". + */ + +#include + +#include +#include +#include +#include +#include + +#include "rsa_priv_2048.h" + +/* Signature size is the length of the modulus of the RSA key */ +#define SIG_SZ (2048 / 8) +/* Maximum bound on digest algorithm encoding around digest */ +#define MAX_ENC_ALG_SZ 32 + +/* Print out the buffer in C code. + * + * name [in] Name of the variable. + * data [in] Data to print out. + * len [in] Length of the data. + */ +void print_buffer(char* name, unsigned char* data, word32 len) +{ + word32 i; + + printf("unsigned char %s[] = {\n", name); + for (i = 0; i < len; i++) { + if ((i % 8) == 0) + printf(" "); + printf(" 0x%02x,", data[i]); + if ((i % 8) == 7) + printf("\n"); + } + if ((i % 8) != 0) + printf("\n"); + printf("};\n"); + +} + +/* Main entry point. + * Signs the message passed in as the first command line argument. + * + * argc [in] Count of command line arguments. + * argv [in] Command line argument vector. + * Returns 0 on success and 1 otherwise. + */ +int main(int argc, char* argv[]) +{ + int ret = 0; + Sha256 sha256; + Sha256* pSha256 = NULL; + RsaKey rsaKey; + RsaKey* pRsaKey = NULL; +#ifdef WC_RSA_BLINDING + WC_RNG rng; + WC_RNG* pRng = NULL; +#endif + word32 idx; + unsigned char* msg; + word32 msgLen; + unsigned char signature[SIG_SZ]; + word32 sigLen; + unsigned char digest[WC_SHA256_DIGEST_SIZE]; + unsigned char encSig[WC_SHA256_DIGEST_SIZE + MAX_ENC_ALG_SZ]; + word32 encSigLen; + + /* Get the message to sign from the command line */ + if (argc != 2) { + fprintf(stderr, "Message to sign required\n"); + ret = -1; + } + else { + msg = (unsigned char*)argv[1]; + msgLen = strlen(argv[1]); + } + + /* Calculate SHA-256 digest of message */ + if (ret == 0) + ret = wc_InitSha256(&sha256); + if (ret == 0) { + pSha256 = &sha256; + ret = wc_Sha256Update(&sha256, msg, msgLen); + } + if (ret == 0) + ret = wc_Sha256Final(&sha256, digest); + + /* Encode digest with algorithm information as per PKCS#1.5 */ + if (ret == 0) { + encSigLen = wc_EncodeSignature(encSig, digest, sizeof(digest), SHA256h); + if ((int)encSigLen < 0) + ret = (int)encSigLen; + } + + /* Initialize RSA key and random (if required) */ + if (ret == 0) { + ret = wc_InitRsaKey(&rsaKey, NULL); + if (ret == 0) + pRsaKey = &rsaKey; + } +#ifdef WC_RSA_BLINDING + if (ret == 0) + ret = wc_InitRng(&rng); +#endif + /* Load DER encoded RSA private key from buffer */ + if (ret == 0) { +#ifdef WC_RSA_BLINDING + pRng = &rng; +#endif + idx = 0; + ret = wc_RsaPrivateKeyDecode(private_key_2048, &idx, &rsaKey, + sizeof(private_key_2048)); + } + + /* Sign encoded digest */ + if (ret == 0) { +#ifdef WC_RSA_BLINDING + ret = wc_RsaSSL_Sign(encSig, encSigLen, signature, sizeof(signature), + &rsaKey, pRng); +#else + ret = wc_RsaSSL_Sign(encSig, encSigLen, signature, sizeof(signature), + &rsaKey, NULL); +#endif + if (ret >= 0) { + sigLen = ret; + ret = 0; + } + } + + if (ret == 0) { + /* Display message as a buffer */ + print_buffer("msg", msg, msgLen); + printf("\n"); + /* Display binary signature as a buffer */ + print_buffer("rsa_sig_2048", signature, sigLen); + } + + /* Free data structures */ +#ifdef WC_RSA_BLINDING + if (pRng != NULL) + wc_FreeRng(pRng); +#endif + if (pRsaKey != NULL) + wc_FreeRsaKey(pRsaKey); + if (pSha256 != NULL) + wc_Sha256Free(pSha256); + + return ret == 0 ? 0 : 1; +} + diff --git a/signature/rsa_buffer/sign_vfy.sh b/signature/rsa_buffer/sign_vfy.sh new file mode 100755 index 00000000..0ad83d54 --- /dev/null +++ b/signature/rsa_buffer/sign_vfy.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +make clean + +make sign +./sign "This is the message" > signature.h +make verify +./verify + + diff --git a/signature/rsa_buffer/signature.h b/signature/rsa_buffer/signature.h new file mode 100644 index 00000000..e0b34c8c --- /dev/null +++ b/signature/rsa_buffer/signature.h @@ -0,0 +1,40 @@ +unsigned char msg[] = { + 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, + 0x74, 0x68, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, + 0x61, 0x67, 0x65, +}; + +unsigned char rsa_sig_2048[] = { + 0x41, 0xeb, 0xf5, 0x5e, 0x97, 0x43, 0xf4, 0xd1, + 0xda, 0xb6, 0x5c, 0x75, 0x57, 0x2c, 0xe1, 0x01, + 0x07, 0xdc, 0x42, 0xc4, 0x2d, 0xe2, 0xb5, 0xc8, + 0x63, 0xe8, 0x45, 0x9a, 0x4a, 0xfa, 0xdf, 0x5e, + 0xa6, 0x08, 0x0a, 0x26, 0x2e, 0xca, 0x2c, 0x10, + 0x7a, 0x15, 0x8d, 0xc1, 0x55, 0xcc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 0x4e, 0x7a, 0xe1, 0x89, 0xb4, 0xf0, 0x9a, + 0xb7, 0xd3, 0xd6, 0x0a, 0x10, 0x67, 0x28, 0x25, + 0x6d, 0xda, 0x92, 0x99, 0x3f, 0x64, 0xa7, 0xea, + 0xe0, 0xdc, 0x7c, 0xe8, 0x41, 0xb0, 0xeb, 0x45, +}; diff --git a/signature/rsa_buffer/verify.c b/signature/rsa_buffer/verify.c new file mode 100644 index 00000000..d362f458 --- /dev/null +++ b/signature/rsa_buffer/verify.c @@ -0,0 +1,116 @@ +/* rsa_pub_2048.h + * + * Copyright (C) 2006-2018 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +/* This file is an example of verifying an RSA signature. + * The signature is PKCS#1.5 formatted. + * Key and data are held in buffers. + * "signature.h", used by this program, can be generated using "sign.c". + */ + +#include +#include +#include +#include +#include + +#include "rsa_pub_2048.h" +#include "signature.h" + +/* Maximum bound on digest algorithm encoding around digest */ +#define MAX_ENC_ALG_SZ 32 + +/* Main entry point. + * Verifies the signature with the message and RSA public key. + * + * argc [in] Count of command line arguments. + * argv [in] Command line argument vector. + * Returns 0 on success and 1 otherwise. + */ +int main(int argc, char* argv[]) +{ + int ret = 0; + Sha256 sha256; + Sha256* pSha256 = NULL; + RsaKey rsaKey; + RsaKey* pRsaKey = NULL; + word32 idx; + unsigned char digest[WC_SHA256_DIGEST_SIZE]; + unsigned char encSig[WC_SHA256_DIGEST_SIZE + MAX_ENC_ALG_SZ]; + word32 encSigLen = 0; + unsigned char* decSig = NULL; + word32 decSigLen = 0; + + /* Calculate SHA-256 digest of message */ + if (ret == 0) + ret = wc_InitSha256(&sha256); + if (ret == 0) { + pSha256 = &sha256; + ret = wc_Sha256Update(&sha256, msg, sizeof(msg)); + } + if (ret == 0) + ret = wc_Sha256Final(&sha256, digest); + + /* Encode digest with algorithm information as per PKCS#1.5 */ + if (ret == 0) { + encSigLen = wc_EncodeSignature(encSig, digest, sizeof(digest), SHA256h); + if ((int)encSigLen < 0) + ret = (int)encSigLen; + } + + /* Initialize the RSA key and decode the DER encoded public key. */ + if (ret == 0) + ret = wc_InitRsaKey(&rsaKey, NULL); + if (ret == 0) { + pRsaKey = &rsaKey; + + idx = 0; + ret = wc_RsaPublicKeyDecode(public_key_2048, &idx, &rsaKey, + sizeof(public_key_2048)); + } + + /* Verify the signature by decrypting the value. */ + if (ret == 0) { + decSigLen = wc_RsaSSL_VerifyInline(rsa_sig_2048, sizeof(rsa_sig_2048), + &decSig, &rsaKey); + if ((int)decSigLen < 0) + ret = (int)decSigLen; + } + /* Check the decrypted result matches the encoded digest. */ + if (ret == 0 && encSigLen != decSigLen) + ret = -1; + if (ret == 0 && XMEMCMP(encSig, decSig, encSigLen) != 0) + ret = -1; + + /* Report on the verification */ + if (ret == 0) + fprintf(stderr, "Verified\n"); + else + fprintf(stderr, "Failure\n"); + + /* Free the data structures */ + if (pRsaKey != NULL) + wc_FreeRsaKey(pRsaKey); + if (pSha256 != NULL) + wc_Sha256Free(pSha256); + + return ret == 0 ? 0 : 1; +} +