Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/agc-netpgp-standalone]: src/crypto/external/bsd/netpgp/dist/src/librsa A...
details: https://anonhg.NetBSD.org/src/rev/3d9a6ca74b34
branches: agc-netpgp-standalone
changeset: 777800:3d9a6ca74b34
user: agc <agc%NetBSD.org@localhost>
date: Sun May 06 17:46:45 2012 +0000
description:
Add the RSA and DSA signature code to the agc-netpgp-standalone branch.
Despite the library name, this library implements both RSA and DSA functionality.
XXX There is still some work to do on DSA signatures and verification. XXX
diffstat:
crypto/external/bsd/netpgp/dist/src/librsa/Makefile | 10 +
crypto/external/bsd/netpgp/dist/src/librsa/libnetpgprsa.3 | 114 ++
crypto/external/bsd/netpgp/dist/src/librsa/rsa.c | 597 ++++++++++++++
crypto/external/bsd/netpgp/dist/src/librsa/rsa.h | 154 +++
crypto/external/bsd/netpgp/dist/src/librsa/rsastubs.c | 83 +
crypto/external/bsd/netpgp/dist/src/librsa/rsastubs.h | 25 +
6 files changed, 983 insertions(+), 0 deletions(-)
diffs (truncated from 1007 to 300 lines):
diff -r 136a9d498d0b -r 3d9a6ca74b34 crypto/external/bsd/netpgp/dist/src/librsa/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/librsa/Makefile Sun May 06 17:46:45 2012 +0000
@@ -0,0 +1,10 @@
+LIB=netrsa
+SRCS=rsa.c stubs.c
+MKMAN=no
+WARNS=4
+CPPFLAGS+=-I${.CURDIR}/../bn
+
+INCS=rsa.h
+INCSDIR=/usr/include/netpgp
+
+.include <bsd.lib.mk>
diff -r 136a9d498d0b -r 3d9a6ca74b34 crypto/external/bsd/netpgp/dist/src/librsa/libnetpgprsa.3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/librsa/libnetpgprsa.3 Sun May 06 17:46:45 2012 +0000
@@ -0,0 +1,114 @@
+.\" $NetBSD: libnetpgprsa.3,v 1.1.2.1 2012/05/06 17:46:45 agc Exp $
+.\"
+.\" Copyright (c) 2012 Alistair Crooks <agc%NetBSD.org@localhost>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd April 13, 2012
+.Dt LIBNETPGPRSA 3
+.Os
+.Sh NAME
+.Nm libnetpgprsa
+.Nd BIGNUM library of multi-precision integers
+.Sh LIBRARY
+.Lb libnetpgprsa
+.Sh SYNOPSIS
+.In netpgp/rsa.h
+.Ft RSA *
+.Fo RSA_new
+.Fa "void"
+.Fc
+.Ft int
+.Fo RSA_size
+.Fa "const RSA *rsa"
+.Fc
+.Ft void
+.Fo RSA_free
+.Fa "RSA *rsa"
+.Fc
+.Ft int
+.Fo RSA_check_key
+.Fa "RSA *rsa"
+.Fc
+.Ft RSA *
+.Fo RSA_generate_key
+.Fa "int num" "unsigned long e" "void (*callback)(int, int, void *)" "void *callbackarg"
+.Fc
+.Ft int
+.Fo RSA_public_encrypt
+.Fa "int siglen" "const uint8_t *signature" "uint8_t *to" "RSA *rsa" "int padding"
+.Fc
+.Ft int
+.Fo RSA_private_encrypt
+.Fa "int siglen" "const uint8_t *signature" "uint8_t *to" "RSA *rsa" "int padding"
+.Fc
+.Ft int
+.Fo RSA_private_decrypt
+.Fa "int siglen" "const uint8_t *signature" "uint8_t *to" "RSA *rsa" "int padding"
+.Fc
+.Pp
+.Ft DSA *
+.Fo DSA_new
+.Fa "void"
+.Fc
+.Ft int
+.Fo DSA_size
+.Fa "const DSA *dsa"
+.Fc
+.Ft void
+.Fo DSA_free
+.Fa "DSA *dsa"
+.Fc
+.Ft DSA_SIG *
+.Fo DSA_SIG_new
+.Fa "void"
+.Fc
+.Ft void
+.Fo DSA_SIG_free
+.Fa "DSA_SIG *sig"
+.Fc
+.Ft int
+.Fo DSA_do_verify
+.Fa "const unsigned char *digest" "int digestlen" "DSA_SIG *sig" "DSA *dsa"
+.Fc
+.Ft int
+.Fo DSA_do_sign
+.Fa "const unsigned char *digest" "int digestlen" "DSA *dsa"
+.Fc
+.Sh DESCRIPTION
+.Nm
+is a small library which provides RSA signing,
+encryption and decryption, and DSA signing.
+RSA and DSA verification are provided by the
+.Xr libnetpgpverify 3
+library.
+.Pp
+.Sh SEE ALSO
+.Xr libnetpgpbn 3
+.Xr libnetpgpverify 3
+.Sh HISTORY
+The
+.Nm
+library first appeared in
+.Nx 7.0 .
+.Sh AUTHORS
+.An Alistair Crooks Aq agc%NetBSD.org@localhost
diff -r 136a9d498d0b -r 3d9a6ca74b34 crypto/external/bsd/netpgp/dist/src/librsa/rsa.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/crypto/external/bsd/netpgp/dist/src/librsa/rsa.c Sun May 06 17:46:45 2012 +0000
@@ -0,0 +1,597 @@
+/*-
+ * Copyright (c) 2012 Alistair Crooks <agc%NetBSD.org@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/types.h>
+#include <sys/syslog.h>
+
+#ifdef _KERNEL
+# include <sys/kmem.h>
+# define logmessage log
+#else
+# include <stdio.h>
+# include <stdlib.h>
+# include <string.h>
+# include <unistd.h>
+#endif
+
+#include "misc.h"
+#include "digest.h"
+#include "rsa.h"
+
+#ifndef USE_ARG
+#define USE_ARG(x) /*LINTED*/(void)&(x)
+#endif
+
+#define RSA_MAX_MODULUS_BITS 16384
+#define RSA_SMALL_MODULUS_BITS 3072
+#define RSA_MAX_PUBEXP_BITS 64 /* exponent limit enforced for "large" modulus only */
+
+static int
+rsa_padding_check_none(uint8_t *to, int tlen, const uint8_t *from, int flen, int num)
+{
+ USE_ARG(num);
+ if (flen > tlen) {
+ printf("r too large\n");
+ return -1;
+ }
+ (void) memset(to, 0x0, tlen - flen);
+ (void) memcpy(to + tlen - flen, from, flen);
+ return tlen;
+}
+
+static int
+lowlevel_rsa_private_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa)
+{
+ BIGNUM *decbn;
+ BIGNUM *signedbn;
+ uint8_t *decbuf;
+ int nbytes;
+ int signc;
+ int signedbytes;
+ int r;
+
+ decbuf = NULL;
+ r = -1;
+ decbn = BN_new();
+ signedbn = BN_new();
+ nbytes = BN_num_bytes(rsa->n);
+ decbuf = netpgp_allocate(1, nbytes);
+ /* add no padding */
+ memcpy(decbuf, plain, plainc);
+ BN_bin2bn(decbuf, nbytes, decbn);
+ if (BN_cmp(decbn, rsa->n) >= 0) {
+ printf("decbn too big\n");
+ goto err;
+ }
+ if (!BN_mod_exp(signedbn, decbn, rsa->d, rsa->n, NULL)) {
+ printf("bad mod_exp\n");
+ goto err;
+ }
+ signedbytes = BN_num_bytes(signedbn);
+ signc = BN_bn2bin(signedbn, &encbuf[nbytes - signedbytes]);
+ memset(encbuf, 0x0, nbytes - signc);
+ r = nbytes;
+err:
+ netpgp_deallocate(decbuf, nbytes);
+ BN_clear_free(decbn);
+ BN_clear_free(signedbn);
+ return r;
+}
+
+static int
+lowlevel_rsa_public_encrypt(int plainc, const unsigned char *plain, unsigned char *encbuf, RSA *rsa)
+{
+ BIGNUM *decbn;
+ BIGNUM *encbn;
+ uint8_t *decbuf;
+ int nbytes;
+ int encc;
+ int r;
+ int i;
+
+ r = -1;
+ decbn = BN_new();
+ encbn = BN_new();
+ nbytes = BN_num_bytes(rsa->n);
+ decbuf = netpgp_allocate(1, nbytes);
+ (void) memcpy(decbuf, plain, plainc);
+ if (BN_bin2bn(decbuf, nbytes, decbn) == NULL) {
+ printf("bin2bn failed\n");
+ goto err;
+ }
+ if (BN_cmp(decbn, rsa->n) >= 0) {
+ printf("BN_cmp failed\n");
+ goto err;
+ }
+ if (!BN_mod_exp(encbn, decbn, rsa->e, rsa->n, NULL)) {
+ printf("BN_mod_exp failed\n");
+ goto err;
+ }
+ encc = BN_num_bytes(encbn);
+ i = BN_bn2bin(encbn, &encbuf[nbytes - encc]);
+ (void) memset(encbuf, 0x0, nbytes - i);
+ r = nbytes;
+err:
+ if (decbuf) {
+ memset(decbuf, 0x0, nbytes);
+ netpgp_deallocate(decbuf, nbytes);
+ }
+ BN_clear_free(decbn);
+ BN_clear_free(encbn);
+ return r;
+}
+
+static int
+lowlevel_rsa_private_decrypt(int enclen, const unsigned char *encbuf, unsigned char *to, RSA *rsa)
+{
+ BIGNUM *encbn;
+ BIGNUM *decbn;
+ uint8_t *buf;
+ int nbytes;
+ int j;
+ int r;
+
+ r = -1;
+ decbn = encbn = NULL;
+ buf = NULL;
+ if (BN_num_bits(rsa->n) > RSA_MAX_MODULUS_BITS) {
+ return -1;
+ }
+ if (BN_cmp(rsa->n, rsa->e) <= 0) {
+ return -1;
+ }
+ encbn = BN_new();
Home |
Main Index |
Thread Index |
Old Index