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 | 413 | ||||
| -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 | 818 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_err.c | 165 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_gen.c | 201 | ||||
| -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_null.c | 15 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_oaep.c | 212 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_pk1.c | 224 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_pss.c | 261 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_saos.c | 149 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_sign.c | 249 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_ssl.c | 154 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_test.c | 32 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_x931.c | 177 |
18 files changed, 4093 insertions, 35 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..dbed701e89 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa.h | |||
| @@ -0,0 +1,413 @@ | |||
| 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 OPENSSL_RSA_MAX_MODULUS_BITS 16384 | ||
| 158 | |||
| 159 | #define OPENSSL_RSA_SMALL_MODULUS_BITS 3072 | ||
| 160 | #define OPENSSL_RSA_MAX_PUBEXP_BITS 64 /* exponent limit enforced for "small" modulus only */ | ||
| 161 | |||
| 162 | #define RSA_3 0x3L | ||
| 163 | #define RSA_F4 0x10001L | ||
| 164 | |||
| 165 | #define RSA_METHOD_FLAG_NO_CHECK 0x0001 /* don't check pub/private match */ | ||
| 166 | |||
| 167 | #define RSA_FLAG_CACHE_PUBLIC 0x0002 | ||
| 168 | #define RSA_FLAG_CACHE_PRIVATE 0x0004 | ||
| 169 | #define RSA_FLAG_BLINDING 0x0008 | ||
| 170 | #define RSA_FLAG_THREAD_SAFE 0x0010 | ||
| 171 | /* This flag means the private key operations will be handled by rsa_mod_exp | ||
| 172 | * and that they do not depend on the private key components being present: | ||
| 173 | * for example a key stored in external hardware. Without this flag bn_mod_exp | ||
| 174 | * gets called when private key components are absent. | ||
| 175 | */ | ||
| 176 | #define RSA_FLAG_EXT_PKEY 0x0020 | ||
| 177 | |||
| 178 | /* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions. | ||
| 179 | */ | ||
| 180 | #define RSA_FLAG_SIGN_VER 0x0040 | ||
| 181 | |||
| 182 | #define RSA_FLAG_NO_BLINDING 0x0080 /* new with 0.9.6j and 0.9.7b; the built-in | ||
| 183 | * RSA implementation now uses blinding by | ||
| 184 | * default (ignoring RSA_FLAG_BLINDING), | ||
| 185 | * but other engines might not need it | ||
| 186 | */ | ||
| 187 | #define RSA_FLAG_NO_EXP_CONSTTIME 0x0100 /* new with 0.9.7h; the built-in RSA | ||
| 188 | * implementation now uses constant time | ||
| 189 | * modular exponentiation for secret exponents | ||
| 190 | * by default. This flag causes the | ||
| 191 | * faster variable sliding window method to | ||
| 192 | * be used for all exponents. | ||
| 193 | */ | ||
| 194 | |||
| 195 | #define RSA_PKCS1_PADDING 1 | ||
| 196 | #define RSA_SSLV23_PADDING 2 | ||
| 197 | #define RSA_NO_PADDING 3 | ||
| 198 | #define RSA_PKCS1_OAEP_PADDING 4 | ||
| 199 | #define RSA_X931_PADDING 5 | ||
| 200 | |||
| 201 | #define RSA_PKCS1_PADDING_SIZE 11 | ||
| 202 | |||
| 203 | #define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg) | ||
| 204 | #define RSA_get_app_data(s) RSA_get_ex_data(s,0) | ||
| 205 | |||
| 206 | RSA * RSA_new(void); | ||
| 207 | RSA * RSA_new_method(ENGINE *engine); | ||
| 208 | int RSA_size(const RSA *); | ||
| 209 | RSA * RSA_generate_key(int bits, unsigned long e,void | ||
| 210 | (*callback)(int,int,void *),void *cb_arg); | ||
| 211 | int RSA_check_key(const RSA *); | ||
| 212 | #ifdef OPENSSL_FIPS | ||
| 213 | int RSA_X931_derive(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1, BIGNUM *q2, | ||
| 214 | void (*cb)(int, int, void *), void *cb_arg, | ||
| 215 | const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *Xp, | ||
| 216 | const BIGNUM *Xq1, const BIGNUM *Xq2, const BIGNUM *Xq, | ||
| 217 | const BIGNUM *e); | ||
| 218 | RSA *RSA_X931_generate_key(int bits, const BIGNUM *e, | ||
| 219 | void (*cb)(int,int,void *), void *cb_arg); | ||
| 220 | #endif | ||
| 221 | /* next 4 return -1 on error */ | ||
| 222 | int RSA_public_encrypt(int flen, const unsigned char *from, | ||
| 223 | unsigned char *to, RSA *rsa,int padding); | ||
| 224 | int RSA_private_encrypt(int flen, const unsigned char *from, | ||
| 225 | unsigned char *to, RSA *rsa,int padding); | ||
| 226 | int RSA_public_decrypt(int flen, const unsigned char *from, | ||
| 227 | unsigned char *to, RSA *rsa,int padding); | ||
| 228 | int RSA_private_decrypt(int flen, const unsigned char *from, | ||
| 229 | unsigned char *to, RSA *rsa,int padding); | ||
| 230 | void RSA_free (RSA *r); | ||
| 231 | /* "up" the RSA object's reference count */ | ||
| 232 | int RSA_up_ref(RSA *r); | ||
| 233 | |||
| 234 | int RSA_flags(const RSA *r); | ||
| 235 | |||
| 236 | void RSA_set_default_method(const RSA_METHOD *meth); | ||
| 237 | const RSA_METHOD *RSA_get_default_method(void); | ||
| 238 | const RSA_METHOD *RSA_get_method(const RSA *rsa); | ||
| 239 | int RSA_set_method(RSA *rsa, const RSA_METHOD *meth); | ||
| 240 | |||
| 241 | /* This function needs the memory locking malloc callbacks to be installed */ | ||
| 242 | int RSA_memory_lock(RSA *r); | ||
| 243 | |||
| 244 | /* these are the actual SSLeay RSA functions */ | ||
| 245 | const RSA_METHOD *RSA_PKCS1_SSLeay(void); | ||
| 246 | |||
| 247 | const RSA_METHOD *RSA_null_method(void); | ||
| 248 | |||
| 249 | DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPublicKey) | ||
| 250 | DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPrivateKey) | ||
| 251 | |||
| 252 | #ifndef OPENSSL_NO_FP_API | ||
| 253 | int RSA_print_fp(FILE *fp, const RSA *r,int offset); | ||
| 254 | #endif | ||
| 255 | |||
| 256 | #ifndef OPENSSL_NO_BIO | ||
| 257 | int RSA_print(BIO *bp, const RSA *r,int offset); | ||
| 258 | #endif | ||
| 259 | |||
| 260 | int i2d_RSA_NET(const RSA *a, unsigned char **pp, int (*cb)(), int sgckey); | ||
| 261 | RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length, int (*cb)(), int sgckey); | ||
| 262 | |||
| 263 | int i2d_Netscape_RSA(const RSA *a, unsigned char **pp, int (*cb)()); | ||
| 264 | RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, int (*cb)()); | ||
| 265 | |||
| 266 | /* The following 2 functions sign and verify a X509_SIG ASN1 object | ||
| 267 | * inside PKCS#1 padded RSA encryption */ | ||
| 268 | int RSA_sign(int type, const unsigned char *m, unsigned int m_length, | ||
| 269 | unsigned char *sigret, unsigned int *siglen, RSA *rsa); | ||
| 270 | int RSA_verify(int type, const unsigned char *m, unsigned int m_length, | ||
| 271 | unsigned char *sigbuf, unsigned int siglen, RSA *rsa); | ||
| 272 | |||
| 273 | /* The following 2 function sign and verify a ASN1_OCTET_STRING | ||
| 274 | * object inside PKCS#1 padded RSA encryption */ | ||
| 275 | int RSA_sign_ASN1_OCTET_STRING(int type, | ||
| 276 | const unsigned char *m, unsigned int m_length, | ||
| 277 | unsigned char *sigret, unsigned int *siglen, RSA *rsa); | ||
| 278 | int RSA_verify_ASN1_OCTET_STRING(int type, | ||
| 279 | const unsigned char *m, unsigned int m_length, | ||
| 280 | unsigned char *sigbuf, unsigned int siglen, RSA *rsa); | ||
| 281 | |||
| 282 | int RSA_blinding_on(RSA *rsa, BN_CTX *ctx); | ||
| 283 | void RSA_blinding_off(RSA *rsa); | ||
| 284 | |||
| 285 | int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen, | ||
| 286 | const unsigned char *f,int fl); | ||
| 287 | int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen, | ||
| 288 | const unsigned char *f,int fl,int rsa_len); | ||
| 289 | int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen, | ||
| 290 | const unsigned char *f,int fl); | ||
| 291 | int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen, | ||
| 292 | const unsigned char *f,int fl,int rsa_len); | ||
| 293 | int PKCS1_MGF1(unsigned char *mask, long len, | ||
| 294 | const unsigned char *seed, long seedlen, const EVP_MD *dgst); | ||
| 295 | int RSA_padding_add_PKCS1_OAEP(unsigned char *to,int tlen, | ||
| 296 | const unsigned char *f,int fl, | ||
| 297 | const unsigned char *p,int pl); | ||
| 298 | int RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen, | ||
| 299 | const unsigned char *f,int fl,int rsa_len, | ||
| 300 | const unsigned char *p,int pl); | ||
| 301 | int RSA_padding_add_SSLv23(unsigned char *to,int tlen, | ||
| 302 | const unsigned char *f,int fl); | ||
| 303 | int RSA_padding_check_SSLv23(unsigned char *to,int tlen, | ||
| 304 | const unsigned char *f,int fl,int rsa_len); | ||
| 305 | int RSA_padding_add_none(unsigned char *to,int tlen, | ||
| 306 | const unsigned char *f,int fl); | ||
| 307 | int RSA_padding_check_none(unsigned char *to,int tlen, | ||
| 308 | const unsigned char *f,int fl,int rsa_len); | ||
| 309 | int RSA_padding_add_X931(unsigned char *to,int tlen, | ||
| 310 | const unsigned char *f,int fl); | ||
| 311 | int RSA_padding_check_X931(unsigned char *to,int tlen, | ||
| 312 | const unsigned char *f,int fl,int rsa_len); | ||
| 313 | int RSA_X931_hash_id(int nid); | ||
| 314 | |||
| 315 | int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, | ||
| 316 | const EVP_MD *Hash, const unsigned char *EM, int sLen); | ||
| 317 | int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM, | ||
| 318 | const unsigned char *mHash, | ||
| 319 | const EVP_MD *Hash, int sLen); | ||
| 320 | |||
| 321 | int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 322 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); | ||
| 323 | int RSA_set_ex_data(RSA *r,int idx,void *arg); | ||
| 324 | void *RSA_get_ex_data(const RSA *r, int idx); | ||
| 325 | |||
| 326 | RSA *RSAPublicKey_dup(RSA *rsa); | ||
| 327 | RSA *RSAPrivateKey_dup(RSA *rsa); | ||
| 328 | |||
| 329 | /* BEGIN ERROR CODES */ | ||
| 330 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
| 331 | * made after this point may be overwritten when the script is next run. | ||
| 332 | */ | ||
| 333 | void ERR_load_RSA_strings(void); | ||
| 334 | |||
| 335 | /* Error codes for the RSA functions. */ | ||
| 336 | |||
| 337 | /* Function codes. */ | ||
| 338 | #define RSA_F_MEMORY_LOCK 100 | ||
| 339 | #define RSA_F_RSA_CHECK_KEY 123 | ||
| 340 | #define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101 | ||
| 341 | #define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102 | ||
| 342 | #define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103 | ||
| 343 | #define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104 | ||
| 344 | #define RSA_F_RSA_GENERATE_KEY 105 | ||
| 345 | #define RSA_F_RSA_NEW_METHOD 106 | ||
| 346 | #define RSA_F_RSA_NULL 124 | ||
| 347 | #define RSA_F_RSA_PADDING_ADD_NONE 107 | ||
| 348 | #define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121 | ||
| 349 | #define RSA_F_RSA_PADDING_ADD_PKCS1_PSS 125 | ||
| 350 | #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108 | ||
| 351 | #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109 | ||
| 352 | #define RSA_F_RSA_PADDING_ADD_SSLV23 110 | ||
| 353 | #define RSA_F_RSA_PADDING_ADD_X931 127 | ||
| 354 | #define RSA_F_RSA_PADDING_CHECK_NONE 111 | ||
| 355 | #define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122 | ||
| 356 | #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112 | ||
| 357 | #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113 | ||
| 358 | #define RSA_F_RSA_PADDING_CHECK_SSLV23 114 | ||
| 359 | #define RSA_F_RSA_PADDING_CHECK_X931 128 | ||
| 360 | #define RSA_F_RSA_PRINT 115 | ||
| 361 | #define RSA_F_RSA_PRINT_FP 116 | ||
| 362 | #define RSA_F_RSA_SIGN 117 | ||
| 363 | #define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118 | ||
| 364 | #define RSA_F_RSA_VERIFY 119 | ||
| 365 | #define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120 | ||
| 366 | #define RSA_F_RSA_VERIFY_PKCS1_PSS 126 | ||
| 367 | |||
| 368 | /* Reason codes. */ | ||
| 369 | #define RSA_R_ALGORITHM_MISMATCH 100 | ||
| 370 | #define RSA_R_BAD_E_VALUE 101 | ||
| 371 | #define RSA_R_BAD_FIXED_HEADER_DECRYPT 102 | ||
| 372 | #define RSA_R_BAD_PAD_BYTE_COUNT 103 | ||
| 373 | #define RSA_R_BAD_SIGNATURE 104 | ||
| 374 | #define RSA_R_BLOCK_TYPE_IS_NOT_01 106 | ||
| 375 | #define RSA_R_BLOCK_TYPE_IS_NOT_02 107 | ||
| 376 | #define RSA_R_DATA_GREATER_THAN_MOD_LEN 108 | ||
| 377 | #define RSA_R_DATA_TOO_LARGE 109 | ||
| 378 | #define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110 | ||
| 379 | #define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132 | ||
| 380 | #define RSA_R_DATA_TOO_SMALL 111 | ||
| 381 | #define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122 | ||
| 382 | #define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112 | ||
| 383 | #define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124 | ||
| 384 | #define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125 | ||
| 385 | #define RSA_R_D_E_NOT_CONGRUENT_TO_1 123 | ||
| 386 | #define RSA_R_FIRST_OCTET_INVALID 133 | ||
| 387 | #define RSA_R_INVALID_HEADER 137 | ||
| 388 | #define RSA_R_INVALID_MESSAGE_LENGTH 131 | ||
| 389 | #define RSA_R_INVALID_PADDING 138 | ||
| 390 | #define RSA_R_INVALID_TRAILER 139 | ||
| 391 | #define RSA_R_IQMP_NOT_INVERSE_OF_Q 126 | ||
| 392 | #define RSA_R_KEY_SIZE_TOO_SMALL 120 | ||
| 393 | #define RSA_R_LAST_OCTET_INVALID 134 | ||
| 394 | #define RSA_R_MODULUS_TOO_LARGE 105 | ||
| 395 | #define RSA_R_NULL_BEFORE_BLOCK_MISSING 113 | ||
| 396 | #define RSA_R_N_DOES_NOT_EQUAL_P_Q 127 | ||
| 397 | #define RSA_R_OAEP_DECODING_ERROR 121 | ||
| 398 | #define RSA_R_PADDING_CHECK_FAILED 114 | ||
| 399 | #define RSA_R_P_NOT_PRIME 128 | ||
| 400 | #define RSA_R_Q_NOT_PRIME 129 | ||
| 401 | #define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130 | ||
| 402 | #define RSA_R_SLEN_CHECK_FAILED 136 | ||
| 403 | #define RSA_R_SLEN_RECOVERY_FAILED 135 | ||
| 404 | #define RSA_R_SSLV3_ROLLBACK_ATTACK 115 | ||
| 405 | #define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116 | ||
| 406 | #define RSA_R_UNKNOWN_ALGORITHM_TYPE 117 | ||
| 407 | #define RSA_R_UNKNOWN_PADDING_TYPE 118 | ||
| 408 | #define RSA_R_WRONG_SIGNATURE_LENGTH 119 | ||
| 409 | |||
| 410 | #ifdef __cplusplus | ||
| 411 | } | ||
| 412 | #endif | ||
| 413 | #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..610889dc80 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_eay.c | |||
| @@ -0,0 +1,818 @@ | |||
| 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 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. | ||
| 60 | * | ||
| 61 | * Redistribution and use in source and binary forms, with or without | ||
| 62 | * modification, are permitted provided that the following conditions | ||
| 63 | * are met: | ||
| 64 | * | ||
| 65 | * 1. Redistributions of source code must retain the above copyright | ||
| 66 | * notice, this list of conditions and the following disclaimer. | ||
| 67 | * | ||
| 68 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 69 | * notice, this list of conditions and the following disclaimer in | ||
| 70 | * the documentation and/or other materials provided with the | ||
| 71 | * distribution. | ||
| 72 | * | ||
| 73 | * 3. All advertising materials mentioning features or use of this | ||
| 74 | * software must display the following acknowledgment: | ||
| 75 | * "This product includes software developed by the OpenSSL Project | ||
| 76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 77 | * | ||
| 78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 79 | * endorse or promote products derived from this software without | ||
| 80 | * prior written permission. For written permission, please contact | ||
| 81 | * openssl-core@openssl.org. | ||
| 82 | * | ||
| 83 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 84 | * nor may "OpenSSL" appear in their names without prior written | ||
| 85 | * permission of the OpenSSL Project. | ||
| 86 | * | ||
| 87 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 88 | * acknowledgment: | ||
| 89 | * "This product includes software developed by the OpenSSL Project | ||
| 90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 91 | * | ||
| 92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 103 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 104 | * ==================================================================== | ||
| 105 | * | ||
| 106 | * This product includes cryptographic software written by Eric Young | ||
| 107 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 108 | * Hudson (tjh@cryptsoft.com). | ||
| 109 | * | ||
| 110 | */ | ||
| 111 | |||
| 112 | #include <stdio.h> | ||
| 113 | #include "cryptlib.h" | ||
| 114 | #include <openssl/bn.h> | ||
| 115 | #include <openssl/rsa.h> | ||
| 116 | #include <openssl/rand.h> | ||
| 117 | |||
| 118 | #if !defined(RSA_NULL) && !defined(OPENSSL_FIPS) | ||
| 119 | |||
| 120 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, | ||
| 121 | unsigned char *to, RSA *rsa,int padding); | ||
| 122 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, | ||
| 123 | unsigned char *to, RSA *rsa,int padding); | ||
| 124 | static int RSA_eay_public_decrypt(int flen, const unsigned char *from, | ||
| 125 | unsigned char *to, RSA *rsa,int padding); | ||
| 126 | static int RSA_eay_private_decrypt(int flen, const unsigned char *from, | ||
| 127 | unsigned char *to, RSA *rsa,int padding); | ||
| 128 | static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa); | ||
| 129 | static int RSA_eay_init(RSA *rsa); | ||
| 130 | static int RSA_eay_finish(RSA *rsa); | ||
| 131 | static RSA_METHOD rsa_pkcs1_eay_meth={ | ||
| 132 | "Eric Young's PKCS#1 RSA", | ||
| 133 | RSA_eay_public_encrypt, | ||
| 134 | RSA_eay_public_decrypt, /* signature verification */ | ||
| 135 | RSA_eay_private_encrypt, /* signing */ | ||
| 136 | RSA_eay_private_decrypt, | ||
| 137 | RSA_eay_mod_exp, | ||
| 138 | BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */ | ||
| 139 | RSA_eay_init, | ||
| 140 | RSA_eay_finish, | ||
| 141 | 0, /* flags */ | ||
| 142 | NULL, | ||
| 143 | 0, /* rsa_sign */ | ||
| 144 | 0 /* rsa_verify */ | ||
| 145 | }; | ||
| 146 | |||
| 147 | const RSA_METHOD *RSA_PKCS1_SSLeay(void) | ||
| 148 | { | ||
| 149 | return(&rsa_pkcs1_eay_meth); | ||
| 150 | } | ||
| 151 | |||
| 152 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, | ||
| 153 | unsigned char *to, RSA *rsa, int padding) | ||
| 154 | { | ||
| 155 | BIGNUM f,ret; | ||
| 156 | int i,j,k,num=0,r= -1; | ||
| 157 | unsigned char *buf=NULL; | ||
| 158 | BN_CTX *ctx=NULL; | ||
| 159 | |||
| 160 | BN_init(&f); | ||
| 161 | BN_init(&ret); | ||
| 162 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 163 | num=BN_num_bytes(rsa->n); | ||
| 164 | if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
| 165 | { | ||
| 166 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 167 | goto err; | ||
| 168 | } | ||
| 169 | |||
| 170 | switch (padding) | ||
| 171 | { | ||
| 172 | case RSA_PKCS1_PADDING: | ||
| 173 | i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen); | ||
| 174 | break; | ||
| 175 | #ifndef OPENSSL_NO_SHA | ||
| 176 | case RSA_PKCS1_OAEP_PADDING: | ||
| 177 | i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0); | ||
| 178 | break; | ||
| 179 | #endif | ||
| 180 | case RSA_SSLV23_PADDING: | ||
| 181 | i=RSA_padding_add_SSLv23(buf,num,from,flen); | ||
| 182 | break; | ||
| 183 | case RSA_NO_PADDING: | ||
| 184 | i=RSA_padding_add_none(buf,num,from,flen); | ||
| 185 | break; | ||
| 186 | default: | ||
| 187 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 188 | goto err; | ||
| 189 | } | ||
| 190 | if (i <= 0) goto err; | ||
| 191 | |||
| 192 | if (BN_bin2bn(buf,num,&f) == NULL) goto err; | ||
| 193 | |||
| 194 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
| 195 | { | ||
| 196 | /* usually the padding functions would catch this */ | ||
| 197 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 198 | goto err; | ||
| 199 | } | ||
| 200 | |||
| 201 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) | ||
| 202 | { | ||
| 203 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, | ||
| 204 | CRYPTO_LOCK_RSA, rsa->n, ctx)) | ||
| 205 | goto err; | ||
| 206 | } | ||
| 207 | |||
| 208 | if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, | ||
| 209 | rsa->_method_mod_n)) goto err; | ||
| 210 | |||
| 211 | /* put in leading 0 bytes if the number is less than the | ||
| 212 | * length of the modulus */ | ||
| 213 | j=BN_num_bytes(&ret); | ||
| 214 | i=BN_bn2bin(&ret,&(to[num-j])); | ||
| 215 | for (k=0; k<(num-i); k++) | ||
| 216 | to[k]=0; | ||
| 217 | |||
| 218 | r=num; | ||
| 219 | err: | ||
| 220 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 221 | BN_clear_free(&f); | ||
| 222 | BN_clear_free(&ret); | ||
| 223 | if (buf != NULL) | ||
| 224 | { | ||
| 225 | OPENSSL_cleanse(buf,num); | ||
| 226 | OPENSSL_free(buf); | ||
| 227 | } | ||
| 228 | return(r); | ||
| 229 | } | ||
| 230 | |||
| 231 | static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx) | ||
| 232 | { | ||
| 233 | int ret = 1; | ||
| 234 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
| 235 | /* Check again inside the lock - the macro's check is racey */ | ||
| 236 | if(rsa->blinding == NULL) | ||
| 237 | ret = RSA_blinding_on(rsa, ctx); | ||
| 238 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
| 239 | return ret; | ||
| 240 | } | ||
| 241 | |||
| 242 | #define BLINDING_HELPER(rsa, ctx, err_instr) \ | ||
| 243 | do { \ | ||
| 244 | if((!((rsa)->flags & RSA_FLAG_NO_BLINDING)) && \ | ||
| 245 | ((rsa)->blinding == NULL) && \ | ||
| 246 | !rsa_eay_blinding(rsa, ctx)) \ | ||
| 247 | err_instr \ | ||
| 248 | } while(0) | ||
| 249 | |||
| 250 | static BN_BLINDING *setup_blinding(RSA *rsa, BN_CTX *ctx) | ||
| 251 | { | ||
| 252 | BIGNUM *A, *Ai; | ||
| 253 | BN_BLINDING *ret = NULL; | ||
| 254 | |||
| 255 | /* added in OpenSSL 0.9.6j and 0.9.7b */ | ||
| 256 | |||
| 257 | /* NB: similar code appears in RSA_blinding_on (rsa_lib.c); | ||
| 258 | * this should be placed in a new function of its own, but for reasons | ||
| 259 | * of binary compatibility can't */ | ||
| 260 | |||
| 261 | BN_CTX_start(ctx); | ||
| 262 | A = BN_CTX_get(ctx); | ||
| 263 | if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL) | ||
| 264 | { | ||
| 265 | /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */ | ||
| 266 | RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0); | ||
| 267 | if (!BN_pseudo_rand_range(A,rsa->n)) goto err; | ||
| 268 | } | ||
| 269 | else | ||
| 270 | { | ||
| 271 | if (!BN_rand_range(A,rsa->n)) goto err; | ||
| 272 | } | ||
| 273 | if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err; | ||
| 274 | |||
| 275 | if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) | ||
| 276 | goto err; | ||
| 277 | ret = BN_BLINDING_new(A,Ai,rsa->n); | ||
| 278 | BN_free(Ai); | ||
| 279 | err: | ||
| 280 | BN_CTX_end(ctx); | ||
| 281 | return ret; | ||
| 282 | } | ||
| 283 | |||
| 284 | /* signing */ | ||
| 285 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, | ||
| 286 | unsigned char *to, RSA *rsa, int padding) | ||
| 287 | { | ||
| 288 | BIGNUM f,ret, *res; | ||
| 289 | int i,j,k,num=0,r= -1; | ||
| 290 | unsigned char *buf=NULL; | ||
| 291 | BN_CTX *ctx=NULL; | ||
| 292 | int local_blinding = 0; | ||
| 293 | BN_BLINDING *blinding = NULL; | ||
| 294 | |||
| 295 | BN_init(&f); | ||
| 296 | BN_init(&ret); | ||
| 297 | |||
| 298 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) | ||
| 299 | { | ||
| 300 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE); | ||
| 301 | return -1; | ||
| 302 | } | ||
| 303 | |||
| 304 | if (BN_ucmp(rsa->n, rsa->e) <= 0) | ||
| 305 | { | ||
| 306 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); | ||
| 307 | return -1; | ||
| 308 | } | ||
| 309 | |||
| 310 | /* for large moduli, enforce exponent limit */ | ||
| 311 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) | ||
| 312 | { | ||
| 313 | if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) | ||
| 314 | { | ||
| 315 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); | ||
| 316 | return -1; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | |||
| 320 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 321 | num=BN_num_bytes(rsa->n); | ||
| 322 | if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
| 323 | { | ||
| 324 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 325 | goto err; | ||
| 326 | } | ||
| 327 | |||
| 328 | switch (padding) | ||
| 329 | { | ||
| 330 | case RSA_PKCS1_PADDING: | ||
| 331 | i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen); | ||
| 332 | break; | ||
| 333 | case RSA_NO_PADDING: | ||
| 334 | i=RSA_padding_add_none(buf,num,from,flen); | ||
| 335 | break; | ||
| 336 | case RSA_SSLV23_PADDING: | ||
| 337 | default: | ||
| 338 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 339 | goto err; | ||
| 340 | } | ||
| 341 | if (i <= 0) goto err; | ||
| 342 | |||
| 343 | if (BN_bin2bn(buf,num,&f) == NULL) goto err; | ||
| 344 | |||
| 345 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
| 346 | { | ||
| 347 | /* usually the padding functions would catch this */ | ||
| 348 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 349 | goto err; | ||
| 350 | } | ||
| 351 | |||
| 352 | BLINDING_HELPER(rsa, ctx, goto err;); | ||
| 353 | blinding = rsa->blinding; | ||
| 354 | |||
| 355 | /* Now unless blinding is disabled, 'blinding' is non-NULL. | ||
| 356 | * But the BN_BLINDING object may be owned by some other thread | ||
| 357 | * (we don't want to keep it constant and we don't want to use | ||
| 358 | * lots of locking to avoid race conditions, so only a single | ||
| 359 | * thread can use it; other threads have to use local blinding | ||
| 360 | * factors) */ | ||
| 361 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) | ||
| 362 | { | ||
| 363 | if (blinding == NULL) | ||
| 364 | { | ||
| 365 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); | ||
| 366 | goto err; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | if (blinding != NULL) | ||
| 371 | { | ||
| 372 | if (blinding->thread_id != CRYPTO_thread_id()) | ||
| 373 | { | ||
| 374 | /* we need a local one-time blinding factor */ | ||
| 375 | |||
| 376 | blinding = setup_blinding(rsa, ctx); | ||
| 377 | if (blinding == NULL) | ||
| 378 | goto err; | ||
| 379 | local_blinding = 1; | ||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | if (blinding) | ||
| 384 | if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err; | ||
| 385 | |||
| 386 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || | ||
| 387 | ((rsa->p != NULL) && | ||
| 388 | (rsa->q != NULL) && | ||
| 389 | (rsa->dmp1 != NULL) && | ||
| 390 | (rsa->dmq1 != NULL) && | ||
| 391 | (rsa->iqmp != NULL)) ) | ||
| 392 | { | ||
| 393 | if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; | ||
| 394 | } | ||
| 395 | else | ||
| 396 | { | ||
| 397 | BIGNUM local_d; | ||
| 398 | BIGNUM *d = NULL; | ||
| 399 | |||
| 400 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
| 401 | { | ||
| 402 | BN_init(&local_d); | ||
| 403 | d = &local_d; | ||
| 404 | BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME); | ||
| 405 | } | ||
| 406 | else | ||
| 407 | d = rsa->d; | ||
| 408 | if (!rsa->meth->bn_mod_exp(&ret,&f,d,rsa->n,ctx,NULL)) goto err; | ||
| 409 | } | ||
| 410 | |||
| 411 | if (blinding) | ||
| 412 | if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err; | ||
| 413 | |||
| 414 | if (padding == RSA_X931_PADDING) | ||
| 415 | { | ||
| 416 | BN_sub(&f, rsa->n, &ret); | ||
| 417 | if (BN_cmp(&ret, &f)) | ||
| 418 | res = &f; | ||
| 419 | else | ||
| 420 | res = &ret; | ||
| 421 | } | ||
| 422 | else | ||
| 423 | res = &ret; | ||
| 424 | |||
| 425 | /* put in leading 0 bytes if the number is less than the | ||
| 426 | * length of the modulus */ | ||
| 427 | j=BN_num_bytes(res); | ||
| 428 | i=BN_bn2bin(res,&(to[num-j])); | ||
| 429 | for (k=0; k<(num-i); k++) | ||
| 430 | to[k]=0; | ||
| 431 | |||
| 432 | r=num; | ||
| 433 | err: | ||
| 434 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 435 | BN_clear_free(&ret); | ||
| 436 | BN_clear_free(&f); | ||
| 437 | if (local_blinding) | ||
| 438 | BN_BLINDING_free(blinding); | ||
| 439 | if (buf != NULL) | ||
| 440 | { | ||
| 441 | OPENSSL_cleanse(buf,num); | ||
| 442 | OPENSSL_free(buf); | ||
| 443 | } | ||
| 444 | return(r); | ||
| 445 | } | ||
| 446 | |||
| 447 | static int RSA_eay_private_decrypt(int flen, const unsigned char *from, | ||
| 448 | unsigned char *to, RSA *rsa, int padding) | ||
| 449 | { | ||
| 450 | BIGNUM f,ret; | ||
| 451 | int j,num=0,r= -1; | ||
| 452 | unsigned char *p; | ||
| 453 | unsigned char *buf=NULL; | ||
| 454 | BN_CTX *ctx=NULL; | ||
| 455 | int local_blinding = 0; | ||
| 456 | BN_BLINDING *blinding = NULL; | ||
| 457 | |||
| 458 | BN_init(&f); | ||
| 459 | BN_init(&ret); | ||
| 460 | ctx=BN_CTX_new(); | ||
| 461 | if (ctx == NULL) goto err; | ||
| 462 | |||
| 463 | num=BN_num_bytes(rsa->n); | ||
| 464 | |||
| 465 | if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL) | ||
| 466 | { | ||
| 467 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); | ||
| 468 | goto err; | ||
| 469 | } | ||
| 470 | |||
| 471 | /* This check was for equality but PGP does evil things | ||
| 472 | * and chops off the top '0' bytes */ | ||
| 473 | if (flen > num) | ||
| 474 | { | ||
| 475 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | ||
| 476 | goto err; | ||
| 477 | } | ||
| 478 | |||
| 479 | /* make data into a big number */ | ||
| 480 | if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err; | ||
| 481 | |||
| 482 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
| 483 | { | ||
| 484 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 485 | goto err; | ||
| 486 | } | ||
| 487 | |||
| 488 | BLINDING_HELPER(rsa, ctx, goto err;); | ||
| 489 | blinding = rsa->blinding; | ||
| 490 | |||
| 491 | /* Now unless blinding is disabled, 'blinding' is non-NULL. | ||
| 492 | * But the BN_BLINDING object may be owned by some other thread | ||
| 493 | * (we don't want to keep it constant and we don't want to use | ||
| 494 | * lots of locking to avoid race conditions, so only a single | ||
| 495 | * thread can use it; other threads have to use local blinding | ||
| 496 | * factors) */ | ||
| 497 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) | ||
| 498 | { | ||
| 499 | if (blinding == NULL) | ||
| 500 | { | ||
| 501 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR); | ||
| 502 | goto err; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | if (blinding != NULL) | ||
| 507 | { | ||
| 508 | if (blinding->thread_id != CRYPTO_thread_id()) | ||
| 509 | { | ||
| 510 | /* we need a local one-time blinding factor */ | ||
| 511 | |||
| 512 | blinding = setup_blinding(rsa, ctx); | ||
| 513 | if (blinding == NULL) | ||
| 514 | goto err; | ||
| 515 | local_blinding = 1; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | if (blinding) | ||
| 520 | if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err; | ||
| 521 | |||
| 522 | /* do the decrypt */ | ||
| 523 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || | ||
| 524 | ((rsa->p != NULL) && | ||
| 525 | (rsa->q != NULL) && | ||
| 526 | (rsa->dmp1 != NULL) && | ||
| 527 | (rsa->dmq1 != NULL) && | ||
| 528 | (rsa->iqmp != NULL)) ) | ||
| 529 | { | ||
| 530 | if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; | ||
| 531 | } | ||
| 532 | else | ||
| 533 | { | ||
| 534 | BIGNUM local_d; | ||
| 535 | BIGNUM *d = NULL; | ||
| 536 | |||
| 537 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
| 538 | { | ||
| 539 | d = &local_d; | ||
| 540 | BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME); | ||
| 541 | } | ||
| 542 | else | ||
| 543 | d = rsa->d; | ||
| 544 | if (!rsa->meth->bn_mod_exp(&ret,&f,d,rsa->n,ctx,NULL)) | ||
| 545 | goto err; | ||
| 546 | } | ||
| 547 | |||
| 548 | if (blinding) | ||
| 549 | if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err; | ||
| 550 | |||
| 551 | p=buf; | ||
| 552 | j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */ | ||
| 553 | |||
| 554 | switch (padding) | ||
| 555 | { | ||
| 556 | case RSA_PKCS1_PADDING: | ||
| 557 | r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num); | ||
| 558 | break; | ||
| 559 | #ifndef OPENSSL_NO_SHA | ||
| 560 | case RSA_PKCS1_OAEP_PADDING: | ||
| 561 | r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0); | ||
| 562 | break; | ||
| 563 | #endif | ||
| 564 | case RSA_SSLV23_PADDING: | ||
| 565 | r=RSA_padding_check_SSLv23(to,num,buf,j,num); | ||
| 566 | break; | ||
| 567 | case RSA_NO_PADDING: | ||
| 568 | r=RSA_padding_check_none(to,num,buf,j,num); | ||
| 569 | break; | ||
| 570 | default: | ||
| 571 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 572 | goto err; | ||
| 573 | } | ||
| 574 | if (r < 0) | ||
| 575 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | ||
| 576 | |||
| 577 | err: | ||
| 578 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 579 | BN_clear_free(&f); | ||
| 580 | BN_clear_free(&ret); | ||
| 581 | if (local_blinding) | ||
| 582 | BN_BLINDING_free(blinding); | ||
| 583 | if (buf != NULL) | ||
| 584 | { | ||
| 585 | OPENSSL_cleanse(buf,num); | ||
| 586 | OPENSSL_free(buf); | ||
| 587 | } | ||
| 588 | return(r); | ||
| 589 | } | ||
| 590 | |||
| 591 | /* signature verification */ | ||
| 592 | static int RSA_eay_public_decrypt(int flen, const unsigned char *from, | ||
| 593 | unsigned char *to, RSA *rsa, int padding) | ||
| 594 | { | ||
| 595 | BIGNUM f,ret; | ||
| 596 | int i,num=0,r= -1; | ||
| 597 | unsigned char *p; | ||
| 598 | unsigned char *buf=NULL; | ||
| 599 | BN_CTX *ctx=NULL; | ||
| 600 | |||
| 601 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) | ||
| 602 | { | ||
| 603 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE); | ||
| 604 | return -1; | ||
| 605 | } | ||
| 606 | |||
| 607 | if (BN_ucmp(rsa->n, rsa->e) <= 0) | ||
| 608 | { | ||
| 609 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); | ||
| 610 | return -1; | ||
| 611 | } | ||
| 612 | |||
| 613 | /* for large moduli, enforce exponent limit */ | ||
| 614 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) | ||
| 615 | { | ||
| 616 | if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) | ||
| 617 | { | ||
| 618 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); | ||
| 619 | return -1; | ||
| 620 | } | ||
| 621 | } | ||
| 622 | |||
| 623 | BN_init(&f); | ||
| 624 | BN_init(&ret); | ||
| 625 | ctx=BN_CTX_new(); | ||
| 626 | if (ctx == NULL) goto err; | ||
| 627 | |||
| 628 | num=BN_num_bytes(rsa->n); | ||
| 629 | buf=(unsigned char *)OPENSSL_malloc(num); | ||
| 630 | if (buf == NULL) | ||
| 631 | { | ||
| 632 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE); | ||
| 633 | goto err; | ||
| 634 | } | ||
| 635 | |||
| 636 | /* This check was for equality but PGP does evil things | ||
| 637 | * and chops off the top '0' bytes */ | ||
| 638 | if (flen > num) | ||
| 639 | { | ||
| 640 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | ||
| 641 | goto err; | ||
| 642 | } | ||
| 643 | |||
| 644 | if (BN_bin2bn(from,flen,&f) == NULL) goto err; | ||
| 645 | |||
| 646 | if (BN_ucmp(&f, rsa->n) >= 0) | ||
| 647 | { | ||
| 648 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 649 | goto err; | ||
| 650 | } | ||
| 651 | |||
| 652 | /* do the decrypt */ | ||
| 653 | |||
| 654 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) | ||
| 655 | { | ||
| 656 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, | ||
| 657 | CRYPTO_LOCK_RSA, rsa->n, ctx)) | ||
| 658 | goto err; | ||
| 659 | } | ||
| 660 | |||
| 661 | if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx, | ||
| 662 | rsa->_method_mod_n)) goto err; | ||
| 663 | |||
| 664 | if ((padding == RSA_X931_PADDING) && ((ret.d[0] & 0xf) != 12)) | ||
| 665 | BN_sub(&ret, rsa->n, &ret); | ||
| 666 | |||
| 667 | p=buf; | ||
| 668 | i=BN_bn2bin(&ret,p); | ||
| 669 | |||
| 670 | switch (padding) | ||
| 671 | { | ||
| 672 | case RSA_PKCS1_PADDING: | ||
| 673 | r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num); | ||
| 674 | break; | ||
| 675 | case RSA_NO_PADDING: | ||
| 676 | r=RSA_padding_check_none(to,num,buf,i,num); | ||
| 677 | break; | ||
| 678 | default: | ||
| 679 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 680 | goto err; | ||
| 681 | } | ||
| 682 | if (r < 0) | ||
| 683 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | ||
| 684 | |||
| 685 | err: | ||
| 686 | if (ctx != NULL) BN_CTX_free(ctx); | ||
| 687 | BN_clear_free(&f); | ||
| 688 | BN_clear_free(&ret); | ||
| 689 | if (buf != NULL) | ||
| 690 | { | ||
| 691 | OPENSSL_cleanse(buf,num); | ||
| 692 | OPENSSL_free(buf); | ||
| 693 | } | ||
| 694 | return(r); | ||
| 695 | } | ||
| 696 | |||
| 697 | static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa) | ||
| 698 | { | ||
| 699 | BIGNUM r1,m1,vrfy; | ||
| 700 | BIGNUM local_dmp1, local_dmq1; | ||
| 701 | BIGNUM *dmp1, *dmq1; | ||
| 702 | int ret=0; | ||
| 703 | BN_CTX *ctx; | ||
| 704 | |||
| 705 | BN_init(&m1); | ||
| 706 | BN_init(&r1); | ||
| 707 | BN_init(&vrfy); | ||
| 708 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 709 | |||
| 710 | if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) | ||
| 711 | { | ||
| 712 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, | ||
| 713 | CRYPTO_LOCK_RSA, rsa->p, ctx)) | ||
| 714 | goto err; | ||
| 715 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, | ||
| 716 | CRYPTO_LOCK_RSA, rsa->q, ctx)) | ||
| 717 | goto err; | ||
| 718 | } | ||
| 719 | |||
| 720 | if (!BN_mod(&r1,I,rsa->q,ctx)) goto err; | ||
| 721 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
| 722 | { | ||
| 723 | dmq1 = &local_dmq1; | ||
| 724 | BN_with_flags(dmq1, rsa->dmq1, BN_FLG_EXP_CONSTTIME); | ||
| 725 | } | ||
| 726 | else | ||
| 727 | dmq1 = rsa->dmq1; | ||
| 728 | if (!rsa->meth->bn_mod_exp(&m1,&r1,dmq1,rsa->q,ctx, | ||
| 729 | rsa->_method_mod_q)) goto err; | ||
| 730 | |||
| 731 | if (!BN_mod(&r1,I,rsa->p,ctx)) goto err; | ||
| 732 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
| 733 | { | ||
| 734 | dmp1 = &local_dmp1; | ||
| 735 | BN_with_flags(dmp1, rsa->dmp1, BN_FLG_EXP_CONSTTIME); | ||
| 736 | } | ||
| 737 | else | ||
| 738 | dmp1 = rsa->dmp1; | ||
| 739 | if (!rsa->meth->bn_mod_exp(r0,&r1,dmp1,rsa->p,ctx, | ||
| 740 | rsa->_method_mod_p)) goto err; | ||
| 741 | |||
| 742 | if (!BN_sub(r0,r0,&m1)) goto err; | ||
| 743 | /* This will help stop the size of r0 increasing, which does | ||
| 744 | * affect the multiply if it optimised for a power of 2 size */ | ||
| 745 | if (r0->neg) | ||
| 746 | if (!BN_add(r0,r0,rsa->p)) goto err; | ||
| 747 | |||
| 748 | if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err; | ||
| 749 | if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err; | ||
| 750 | /* If p < q it is occasionally possible for the correction of | ||
| 751 | * adding 'p' if r0 is negative above to leave the result still | ||
| 752 | * negative. This can break the private key operations: the following | ||
| 753 | * second correction should *always* correct this rare occurrence. | ||
| 754 | * This will *never* happen with OpenSSL generated keys because | ||
| 755 | * they ensure p > q [steve] | ||
| 756 | */ | ||
| 757 | if (r0->neg) | ||
| 758 | if (!BN_add(r0,r0,rsa->p)) goto err; | ||
| 759 | if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err; | ||
| 760 | if (!BN_add(r0,&r1,&m1)) goto err; | ||
| 761 | |||
| 762 | if (rsa->e && rsa->n) | ||
| 763 | { | ||
| 764 | if (!rsa->meth->bn_mod_exp(&vrfy,r0,rsa->e,rsa->n,ctx,NULL)) goto err; | ||
| 765 | /* If 'I' was greater than (or equal to) rsa->n, the operation | ||
| 766 | * will be equivalent to using 'I mod n'. However, the result of | ||
| 767 | * the verify will *always* be less than 'n' so we don't check | ||
| 768 | * for absolute equality, just congruency. */ | ||
| 769 | if (!BN_sub(&vrfy, &vrfy, I)) goto err; | ||
| 770 | if (!BN_mod(&vrfy, &vrfy, rsa->n, ctx)) goto err; | ||
| 771 | if (vrfy.neg) | ||
| 772 | if (!BN_add(&vrfy, &vrfy, rsa->n)) goto err; | ||
| 773 | if (!BN_is_zero(&vrfy)) | ||
| 774 | { | ||
| 775 | /* 'I' and 'vrfy' aren't congruent mod n. Don't leak | ||
| 776 | * miscalculated CRT output, just do a raw (slower) | ||
| 777 | * mod_exp and return that instead. */ | ||
| 778 | |||
| 779 | BIGNUM local_d; | ||
| 780 | BIGNUM *d = NULL; | ||
| 781 | |||
| 782 | if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME)) | ||
| 783 | { | ||
| 784 | d = &local_d; | ||
| 785 | BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME); | ||
| 786 | } | ||
| 787 | else | ||
| 788 | d = rsa->d; | ||
| 789 | if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,NULL)) goto err; | ||
| 790 | } | ||
| 791 | } | ||
| 792 | ret=1; | ||
| 793 | err: | ||
| 794 | BN_clear_free(&m1); | ||
| 795 | BN_clear_free(&r1); | ||
| 796 | BN_clear_free(&vrfy); | ||
| 797 | BN_CTX_free(ctx); | ||
| 798 | return(ret); | ||
| 799 | } | ||
| 800 | |||
| 801 | static int RSA_eay_init(RSA *rsa) | ||
| 802 | { | ||
| 803 | rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; | ||
| 804 | return(1); | ||
| 805 | } | ||
| 806 | |||
| 807 | static int RSA_eay_finish(RSA *rsa) | ||
| 808 | { | ||
| 809 | if (rsa->_method_mod_n != NULL) | ||
| 810 | BN_MONT_CTX_free(rsa->_method_mod_n); | ||
| 811 | if (rsa->_method_mod_p != NULL) | ||
| 812 | BN_MONT_CTX_free(rsa->_method_mod_p); | ||
| 813 | if (rsa->_method_mod_q != NULL) | ||
| 814 | BN_MONT_CTX_free(rsa->_method_mod_q); | ||
| 815 | return(1); | ||
| 816 | } | ||
| 817 | |||
| 818 | #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..ddcb28e663 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_err.c | |||
| @@ -0,0 +1,165 @@ | |||
| 1 | /* crypto/rsa/rsa_err.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999-2005 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 | |||
| 68 | #define ERR_FUNC(func) ERR_PACK(ERR_LIB_RSA,func,0) | ||
| 69 | #define ERR_REASON(reason) ERR_PACK(ERR_LIB_RSA,0,reason) | ||
| 70 | |||
| 71 | static ERR_STRING_DATA RSA_str_functs[]= | ||
| 72 | { | ||
| 73 | {ERR_FUNC(RSA_F_MEMORY_LOCK), "MEMORY_LOCK"}, | ||
| 74 | {ERR_FUNC(RSA_F_RSA_CHECK_KEY), "RSA_check_key"}, | ||
| 75 | {ERR_FUNC(RSA_F_RSA_EAY_PRIVATE_DECRYPT), "RSA_EAY_PRIVATE_DECRYPT"}, | ||
| 76 | {ERR_FUNC(RSA_F_RSA_EAY_PRIVATE_ENCRYPT), "RSA_EAY_PRIVATE_ENCRYPT"}, | ||
| 77 | {ERR_FUNC(RSA_F_RSA_EAY_PUBLIC_DECRYPT), "RSA_EAY_PUBLIC_DECRYPT"}, | ||
| 78 | {ERR_FUNC(RSA_F_RSA_EAY_PUBLIC_ENCRYPT), "RSA_EAY_PUBLIC_ENCRYPT"}, | ||
| 79 | {ERR_FUNC(RSA_F_RSA_GENERATE_KEY), "RSA_generate_key"}, | ||
| 80 | {ERR_FUNC(RSA_F_RSA_NEW_METHOD), "RSA_new_method"}, | ||
| 81 | {ERR_FUNC(RSA_F_RSA_NULL), "RSA_NULL"}, | ||
| 82 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_NONE), "RSA_padding_add_none"}, | ||
| 83 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP), "RSA_padding_add_PKCS1_OAEP"}, | ||
| 84 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_PSS), "RSA_padding_add_PKCS1_PSS"}, | ||
| 85 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1), "RSA_padding_add_PKCS1_type_1"}, | ||
| 86 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2), "RSA_padding_add_PKCS1_type_2"}, | ||
| 87 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_SSLV23), "RSA_padding_add_SSLv23"}, | ||
| 88 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_X931), "RSA_padding_add_X931"}, | ||
| 89 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_NONE), "RSA_padding_check_none"}, | ||
| 90 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP), "RSA_padding_check_PKCS1_OAEP"}, | ||
| 91 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1), "RSA_padding_check_PKCS1_type_1"}, | ||
| 92 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2), "RSA_padding_check_PKCS1_type_2"}, | ||
| 93 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_SSLV23), "RSA_padding_check_SSLv23"}, | ||
| 94 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_X931), "RSA_padding_check_X931"}, | ||
| 95 | {ERR_FUNC(RSA_F_RSA_PRINT), "RSA_print"}, | ||
| 96 | {ERR_FUNC(RSA_F_RSA_PRINT_FP), "RSA_print_fp"}, | ||
| 97 | {ERR_FUNC(RSA_F_RSA_SIGN), "RSA_sign"}, | ||
| 98 | {ERR_FUNC(RSA_F_RSA_SIGN_ASN1_OCTET_STRING), "RSA_sign_ASN1_OCTET_STRING"}, | ||
| 99 | {ERR_FUNC(RSA_F_RSA_VERIFY), "RSA_verify"}, | ||
| 100 | {ERR_FUNC(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING), "RSA_verify_ASN1_OCTET_STRING"}, | ||
| 101 | {ERR_FUNC(RSA_F_RSA_VERIFY_PKCS1_PSS), "RSA_verify_PKCS1_PSS"}, | ||
| 102 | {0,NULL} | ||
| 103 | }; | ||
| 104 | |||
| 105 | static ERR_STRING_DATA RSA_str_reasons[]= | ||
| 106 | { | ||
| 107 | {ERR_REASON(RSA_R_ALGORITHM_MISMATCH) ,"algorithm mismatch"}, | ||
| 108 | {ERR_REASON(RSA_R_BAD_E_VALUE) ,"bad e value"}, | ||
| 109 | {ERR_REASON(RSA_R_BAD_FIXED_HEADER_DECRYPT),"bad fixed header decrypt"}, | ||
| 110 | {ERR_REASON(RSA_R_BAD_PAD_BYTE_COUNT) ,"bad pad byte count"}, | ||
| 111 | {ERR_REASON(RSA_R_BAD_SIGNATURE) ,"bad signature"}, | ||
| 112 | {ERR_REASON(RSA_R_BLOCK_TYPE_IS_NOT_01) ,"block type is not 01"}, | ||
| 113 | {ERR_REASON(RSA_R_BLOCK_TYPE_IS_NOT_02) ,"block type is not 02"}, | ||
| 114 | {ERR_REASON(RSA_R_DATA_GREATER_THAN_MOD_LEN),"data greater than mod len"}, | ||
| 115 | {ERR_REASON(RSA_R_DATA_TOO_LARGE) ,"data too large"}, | ||
| 116 | {ERR_REASON(RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE),"data too large for key size"}, | ||
| 117 | {ERR_REASON(RSA_R_DATA_TOO_LARGE_FOR_MODULUS),"data too large for modulus"}, | ||
| 118 | {ERR_REASON(RSA_R_DATA_TOO_SMALL) ,"data too small"}, | ||
| 119 | {ERR_REASON(RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE),"data too small for key size"}, | ||
| 120 | {ERR_REASON(RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY),"digest too big for rsa key"}, | ||
| 121 | {ERR_REASON(RSA_R_DMP1_NOT_CONGRUENT_TO_D),"dmp1 not congruent to d"}, | ||
| 122 | {ERR_REASON(RSA_R_DMQ1_NOT_CONGRUENT_TO_D),"dmq1 not congruent to d"}, | ||
| 123 | {ERR_REASON(RSA_R_D_E_NOT_CONGRUENT_TO_1),"d e not congruent to 1"}, | ||
| 124 | {ERR_REASON(RSA_R_FIRST_OCTET_INVALID) ,"first octet invalid"}, | ||
| 125 | {ERR_REASON(RSA_R_INVALID_HEADER) ,"invalid header"}, | ||
| 126 | {ERR_REASON(RSA_R_INVALID_MESSAGE_LENGTH),"invalid message length"}, | ||
| 127 | {ERR_REASON(RSA_R_INVALID_PADDING) ,"invalid padding"}, | ||
| 128 | {ERR_REASON(RSA_R_INVALID_TRAILER) ,"invalid trailer"}, | ||
| 129 | {ERR_REASON(RSA_R_IQMP_NOT_INVERSE_OF_Q) ,"iqmp not inverse of q"}, | ||
| 130 | {ERR_REASON(RSA_R_KEY_SIZE_TOO_SMALL) ,"key size too small"}, | ||
| 131 | {ERR_REASON(RSA_R_LAST_OCTET_INVALID) ,"last octet invalid"}, | ||
| 132 | {ERR_REASON(RSA_R_MODULUS_TOO_LARGE) ,"modulus too large"}, | ||
| 133 | {ERR_REASON(RSA_R_NULL_BEFORE_BLOCK_MISSING),"null before block missing"}, | ||
| 134 | {ERR_REASON(RSA_R_N_DOES_NOT_EQUAL_P_Q) ,"n does not equal p q"}, | ||
| 135 | {ERR_REASON(RSA_R_OAEP_DECODING_ERROR) ,"oaep decoding error"}, | ||
| 136 | {ERR_REASON(RSA_R_SLEN_RECOVERY_FAILED) ,"salt length recovery failed"}, | ||
| 137 | {ERR_REASON(RSA_R_PADDING_CHECK_FAILED) ,"padding check failed"}, | ||
| 138 | {ERR_REASON(RSA_R_P_NOT_PRIME) ,"p not prime"}, | ||
| 139 | {ERR_REASON(RSA_R_Q_NOT_PRIME) ,"q not prime"}, | ||
| 140 | {ERR_REASON(RSA_R_RSA_OPERATIONS_NOT_SUPPORTED),"rsa operations not supported"}, | ||
| 141 | {ERR_REASON(RSA_R_SSLV3_ROLLBACK_ATTACK) ,"sslv3 rollback attack"}, | ||
| 142 | {ERR_REASON(RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD),"the asn1 object identifier is not known for this md"}, | ||
| 143 | {ERR_REASON(RSA_R_UNKNOWN_ALGORITHM_TYPE),"unknown algorithm type"}, | ||
| 144 | {ERR_REASON(RSA_R_UNKNOWN_PADDING_TYPE) ,"unknown padding type"}, | ||
| 145 | {ERR_REASON(RSA_R_WRONG_SIGNATURE_LENGTH),"wrong signature length"}, | ||
| 146 | {ERR_REASON(RSA_R_SLEN_CHECK_FAILED) ,"salt length check failed"}, | ||
| 147 | {0,NULL} | ||
| 148 | }; | ||
| 149 | |||
| 150 | #endif | ||
| 151 | |||
| 152 | void ERR_load_RSA_strings(void) | ||
| 153 | { | ||
| 154 | static int init=1; | ||
| 155 | |||
| 156 | if (init) | ||
| 157 | { | ||
| 158 | init=0; | ||
| 159 | #ifndef OPENSSL_NO_ERR | ||
| 160 | ERR_load_strings(0,RSA_str_functs); | ||
| 161 | ERR_load_strings(0,RSA_str_reasons); | ||
| 162 | #endif | ||
| 163 | |||
| 164 | } | ||
| 165 | } | ||
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c new file mode 100644 index 0000000000..dd1422cc98 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_gen.c | |||
| @@ -0,0 +1,201 @@ | |||
| 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 | if (ctx != NULL) | ||
| 188 | BN_CTX_end(ctx); | ||
| 189 | BN_CTX_free(ctx); | ||
| 190 | BN_CTX_free(ctx2); | ||
| 191 | |||
| 192 | if (!ok) | ||
| 193 | { | ||
| 194 | if (rsa != NULL) RSA_free(rsa); | ||
| 195 | return(NULL); | ||
| 196 | } | ||
| 197 | else | ||
| 198 | return(rsa); | ||
| 199 | } | ||
| 200 | |||
| 201 | #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_null.c b/src/lib/libcrypto/rsa/rsa_null.c index 491572c82b..64057fbdcf 100644 --- a/src/lib/libcrypto/rsa/rsa_null.c +++ b/src/lib/libcrypto/rsa/rsa_null.c | |||
| @@ -94,9 +94,6 @@ static RSA_METHOD rsa_null_meth={ | |||
| 94 | RSA_null_finish, | 94 | RSA_null_finish, |
| 95 | 0, | 95 | 0, |
| 96 | NULL, | 96 | NULL, |
| 97 | NULL, | ||
| 98 | NULL, | ||
| 99 | NULL | ||
| 100 | }; | 97 | }; |
| 101 | 98 | ||
| 102 | const RSA_METHOD *RSA_null_method(void) | 99 | const RSA_METHOD *RSA_null_method(void) |
| @@ -107,35 +104,35 @@ const RSA_METHOD *RSA_null_method(void) | |||
| 107 | static int RSA_null_public_encrypt(int flen, const unsigned char *from, | 104 | static int RSA_null_public_encrypt(int flen, const unsigned char *from, |
| 108 | unsigned char *to, RSA *rsa, int padding) | 105 | unsigned char *to, RSA *rsa, int padding) |
| 109 | { | 106 | { |
| 110 | RSAerr(RSA_F_RSA_NULL_PUBLIC_ENCRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | 107 | RSAerr(RSA_F_RSA_NULL, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); |
| 111 | return -1; | 108 | return -1; |
| 112 | } | 109 | } |
| 113 | 110 | ||
| 114 | static int RSA_null_private_encrypt(int flen, const unsigned char *from, | 111 | static int RSA_null_private_encrypt(int flen, const unsigned char *from, |
| 115 | unsigned char *to, RSA *rsa, int padding) | 112 | unsigned char *to, RSA *rsa, int padding) |
| 116 | { | 113 | { |
| 117 | RSAerr(RSA_F_RSA_NULL_PRIVATE_ENCRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | 114 | RSAerr(RSA_F_RSA_NULL, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); |
| 118 | return -1; | 115 | return -1; |
| 119 | } | 116 | } |
| 120 | 117 | ||
| 121 | static int RSA_null_private_decrypt(int flen, const unsigned char *from, | 118 | static int RSA_null_private_decrypt(int flen, const unsigned char *from, |
| 122 | unsigned char *to, RSA *rsa, int padding) | 119 | unsigned char *to, RSA *rsa, int padding) |
| 123 | { | 120 | { |
| 124 | RSAerr(RSA_F_RSA_NULL_PRIVATE_DECRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | 121 | RSAerr(RSA_F_RSA_NULL, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); |
| 125 | return -1; | 122 | return -1; |
| 126 | } | 123 | } |
| 127 | 124 | ||
| 128 | static int RSA_null_public_decrypt(int flen, const unsigned char *from, | 125 | static int RSA_null_public_decrypt(int flen, const unsigned char *from, |
| 129 | unsigned char *to, RSA *rsa, int padding) | 126 | unsigned char *to, RSA *rsa, int padding) |
| 130 | { | 127 | { |
| 131 | RSAerr(RSA_F_RSA_NULL_PUBLIC_DECRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | 128 | RSAerr(RSA_F_RSA_NULL, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); |
| 132 | return -1; | 129 | return -1; |
| 133 | } | 130 | } |
| 134 | 131 | ||
| 135 | #if 0 /* not currently used */ | 132 | #if 0 /* not currently used */ |
| 136 | static int RSA_null_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa) | 133 | static int RSA_null_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa) |
| 137 | { | 134 | { |
| 138 | ...err(RSA_F_RSA_NULL_MOD_EXP, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); | 135 | RSAerr(RSA_F_RSA_NULL, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); |
| 139 | return -1; | 136 | return -1; |
| 140 | } | 137 | } |
| 141 | #endif | 138 | #endif |
| @@ -149,3 +146,5 @@ static int RSA_null_finish(RSA *rsa) | |||
| 149 | { | 146 | { |
| 150 | return(1); | 147 | return(1); |
| 151 | } | 148 | } |
| 149 | |||
| 150 | |||
diff --git a/src/lib/libcrypto/rsa/rsa_oaep.c b/src/lib/libcrypto/rsa/rsa_oaep.c new file mode 100644 index 0000000000..d43ecaca63 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_oaep.c | |||
| @@ -0,0 +1,212 @@ | |||
| 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 RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, | ||
| 32 | const unsigned char *from, int flen, | ||
| 33 | const unsigned char *param, int plen) | ||
| 34 | { | ||
| 35 | int i, emlen = tlen - 1; | ||
| 36 | unsigned char *db, *seed; | ||
| 37 | unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH]; | ||
| 38 | |||
| 39 | if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1) | ||
| 40 | { | ||
| 41 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, | ||
| 42 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | |||
| 46 | if (emlen < 2 * SHA_DIGEST_LENGTH + 1) | ||
| 47 | { | ||
| 48 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL); | ||
| 49 | return 0; | ||
| 50 | } | ||
| 51 | |||
| 52 | dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH); | ||
| 53 | if (dbmask == NULL) | ||
| 54 | { | ||
| 55 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE); | ||
| 56 | return 0; | ||
| 57 | } | ||
| 58 | |||
| 59 | to[0] = 0; | ||
| 60 | seed = to + 1; | ||
| 61 | db = to + SHA_DIGEST_LENGTH + 1; | ||
| 62 | |||
| 63 | EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL); | ||
| 64 | memset(db + SHA_DIGEST_LENGTH, 0, | ||
| 65 | emlen - flen - 2 * SHA_DIGEST_LENGTH - 1); | ||
| 66 | db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01; | ||
| 67 | memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen); | ||
| 68 | if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0) | ||
| 69 | return 0; | ||
| 70 | #ifdef PKCS_TESTVECT | ||
| 71 | memcpy(seed, | ||
| 72 | "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f", | ||
| 73 | 20); | ||
| 74 | #endif | ||
| 75 | |||
| 76 | PKCS1_MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH, | ||
| 77 | EVP_sha1()); | ||
| 78 | for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++) | ||
| 79 | db[i] ^= dbmask[i]; | ||
| 80 | |||
| 81 | PKCS1_MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH, | ||
| 82 | EVP_sha1()); | ||
| 83 | for (i = 0; i < SHA_DIGEST_LENGTH; i++) | ||
| 84 | seed[i] ^= seedmask[i]; | ||
| 85 | |||
| 86 | OPENSSL_free(dbmask); | ||
| 87 | return 1; | ||
| 88 | } | ||
| 89 | |||
| 90 | int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen, | ||
| 91 | const unsigned char *from, int flen, int num, | ||
| 92 | const unsigned char *param, int plen) | ||
| 93 | { | ||
| 94 | int i, dblen, mlen = -1; | ||
| 95 | const unsigned char *maskeddb; | ||
| 96 | int lzero; | ||
| 97 | unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH]; | ||
| 98 | int bad = 0; | ||
| 99 | |||
| 100 | if (--num < 2 * SHA_DIGEST_LENGTH + 1) | ||
| 101 | /* 'num' is the length of the modulus, i.e. does not depend on the | ||
| 102 | * particular ciphertext. */ | ||
| 103 | goto decoding_err; | ||
| 104 | |||
| 105 | lzero = num - flen; | ||
| 106 | if (lzero < 0) | ||
| 107 | { | ||
| 108 | /* lzero == -1 */ | ||
| 109 | |||
| 110 | /* signalling this error immediately after detection might allow | ||
| 111 | * for side-channel attacks (e.g. timing if 'plen' is huge | ||
| 112 | * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal | ||
| 113 | * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001), | ||
| 114 | * so we use a 'bad' flag */ | ||
| 115 | bad = 1; | ||
| 116 | lzero = 0; | ||
| 117 | } | ||
| 118 | maskeddb = from - lzero + SHA_DIGEST_LENGTH; | ||
| 119 | |||
| 120 | dblen = num - SHA_DIGEST_LENGTH; | ||
| 121 | db = OPENSSL_malloc(dblen); | ||
| 122 | if (db == NULL) | ||
| 123 | { | ||
| 124 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE); | ||
| 125 | return -1; | ||
| 126 | } | ||
| 127 | |||
| 128 | PKCS1_MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen, EVP_sha1()); | ||
| 129 | for (i = lzero; i < SHA_DIGEST_LENGTH; i++) | ||
| 130 | seed[i] ^= from[i - lzero]; | ||
| 131 | |||
| 132 | PKCS1_MGF1(db, dblen, seed, SHA_DIGEST_LENGTH, EVP_sha1()); | ||
| 133 | for (i = 0; i < dblen; i++) | ||
| 134 | db[i] ^= maskeddb[i]; | ||
| 135 | |||
| 136 | EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL); | ||
| 137 | |||
| 138 | if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad) | ||
| 139 | goto decoding_err; | ||
| 140 | else | ||
| 141 | { | ||
| 142 | for (i = SHA_DIGEST_LENGTH; i < dblen; i++) | ||
| 143 | if (db[i] != 0x00) | ||
| 144 | break; | ||
| 145 | if (db[i] != 0x01 || i++ >= dblen) | ||
| 146 | goto decoding_err; | ||
| 147 | else | ||
| 148 | { | ||
| 149 | /* everything looks OK */ | ||
| 150 | |||
| 151 | mlen = dblen - i; | ||
| 152 | if (tlen < mlen) | ||
| 153 | { | ||
| 154 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE); | ||
| 155 | mlen = -1; | ||
| 156 | } | ||
| 157 | else | ||
| 158 | memcpy(to, db + i, mlen); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | OPENSSL_free(db); | ||
| 162 | return mlen; | ||
| 163 | |||
| 164 | decoding_err: | ||
| 165 | /* to avoid chosen ciphertext attacks, the error message should not reveal | ||
| 166 | * which kind of decoding error happened */ | ||
| 167 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR); | ||
| 168 | if (db != NULL) OPENSSL_free(db); | ||
| 169 | return -1; | ||
| 170 | } | ||
| 171 | |||
| 172 | int PKCS1_MGF1(unsigned char *mask, long len, | ||
| 173 | const unsigned char *seed, long seedlen, const EVP_MD *dgst) | ||
| 174 | { | ||
| 175 | long i, outlen = 0; | ||
| 176 | unsigned char cnt[4]; | ||
| 177 | EVP_MD_CTX c; | ||
| 178 | unsigned char md[EVP_MAX_MD_SIZE]; | ||
| 179 | int mdlen; | ||
| 180 | |||
| 181 | EVP_MD_CTX_init(&c); | ||
| 182 | mdlen = EVP_MD_size(dgst); | ||
| 183 | for (i = 0; outlen < len; i++) | ||
| 184 | { | ||
| 185 | cnt[0] = (unsigned char)((i >> 24) & 255); | ||
| 186 | cnt[1] = (unsigned char)((i >> 16) & 255); | ||
| 187 | cnt[2] = (unsigned char)((i >> 8)) & 255; | ||
| 188 | cnt[3] = (unsigned char)(i & 255); | ||
| 189 | EVP_DigestInit_ex(&c,dgst, NULL); | ||
| 190 | EVP_DigestUpdate(&c, seed, seedlen); | ||
| 191 | EVP_DigestUpdate(&c, cnt, 4); | ||
| 192 | if (outlen + mdlen <= len) | ||
| 193 | { | ||
| 194 | EVP_DigestFinal_ex(&c, mask + outlen, NULL); | ||
| 195 | outlen += mdlen; | ||
| 196 | } | ||
| 197 | else | ||
| 198 | { | ||
| 199 | EVP_DigestFinal_ex(&c, md, NULL); | ||
| 200 | memcpy(mask + outlen, md, len - outlen); | ||
| 201 | outlen = len; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | EVP_MD_CTX_cleanup(&c); | ||
| 205 | return 0; | ||
| 206 | } | ||
| 207 | |||
| 208 | int MGF1(unsigned char *mask, long len, const unsigned char *seed, long seedlen) | ||
| 209 | { | ||
| 210 | return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1()); | ||
| 211 | } | ||
| 212 | #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_pss.c b/src/lib/libcrypto/rsa/rsa_pss.c new file mode 100644 index 0000000000..2815628f5f --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_pss.c | |||
| @@ -0,0 +1,261 @@ | |||
| 1 | /* rsa_pss.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project 2005. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2005 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/evp.h> | ||
| 64 | #include <openssl/rand.h> | ||
| 65 | #include <openssl/sha.h> | ||
| 66 | |||
| 67 | const static unsigned char zeroes[] = {0,0,0,0,0,0,0,0}; | ||
| 68 | |||
| 69 | int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, | ||
| 70 | const EVP_MD *Hash, const unsigned char *EM, int sLen) | ||
| 71 | { | ||
| 72 | int i; | ||
| 73 | int ret = 0; | ||
| 74 | int hLen, maskedDBLen, MSBits, emLen; | ||
| 75 | const unsigned char *H; | ||
| 76 | unsigned char *DB = NULL; | ||
| 77 | EVP_MD_CTX ctx; | ||
| 78 | unsigned char H_[EVP_MAX_MD_SIZE]; | ||
| 79 | |||
| 80 | hLen = EVP_MD_size(Hash); | ||
| 81 | /* | ||
| 82 | * Negative sLen has special meanings: | ||
| 83 | * -1 sLen == hLen | ||
| 84 | * -2 salt length is autorecovered from signature | ||
| 85 | * -N reserved | ||
| 86 | */ | ||
| 87 | if (sLen == -1) sLen = hLen; | ||
| 88 | else if (sLen == -2) sLen = -2; | ||
| 89 | else if (sLen < -2) | ||
| 90 | { | ||
| 91 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED); | ||
| 92 | goto err; | ||
| 93 | } | ||
| 94 | |||
| 95 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; | ||
| 96 | emLen = RSA_size(rsa); | ||
| 97 | if (EM[0] & (0xFF << MSBits)) | ||
| 98 | { | ||
| 99 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_FIRST_OCTET_INVALID); | ||
| 100 | goto err; | ||
| 101 | } | ||
| 102 | if (MSBits == 0) | ||
| 103 | { | ||
| 104 | EM++; | ||
| 105 | emLen--; | ||
| 106 | } | ||
| 107 | if (emLen < (hLen + sLen + 2)) /* sLen can be small negative */ | ||
| 108 | { | ||
| 109 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_DATA_TOO_LARGE); | ||
| 110 | goto err; | ||
| 111 | } | ||
| 112 | if (EM[emLen - 1] != 0xbc) | ||
| 113 | { | ||
| 114 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_LAST_OCTET_INVALID); | ||
| 115 | goto err; | ||
| 116 | } | ||
| 117 | maskedDBLen = emLen - hLen - 1; | ||
| 118 | H = EM + maskedDBLen; | ||
| 119 | DB = OPENSSL_malloc(maskedDBLen); | ||
| 120 | if (!DB) | ||
| 121 | { | ||
| 122 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, ERR_R_MALLOC_FAILURE); | ||
| 123 | goto err; | ||
| 124 | } | ||
| 125 | PKCS1_MGF1(DB, maskedDBLen, H, hLen, Hash); | ||
| 126 | for (i = 0; i < maskedDBLen; i++) | ||
| 127 | DB[i] ^= EM[i]; | ||
| 128 | if (MSBits) | ||
| 129 | DB[0] &= 0xFF >> (8 - MSBits); | ||
| 130 | for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) ; | ||
| 131 | if (DB[i++] != 0x1) | ||
| 132 | { | ||
| 133 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_RECOVERY_FAILED); | ||
| 134 | goto err; | ||
| 135 | } | ||
| 136 | if (sLen >= 0 && (maskedDBLen - i) != sLen) | ||
| 137 | { | ||
| 138 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED); | ||
| 139 | goto err; | ||
| 140 | } | ||
| 141 | EVP_MD_CTX_init(&ctx); | ||
| 142 | EVP_DigestInit_ex(&ctx, Hash, NULL); | ||
| 143 | EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes); | ||
| 144 | EVP_DigestUpdate(&ctx, mHash, hLen); | ||
| 145 | if (maskedDBLen - i) | ||
| 146 | EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i); | ||
| 147 | EVP_DigestFinal(&ctx, H_, NULL); | ||
| 148 | EVP_MD_CTX_cleanup(&ctx); | ||
| 149 | if (memcmp(H_, H, hLen)) | ||
| 150 | { | ||
| 151 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_BAD_SIGNATURE); | ||
| 152 | ret = 0; | ||
| 153 | } | ||
| 154 | else | ||
| 155 | ret = 1; | ||
| 156 | |||
| 157 | err: | ||
| 158 | if (DB) | ||
| 159 | OPENSSL_free(DB); | ||
| 160 | |||
| 161 | return ret; | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM, | ||
| 166 | const unsigned char *mHash, | ||
| 167 | const EVP_MD *Hash, int sLen) | ||
| 168 | { | ||
| 169 | int i; | ||
| 170 | int ret = 0; | ||
| 171 | int hLen, maskedDBLen, MSBits, emLen; | ||
| 172 | unsigned char *H, *salt = NULL, *p; | ||
| 173 | EVP_MD_CTX ctx; | ||
| 174 | |||
| 175 | hLen = EVP_MD_size(Hash); | ||
| 176 | /* | ||
| 177 | * Negative sLen has special meanings: | ||
| 178 | * -1 sLen == hLen | ||
| 179 | * -2 salt length is maximized | ||
| 180 | * -N reserved | ||
| 181 | */ | ||
| 182 | if (sLen == -1) sLen = hLen; | ||
| 183 | else if (sLen == -2) sLen = -2; | ||
| 184 | else if (sLen < -2) | ||
| 185 | { | ||
| 186 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED); | ||
| 187 | goto err; | ||
| 188 | } | ||
| 189 | |||
| 190 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; | ||
| 191 | emLen = RSA_size(rsa); | ||
| 192 | if (MSBits == 0) | ||
| 193 | { | ||
| 194 | *EM++ = 0; | ||
| 195 | emLen--; | ||
| 196 | } | ||
| 197 | if (sLen == -2) | ||
| 198 | { | ||
| 199 | sLen = emLen - hLen - 2; | ||
| 200 | } | ||
| 201 | else if (emLen < (hLen + sLen + 2)) | ||
| 202 | { | ||
| 203 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, | ||
| 204 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 205 | goto err; | ||
| 206 | } | ||
| 207 | if (sLen > 0) | ||
| 208 | { | ||
| 209 | salt = OPENSSL_malloc(sLen); | ||
| 210 | if (!salt) | ||
| 211 | { | ||
| 212 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, | ||
| 213 | ERR_R_MALLOC_FAILURE); | ||
| 214 | goto err; | ||
| 215 | } | ||
| 216 | if (!RAND_bytes(salt, sLen)) | ||
| 217 | goto err; | ||
| 218 | } | ||
| 219 | maskedDBLen = emLen - hLen - 1; | ||
| 220 | H = EM + maskedDBLen; | ||
| 221 | EVP_MD_CTX_init(&ctx); | ||
| 222 | EVP_DigestInit_ex(&ctx, Hash, NULL); | ||
| 223 | EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes); | ||
| 224 | EVP_DigestUpdate(&ctx, mHash, hLen); | ||
| 225 | if (sLen) | ||
| 226 | EVP_DigestUpdate(&ctx, salt, sLen); | ||
| 227 | EVP_DigestFinal(&ctx, H, NULL); | ||
| 228 | EVP_MD_CTX_cleanup(&ctx); | ||
| 229 | |||
| 230 | /* Generate dbMask in place then perform XOR on it */ | ||
| 231 | PKCS1_MGF1(EM, maskedDBLen, H, hLen, Hash); | ||
| 232 | |||
| 233 | p = EM; | ||
| 234 | |||
| 235 | /* Initial PS XORs with all zeroes which is a NOP so just update | ||
| 236 | * pointer. Note from a test above this value is guaranteed to | ||
| 237 | * be non-negative. | ||
| 238 | */ | ||
| 239 | p += emLen - sLen - hLen - 2; | ||
| 240 | *p++ ^= 0x1; | ||
| 241 | if (sLen > 0) | ||
| 242 | { | ||
| 243 | for (i = 0; i < sLen; i++) | ||
| 244 | *p++ ^= salt[i]; | ||
| 245 | } | ||
| 246 | if (MSBits) | ||
| 247 | EM[0] &= 0xFF >> (8 - MSBits); | ||
| 248 | |||
| 249 | /* H is already in place so just set final 0xbc */ | ||
| 250 | |||
| 251 | EM[emLen - 1] = 0xbc; | ||
| 252 | |||
| 253 | ret = 1; | ||
| 254 | |||
| 255 | err: | ||
| 256 | if (salt) | ||
| 257 | OPENSSL_free(salt); | ||
| 258 | |||
| 259 | return ret; | ||
| 260 | |||
| 261 | } | ||
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..db86f1ac58 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_sign.c | |||
| @@ -0,0 +1,249 @@ | |||
| 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 | |||
| 189 | /* Excess data can be used to create forgeries */ | ||
| 190 | if(p != s+i) | ||
| 191 | { | ||
| 192 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); | ||
| 193 | goto err; | ||
| 194 | } | ||
| 195 | |||
| 196 | /* Parameters to the signature algorithm can also be used to | ||
| 197 | create forgeries */ | ||
| 198 | if(sig->algor->parameter | ||
| 199 | && ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL) | ||
| 200 | { | ||
| 201 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); | ||
| 202 | goto err; | ||
| 203 | } | ||
| 204 | |||
| 205 | sigtype=OBJ_obj2nid(sig->algor->algorithm); | ||
| 206 | |||
| 207 | |||
| 208 | #ifdef RSA_DEBUG | ||
| 209 | /* put a backward compatibility flag in EAY */ | ||
| 210 | fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype), | ||
| 211 | OBJ_nid2ln(dtype)); | ||
| 212 | #endif | ||
| 213 | if (sigtype != dtype) | ||
| 214 | { | ||
| 215 | if (((dtype == NID_md5) && | ||
| 216 | (sigtype == NID_md5WithRSAEncryption)) || | ||
| 217 | ((dtype == NID_md2) && | ||
| 218 | (sigtype == NID_md2WithRSAEncryption))) | ||
| 219 | { | ||
| 220 | /* ok, we will let it through */ | ||
| 221 | #if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16) | ||
| 222 | fprintf(stderr,"signature has problems, re-make with post SSLeay045\n"); | ||
| 223 | #endif | ||
| 224 | } | ||
| 225 | else | ||
| 226 | { | ||
| 227 | RSAerr(RSA_F_RSA_VERIFY, | ||
| 228 | RSA_R_ALGORITHM_MISMATCH); | ||
| 229 | goto err; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | if ( ((unsigned int)sig->digest->length != m_len) || | ||
| 233 | (memcmp(m,sig->digest->data,m_len) != 0)) | ||
| 234 | { | ||
| 235 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); | ||
| 236 | } | ||
| 237 | else | ||
| 238 | ret=1; | ||
| 239 | } | ||
| 240 | err: | ||
| 241 | if (sig != NULL) X509_SIG_free(sig); | ||
| 242 | if (s != NULL) | ||
| 243 | { | ||
| 244 | OPENSSL_cleanse(s,(unsigned int)siglen); | ||
| 245 | OPENSSL_free(s); | ||
| 246 | } | ||
| 247 | return(ret); | ||
| 248 | } | ||
| 249 | |||
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 4080de8bcf..218bb2a39b 100644 --- a/src/lib/libcrypto/rsa/rsa_test.c +++ b/src/lib/libcrypto/rsa/rsa_test.c | |||
| @@ -8,7 +8,6 @@ | |||
| 8 | #include <openssl/crypto.h> | 8 | #include <openssl/crypto.h> |
| 9 | #include <openssl/err.h> | 9 | #include <openssl/err.h> |
| 10 | #include <openssl/rand.h> | 10 | #include <openssl/rand.h> |
| 11 | #include <openssl/bn.h> | ||
| 12 | #ifdef OPENSSL_NO_RSA | 11 | #ifdef OPENSSL_NO_RSA |
| 13 | int main(int argc, char *argv[]) | 12 | int main(int argc, char *argv[]) |
| 14 | { | 13 | { |
| @@ -219,7 +218,6 @@ int main(int argc, char *argv[]) | |||
| 219 | int plen; | 218 | int plen; |
| 220 | int clen = 0; | 219 | int clen = 0; |
| 221 | int num; | 220 | int num; |
| 222 | int n; | ||
| 223 | 221 | ||
| 224 | CRYPTO_malloc_debug_init(); | 222 | CRYPTO_malloc_debug_init(); |
| 225 | CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL); | 223 | CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL); |
| @@ -243,7 +241,7 @@ int main(int argc, char *argv[]) | |||
| 243 | clen = key3(key, ctext_ex); | 241 | clen = key3(key, ctext_ex); |
| 244 | break; | 242 | break; |
| 245 | } | 243 | } |
| 246 | if (v/3 >= 1) key->flags |= RSA_FLAG_NO_CONSTTIME; | 244 | if (v/3 > 1) key->flags |= RSA_FLAG_NO_EXP_CONSTTIME; |
| 247 | 245 | ||
| 248 | num = RSA_public_encrypt(plen, ptext_ex, ctext, key, | 246 | num = RSA_public_encrypt(plen, ptext_ex, ctext, key, |
| 249 | RSA_PKCS1_PADDING); | 247 | RSA_PKCS1_PADDING); |
| @@ -279,7 +277,7 @@ int main(int argc, char *argv[]) | |||
| 279 | err=1; | 277 | err=1; |
| 280 | goto next; | 278 | goto next; |
| 281 | } | 279 | } |
| 282 | 280 | ||
| 283 | num = RSA_private_decrypt(num, ctext, ptext, key, | 281 | num = RSA_private_decrypt(num, ctext, ptext, key, |
| 284 | RSA_PKCS1_OAEP_PADDING); | 282 | RSA_PKCS1_OAEP_PADDING); |
| 285 | if (num != plen || memcmp(ptext, ptext_ex, num) != 0) | 283 | if (num != plen || memcmp(ptext, ptext_ex, num) != 0) |
| @@ -288,7 +286,10 @@ int main(int argc, char *argv[]) | |||
| 288 | err=1; | 286 | err=1; |
| 289 | } | 287 | } |
| 290 | else if (memcmp(ctext, ctext_ex, num) == 0) | 288 | else if (memcmp(ctext, ctext_ex, num) == 0) |
| 289 | { | ||
| 291 | printf("OAEP test vector %d passed!\n", v); | 290 | printf("OAEP test vector %d passed!\n", v); |
| 291 | goto next; | ||
| 292 | } | ||
| 292 | 293 | ||
| 293 | /* Different ciphertexts (rsa_oaep.c without -DPKCS_TESTVECT). | 294 | /* Different ciphertexts (rsa_oaep.c without -DPKCS_TESTVECT). |
| 294 | Try decrypting ctext_ex */ | 295 | Try decrypting ctext_ex */ |
| @@ -303,26 +304,6 @@ int main(int argc, char *argv[]) | |||
| 303 | } | 304 | } |
| 304 | else | 305 | else |
| 305 | printf("OAEP encryption/decryption ok\n"); | 306 | printf("OAEP encryption/decryption ok\n"); |
| 306 | |||
| 307 | /* Try decrypting corrupted ciphertexts */ | ||
| 308 | for(n = 0 ; n < clen ; ++n) | ||
| 309 | { | ||
| 310 | int b; | ||
| 311 | unsigned char saved = ctext[n]; | ||
| 312 | for(b = 0 ; b < 256 ; ++b) | ||
| 313 | { | ||
| 314 | if(b == saved) | ||
| 315 | continue; | ||
| 316 | ctext[n] = b; | ||
| 317 | num = RSA_private_decrypt(num, ctext, ptext, key, | ||
| 318 | RSA_PKCS1_OAEP_PADDING); | ||
| 319 | if(num > 0) | ||
| 320 | { | ||
| 321 | printf("Corrupt data decrypted!\n"); | ||
| 322 | err = 1; | ||
| 323 | } | ||
| 324 | } | ||
| 325 | } | ||
| 326 | next: | 307 | next: |
| 327 | RSA_free(key); | 308 | RSA_free(key); |
| 328 | } | 309 | } |
| @@ -332,9 +313,6 @@ int main(int argc, char *argv[]) | |||
| 332 | 313 | ||
| 333 | CRYPTO_mem_leaks_fp(stderr); | 314 | CRYPTO_mem_leaks_fp(stderr); |
| 334 | 315 | ||
| 335 | #ifdef OPENSSL_SYS_NETWARE | ||
| 336 | if (err) printf("ERROR: %d\n", err); | ||
| 337 | #endif | ||
| 338 | return err; | 316 | return err; |
| 339 | } | 317 | } |
| 340 | #endif | 318 | #endif |
diff --git a/src/lib/libcrypto/rsa/rsa_x931.c b/src/lib/libcrypto/rsa/rsa_x931.c new file mode 100644 index 0000000000..df3c45f802 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_x931.c | |||
| @@ -0,0 +1,177 @@ | |||
| 1 | /* rsa_x931.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
| 3 | * project 2005. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2005 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/rand.h> | ||
| 64 | #include <openssl/objects.h> | ||
| 65 | |||
| 66 | int RSA_padding_add_X931(unsigned char *to, int tlen, | ||
| 67 | const unsigned char *from, int flen) | ||
| 68 | { | ||
| 69 | int j; | ||
| 70 | unsigned char *p; | ||
| 71 | |||
| 72 | /* Absolute minimum amount of padding is 1 header nibble, 1 padding | ||
| 73 | * nibble and 2 trailer bytes: but 1 hash if is already in 'from'. | ||
| 74 | */ | ||
| 75 | |||
| 76 | j = tlen - flen - 2; | ||
| 77 | |||
| 78 | if (j < 0) | ||
| 79 | { | ||
| 80 | RSAerr(RSA_F_RSA_PADDING_ADD_X931,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 81 | return -1; | ||
| 82 | } | ||
| 83 | |||
| 84 | p=(unsigned char *)to; | ||
| 85 | |||
| 86 | /* If no padding start and end nibbles are in one byte */ | ||
| 87 | if (j == 0) | ||
| 88 | *p++ = 0x6A; | ||
| 89 | else | ||
| 90 | { | ||
| 91 | *p++ = 0x6B; | ||
| 92 | if (j > 1) | ||
| 93 | { | ||
| 94 | memset(p, 0xBB, j - 1); | ||
| 95 | p += j - 1; | ||
| 96 | } | ||
| 97 | *p++ = 0xBA; | ||
| 98 | } | ||
| 99 | memcpy(p,from,(unsigned int)flen); | ||
| 100 | p += flen; | ||
| 101 | *p = 0xCC; | ||
| 102 | return(1); | ||
| 103 | } | ||
| 104 | |||
| 105 | int RSA_padding_check_X931(unsigned char *to, int tlen, | ||
| 106 | const unsigned char *from, int flen, int num) | ||
| 107 | { | ||
| 108 | int i,j; | ||
| 109 | const unsigned char *p; | ||
| 110 | |||
| 111 | p=from; | ||
| 112 | if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B))) | ||
| 113 | { | ||
| 114 | RSAerr(RSA_F_RSA_PADDING_CHECK_X931,RSA_R_INVALID_HEADER); | ||
| 115 | return -1; | ||
| 116 | } | ||
| 117 | |||
| 118 | if (*p++ == 0x6B) | ||
| 119 | { | ||
| 120 | j=flen-3; | ||
| 121 | for (i = 0; i < j; i++) | ||
| 122 | { | ||
| 123 | unsigned char c = *p++; | ||
| 124 | if (c == 0xBA) | ||
| 125 | break; | ||
| 126 | if (c != 0xBB) | ||
| 127 | { | ||
| 128 | RSAerr(RSA_F_RSA_PADDING_CHECK_X931, | ||
| 129 | RSA_R_INVALID_PADDING); | ||
| 130 | return -1; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | j -= i; | ||
| 135 | |||
| 136 | if (i == 0) | ||
| 137 | { | ||
| 138 | RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING); | ||
| 139 | return -1; | ||
| 140 | } | ||
| 141 | |||
| 142 | } | ||
| 143 | else j = flen - 2; | ||
| 144 | |||
| 145 | if (p[j] != 0xCC) | ||
| 146 | { | ||
| 147 | RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_TRAILER); | ||
| 148 | return -1; | ||
| 149 | } | ||
| 150 | |||
| 151 | memcpy(to,p,(unsigned int)j); | ||
| 152 | |||
| 153 | return(j); | ||
| 154 | } | ||
| 155 | |||
| 156 | /* Translate between X931 hash ids and NIDs */ | ||
| 157 | |||
| 158 | int RSA_X931_hash_id(int nid) | ||
| 159 | { | ||
| 160 | switch (nid) | ||
| 161 | { | ||
| 162 | case NID_sha1: | ||
| 163 | return 0x33; | ||
| 164 | |||
| 165 | case NID_sha256: | ||
| 166 | return 0x34; | ||
| 167 | |||
| 168 | case NID_sha384: | ||
| 169 | return 0x36; | ||
| 170 | |||
| 171 | case NID_sha512: | ||
| 172 | return 0x35; | ||
| 173 | |||
| 174 | } | ||
| 175 | return -1; | ||
| 176 | } | ||
| 177 | |||
