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 | 496 | ||||
| -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_depr.c | 101 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_eay.c | 913 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_err.c | 179 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_gen.c | 222 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_lib.c | 234 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_none.c | 98 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_oaep.c | 220 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_pk1.c | 224 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_pss.c | 269 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_saos.c | 150 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_sign.c | 269 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_ssl.c | 154 | ||||
| -rw-r--r-- | src/lib/libcrypto/rsa/rsa_x931.c | 177 |
17 files changed, 4252 insertions, 0 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..5bb932ae15 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa.h | |||
| @@ -0,0 +1,496 @@ | |||
| 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/crypto.h> | ||
| 68 | #include <openssl/ossl_typ.h> | ||
| 69 | #ifndef OPENSSL_NO_DEPRECATED | ||
| 70 | #include <openssl/bn.h> | ||
| 71 | #endif | ||
| 72 | |||
| 73 | #ifdef OPENSSL_NO_RSA | ||
| 74 | #error RSA is disabled. | ||
| 75 | #endif | ||
| 76 | |||
| 77 | /* If this flag is set the RSA method is FIPS compliant and can be used | ||
| 78 | * in FIPS mode. This is set in the validated module method. If an | ||
| 79 | * application sets this flag in its own methods it is its reposibility | ||
| 80 | * to ensure the result is compliant. | ||
| 81 | */ | ||
| 82 | |||
| 83 | #define RSA_FLAG_FIPS_METHOD 0x0400 | ||
| 84 | |||
| 85 | /* If this flag is set the operations normally disabled in FIPS mode are | ||
| 86 | * permitted it is then the applications responsibility to ensure that the | ||
| 87 | * usage is compliant. | ||
| 88 | */ | ||
| 89 | |||
| 90 | #define RSA_FLAG_NON_FIPS_ALLOW 0x0400 | ||
| 91 | |||
| 92 | #ifdef OPENSSL_FIPS | ||
| 93 | #define FIPS_RSA_SIZE_T int | ||
| 94 | #endif | ||
| 95 | |||
| 96 | #ifdef __cplusplus | ||
| 97 | extern "C" { | ||
| 98 | #endif | ||
| 99 | |||
| 100 | /* Declared already in ossl_typ.h */ | ||
| 101 | /* typedef struct rsa_st RSA; */ | ||
| 102 | /* typedef struct rsa_meth_st RSA_METHOD; */ | ||
| 103 | |||
| 104 | struct rsa_meth_st | ||
| 105 | { | ||
| 106 | const char *name; | ||
| 107 | int (*rsa_pub_enc)(int flen,const unsigned char *from, | ||
| 108 | unsigned char *to, | ||
| 109 | RSA *rsa,int padding); | ||
| 110 | int (*rsa_pub_dec)(int flen,const unsigned char *from, | ||
| 111 | unsigned char *to, | ||
| 112 | RSA *rsa,int padding); | ||
| 113 | int (*rsa_priv_enc)(int flen,const unsigned char *from, | ||
| 114 | unsigned char *to, | ||
| 115 | RSA *rsa,int padding); | ||
| 116 | int (*rsa_priv_dec)(int flen,const unsigned char *from, | ||
| 117 | unsigned char *to, | ||
| 118 | RSA *rsa,int padding); | ||
| 119 | int (*rsa_mod_exp)(BIGNUM *r0,const BIGNUM *I,RSA *rsa,BN_CTX *ctx); /* Can be null */ | ||
| 120 | int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, | ||
| 121 | const BIGNUM *m, BN_CTX *ctx, | ||
| 122 | BN_MONT_CTX *m_ctx); /* Can be null */ | ||
| 123 | int (*init)(RSA *rsa); /* called at new */ | ||
| 124 | int (*finish)(RSA *rsa); /* called at free */ | ||
| 125 | int flags; /* RSA_METHOD_FLAG_* things */ | ||
| 126 | char *app_data; /* may be needed! */ | ||
| 127 | /* New sign and verify functions: some libraries don't allow arbitrary data | ||
| 128 | * to be signed/verified: this allows them to be used. Note: for this to work | ||
| 129 | * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used | ||
| 130 | * RSA_sign(), RSA_verify() should be used instead. Note: for backwards | ||
| 131 | * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER | ||
| 132 | * option is set in 'flags'. | ||
| 133 | */ | ||
| 134 | int (*rsa_sign)(int type, | ||
| 135 | const unsigned char *m, unsigned int m_length, | ||
| 136 | unsigned char *sigret, unsigned int *siglen, const RSA *rsa); | ||
| 137 | int (*rsa_verify)(int dtype, | ||
| 138 | const unsigned char *m, unsigned int m_length, | ||
| 139 | unsigned char *sigbuf, unsigned int siglen, const RSA *rsa); | ||
| 140 | /* If this callback is NULL, the builtin software RSA key-gen will be used. This | ||
| 141 | * is for behavioural compatibility whilst the code gets rewired, but one day | ||
| 142 | * it would be nice to assume there are no such things as "builtin software" | ||
| 143 | * implementations. */ | ||
| 144 | int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb); | ||
| 145 | }; | ||
| 146 | |||
| 147 | struct rsa_st | ||
| 148 | { | ||
| 149 | /* The first parameter is used to pickup errors where | ||
| 150 | * this is passed instead of aEVP_PKEY, it is set to 0 */ | ||
| 151 | int pad; | ||
| 152 | long version; | ||
| 153 | const RSA_METHOD *meth; | ||
| 154 | /* functional reference if 'meth' is ENGINE-provided */ | ||
| 155 | ENGINE *engine; | ||
| 156 | BIGNUM *n; | ||
| 157 | BIGNUM *e; | ||
| 158 | BIGNUM *d; | ||
| 159 | BIGNUM *p; | ||
| 160 | BIGNUM *q; | ||
| 161 | BIGNUM *dmp1; | ||
| 162 | BIGNUM *dmq1; | ||
| 163 | BIGNUM *iqmp; | ||
| 164 | /* be careful using this if the RSA structure is shared */ | ||
| 165 | CRYPTO_EX_DATA ex_data; | ||
| 166 | int references; | ||
| 167 | int flags; | ||
| 168 | |||
| 169 | /* Used to cache montgomery values */ | ||
| 170 | BN_MONT_CTX *_method_mod_n; | ||
| 171 | BN_MONT_CTX *_method_mod_p; | ||
| 172 | BN_MONT_CTX *_method_mod_q; | ||
| 173 | |||
| 174 | /* all BIGNUM values are actually in the following data, if it is not | ||
| 175 | * NULL */ | ||
| 176 | char *bignum_data; | ||
| 177 | BN_BLINDING *blinding; | ||
| 178 | BN_BLINDING *mt_blinding; | ||
| 179 | }; | ||
| 180 | |||
| 181 | #ifndef OPENSSL_RSA_MAX_MODULUS_BITS | ||
| 182 | # define OPENSSL_RSA_MAX_MODULUS_BITS 16384 | ||
| 183 | #endif | ||
| 184 | |||
| 185 | #define OPENSSL_RSA_FIPS_MIN_MODULUS_BITS 1024 | ||
| 186 | |||
| 187 | #ifndef OPENSSL_RSA_SMALL_MODULUS_BITS | ||
| 188 | # define OPENSSL_RSA_SMALL_MODULUS_BITS 3072 | ||
| 189 | #endif | ||
| 190 | #ifndef OPENSSL_RSA_MAX_PUBEXP_BITS | ||
| 191 | # define OPENSSL_RSA_MAX_PUBEXP_BITS 64 /* exponent limit enforced for "large" modulus only */ | ||
| 192 | #endif | ||
| 193 | |||
| 194 | #define RSA_3 0x3L | ||
| 195 | #define RSA_F4 0x10001L | ||
| 196 | |||
| 197 | #define RSA_METHOD_FLAG_NO_CHECK 0x0001 /* don't check pub/private match */ | ||
| 198 | |||
| 199 | #define RSA_FLAG_CACHE_PUBLIC 0x0002 | ||
| 200 | #define RSA_FLAG_CACHE_PRIVATE 0x0004 | ||
| 201 | #define RSA_FLAG_BLINDING 0x0008 | ||
| 202 | #define RSA_FLAG_THREAD_SAFE 0x0010 | ||
| 203 | /* This flag means the private key operations will be handled by rsa_mod_exp | ||
| 204 | * and that they do not depend on the private key components being present: | ||
| 205 | * for example a key stored in external hardware. Without this flag bn_mod_exp | ||
| 206 | * gets called when private key components are absent. | ||
| 207 | */ | ||
| 208 | #define RSA_FLAG_EXT_PKEY 0x0020 | ||
| 209 | |||
| 210 | /* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions. | ||
| 211 | */ | ||
| 212 | #define RSA_FLAG_SIGN_VER 0x0040 | ||
| 213 | |||
| 214 | #define RSA_FLAG_NO_BLINDING 0x0080 /* new with 0.9.6j and 0.9.7b; the built-in | ||
| 215 | * RSA implementation now uses blinding by | ||
| 216 | * default (ignoring RSA_FLAG_BLINDING), | ||
| 217 | * but other engines might not need it | ||
| 218 | */ | ||
| 219 | #define RSA_FLAG_NO_CONSTTIME 0x0100 /* new with 0.9.8f; the built-in RSA | ||
| 220 | * implementation now uses constant time | ||
| 221 | * operations by default in private key operations, | ||
| 222 | * e.g., constant time modular exponentiation, | ||
| 223 | * modular inverse without leaking branches, | ||
| 224 | * division without leaking branches. This | ||
| 225 | * flag disables these constant time | ||
| 226 | * operations and results in faster RSA | ||
| 227 | * private key operations. | ||
| 228 | */ | ||
| 229 | #ifndef OPENSSL_NO_DEPRECATED | ||
| 230 | #define RSA_FLAG_NO_EXP_CONSTTIME RSA_FLAG_NO_CONSTTIME /* deprecated name for the flag*/ | ||
| 231 | /* new with 0.9.7h; the built-in RSA | ||
| 232 | * implementation now uses constant time | ||
| 233 | * modular exponentiation for secret exponents | ||
| 234 | * by default. This flag causes the | ||
| 235 | * faster variable sliding window method to | ||
| 236 | * be used for all exponents. | ||
| 237 | */ | ||
| 238 | #endif | ||
| 239 | |||
| 240 | |||
| 241 | #define RSA_PKCS1_PADDING 1 | ||
| 242 | #define RSA_SSLV23_PADDING 2 | ||
| 243 | #define RSA_NO_PADDING 3 | ||
| 244 | #define RSA_PKCS1_OAEP_PADDING 4 | ||
| 245 | #define RSA_X931_PADDING 5 | ||
| 246 | |||
| 247 | #define RSA_PKCS1_PADDING_SIZE 11 | ||
| 248 | |||
| 249 | #define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg) | ||
| 250 | #define RSA_get_app_data(s) RSA_get_ex_data(s,0) | ||
| 251 | |||
| 252 | RSA * RSA_new(void); | ||
| 253 | RSA * RSA_new_method(ENGINE *engine); | ||
| 254 | int RSA_size(const RSA *); | ||
| 255 | |||
| 256 | /* Deprecated version */ | ||
| 257 | #ifndef OPENSSL_NO_DEPRECATED | ||
| 258 | RSA * RSA_generate_key(int bits, unsigned long e,void | ||
| 259 | (*callback)(int,int,void *),void *cb_arg); | ||
| 260 | #endif /* !defined(OPENSSL_NO_DEPRECATED) */ | ||
| 261 | |||
| 262 | /* New version */ | ||
| 263 | int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb); | ||
| 264 | int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1, BIGNUM *q2, | ||
| 265 | const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *Xp, | ||
| 266 | const BIGNUM *Xq1, const BIGNUM *Xq2, const BIGNUM *Xq, | ||
| 267 | const BIGNUM *e, BN_GENCB *cb); | ||
| 268 | int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e, BN_GENCB *cb); | ||
| 269 | |||
| 270 | int RSA_check_key(const RSA *); | ||
| 271 | /* next 4 return -1 on error */ | ||
| 272 | int RSA_public_encrypt(int flen, const unsigned char *from, | ||
| 273 | unsigned char *to, RSA *rsa,int padding); | ||
| 274 | int RSA_private_encrypt(int flen, const unsigned char *from, | ||
| 275 | unsigned char *to, RSA *rsa,int padding); | ||
| 276 | int RSA_public_decrypt(int flen, const unsigned char *from, | ||
| 277 | unsigned char *to, RSA *rsa,int padding); | ||
| 278 | int RSA_private_decrypt(int flen, const unsigned char *from, | ||
| 279 | unsigned char *to, RSA *rsa,int padding); | ||
| 280 | void RSA_free (RSA *r); | ||
| 281 | /* "up" the RSA object's reference count */ | ||
| 282 | int RSA_up_ref(RSA *r); | ||
| 283 | |||
| 284 | int RSA_flags(const RSA *r); | ||
| 285 | |||
| 286 | #ifdef OPENSSL_FIPS | ||
| 287 | RSA *FIPS_rsa_new(void); | ||
| 288 | void FIPS_rsa_free(RSA *r); | ||
| 289 | #endif | ||
| 290 | |||
| 291 | void RSA_set_default_method(const RSA_METHOD *meth); | ||
| 292 | const RSA_METHOD *RSA_get_default_method(void); | ||
| 293 | const RSA_METHOD *RSA_get_method(const RSA *rsa); | ||
| 294 | int RSA_set_method(RSA *rsa, const RSA_METHOD *meth); | ||
| 295 | |||
| 296 | /* This function needs the memory locking malloc callbacks to be installed */ | ||
| 297 | int RSA_memory_lock(RSA *r); | ||
| 298 | |||
| 299 | /* these are the actual SSLeay RSA functions */ | ||
| 300 | const RSA_METHOD *RSA_PKCS1_SSLeay(void); | ||
| 301 | |||
| 302 | const RSA_METHOD *RSA_null_method(void); | ||
| 303 | |||
| 304 | DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPublicKey) | ||
| 305 | DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPrivateKey) | ||
| 306 | |||
| 307 | #ifndef OPENSSL_NO_FP_API | ||
| 308 | int RSA_print_fp(FILE *fp, const RSA *r,int offset); | ||
| 309 | #endif | ||
| 310 | |||
| 311 | #ifndef OPENSSL_NO_BIO | ||
| 312 | int RSA_print(BIO *bp, const RSA *r,int offset); | ||
| 313 | #endif | ||
| 314 | |||
| 315 | #ifndef OPENSSL_NO_RC4 | ||
| 316 | int i2d_RSA_NET(const RSA *a, unsigned char **pp, | ||
| 317 | int (*cb)(char *buf, int len, const char *prompt, int verify), | ||
| 318 | int sgckey); | ||
| 319 | RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length, | ||
| 320 | int (*cb)(char *buf, int len, const char *prompt, int verify), | ||
| 321 | int sgckey); | ||
| 322 | |||
| 323 | int i2d_Netscape_RSA(const RSA *a, unsigned char **pp, | ||
| 324 | int (*cb)(char *buf, int len, const char *prompt, | ||
| 325 | int verify)); | ||
| 326 | RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, | ||
| 327 | int (*cb)(char *buf, int len, const char *prompt, | ||
| 328 | int verify)); | ||
| 329 | #endif | ||
| 330 | |||
| 331 | /* The following 2 functions sign and verify a X509_SIG ASN1 object | ||
| 332 | * inside PKCS#1 padded RSA encryption */ | ||
| 333 | int RSA_sign(int type, const unsigned char *m, unsigned int m_length, | ||
| 334 | unsigned char *sigret, unsigned int *siglen, RSA *rsa); | ||
| 335 | int RSA_verify(int type, const unsigned char *m, unsigned int m_length, | ||
| 336 | unsigned char *sigbuf, unsigned int siglen, RSA *rsa); | ||
| 337 | |||
| 338 | /* The following 2 function sign and verify a ASN1_OCTET_STRING | ||
| 339 | * object inside PKCS#1 padded RSA encryption */ | ||
| 340 | int RSA_sign_ASN1_OCTET_STRING(int type, | ||
| 341 | const unsigned char *m, unsigned int m_length, | ||
| 342 | unsigned char *sigret, unsigned int *siglen, RSA *rsa); | ||
| 343 | int RSA_verify_ASN1_OCTET_STRING(int type, | ||
| 344 | const unsigned char *m, unsigned int m_length, | ||
| 345 | unsigned char *sigbuf, unsigned int siglen, RSA *rsa); | ||
| 346 | |||
| 347 | int RSA_blinding_on(RSA *rsa, BN_CTX *ctx); | ||
| 348 | void RSA_blinding_off(RSA *rsa); | ||
| 349 | BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *ctx); | ||
| 350 | |||
| 351 | int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen, | ||
| 352 | const unsigned char *f,int fl); | ||
| 353 | int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen, | ||
| 354 | const unsigned char *f,int fl,int rsa_len); | ||
| 355 | int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen, | ||
| 356 | const unsigned char *f,int fl); | ||
| 357 | int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen, | ||
| 358 | const unsigned char *f,int fl,int rsa_len); | ||
| 359 | int PKCS1_MGF1(unsigned char *mask, long len, | ||
| 360 | const unsigned char *seed, long seedlen, const EVP_MD *dgst); | ||
| 361 | int RSA_padding_add_PKCS1_OAEP(unsigned char *to,int tlen, | ||
| 362 | const unsigned char *f,int fl, | ||
| 363 | const unsigned char *p,int pl); | ||
| 364 | int RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen, | ||
| 365 | const unsigned char *f,int fl,int rsa_len, | ||
| 366 | const unsigned char *p,int pl); | ||
| 367 | int RSA_padding_add_SSLv23(unsigned char *to,int tlen, | ||
| 368 | const unsigned char *f,int fl); | ||
| 369 | int RSA_padding_check_SSLv23(unsigned char *to,int tlen, | ||
| 370 | const unsigned char *f,int fl,int rsa_len); | ||
| 371 | int RSA_padding_add_none(unsigned char *to,int tlen, | ||
| 372 | const unsigned char *f,int fl); | ||
| 373 | int RSA_padding_check_none(unsigned char *to,int tlen, | ||
| 374 | const unsigned char *f,int fl,int rsa_len); | ||
| 375 | int RSA_padding_add_X931(unsigned char *to,int tlen, | ||
| 376 | const unsigned char *f,int fl); | ||
| 377 | int RSA_padding_check_X931(unsigned char *to,int tlen, | ||
| 378 | const unsigned char *f,int fl,int rsa_len); | ||
| 379 | int RSA_X931_hash_id(int nid); | ||
| 380 | |||
| 381 | int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, | ||
| 382 | const EVP_MD *Hash, const unsigned char *EM, int sLen); | ||
| 383 | int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM, | ||
| 384 | const unsigned char *mHash, | ||
| 385 | const EVP_MD *Hash, int sLen); | ||
| 386 | |||
| 387 | int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 388 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func); | ||
| 389 | int RSA_set_ex_data(RSA *r,int idx,void *arg); | ||
| 390 | void *RSA_get_ex_data(const RSA *r, int idx); | ||
| 391 | |||
| 392 | RSA *RSAPublicKey_dup(RSA *rsa); | ||
| 393 | RSA *RSAPrivateKey_dup(RSA *rsa); | ||
| 394 | |||
| 395 | /* BEGIN ERROR CODES */ | ||
| 396 | /* The following lines are auto generated by the script mkerr.pl. Any changes | ||
| 397 | * made after this point may be overwritten when the script is next run. | ||
| 398 | */ | ||
| 399 | void ERR_load_RSA_strings(void); | ||
| 400 | |||
| 401 | /* Error codes for the RSA functions. */ | ||
| 402 | |||
| 403 | /* Function codes. */ | ||
| 404 | #define RSA_F_FIPS_RSA_SIGN 140 | ||
| 405 | #define RSA_F_FIPS_RSA_VERIFY 141 | ||
| 406 | #define RSA_F_MEMORY_LOCK 100 | ||
| 407 | #define RSA_F_RSA_BUILTIN_KEYGEN 129 | ||
| 408 | #define RSA_F_RSA_CHECK_KEY 123 | ||
| 409 | #define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101 | ||
| 410 | #define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102 | ||
| 411 | #define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103 | ||
| 412 | #define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104 | ||
| 413 | #define RSA_F_RSA_GENERATE_KEY 105 | ||
| 414 | #define RSA_F_RSA_MEMORY_LOCK 130 | ||
| 415 | #define RSA_F_RSA_NEW_METHOD 106 | ||
| 416 | #define RSA_F_RSA_NULL 124 | ||
| 417 | #define RSA_F_RSA_NULL_MOD_EXP 131 | ||
| 418 | #define RSA_F_RSA_NULL_PRIVATE_DECRYPT 132 | ||
| 419 | #define RSA_F_RSA_NULL_PRIVATE_ENCRYPT 133 | ||
| 420 | #define RSA_F_RSA_NULL_PUBLIC_DECRYPT 134 | ||
| 421 | #define RSA_F_RSA_NULL_PUBLIC_ENCRYPT 135 | ||
| 422 | #define RSA_F_RSA_PADDING_ADD_NONE 107 | ||
| 423 | #define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121 | ||
| 424 | #define RSA_F_RSA_PADDING_ADD_PKCS1_PSS 125 | ||
| 425 | #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108 | ||
| 426 | #define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109 | ||
| 427 | #define RSA_F_RSA_PADDING_ADD_SSLV23 110 | ||
| 428 | #define RSA_F_RSA_PADDING_ADD_X931 127 | ||
| 429 | #define RSA_F_RSA_PADDING_CHECK_NONE 111 | ||
| 430 | #define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122 | ||
| 431 | #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112 | ||
| 432 | #define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113 | ||
| 433 | #define RSA_F_RSA_PADDING_CHECK_SSLV23 114 | ||
| 434 | #define RSA_F_RSA_PADDING_CHECK_X931 128 | ||
| 435 | #define RSA_F_RSA_PRINT 115 | ||
| 436 | #define RSA_F_RSA_PRINT_FP 116 | ||
| 437 | #define RSA_F_RSA_PRIVATE_ENCRYPT 137 | ||
| 438 | #define RSA_F_RSA_PUBLIC_DECRYPT 138 | ||
| 439 | #define RSA_F_RSA_SETUP_BLINDING 136 | ||
| 440 | #define RSA_F_RSA_SET_DEFAULT_METHOD 139 | ||
| 441 | #define RSA_F_RSA_SET_METHOD 142 | ||
| 442 | #define RSA_F_RSA_SIGN 117 | ||
| 443 | #define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118 | ||
| 444 | #define RSA_F_RSA_VERIFY 119 | ||
| 445 | #define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120 | ||
| 446 | #define RSA_F_RSA_VERIFY_PKCS1_PSS 126 | ||
| 447 | |||
| 448 | /* Reason codes. */ | ||
| 449 | #define RSA_R_ALGORITHM_MISMATCH 100 | ||
| 450 | #define RSA_R_BAD_E_VALUE 101 | ||
| 451 | #define RSA_R_BAD_FIXED_HEADER_DECRYPT 102 | ||
| 452 | #define RSA_R_BAD_PAD_BYTE_COUNT 103 | ||
| 453 | #define RSA_R_BAD_SIGNATURE 104 | ||
| 454 | #define RSA_R_BLOCK_TYPE_IS_NOT_01 106 | ||
| 455 | #define RSA_R_BLOCK_TYPE_IS_NOT_02 107 | ||
| 456 | #define RSA_R_DATA_GREATER_THAN_MOD_LEN 108 | ||
| 457 | #define RSA_R_DATA_TOO_LARGE 109 | ||
| 458 | #define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110 | ||
| 459 | #define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132 | ||
| 460 | #define RSA_R_DATA_TOO_SMALL 111 | ||
| 461 | #define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122 | ||
| 462 | #define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112 | ||
| 463 | #define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124 | ||
| 464 | #define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125 | ||
| 465 | #define RSA_R_D_E_NOT_CONGRUENT_TO_1 123 | ||
| 466 | #define RSA_R_FIRST_OCTET_INVALID 133 | ||
| 467 | #define RSA_R_INVALID_HEADER 137 | ||
| 468 | #define RSA_R_INVALID_MESSAGE_LENGTH 131 | ||
| 469 | #define RSA_R_INVALID_PADDING 138 | ||
| 470 | #define RSA_R_INVALID_TRAILER 139 | ||
| 471 | #define RSA_R_IQMP_NOT_INVERSE_OF_Q 126 | ||
| 472 | #define RSA_R_KEY_SIZE_TOO_SMALL 120 | ||
| 473 | #define RSA_R_LAST_OCTET_INVALID 134 | ||
| 474 | #define RSA_R_MODULUS_TOO_LARGE 105 | ||
| 475 | #define RSA_R_NON_FIPS_METHOD 141 | ||
| 476 | #define RSA_R_NO_PUBLIC_EXPONENT 140 | ||
| 477 | #define RSA_R_NULL_BEFORE_BLOCK_MISSING 113 | ||
| 478 | #define RSA_R_N_DOES_NOT_EQUAL_P_Q 127 | ||
| 479 | #define RSA_R_OAEP_DECODING_ERROR 121 | ||
| 480 | #define RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE 142 | ||
| 481 | #define RSA_R_PADDING_CHECK_FAILED 114 | ||
| 482 | #define RSA_R_P_NOT_PRIME 128 | ||
| 483 | #define RSA_R_Q_NOT_PRIME 129 | ||
| 484 | #define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130 | ||
| 485 | #define RSA_R_SLEN_CHECK_FAILED 136 | ||
| 486 | #define RSA_R_SLEN_RECOVERY_FAILED 135 | ||
| 487 | #define RSA_R_SSLV3_ROLLBACK_ATTACK 115 | ||
| 488 | #define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116 | ||
| 489 | #define RSA_R_UNKNOWN_ALGORITHM_TYPE 117 | ||
| 490 | #define RSA_R_UNKNOWN_PADDING_TYPE 118 | ||
| 491 | #define RSA_R_WRONG_SIGNATURE_LENGTH 119 | ||
| 492 | |||
| 493 | #ifdef __cplusplus | ||
| 494 | } | ||
| 495 | #endif | ||
| 496 | #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..6e8a803e81 --- /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 (steve@openssl.org) 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 | (I2D_OF(void)) i2d_RSAPrivateKey, | ||
| 67 | (D2I_OF(void)) d2i_RSAPrivateKey, | ||
| 68 | (void *(*)(void)) RSA_new, | ||
| 69 | (void (*)(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..9d848db8c6 --- /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_ex(key->p, BN_prime_checks, 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_ex(key->q, BN_prime_checks, 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_depr.c b/src/lib/libcrypto/rsa/rsa_depr.c new file mode 100644 index 0000000000..a859ded987 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_depr.c | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | /* crypto/rsa/rsa_depr.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1998-2002 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 | /* NB: This file contains deprecated functions (compatibility wrappers to the | ||
| 57 | * "new" versions). */ | ||
| 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 | #ifdef OPENSSL_NO_DEPRECATED | ||
| 66 | |||
| 67 | static void *dummy=&dummy; | ||
| 68 | |||
| 69 | #else | ||
| 70 | |||
| 71 | RSA *RSA_generate_key(int bits, unsigned long e_value, | ||
| 72 | void (*callback)(int,int,void *), void *cb_arg) | ||
| 73 | { | ||
| 74 | BN_GENCB cb; | ||
| 75 | int i; | ||
| 76 | RSA *rsa = RSA_new(); | ||
| 77 | BIGNUM *e = BN_new(); | ||
| 78 | |||
| 79 | if(!rsa || !e) goto err; | ||
| 80 | |||
| 81 | /* The problem is when building with 8, 16, or 32 BN_ULONG, | ||
| 82 | * unsigned long can be larger */ | ||
| 83 | for (i=0; i<(int)sizeof(unsigned long)*8; i++) | ||
| 84 | { | ||
| 85 | if (e_value & (1UL<<i)) | ||
| 86 | if (BN_set_bit(e,i) == 0) | ||
| 87 | goto err; | ||
| 88 | } | ||
| 89 | |||
| 90 | BN_GENCB_set_old(&cb, callback, cb_arg); | ||
| 91 | |||
| 92 | if(RSA_generate_key_ex(rsa, bits, e, &cb)) { | ||
| 93 | BN_free(e); | ||
| 94 | return rsa; | ||
| 95 | } | ||
| 96 | err: | ||
| 97 | if(e) BN_free(e); | ||
| 98 | if(rsa) RSA_free(rsa); | ||
| 99 | return 0; | ||
| 100 | } | ||
| 101 | #endif | ||
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c new file mode 100644 index 0000000000..04ec789ee9 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_eay.c | |||
| @@ -0,0 +1,913 @@ | |||
| 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-2006 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, BN_CTX *ctx); | ||
| 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 | NULL /* rsa_keygen */ | ||
| 146 | }; | ||
| 147 | |||
| 148 | const RSA_METHOD *RSA_PKCS1_SSLeay(void) | ||
| 149 | { | ||
| 150 | return(&rsa_pkcs1_eay_meth); | ||
| 151 | } | ||
| 152 | |||
| 153 | static int RSA_eay_public_encrypt(int flen, const unsigned char *from, | ||
| 154 | unsigned char *to, RSA *rsa, int padding) | ||
| 155 | { | ||
| 156 | BIGNUM *f,*ret; | ||
| 157 | int i,j,k,num=0,r= -1; | ||
| 158 | unsigned char *buf=NULL; | ||
| 159 | BN_CTX *ctx=NULL; | ||
| 160 | |||
| 161 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) | ||
| 162 | { | ||
| 163 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE); | ||
| 164 | return -1; | ||
| 165 | } | ||
| 166 | |||
| 167 | if (BN_ucmp(rsa->n, rsa->e) <= 0) | ||
| 168 | { | ||
| 169 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); | ||
| 170 | return -1; | ||
| 171 | } | ||
| 172 | |||
| 173 | /* for large moduli, enforce exponent limit */ | ||
| 174 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) | ||
| 175 | { | ||
| 176 | if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) | ||
| 177 | { | ||
| 178 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); | ||
| 179 | return -1; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 184 | BN_CTX_start(ctx); | ||
| 185 | f = BN_CTX_get(ctx); | ||
| 186 | ret = BN_CTX_get(ctx); | ||
| 187 | num=BN_num_bytes(rsa->n); | ||
| 188 | buf = OPENSSL_malloc(num); | ||
| 189 | if (!f || !ret || !buf) | ||
| 190 | { | ||
| 191 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 192 | goto err; | ||
| 193 | } | ||
| 194 | |||
| 195 | switch (padding) | ||
| 196 | { | ||
| 197 | case RSA_PKCS1_PADDING: | ||
| 198 | i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen); | ||
| 199 | break; | ||
| 200 | #ifndef OPENSSL_NO_SHA | ||
| 201 | case RSA_PKCS1_OAEP_PADDING: | ||
| 202 | i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0); | ||
| 203 | break; | ||
| 204 | #endif | ||
| 205 | case RSA_SSLV23_PADDING: | ||
| 206 | i=RSA_padding_add_SSLv23(buf,num,from,flen); | ||
| 207 | break; | ||
| 208 | case RSA_NO_PADDING: | ||
| 209 | i=RSA_padding_add_none(buf,num,from,flen); | ||
| 210 | break; | ||
| 211 | default: | ||
| 212 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 213 | goto err; | ||
| 214 | } | ||
| 215 | if (i <= 0) goto err; | ||
| 216 | |||
| 217 | if (BN_bin2bn(buf,num,f) == NULL) goto err; | ||
| 218 | |||
| 219 | if (BN_ucmp(f, rsa->n) >= 0) | ||
| 220 | { | ||
| 221 | /* usually the padding functions would catch this */ | ||
| 222 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 223 | goto err; | ||
| 224 | } | ||
| 225 | |||
| 226 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) | ||
| 227 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | ||
| 228 | goto err; | ||
| 229 | |||
| 230 | if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, | ||
| 231 | rsa->_method_mod_n)) goto err; | ||
| 232 | |||
| 233 | /* put in leading 0 bytes if the number is less than the | ||
| 234 | * length of the modulus */ | ||
| 235 | j=BN_num_bytes(ret); | ||
| 236 | i=BN_bn2bin(ret,&(to[num-j])); | ||
| 237 | for (k=0; k<(num-i); k++) | ||
| 238 | to[k]=0; | ||
| 239 | |||
| 240 | r=num; | ||
| 241 | err: | ||
| 242 | if (ctx != NULL) | ||
| 243 | { | ||
| 244 | BN_CTX_end(ctx); | ||
| 245 | BN_CTX_free(ctx); | ||
| 246 | } | ||
| 247 | if (buf != NULL) | ||
| 248 | { | ||
| 249 | OPENSSL_cleanse(buf,num); | ||
| 250 | OPENSSL_free(buf); | ||
| 251 | } | ||
| 252 | return(r); | ||
| 253 | } | ||
| 254 | |||
| 255 | static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx) | ||
| 256 | { | ||
| 257 | BN_BLINDING *ret; | ||
| 258 | int got_write_lock = 0; | ||
| 259 | |||
| 260 | CRYPTO_r_lock(CRYPTO_LOCK_RSA); | ||
| 261 | |||
| 262 | if (rsa->blinding == NULL) | ||
| 263 | { | ||
| 264 | CRYPTO_r_unlock(CRYPTO_LOCK_RSA); | ||
| 265 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
| 266 | got_write_lock = 1; | ||
| 267 | |||
| 268 | if (rsa->blinding == NULL) | ||
| 269 | rsa->blinding = RSA_setup_blinding(rsa, ctx); | ||
| 270 | } | ||
| 271 | |||
| 272 | ret = rsa->blinding; | ||
| 273 | if (ret == NULL) | ||
| 274 | goto err; | ||
| 275 | |||
| 276 | if (BN_BLINDING_get_thread_id(ret) == CRYPTO_thread_id()) | ||
| 277 | { | ||
| 278 | /* rsa->blinding is ours! */ | ||
| 279 | |||
| 280 | *local = 1; | ||
| 281 | } | ||
| 282 | else | ||
| 283 | { | ||
| 284 | /* resort to rsa->mt_blinding instead */ | ||
| 285 | |||
| 286 | *local = 0; /* instructs rsa_blinding_convert(), rsa_blinding_invert() | ||
| 287 | * that the BN_BLINDING is shared, meaning that accesses | ||
| 288 | * require locks, and that the blinding factor must be | ||
| 289 | * stored outside the BN_BLINDING | ||
| 290 | */ | ||
| 291 | |||
| 292 | if (rsa->mt_blinding == NULL) | ||
| 293 | { | ||
| 294 | if (!got_write_lock) | ||
| 295 | { | ||
| 296 | CRYPTO_r_unlock(CRYPTO_LOCK_RSA); | ||
| 297 | CRYPTO_w_lock(CRYPTO_LOCK_RSA); | ||
| 298 | got_write_lock = 1; | ||
| 299 | } | ||
| 300 | |||
| 301 | if (rsa->mt_blinding == NULL) | ||
| 302 | rsa->mt_blinding = RSA_setup_blinding(rsa, ctx); | ||
| 303 | } | ||
| 304 | ret = rsa->mt_blinding; | ||
| 305 | } | ||
| 306 | |||
| 307 | err: | ||
| 308 | if (got_write_lock) | ||
| 309 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA); | ||
| 310 | else | ||
| 311 | CRYPTO_r_unlock(CRYPTO_LOCK_RSA); | ||
| 312 | return ret; | ||
| 313 | } | ||
| 314 | |||
| 315 | static int rsa_blinding_convert(BN_BLINDING *b, int local, BIGNUM *f, | ||
| 316 | BIGNUM *r, BN_CTX *ctx) | ||
| 317 | { | ||
| 318 | if (local) | ||
| 319 | return BN_BLINDING_convert_ex(f, NULL, b, ctx); | ||
| 320 | else | ||
| 321 | { | ||
| 322 | int ret; | ||
| 323 | CRYPTO_r_lock(CRYPTO_LOCK_RSA_BLINDING); | ||
| 324 | ret = BN_BLINDING_convert_ex(f, r, b, ctx); | ||
| 325 | CRYPTO_r_unlock(CRYPTO_LOCK_RSA_BLINDING); | ||
| 326 | return ret; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | static int rsa_blinding_invert(BN_BLINDING *b, int local, BIGNUM *f, | ||
| 331 | BIGNUM *r, BN_CTX *ctx) | ||
| 332 | { | ||
| 333 | if (local) | ||
| 334 | return BN_BLINDING_invert_ex(f, NULL, b, ctx); | ||
| 335 | else | ||
| 336 | { | ||
| 337 | int ret; | ||
| 338 | CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING); | ||
| 339 | ret = BN_BLINDING_invert_ex(f, r, b, ctx); | ||
| 340 | CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING); | ||
| 341 | return ret; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | /* signing */ | ||
| 346 | static int RSA_eay_private_encrypt(int flen, const unsigned char *from, | ||
| 347 | unsigned char *to, RSA *rsa, int padding) | ||
| 348 | { | ||
| 349 | BIGNUM *f, *ret, *br, *res; | ||
| 350 | int i,j,k,num=0,r= -1; | ||
| 351 | unsigned char *buf=NULL; | ||
| 352 | BN_CTX *ctx=NULL; | ||
| 353 | int local_blinding = 0; | ||
| 354 | BN_BLINDING *blinding = NULL; | ||
| 355 | |||
| 356 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) | ||
| 357 | { | ||
| 358 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE); | ||
| 359 | return -1; | ||
| 360 | } | ||
| 361 | |||
| 362 | if (BN_ucmp(rsa->n, rsa->e) <= 0) | ||
| 363 | { | ||
| 364 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); | ||
| 365 | return -1; | ||
| 366 | } | ||
| 367 | |||
| 368 | /* for large moduli, enforce exponent limit */ | ||
| 369 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) | ||
| 370 | { | ||
| 371 | if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) | ||
| 372 | { | ||
| 373 | RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); | ||
| 374 | return -1; | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | if ((ctx=BN_CTX_new()) == NULL) goto err; | ||
| 379 | BN_CTX_start(ctx); | ||
| 380 | f = BN_CTX_get(ctx); | ||
| 381 | br = BN_CTX_get(ctx); | ||
| 382 | ret = BN_CTX_get(ctx); | ||
| 383 | num = BN_num_bytes(rsa->n); | ||
| 384 | buf = OPENSSL_malloc(num); | ||
| 385 | if(!f || !ret || !buf) | ||
| 386 | { | ||
| 387 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE); | ||
| 388 | goto err; | ||
| 389 | } | ||
| 390 | |||
| 391 | switch (padding) | ||
| 392 | { | ||
| 393 | case RSA_PKCS1_PADDING: | ||
| 394 | i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen); | ||
| 395 | break; | ||
| 396 | case RSA_X931_PADDING: | ||
| 397 | i=RSA_padding_add_X931(buf,num,from,flen); | ||
| 398 | break; | ||
| 399 | case RSA_NO_PADDING: | ||
| 400 | i=RSA_padding_add_none(buf,num,from,flen); | ||
| 401 | break; | ||
| 402 | case RSA_SSLV23_PADDING: | ||
| 403 | default: | ||
| 404 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 405 | goto err; | ||
| 406 | } | ||
| 407 | if (i <= 0) goto err; | ||
| 408 | |||
| 409 | if (BN_bin2bn(buf,num,f) == NULL) goto err; | ||
| 410 | |||
| 411 | if (BN_ucmp(f, rsa->n) >= 0) | ||
| 412 | { | ||
| 413 | /* usually the padding functions would catch this */ | ||
| 414 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 415 | goto err; | ||
| 416 | } | ||
| 417 | |||
| 418 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) | ||
| 419 | { | ||
| 420 | blinding = rsa_get_blinding(rsa, &local_blinding, ctx); | ||
| 421 | if (blinding == NULL) | ||
| 422 | { | ||
| 423 | RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); | ||
| 424 | goto err; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | if (blinding != NULL) | ||
| 429 | if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx)) | ||
| 430 | goto err; | ||
| 431 | |||
| 432 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || | ||
| 433 | ((rsa->p != NULL) && | ||
| 434 | (rsa->q != NULL) && | ||
| 435 | (rsa->dmp1 != NULL) && | ||
| 436 | (rsa->dmq1 != NULL) && | ||
| 437 | (rsa->iqmp != NULL)) ) | ||
| 438 | { | ||
| 439 | if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err; | ||
| 440 | } | ||
| 441 | else | ||
| 442 | { | ||
| 443 | BIGNUM local_d; | ||
| 444 | BIGNUM *d = NULL; | ||
| 445 | |||
| 446 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 447 | { | ||
| 448 | BN_init(&local_d); | ||
| 449 | d = &local_d; | ||
| 450 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); | ||
| 451 | } | ||
| 452 | else | ||
| 453 | d= rsa->d; | ||
| 454 | |||
| 455 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) | ||
| 456 | if(!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | ||
| 457 | goto err; | ||
| 458 | |||
| 459 | if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, | ||
| 460 | rsa->_method_mod_n)) goto err; | ||
| 461 | } | ||
| 462 | |||
| 463 | if (blinding) | ||
| 464 | if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx)) | ||
| 465 | goto err; | ||
| 466 | |||
| 467 | if (padding == RSA_X931_PADDING) | ||
| 468 | { | ||
| 469 | BN_sub(f, rsa->n, ret); | ||
| 470 | if (BN_cmp(ret, f)) | ||
| 471 | res = f; | ||
| 472 | else | ||
| 473 | res = ret; | ||
| 474 | } | ||
| 475 | else | ||
| 476 | res = ret; | ||
| 477 | |||
| 478 | /* put in leading 0 bytes if the number is less than the | ||
| 479 | * length of the modulus */ | ||
| 480 | j=BN_num_bytes(res); | ||
| 481 | i=BN_bn2bin(res,&(to[num-j])); | ||
| 482 | for (k=0; k<(num-i); k++) | ||
| 483 | to[k]=0; | ||
| 484 | |||
| 485 | r=num; | ||
| 486 | err: | ||
| 487 | if (ctx != NULL) | ||
| 488 | { | ||
| 489 | BN_CTX_end(ctx); | ||
| 490 | BN_CTX_free(ctx); | ||
| 491 | } | ||
| 492 | if (buf != NULL) | ||
| 493 | { | ||
| 494 | OPENSSL_cleanse(buf,num); | ||
| 495 | OPENSSL_free(buf); | ||
| 496 | } | ||
| 497 | return(r); | ||
| 498 | } | ||
| 499 | |||
| 500 | static int RSA_eay_private_decrypt(int flen, const unsigned char *from, | ||
| 501 | unsigned char *to, RSA *rsa, int padding) | ||
| 502 | { | ||
| 503 | BIGNUM *f, *ret, *br; | ||
| 504 | int j,num=0,r= -1; | ||
| 505 | unsigned char *p; | ||
| 506 | unsigned char *buf=NULL; | ||
| 507 | BN_CTX *ctx=NULL; | ||
| 508 | int local_blinding = 0; | ||
| 509 | BN_BLINDING *blinding = NULL; | ||
| 510 | |||
| 511 | if((ctx = BN_CTX_new()) == NULL) goto err; | ||
| 512 | BN_CTX_start(ctx); | ||
| 513 | f = BN_CTX_get(ctx); | ||
| 514 | br = BN_CTX_get(ctx); | ||
| 515 | ret = BN_CTX_get(ctx); | ||
| 516 | num = BN_num_bytes(rsa->n); | ||
| 517 | buf = OPENSSL_malloc(num); | ||
| 518 | if(!f || !ret || !buf) | ||
| 519 | { | ||
| 520 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); | ||
| 521 | goto err; | ||
| 522 | } | ||
| 523 | |||
| 524 | /* This check was for equality but PGP does evil things | ||
| 525 | * and chops off the top '0' bytes */ | ||
| 526 | if (flen > num) | ||
| 527 | { | ||
| 528 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | ||
| 529 | goto err; | ||
| 530 | } | ||
| 531 | |||
| 532 | /* make data into a big number */ | ||
| 533 | if (BN_bin2bn(from,(int)flen,f) == NULL) goto err; | ||
| 534 | |||
| 535 | if (BN_ucmp(f, rsa->n) >= 0) | ||
| 536 | { | ||
| 537 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 538 | goto err; | ||
| 539 | } | ||
| 540 | |||
| 541 | if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) | ||
| 542 | { | ||
| 543 | blinding = rsa_get_blinding(rsa, &local_blinding, ctx); | ||
| 544 | if (blinding == NULL) | ||
| 545 | { | ||
| 546 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR); | ||
| 547 | goto err; | ||
| 548 | } | ||
| 549 | } | ||
| 550 | |||
| 551 | if (blinding != NULL) | ||
| 552 | if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx)) | ||
| 553 | goto err; | ||
| 554 | |||
| 555 | /* do the decrypt */ | ||
| 556 | if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || | ||
| 557 | ((rsa->p != NULL) && | ||
| 558 | (rsa->q != NULL) && | ||
| 559 | (rsa->dmp1 != NULL) && | ||
| 560 | (rsa->dmq1 != NULL) && | ||
| 561 | (rsa->iqmp != NULL)) ) | ||
| 562 | { | ||
| 563 | if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err; | ||
| 564 | } | ||
| 565 | else | ||
| 566 | { | ||
| 567 | BIGNUM local_d; | ||
| 568 | BIGNUM *d = NULL; | ||
| 569 | |||
| 570 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 571 | { | ||
| 572 | d = &local_d; | ||
| 573 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); | ||
| 574 | } | ||
| 575 | else | ||
| 576 | d = rsa->d; | ||
| 577 | |||
| 578 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) | ||
| 579 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | ||
| 580 | goto err; | ||
| 581 | if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, | ||
| 582 | rsa->_method_mod_n)) | ||
| 583 | goto err; | ||
| 584 | } | ||
| 585 | |||
| 586 | if (blinding) | ||
| 587 | if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx)) | ||
| 588 | goto err; | ||
| 589 | |||
| 590 | p=buf; | ||
| 591 | j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */ | ||
| 592 | |||
| 593 | switch (padding) | ||
| 594 | { | ||
| 595 | case RSA_PKCS1_PADDING: | ||
| 596 | r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num); | ||
| 597 | break; | ||
| 598 | #ifndef OPENSSL_NO_SHA | ||
| 599 | case RSA_PKCS1_OAEP_PADDING: | ||
| 600 | r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0); | ||
| 601 | break; | ||
| 602 | #endif | ||
| 603 | case RSA_SSLV23_PADDING: | ||
| 604 | r=RSA_padding_check_SSLv23(to,num,buf,j,num); | ||
| 605 | break; | ||
| 606 | case RSA_NO_PADDING: | ||
| 607 | r=RSA_padding_check_none(to,num,buf,j,num); | ||
| 608 | break; | ||
| 609 | default: | ||
| 610 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 611 | goto err; | ||
| 612 | } | ||
| 613 | if (r < 0) | ||
| 614 | RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | ||
| 615 | |||
| 616 | err: | ||
| 617 | if (ctx != NULL) | ||
| 618 | { | ||
| 619 | BN_CTX_end(ctx); | ||
| 620 | BN_CTX_free(ctx); | ||
| 621 | } | ||
| 622 | if (buf != NULL) | ||
| 623 | { | ||
| 624 | OPENSSL_cleanse(buf,num); | ||
| 625 | OPENSSL_free(buf); | ||
| 626 | } | ||
| 627 | return(r); | ||
| 628 | } | ||
| 629 | |||
| 630 | /* signature verification */ | ||
| 631 | static int RSA_eay_public_decrypt(int flen, const unsigned char *from, | ||
| 632 | unsigned char *to, RSA *rsa, int padding) | ||
| 633 | { | ||
| 634 | BIGNUM *f,*ret; | ||
| 635 | int i,num=0,r= -1; | ||
| 636 | unsigned char *p; | ||
| 637 | unsigned char *buf=NULL; | ||
| 638 | BN_CTX *ctx=NULL; | ||
| 639 | |||
| 640 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) | ||
| 641 | { | ||
| 642 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE); | ||
| 643 | return -1; | ||
| 644 | } | ||
| 645 | |||
| 646 | if (BN_ucmp(rsa->n, rsa->e) <= 0) | ||
| 647 | { | ||
| 648 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); | ||
| 649 | return -1; | ||
| 650 | } | ||
| 651 | |||
| 652 | /* for large moduli, enforce exponent limit */ | ||
| 653 | if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) | ||
| 654 | { | ||
| 655 | if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) | ||
| 656 | { | ||
| 657 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); | ||
| 658 | return -1; | ||
| 659 | } | ||
| 660 | } | ||
| 661 | |||
| 662 | if((ctx = BN_CTX_new()) == NULL) goto err; | ||
| 663 | BN_CTX_start(ctx); | ||
| 664 | f = BN_CTX_get(ctx); | ||
| 665 | ret = BN_CTX_get(ctx); | ||
| 666 | num=BN_num_bytes(rsa->n); | ||
| 667 | buf = OPENSSL_malloc(num); | ||
| 668 | if(!f || !ret || !buf) | ||
| 669 | { | ||
| 670 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE); | ||
| 671 | goto err; | ||
| 672 | } | ||
| 673 | |||
| 674 | /* This check was for equality but PGP does evil things | ||
| 675 | * and chops off the top '0' bytes */ | ||
| 676 | if (flen > num) | ||
| 677 | { | ||
| 678 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); | ||
| 679 | goto err; | ||
| 680 | } | ||
| 681 | |||
| 682 | if (BN_bin2bn(from,flen,f) == NULL) goto err; | ||
| 683 | |||
| 684 | if (BN_ucmp(f, rsa->n) >= 0) | ||
| 685 | { | ||
| 686 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); | ||
| 687 | goto err; | ||
| 688 | } | ||
| 689 | |||
| 690 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) | ||
| 691 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | ||
| 692 | goto err; | ||
| 693 | |||
| 694 | if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, | ||
| 695 | rsa->_method_mod_n)) goto err; | ||
| 696 | |||
| 697 | if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12)) | ||
| 698 | BN_sub(ret, rsa->n, ret); | ||
| 699 | |||
| 700 | p=buf; | ||
| 701 | i=BN_bn2bin(ret,p); | ||
| 702 | |||
| 703 | switch (padding) | ||
| 704 | { | ||
| 705 | case RSA_PKCS1_PADDING: | ||
| 706 | r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num); | ||
| 707 | break; | ||
| 708 | case RSA_X931_PADDING: | ||
| 709 | r=RSA_padding_check_X931(to,num,buf,i,num); | ||
| 710 | break; | ||
| 711 | case RSA_NO_PADDING: | ||
| 712 | r=RSA_padding_check_none(to,num,buf,i,num); | ||
| 713 | break; | ||
| 714 | default: | ||
| 715 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); | ||
| 716 | goto err; | ||
| 717 | } | ||
| 718 | if (r < 0) | ||
| 719 | RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED); | ||
| 720 | |||
| 721 | err: | ||
| 722 | if (ctx != NULL) | ||
| 723 | { | ||
| 724 | BN_CTX_end(ctx); | ||
| 725 | BN_CTX_free(ctx); | ||
| 726 | } | ||
| 727 | if (buf != NULL) | ||
| 728 | { | ||
| 729 | OPENSSL_cleanse(buf,num); | ||
| 730 | OPENSSL_free(buf); | ||
| 731 | } | ||
| 732 | return(r); | ||
| 733 | } | ||
| 734 | |||
| 735 | static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) | ||
| 736 | { | ||
| 737 | BIGNUM *r1,*m1,*vrfy; | ||
| 738 | BIGNUM local_dmp1,local_dmq1,local_c,local_r1; | ||
| 739 | BIGNUM *dmp1,*dmq1,*c,*pr1; | ||
| 740 | int ret=0; | ||
| 741 | |||
| 742 | BN_CTX_start(ctx); | ||
| 743 | r1 = BN_CTX_get(ctx); | ||
| 744 | m1 = BN_CTX_get(ctx); | ||
| 745 | vrfy = BN_CTX_get(ctx); | ||
| 746 | |||
| 747 | { | ||
| 748 | BIGNUM local_p, local_q; | ||
| 749 | BIGNUM *p = NULL, *q = NULL; | ||
| 750 | |||
| 751 | /* Make sure BN_mod_inverse in Montgomery intialization uses the | ||
| 752 | * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set) | ||
| 753 | */ | ||
| 754 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 755 | { | ||
| 756 | BN_init(&local_p); | ||
| 757 | p = &local_p; | ||
| 758 | BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME); | ||
| 759 | |||
| 760 | BN_init(&local_q); | ||
| 761 | q = &local_q; | ||
| 762 | BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME); | ||
| 763 | } | ||
| 764 | else | ||
| 765 | { | ||
| 766 | p = rsa->p; | ||
| 767 | q = rsa->q; | ||
| 768 | } | ||
| 769 | |||
| 770 | if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) | ||
| 771 | { | ||
| 772 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx)) | ||
| 773 | goto err; | ||
| 774 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx)) | ||
| 775 | goto err; | ||
| 776 | } | ||
| 777 | } | ||
| 778 | |||
| 779 | if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) | ||
| 780 | if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) | ||
| 781 | goto err; | ||
| 782 | |||
| 783 | /* compute I mod q */ | ||
| 784 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 785 | { | ||
| 786 | c = &local_c; | ||
| 787 | BN_with_flags(c, I, BN_FLG_CONSTTIME); | ||
| 788 | if (!BN_mod(r1,c,rsa->q,ctx)) goto err; | ||
| 789 | } | ||
| 790 | else | ||
| 791 | { | ||
| 792 | if (!BN_mod(r1,I,rsa->q,ctx)) goto err; | ||
| 793 | } | ||
| 794 | |||
| 795 | /* compute r1^dmq1 mod q */ | ||
| 796 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 797 | { | ||
| 798 | dmq1 = &local_dmq1; | ||
| 799 | BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME); | ||
| 800 | } | ||
| 801 | else | ||
| 802 | dmq1 = rsa->dmq1; | ||
| 803 | if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx, | ||
| 804 | rsa->_method_mod_q)) goto err; | ||
| 805 | |||
| 806 | /* compute I mod p */ | ||
| 807 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 808 | { | ||
| 809 | c = &local_c; | ||
| 810 | BN_with_flags(c, I, BN_FLG_CONSTTIME); | ||
| 811 | if (!BN_mod(r1,c,rsa->p,ctx)) goto err; | ||
| 812 | } | ||
| 813 | else | ||
| 814 | { | ||
| 815 | if (!BN_mod(r1,I,rsa->p,ctx)) goto err; | ||
| 816 | } | ||
| 817 | |||
| 818 | /* compute r1^dmp1 mod p */ | ||
| 819 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 820 | { | ||
| 821 | dmp1 = &local_dmp1; | ||
| 822 | BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME); | ||
| 823 | } | ||
| 824 | else | ||
| 825 | dmp1 = rsa->dmp1; | ||
| 826 | if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx, | ||
| 827 | rsa->_method_mod_p)) goto err; | ||
| 828 | |||
| 829 | if (!BN_sub(r0,r0,m1)) goto err; | ||
| 830 | /* This will help stop the size of r0 increasing, which does | ||
| 831 | * affect the multiply if it optimised for a power of 2 size */ | ||
| 832 | if (BN_is_negative(r0)) | ||
| 833 | if (!BN_add(r0,r0,rsa->p)) goto err; | ||
| 834 | |||
| 835 | if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err; | ||
| 836 | |||
| 837 | /* Turn BN_FLG_CONSTTIME flag on before division operation */ | ||
| 838 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 839 | { | ||
| 840 | pr1 = &local_r1; | ||
| 841 | BN_with_flags(pr1, r1, BN_FLG_CONSTTIME); | ||
| 842 | } | ||
| 843 | else | ||
| 844 | pr1 = r1; | ||
| 845 | if (!BN_mod(r0,pr1,rsa->p,ctx)) goto err; | ||
| 846 | |||
| 847 | /* If p < q it is occasionally possible for the correction of | ||
| 848 | * adding 'p' if r0 is negative above to leave the result still | ||
| 849 | * negative. This can break the private key operations: the following | ||
| 850 | * second correction should *always* correct this rare occurrence. | ||
| 851 | * This will *never* happen with OpenSSL generated keys because | ||
| 852 | * they ensure p > q [steve] | ||
| 853 | */ | ||
| 854 | if (BN_is_negative(r0)) | ||
| 855 | if (!BN_add(r0,r0,rsa->p)) goto err; | ||
| 856 | if (!BN_mul(r1,r0,rsa->q,ctx)) goto err; | ||
| 857 | if (!BN_add(r0,r1,m1)) goto err; | ||
| 858 | |||
| 859 | if (rsa->e && rsa->n) | ||
| 860 | { | ||
| 861 | if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err; | ||
| 862 | /* If 'I' was greater than (or equal to) rsa->n, the operation | ||
| 863 | * will be equivalent to using 'I mod n'. However, the result of | ||
| 864 | * the verify will *always* be less than 'n' so we don't check | ||
| 865 | * for absolute equality, just congruency. */ | ||
| 866 | if (!BN_sub(vrfy, vrfy, I)) goto err; | ||
| 867 | if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err; | ||
| 868 | if (BN_is_negative(vrfy)) | ||
| 869 | if (!BN_add(vrfy, vrfy, rsa->n)) goto err; | ||
| 870 | if (!BN_is_zero(vrfy)) | ||
| 871 | { | ||
| 872 | /* 'I' and 'vrfy' aren't congruent mod n. Don't leak | ||
| 873 | * miscalculated CRT output, just do a raw (slower) | ||
| 874 | * mod_exp and return that instead. */ | ||
| 875 | |||
| 876 | BIGNUM local_d; | ||
| 877 | BIGNUM *d = NULL; | ||
| 878 | |||
| 879 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 880 | { | ||
| 881 | d = &local_d; | ||
| 882 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); | ||
| 883 | } | ||
| 884 | else | ||
| 885 | d = rsa->d; | ||
| 886 | if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx, | ||
| 887 | rsa->_method_mod_n)) goto err; | ||
| 888 | } | ||
| 889 | } | ||
| 890 | ret=1; | ||
| 891 | err: | ||
| 892 | BN_CTX_end(ctx); | ||
| 893 | return(ret); | ||
| 894 | } | ||
| 895 | |||
| 896 | static int RSA_eay_init(RSA *rsa) | ||
| 897 | { | ||
| 898 | rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; | ||
| 899 | return(1); | ||
| 900 | } | ||
| 901 | |||
| 902 | static int RSA_eay_finish(RSA *rsa) | ||
| 903 | { | ||
| 904 | if (rsa->_method_mod_n != NULL) | ||
| 905 | BN_MONT_CTX_free(rsa->_method_mod_n); | ||
| 906 | if (rsa->_method_mod_p != NULL) | ||
| 907 | BN_MONT_CTX_free(rsa->_method_mod_p); | ||
| 908 | if (rsa->_method_mod_q != NULL) | ||
| 909 | BN_MONT_CTX_free(rsa->_method_mod_q); | ||
| 910 | return(1); | ||
| 911 | } | ||
| 912 | |||
| 913 | #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..501f5ea389 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_err.c | |||
| @@ -0,0 +1,179 @@ | |||
| 1 | /* crypto/rsa/rsa_err.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999-2007 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_FIPS_RSA_SIGN), "FIPS_RSA_SIGN"}, | ||
| 74 | {ERR_FUNC(RSA_F_FIPS_RSA_VERIFY), "FIPS_RSA_VERIFY"}, | ||
| 75 | {ERR_FUNC(RSA_F_MEMORY_LOCK), "MEMORY_LOCK"}, | ||
| 76 | {ERR_FUNC(RSA_F_RSA_BUILTIN_KEYGEN), "RSA_BUILTIN_KEYGEN"}, | ||
| 77 | {ERR_FUNC(RSA_F_RSA_CHECK_KEY), "RSA_check_key"}, | ||
| 78 | {ERR_FUNC(RSA_F_RSA_EAY_PRIVATE_DECRYPT), "RSA_EAY_PRIVATE_DECRYPT"}, | ||
| 79 | {ERR_FUNC(RSA_F_RSA_EAY_PRIVATE_ENCRYPT), "RSA_EAY_PRIVATE_ENCRYPT"}, | ||
| 80 | {ERR_FUNC(RSA_F_RSA_EAY_PUBLIC_DECRYPT), "RSA_EAY_PUBLIC_DECRYPT"}, | ||
| 81 | {ERR_FUNC(RSA_F_RSA_EAY_PUBLIC_ENCRYPT), "RSA_EAY_PUBLIC_ENCRYPT"}, | ||
| 82 | {ERR_FUNC(RSA_F_RSA_GENERATE_KEY), "RSA_generate_key"}, | ||
| 83 | {ERR_FUNC(RSA_F_RSA_MEMORY_LOCK), "RSA_memory_lock"}, | ||
| 84 | {ERR_FUNC(RSA_F_RSA_NEW_METHOD), "RSA_new_method"}, | ||
| 85 | {ERR_FUNC(RSA_F_RSA_NULL), "RSA_NULL"}, | ||
| 86 | {ERR_FUNC(RSA_F_RSA_NULL_MOD_EXP), "RSA_NULL_MOD_EXP"}, | ||
| 87 | {ERR_FUNC(RSA_F_RSA_NULL_PRIVATE_DECRYPT), "RSA_NULL_PRIVATE_DECRYPT"}, | ||
| 88 | {ERR_FUNC(RSA_F_RSA_NULL_PRIVATE_ENCRYPT), "RSA_NULL_PRIVATE_ENCRYPT"}, | ||
| 89 | {ERR_FUNC(RSA_F_RSA_NULL_PUBLIC_DECRYPT), "RSA_NULL_PUBLIC_DECRYPT"}, | ||
| 90 | {ERR_FUNC(RSA_F_RSA_NULL_PUBLIC_ENCRYPT), "RSA_NULL_PUBLIC_ENCRYPT"}, | ||
| 91 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_NONE), "RSA_padding_add_none"}, | ||
| 92 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP), "RSA_padding_add_PKCS1_OAEP"}, | ||
| 93 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_PSS), "RSA_padding_add_PKCS1_PSS"}, | ||
| 94 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1), "RSA_padding_add_PKCS1_type_1"}, | ||
| 95 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2), "RSA_padding_add_PKCS1_type_2"}, | ||
| 96 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_SSLV23), "RSA_padding_add_SSLv23"}, | ||
| 97 | {ERR_FUNC(RSA_F_RSA_PADDING_ADD_X931), "RSA_padding_add_X931"}, | ||
| 98 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_NONE), "RSA_padding_check_none"}, | ||
| 99 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP), "RSA_padding_check_PKCS1_OAEP"}, | ||
| 100 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1), "RSA_padding_check_PKCS1_type_1"}, | ||
| 101 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2), "RSA_padding_check_PKCS1_type_2"}, | ||
| 102 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_SSLV23), "RSA_padding_check_SSLv23"}, | ||
| 103 | {ERR_FUNC(RSA_F_RSA_PADDING_CHECK_X931), "RSA_padding_check_X931"}, | ||
| 104 | {ERR_FUNC(RSA_F_RSA_PRINT), "RSA_print"}, | ||
| 105 | {ERR_FUNC(RSA_F_RSA_PRINT_FP), "RSA_print_fp"}, | ||
| 106 | {ERR_FUNC(RSA_F_RSA_PRIVATE_ENCRYPT), "RSA_private_encrypt"}, | ||
| 107 | {ERR_FUNC(RSA_F_RSA_PUBLIC_DECRYPT), "RSA_public_decrypt"}, | ||
| 108 | {ERR_FUNC(RSA_F_RSA_SETUP_BLINDING), "RSA_setup_blinding"}, | ||
| 109 | {ERR_FUNC(RSA_F_RSA_SET_DEFAULT_METHOD), "RSA_set_default_method"}, | ||
| 110 | {ERR_FUNC(RSA_F_RSA_SET_METHOD), "RSA_set_method"}, | ||
| 111 | {ERR_FUNC(RSA_F_RSA_SIGN), "RSA_sign"}, | ||
| 112 | {ERR_FUNC(RSA_F_RSA_SIGN_ASN1_OCTET_STRING), "RSA_sign_ASN1_OCTET_STRING"}, | ||
| 113 | {ERR_FUNC(RSA_F_RSA_VERIFY), "RSA_verify"}, | ||
| 114 | {ERR_FUNC(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING), "RSA_verify_ASN1_OCTET_STRING"}, | ||
| 115 | {ERR_FUNC(RSA_F_RSA_VERIFY_PKCS1_PSS), "RSA_verify_PKCS1_PSS"}, | ||
| 116 | {0,NULL} | ||
| 117 | }; | ||
| 118 | |||
| 119 | static ERR_STRING_DATA RSA_str_reasons[]= | ||
| 120 | { | ||
| 121 | {ERR_REASON(RSA_R_ALGORITHM_MISMATCH) ,"algorithm mismatch"}, | ||
| 122 | {ERR_REASON(RSA_R_BAD_E_VALUE) ,"bad e value"}, | ||
| 123 | {ERR_REASON(RSA_R_BAD_FIXED_HEADER_DECRYPT),"bad fixed header decrypt"}, | ||
| 124 | {ERR_REASON(RSA_R_BAD_PAD_BYTE_COUNT) ,"bad pad byte count"}, | ||
| 125 | {ERR_REASON(RSA_R_BAD_SIGNATURE) ,"bad signature"}, | ||
| 126 | {ERR_REASON(RSA_R_BLOCK_TYPE_IS_NOT_01) ,"block type is not 01"}, | ||
| 127 | {ERR_REASON(RSA_R_BLOCK_TYPE_IS_NOT_02) ,"block type is not 02"}, | ||
| 128 | {ERR_REASON(RSA_R_DATA_GREATER_THAN_MOD_LEN),"data greater than mod len"}, | ||
| 129 | {ERR_REASON(RSA_R_DATA_TOO_LARGE) ,"data too large"}, | ||
| 130 | {ERR_REASON(RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE),"data too large for key size"}, | ||
| 131 | {ERR_REASON(RSA_R_DATA_TOO_LARGE_FOR_MODULUS),"data too large for modulus"}, | ||
| 132 | {ERR_REASON(RSA_R_DATA_TOO_SMALL) ,"data too small"}, | ||
| 133 | {ERR_REASON(RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE),"data too small for key size"}, | ||
| 134 | {ERR_REASON(RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY),"digest too big for rsa key"}, | ||
| 135 | {ERR_REASON(RSA_R_DMP1_NOT_CONGRUENT_TO_D),"dmp1 not congruent to d"}, | ||
| 136 | {ERR_REASON(RSA_R_DMQ1_NOT_CONGRUENT_TO_D),"dmq1 not congruent to d"}, | ||
| 137 | {ERR_REASON(RSA_R_D_E_NOT_CONGRUENT_TO_1),"d e not congruent to 1"}, | ||
| 138 | {ERR_REASON(RSA_R_FIRST_OCTET_INVALID) ,"first octet invalid"}, | ||
| 139 | {ERR_REASON(RSA_R_INVALID_HEADER) ,"invalid header"}, | ||
| 140 | {ERR_REASON(RSA_R_INVALID_MESSAGE_LENGTH),"invalid message length"}, | ||
| 141 | {ERR_REASON(RSA_R_INVALID_PADDING) ,"invalid padding"}, | ||
| 142 | {ERR_REASON(RSA_R_INVALID_TRAILER) ,"invalid trailer"}, | ||
| 143 | {ERR_REASON(RSA_R_IQMP_NOT_INVERSE_OF_Q) ,"iqmp not inverse of q"}, | ||
| 144 | {ERR_REASON(RSA_R_KEY_SIZE_TOO_SMALL) ,"key size too small"}, | ||
| 145 | {ERR_REASON(RSA_R_LAST_OCTET_INVALID) ,"last octet invalid"}, | ||
| 146 | {ERR_REASON(RSA_R_MODULUS_TOO_LARGE) ,"modulus too large"}, | ||
| 147 | {ERR_REASON(RSA_R_NON_FIPS_METHOD) ,"non fips method"}, | ||
| 148 | {ERR_REASON(RSA_R_NO_PUBLIC_EXPONENT) ,"no public exponent"}, | ||
| 149 | {ERR_REASON(RSA_R_NULL_BEFORE_BLOCK_MISSING),"null before block missing"}, | ||
| 150 | {ERR_REASON(RSA_R_N_DOES_NOT_EQUAL_P_Q) ,"n does not equal p q"}, | ||
| 151 | {ERR_REASON(RSA_R_OAEP_DECODING_ERROR) ,"oaep decoding error"}, | ||
| 152 | {ERR_REASON(RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE),"operation not allowed in fips mode"}, | ||
| 153 | {ERR_REASON(RSA_R_PADDING_CHECK_FAILED) ,"padding check failed"}, | ||
| 154 | {ERR_REASON(RSA_R_P_NOT_PRIME) ,"p not prime"}, | ||
| 155 | {ERR_REASON(RSA_R_Q_NOT_PRIME) ,"q not prime"}, | ||
| 156 | {ERR_REASON(RSA_R_RSA_OPERATIONS_NOT_SUPPORTED),"rsa operations not supported"}, | ||
| 157 | {ERR_REASON(RSA_R_SLEN_CHECK_FAILED) ,"salt length check failed"}, | ||
| 158 | {ERR_REASON(RSA_R_SLEN_RECOVERY_FAILED) ,"salt length recovery failed"}, | ||
| 159 | {ERR_REASON(RSA_R_SSLV3_ROLLBACK_ATTACK) ,"sslv3 rollback attack"}, | ||
| 160 | {ERR_REASON(RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD),"the asn1 object identifier is not known for this md"}, | ||
| 161 | {ERR_REASON(RSA_R_UNKNOWN_ALGORITHM_TYPE),"unknown algorithm type"}, | ||
| 162 | {ERR_REASON(RSA_R_UNKNOWN_PADDING_TYPE) ,"unknown padding type"}, | ||
| 163 | {ERR_REASON(RSA_R_WRONG_SIGNATURE_LENGTH),"wrong signature length"}, | ||
| 164 | {0,NULL} | ||
| 165 | }; | ||
| 166 | |||
| 167 | #endif | ||
| 168 | |||
| 169 | void ERR_load_RSA_strings(void) | ||
| 170 | { | ||
| 171 | #ifndef OPENSSL_NO_ERR | ||
| 172 | |||
| 173 | if (ERR_func_error_string(RSA_str_functs[0].error) == NULL) | ||
| 174 | { | ||
| 175 | ERR_load_strings(0,RSA_str_functs); | ||
| 176 | ERR_load_strings(0,RSA_str_reasons); | ||
| 177 | } | ||
| 178 | #endif | ||
| 179 | } | ||
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c new file mode 100644 index 0000000000..41278f83c6 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_gen.c | |||
| @@ -0,0 +1,222 @@ | |||
| 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 | |||
| 60 | /* NB: these functions have been "upgraded", the deprecated versions (which are | ||
| 61 | * compatibility wrappers using these functions) are in rsa_depr.c. | ||
| 62 | * - Geoff | ||
| 63 | */ | ||
| 64 | |||
| 65 | #include <stdio.h> | ||
| 66 | #include <time.h> | ||
| 67 | #include "cryptlib.h" | ||
| 68 | #include <openssl/bn.h> | ||
| 69 | #include <openssl/rsa.h> | ||
| 70 | |||
| 71 | #ifndef OPENSSL_FIPS | ||
| 72 | |||
| 73 | static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb); | ||
| 74 | |||
| 75 | /* NB: this wrapper would normally be placed in rsa_lib.c and the static | ||
| 76 | * implementation would probably be in rsa_eay.c. Nonetheless, is kept here so | ||
| 77 | * that we don't introduce a new linker dependency. Eg. any application that | ||
| 78 | * wasn't previously linking object code related to key-generation won't have to | ||
| 79 | * now just because key-generation is part of RSA_METHOD. */ | ||
| 80 | int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) | ||
| 81 | { | ||
| 82 | if(rsa->meth->rsa_keygen) | ||
| 83 | return rsa->meth->rsa_keygen(rsa, bits, e_value, cb); | ||
| 84 | return rsa_builtin_keygen(rsa, bits, e_value, cb); | ||
| 85 | } | ||
| 86 | |||
| 87 | static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) | ||
| 88 | { | ||
| 89 | BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp; | ||
| 90 | BIGNUM local_r0,local_d,local_p; | ||
| 91 | BIGNUM *pr0,*d,*p; | ||
| 92 | int bitsp,bitsq,ok= -1,n=0; | ||
| 93 | BN_CTX *ctx=NULL; | ||
| 94 | |||
| 95 | ctx=BN_CTX_new(); | ||
| 96 | if (ctx == NULL) goto err; | ||
| 97 | BN_CTX_start(ctx); | ||
| 98 | r0 = BN_CTX_get(ctx); | ||
| 99 | r1 = BN_CTX_get(ctx); | ||
| 100 | r2 = BN_CTX_get(ctx); | ||
| 101 | r3 = BN_CTX_get(ctx); | ||
| 102 | if (r3 == NULL) goto err; | ||
| 103 | |||
| 104 | bitsp=(bits+1)/2; | ||
| 105 | bitsq=bits-bitsp; | ||
| 106 | |||
| 107 | /* We need the RSA components non-NULL */ | ||
| 108 | if(!rsa->n && ((rsa->n=BN_new()) == NULL)) goto err; | ||
| 109 | if(!rsa->d && ((rsa->d=BN_new()) == NULL)) goto err; | ||
| 110 | if(!rsa->e && ((rsa->e=BN_new()) == NULL)) goto err; | ||
| 111 | if(!rsa->p && ((rsa->p=BN_new()) == NULL)) goto err; | ||
| 112 | if(!rsa->q && ((rsa->q=BN_new()) == NULL)) goto err; | ||
| 113 | if(!rsa->dmp1 && ((rsa->dmp1=BN_new()) == NULL)) goto err; | ||
| 114 | if(!rsa->dmq1 && ((rsa->dmq1=BN_new()) == NULL)) goto err; | ||
| 115 | if(!rsa->iqmp && ((rsa->iqmp=BN_new()) == NULL)) goto err; | ||
| 116 | |||
| 117 | BN_copy(rsa->e, e_value); | ||
| 118 | |||
| 119 | /* generate p and q */ | ||
| 120 | for (;;) | ||
| 121 | { | ||
| 122 | if(!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb)) | ||
| 123 | goto err; | ||
| 124 | if (!BN_sub(r2,rsa->p,BN_value_one())) goto err; | ||
| 125 | if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; | ||
| 126 | if (BN_is_one(r1)) break; | ||
| 127 | if(!BN_GENCB_call(cb, 2, n++)) | ||
| 128 | goto err; | ||
| 129 | } | ||
| 130 | if(!BN_GENCB_call(cb, 3, 0)) | ||
| 131 | goto err; | ||
| 132 | for (;;) | ||
| 133 | { | ||
| 134 | /* When generating ridiculously small keys, we can get stuck | ||
| 135 | * continually regenerating the same prime values. Check for | ||
| 136 | * this and bail if it happens 3 times. */ | ||
| 137 | unsigned int degenerate = 0; | ||
| 138 | do | ||
| 139 | { | ||
| 140 | if(!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb)) | ||
| 141 | goto err; | ||
| 142 | } while((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3)); | ||
| 143 | if(degenerate == 3) | ||
| 144 | { | ||
| 145 | ok = 0; /* we set our own err */ | ||
| 146 | RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,RSA_R_KEY_SIZE_TOO_SMALL); | ||
| 147 | goto err; | ||
| 148 | } | ||
| 149 | if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; | ||
| 150 | if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; | ||
| 151 | if (BN_is_one(r1)) | ||
| 152 | break; | ||
| 153 | if(!BN_GENCB_call(cb, 2, n++)) | ||
| 154 | goto err; | ||
| 155 | } | ||
| 156 | if(!BN_GENCB_call(cb, 3, 1)) | ||
| 157 | goto err; | ||
| 158 | if (BN_cmp(rsa->p,rsa->q) < 0) | ||
| 159 | { | ||
| 160 | tmp=rsa->p; | ||
| 161 | rsa->p=rsa->q; | ||
| 162 | rsa->q=tmp; | ||
| 163 | } | ||
| 164 | |||
| 165 | /* calculate n */ | ||
| 166 | if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err; | ||
| 167 | |||
| 168 | /* calculate d */ | ||
| 169 | if (!BN_sub(r1,rsa->p,BN_value_one())) goto err; /* p-1 */ | ||
| 170 | if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; /* q-1 */ | ||
| 171 | if (!BN_mul(r0,r1,r2,ctx)) goto err; /* (p-1)(q-1) */ | ||
| 172 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 173 | { | ||
| 174 | pr0 = &local_r0; | ||
| 175 | BN_with_flags(pr0, r0, BN_FLG_CONSTTIME); | ||
| 176 | } | ||
| 177 | else | ||
| 178 | pr0 = r0; | ||
| 179 | if (!BN_mod_inverse(rsa->d,rsa->e,pr0,ctx)) goto err; /* d */ | ||
| 180 | |||
| 181 | /* set up d for correct BN_FLG_CONSTTIME flag */ | ||
| 182 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 183 | { | ||
| 184 | d = &local_d; | ||
| 185 | BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); | ||
| 186 | } | ||
| 187 | else | ||
| 188 | d = rsa->d; | ||
| 189 | |||
| 190 | /* calculate d mod (p-1) */ | ||
| 191 | if (!BN_mod(rsa->dmp1,d,r1,ctx)) goto err; | ||
| 192 | |||
| 193 | /* calculate d mod (q-1) */ | ||
| 194 | if (!BN_mod(rsa->dmq1,d,r2,ctx)) goto err; | ||
| 195 | |||
| 196 | /* calculate inverse of q mod p */ | ||
| 197 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 198 | { | ||
| 199 | p = &local_p; | ||
| 200 | BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME); | ||
| 201 | } | ||
| 202 | else | ||
| 203 | p = rsa->p; | ||
| 204 | if (!BN_mod_inverse(rsa->iqmp,rsa->q,p,ctx)) goto err; | ||
| 205 | |||
| 206 | ok=1; | ||
| 207 | err: | ||
| 208 | if (ok == -1) | ||
| 209 | { | ||
| 210 | RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,ERR_LIB_BN); | ||
| 211 | ok=0; | ||
| 212 | } | ||
| 213 | if (ctx != NULL) | ||
| 214 | { | ||
| 215 | BN_CTX_end(ctx); | ||
| 216 | BN_CTX_free(ctx); | ||
| 217 | } | ||
| 218 | |||
| 219 | return ok; | ||
| 220 | } | ||
| 221 | |||
| 222 | #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..5714841f4c --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_lib.c | |||
| @@ -0,0 +1,234 @@ | |||
| 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 | int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, | ||
| 71 | RSA *rsa, int padding) | ||
| 72 | { | ||
| 73 | return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding)); | ||
| 74 | } | ||
| 75 | |||
| 76 | int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, | ||
| 77 | RSA *rsa, int padding) | ||
| 78 | { | ||
| 79 | #ifdef OPENSSL_FIPS | ||
| 80 | if(FIPS_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)) | ||
| 81 | { | ||
| 82 | RSAerr(RSA_F_RSA_PRIVATE_ENCRYPT, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE); | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | #endif | ||
| 86 | return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding)); | ||
| 87 | } | ||
| 88 | |||
| 89 | int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, | ||
| 90 | RSA *rsa, int padding) | ||
| 91 | { | ||
| 92 | return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding)); | ||
| 93 | } | ||
| 94 | |||
| 95 | int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, | ||
| 96 | RSA *rsa, int padding) | ||
| 97 | { | ||
| 98 | #ifdef OPENSSL_FIPS | ||
| 99 | if(FIPS_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)) | ||
| 100 | { | ||
| 101 | RSAerr(RSA_F_RSA_PUBLIC_DECRYPT, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE); | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | #endif | ||
| 105 | return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding)); | ||
| 106 | } | ||
| 107 | |||
| 108 | int RSA_size(const RSA *r) | ||
| 109 | { | ||
| 110 | return(BN_num_bytes(r->n)); | ||
| 111 | } | ||
| 112 | |||
| 113 | void RSA_blinding_off(RSA *rsa) | ||
| 114 | { | ||
| 115 | if (rsa->blinding != NULL) | ||
| 116 | { | ||
| 117 | BN_BLINDING_free(rsa->blinding); | ||
| 118 | rsa->blinding=NULL; | ||
| 119 | } | ||
| 120 | rsa->flags &= ~RSA_FLAG_BLINDING; | ||
| 121 | rsa->flags |= RSA_FLAG_NO_BLINDING; | ||
| 122 | } | ||
| 123 | |||
| 124 | int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) | ||
| 125 | { | ||
| 126 | int ret=0; | ||
| 127 | |||
| 128 | if (rsa->blinding != NULL) | ||
| 129 | RSA_blinding_off(rsa); | ||
| 130 | |||
| 131 | rsa->blinding = RSA_setup_blinding(rsa, ctx); | ||
| 132 | if (rsa->blinding == NULL) | ||
| 133 | goto err; | ||
| 134 | |||
| 135 | rsa->flags |= RSA_FLAG_BLINDING; | ||
| 136 | rsa->flags &= ~RSA_FLAG_NO_BLINDING; | ||
| 137 | ret=1; | ||
| 138 | err: | ||
| 139 | return(ret); | ||
| 140 | } | ||
| 141 | |||
| 142 | static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, | ||
| 143 | const BIGNUM *q, BN_CTX *ctx) | ||
| 144 | { | ||
| 145 | BIGNUM *ret = NULL, *r0, *r1, *r2; | ||
| 146 | |||
| 147 | if (d == NULL || p == NULL || q == NULL) | ||
| 148 | return NULL; | ||
| 149 | |||
| 150 | BN_CTX_start(ctx); | ||
| 151 | r0 = BN_CTX_get(ctx); | ||
| 152 | r1 = BN_CTX_get(ctx); | ||
| 153 | r2 = BN_CTX_get(ctx); | ||
| 154 | if (r2 == NULL) | ||
| 155 | goto err; | ||
| 156 | |||
| 157 | if (!BN_sub(r1, p, BN_value_one())) goto err; | ||
| 158 | if (!BN_sub(r2, q, BN_value_one())) goto err; | ||
| 159 | if (!BN_mul(r0, r1, r2, ctx)) goto err; | ||
| 160 | |||
| 161 | ret = BN_mod_inverse(NULL, d, r0, ctx); | ||
| 162 | err: | ||
| 163 | BN_CTX_end(ctx); | ||
| 164 | return ret; | ||
| 165 | } | ||
| 166 | |||
| 167 | BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx) | ||
| 168 | { | ||
| 169 | BIGNUM local_n; | ||
| 170 | BIGNUM *e,*n; | ||
| 171 | BN_CTX *ctx; | ||
| 172 | BN_BLINDING *ret = NULL; | ||
| 173 | |||
| 174 | if (in_ctx == NULL) | ||
| 175 | { | ||
| 176 | if ((ctx = BN_CTX_new()) == NULL) return 0; | ||
| 177 | } | ||
| 178 | else | ||
| 179 | ctx = in_ctx; | ||
| 180 | |||
| 181 | BN_CTX_start(ctx); | ||
| 182 | e = BN_CTX_get(ctx); | ||
| 183 | if (e == NULL) | ||
| 184 | { | ||
| 185 | RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE); | ||
| 186 | goto err; | ||
| 187 | } | ||
| 188 | |||
| 189 | if (rsa->e == NULL) | ||
| 190 | { | ||
| 191 | e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx); | ||
| 192 | if (e == NULL) | ||
| 193 | { | ||
| 194 | RSAerr(RSA_F_RSA_SETUP_BLINDING, RSA_R_NO_PUBLIC_EXPONENT); | ||
| 195 | goto err; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | else | ||
| 199 | e = rsa->e; | ||
| 200 | |||
| 201 | |||
| 202 | if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL) | ||
| 203 | { | ||
| 204 | /* if PRNG is not properly seeded, resort to secret | ||
| 205 | * exponent as unpredictable seed */ | ||
| 206 | RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0.0); | ||
| 207 | } | ||
| 208 | |||
| 209 | if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) | ||
| 210 | { | ||
| 211 | /* Set BN_FLG_CONSTTIME flag */ | ||
| 212 | n = &local_n; | ||
| 213 | BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME); | ||
| 214 | } | ||
| 215 | else | ||
| 216 | n = rsa->n; | ||
| 217 | |||
| 218 | ret = BN_BLINDING_create_param(NULL, e, n, ctx, | ||
| 219 | rsa->meth->bn_mod_exp, rsa->_method_mod_n); | ||
| 220 | if (ret == NULL) | ||
| 221 | { | ||
| 222 | RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB); | ||
| 223 | goto err; | ||
| 224 | } | ||
| 225 | BN_BLINDING_set_thread_id(ret, CRYPTO_thread_id()); | ||
| 226 | err: | ||
| 227 | BN_CTX_end(ctx); | ||
| 228 | if (in_ctx == NULL) | ||
| 229 | BN_CTX_free(ctx); | ||
| 230 | if(rsa->e == NULL) | ||
| 231 | BN_free(e); | ||
| 232 | |||
| 233 | return ret; | ||
| 234 | } | ||
diff --git a/src/lib/libcrypto/rsa/rsa_none.c b/src/lib/libcrypto/rsa/rsa_none.c new file mode 100644 index 0000000000..e6f3e627ca --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_none.c | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | /* crypto/rsa/rsa_none.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include "cryptlib.h" | ||
| 61 | #include <openssl/bn.h> | ||
| 62 | #include <openssl/rsa.h> | ||
| 63 | #include <openssl/rand.h> | ||
| 64 | |||
| 65 | int RSA_padding_add_none(unsigned char *to, int tlen, | ||
| 66 | const unsigned char *from, int flen) | ||
| 67 | { | ||
| 68 | if (flen > tlen) | ||
| 69 | { | ||
| 70 | RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 71 | return(0); | ||
| 72 | } | ||
| 73 | |||
| 74 | if (flen < tlen) | ||
| 75 | { | ||
| 76 | RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE); | ||
| 77 | return(0); | ||
| 78 | } | ||
| 79 | |||
| 80 | memcpy(to,from,(unsigned int)flen); | ||
| 81 | return(1); | ||
| 82 | } | ||
| 83 | |||
| 84 | int RSA_padding_check_none(unsigned char *to, int tlen, | ||
| 85 | const unsigned char *from, int flen, int num) | ||
| 86 | { | ||
| 87 | |||
| 88 | if (flen > tlen) | ||
| 89 | { | ||
| 90 | RSAerr(RSA_F_RSA_PADDING_CHECK_NONE,RSA_R_DATA_TOO_LARGE); | ||
| 91 | return(-1); | ||
| 92 | } | ||
| 93 | |||
| 94 | memset(to,0,tlen-flen); | ||
| 95 | memcpy(to+tlen-flen,from,flen); | ||
| 96 | return(tlen); | ||
| 97 | } | ||
| 98 | |||
diff --git a/src/lib/libcrypto/rsa/rsa_oaep.c b/src/lib/libcrypto/rsa/rsa_oaep.c new file mode 100644 index 0000000000..4d30c9d2d3 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_oaep.c | |||
| @@ -0,0 +1,220 @@ | |||
| 1 | /* crypto/rsa/rsa_oaep.c */ | ||
| 2 | /* Written by Ulf Moeller. This software is distributed on an "AS IS" | ||
| 3 | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */ | ||
| 4 | |||
| 5 | /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */ | ||
| 6 | |||
| 7 | /* See Victor Shoup, "OAEP reconsidered," Nov. 2000, | ||
| 8 | * <URL: http://www.shoup.net/papers/oaep.ps.Z> | ||
| 9 | * for problems with the security proof for the | ||
| 10 | * original OAEP scheme, which EME-OAEP is based on. | ||
| 11 | * | ||
| 12 | * A new proof can be found in E. Fujisaki, T. Okamoto, | ||
| 13 | * D. Pointcheval, J. Stern, "RSA-OEAP is Still Alive!", | ||
| 14 | * Dec. 2000, <URL: http://eprint.iacr.org/2000/061/>. | ||
| 15 | * The new proof has stronger requirements for the | ||
| 16 | * underlying permutation: "partial-one-wayness" instead | ||
| 17 | * of one-wayness. For the RSA function, this is | ||
| 18 | * an equivalent notion. | ||
| 19 | */ | ||
| 20 | |||
| 21 | |||
| 22 | #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1) | ||
| 23 | #include <stdio.h> | ||
| 24 | #include "cryptlib.h" | ||
| 25 | #include <openssl/bn.h> | ||
| 26 | #include <openssl/rsa.h> | ||
| 27 | #include <openssl/evp.h> | ||
| 28 | #include <openssl/rand.h> | ||
| 29 | #include <openssl/sha.h> | ||
| 30 | |||
| 31 | int MGF1(unsigned char *mask, long len, | ||
| 32 | const unsigned char *seed, long seedlen); | ||
| 33 | |||
| 34 | int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, | ||
| 35 | const unsigned char *from, int flen, | ||
| 36 | const unsigned char *param, int plen) | ||
| 37 | { | ||
| 38 | int i, emlen = tlen - 1; | ||
| 39 | unsigned char *db, *seed; | ||
| 40 | unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH]; | ||
| 41 | |||
| 42 | if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1) | ||
| 43 | { | ||
| 44 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, | ||
| 45 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | if (emlen < 2 * SHA_DIGEST_LENGTH + 1) | ||
| 50 | { | ||
| 51 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL); | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH); | ||
| 56 | if (dbmask == NULL) | ||
| 57 | { | ||
| 58 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE); | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | to[0] = 0; | ||
| 63 | seed = to + 1; | ||
| 64 | db = to + SHA_DIGEST_LENGTH + 1; | ||
| 65 | |||
| 66 | EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL); | ||
| 67 | memset(db + SHA_DIGEST_LENGTH, 0, | ||
| 68 | emlen - flen - 2 * SHA_DIGEST_LENGTH - 1); | ||
| 69 | db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01; | ||
| 70 | memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen); | ||
| 71 | if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0) | ||
| 72 | return 0; | ||
| 73 | #ifdef PKCS_TESTVECT | ||
| 74 | memcpy(seed, | ||
| 75 | "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f", | ||
| 76 | 20); | ||
| 77 | #endif | ||
| 78 | |||
| 79 | MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH); | ||
| 80 | for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++) | ||
| 81 | db[i] ^= dbmask[i]; | ||
| 82 | |||
| 83 | MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH); | ||
| 84 | for (i = 0; i < SHA_DIGEST_LENGTH; i++) | ||
| 85 | seed[i] ^= seedmask[i]; | ||
| 86 | |||
| 87 | OPENSSL_free(dbmask); | ||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen, | ||
| 92 | const unsigned char *from, int flen, int num, | ||
| 93 | const unsigned char *param, int plen) | ||
| 94 | { | ||
| 95 | int i, dblen, mlen = -1; | ||
| 96 | const unsigned char *maskeddb; | ||
| 97 | int lzero; | ||
| 98 | unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH]; | ||
| 99 | unsigned char *padded_from; | ||
| 100 | int bad = 0; | ||
| 101 | |||
| 102 | if (--num < 2 * SHA_DIGEST_LENGTH + 1) | ||
| 103 | /* 'num' is the length of the modulus, i.e. does not depend on the | ||
| 104 | * particular ciphertext. */ | ||
| 105 | goto decoding_err; | ||
| 106 | |||
| 107 | lzero = num - flen; | ||
| 108 | if (lzero < 0) | ||
| 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 | flen = num; /* don't overflow the memcpy to padded_from */ | ||
| 118 | } | ||
| 119 | |||
| 120 | dblen = num - SHA_DIGEST_LENGTH; | ||
| 121 | db = OPENSSL_malloc(dblen + num); | ||
| 122 | if (db == NULL) | ||
| 123 | { | ||
| 124 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE); | ||
| 125 | return -1; | ||
| 126 | } | ||
| 127 | |||
| 128 | /* Always do this zero-padding copy (even when lzero == 0) | ||
| 129 | * to avoid leaking timing info about the value of lzero. */ | ||
| 130 | padded_from = db + dblen; | ||
| 131 | memset(padded_from, 0, lzero); | ||
| 132 | memcpy(padded_from + lzero, from, flen); | ||
| 133 | |||
| 134 | maskeddb = padded_from + SHA_DIGEST_LENGTH; | ||
| 135 | |||
| 136 | MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen); | ||
| 137 | for (i = 0; i < SHA_DIGEST_LENGTH; i++) | ||
| 138 | seed[i] ^= padded_from[i]; | ||
| 139 | |||
| 140 | MGF1(db, dblen, seed, SHA_DIGEST_LENGTH); | ||
| 141 | for (i = 0; i < dblen; i++) | ||
| 142 | db[i] ^= maskeddb[i]; | ||
| 143 | |||
| 144 | EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL); | ||
| 145 | |||
| 146 | if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad) | ||
| 147 | goto decoding_err; | ||
| 148 | else | ||
| 149 | { | ||
| 150 | for (i = SHA_DIGEST_LENGTH; i < dblen; i++) | ||
| 151 | if (db[i] != 0x00) | ||
| 152 | break; | ||
| 153 | if (i == dblen || db[i] != 0x01) | ||
| 154 | goto decoding_err; | ||
| 155 | else | ||
| 156 | { | ||
| 157 | /* everything looks OK */ | ||
| 158 | |||
| 159 | mlen = dblen - ++i; | ||
| 160 | if (tlen < mlen) | ||
| 161 | { | ||
| 162 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE); | ||
| 163 | mlen = -1; | ||
| 164 | } | ||
| 165 | else | ||
| 166 | memcpy(to, db + i, mlen); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | OPENSSL_free(db); | ||
| 170 | return mlen; | ||
| 171 | |||
| 172 | decoding_err: | ||
| 173 | /* to avoid chosen ciphertext attacks, the error message should not reveal | ||
| 174 | * which kind of decoding error happened */ | ||
| 175 | RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR); | ||
| 176 | if (db != NULL) OPENSSL_free(db); | ||
| 177 | return -1; | ||
| 178 | } | ||
| 179 | |||
| 180 | int PKCS1_MGF1(unsigned char *mask, long len, | ||
| 181 | const unsigned char *seed, long seedlen, const EVP_MD *dgst) | ||
| 182 | { | ||
| 183 | long i, outlen = 0; | ||
| 184 | unsigned char cnt[4]; | ||
| 185 | EVP_MD_CTX c; | ||
| 186 | unsigned char md[EVP_MAX_MD_SIZE]; | ||
| 187 | int mdlen; | ||
| 188 | |||
| 189 | EVP_MD_CTX_init(&c); | ||
| 190 | mdlen = M_EVP_MD_size(dgst); | ||
| 191 | for (i = 0; outlen < len; i++) | ||
| 192 | { | ||
| 193 | cnt[0] = (unsigned char)((i >> 24) & 255); | ||
| 194 | cnt[1] = (unsigned char)((i >> 16) & 255); | ||
| 195 | cnt[2] = (unsigned char)((i >> 8)) & 255; | ||
| 196 | cnt[3] = (unsigned char)(i & 255); | ||
| 197 | EVP_DigestInit_ex(&c,dgst, NULL); | ||
| 198 | EVP_DigestUpdate(&c, seed, seedlen); | ||
| 199 | EVP_DigestUpdate(&c, cnt, 4); | ||
| 200 | if (outlen + mdlen <= len) | ||
| 201 | { | ||
| 202 | EVP_DigestFinal_ex(&c, mask + outlen, NULL); | ||
| 203 | outlen += mdlen; | ||
| 204 | } | ||
| 205 | else | ||
| 206 | { | ||
| 207 | EVP_DigestFinal_ex(&c, md, NULL); | ||
| 208 | memcpy(mask + outlen, md, len - outlen); | ||
| 209 | outlen = len; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | EVP_MD_CTX_cleanup(&c); | ||
| 213 | return 0; | ||
| 214 | } | ||
| 215 | |||
| 216 | int MGF1(unsigned char *mask, long len, const unsigned char *seed, long seedlen) | ||
| 217 | { | ||
| 218 | return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1()); | ||
| 219 | } | ||
| 220 | #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..9b993aca49 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_pss.c | |||
| @@ -0,0 +1,269 @@ | |||
| 1 | /* rsa_pss.c */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) 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 | static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0}; | ||
| 68 | |||
| 69 | #if defined(_MSC_VER) && defined(_ARM_) | ||
| 70 | #pragma optimize("g", off) | ||
| 71 | #endif | ||
| 72 | |||
| 73 | int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, | ||
| 74 | const EVP_MD *Hash, const unsigned char *EM, int sLen) | ||
| 75 | { | ||
| 76 | int i; | ||
| 77 | int ret = 0; | ||
| 78 | int hLen, maskedDBLen, MSBits, emLen; | ||
| 79 | const unsigned char *H; | ||
| 80 | unsigned char *DB = NULL; | ||
| 81 | EVP_MD_CTX ctx; | ||
| 82 | unsigned char H_[EVP_MAX_MD_SIZE]; | ||
| 83 | |||
| 84 | hLen = M_EVP_MD_size(Hash); | ||
| 85 | /* | ||
| 86 | * Negative sLen has special meanings: | ||
| 87 | * -1 sLen == hLen | ||
| 88 | * -2 salt length is autorecovered from signature | ||
| 89 | * -N reserved | ||
| 90 | */ | ||
| 91 | if (sLen == -1) sLen = hLen; | ||
| 92 | else if (sLen == -2) sLen = -2; | ||
| 93 | else if (sLen < -2) | ||
| 94 | { | ||
| 95 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED); | ||
| 96 | goto err; | ||
| 97 | } | ||
| 98 | |||
| 99 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; | ||
| 100 | emLen = RSA_size(rsa); | ||
| 101 | if (EM[0] & (0xFF << MSBits)) | ||
| 102 | { | ||
| 103 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_FIRST_OCTET_INVALID); | ||
| 104 | goto err; | ||
| 105 | } | ||
| 106 | if (MSBits == 0) | ||
| 107 | { | ||
| 108 | EM++; | ||
| 109 | emLen--; | ||
| 110 | } | ||
| 111 | if (emLen < (hLen + sLen + 2)) /* sLen can be small negative */ | ||
| 112 | { | ||
| 113 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_DATA_TOO_LARGE); | ||
| 114 | goto err; | ||
| 115 | } | ||
| 116 | if (EM[emLen - 1] != 0xbc) | ||
| 117 | { | ||
| 118 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_LAST_OCTET_INVALID); | ||
| 119 | goto err; | ||
| 120 | } | ||
| 121 | maskedDBLen = emLen - hLen - 1; | ||
| 122 | H = EM + maskedDBLen; | ||
| 123 | DB = OPENSSL_malloc(maskedDBLen); | ||
| 124 | if (!DB) | ||
| 125 | { | ||
| 126 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, ERR_R_MALLOC_FAILURE); | ||
| 127 | goto err; | ||
| 128 | } | ||
| 129 | PKCS1_MGF1(DB, maskedDBLen, H, hLen, Hash); | ||
| 130 | for (i = 0; i < maskedDBLen; i++) | ||
| 131 | DB[i] ^= EM[i]; | ||
| 132 | if (MSBits) | ||
| 133 | DB[0] &= 0xFF >> (8 - MSBits); | ||
| 134 | for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) ; | ||
| 135 | if (DB[i++] != 0x1) | ||
| 136 | { | ||
| 137 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_RECOVERY_FAILED); | ||
| 138 | goto err; | ||
| 139 | } | ||
| 140 | if (sLen >= 0 && (maskedDBLen - i) != sLen) | ||
| 141 | { | ||
| 142 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED); | ||
| 143 | goto err; | ||
| 144 | } | ||
| 145 | EVP_MD_CTX_init(&ctx); | ||
| 146 | EVP_DigestInit_ex(&ctx, Hash, NULL); | ||
| 147 | EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes); | ||
| 148 | EVP_DigestUpdate(&ctx, mHash, hLen); | ||
| 149 | if (maskedDBLen - i) | ||
| 150 | EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i); | ||
| 151 | EVP_DigestFinal(&ctx, H_, NULL); | ||
| 152 | EVP_MD_CTX_cleanup(&ctx); | ||
| 153 | if (memcmp(H_, H, hLen)) | ||
| 154 | { | ||
| 155 | RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_BAD_SIGNATURE); | ||
| 156 | ret = 0; | ||
| 157 | } | ||
| 158 | else | ||
| 159 | ret = 1; | ||
| 160 | |||
| 161 | err: | ||
| 162 | if (DB) | ||
| 163 | OPENSSL_free(DB); | ||
| 164 | |||
| 165 | return ret; | ||
| 166 | |||
| 167 | } | ||
| 168 | |||
| 169 | int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM, | ||
| 170 | const unsigned char *mHash, | ||
| 171 | const EVP_MD *Hash, int sLen) | ||
| 172 | { | ||
| 173 | int i; | ||
| 174 | int ret = 0; | ||
| 175 | int hLen, maskedDBLen, MSBits, emLen; | ||
| 176 | unsigned char *H, *salt = NULL, *p; | ||
| 177 | EVP_MD_CTX ctx; | ||
| 178 | |||
| 179 | hLen = M_EVP_MD_size(Hash); | ||
| 180 | /* | ||
| 181 | * Negative sLen has special meanings: | ||
| 182 | * -1 sLen == hLen | ||
| 183 | * -2 salt length is maximized | ||
| 184 | * -N reserved | ||
| 185 | */ | ||
| 186 | if (sLen == -1) sLen = hLen; | ||
| 187 | else if (sLen == -2) sLen = -2; | ||
| 188 | else if (sLen < -2) | ||
| 189 | { | ||
| 190 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED); | ||
| 191 | goto err; | ||
| 192 | } | ||
| 193 | |||
| 194 | MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; | ||
| 195 | emLen = RSA_size(rsa); | ||
| 196 | if (MSBits == 0) | ||
| 197 | { | ||
| 198 | *EM++ = 0; | ||
| 199 | emLen--; | ||
| 200 | } | ||
| 201 | if (sLen == -2) | ||
| 202 | { | ||
| 203 | sLen = emLen - hLen - 2; | ||
| 204 | } | ||
| 205 | else if (emLen < (hLen + sLen + 2)) | ||
| 206 | { | ||
| 207 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, | ||
| 208 | RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | ||
| 209 | goto err; | ||
| 210 | } | ||
| 211 | if (sLen > 0) | ||
| 212 | { | ||
| 213 | salt = OPENSSL_malloc(sLen); | ||
| 214 | if (!salt) | ||
| 215 | { | ||
| 216 | RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, | ||
| 217 | ERR_R_MALLOC_FAILURE); | ||
| 218 | goto err; | ||
| 219 | } | ||
| 220 | if (!RAND_bytes(salt, sLen)) | ||
| 221 | goto err; | ||
| 222 | } | ||
| 223 | maskedDBLen = emLen - hLen - 1; | ||
| 224 | H = EM + maskedDBLen; | ||
| 225 | EVP_MD_CTX_init(&ctx); | ||
| 226 | EVP_DigestInit_ex(&ctx, Hash, NULL); | ||
| 227 | EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes); | ||
| 228 | EVP_DigestUpdate(&ctx, mHash, hLen); | ||
| 229 | if (sLen) | ||
| 230 | EVP_DigestUpdate(&ctx, salt, sLen); | ||
| 231 | EVP_DigestFinal(&ctx, H, NULL); | ||
| 232 | EVP_MD_CTX_cleanup(&ctx); | ||
| 233 | |||
| 234 | /* Generate dbMask in place then perform XOR on it */ | ||
| 235 | PKCS1_MGF1(EM, maskedDBLen, H, hLen, Hash); | ||
| 236 | |||
| 237 | p = EM; | ||
| 238 | |||
| 239 | /* Initial PS XORs with all zeroes which is a NOP so just update | ||
| 240 | * pointer. Note from a test above this value is guaranteed to | ||
| 241 | * be non-negative. | ||
| 242 | */ | ||
| 243 | p += emLen - sLen - hLen - 2; | ||
| 244 | *p++ ^= 0x1; | ||
| 245 | if (sLen > 0) | ||
| 246 | { | ||
| 247 | for (i = 0; i < sLen; i++) | ||
| 248 | *p++ ^= salt[i]; | ||
| 249 | } | ||
| 250 | if (MSBits) | ||
| 251 | EM[0] &= 0xFF >> (8 - MSBits); | ||
| 252 | |||
| 253 | /* H is already in place so just set final 0xbc */ | ||
| 254 | |||
| 255 | EM[emLen - 1] = 0xbc; | ||
| 256 | |||
| 257 | ret = 1; | ||
| 258 | |||
| 259 | err: | ||
| 260 | if (salt) | ||
| 261 | OPENSSL_free(salt); | ||
| 262 | |||
| 263 | return ret; | ||
| 264 | |||
| 265 | } | ||
| 266 | |||
| 267 | #if defined(_MSC_VER) | ||
| 268 | #pragma optimize("",on) | ||
| 269 | #endif | ||
diff --git a/src/lib/libcrypto/rsa/rsa_saos.c b/src/lib/libcrypto/rsa/rsa_saos.c new file mode 100644 index 0000000000..f98e0a80a6 --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_saos.c | |||
| @@ -0,0 +1,150 @@ | |||
| 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 *s; | ||
| 111 | const unsigned char *p; | ||
| 112 | ASN1_OCTET_STRING *sig=NULL; | ||
| 113 | |||
| 114 | if (siglen != (unsigned int)RSA_size(rsa)) | ||
| 115 | { | ||
| 116 | RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH); | ||
| 117 | return(0); | ||
| 118 | } | ||
| 119 | |||
| 120 | s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen); | ||
| 121 | if (s == NULL) | ||
| 122 | { | ||
| 123 | RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); | ||
| 124 | goto err; | ||
| 125 | } | ||
| 126 | i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); | ||
| 127 | |||
| 128 | if (i <= 0) goto err; | ||
| 129 | |||
| 130 | p=s; | ||
| 131 | sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i); | ||
| 132 | if (sig == NULL) goto err; | ||
| 133 | |||
| 134 | if ( ((unsigned int)sig->length != m_len) || | ||
| 135 | (memcmp(m,sig->data,m_len) != 0)) | ||
| 136 | { | ||
| 137 | RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE); | ||
| 138 | } | ||
| 139 | else | ||
| 140 | ret=1; | ||
| 141 | err: | ||
| 142 | if (sig != NULL) M_ASN1_OCTET_STRING_free(sig); | ||
| 143 | if (s != NULL) | ||
| 144 | { | ||
| 145 | OPENSSL_cleanse(s,(unsigned int)siglen); | ||
| 146 | OPENSSL_free(s); | ||
| 147 | } | ||
| 148 | return(ret); | ||
| 149 | } | ||
| 150 | |||
diff --git a/src/lib/libcrypto/rsa/rsa_sign.c b/src/lib/libcrypto/rsa/rsa_sign.c new file mode 100644 index 0000000000..5488c06f6d --- /dev/null +++ b/src/lib/libcrypto/rsa/rsa_sign.c | |||
| @@ -0,0 +1,269 @@ | |||
| 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 | /* NB: in FIPS mode block anything that isn't a TLS signature */ | ||
| 94 | #ifdef OPENSSL_FIPS | ||
| 95 | if(FIPS_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)) | ||
| 96 | { | ||
| 97 | RSAerr(RSA_F_RSA_SIGN, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE); | ||
| 98 | return 0; | ||
| 99 | } | ||
| 100 | #endif | ||
| 101 | sig.algor= &algor; | ||
| 102 | sig.algor->algorithm=OBJ_nid2obj(type); | ||
| 103 | if (sig.algor->algorithm == NULL) | ||
| 104 | { | ||
| 105 | RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE); | ||
| 106 | return(0); | ||
| 107 | } | ||
| 108 | if (sig.algor->algorithm->length == 0) | ||
| 109 | { | ||
| 110 | RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); | ||
| 111 | return(0); | ||
| 112 | } | ||
| 113 | parameter.type=V_ASN1_NULL; | ||
| 114 | parameter.value.ptr=NULL; | ||
| 115 | sig.algor->parameter= ¶meter; | ||
| 116 | |||
| 117 | sig.digest= &digest; | ||
| 118 | sig.digest->data=(unsigned char *)m; /* TMP UGLY CAST */ | ||
| 119 | sig.digest->length=m_len; | ||
| 120 | |||
| 121 | i=i2d_X509_SIG(&sig,NULL); | ||
| 122 | } | ||
| 123 | j=RSA_size(rsa); | ||
| 124 | if (i > (j-RSA_PKCS1_PADDING_SIZE)) | ||
| 125 | { | ||
| 126 | RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); | ||
| 127 | return(0); | ||
| 128 | } | ||
| 129 | if(type != NID_md5_sha1) { | ||
| 130 | tmps=(unsigned char *)OPENSSL_malloc((unsigned int)j+1); | ||
| 131 | if (tmps == NULL) | ||
| 132 | { | ||
| 133 | RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE); | ||
| 134 | return(0); | ||
| 135 | } | ||
| 136 | p=tmps; | ||
| 137 | i2d_X509_SIG(&sig,&p); | ||
| 138 | s=tmps; | ||
| 139 | } | ||
| 140 | i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING); | ||
| 141 | if (i <= 0) | ||
| 142 | ret=0; | ||
| 143 | else | ||
| 144 | *siglen=i; | ||
| 145 | |||
| 146 | if(type != NID_md5_sha1) { | ||
| 147 | OPENSSL_cleanse(tmps,(unsigned int)j+1); | ||
| 148 | OPENSSL_free(tmps); | ||
| 149 | } | ||
| 150 | return(ret); | ||
| 151 | } | ||
| 152 | |||
| 153 | int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len, | ||
| 154 | unsigned char *sigbuf, unsigned int siglen, RSA *rsa) | ||
| 155 | { | ||
| 156 | int i,ret=0,sigtype; | ||
| 157 | unsigned char *s; | ||
| 158 | X509_SIG *sig=NULL; | ||
| 159 | |||
| 160 | if (siglen != (unsigned int)RSA_size(rsa)) | ||
| 161 | { | ||
| 162 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH); | ||
| 163 | return(0); | ||
| 164 | } | ||
| 165 | |||
| 166 | if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify) | ||
| 167 | { | ||
| 168 | return rsa->meth->rsa_verify(dtype, m, m_len, | ||
| 169 | sigbuf, siglen, rsa); | ||
| 170 | } | ||
| 171 | |||
| 172 | s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen); | ||
| 173 | if (s == NULL) | ||
| 174 | { | ||
| 175 | RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE); | ||
| 176 | goto err; | ||
| 177 | } | ||
| 178 | if(dtype == NID_md5_sha1) | ||
| 179 | { | ||
| 180 | if (m_len != SSL_SIG_LENGTH) | ||
| 181 | { | ||
| 182 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH); | ||
| 183 | goto err; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | /* NB: in FIPS mode block anything that isn't a TLS signature */ | ||
| 187 | #ifdef OPENSSL_FIPS | ||
| 188 | else if(FIPS_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)) | ||
| 189 | { | ||
| 190 | RSAerr(RSA_F_RSA_VERIFY, RSA_R_OPERATION_NOT_ALLOWED_IN_FIPS_MODE); | ||
| 191 | return 0; | ||
| 192 | } | ||
| 193 | #endif | ||
| 194 | i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); | ||
| 195 | |||
| 196 | if (i <= 0) goto err; | ||
| 197 | |||
| 198 | /* Special case: SSL signature */ | ||
| 199 | if(dtype == NID_md5_sha1) { | ||
| 200 | if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH)) | ||
| 201 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); | ||
| 202 | else ret = 1; | ||
| 203 | } else { | ||
| 204 | const unsigned char *p=s; | ||
| 205 | sig=d2i_X509_SIG(NULL,&p,(long)i); | ||
| 206 | |||
| 207 | if (sig == NULL) goto err; | ||
| 208 | |||
| 209 | /* Excess data can be used to create forgeries */ | ||
| 210 | if(p != s+i) | ||
| 211 | { | ||
| 212 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); | ||
| 213 | goto err; | ||
| 214 | } | ||
| 215 | |||
| 216 | /* Parameters to the signature algorithm can also be used to | ||
| 217 | create forgeries */ | ||
| 218 | if(sig->algor->parameter | ||
| 219 | && ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL) | ||
| 220 | { | ||
| 221 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); | ||
| 222 | goto err; | ||
| 223 | } | ||
| 224 | |||
| 225 | sigtype=OBJ_obj2nid(sig->algor->algorithm); | ||
| 226 | |||
| 227 | |||
| 228 | #ifdef RSA_DEBUG | ||
| 229 | /* put a backward compatibility flag in EAY */ | ||
| 230 | fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype), | ||
| 231 | OBJ_nid2ln(dtype)); | ||
| 232 | #endif | ||
| 233 | if (sigtype != dtype) | ||
| 234 | { | ||
| 235 | if (((dtype == NID_md5) && | ||
| 236 | (sigtype == NID_md5WithRSAEncryption)) || | ||
| 237 | ((dtype == NID_md2) && | ||
| 238 | (sigtype == NID_md2WithRSAEncryption))) | ||
| 239 | { | ||
| 240 | /* ok, we will let it through */ | ||
| 241 | #if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16) | ||
| 242 | fprintf(stderr,"signature has problems, re-make with post SSLeay045\n"); | ||
| 243 | #endif | ||
| 244 | } | ||
| 245 | else | ||
| 246 | { | ||
| 247 | RSAerr(RSA_F_RSA_VERIFY, | ||
| 248 | RSA_R_ALGORITHM_MISMATCH); | ||
| 249 | goto err; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | if ( ((unsigned int)sig->digest->length != m_len) || | ||
| 253 | (memcmp(m,sig->digest->data,m_len) != 0)) | ||
| 254 | { | ||
| 255 | RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE); | ||
| 256 | } | ||
| 257 | else | ||
| 258 | ret=1; | ||
| 259 | } | ||
| 260 | err: | ||
| 261 | if (sig != NULL) X509_SIG_free(sig); | ||
| 262 | if (s != NULL) | ||
| 263 | { | ||
| 264 | OPENSSL_cleanse(s,(unsigned int)siglen); | ||
| 265 | OPENSSL_free(s); | ||
| 266 | } | ||
| 267 | return(ret); | ||
| 268 | } | ||
| 269 | |||
diff --git a/src/lib/libcrypto/rsa/rsa_ssl.c b/src/lib/libcrypto/rsa/rsa_ssl.c new file mode 100644 index 0000000000..cfeff15bc9 --- /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 = -9; k<-1; 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_x931.c b/src/lib/libcrypto/rsa/rsa_x931.c new file mode 100644 index 0000000000..21548e37ed --- /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 (steve@openssl.org) 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 = 0,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 | |||
