diff options
Diffstat (limited to 'src/lib/libcrypto/rsa')
| -rw-r--r-- | src/lib/libcrypto/rsa/Makefile.ssl | 241 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa.h | 366 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_asn1.c | 121 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_chk.c | 184 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_eay.c | 727 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_err.c | 149 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_gen.c | 200 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_lib.c | 414 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_none.c | 98 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_oaep.c | 206 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_pk1.c | 224 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_saos.c | 149 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_sign.c | 232 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_ssl.c | 154 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_test.c | 5 |
15 files changed, 3467 insertions, 3 deletions
diff --git a/src/lib/libcrypto/rsa/Makefile.ssl b/src/lib/libcrypto/rsa/Makefile.ssl new file mode 100644 index 0000000000..8089344a04 --- /dev/null +++ b/src/lib/libcrypto/rsa/Makefile.ssl | |||
| @@ -0,0 +1,241 @@ | |||
| 1 | # | ||
| 2 | # SSLeay/crypto/rsa/Makefile | ||
| 3 | # | ||
| 4 | |||
| 5 | DIR= rsa | ||
| 6 | TOP= ../.. | ||
| 7 | CC= cc | ||
| 8 | INCLUDES= -I.. -I$(TOP) -I../../include | ||
| 9 | CFLAG=-g | ||
| 10 | INSTALL_PREFIX= | ||
| 11 | OPENSSLDIR= /usr/local/ssl | ||
| 12 | INSTALLTOP=/usr/local/ssl | ||
| 13 | MAKE= make -f Makefile.ssl | ||
| 14 | MAKEDEPPROG= makedepend | ||
| 15 | MAKEDEPEND= $(TOP)/util/domd $(TOP) -MD $(MAKEDEPPROG) | ||
| 16 | MAKEFILE= Makefile.ssl | ||
| 17 | AR= ar r | ||
| 18 | |||
| 19 | CFLAGS= $(INCLUDES) $(CFLAG) | ||
| 20 | |||
| 21 | GENERAL=Makefile | ||
| 22 | TEST=rsa_test.c | ||
| 23 | APPS= | ||
| 24 | |||
| 25 | LIB=$(TOP)/libcrypto.a | ||
| 26 | LIBSRC= rsa_eay.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_saos.c rsa_err.c \ | ||
| 27 | rsa_pk1.c rsa_ssl.c rsa_none.c rsa_oaep.c rsa_chk.c rsa_null.c \ | ||
| 28 | rsa_asn1.c | ||
| 29 | LIBOBJ= rsa_eay.o rsa_gen.o rsa_lib.o rsa_sign.o rsa_saos.o rsa_err.o \ | ||
| 30 | rsa_pk1.o rsa_ssl.o rsa_none.o rsa_oaep.o rsa_chk.o rsa_null.o \ | ||
| 31 | rsa_asn1.o | ||
| 32 | |||
| 33 | SRC= $(LIBSRC) | ||
| 34 | |||
| 35 | EXHEADER= rsa.h | ||
| 36 | HEADER= $(EXHEADER) | ||
| 37 | |||
| 38 | ALL= $(GENERAL) $(SRC) $(HEADER) | ||
| 39 | |||
| 40 | top: | ||
| 41 | (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all) | ||
| 42 | |||
| 43 | all: lib | ||
| 44 | |||
| 45 | lib: $(LIBOBJ) | ||
| 46 | $(AR) $(LIB) $(LIBOBJ) | ||
| 47 | $(RANLIB) $(LIB) || echo Never mind. | ||
| 48 | @touch lib | ||
| 49 | |||
| 50 | files: | ||
| 51 | $(PERL) $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO | ||
| 52 | |||
| 53 | links: | ||
| 54 | @sh $(TOP)/util/point.sh Makefile.ssl Makefile | ||
| 55 | @$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER) | ||
| 56 | @$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST) | ||
| 57 | @$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS) | ||
| 58 | |||
| 59 | install: | ||
| 60 | @for i in $(EXHEADER) ; \ | ||
| 61 | do \ | ||
| 62 | (cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \ | ||
| 63 | chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \ | ||
| 64 | done; | ||
| 65 | |||
| 66 | tags: | ||
| 67 | ctags $(SRC) | ||
| 68 | |||
| 69 | tests: | ||
| 70 | |||
| 71 | lint: | ||
| 72 | lint -DLINT $(INCLUDES) $(SRC)>fluff | ||
| 73 | |||
| 74 | depend: | ||
| 75 | $(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC) | ||
| 76 | |||
| 77 | dclean: | ||
| 78 | $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new | ||
| 79 | mv -f Makefile.new $(MAKEFILE) | ||
| 80 | |||
| 81 | clean: | ||
| 82 | rm -f *.o */*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff | ||
| 83 | |||
| 84 | # DO NOT DELETE THIS LINE -- make depend depends on it. | ||
| 85 | |||
| 86 | rsa_asn1.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 87 | rsa_asn1.o: ../../include/openssl/asn1t.h ../../include/openssl/bio.h | ||
| 88 | rsa_asn1.o: ../../include/openssl/bn.h ../../include/openssl/buffer.h | ||
| 89 | rsa_asn1.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h | ||
| 90 | rsa_asn1.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | ||
| 91 | rsa_asn1.o: ../../include/openssl/opensslconf.h | ||
| 92 | rsa_asn1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 93 | rsa_asn1.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 94 | rsa_asn1.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 95 | rsa_asn1.o: ../cryptlib.h rsa_asn1.c | ||
| 96 | rsa_chk.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 97 | rsa_chk.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h | ||
| 98 | rsa_chk.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 99 | rsa_chk.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 100 | rsa_chk.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 101 | rsa_chk.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 102 | rsa_chk.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 103 | rsa_chk.o: rsa_chk.c | ||
| 104 | rsa_eay.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 105 | rsa_eay.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 106 | rsa_eay.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 107 | rsa_eay.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 108 | rsa_eay.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 109 | rsa_eay.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 110 | rsa_eay.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h | ||
| 111 | rsa_eay.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 112 | rsa_eay.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_eay.c | ||
| 113 | rsa_err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 114 | rsa_err.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h | ||
| 115 | rsa_err.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 116 | rsa_err.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 117 | rsa_err.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 118 | rsa_err.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 119 | rsa_err.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 120 | rsa_err.o: rsa_err.c | ||
| 121 | rsa_gen.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 122 | rsa_gen.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 123 | rsa_gen.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 124 | rsa_gen.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 125 | rsa_gen.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 126 | rsa_gen.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 127 | rsa_gen.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 128 | rsa_gen.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 129 | rsa_gen.o: ../cryptlib.h rsa_gen.c | ||
| 130 | rsa_lib.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 131 | rsa_lib.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 132 | rsa_lib.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 133 | rsa_lib.o: ../../include/openssl/dh.h ../../include/openssl/dsa.h | ||
| 134 | rsa_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/engine.h | ||
| 135 | rsa_lib.o: ../../include/openssl/err.h ../../include/openssl/lhash.h | ||
| 136 | rsa_lib.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h | ||
| 137 | rsa_lib.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h | ||
| 138 | rsa_lib.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h | ||
| 139 | rsa_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 140 | rsa_lib.o: ../../include/openssl/ui.h ../cryptlib.h rsa_lib.c | ||
| 141 | rsa_none.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 142 | rsa_none.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 143 | rsa_none.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 144 | rsa_none.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 145 | rsa_none.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 146 | rsa_none.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 147 | rsa_none.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h | ||
| 148 | rsa_none.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 149 | rsa_none.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_none.c | ||
| 150 | rsa_null.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 151 | rsa_null.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 152 | rsa_null.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 153 | rsa_null.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 154 | rsa_null.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 155 | rsa_null.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 156 | rsa_null.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h | ||
| 157 | rsa_null.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 158 | rsa_null.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_null.c | ||
| 159 | rsa_oaep.o: ../../e_os.h ../../include/openssl/aes.h | ||
| 160 | rsa_oaep.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 161 | rsa_oaep.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h | ||
| 162 | rsa_oaep.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h | ||
| 163 | rsa_oaep.o: ../../include/openssl/crypto.h ../../include/openssl/des.h | ||
| 164 | rsa_oaep.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h | ||
| 165 | rsa_oaep.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 166 | rsa_oaep.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
| 167 | rsa_oaep.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h | ||
| 168 | rsa_oaep.o: ../../include/openssl/md2.h ../../include/openssl/md4.h | ||
| 169 | rsa_oaep.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h | ||
| 170 | rsa_oaep.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
| 171 | rsa_oaep.o: ../../include/openssl/opensslconf.h | ||
| 172 | rsa_oaep.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 173 | rsa_oaep.o: ../../include/openssl/rand.h ../../include/openssl/rc2.h | ||
| 174 | rsa_oaep.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h | ||
| 175 | rsa_oaep.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h | ||
| 176 | rsa_oaep.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
| 177 | rsa_oaep.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 178 | rsa_oaep.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h | ||
| 179 | rsa_oaep.o: ../cryptlib.h rsa_oaep.c | ||
| 180 | rsa_pk1.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 181 | rsa_pk1.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 182 | rsa_pk1.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 183 | rsa_pk1.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 184 | rsa_pk1.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 185 | rsa_pk1.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 186 | rsa_pk1.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h | ||
| 187 | rsa_pk1.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 188 | rsa_pk1.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_pk1.c | ||
| 189 | rsa_saos.o: ../../e_os.h ../../include/openssl/aes.h | ||
| 190 | rsa_saos.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 191 | rsa_saos.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h | ||
| 192 | rsa_saos.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h | ||
| 193 | rsa_saos.o: ../../include/openssl/crypto.h ../../include/openssl/des.h | ||
| 194 | rsa_saos.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h | ||
| 195 | rsa_saos.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 196 | rsa_saos.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
| 197 | rsa_saos.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h | ||
| 198 | rsa_saos.o: ../../include/openssl/md2.h ../../include/openssl/md4.h | ||
| 199 | rsa_saos.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h | ||
| 200 | rsa_saos.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
| 201 | rsa_saos.o: ../../include/openssl/opensslconf.h | ||
| 202 | rsa_saos.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 203 | rsa_saos.o: ../../include/openssl/pkcs7.h ../../include/openssl/rc2.h | ||
| 204 | rsa_saos.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h | ||
| 205 | rsa_saos.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h | ||
| 206 | rsa_saos.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
| 207 | rsa_saos.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 208 | rsa_saos.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h | ||
| 209 | rsa_saos.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
| 210 | rsa_saos.o: ../cryptlib.h rsa_saos.c | ||
| 211 | rsa_sign.o: ../../e_os.h ../../include/openssl/aes.h | ||
| 212 | rsa_sign.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h | ||
| 213 | rsa_sign.o: ../../include/openssl/blowfish.h ../../include/openssl/bn.h | ||
| 214 | rsa_sign.o: ../../include/openssl/buffer.h ../../include/openssl/cast.h | ||
| 215 | rsa_sign.o: ../../include/openssl/crypto.h ../../include/openssl/des.h | ||
| 216 | rsa_sign.o: ../../include/openssl/des_old.h ../../include/openssl/dh.h | ||
| 217 | rsa_sign.o: ../../include/openssl/dsa.h ../../include/openssl/e_os2.h | ||
| 218 | rsa_sign.o: ../../include/openssl/err.h ../../include/openssl/evp.h | ||
| 219 | rsa_sign.o: ../../include/openssl/idea.h ../../include/openssl/lhash.h | ||
| 220 | rsa_sign.o: ../../include/openssl/md2.h ../../include/openssl/md4.h | ||
| 221 | rsa_sign.o: ../../include/openssl/md5.h ../../include/openssl/mdc2.h | ||
| 222 | rsa_sign.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h | ||
| 223 | rsa_sign.o: ../../include/openssl/opensslconf.h | ||
| 224 | rsa_sign.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 225 | rsa_sign.o: ../../include/openssl/pkcs7.h ../../include/openssl/rc2.h | ||
| 226 | rsa_sign.o: ../../include/openssl/rc4.h ../../include/openssl/rc5.h | ||
| 227 | rsa_sign.o: ../../include/openssl/ripemd.h ../../include/openssl/rsa.h | ||
| 228 | rsa_sign.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h | ||
| 229 | rsa_sign.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h | ||
| 230 | rsa_sign.o: ../../include/openssl/ui.h ../../include/openssl/ui_compat.h | ||
| 231 | rsa_sign.o: ../../include/openssl/x509.h ../../include/openssl/x509_vfy.h | ||
| 232 | rsa_sign.o: ../cryptlib.h rsa_sign.c | ||
| 233 | rsa_ssl.o: ../../e_os.h ../../include/openssl/asn1.h | ||
| 234 | rsa_ssl.o: ../../include/openssl/bio.h ../../include/openssl/bn.h | ||
| 235 | rsa_ssl.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h | ||
| 236 | rsa_ssl.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h | ||
| 237 | rsa_ssl.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h | ||
| 238 | rsa_ssl.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h | ||
| 239 | rsa_ssl.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h | ||
| 240 | rsa_ssl.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h | ||
| 241 | rsa_ssl.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_ssl.c | ||
diff --git a/src/lib/libcrypto/rsa/rsa.h b/src/lib/libcrypto/rsa/rsa.h new file mode 100644 index 0000000000..fc3bb5f86d --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa.h | |||
| @@ -0,0 +1,366 @@ | |||
| 1 | /* crypto/rsa/rsa.h */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #ifndef HEADER_RSA_H | ||
| 60 | #define HEADER_RSA_H | ||
| 61 | |||
| 62 | #include <openssl/asn1.h> | ||
| 63 | |||
| 64 | #ifndef OPENSSL_NO_BIO | ||
| 65 | #include <openssl/bio.h> | ||
| 66 | #endif | ||
| 67 | #include <openssl/bn.h> | ||
| 68 | #include <openssl/crypto.h> | ||
| 69 | #include <openssl/ossl_typ.h> | ||
| 70 | |||
| 71 | #ifdef OPENSSL_NO_RSA | ||
| 72 | #error RSA is disabled. | ||
| 73 | #endif | ||
| 74 | |||
| 75 | #if defined(OPENSSL_FIPS) | ||
| 76 | #define FIPS_RSA_SIZE_T int | ||
| 77 | #endif | ||
| 78 | |||
| 79 | #ifdef __cplusplus | ||
| 80 | extern "C" { | ||
| 81 | #endif | ||
| 82 | |||
| 83 | typedef struct rsa_st RSA; | ||
| 84 | |||
| 85 | typedef struct rsa_meth_st | ||
| 86 | { | ||
| 87 | const char *name; | ||
| 88 | int (*rsa_pub_enc)(int flen,const unsigned char *from, | ||
| 89 | unsigned char *to, | ||
| 90 | RSA *rsa,int padding); | ||
| 91 | int (*rsa_pub_dec)(int flen,const unsigned char *from, | ||
| 92 | unsigned char *to, | ||
| 93 | RSA *rsa,int padding); | ||
| 94 | int (*rsa_priv_enc)(int flen,const unsigned char *from, | ||
| 95 | unsigned char *to, | ||
| 96 | RSA *rsa,int padding); | ||
| 97 | int (*rsa_priv_dec)(int flen,const unsigned char *from, | ||
| 98 | unsigned char *to, | ||
| 99 | RSA *rsa,int padding); | ||
| 100 | int (*rsa_mod_exp)(BIGNUM *r0,const BIGNUM *I,RSA *rsa); /* Can be null */ | ||
| 101 | int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
| 102 | const BIGNUM *m, BN_CTX *ctx, | ||
| 103 | BN_MONT_CTX *m_ctx); /* Can be null */ | ||
| 104 | int (*init)(RSA *rsa); /* called at new */ | ||
| 105 | int (*finish)(RSA *rsa); /* called at free */ | ||
| 106 | int flags; /* RSA_METHOD_FLAG_* things */ | ||
| 107 | char *app_data; /* may be needed! */ | ||
| 108 | /* New sign and verify functions: some libraries don't allow arbitrary data | ||
| 109 | * to be signed/verified: this allows them to be used. Note: for this to work | ||
| 110 | * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used | ||
| 111 | * RSA_sign(), RSA_verify() should be used instead. Note: for backwards | ||
| 112 | * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER | ||
| 113 | * option is set in 'flags'. | ||
| 114 | */ | ||
| 115 | int (*rsa_sign)(int type, | ||
| 116 | const unsigned char *m, unsigned int m_length, | ||
| 117 | unsigned char *sigret, unsigned int *siglen, const RSA *rsa); | ||
| 118 | int (*rsa_verify)(int dtype, | ||
| 119 | const unsigned char *m, unsigned int m_length, | ||
| 120 | unsigned char *sigbuf, unsigned int siglen, const RSA *rsa); | ||
| 121 | |||
| 122 | } RSA_METHOD; | ||
| 123 | |||
| 124 | struct rsa_st | ||
| 125 | { | ||
| 126 | /* The first parameter is used to pickup errors where | ||
| 127 | * this is passed instead of aEVP_PKEY, it is set to 0 */ | ||
| 128 | int pad; | ||
| 129 | long version; | ||
| 130 | const RSA_METHOD *meth; | ||
| 131 | /* functional reference if 'meth' is ENGINE-provided */ | ||
| 132 | ENGINE *engine; | ||
| 133 | BIGNUM *n; | ||
| 134 | BIGNUM *e; | ||
| 135 | BIGNUM *d; | ||
| 136 | BIGNUM *p; | ||
| 137 | BIGNUM *q; | ||
| 138 | BIGNUM *dmp1; | ||
| 139 | BIGNUM *dmq1; | ||
| 140 | BIGNUM *iqmp; | ||
| 141 | /* be careful using this if the RSA structure is shared */ | ||
| 142 | CRYPTO_EX_DATA ex_data; | ||
| 143 | int references; | ||
| 144 | int flags; | ||
| 145 | |||
| 146 | /* Used to cache montgomery values */ | ||
| 147 | BN_MONT_CTX *_method_mod_n; | ||
| 148 | BN_MONT_CTX *_method_mod_p; | ||
| 149 | BN_MONT_CTX *_method_mod_q; | ||
| 150 | |||
| 151 | /* all BIGNUM values are actually in the following data, if it is not | ||
| 152 | * NULL */ | ||
| 153 | char *bignum_data; | ||
| 154 | BN_BLINDING *blinding; | ||
| 155 | }; | ||
| 156 | |||
| 157 | #define RSA_3 0x3L | ||
| 158 | #define RSA_F4 0x10001L | ||
| 159 | |||
| 160 | #define RSA_METHOD_FLAG_NO_CHECK 0x01 /* don't check pub/private match */ | ||
| 161 | |||
| 162 | #define RSA_FLAG_CACHE_PUBLIC 0x02 | ||
| 163 | #define RSA_FLAG_CACHE_PRIVATE 0x04 | ||
| 164 | #define RSA_FLAG_BLINDING 0x08 | ||
| 165 | #define RSA_FLAG_THREAD_SAFE 0x10 | ||
| 166 | /* This flag means the private key operations will be handled by rsa_mod_exp | ||
| 167 | * and that they do not depend on the private key components being present: | ||
| 168 | * for example a key stored in external hardware. Without this flag bn_mod_exp | ||
| 169 | * gets called when private key components are absent. | ||
| 170 | */ | ||
| 171 | #define RSA_FLAG_EXT_PKEY 0x20 | ||
| 172 | |||
| 173 | /* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions. | ||
| 174 | */ | ||
| 175 | #define RSA_FLAG_SIGN_VER 0x40 | ||
| 176 | |||
| 177 | #define RSA_FLAG_NO_BLINDING 0x80 /* new with 0.9.6j and 0.9.7b; the built-in | ||
| 178 | * RSA implementation now uses blinding by | ||
| 179 | * default (ignoring RSA_FLAG_BLINDING), | ||
| 180 | * but other engines might not need it | ||
| 181 | */ | ||
| 182 | |||
| 183 | #define RSA_PKCS1_PADDING 1 | ||
| 184 | #define RSA_SSLV23_PADDING 2 | ||
| 185 | #define RSA_NO_PADDING 3 | ||
| 186 | #define RSA_PKCS1_OAEP_PADDING 4 | ||
| 187 | |||
| 188 | #define RSA_PKCS1_PADDING_SIZE 11 | ||
| 189 | |||
| 190 | #define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg) | ||
| 191 | #define RSA_get_app_data(s) RSA_get_ex_data(s,0) | ||
| 192 | |||
| 193 | RSA * RSA_new(void); | ||
| 194 | RSA * RSA_new_method(ENGINE *engine); | ||
| 195 | int RSA_size(const RSA *); | ||
| 196 | RSA * RSA_generate_key(int bits, unsigned long e,void | ||
| 197 | (*callback)(int,int,void *),void *cb_arg); | ||
| 198 | int RSA_check_key(const RSA *); | ||
| 199 | /* next 4 return -1 on error */ | ||
| 200 | int RSA_public_encrypt(int flen, const unsigned char *from, | ||
| 201 | unsigned char *to, RSA *rsa,int padding); | ||
| 202 | int RSA_private_encrypt(int flen, const unsigned char *from, | ||
| 203 | unsigned char *to, RSA *rsa,int padding); | ||
| 204 | int RSA_public_decrypt(int flen, const unsigned char *from, | ||
| 205 | unsigned char *to, RSA *rsa,int padding); | ||
| 206 | int RSA_private_decrypt(int flen, const unsigned char *from, | ||
| 207 | unsigned char *to, RSA *rsa,int padding); | ||
| 208 | void RSA_free (RSA *r); | ||
| 209 | /* "up" the RSA object's reference count */ | ||
| 210 | int RSA_up_ref(RSA *r); | ||
| 211 | |||
| 212 | int RSA_flags(const RSA *r); | ||
| 213 | |||
| 214 | void RSA_set_default_method(const RSA_METHOD *meth); | ||
| 215 | const RSA_METHOD *RSA_get_default_method(void); | ||
| 216 | const RSA_METHOD *RSA_get_method(const RSA *rsa); | ||
| 217 | int RSA_set_method(RSA *rsa, const RSA_METHOD *meth); | ||
| 218 | |||
| 219 | /* This function needs the memory locking malloc callbacks to be installed */ | ||
| 220 | int RSA_memory_lock(RSA *r); | ||
| 221 | |||
| 222 | /* these are the actual SSLeay RSA functions */ | ||
| 223 | const RSA_METHOD *RSA_PKCS1_SSLeay(void); | ||
| 224 | |||
| 225 | const RSA_METHOD *RSA_null_method(void); | ||
| 226 | |||
| 227 | DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPublicKey) | ||
| 228 | DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPrivateKey) | ||
| 229 | |||
| 230 | #ifndef OPENSSL_NO_FP_API | ||
| 231 | int RSA_print_fp(FILE *fp, const RSA *r,int offset); | ||
| 232 | #endif | ||
| 233 | |||
| 234 | #ifndef OPENSSL_NO_BIO | ||
| 235 | int RSA_print(BIO *bp, const RSA *r,int offset); | ||
| 236 | #endif | ||
| 237 | |||
| 238 | int i2d_RSA_NET(const RSA *a, unsigned char **pp, int (*cb)(), int sgckey); | ||
| 239 | RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length, int (*cb)(), int sgckey); | ||
| 240 | |||
| 241 | int i2d_Netscape_RSA(const RSA *a, unsigned char **pp, int (*cb)()); | ||
| 242 | RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, int (*cb)()); | ||
| 243 | |||
| 244 | /* The following 2 functions sign and verify a X509_SIG ASN1 object | ||
| 245 | * inside PKCS#1 padded RSA encryption */ | ||
| 246 | int RSA_sign(int type, const unsigned char *m, unsigned int m_length, | ||
| 247 | unsigned char *sigret, unsigned int *siglen, RSA *rsa); | ||
| 248 | int RSA_verify(int type, const unsigned char *m, unsigned int m_length, | ||
| 249 | unsigned char *sigbuf, unsigned int siglen, RSA *rsa); | ||
| 250 | |||
| 251 | /* The following 2 function sign and verify a ASN1_OCTET_STRING | ||
| 252 | * object inside PKCS#1 padded RSA encryption */ | ||
| 253 | int RSA_sign_ASN1_OCTET_STRING(int type, | ||
| 254 | const unsigned char *m, unsigned int m_length, | ||
| 255 | unsigned char *sigret, unsigned int *siglen, RSA *rsa); | ||
| 256 | int RSA_verify_ASN1_OCTET_STRING(int type, | ||
| 257 | const unsigned char *m, unsigned int m_length, | ||
| 258 | unsigned char *sigbuf, unsigned int siglen, RSA *rsa); | ||
| 259 | |||
| 260 | int RSA_blinding_on(RSA *rsa, BN_CTX *ctx); | ||
| 261 | void RSA_blinding_off(RSA *rsa); | ||
| 262 | |||
| 263 | int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen, | ||
| 264 | const unsigned char *f,int fl); | ||
| 265 | int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen, | ||
| 266 | const unsigned char *f,int fl,int rsa_len); | ||
| 267 | int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen, | ||
| 268 | const unsigned char *f,int fl); | ||
| 269 | int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen, | ||
| 270 | const unsigned char *f,int fl,int rsa_len); | ||
| 271 | int RSA_padding_add_PKCS1_OAEP(unsigned char *to,int tlen, | ||
| 272 | const unsigned char *f,int fl, | ||
| 273 | const unsigned char *p,int pl); | ||
| 274 | int RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen, | ||
| 275 | const unsigned char *f,int fl,int rsa_len, | ||
| 276 | const unsigned char *p,int pl); | ||
| 277 | int RSA_padding_add_SSLv23(unsigned char *to,int tlen, | ||
| 278 | const unsigned char *f,int fl); | ||
| 279 | int RSA_padding_check_SSLv23(unsigned char *to,int tlen, | ||
| 280 | const unsigned char *f,int fl,int rsa_len); | ||
| 281 | int RSA_padding_add_none(unsigned char *to,int tlen, | ||
| 282 | const unsigned char *f,int fl); | ||
| 283 | int RSA_padding_check_none(unsigned char *to,int tlen, | ||
| 284 | const unsigned char *f,int fl,int rsa_len); | ||
| 285 | |||
| 286 | int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 287 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); | ||
| 288 | int RSA_set_ex_data(RSA *r,int idx,void *arg); | ||
| 289 | void *RSA_get_ex_data(const RSA *r, int idx); | ||
| 290 | |||
| 291 | RSA *RSAPublicKey_dup(RSA *rsa); | ||
| 292 | RSA *RSAPrivateKey_dup(RSA *rsa); | ||
| 293 | |||
| 294 | /* BEGIN ERROR CODES */ | ||
| 295 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
| 296 | * made after this point may be overwritten when the script is next run. | ||
| 297 | */ | ||
| 298 | void ERR_load_RSA_strings(void); | ||
| 299 | |||
| 300 | /* Error codes for the RSA functions. */ | ||
| 301 | |||
| 302 | /* Function codes. */ | ||
| 303 | #define RSA_F_MEMORY_LOCK 100 | ||
| 304 | #define RSA_F_RSA_CHECK_KEY 123 | ||
| 305 | #define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101 | ||
| 306 | #define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102 | ||
| 307 | #define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103 | ||
| 308 | #define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104 | ||
| 309 | #define RSA_F_RSA_GENERATE_KEY 105 | ||
| 310 | #define RSA_F_RSA_NEW_METHOD 106 | ||
| 311 | #define RSA_F_RSA_NULL 124 | ||
| 312 | #define RSA_F_RSA_PADDING_ADD_NONE 107 | ||
| 313 | #define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121 | ||
| 314 | #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108 | ||
| 315 | #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109 | ||
| 316 | #define RSA_F_RSA_PADDING_ADD_SSLV23 110 | ||
| 317 | #define RSA_F_RSA_PADDING_CHECK_NONE 111 | ||
| 318 | #define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122 | ||
| 319 | #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112 | ||
| 320 | #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113 | ||
| 321 | #define RSA_F_RSA_PADDING_CHECK_SSLV23 114 | ||
| 322 | #define RSA_F_RSA_PRINT 115 | ||
| 323 | #define RSA_F_RSA_PRINT_FP 116 | ||
| 324 | #define RSA_F_RSA_SIGN 117 | ||
| 325 | #define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118 | ||
| 326 | #define RSA_F_RSA_VERIFY 119 | ||
| 327 | #define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120 | ||
| 328 | |||
| 329 | /* Reason codes. */ | ||
| 330 | #define RSA_R_ALGORITHM_MISMATCH 100 | ||
| 331 | #define RSA_R_BAD_E_VALUE 101 | ||
| 332 | #define RSA_R_BAD_FIXED_HEADER_DECRYPT 102 | ||
| 333 | #define RSA_R_BAD_PAD_BYTE_COUNT 103 | ||
| 334 | #define RSA_R_BAD_SIGNATURE 104 | ||
| 335 | #define RSA_R_BLOCK_TYPE_IS_NOT_01 106 | ||
| 336 | #define RSA_R_BLOCK_TYPE_IS_NOT_02 107 | ||
| 337 | #define RSA_R_DATA_GREATER_THAN_MOD_LEN 108 | ||
| 338 | #define RSA_R_DATA_TOO_LARGE 109 | ||
| 339 | #define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110 | ||
| 340 | #define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132 | ||
| 341 | #define RSA_R_DATA_TOO_SMALL 111 | ||
| 342 | #define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122 | ||
| 343 | #define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112 | ||
| 344 | #define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124 | ||
| 345 | #define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125 | ||
| 346 | #define RSA_R_D_E_NOT_CONGRUENT_TO_1 123 | ||
| 347 | #define RSA_R_INVALID_MESSAGE_LENGTH 131 | ||
| 348 | #define RSA_R_IQMP_NOT_INVERSE_OF_Q 126 | ||
| 349 | #define RSA_R_KEY_SIZE_TOO_SMALL 120 | ||
| 350 | #define RSA_R_NULL_BEFORE_BLOCK_MISSING 113 | ||
| 351 | #define RSA_R_N_DOES_NOT_EQUAL_P_Q 127 | ||
| 352 | #define RSA_R_OAEP_DECODING_ERROR 121 | ||
| 353 | #define RSA_R_PADDING_CHECK_FAILED 114 | ||
| 354 | #define RSA_R_P_NOT_PRIME 128 | ||
| 355 | #define RSA_R_Q_NOT_PRIME 129 | ||
| 356 | #define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130 | ||
| 357 | #define RSA_R_SSLV3_ROLLBACK_ATTACK 115 | ||
| 358 | #define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116 | ||
| 359 | #define RSA_R_UNKNOWN_ALGORITHM_TYPE 117 | ||
| 360 | #define RSA_R_UNKNOWN_PADDING_TYPE 118 | ||
| 361 | #define RSA_R_WRONG_SIGNATURE_LENGTH 119 | ||
| 362 | |||
| 363 | #ifdef __cplusplus | ||
| 364 | } | ||
| 365 | #endif | ||
| 366 | #endif | ||
diff --git a/src/lib/libcrypto/rsa/rsa_asn1.c b/src/lib/libcrypto/rsa/rsa_asn1.c new file mode 100644 index 0000000000..1455a7e0e4 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_asn1.c | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | /* rsa_asn1.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project 2000. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2000 The OpenSSL Project. All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * | ||
| 12 | * 1. Redistributions of source code must retain the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer. | ||
| 14 | * | ||
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 16 | * notice, this list of conditions and the following disclaimer in | ||
| 17 | * the documentation and/or other materials provided with the | ||
| 18 | * distribution. | ||
| 19 | * | ||
| 20 | * 3. All advertising materials mentioning features or use of this | ||
| 21 | * software must display the following acknowledgment: | ||
| 22 | * "This product includes software developed by the OpenSSL Project | ||
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 24 | * | ||
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 26 | * endorse or promote products derived from this software without | ||
| 27 | * prior written permission. For written permission, please contact | ||
| 28 | * licensing@OpenSSL.org. | ||
| 29 | * | ||
| 30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 31 | * nor may "OpenSSL" appear in their names without prior written | ||
| 32 | * permission of the OpenSSL Project. | ||
| 33 | * | ||
| 34 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 35 | * acknowledgment: | ||
| 36 | * "This product includes software developed by the OpenSSL Project | ||
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 38 | * | ||
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 51 | * ==================================================================== | ||
| 52 | * | ||
| 53 | * This product includes cryptographic software written by Eric Young | ||
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 55 | * Hudson (tjh@cryptsoft.com). | ||
| 56 | * | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/rsa.h> | ||
| 63 | #include <openssl/asn1t.h> | ||
| 64 | |||
| 65 | static ASN1_METHOD method={ | ||
| 66 | (int (*)()) i2d_RSAPrivateKey, | ||
| 67 | (char *(*)())d2i_RSAPrivateKey, | ||
| 68 | (char *(*)())RSA_new, | ||
| 69 | (void (*)()) RSA_free}; | ||
| 70 | |||
| 71 | ASN1_METHOD *RSAPrivateKey_asn1_meth(void) | ||
| 72 | { | ||
| 73 | return(&method); | ||
| 74 | } | ||
| 75 | |||
| 76 | /* Override the default free and new methods */ | ||
| 77 | static int rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
| 78 | { | ||
| 79 | if(operation == ASN1_OP_NEW_PRE) { | ||
| 80 | *pval = (ASN1_VALUE *)RSA_new(); | ||
| 81 | if(*pval) return 2; | ||
| 82 | return 0; | ||
| 83 | } else if(operation == ASN1_OP_FREE_PRE) { | ||
| 84 | RSA_free((RSA *)*pval); | ||
| 85 | *pval = NULL; | ||
| 86 | return 2; | ||
| 87 | } | ||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | ASN1_SEQUENCE_cb(RSAPrivateKey, rsa_cb) = { | ||
| 92 | ASN1_SIMPLE(RSA, version, LONG), | ||
| 93 | ASN1_SIMPLE(RSA, n, BIGNUM), | ||
| 94 | ASN1_SIMPLE(RSA, e, BIGNUM), | ||
| 95 | ASN1_SIMPLE(RSA, d, BIGNUM), | ||
| 96 | ASN1_SIMPLE(RSA, p, BIGNUM), | ||
| 97 | ASN1_SIMPLE(RSA, q, BIGNUM), | ||
| 98 | ASN1_SIMPLE(RSA, dmp1, BIGNUM), | ||
| 99 | ASN1_SIMPLE(RSA, dmq1, BIGNUM), | ||
| 100 | ASN1_SIMPLE(RSA, iqmp, BIGNUM) | ||
| 101 | } ASN1_SEQUENCE_END_cb(RSA, RSAPrivateKey) | ||
| 102 | |||
| 103 | |||
| 104 | ASN1_SEQUENCE_cb(RSAPublicKey, rsa_cb) = { | ||
| 105 | ASN1_SIMPLE(RSA, n, BIGNUM), | ||
| 106 | ASN1_SIMPLE(RSA, e, BIGNUM), | ||
| 107 | } ASN1_SEQUENCE_END_cb(RSA, RSAPublicKey) | ||
| 108 | |||
| 109 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPrivateKey, RSAPrivateKey) | ||
| 110 | |||
| 111 | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPublicKey, RSAPublicKey) | ||
| 112 | |||
| 113 | RSA *RSAPublicKey_dup(RSA *rsa) | ||
| 114 | { | ||
| 115 | return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa); | ||
| 116 | } | ||
| 117 | |||
| 118 | RSA *RSAPrivateKey_dup(RSA *rsa) | ||
| 119 | { | ||
| 120 | return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa); | ||
| 121 | } | ||
diff --git a/src/lib/libcrypto/rsa/rsa_chk.c b/src/lib/libcrypto/rsa/rsa_chk.c new file mode 100644 index 0000000000..002f2cb487 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_chk.c | |||
| @@ -0,0 +1,184 @@ | |||
| 1 | /* crypto/rsa/rsa_chk.c -*- Mode: C; c-file-style: "eay" -*- */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@OpenSSL.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | */ | ||
| 50 | |||
| 51 | #include <openssl/bn.h> | ||
| 52 | #include <openssl/err.h> | ||
| 53 | #include <openssl/rsa.h> | ||
| 54 | |||
| 55 | |||
| 56 | int RSA_check_key(const RSA *key) | ||
| 57 | { | ||
| 58 | BIGNUM *i, *j, *k, *l, *m; | ||
| 59 | BN_CTX *ctx; | ||
| 60 | int r; | ||
| 61 | int ret=1; | ||
| 62 | |||
| 63 | i = BN_new(); | ||
| 64 | j = BN_new(); | ||
| 65 | k = BN_new(); | ||
| 66 | l = BN_new(); | ||
| 67 | m = BN_new(); | ||
| 68 | ctx = BN_CTX_new(); | ||
| 69 | if (i == NULL || j == NULL || k == NULL || l == NULL || | ||
| 70 | m == NULL || ctx == NULL) | ||
| 71 | { | ||
| 72 | ret = -1; | ||
| 73 | RSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE); | ||
| 74 | goto err; | ||
| 75 | } | ||
| 76 | |||
| 77 | /* p prime? */ | ||
| 78 | r = BN_is_prime(key->p, BN_prime_checks, NULL, NULL, NULL); | ||
| 79 | if (r != 1) | ||
| 80 | { | ||
| 81 | ret = r; | ||
| 82 | if (r != 0) | ||
| 83 | goto err; | ||
| 84 | RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME); | ||
| 85 | } | ||
| 86 | |||
| 87 | /* q prime? */ | ||
| 88 | r = BN_is_prime(key->q, BN_prime_checks, NULL, NULL, NULL); | ||
| 89 | if (r != 1) | ||
| 90 | { | ||
| 91 | ret = r; | ||
| 92 | if (r != 0) | ||
| 93 | goto err; | ||
| 94 | RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME); | ||
| 95 | } | ||
| 96 | |||
| 97 | /* n = p*q? */ | ||
| 98 | r = BN_mul(i, key->p, key->q, ctx); | ||
| 99 | if (!r) { ret = -1; goto err; } | ||
| 100 | |||
| 101 | if (BN_cmp(i, key->n) != 0) | ||
| 102 | { | ||
| 103 | ret = 0; | ||
| 104 | RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q); | ||
| 105 | } | ||
| 106 | |||
| 107 | /* d*e = 1 mod lcm(p-1,q-1)? */ | ||
| 108 | |||
| 109 | r = BN_sub(i, key->p, BN_value_one()); | ||
| 110 | if (!r) { ret = -1; goto err; } | ||
| 111 | r = BN_sub(j, key->q, BN_value_one()); | ||
| 112 | if (!r) { ret = -1; goto err; } | ||
| 113 | |||
| 114 | /* now compute k = lcm(i,j) */ | ||
| 115 | r = BN_mul(l, i, j, ctx); | ||
| 116 | if (!r) { ret = -1; goto err; } | ||
| 117 | r = BN_gcd(m, i, j, ctx); | ||
| 118 | if (!r) { ret = -1; goto err; } | ||
| 119 | r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */ | ||
| 120 | if (!r) { ret = -1; goto err; } | ||
| 121 | |||
| 122 | r = BN_mod_mul(i, key->d, key->e, k, ctx); | ||
| 123 | if (!r) { ret = -1; goto err; } | ||
| 124 | |||
| 125 | if (!BN_is_one(i)) | ||
| 126 | { | ||
| 127 | ret = 0; | ||
| 128 | RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1); | ||
| 129 | } | ||
| 130 | |||
| 131 | if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) | ||
| 132 | { | ||
| 133 | /* dmp1 = d mod (p-1)? */ | ||
| 134 | r = BN_sub(i, key->p, BN_value_one()); | ||
| 135 | if (!r) { ret = -1; goto err; } | ||
| 136 | |||
| 137 | r = BN_mod(j, key->d, i, ctx); | ||
| 138 | if (!r) { ret = -1; goto err; } | ||
| 139 | |||
| 140 | if (BN_cmp(j, key->dmp1) != 0) | ||
| 141 | { | ||
| 142 | ret = 0; | ||
| 143 | RSAerr(RSA_F_RSA_CHECK_KEY, | ||
| 144 | RSA_R_DMP1_NOT_CONGRUENT_TO_D); | ||
| 145 | } | ||
| 146 | |||
| 147 | /* dmq1 = d mod (q-1)? */ | ||
| 148 | r = BN_sub(i, key->q, BN_value_one()); | ||
| 149 | if (!r) { ret = -1; goto err; } | ||
| 150 | |||
| 151 | r = BN_mod(j, key->d, i, ctx); | ||
| 152 | if (!r) { ret = -1; goto err; } | ||
| 153 | |||
| 154 | if (BN_cmp(j, key->dmq1) != 0) | ||
| 155 | { | ||
| 156 | ret = 0; | ||
| 157 | RSAerr(RSA_F_RSA_CHECK_KEY, | ||
| 158 | RSA_R_DMQ1_NOT_CONGRUENT_TO_D); | ||
| 159 | } | ||
| 160 | |||
| 161 | /* iqmp = q^-1 mod p? */ | ||
| 162 | if(!BN_mod_inverse(i, key->q, key->p, ctx)) | ||
| 163 | { | ||
| 164 | ret = -1; | ||
| 165 | goto err; | ||
| 166 | } | ||
| 167 | |||
| 168 | if (BN_cmp(i, key->iqmp) != 0) | ||
| 169 | { | ||
| 170 | ret = 0; | ||
| 171 | RSAerr(RSA_F_RSA_CHECK_KEY, | ||
| 172 | RSA_R_IQMP_NOT_INVERSE_OF_Q); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | err: | ||
| 177 | if (i != NULL) BN_free(i); | ||
| 178 | if (j != NULL) BN_free(j); | ||
| 179 | if (k != NULL) BN_free(k); | ||
| 180 | if (l != NULL) BN_free(l); | ||
| 181 | if (m != NULL) BN_free(m); | ||
| 182 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 183 | return (ret); | ||
| 184 | } | ||
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c new file mode 100644 index 0000000000..d4caab3f95 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_eay.c | |||
| @@ -0,0 +1,727 @@ | |||
| 1 | /* crypto/rsa/rsa_eay.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/rsa.h> | ||
| 63 | #include <openssl/rand.h> | ||
| 64 | |||
| 65 | #if !defined(RSA_NULL) && !defined(OPENSSL_FIPS) | ||
| 66 | |||
| 67 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, | ||
| 68 | unsigned char *to, RSA *rsa,int padding); | ||
| 69 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, | ||
| 70 | unsigned char *to, RSA *rsa,int padding); | ||
| 71 | static int RSA_eay_public_decrypt(int flen, const unsigned char *from, | ||
| 72 | unsigned char *to, RSA *rsa,int padding); | ||
| 73 | static int RSA_eay_private_decrypt(int flen, const unsigned char *from, | ||
| 74 | unsigned char *to, RSA *rsa,int padding); | ||
| 75 | static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa); | ||
| 76 | static int RSA_eay_init(RSA *rsa); | ||
| 77 | static int RSA_eay_finish(RSA *rsa); | ||
| 78 | static RSA_METHOD rsa_pkcs1_eay_meth={ | ||
| 79 | "Eric Young's PKCS#1 RSA", | ||
| 80 | RSA_eay_public_encrypt, | ||
| 81 | RSA_eay_public_decrypt, /* signature verification */ | ||
| 82 | RSA_eay_private_encrypt, /* signing */ | ||
| 83 | RSA_eay_private_decrypt, | ||
| 84 | RSA_eay_mod_exp, | ||
| 85 | BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */ | ||
| 86 | RSA_eay_init, | ||
| 87 | RSA_eay_finish, | ||
| 88 | 0, /* flags */ | ||
| 89 | NULL, | ||
| 90 | 0, /* rsa_sign */ | ||
| 91 | 0 /* rsa_verify */ | ||
| 92 | }; | ||
| 93 | |||
| 94 | const RSA_METHOD *RSA_PKCS1_SSLeay(void) | ||
| 95 | { | ||
| 96 | return(&rsa_pkcs1_eay_meth); | ||
| 97 | } | ||
| 98 | |||
| 99 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, | ||
| 100 | unsigned char *to, RSA *rsa, int padding) | ||
| 101 | { | ||
| 102 | BIGNUM f,ret; | ||
| 103 | int i,j,k,num=0,r= -1; | ||
| 104 | unsigned char *buf=NULL; | ||
| 105 | BN_CTX *ctx=NULL; | ||
| 106 | |||
| 107 | BN_init(&f); | ||
| 108 | BN_init(&ret); | ||
| 109 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 110 | num=BN_num_bytes(rsa->n); | ||
| 111 | if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
| 112 | { | ||
| 113 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 114 | goto err; | ||
| 115 | } | ||
| 116 | |||
| 117 | switch (padding) | ||
| 118 | { | ||
| 119 | case RSA_PKCS1_PADDING: | ||
| 120 | i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen); | ||
| 121 | break; | ||
| 122 | #ifndef OPENSSL_NO_SHA | ||
| 123 | case RSA_PKCS1_OAEP_PADDING: | ||
| 124 | i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0); | ||
| 125 | break; | ||
| 126 | #endif | ||
| 127 | case RSA_SSLV23_PADDING: | ||
| 128 | i=RSA_padding_add_SSLv23(buf,num,from,flen); | ||
| 129 | break; | ||
| 130 | case RSA_NO_PADDING: | ||
| 131 | i=RSA_padding_add_none(buf,num,from,flen); | ||
| 132 | break; | ||
| 133 | default: | ||
| 134 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 135 | goto err; | ||
| 136 | } | ||
| 137 | if (i <= 0) goto err; | ||
| 138 | |||
| 139 | if (BN_bin2bn(buf,num,&f) == NULL) goto err; | ||
| 140 | |||
| 141 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
| 142 | { | ||
| 143 | /* usually the padding functions would catch this */ | ||
| 144 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 145 | goto err; | ||
| 146 | } | ||
| 147 | |||
| 148 | if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC)) | ||
| 149 | { | ||
| 150 | BN_MONT_CTX* bn_mont_ctx; | ||
| 151 | if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL) | ||
| 152 | goto err; | ||
| 153 | if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx)) | ||
| 154 | { | ||
| 155 | BN_MONT_CTX_free(bn_mont_ctx); | ||
| 156 | goto err; | ||
| 157 | } | ||
| 158 | if (rsa->_method_mod_n == NULL) /* other thread may have finished first */ | ||
| 159 | { | ||
| 160 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
| 161 | if (rsa->_method_mod_n == NULL) | ||
| 162 | { | ||
| 163 | rsa->_method_mod_n = bn_mont_ctx; | ||
| 164 | bn_mont_ctx = NULL; | ||
| 165 | } | ||
| 166 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
| 167 | } | ||
| 168 | if (bn_mont_ctx) | ||
| 169 | BN_MONT_CTX_free(bn_mont_ctx); | ||
| 170 | } | ||
| 171 | |||
| 172 | if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, | ||
| 173 | rsa->_method_mod_n)) goto err; | ||
| 174 | |||
| 175 | /* put in leading 0 bytes if the number is less than the | ||
| 176 | * length of the modulus */ | ||
| 177 | j=BN_num_bytes(&ret); | ||
| 178 | i=BN_bn2bin(&ret,&(to[num-j])); | ||
| 179 | for (k=0; k<(num-i); k++) | ||
| 180 | to[k]=0; | ||
| 181 | |||
| 182 | r=num; | ||
| 183 | err: | ||
| 184 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 185 | BN_clear_free(&f); | ||
| 186 | BN_clear_free(&ret); | ||
| 187 | if (buf != NULL) | ||
| 188 | { | ||
| 189 | OPENSSL_cleanse(buf,num); | ||
| 190 | OPENSSL_free(buf); | ||
| 191 | } | ||
| 192 | return(r); | ||
| 193 | } | ||
| 194 | |||
| 195 | static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx) | ||
| 196 | { | ||
| 197 | int ret = 1; | ||
| 198 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
| 199 | /* Check again inside the lock - the macro's check is racey */ | ||
| 200 | if(rsa->blinding == NULL) | ||
| 201 | ret = RSA_blinding_on(rsa, ctx); | ||
| 202 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
| 203 | return ret; | ||
| 204 | } | ||
| 205 | |||
| 206 | #define BLINDING_HELPER(rsa, ctx, err_instr) \ | ||
| 207 | do { \ | ||
| 208 | if((!((rsa)->flags & RSA_FLAG_NO_BLINDING)) && \ | ||
| 209 | ((rsa)->blinding == NULL) && \ | ||
| 210 | !rsa_eay_blinding(rsa, ctx)) \ | ||
| 211 | err_instr \ | ||
| 212 | } while(0) | ||
| 213 | |||
| 214 | static BN_BLINDING *setup_blinding(RSA *rsa, BN_CTX *ctx) | ||
| 215 | { | ||
| 216 | BIGNUM *A, *Ai; | ||
| 217 | BN_BLINDING *ret = NULL; | ||
| 218 | |||
| 219 | /* added in OpenSSL 0.9.6j and 0.9.7b */ | ||
| 220 | |||
| 221 | /* NB: similar code appears in RSA_blinding_on (rsa_lib.c); | ||
| 222 | * this should be placed in a new function of its own, but for reasons | ||
| 223 | * of binary compatibility can't */ | ||
| 224 | |||
| 225 | BN_CTX_start(ctx); | ||
| 226 | A = BN_CTX_get(ctx); | ||
| 227 | if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL) | ||
| 228 | { | ||
| 229 | /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */ | ||
| 230 | RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0); | ||
| 231 | if (!BN_pseudo_rand_range(A,rsa->n)) goto err; | ||
| 232 | } | ||
| 233 | else | ||
| 234 | { | ||
| 235 | if (!BN_rand_range(A,rsa->n)) goto err; | ||
| 236 | } | ||
| 237 | if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; | ||
| 238 | |||
| 239 | if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) | ||
| 240 | goto err; | ||
| 241 | ret = BN_BLINDING_new(A,Ai,rsa->n); | ||
| 242 | BN_free(Ai); | ||
| 243 | err: | ||
| 244 | BN_CTX_end(ctx); | ||
| 245 | return ret; | ||
| 246 | } | ||
| 247 | |||
| 248 | /* signing */ | ||
| 249 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, | ||
| 250 | unsigned char *to, RSA *rsa, int padding) | ||
| 251 | { | ||
| 252 | BIGNUM f,ret; | ||
| 253 | int i,j,k,num=0,r= -1; | ||
| 254 | unsigned char *buf=NULL; | ||
| 255 | BN_CTX *ctx=NULL; | ||
| 256 | int local_blinding = 0; | ||
| 257 | BN_BLINDING *blinding = NULL; | ||
| 258 | |||
| 259 | BN_init(&f); | ||
| 260 | BN_init(&ret); | ||
| 261 | |||
| 262 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 263 | num=BN_num_bytes(rsa->n); | ||
| 264 | if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
| 265 | { | ||
| 266 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 267 | goto err; | ||
| 268 | } | ||
| 269 | |||
| 270 | switch (padding) | ||
| 271 | { | ||
| 272 | case RSA_PKCS1_PADDING: | ||
| 273 | i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen); | ||
| 274 | break; | ||
| 275 | case RSA_NO_PADDING: | ||
| 276 | i=RSA_padding_add_none(buf,num,from,flen); | ||
| 277 | break; | ||
| 278 | case RSA_SSLV23_PADDING: | ||
| 279 | default: | ||
| 280 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 281 | goto err; | ||
| 282 | } | ||
| 283 | if (i <= 0) goto err; | ||
| 284 | |||
| 285 | if (BN_bin2bn(buf,num,&f) == NULL) goto err; | ||
| 286 | |||
| 287 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
| 288 | { | ||
| 289 | /* usually the padding functions would catch this */ | ||
| 290 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 291 | goto err; | ||
| 292 | } | ||
| 293 | |||
| 294 | BLINDING_HELPER(rsa, ctx, goto err;); | ||
| 295 | blinding = rsa->blinding; | ||
| 296 | |||
| 297 | /* Now unless blinding is disabled, 'blinding' is non-NULL. | ||
| 298 | * But the BN_BLINDING object may be owned by some other thread | ||
| 299 | * (we don't want to keep it constant and we don't want to use | ||
| 300 | * lots of locking to avoid race conditions, so only a single | ||
| 301 | * thread can use it; other threads have to use local blinding | ||
| 302 | * factors) */ | ||
| 303 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) | ||
| 304 | { | ||
| 305 | if (blinding == NULL) | ||
| 306 | { | ||
| 307 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); | ||
| 308 | goto err; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | if (blinding != NULL) | ||
| 313 | { | ||
| 314 | if (blinding->thread_id != CRYPTO_thread_id()) | ||
| 315 | { | ||
| 316 | /* we need a local one-time blinding factor */ | ||
| 317 | |||
| 318 | blinding = setup_blinding(rsa, ctx); | ||
| 319 | if (blinding == NULL) | ||
| 320 | goto err; | ||
| 321 | local_blinding = 1; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | if (blinding) | ||
| 326 | if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err; | ||
| 327 | |||
| 328 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || | ||
| 329 | ((rsa->p != NULL) && | ||
| 330 | (rsa->q != NULL) && | ||
| 331 | (rsa->dmp1 != NULL) && | ||
| 332 | (rsa->dmq1 != NULL) && | ||
| 333 | (rsa->iqmp != NULL)) ) | ||
| 334 | { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; } | ||
| 335 | else | ||
| 336 | { | ||
| 337 | if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err; | ||
| 338 | } | ||
| 339 | |||
| 340 | if (blinding) | ||
| 341 | if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err; | ||
| 342 | |||
| 343 | /* put in leading 0 bytes if the number is less than the | ||
| 344 | * length of the modulus */ | ||
| 345 | j=BN_num_bytes(&ret); | ||
| 346 | i=BN_bn2bin(&ret,&(to[num-j])); | ||
| 347 | for (k=0; k<(num-i); k++) | ||
| 348 | to[k]=0; | ||
| 349 | |||
| 350 | r=num; | ||
| 351 | err: | ||
| 352 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 353 | BN_clear_free(&ret); | ||
| 354 | BN_clear_free(&f); | ||
| 355 | if (local_blinding) | ||
| 356 | BN_BLINDING_free(blinding); | ||
| 357 | if (buf != NULL) | ||
| 358 | { | ||
| 359 | OPENSSL_cleanse(buf,num); | ||
| 360 | OPENSSL_free(buf); | ||
| 361 | } | ||
| 362 | return(r); | ||
| 363 | } | ||
| 364 | |||
| 365 | static int RSA_eay_private_decrypt(int flen, const unsigned char *from, | ||
| 366 | unsigned char *to, RSA *rsa, int padding) | ||
| 367 | { | ||
| 368 | BIGNUM f,ret; | ||
| 369 | int j,num=0,r= -1; | ||
| 370 | unsigned char *p; | ||
| 371 | unsigned char *buf=NULL; | ||
| 372 | BN_CTX *ctx=NULL; | ||
| 373 | int local_blinding = 0; | ||
| 374 | BN_BLINDING *blinding = NULL; | ||
| 375 | |||
| 376 | BN_init(&f); | ||
| 377 | BN_init(&ret); | ||
| 378 | ctx=BN_CTX_new(); | ||
| 379 | if (ctx == NULL) goto err; | ||
| 380 | |||
| 381 | num=BN_num_bytes(rsa->n); | ||
| 382 | |||
| 383 | if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
| 384 | { | ||
| 385 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); | ||
| 386 | goto err; | ||
| 387 | } | ||
| 388 | |||
| 389 | /* This check was for equality but PGP does evil things | ||
| 390 | * and chops off the top '0' bytes */ | ||
| 391 | if (flen > num) | ||
| 392 | { | ||
| 393 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | ||
| 394 | goto err; | ||
| 395 | } | ||
| 396 | |||
| 397 | /* make data into a big number */ | ||
| 398 | if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err; | ||
| 399 | |||
| 400 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
| 401 | { | ||
| 402 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 403 | goto err; | ||
| 404 | } | ||
| 405 | |||
| 406 | BLINDING_HELPER(rsa, ctx, goto err;); | ||
| 407 | blinding = rsa->blinding; | ||
| 408 | |||
| 409 | /* Now unless blinding is disabled, 'blinding' is non-NULL. | ||
| 410 | * But the BN_BLINDING object may be owned by some other thread | ||
| 411 | * (we don't want to keep it constant and we don't want to use | ||
| 412 | * lots of locking to avoid race conditions, so only a single | ||
| 413 | * thread can use it; other threads have to use local blinding | ||
| 414 | * factors) */ | ||
| 415 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) | ||
| 416 | { | ||
| 417 | if (blinding == NULL) | ||
| 418 | { | ||
| 419 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR); | ||
| 420 | goto err; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | if (blinding != NULL) | ||
| 425 | { | ||
| 426 | if (blinding->thread_id != CRYPTO_thread_id()) | ||
| 427 | { | ||
| 428 | /* we need a local one-time blinding factor */ | ||
| 429 | |||
| 430 | blinding = setup_blinding(rsa, ctx); | ||
| 431 | if (blinding == NULL) | ||
| 432 | goto err; | ||
| 433 | local_blinding = 1; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 | if (blinding) | ||
| 438 | if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err; | ||
| 439 | |||
| 440 | /* do the decrypt */ | ||
| 441 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || | ||
| 442 | ((rsa->p != NULL) && | ||
| 443 | (rsa->q != NULL) && | ||
| 444 | (rsa->dmp1 != NULL) && | ||
| 445 | (rsa->dmq1 != NULL) && | ||
| 446 | (rsa->iqmp != NULL)) ) | ||
| 447 | { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; } | ||
| 448 | else | ||
| 449 | { | ||
| 450 | if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) | ||
| 451 | goto err; | ||
| 452 | } | ||
| 453 | |||
| 454 | if (blinding) | ||
| 455 | if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err; | ||
| 456 | |||
| 457 | p=buf; | ||
| 458 | j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */ | ||
| 459 | |||
| 460 | switch (padding) | ||
| 461 | { | ||
| 462 | case RSA_PKCS1_PADDING: | ||
| 463 | r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num); | ||
| 464 | break; | ||
| 465 | #ifndef OPENSSL_NO_SHA | ||
| 466 | case RSA_PKCS1_OAEP_PADDING: | ||
| 467 | r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0); | ||
| 468 | break; | ||
| 469 | #endif | ||
| 470 | case RSA_SSLV23_PADDING: | ||
| 471 | r=RSA_padding_check_SSLv23(to,num,buf,j,num); | ||
| 472 | break; | ||
| 473 | case RSA_NO_PADDING: | ||
| 474 | r=RSA_padding_check_none(to,num,buf,j,num); | ||
| 475 | break; | ||
| 476 | default: | ||
| 477 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 478 | goto err; | ||
| 479 | } | ||
| 480 | if (r < 0) | ||
| 481 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | ||
| 482 | |||
| 483 | err: | ||
| 484 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 485 | BN_clear_free(&f); | ||
| 486 | BN_clear_free(&ret); | ||
| 487 | if (local_blinding) | ||
| 488 | BN_BLINDING_free(blinding); | ||
| 489 | if (buf != NULL) | ||
| 490 | { | ||
| 491 | OPENSSL_cleanse(buf,num); | ||
| 492 | OPENSSL_free(buf); | ||
| 493 | } | ||
| 494 | return(r); | ||
| 495 | } | ||
| 496 | |||
| 497 | /* signature verification */ | ||
| 498 | static int RSA_eay_public_decrypt(int flen, const unsigned char *from, | ||
| 499 | unsigned char *to, RSA *rsa, int padding) | ||
| 500 | { | ||
| 501 | BIGNUM f,ret; | ||
| 502 | int i,num=0,r= -1; | ||
| 503 | unsigned char *p; | ||
| 504 | unsigned char *buf=NULL; | ||
| 505 | BN_CTX *ctx=NULL; | ||
| 506 | |||
| 507 | BN_init(&f); | ||
| 508 | BN_init(&ret); | ||
| 509 | ctx=BN_CTX_new(); | ||
| 510 | if (ctx == NULL) goto err; | ||
| 511 | |||
| 512 | num=BN_num_bytes(rsa->n); | ||
| 513 | buf=(unsigned char *)OPENSSL_malloc(num); | ||
| 514 | if (buf == NULL) | ||
| 515 | { | ||
| 516 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE); | ||
| 517 | goto err; | ||
| 518 | } | ||
| 519 | |||
| 520 | /* This check was for equality but PGP does evil things | ||
| 521 | * and chops off the top '0' bytes */ | ||
| 522 | if (flen > num) | ||
| 523 | { | ||
| 524 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | ||
| 525 | goto err; | ||
| 526 | } | ||
| 527 | |||
| 528 | if (BN_bin2bn(from,flen,&f) == NULL) goto err; | ||
| 529 | |||
| 530 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
| 531 | { | ||
| 532 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 533 | goto err; | ||
| 534 | } | ||
| 535 | |||
| 536 | /* do the decrypt */ | ||
| 537 | if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC)) | ||
| 538 | { | ||
| 539 | BN_MONT_CTX* bn_mont_ctx; | ||
| 540 | if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL) | ||
| 541 | goto err; | ||
| 542 | if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx)) | ||
| 543 | { | ||
| 544 | BN_MONT_CTX_free(bn_mont_ctx); | ||
| 545 | goto err; | ||
| 546 | } | ||
| 547 | if (rsa->_method_mod_n == NULL) /* other thread may have finished first */ | ||
| 548 | { | ||
| 549 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
| 550 | if (rsa->_method_mod_n == NULL) | ||
| 551 | { | ||
| 552 | rsa->_method_mod_n = bn_mont_ctx; | ||
| 553 | bn_mont_ctx = NULL; | ||
| 554 | } | ||
| 555 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
| 556 | } | ||
| 557 | if (bn_mont_ctx) | ||
| 558 | BN_MONT_CTX_free(bn_mont_ctx); | ||
| 559 | } | ||
| 560 | |||
| 561 | if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, | ||
| 562 | rsa->_method_mod_n)) goto err; | ||
| 563 | |||
| 564 | p=buf; | ||
| 565 | i=BN_bn2bin(&ret,p); | ||
| 566 | |||
| 567 | switch (padding) | ||
| 568 | { | ||
| 569 | case RSA_PKCS1_PADDING: | ||
| 570 | r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num); | ||
| 571 | break; | ||
| 572 | case RSA_NO_PADDING: | ||
| 573 | r=RSA_padding_check_none(to,num,buf,i,num); | ||
| 574 | break; | ||
| 575 | default: | ||
| 576 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 577 | goto err; | ||
| 578 | } | ||
| 579 | if (r < 0) | ||
| 580 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | ||
| 581 | |||
| 582 | err: | ||
| 583 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 584 | BN_clear_free(&f); | ||
| 585 | BN_clear_free(&ret); | ||
| 586 | if (buf != NULL) | ||
| 587 | { | ||
| 588 | OPENSSL_cleanse(buf,num); | ||
| 589 | OPENSSL_free(buf); | ||
| 590 | } | ||
| 591 | return(r); | ||
| 592 | } | ||
| 593 | |||
| 594 | static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa) | ||
| 595 | { | ||
| 596 | BIGNUM r1,m1,vrfy; | ||
| 597 | int ret=0; | ||
| 598 | BN_CTX *ctx; | ||
| 599 | |||
| 600 | BN_init(&m1); | ||
| 601 | BN_init(&r1); | ||
| 602 | BN_init(&vrfy); | ||
| 603 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 604 | |||
| 605 | if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) | ||
| 606 | { | ||
| 607 | if (rsa->_method_mod_p == NULL) | ||
| 608 | { | ||
| 609 | BN_MONT_CTX* bn_mont_ctx; | ||
| 610 | if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL) | ||
| 611 | goto err; | ||
| 612 | if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->p,ctx)) | ||
| 613 | { | ||
| 614 | BN_MONT_CTX_free(bn_mont_ctx); | ||
| 615 | goto err; | ||
| 616 | } | ||
| 617 | if (rsa->_method_mod_p == NULL) /* other thread may have finished first */ | ||
| 618 | { | ||
| 619 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
| 620 | if (rsa->_method_mod_p == NULL) | ||
| 621 | { | ||
| 622 | rsa->_method_mod_p = bn_mont_ctx; | ||
| 623 | bn_mont_ctx = NULL; | ||
| 624 | } | ||
| 625 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
| 626 | } | ||
| 627 | if (bn_mont_ctx) | ||
| 628 | BN_MONT_CTX_free(bn_mont_ctx); | ||
| 629 | } | ||
| 630 | |||
| 631 | if (rsa->_method_mod_q == NULL) | ||
| 632 | { | ||
| 633 | BN_MONT_CTX* bn_mont_ctx; | ||
| 634 | if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL) | ||
| 635 | goto err; | ||
| 636 | if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->q,ctx)) | ||
| 637 | { | ||
| 638 | BN_MONT_CTX_free(bn_mont_ctx); | ||
| 639 | goto err; | ||
| 640 | } | ||
| 641 | if (rsa->_method_mod_q == NULL) /* other thread may have finished first */ | ||
| 642 | { | ||
| 643 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
| 644 | if (rsa->_method_mod_q == NULL) | ||
| 645 | { | ||
| 646 | rsa->_method_mod_q = bn_mont_ctx; | ||
| 647 | bn_mont_ctx = NULL; | ||
| 648 | } | ||
| 649 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
| 650 | } | ||
| 651 | if (bn_mont_ctx) | ||
| 652 | BN_MONT_CTX_free(bn_mont_ctx); | ||
| 653 | } | ||
| 654 | } | ||
| 655 | |||
| 656 | if (!BN_mod(&r1,I,rsa->q,ctx)) goto err; | ||
| 657 | if (!rsa->meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx, | ||
| 658 | rsa->_method_mod_q)) goto err; | ||
| 659 | |||
| 660 | if (!BN_mod(&r1,I,rsa->p,ctx)) goto err; | ||
| 661 | if (!rsa->meth->bn_mod_exp(r0,&r1,rsa->dmp1,rsa->p,ctx, | ||
| 662 | rsa->_method_mod_p)) goto err; | ||
| 663 | |||
| 664 | if (!BN_sub(r0,r0,&m1)) goto err; | ||
| 665 | /* This will help stop the size of r0 increasing, which does | ||
| 666 | * affect the multiply if it optimised for a power of 2 size */ | ||
| 667 | if (r0->neg) | ||
| 668 | if (!BN_add(r0,r0,rsa->p)) goto err; | ||
| 669 | |||
| 670 | if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err; | ||
| 671 | if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err; | ||
| 672 | /* If p < q it is occasionally possible for the correction of | ||
| 673 | * adding 'p' if r0 is negative above to leave the result still | ||
| 674 | * negative. This can break the private key operations: the following | ||
| 675 | * second correction should *always* correct this rare occurrence. | ||
| 676 | * This will *never* happen with OpenSSL generated keys because | ||
| 677 | * they ensure p > q [steve] | ||
| 678 | */ | ||
| 679 | if (r0->neg) | ||
| 680 | if (!BN_add(r0,r0,rsa->p)) goto err; | ||
| 681 | if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err; | ||
| 682 | if (!BN_add(r0,&r1,&m1)) goto err; | ||
| 683 | |||
| 684 | if (rsa->e && rsa->n) | ||
| 685 | { | ||
| 686 | if (!rsa->meth->bn_mod_exp(&vrfy,r0,rsa->e,rsa->n,ctx,NULL)) goto err; | ||
| 687 | /* If 'I' was greater than (or equal to) rsa->n, the operation | ||
| 688 | * will be equivalent to using 'I mod n'. However, the result of | ||
| 689 | * the verify will *always* be less than 'n' so we don't check | ||
| 690 | * for absolute equality, just congruency. */ | ||
| 691 | if (!BN_sub(&vrfy, &vrfy, I)) goto err; | ||
| 692 | if (!BN_mod(&vrfy, &vrfy, rsa->n, ctx)) goto err; | ||
| 693 | if (vrfy.neg) | ||
| 694 | if (!BN_add(&vrfy, &vrfy, rsa->n)) goto err; | ||
| 695 | if (!BN_is_zero(&vrfy)) | ||
| 696 | /* 'I' and 'vrfy' aren't congruent mod n. Don't leak | ||
| 697 | * miscalculated CRT output, just do a raw (slower) | ||
| 698 | * mod_exp and return that instead. */ | ||
| 699 | if (!rsa->meth->bn_mod_exp(r0,I,rsa->d,rsa->n,ctx,NULL)) goto err; | ||
| 700 | } | ||
| 701 | ret=1; | ||
| 702 | err: | ||
| 703 | BN_clear_free(&m1); | ||
| 704 | BN_clear_free(&r1); | ||
| 705 | BN_clear_free(&vrfy); | ||
| 706 | BN_CTX_free(ctx); | ||
| 707 | return(ret); | ||
| 708 | } | ||
| 709 | |||
| 710 | static int RSA_eay_init(RSA *rsa) | ||
| 711 | { | ||
| 712 | rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; | ||
| 713 | return(1); | ||
| 714 | } | ||
| 715 | |||
| 716 | static int RSA_eay_finish(RSA *rsa) | ||
| 717 | { | ||
| 718 | if (rsa->_method_mod_n != NULL) | ||
| 719 | BN_MONT_CTX_free(rsa->_method_mod_n); | ||
| 720 | if (rsa->_method_mod_p != NULL) | ||
| 721 | BN_MONT_CTX_free(rsa->_method_mod_p); | ||
| 722 | if (rsa->_method_mod_q != NULL) | ||
| 723 | BN_MONT_CTX_free(rsa->_method_mod_q); | ||
| 724 | return(1); | ||
| 725 | } | ||
| 726 | |||
| 727 | #endif | ||
diff --git a/src/lib/libcrypto/rsa/rsa_err.c b/src/lib/libcrypto/rsa/rsa_err.c new file mode 100644 index 0000000000..a7766c3b76 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_err.c | |||
| @@ -0,0 +1,149 @@ | |||
| 1 | /* crypto/rsa/rsa_err.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@OpenSSL.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | /* NOTE: this file was auto generated by the mkerr.pl script: any changes | ||
| 57 | * made to it will be overwritten when the script next updates this file, | ||
| 58 | * only reason strings will be preserved. | ||
| 59 | */ | ||
| 60 | |||
| 61 | #include <stdio.h> | ||
| 62 | #include <openssl/err.h> | ||
| 63 | #include <openssl/rsa.h> | ||
| 64 | |||
| 65 | /* BEGIN ERROR CODES */ | ||
| 66 | #ifndef OPENSSL_NO_ERR | ||
| 67 | static ERR_STRING_DATA RSA_str_functs[]= | ||
| 68 | { | ||
| 69 | {ERR_PACK(0,RSA_F_MEMORY_LOCK,0), "MEMORY_LOCK"}, | ||
| 70 | {ERR_PACK(0,RSA_F_RSA_CHECK_KEY,0), "RSA_check_key"}, | ||
| 71 | {ERR_PACK(0,RSA_F_RSA_EAY_PRIVATE_DECRYPT,0), "RSA_EAY_PRIVATE_DECRYPT"}, | ||
| 72 | {ERR_PACK(0,RSA_F_RSA_EAY_PRIVATE_ENCRYPT,0), "RSA_EAY_PRIVATE_ENCRYPT"}, | ||
| 73 | {ERR_PACK(0,RSA_F_RSA_EAY_PUBLIC_DECRYPT,0), "RSA_EAY_PUBLIC_DECRYPT"}, | ||
| 74 | {ERR_PACK(0,RSA_F_RSA_EAY_PUBLIC_ENCRYPT,0), "RSA_EAY_PUBLIC_ENCRYPT"}, | ||
| 75 | {ERR_PACK(0,RSA_F_RSA_GENERATE_KEY,0), "RSA_generate_key"}, | ||
| 76 | {ERR_PACK(0,RSA_F_RSA_NEW_METHOD,0), "RSA_new_method"}, | ||
| 77 | {ERR_PACK(0,RSA_F_RSA_NULL,0), "RSA_NULL"}, | ||
| 78 | {ERR_PACK(0,RSA_F_RSA_PADDING_ADD_NONE,0), "RSA_padding_add_none"}, | ||
| 79 | {ERR_PACK(0,RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,0), "RSA_padding_add_PKCS1_OAEP"}, | ||
| 80 | {ERR_PACK(0,RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,0), "RSA_padding_add_PKCS1_type_1"}, | ||
| 81 | {ERR_PACK(0,RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,0), "RSA_padding_add_PKCS1_type_2"}, | ||
| 82 | {ERR_PACK(0,RSA_F_RSA_PADDING_ADD_SSLV23,0), "RSA_padding_add_SSLv23"}, | ||
| 83 | {ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_NONE,0), "RSA_padding_check_none"}, | ||
| 84 | {ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP,0), "RSA_padding_check_PKCS1_OAEP"}, | ||
| 85 | {ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,0), "RSA_padding_check_PKCS1_type_1"}, | ||
| 86 | {ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,0), "RSA_padding_check_PKCS1_type_2"}, | ||
| 87 | {ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_SSLV23,0), "RSA_padding_check_SSLv23"}, | ||
| 88 | {ERR_PACK(0,RSA_F_RSA_PRINT,0), "RSA_print"}, | ||
| 89 | {ERR_PACK(0,RSA_F_RSA_PRINT_FP,0), "RSA_print_fp"}, | ||
| 90 | {ERR_PACK(0,RSA_F_RSA_SIGN,0), "RSA_sign"}, | ||
| 91 | {ERR_PACK(0,RSA_F_RSA_SIGN_ASN1_OCTET_STRING,0), "RSA_sign_ASN1_OCTET_STRING"}, | ||
| 92 | {ERR_PACK(0,RSA_F_RSA_VERIFY,0), "RSA_verify"}, | ||
| 93 | {ERR_PACK(0,RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,0), "RSA_verify_ASN1_OCTET_STRING"}, | ||
| 94 | {0,NULL} | ||
| 95 | }; | ||
| 96 | |||
| 97 | static ERR_STRING_DATA RSA_str_reasons[]= | ||
| 98 | { | ||
| 99 | {RSA_R_ALGORITHM_MISMATCH ,"algorithm mismatch"}, | ||
| 100 | {RSA_R_BAD_E_VALUE ,"bad e value"}, | ||
| 101 | {RSA_R_BAD_FIXED_HEADER_DECRYPT ,"bad fixed header decrypt"}, | ||
| 102 | {RSA_R_BAD_PAD_BYTE_COUNT ,"bad pad byte count"}, | ||
| 103 | {RSA_R_BAD_SIGNATURE ,"bad signature"}, | ||
| 104 | {RSA_R_BLOCK_TYPE_IS_NOT_01 ,"block type is not 01"}, | ||
| 105 | {RSA_R_BLOCK_TYPE_IS_NOT_02 ,"block type is not 02"}, | ||
| 106 | {RSA_R_DATA_GREATER_THAN_MOD_LEN ,"data greater than mod len"}, | ||
| 107 | {RSA_R_DATA_TOO_LARGE ,"data too large"}, | ||
| 108 | {RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE ,"data too large for key size"}, | ||
| 109 | {RSA_R_DATA_TOO_LARGE_FOR_MODULUS ,"data too large for modulus"}, | ||
| 110 | {RSA_R_DATA_TOO_SMALL ,"data too small"}, | ||
| 111 | {RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE ,"data too small for key size"}, | ||
| 112 | {RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY ,"digest too big for rsa key"}, | ||
| 113 | {RSA_R_DMP1_NOT_CONGRUENT_TO_D ,"dmp1 not congruent to d"}, | ||
| 114 | {RSA_R_DMQ1_NOT_CONGRUENT_TO_D ,"dmq1 not congruent to d"}, | ||
| 115 | {RSA_R_D_E_NOT_CONGRUENT_TO_1 ,"d e not congruent to 1"}, | ||
| 116 | {RSA_R_INVALID_MESSAGE_LENGTH ,"invalid message length"}, | ||
| 117 | {RSA_R_IQMP_NOT_INVERSE_OF_Q ,"iqmp not inverse of q"}, | ||
| 118 | {RSA_R_KEY_SIZE_TOO_SMALL ,"key size too small"}, | ||
| 119 | {RSA_R_NULL_BEFORE_BLOCK_MISSING ,"null before block missing"}, | ||
| 120 | {RSA_R_N_DOES_NOT_EQUAL_P_Q ,"n does not equal p q"}, | ||
| 121 | {RSA_R_OAEP_DECODING_ERROR ,"oaep decoding error"}, | ||
| 122 | {RSA_R_PADDING_CHECK_FAILED ,"padding check failed"}, | ||
| 123 | {RSA_R_P_NOT_PRIME ,"p not prime"}, | ||
| 124 | {RSA_R_Q_NOT_PRIME ,"q not prime"}, | ||
| 125 | {RSA_R_RSA_OPERATIONS_NOT_SUPPORTED ,"rsa operations not supported"}, | ||
| 126 | {RSA_R_SSLV3_ROLLBACK_ATTACK ,"sslv3 rollback attack"}, | ||
| 127 | {RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD,"the asn1 object identifier is not known for this md"}, | ||
| 128 | {RSA_R_UNKNOWN_ALGORITHM_TYPE ,"unknown algorithm type"}, | ||
| 129 | {RSA_R_UNKNOWN_PADDING_TYPE ,"unknown padding type"}, | ||
| 130 | {RSA_R_WRONG_SIGNATURE_LENGTH ,"wrong signature length"}, | ||
| 131 | {0,NULL} | ||
| 132 | }; | ||
| 133 | |||
| 134 | #endif | ||
| 135 | |||
| 136 | void ERR_load_RSA_strings(void) | ||
| 137 | { | ||
| 138 | static int init=1; | ||
| 139 | |||
| 140 | if (init) | ||
| 141 | { | ||
| 142 | init=0; | ||
| 143 | #ifndef OPENSSL_NO_ERR | ||
| 144 | ERR_load_strings(ERR_LIB_RSA,RSA_str_functs); | ||
| 145 | ERR_load_strings(ERR_LIB_RSA,RSA_str_reasons); | ||
| 146 | #endif | ||
| 147 | |||
| 148 | } | ||
| 149 | } | ||
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c new file mode 100644 index 0000000000..adb5e34da5 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_gen.c | |||
| @@ -0,0 +1,200 @@ | |||
| 1 | /* crypto/rsa/rsa_gen.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <time.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #include <openssl/bn.h> | ||
| 63 | #include <openssl/rsa.h> | ||
| 64 | |||
| 65 | #ifndef OPENSSL_FIPS | ||
| 66 | |||
| 67 | RSA *RSA_generate_key(int bits, unsigned long e_value, | ||
| 68 | void (*callback)(int,int,void *), void *cb_arg) | ||
| 69 | { | ||
| 70 | RSA *rsa=NULL; | ||
| 71 | BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp; | ||
| 72 | int bitsp,bitsq,ok= -1,n=0,i; | ||
| 73 | BN_CTX *ctx=NULL,*ctx2=NULL; | ||
| 74 | |||
| 75 | ctx=BN_CTX_new(); | ||
| 76 | if (ctx == NULL) goto err; | ||
| 77 | ctx2=BN_CTX_new(); | ||
| 78 | if (ctx2 == NULL) goto err; | ||
| 79 | BN_CTX_start(ctx); | ||
| 80 | r0 = BN_CTX_get(ctx); | ||
| 81 | r1 = BN_CTX_get(ctx); | ||
| 82 | r2 = BN_CTX_get(ctx); | ||
| 83 | r3 = BN_CTX_get(ctx); | ||
| 84 | if (r3 == NULL) goto err; | ||
| 85 | |||
| 86 | bitsp=(bits+1)/2; | ||
| 87 | bitsq=bits-bitsp; | ||
| 88 | rsa=RSA_new(); | ||
| 89 | if (rsa == NULL) goto err; | ||
| 90 | |||
| 91 | /* set e */ | ||
| 92 | rsa->e=BN_new(); | ||
| 93 | if (rsa->e == NULL) goto err; | ||
| 94 | |||
| 95 | #if 1 | ||
| 96 | /* The problem is when building with 8, 16, or 32 BN_ULONG, | ||
| 97 | * unsigned long can be larger */ | ||
| 98 | for (i=0; i<sizeof(unsigned long)*8; i++) | ||
| 99 | { | ||
| 100 | if (e_value & (1UL<<i)) | ||
| 101 | BN_set_bit(rsa->e,i); | ||
| 102 | } | ||
| 103 | #else | ||
| 104 | if (!BN_set_word(rsa->e,e_value)) goto err; | ||
| 105 | #endif | ||
| 106 | |||
| 107 | /* generate p and q */ | ||
| 108 | for (;;) | ||
| 109 | { | ||
| 110 | rsa->p=BN_generate_prime(NULL,bitsp,0,NULL,NULL,callback,cb_arg); | ||
| 111 | if (rsa->p == NULL) goto err; | ||
| 112 | if (!BN_sub(r2,rsa->p,BN_value_one())) goto err; | ||
| 113 | if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; | ||
| 114 | if (BN_is_one(r1)) break; | ||
| 115 | if (callback != NULL) callback(2,n++,cb_arg); | ||
| 116 | BN_free(rsa->p); | ||
| 117 | } | ||
| 118 | if (callback != NULL) callback(3,0,cb_arg); | ||
| 119 | for (;;) | ||
| 120 | { | ||
| 121 | rsa->q=BN_generate_prime(NULL,bitsq,0,NULL,NULL,callback,cb_arg); | ||
| 122 | if (rsa->q == NULL) goto err; | ||
| 123 | if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; | ||
| 124 | if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; | ||
| 125 | if (BN_is_one(r1) && (BN_cmp(rsa->p,rsa->q) != 0)) | ||
| 126 | break; | ||
| 127 | if (callback != NULL) callback(2,n++,cb_arg); | ||
| 128 | BN_free(rsa->q); | ||
| 129 | } | ||
| 130 | if (callback != NULL) callback(3,1,cb_arg); | ||
| 131 | if (BN_cmp(rsa->p,rsa->q) < 0) | ||
| 132 | { | ||
| 133 | tmp=rsa->p; | ||
| 134 | rsa->p=rsa->q; | ||
| 135 | rsa->q=tmp; | ||
| 136 | } | ||
| 137 | |||
| 138 | /* calculate n */ | ||
| 139 | rsa->n=BN_new(); | ||
| 140 | if (rsa->n == NULL) goto err; | ||
| 141 | if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err; | ||
| 142 | |||
| 143 | /* calculate d */ | ||
| 144 | if (!BN_sub(r1,rsa->p,BN_value_one())) goto err; /* p-1 */ | ||
| 145 | if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; /* q-1 */ | ||
| 146 | if (!BN_mul(r0,r1,r2,ctx)) goto err; /* (p-1)(q-1) */ | ||
| 147 | |||
| 148 | /* should not be needed, since gcd(p-1,e) == 1 and gcd(q-1,e) == 1 */ | ||
| 149 | /* for (;;) | ||
| 150 | { | ||
| 151 | if (!BN_gcd(r3,r0,rsa->e,ctx)) goto err; | ||
| 152 | if (BN_is_one(r3)) break; | ||
| 153 | |||
| 154 | if (1) | ||
| 155 | { | ||
| 156 | if (!BN_add_word(rsa->e,2L)) goto err; | ||
| 157 | continue; | ||
| 158 | } | ||
| 159 | RSAerr(RSA_F_RSA_GENERATE_KEY,RSA_R_BAD_E_VALUE); | ||
| 160 | goto err; | ||
| 161 | } | ||
| 162 | */ | ||
| 163 | rsa->d=BN_mod_inverse(NULL,rsa->e,r0,ctx2); /* d */ | ||
| 164 | if (rsa->d == NULL) goto err; | ||
| 165 | |||
| 166 | /* calculate d mod (p-1) */ | ||
| 167 | rsa->dmp1=BN_new(); | ||
| 168 | if (rsa->dmp1 == NULL) goto err; | ||
| 169 | if (!BN_mod(rsa->dmp1,rsa->d,r1,ctx)) goto err; | ||
| 170 | |||
| 171 | /* calculate d mod (q-1) */ | ||
| 172 | rsa->dmq1=BN_new(); | ||
| 173 | if (rsa->dmq1 == NULL) goto err; | ||
| 174 | if (!BN_mod(rsa->dmq1,rsa->d,r2,ctx)) goto err; | ||
| 175 | |||
| 176 | /* calculate inverse of q mod p */ | ||
| 177 | rsa->iqmp=BN_mod_inverse(NULL,rsa->q,rsa->p,ctx2); | ||
| 178 | if (rsa->iqmp == NULL) goto err; | ||
| 179 | |||
| 180 | ok=1; | ||
| 181 | err: | ||
| 182 | if (ok == -1) | ||
| 183 | { | ||
| 184 | RSAerr(RSA_F_RSA_GENERATE_KEY,ERR_LIB_BN); | ||
| 185 | ok=0; | ||
| 186 | } | ||
| 187 | BN_CTX_end(ctx); | ||
| 188 | BN_CTX_free(ctx); | ||
| 189 | BN_CTX_free(ctx2); | ||
| 190 | |||
| 191 | if (!ok) | ||
| 192 | { | ||
| 193 | if (rsa != NULL) RSA_free(rsa); | ||
| 194 | return(NULL); | ||
| 195 | } | ||
| 196 | else | ||
| 197 | return(rsa); | ||
| 198 | } | ||
| 199 | |||
| 200 | #endif | ||
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c new file mode 100644 index 0000000000..e4d622851e --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_lib.c | |||
| @@ -0,0 +1,414 @@ | |||
| 1 | /* crypto/rsa/rsa_lib.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <openssl/crypto.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #include <openssl/lhash.h> | ||
| 63 | #include <openssl/bn.h> | ||
| 64 | #include <openssl/rsa.h> | ||
| 65 | #include <openssl/rand.h> | ||
| 66 | #ifndef OPENSSL_NO_ENGINE | ||
| 67 | #include <openssl/engine.h> | ||
| 68 | #endif | ||
| 69 | |||
| 70 | const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT; | ||
| 71 | |||
| 72 | static const RSA_METHOD *default_RSA_meth=NULL; | ||
| 73 | |||
| 74 | RSA *RSA_new(void) | ||
| 75 | { | ||
| 76 | RSA *r=RSA_new_method(NULL); | ||
| 77 | |||
| 78 | return r; | ||
| 79 | } | ||
| 80 | |||
| 81 | void RSA_set_default_method(const RSA_METHOD *meth) | ||
| 82 | { | ||
| 83 | default_RSA_meth = meth; | ||
| 84 | } | ||
| 85 | |||
| 86 | const RSA_METHOD *RSA_get_default_method(void) | ||
| 87 | { | ||
| 88 | if (default_RSA_meth == NULL) | ||
| 89 | { | ||
| 90 | #ifdef RSA_NULL | ||
| 91 | default_RSA_meth=RSA_null_method(); | ||
| 92 | #else | ||
| 93 | #if 0 /* was: #ifdef RSAref */ | ||
| 94 | default_RSA_meth=RSA_PKCS1_RSAref(); | ||
| 95 | #else | ||
| 96 | default_RSA_meth=RSA_PKCS1_SSLeay(); | ||
| 97 | #endif | ||
| 98 | #endif | ||
| 99 | } | ||
| 100 | |||
| 101 | return default_RSA_meth; | ||
| 102 | } | ||
| 103 | |||
| 104 | const RSA_METHOD *RSA_get_method(const RSA *rsa) | ||
| 105 | { | ||
| 106 | return rsa->meth; | ||
| 107 | } | ||
| 108 | |||
| 109 | int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) | ||
| 110 | { | ||
| 111 | /* NB: The caller is specifically setting a method, so it's not up to us | ||
| 112 | * to deal with which ENGINE it comes from. */ | ||
| 113 | const RSA_METHOD *mtmp; | ||
| 114 | mtmp = rsa->meth; | ||
| 115 | if (mtmp->finish) mtmp->finish(rsa); | ||
| 116 | #ifndef OPENSSL_NO_ENGINE | ||
| 117 | if (rsa->engine) | ||
| 118 | { | ||
| 119 | ENGINE_finish(rsa->engine); | ||
| 120 | rsa->engine = NULL; | ||
| 121 | } | ||
| 122 | #endif | ||
| 123 | rsa->meth = meth; | ||
| 124 | if (meth->init) meth->init(rsa); | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | RSA *RSA_new_method(ENGINE *engine) | ||
| 129 | { | ||
| 130 | RSA *ret; | ||
| 131 | |||
| 132 | ret=(RSA *)OPENSSL_malloc(sizeof(RSA)); | ||
| 133 | if (ret == NULL) | ||
| 134 | { | ||
| 135 | RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); | ||
| 136 | return NULL; | ||
| 137 | } | ||
| 138 | |||
| 139 | ret->meth = RSA_get_default_method(); | ||
| 140 | #ifndef OPENSSL_NO_ENGINE | ||
| 141 | if (engine) | ||
| 142 | { | ||
| 143 | if (!ENGINE_init(engine)) | ||
| 144 | { | ||
| 145 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); | ||
| 146 | OPENSSL_free(ret); | ||
| 147 | return NULL; | ||
| 148 | } | ||
| 149 | ret->engine = engine; | ||
| 150 | } | ||
| 151 | else | ||
| 152 | ret->engine = ENGINE_get_default_RSA(); | ||
| 153 | if(ret->engine) | ||
| 154 | { | ||
| 155 | ret->meth = ENGINE_get_RSA(ret->engine); | ||
| 156 | if(!ret->meth) | ||
| 157 | { | ||
| 158 | RSAerr(RSA_F_RSA_NEW_METHOD, | ||
| 159 | ERR_R_ENGINE_LIB); | ||
| 160 | ENGINE_finish(ret->engine); | ||
| 161 | OPENSSL_free(ret); | ||
| 162 | return NULL; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | #endif | ||
| 166 | |||
| 167 | ret->pad=0; | ||
| 168 | ret->version=0; | ||
| 169 | ret->n=NULL; | ||
| 170 | ret->e=NULL; | ||
| 171 | ret->d=NULL; | ||
| 172 | ret->p=NULL; | ||
| 173 | ret->q=NULL; | ||
| 174 | ret->dmp1=NULL; | ||
| 175 | ret->dmq1=NULL; | ||
| 176 | ret->iqmp=NULL; | ||
| 177 | ret->references=1; | ||
| 178 | ret->_method_mod_n=NULL; | ||
| 179 | ret->_method_mod_p=NULL; | ||
| 180 | ret->_method_mod_q=NULL; | ||
| 181 | ret->blinding=NULL; | ||
| 182 | ret->bignum_data=NULL; | ||
| 183 | ret->flags=ret->meth->flags; | ||
| 184 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); | ||
| 185 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | ||
| 186 | { | ||
| 187 | #ifndef OPENSSL_NO_ENGINE | ||
| 188 | if (ret->engine) | ||
| 189 | ENGINE_finish(ret->engine); | ||
| 190 | #endif | ||
| 191 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); | ||
| 192 | OPENSSL_free(ret); | ||
| 193 | ret=NULL; | ||
| 194 | } | ||
| 195 | return(ret); | ||
| 196 | } | ||
| 197 | |||
| 198 | void RSA_free(RSA *r) | ||
| 199 | { | ||
| 200 | int i; | ||
| 201 | |||
| 202 | if (r == NULL) return; | ||
| 203 | |||
| 204 | i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA); | ||
| 205 | #ifdef REF_PRINT | ||
| 206 | REF_PRINT("RSA",r); | ||
| 207 | #endif | ||
| 208 | if (i > 0) return; | ||
| 209 | #ifdef REF_CHECK | ||
| 210 | if (i < 0) | ||
| 211 | { | ||
| 212 | fprintf(stderr,"RSA_free, bad reference count\n"); | ||
| 213 | abort(); | ||
| 214 | } | ||
| 215 | #endif | ||
| 216 | |||
| 217 | if (r->meth->finish) | ||
| 218 | r->meth->finish(r); | ||
| 219 | #ifndef OPENSSL_NO_ENGINE | ||
| 220 | if (r->engine) | ||
| 221 | ENGINE_finish(r->engine); | ||
| 222 | #endif | ||
| 223 | |||
| 224 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); | ||
| 225 | |||
| 226 | if (r->n != NULL) BN_clear_free(r->n); | ||
| 227 | if (r->e != NULL) BN_clear_free(r->e); | ||
| 228 | if (r->d != NULL) BN_clear_free(r->d); | ||
| 229 | if (r->p != NULL) BN_clear_free(r->p); | ||
| 230 | if (r->q != NULL) BN_clear_free(r->q); | ||
| 231 | if (r->dmp1 != NULL) BN_clear_free(r->dmp1); | ||
| 232 | if (r->dmq1 != NULL) BN_clear_free(r->dmq1); | ||
| 233 | if (r->iqmp != NULL) BN_clear_free(r->iqmp); | ||
| 234 | if (r->blinding != NULL) BN_BLINDING_free(r->blinding); | ||
| 235 | if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data); | ||
| 236 | OPENSSL_free(r); | ||
| 237 | } | ||
| 238 | |||
| 239 | int RSA_up_ref(RSA *r) | ||
| 240 | { | ||
| 241 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA); | ||
| 242 | #ifdef REF_PRINT | ||
| 243 | REF_PRINT("RSA",r); | ||
| 244 | #endif | ||
| 245 | #ifdef REF_CHECK | ||
| 246 | if (i < 2) | ||
| 247 | { | ||
| 248 | fprintf(stderr, "RSA_up_ref, bad reference count\n"); | ||
| 249 | abort(); | ||
| 250 | } | ||
| 251 | #endif | ||
| 252 | return ((i > 1) ? 1 : 0); | ||
| 253 | } | ||
| 254 | |||
| 255 | int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 256 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | ||
| 257 | { | ||
| 258 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp, | ||
| 259 | new_func, dup_func, free_func); | ||
| 260 | } | ||
| 261 | |||
| 262 | int RSA_set_ex_data(RSA *r, int idx, void *arg) | ||
| 263 | { | ||
| 264 | return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); | ||
| 265 | } | ||
| 266 | |||
| 267 | void *RSA_get_ex_data(const RSA *r, int idx) | ||
| 268 | { | ||
| 269 | return(CRYPTO_get_ex_data(&r->ex_data,idx)); | ||
| 270 | } | ||
| 271 | |||
| 272 | int RSA_size(const RSA *r) | ||
| 273 | { | ||
| 274 | return(BN_num_bytes(r->n)); | ||
| 275 | } | ||
| 276 | |||
| 277 | int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, | ||
| 278 | RSA *rsa, int padding) | ||
| 279 | { | ||
| 280 | return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding)); | ||
| 281 | } | ||
| 282 | |||
| 283 | int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, | ||
| 284 | RSA *rsa, int padding) | ||
| 285 | { | ||
| 286 | return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding)); | ||
| 287 | } | ||
| 288 | |||
| 289 | int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, | ||
| 290 | RSA *rsa, int padding) | ||
| 291 | { | ||
| 292 | return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding)); | ||
| 293 | } | ||
| 294 | |||
| 295 | int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, | ||
| 296 | RSA *rsa, int padding) | ||
| 297 | { | ||
| 298 | return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding)); | ||
| 299 | } | ||
| 300 | |||
| 301 | int RSA_flags(const RSA *r) | ||
| 302 | { | ||
| 303 | return((r == NULL)?0:r->meth->flags); | ||
| 304 | } | ||
| 305 | |||
| 306 | void RSA_blinding_off(RSA *rsa) | ||
| 307 | { | ||
| 308 | if (rsa->blinding != NULL) | ||
| 309 | { | ||
| 310 | BN_BLINDING_free(rsa->blinding); | ||
| 311 | rsa->blinding=NULL; | ||
| 312 | } | ||
| 313 | rsa->flags &= ~RSA_FLAG_BLINDING; | ||
| 314 | rsa->flags |= RSA_FLAG_NO_BLINDING; | ||
| 315 | } | ||
| 316 | |||
| 317 | int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx) | ||
| 318 | { | ||
| 319 | BIGNUM *A,*Ai = NULL; | ||
| 320 | BN_CTX *ctx; | ||
| 321 | int ret=0; | ||
| 322 | |||
| 323 | if (p_ctx == NULL) | ||
| 324 | { | ||
| 325 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 326 | } | ||
| 327 | else | ||
| 328 | ctx=p_ctx; | ||
| 329 | |||
| 330 | /* XXXXX: Shouldn't this be RSA_blinding_off(rsa)? */ | ||
| 331 | if (rsa->blinding != NULL) | ||
| 332 | { | ||
| 333 | BN_BLINDING_free(rsa->blinding); | ||
| 334 | rsa->blinding = NULL; | ||
| 335 | } | ||
| 336 | |||
| 337 | /* NB: similar code appears in setup_blinding (rsa_eay.c); | ||
| 338 | * this should be placed in a new function of its own, but for reasons | ||
| 339 | * of binary compatibility can't */ | ||
| 340 | |||
| 341 | BN_CTX_start(ctx); | ||
| 342 | A = BN_CTX_get(ctx); | ||
| 343 | if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL) | ||
| 344 | { | ||
| 345 | /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */ | ||
| 346 | RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0); | ||
| 347 | if (!BN_pseudo_rand_range(A,rsa->n)) goto err; | ||
| 348 | } | ||
| 349 | else | ||
| 350 | { | ||
| 351 | if (!BN_rand_range(A,rsa->n)) goto err; | ||
| 352 | } | ||
| 353 | if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; | ||
| 354 | |||
| 355 | if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) | ||
| 356 | goto err; | ||
| 357 | if ((rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n)) == NULL) goto err; | ||
| 358 | /* to make things thread-safe without excessive locking, | ||
| 359 | * rsa->blinding will be used just by the current thread: */ | ||
| 360 | rsa->blinding->thread_id = CRYPTO_thread_id(); | ||
| 361 | rsa->flags |= RSA_FLAG_BLINDING; | ||
| 362 | rsa->flags &= ~RSA_FLAG_NO_BLINDING; | ||
| 363 | ret=1; | ||
| 364 | err: | ||
| 365 | if (Ai != NULL) BN_free(Ai); | ||
| 366 | BN_CTX_end(ctx); | ||
| 367 | if (ctx != p_ctx) BN_CTX_free(ctx); | ||
| 368 | return(ret); | ||
| 369 | } | ||
| 370 | |||
| 371 | int RSA_memory_lock(RSA *r) | ||
| 372 | { | ||
| 373 | int i,j,k,off; | ||
| 374 | char *p; | ||
| 375 | BIGNUM *bn,**t[6],*b; | ||
| 376 | BN_ULONG *ul; | ||
| 377 | |||
| 378 | if (r->d == NULL) return(1); | ||
| 379 | t[0]= &r->d; | ||
| 380 | t[1]= &r->p; | ||
| 381 | t[2]= &r->q; | ||
| 382 | t[3]= &r->dmp1; | ||
| 383 | t[4]= &r->dmq1; | ||
| 384 | t[5]= &r->iqmp; | ||
| 385 | k=sizeof(BIGNUM)*6; | ||
| 386 | off=k/sizeof(BN_ULONG)+1; | ||
| 387 | j=1; | ||
| 388 | for (i=0; i<6; i++) | ||
| 389 | j+= (*t[i])->top; | ||
| 390 | if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL) | ||
| 391 | { | ||
| 392 | RSAerr(RSA_F_MEMORY_LOCK,ERR_R_MALLOC_FAILURE); | ||
| 393 | return(0); | ||
| 394 | } | ||
| 395 | bn=(BIGNUM *)p; | ||
| 396 | ul=(BN_ULONG *)&(p[off]); | ||
| 397 | for (i=0; i<6; i++) | ||
| 398 | { | ||
| 399 | b= *(t[i]); | ||
| 400 | *(t[i])= &(bn[i]); | ||
| 401 | memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM)); | ||
| 402 | bn[i].flags=BN_FLG_STATIC_DATA; | ||
| 403 | bn[i].d=ul; | ||
| 404 | memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top); | ||
| 405 | ul+=b->top; | ||
| 406 | BN_clear_free(b); | ||
| 407 | } | ||
| 408 | |||
| 409 | /* I should fix this so it can still be done */ | ||
| 410 | r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC); | ||
| 411 | |||
| 412 | r->bignum_data=p; | ||
| 413 | return(1); | ||
| 414 | } | ||
diff --git a/src/lib/libcrypto/rsa/rsa_none.c b/src/lib/libcrypto/rsa/rsa_none.c new file mode 100644 index 0000000000..e6f3e627ca --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_none.c | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | /* crypto/rsa/rsa_none.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/rsa.h> | ||
| 63 | #include <openssl/rand.h> | ||
| 64 | |||
| 65 | int RSA_padding_add_none(unsigned char *to, int tlen, | ||
| 66 | const unsigned char *from, int flen) | ||
| 67 | { | ||
| 68 | if (flen > tlen) | ||
| 69 | { | ||
| 70 | RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 71 | return(0); | ||
| 72 | } | ||
| 73 | |||
| 74 | if (flen < tlen) | ||
| 75 | { | ||
| 76 | RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE); | ||
| 77 | return(0); | ||
| 78 | } | ||
| 79 | |||
| 80 | memcpy(to,from,(unsigned int)flen); | ||
| 81 | return(1); | ||
| 82 | } | ||
| 83 | |||
| 84 | int RSA_padding_check_none(unsigned char *to, int tlen, | ||
| 85 | const unsigned char *from, int flen, int num) | ||
| 86 | { | ||
| 87 | |||
| 88 | if (flen > tlen) | ||
| 89 | { | ||
| 90 | RSAerr(RSA_F_RSA_PADDING_CHECK_NONE,RSA_R_DATA_TOO_LARGE); | ||
| 91 | return(-1); | ||
| 92 | } | ||
| 93 | |||
| 94 | memset(to,0,tlen-flen); | ||
| 95 | memcpy(to+tlen-flen,from,flen); | ||
| 96 | return(tlen); | ||
| 97 | } | ||
| 98 | |||
diff --git a/src/lib/libcrypto/rsa/rsa_oaep.c b/src/lib/libcrypto/rsa/rsa_oaep.c new file mode 100644 index 0000000000..e3f7c608ec --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_oaep.c | |||
| @@ -0,0 +1,206 @@ | |||
| 1 | /* crypto/rsa/rsa_oaep.c */ | ||
| 2 | /* Written by Ulf Moeller. This software is distributed on an "AS IS" | ||
| 3 | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */ | ||
| 4 | |||
| 5 | /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */ | ||
| 6 | |||
| 7 | /* See Victor Shoup, "OAEP reconsidered," Nov. 2000, | ||
| 8 | * <URL: http://www.shoup.net/papers/oaep.ps.Z> | ||
| 9 | * for problems with the security proof for the | ||
| 10 | * original OAEP scheme, which EME-OAEP is based on. | ||
| 11 | * | ||
| 12 | * A new proof can be found in E. Fujisaki, T. Okamoto, | ||
| 13 | * D. Pointcheval, J. Stern, "RSA-OEAP is Still Alive!", | ||
| 14 | * Dec. 2000, <URL: http://eprint.iacr.org/2000/061/>. | ||
| 15 | * The new proof has stronger requirements for the | ||
| 16 | * underlying permutation: "partial-one-wayness" instead | ||
| 17 | * of one-wayness. For the RSA function, this is | ||
| 18 | * an equivalent notion. | ||
| 19 | */ | ||
| 20 | |||
| 21 | |||
| 22 | #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1) | ||
| 23 | #include <stdio.h> | ||
| 24 | #include "cryptlib.h" | ||
| 25 | #include <openssl/bn.h> | ||
| 26 | #include <openssl/rsa.h> | ||
| 27 | #include <openssl/evp.h> | ||
| 28 | #include <openssl/rand.h> | ||
| 29 | #include <openssl/sha.h> | ||
| 30 | |||
| 31 | int MGF1(unsigned char *mask, long len, | ||
| 32 | const unsigned char *seed, long seedlen); | ||
| 33 | |||
| 34 | int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, | ||
| 35 | const unsigned char *from, int flen, | ||
| 36 | const unsigned char *param, int plen) | ||
| 37 | { | ||
| 38 | int i, emlen = tlen - 1; | ||
| 39 | unsigned char *db, *seed; | ||
| 40 | unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH]; | ||
| 41 | |||
| 42 | if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1) | ||
| 43 | { | ||
| 44 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, | ||
| 45 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | if (emlen < 2 * SHA_DIGEST_LENGTH + 1) | ||
| 50 | { | ||
| 51 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL); | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH); | ||
| 56 | if (dbmask == NULL) | ||
| 57 | { | ||
| 58 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE); | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | to[0] = 0; | ||
| 63 | seed = to + 1; | ||
| 64 | db = to + SHA_DIGEST_LENGTH + 1; | ||
| 65 | |||
| 66 | EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL); | ||
| 67 | memset(db + SHA_DIGEST_LENGTH, 0, | ||
| 68 | emlen - flen - 2 * SHA_DIGEST_LENGTH - 1); | ||
| 69 | db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01; | ||
| 70 | memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen); | ||
| 71 | if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0) | ||
| 72 | return 0; | ||
| 73 | #ifdef PKCS_TESTVECT | ||
| 74 | memcpy(seed, | ||
| 75 | "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f", | ||
| 76 | 20); | ||
| 77 | #endif | ||
| 78 | |||
| 79 | MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH); | ||
| 80 | for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++) | ||
| 81 | db[i] ^= dbmask[i]; | ||
| 82 | |||
| 83 | MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH); | ||
| 84 | for (i = 0; i < SHA_DIGEST_LENGTH; i++) | ||
| 85 | seed[i] ^= seedmask[i]; | ||
| 86 | |||
| 87 | OPENSSL_free(dbmask); | ||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen, | ||
| 92 | const unsigned char *from, int flen, int num, | ||
| 93 | const unsigned char *param, int plen) | ||
| 94 | { | ||
| 95 | int i, dblen, mlen = -1; | ||
| 96 | const unsigned char *maskeddb; | ||
| 97 | int lzero; | ||
| 98 | unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH]; | ||
| 99 | int bad = 0; | ||
| 100 | |||
| 101 | if (--num < 2 * SHA_DIGEST_LENGTH + 1) | ||
| 102 | /* 'num' is the length of the modulus, i.e. does not depend on the | ||
| 103 | * particular ciphertext. */ | ||
| 104 | goto decoding_err; | ||
| 105 | |||
| 106 | lzero = num - flen; | ||
| 107 | if (lzero < 0) | ||
| 108 | { | ||
| 109 | /* lzero == -1 */ | ||
| 110 | |||
| 111 | /* signalling this error immediately after detection might allow | ||
| 112 | * for side-channel attacks (e.g. timing if 'plen' is huge | ||
| 113 | * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal | ||
| 114 | * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001), | ||
| 115 | * so we use a 'bad' flag */ | ||
| 116 | bad = 1; | ||
| 117 | lzero = 0; | ||
| 118 | } | ||
| 119 | maskeddb = from - lzero + SHA_DIGEST_LENGTH; | ||
| 120 | |||
| 121 | dblen = num - SHA_DIGEST_LENGTH; | ||
| 122 | db = OPENSSL_malloc(dblen); | ||
| 123 | if (db == NULL) | ||
| 124 | { | ||
| 125 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE); | ||
| 126 | return -1; | ||
| 127 | } | ||
| 128 | |||
| 129 | MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen); | ||
| 130 | for (i = lzero; i < SHA_DIGEST_LENGTH; i++) | ||
| 131 | seed[i] ^= from[i - lzero]; | ||
| 132 | |||
| 133 | MGF1(db, dblen, seed, SHA_DIGEST_LENGTH); | ||
| 134 | for (i = 0; i < dblen; i++) | ||
| 135 | db[i] ^= maskeddb[i]; | ||
| 136 | |||
| 137 | EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL); | ||
| 138 | |||
| 139 | if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad) | ||
| 140 | goto decoding_err; | ||
| 141 | else | ||
| 142 | { | ||
| 143 | for (i = SHA_DIGEST_LENGTH; i < dblen; i++) | ||
| 144 | if (db[i] != 0x00) | ||
| 145 | break; | ||
| 146 | if (db[i] != 0x01 || i++ >= dblen) | ||
| 147 | goto decoding_err; | ||
| 148 | else | ||
| 149 | { | ||
| 150 | /* everything looks OK */ | ||
| 151 | |||
| 152 | mlen = dblen - i; | ||
| 153 | if (tlen < mlen) | ||
| 154 | { | ||
| 155 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE); | ||
| 156 | mlen = -1; | ||
| 157 | } | ||
| 158 | else | ||
| 159 | memcpy(to, db + i, mlen); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | OPENSSL_free(db); | ||
| 163 | return mlen; | ||
| 164 | |||
| 165 | decoding_err: | ||
| 166 | /* to avoid chosen ciphertext attacks, the error message should not reveal | ||
| 167 | * which kind of decoding error happened */ | ||
| 168 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR); | ||
| 169 | if (db != NULL) OPENSSL_free(db); | ||
| 170 | return -1; | ||
| 171 | } | ||
| 172 | |||
| 173 | int MGF1(unsigned char *mask, long len, | ||
| 174 | const unsigned char *seed, long seedlen) | ||
| 175 | { | ||
| 176 | long i, outlen = 0; | ||
| 177 | unsigned char cnt[4]; | ||
| 178 | EVP_MD_CTX c; | ||
| 179 | unsigned char md[SHA_DIGEST_LENGTH]; | ||
| 180 | |||
| 181 | EVP_MD_CTX_init(&c); | ||
| 182 | for (i = 0; outlen < len; i++) | ||
| 183 | { | ||
| 184 | cnt[0] = (unsigned char)((i >> 24) & 255); | ||
| 185 | cnt[1] = (unsigned char)((i >> 16) & 255); | ||
| 186 | cnt[2] = (unsigned char)((i >> 8)) & 255; | ||
| 187 | cnt[3] = (unsigned char)(i & 255); | ||
| 188 | EVP_DigestInit_ex(&c,EVP_sha1(), NULL); | ||
| 189 | EVP_DigestUpdate(&c, seed, seedlen); | ||
| 190 | EVP_DigestUpdate(&c, cnt, 4); | ||
| 191 | if (outlen + SHA_DIGEST_LENGTH <= len) | ||
| 192 | { | ||
| 193 | EVP_DigestFinal_ex(&c, mask + outlen, NULL); | ||
| 194 | outlen += SHA_DIGEST_LENGTH; | ||
| 195 | } | ||
| 196 | else | ||
| 197 | { | ||
| 198 | EVP_DigestFinal_ex(&c, md, NULL); | ||
| 199 | memcpy(mask + outlen, md, len - outlen); | ||
| 200 | outlen = len; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | EVP_MD_CTX_cleanup(&c); | ||
| 204 | return 0; | ||
| 205 | } | ||
| 206 | #endif | ||
diff --git a/src/lib/libcrypto/rsa/rsa_pk1.c b/src/lib/libcrypto/rsa/rsa_pk1.c new file mode 100644 index 0000000000..8560755f1d --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_pk1.c | |||
| @@ -0,0 +1,224 @@ | |||
| 1 | /* crypto/rsa/rsa_pk1.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/rsa.h> | ||
| 63 | #include <openssl/rand.h> | ||
| 64 | |||
| 65 | int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen, | ||
| 66 | const unsigned char *from, int flen) | ||
| 67 | { | ||
| 68 | int j; | ||
| 69 | unsigned char *p; | ||
| 70 | |||
| 71 | if (flen > (tlen-RSA_PKCS1_PADDING_SIZE)) | ||
| 72 | { | ||
| 73 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 74 | return(0); | ||
| 75 | } | ||
| 76 | |||
| 77 | p=(unsigned char *)to; | ||
| 78 | |||
| 79 | *(p++)=0; | ||
| 80 | *(p++)=1; /* Private Key BT (Block Type) */ | ||
| 81 | |||
| 82 | /* pad out with 0xff data */ | ||
| 83 | j=tlen-3-flen; | ||
| 84 | memset(p,0xff,j); | ||
| 85 | p+=j; | ||
| 86 | *(p++)='\0'; | ||
| 87 | memcpy(p,from,(unsigned int)flen); | ||
| 88 | return(1); | ||
| 89 | } | ||
| 90 | |||
| 91 | int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen, | ||
| 92 | const unsigned char *from, int flen, int num) | ||
| 93 | { | ||
| 94 | int i,j; | ||
| 95 | const unsigned char *p; | ||
| 96 | |||
| 97 | p=from; | ||
| 98 | if ((num != (flen+1)) || (*(p++) != 01)) | ||
| 99 | { | ||
| 100 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BLOCK_TYPE_IS_NOT_01); | ||
| 101 | return(-1); | ||
| 102 | } | ||
| 103 | |||
| 104 | /* scan over padding data */ | ||
| 105 | j=flen-1; /* one for type. */ | ||
| 106 | for (i=0; i<j; i++) | ||
| 107 | { | ||
| 108 | if (*p != 0xff) /* should decrypt to 0xff */ | ||
| 109 | { | ||
| 110 | if (*p == 0) | ||
| 111 | { p++; break; } | ||
| 112 | else { | ||
| 113 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_FIXED_HEADER_DECRYPT); | ||
| 114 | return(-1); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | p++; | ||
| 118 | } | ||
| 119 | |||
| 120 | if (i == j) | ||
| 121 | { | ||
| 122 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_NULL_BEFORE_BLOCK_MISSING); | ||
| 123 | return(-1); | ||
| 124 | } | ||
| 125 | |||
| 126 | if (i < 8) | ||
| 127 | { | ||
| 128 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_PAD_BYTE_COUNT); | ||
| 129 | return(-1); | ||
| 130 | } | ||
| 131 | i++; /* Skip over the '\0' */ | ||
| 132 | j-=i; | ||
| 133 | if (j > tlen) | ||
| 134 | { | ||
| 135 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE); | ||
| 136 | return(-1); | ||
| 137 | } | ||
| 138 | memcpy(to,p,(unsigned int)j); | ||
| 139 | |||
| 140 | return(j); | ||
| 141 | } | ||
| 142 | |||
| 143 | int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen, | ||
| 144 | const unsigned char *from, int flen) | ||
| 145 | { | ||
| 146 | int i,j; | ||
| 147 | unsigned char *p; | ||
| 148 | |||
| 149 | if (flen > (tlen-11)) | ||
| 150 | { | ||
| 151 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 152 | return(0); | ||
| 153 | } | ||
| 154 | |||
| 155 | p=(unsigned char *)to; | ||
| 156 | |||
| 157 | *(p++)=0; | ||
| 158 | *(p++)=2; /* Public Key BT (Block Type) */ | ||
| 159 | |||
| 160 | /* pad out with non-zero random data */ | ||
| 161 | j=tlen-3-flen; | ||
| 162 | |||
| 163 | if (RAND_bytes(p,j) <= 0) | ||
| 164 | return(0); | ||
| 165 | for (i=0; i<j; i++) | ||
| 166 | { | ||
| 167 | if (*p == '\0') | ||
| 168 | do { | ||
| 169 | if (RAND_bytes(p,1) <= 0) | ||
| 170 | return(0); | ||
| 171 | } while (*p == '\0'); | ||
| 172 | p++; | ||
| 173 | } | ||
| 174 | |||
| 175 | *(p++)='\0'; | ||
| 176 | |||
| 177 | memcpy(p,from,(unsigned int)flen); | ||
| 178 | return(1); | ||
| 179 | } | ||
| 180 | |||
| 181 | int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen, | ||
| 182 | const unsigned char *from, int flen, int num) | ||
| 183 | { | ||
| 184 | int i,j; | ||
| 185 | const unsigned char *p; | ||
| 186 | |||
| 187 | p=from; | ||
| 188 | if ((num != (flen+1)) || (*(p++) != 02)) | ||
| 189 | { | ||
| 190 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BLOCK_TYPE_IS_NOT_02); | ||
| 191 | return(-1); | ||
| 192 | } | ||
| 193 | #ifdef PKCS1_CHECK | ||
| 194 | return(num-11); | ||
| 195 | #endif | ||
| 196 | |||
| 197 | /* scan over padding data */ | ||
| 198 | j=flen-1; /* one for type. */ | ||
| 199 | for (i=0; i<j; i++) | ||
| 200 | if (*(p++) == 0) break; | ||
| 201 | |||
| 202 | if (i == j) | ||
| 203 | { | ||
| 204 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_NULL_BEFORE_BLOCK_MISSING); | ||
| 205 | return(-1); | ||
| 206 | } | ||
| 207 | |||
| 208 | if (i < 8) | ||
| 209 | { | ||
| 210 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BAD_PAD_BYTE_COUNT); | ||
| 211 | return(-1); | ||
| 212 | } | ||
| 213 | i++; /* Skip over the '\0' */ | ||
| 214 | j-=i; | ||
| 215 | if (j > tlen) | ||
| 216 | { | ||
| 217 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE); | ||
| 218 | return(-1); | ||
| 219 | } | ||
| 220 | memcpy(to,p,(unsigned int)j); | ||
| 221 | |||
| 222 | return(j); | ||
| 223 | } | ||
| 224 | |||
diff --git a/src/lib/libcrypto/rsa/rsa_saos.c b/src/lib/libcrypto/rsa/rsa_saos.c new file mode 100644 index 0000000000..24fc94835e --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_saos.c | |||
| @@ -0,0 +1,149 @@ | |||
| 1 | /* crypto/rsa/rsa_saos.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/rsa.h> | ||
| 63 | #include <openssl/objects.h> | ||
| 64 | #include <openssl/x509.h> | ||
| 65 | |||
| 66 | int RSA_sign_ASN1_OCTET_STRING(int type, | ||
| 67 | const unsigned char *m, unsigned int m_len, | ||
| 68 | unsigned char *sigret, unsigned int *siglen, RSA *rsa) | ||
| 69 | { | ||
| 70 | ASN1_OCTET_STRING sig; | ||
| 71 | int i,j,ret=1; | ||
| 72 | unsigned char *p,*s; | ||
| 73 | |||
| 74 | sig.type=V_ASN1_OCTET_STRING; | ||
| 75 | sig.length=m_len; | ||
| 76 | sig.data=(unsigned char *)m; | ||
| 77 | |||
| 78 | i=i2d_ASN1_OCTET_STRING(&sig,NULL); | ||
| 79 | j=RSA_size(rsa); | ||
| 80 | if (i > (j-RSA_PKCS1_PADDING_SIZE)) | ||
| 81 | { | ||
| 82 | RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); | ||
| 83 | return(0); | ||
| 84 | } | ||
| 85 | s=(unsigned char *)OPENSSL_malloc((unsigned int)j+1); | ||
| 86 | if (s == NULL) | ||
| 87 | { | ||
| 88 | RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); | ||
| 89 | return(0); | ||
| 90 | } | ||
| 91 | p=s; | ||
| 92 | i2d_ASN1_OCTET_STRING(&sig,&p); | ||
| 93 | i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING); | ||
| 94 | if (i <= 0) | ||
| 95 | ret=0; | ||
| 96 | else | ||
| 97 | *siglen=i; | ||
| 98 | |||
| 99 | OPENSSL_cleanse(s,(unsigned int)j+1); | ||
| 100 | OPENSSL_free(s); | ||
| 101 | return(ret); | ||
| 102 | } | ||
| 103 | |||
| 104 | int RSA_verify_ASN1_OCTET_STRING(int dtype, | ||
| 105 | const unsigned char *m, | ||
| 106 | unsigned int m_len, unsigned char *sigbuf, unsigned int siglen, | ||
| 107 | RSA *rsa) | ||
| 108 | { | ||
| 109 | int i,ret=0; | ||
| 110 | unsigned char *p,*s; | ||
| 111 | ASN1_OCTET_STRING *sig=NULL; | ||
| 112 | |||
| 113 | if (siglen != (unsigned int)RSA_size(rsa)) | ||
| 114 | { | ||
| 115 | RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH); | ||
| 116 | return(0); | ||
| 117 | } | ||
| 118 | |||
| 119 | s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen); | ||
| 120 | if (s == NULL) | ||
| 121 | { | ||
| 122 | RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); | ||
| 123 | goto err; | ||
| 124 | } | ||
| 125 | i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); | ||
| 126 | |||
| 127 | if (i <= 0) goto err; | ||
| 128 | |||
| 129 | p=s; | ||
| 130 | sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i); | ||
| 131 | if (sig == NULL) goto err; | ||
| 132 | |||
| 133 | if ( ((unsigned int)sig->length != m_len) || | ||
| 134 | (memcmp(m,sig->data,m_len) != 0)) | ||
| 135 | { | ||
| 136 | RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE); | ||
| 137 | } | ||
| 138 | else | ||
| 139 | ret=1; | ||
| 140 | err: | ||
| 141 | if (sig != NULL) M_ASN1_OCTET_STRING_free(sig); | ||
| 142 | if (s != NULL) | ||
| 143 | { | ||
| 144 | OPENSSL_cleanse(s,(unsigned int)siglen); | ||
| 145 | OPENSSL_free(s); | ||
| 146 | } | ||
| 147 | return(ret); | ||
| 148 | } | ||
| 149 | |||
diff --git a/src/lib/libcrypto/rsa/rsa_sign.c b/src/lib/libcrypto/rsa/rsa_sign.c new file mode 100644 index 0000000000..cee09eccb1 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_sign.c | |||
| @@ -0,0 +1,232 @@ | |||
| 1 | /* crypto/rsa/rsa_sign.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/rsa.h> | ||
| 63 | #include <openssl/objects.h> | ||
| 64 | #include <openssl/x509.h> | ||
| 65 | |||
| 66 | /* Size of an SSL signature: MD5+SHA1 */ | ||
| 67 | #define SSL_SIG_LENGTH 36 | ||
| 68 | |||
| 69 | int RSA_sign(int type, const unsigned char *m, unsigned int m_len, | ||
| 70 | unsigned char *sigret, unsigned int *siglen, RSA *rsa) | ||
| 71 | { | ||
| 72 | X509_SIG sig; | ||
| 73 | ASN1_TYPE parameter; | ||
| 74 | int i,j,ret=1; | ||
| 75 | unsigned char *p, *tmps = NULL; | ||
| 76 | const unsigned char *s = NULL; | ||
| 77 | X509_ALGOR algor; | ||
| 78 | ASN1_OCTET_STRING digest; | ||
| 79 | if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_sign) | ||
| 80 | { | ||
| 81 | return rsa->meth->rsa_sign(type, m, m_len, | ||
| 82 | sigret, siglen, rsa); | ||
| 83 | } | ||
| 84 | /* Special case: SSL signature, just check the length */ | ||
| 85 | if(type == NID_md5_sha1) { | ||
| 86 | if(m_len != SSL_SIG_LENGTH) { | ||
| 87 | RSAerr(RSA_F_RSA_SIGN,RSA_R_INVALID_MESSAGE_LENGTH); | ||
| 88 | return(0); | ||
| 89 | } | ||
| 90 | i = SSL_SIG_LENGTH; | ||
| 91 | s = m; | ||
| 92 | } else { | ||
| 93 | sig.algor= &algor; | ||
| 94 | sig.algor->algorithm=OBJ_nid2obj(type); | ||
| 95 | if (sig.algor->algorithm == NULL) | ||
| 96 | { | ||
| 97 | RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE); | ||
| 98 | return(0); | ||
| 99 | } | ||
| 100 | if (sig.algor->algorithm->length == 0) | ||
| 101 | { | ||
| 102 | RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); | ||
| 103 | return(0); | ||
| 104 | } | ||
| 105 | parameter.type=V_ASN1_NULL; | ||
| 106 | parameter.value.ptr=NULL; | ||
| 107 | sig.algor->parameter= ¶meter; | ||
| 108 | |||
| 109 | sig.digest= &digest; | ||
| 110 | sig.digest->data=(unsigned char *)m; /* TMP UGLY CAST */ | ||
| 111 | sig.digest->length=m_len; | ||
| 112 | |||
| 113 | i=i2d_X509_SIG(&sig,NULL); | ||
| 114 | } | ||
| 115 | j=RSA_size(rsa); | ||
| 116 | if (i > (j-RSA_PKCS1_PADDING_SIZE)) | ||
| 117 | { | ||
| 118 | RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); | ||
| 119 | return(0); | ||
| 120 | } | ||
| 121 | if(type != NID_md5_sha1) { | ||
| 122 | tmps=(unsigned char *)OPENSSL_malloc((unsigned int)j+1); | ||
| 123 | if (tmps == NULL) | ||
| 124 | { | ||
| 125 | RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE); | ||
| 126 | return(0); | ||
| 127 | } | ||
| 128 | p=tmps; | ||
| 129 | i2d_X509_SIG(&sig,&p); | ||
| 130 | s=tmps; | ||
| 131 | } | ||
| 132 | i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING); | ||
| 133 | if (i <= 0) | ||
| 134 | ret=0; | ||
| 135 | else | ||
| 136 | *siglen=i; | ||
| 137 | |||
| 138 | if(type != NID_md5_sha1) { | ||
| 139 | OPENSSL_cleanse(tmps,(unsigned int)j+1); | ||
| 140 | OPENSSL_free(tmps); | ||
| 141 | } | ||
| 142 | return(ret); | ||
| 143 | } | ||
| 144 | |||
| 145 | int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len, | ||
| 146 | unsigned char *sigbuf, unsigned int siglen, RSA *rsa) | ||
| 147 | { | ||
| 148 | int i,ret=0,sigtype; | ||
| 149 | unsigned char *p,*s; | ||
| 150 | X509_SIG *sig=NULL; | ||
| 151 | |||
| 152 | if (siglen != (unsigned int)RSA_size(rsa)) | ||
| 153 | { | ||
| 154 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH); | ||
| 155 | return(0); | ||
| 156 | } | ||
| 157 | |||
| 158 | if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify) | ||
| 159 | { | ||
| 160 | return rsa->meth->rsa_verify(dtype, m, m_len, | ||
| 161 | sigbuf, siglen, rsa); | ||
| 162 | } | ||
| 163 | |||
| 164 | s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen); | ||
| 165 | if (s == NULL) | ||
| 166 | { | ||
| 167 | RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE); | ||
| 168 | goto err; | ||
| 169 | } | ||
| 170 | if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) { | ||
| 171 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH); | ||
| 172 | goto err; | ||
| 173 | } | ||
| 174 | i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); | ||
| 175 | |||
| 176 | if (i <= 0) goto err; | ||
| 177 | |||
| 178 | /* Special case: SSL signature */ | ||
| 179 | if(dtype == NID_md5_sha1) { | ||
| 180 | if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH)) | ||
| 181 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); | ||
| 182 | else ret = 1; | ||
| 183 | } else { | ||
| 184 | p=s; | ||
| 185 | sig=d2i_X509_SIG(NULL,&p,(long)i); | ||
| 186 | |||
| 187 | if (sig == NULL) goto err; | ||
| 188 | sigtype=OBJ_obj2nid(sig->algor->algorithm); | ||
| 189 | |||
| 190 | |||
| 191 | #ifdef RSA_DEBUG | ||
| 192 | /* put a backward compatibility flag in EAY */ | ||
| 193 | fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype), | ||
| 194 | OBJ_nid2ln(dtype)); | ||
| 195 | #endif | ||
| 196 | if (sigtype != dtype) | ||
| 197 | { | ||
| 198 | if (((dtype == NID_md5) && | ||
| 199 | (sigtype == NID_md5WithRSAEncryption)) || | ||
| 200 | ((dtype == NID_md2) && | ||
| 201 | (sigtype == NID_md2WithRSAEncryption))) | ||
| 202 | { | ||
| 203 | /* ok, we will let it through */ | ||
| 204 | #if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16) | ||
| 205 | fprintf(stderr,"signature has problems, re-make with post SSLeay045\n"); | ||
| 206 | #endif | ||
| 207 | } | ||
| 208 | else | ||
| 209 | { | ||
| 210 | RSAerr(RSA_F_RSA_VERIFY, | ||
| 211 | RSA_R_ALGORITHM_MISMATCH); | ||
| 212 | goto err; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | if ( ((unsigned int)sig->digest->length != m_len) || | ||
| 216 | (memcmp(m,sig->digest->data,m_len) != 0)) | ||
| 217 | { | ||
| 218 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); | ||
| 219 | } | ||
| 220 | else | ||
| 221 | ret=1; | ||
| 222 | } | ||
| 223 | err: | ||
| 224 | if (sig != NULL) X509_SIG_free(sig); | ||
| 225 | if (s != NULL) | ||
| 226 | { | ||
| 227 | OPENSSL_cleanse(s,(unsigned int)siglen); | ||
| 228 | OPENSSL_free(s); | ||
| 229 | } | ||
| 230 | return(ret); | ||
| 231 | } | ||
| 232 | |||
diff --git a/src/lib/libcrypto/rsa/rsa_ssl.c b/src/lib/libcrypto/rsa/rsa_ssl.c new file mode 100644 index 0000000000..ea72629494 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_ssl.c | |||
| @@ -0,0 +1,154 @@ | |||
| 1 | /* crypto/rsa/rsa_ssl.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/rsa.h> | ||
| 63 | #include <openssl/rand.h> | ||
| 64 | |||
| 65 | int RSA_padding_add_SSLv23(unsigned char *to, int tlen, | ||
| 66 | const unsigned char *from, int flen) | ||
| 67 | { | ||
| 68 | int i,j; | ||
| 69 | unsigned char *p; | ||
| 70 | |||
| 71 | if (flen > (tlen-11)) | ||
| 72 | { | ||
| 73 | RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 74 | return(0); | ||
| 75 | } | ||
| 76 | |||
| 77 | p=(unsigned char *)to; | ||
| 78 | |||
| 79 | *(p++)=0; | ||
| 80 | *(p++)=2; /* Public Key BT (Block Type) */ | ||
| 81 | |||
| 82 | /* pad out with non-zero random data */ | ||
| 83 | j=tlen-3-8-flen; | ||
| 84 | |||
| 85 | if (RAND_bytes(p,j) <= 0) | ||
| 86 | return(0); | ||
| 87 | for (i=0; i<j; i++) | ||
| 88 | { | ||
| 89 | if (*p == '\0') | ||
| 90 | do { | ||
| 91 | if (RAND_bytes(p,1) <= 0) | ||
| 92 | return(0); | ||
| 93 | } while (*p == '\0'); | ||
| 94 | p++; | ||
| 95 | } | ||
| 96 | |||
| 97 | memset(p,3,8); | ||
| 98 | p+=8; | ||
| 99 | *(p++)='\0'; | ||
| 100 | |||
| 101 | memcpy(p,from,(unsigned int)flen); | ||
| 102 | return(1); | ||
| 103 | } | ||
| 104 | |||
| 105 | int RSA_padding_check_SSLv23(unsigned char *to, int tlen, | ||
| 106 | const unsigned char *from, int flen, int num) | ||
| 107 | { | ||
| 108 | int i,j,k; | ||
| 109 | const unsigned char *p; | ||
| 110 | |||
| 111 | p=from; | ||
| 112 | if (flen < 10) | ||
| 113 | { | ||
| 114 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_DATA_TOO_SMALL); | ||
| 115 | return(-1); | ||
| 116 | } | ||
| 117 | if ((num != (flen+1)) || (*(p++) != 02)) | ||
| 118 | { | ||
| 119 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_BLOCK_TYPE_IS_NOT_02); | ||
| 120 | return(-1); | ||
| 121 | } | ||
| 122 | |||
| 123 | /* scan over padding data */ | ||
| 124 | j=flen-1; /* one for type */ | ||
| 125 | for (i=0; i<j; i++) | ||
| 126 | if (*(p++) == 0) break; | ||
| 127 | |||
| 128 | if ((i == j) || (i < 8)) | ||
| 129 | { | ||
| 130 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_NULL_BEFORE_BLOCK_MISSING); | ||
| 131 | return(-1); | ||
| 132 | } | ||
| 133 | for (k= -8; k<0; k++) | ||
| 134 | { | ||
| 135 | if (p[k] != 0x03) break; | ||
| 136 | } | ||
| 137 | if (k == -1) | ||
| 138 | { | ||
| 139 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_SSLV3_ROLLBACK_ATTACK); | ||
| 140 | return(-1); | ||
| 141 | } | ||
| 142 | |||
| 143 | i++; /* Skip over the '\0' */ | ||
| 144 | j-=i; | ||
| 145 | if (j > tlen) | ||
| 146 | { | ||
| 147 | RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_DATA_TOO_LARGE); | ||
| 148 | return(-1); | ||
| 149 | } | ||
| 150 | memcpy(to,p,(unsigned int)j); | ||
| 151 | |||
| 152 | return(j); | ||
| 153 | } | ||
| 154 | |||
diff --git a/src/lib/libcrypto/rsa/rsa_test.c b/src/lib/libcrypto/rsa/rsa_test.c index 218bb2a39b..924e9ad1f6 100644 --- a/src/lib/libcrypto/rsa/rsa_test.c +++ b/src/lib/libcrypto/rsa/rsa_test.c | |||
| @@ -227,10 +227,10 @@ int main(int argc, char *argv[]) | |||
| 227 | 227 | ||
| 228 | plen = sizeof(ptext_ex) - 1; | 228 | plen = sizeof(ptext_ex) - 1; |
| 229 | 229 | ||
| 230 | for (v = 0; v < 6; v++) | 230 | for (v = 0; v < 3; v++) |
| 231 | { | 231 | { |
| 232 | key = RSA_new(); | 232 | key = RSA_new(); |
| 233 | switch (v%3) { | 233 | switch (v) { |
| 234 | case 0: | 234 | case 0: |
| 235 | clen = key1(key, ctext_ex); | 235 | clen = key1(key, ctext_ex); |
| 236 | break; | 236 | break; |
| @@ -241,7 +241,6 @@ int main(int argc, char *argv[]) | |||
| 241 | clen = key3(key, ctext_ex); | 241 | clen = key3(key, ctext_ex); |
| 242 | break; | 242 | break; |
| 243 | } | 243 | } |
| 244 | if (v/3 > 1) key->flags |= RSA_FLAG_NO_EXP_CONSTTIME; | ||
| 245 | 244 | ||
| 246 | num = RSA_public_encrypt(plen, ptext_ex, ctext, key, | 245 | num = RSA_public_encrypt(plen, ptext_ex, ctext, key, |
| 247 | RSA_PKCS1_PADDING); | 246 | RSA_PKCS1_PADDING); |
