grpc 1.12.0 → 1.13.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (245) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +314 -23
  3. data/include/grpc/impl/codegen/fork.h +4 -4
  4. data/include/grpc/impl/codegen/grpc_types.h +1 -1
  5. data/include/grpc/impl/codegen/port_platform.h +3 -0
  6. data/src/boringssl/err_data.c +256 -246
  7. data/src/core/ext/filters/client_channel/channel_connectivity.cc +1 -1
  8. data/src/core/ext/filters/client_channel/client_channel.cc +367 -272
  9. data/src/core/ext/filters/client_channel/lb_policy.h +1 -3
  10. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.cc +11 -9
  11. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +42 -32
  12. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h +36 -0
  13. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.cc +36 -102
  14. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.h +37 -32
  15. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.cc +22 -19
  16. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h +1 -1
  17. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +1 -1
  18. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +1 -1
  19. data/src/core/ext/filters/client_channel/resolver.h +1 -3
  20. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +3 -3
  21. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.cc +2 -2
  22. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +0 -1
  23. data/src/core/ext/filters/client_channel/resolver/dns/native/dns_resolver.cc +4 -4
  24. data/src/core/ext/filters/client_channel/subchannel.cc +3 -3
  25. data/src/core/ext/filters/http/client_authority_filter.cc +5 -4
  26. data/src/core/ext/filters/http/message_compress/message_compress_filter.cc +4 -4
  27. data/src/core/ext/filters/http/server/http_server_filter.cc +123 -131
  28. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +1 -1
  29. data/src/core/ext/transport/chttp2/transport/bin_decoder.cc +9 -8
  30. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +19 -19
  31. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +10 -6
  32. data/src/core/ext/transport/chttp2/transport/hpack_encoder.h +4 -3
  33. data/src/core/ext/transport/chttp2/transport/parsing.cc +14 -12
  34. data/src/core/ext/transport/chttp2/transport/writing.cc +6 -6
  35. data/src/core/lib/channel/channel_stack.cc +0 -5
  36. data/src/core/lib/channel/channel_stack.h +1 -1
  37. data/src/core/lib/channel/channel_stack_builder.cc +0 -3
  38. data/src/core/lib/channel/channel_stack_builder.h +0 -2
  39. data/src/core/lib/channel/channel_trace.cc +3 -3
  40. data/src/core/lib/channel/channelz_registry.cc +77 -0
  41. data/src/core/lib/channel/channelz_registry.h +99 -0
  42. data/src/core/lib/channel/handshaker.cc +20 -1
  43. data/src/core/lib/debug/stats.h +7 -0
  44. data/src/core/lib/debug/stats_data.cc +5 -0
  45. data/src/core/lib/debug/stats_data.h +120 -0
  46. data/src/core/lib/debug/trace.h +11 -9
  47. data/src/core/lib/gprpp/fork.cc +260 -0
  48. data/src/core/lib/gprpp/fork.h +79 -0
  49. data/src/core/lib/gprpp/memory.h +12 -0
  50. data/src/core/lib/gprpp/orphanable.h +2 -6
  51. data/src/core/lib/gprpp/ref_counted.h +2 -6
  52. data/src/core/lib/gprpp/thd.h +0 -3
  53. data/src/core/lib/gprpp/thd_posix.cc +4 -53
  54. data/src/core/lib/gprpp/thd_windows.cc +0 -7
  55. data/src/core/lib/http/httpcli_security_connector.cc +1 -3
  56. data/src/core/lib/iomgr/combiner.cc +19 -2
  57. data/src/core/lib/iomgr/combiner.h +1 -1
  58. data/src/core/lib/iomgr/ev_epoll1_linux.cc +2 -2
  59. data/src/core/lib/iomgr/ev_epollex_linux.cc +59 -3
  60. data/src/core/lib/iomgr/ev_epollsig_linux.cc +1 -1
  61. data/src/core/lib/iomgr/ev_poll_posix.cc +2 -2
  62. data/src/core/lib/iomgr/ev_posix.cc +11 -4
  63. data/src/core/lib/iomgr/ev_posix.h +6 -0
  64. data/src/core/lib/iomgr/exec_ctx.cc +9 -9
  65. data/src/core/lib/iomgr/exec_ctx.h +39 -20
  66. data/src/core/lib/iomgr/fork_posix.cc +30 -18
  67. data/src/core/lib/iomgr/iomgr_posix.cc +2 -2
  68. data/src/core/lib/iomgr/polling_entity.cc +11 -2
  69. data/src/core/lib/iomgr/pollset_custom.cc +2 -2
  70. data/src/core/lib/iomgr/port.h +38 -1
  71. data/src/core/lib/iomgr/resolve_address.h +1 -1
  72. data/src/core/lib/iomgr/resolve_address_posix.cc +1 -1
  73. data/src/core/lib/iomgr/resource_quota.cc +1 -1
  74. data/src/core/lib/iomgr/sockaddr_posix.h +1 -1
  75. data/src/core/lib/iomgr/socket_factory_posix.cc +1 -1
  76. data/src/core/lib/iomgr/socket_utils_common_posix.cc +1 -1
  77. data/src/core/lib/iomgr/tcp_client_custom.cc +3 -3
  78. data/src/core/lib/iomgr/tcp_client_posix.cc +3 -2
  79. data/src/core/lib/iomgr/tcp_custom.cc +1 -1
  80. data/src/core/lib/iomgr/tcp_posix.cc +18 -10
  81. data/src/core/lib/iomgr/tcp_server_posix.cc +9 -8
  82. data/src/core/lib/iomgr/tcp_server_utils_posix.h +1 -1
  83. data/src/core/lib/iomgr/tcp_server_utils_posix_common.cc +16 -4
  84. data/src/core/lib/iomgr/timer.h +1 -1
  85. data/src/core/lib/iomgr/timer_generic.cc +113 -41
  86. data/src/core/lib/iomgr/timer_manager.cc +1 -1
  87. data/src/core/lib/security/credentials/credentials.h +1 -0
  88. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +88 -115
  89. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +16 -0
  90. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +10 -6
  91. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.h +1 -1
  92. data/src/core/lib/security/security_connector/alts_security_connector.cc +2 -1
  93. data/src/core/lib/security/security_connector/security_connector.cc +7 -7
  94. data/src/core/lib/security/transport/security_handshaker.cc +1 -0
  95. data/src/core/lib/security/util/json_util.cc +4 -0
  96. data/src/core/lib/slice/slice_buffer.cc +15 -3
  97. data/src/core/lib/surface/call.cc +31 -17
  98. data/src/core/lib/surface/call.h +5 -0
  99. data/src/core/lib/surface/channel.cc +2 -5
  100. data/src/core/lib/surface/completion_queue.cc +1 -3
  101. data/src/core/lib/surface/completion_queue.h +0 -1
  102. data/src/core/lib/surface/init.cc +7 -8
  103. data/src/core/lib/surface/version.cc +2 -2
  104. data/src/core/lib/transport/byte_stream.cc +1 -1
  105. data/src/core/lib/transport/transport.cc +2 -1
  106. data/src/core/lib/transport/transport.h +4 -8
  107. data/src/core/lib/transport/transport_op_string.cc +1 -1
  108. data/src/core/tsi/alts/handshaker/alts_handshaker_client.cc +19 -7
  109. data/src/core/tsi/alts/handshaker/alts_handshaker_client.h +10 -0
  110. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker.cc +28 -2
  111. data/src/core/tsi/alts/handshaker/alts_tsi_handshaker_private.h +3 -0
  112. data/src/core/tsi/fake_transport_security.cc +1 -0
  113. data/src/core/tsi/ssl_transport_security.cc +238 -110
  114. data/src/core/tsi/transport_security.cc +14 -0
  115. data/src/core/tsi/transport_security.h +2 -0
  116. data/src/core/tsi/transport_security_interface.h +11 -1
  117. data/src/ruby/bin/math_client.rb +17 -9
  118. data/src/ruby/lib/grpc/generic/rpc_server.rb +2 -1
  119. data/src/ruby/lib/grpc/version.rb +1 -1
  120. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +4 -1
  121. data/third_party/boringssl/crypto/asn1/a_int.c +33 -28
  122. data/third_party/boringssl/crypto/asn1/a_mbstr.c +24 -22
  123. data/third_party/boringssl/crypto/asn1/a_utf8.c +13 -11
  124. data/third_party/boringssl/crypto/asn1/asn1_locl.h +3 -0
  125. data/third_party/boringssl/crypto/bio/fd.c +1 -0
  126. data/third_party/boringssl/crypto/bio/file.c +2 -0
  127. data/third_party/boringssl/crypto/bn_extra/convert.c +6 -5
  128. data/third_party/boringssl/crypto/bytestring/ber.c +1 -4
  129. data/third_party/boringssl/crypto/bytestring/cbb.c +116 -16
  130. data/third_party/boringssl/crypto/bytestring/cbs.c +150 -20
  131. data/third_party/boringssl/crypto/cipher_extra/e_aesccm.c +171 -0
  132. data/third_party/boringssl/crypto/cipher_extra/e_rc2.c +2 -0
  133. data/third_party/boringssl/crypto/cipher_extra/e_tls.c +1 -2
  134. data/third_party/boringssl/crypto/cpu-aarch64-fuchsia.c +55 -0
  135. data/third_party/boringssl/crypto/cpu-aarch64-linux.c +2 -1
  136. data/third_party/boringssl/crypto/dsa/dsa.c +16 -54
  137. data/third_party/boringssl/crypto/fipsmodule/bcm.c +11 -542
  138. data/third_party/boringssl/crypto/fipsmodule/bn/add.c +33 -64
  139. data/third_party/boringssl/crypto/fipsmodule/bn/asm/x86_64-gcc.c +4 -3
  140. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +122 -70
  141. data/third_party/boringssl/crypto/fipsmodule/bn/bytes.c +32 -71
  142. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +58 -112
  143. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +198 -122
  144. data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +31 -65
  145. data/third_party/boringssl/crypto/fipsmodule/bn/generic.c +2 -1
  146. data/third_party/boringssl/crypto/fipsmodule/bn/internal.h +98 -15
  147. data/third_party/boringssl/crypto/fipsmodule/bn/jacobi.c +1 -1
  148. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +124 -81
  149. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +8 -30
  150. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +303 -347
  151. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +2 -3
  152. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +3 -4
  153. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +199 -222
  154. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.h +27 -47
  155. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +45 -28
  156. data/third_party/boringssl/crypto/fipsmodule/bn/sqrt.c +1 -1
  157. data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +10 -10
  158. data/third_party/boringssl/crypto/fipsmodule/des/internal.h +2 -0
  159. data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +78 -47
  160. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +35 -54
  161. data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +3 -10
  162. data/third_party/boringssl/crypto/fipsmodule/ec/internal.h +36 -22
  163. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +59 -90
  164. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +29 -48
  165. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +17 -26
  166. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.h +15 -11
  167. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +45 -51
  168. data/third_party/boringssl/crypto/fipsmodule/ec/{util-64.c → util.c} +0 -5
  169. data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +144 -264
  170. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +78 -56
  171. data/third_party/boringssl/crypto/fipsmodule/modes/ccm.c +256 -0
  172. data/third_party/boringssl/crypto/fipsmodule/modes/internal.h +36 -32
  173. data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +9 -7
  174. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +16 -10
  175. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +255 -102
  176. data/third_party/boringssl/crypto/fipsmodule/self_check/self_check.c +581 -0
  177. data/third_party/boringssl/crypto/fipsmodule/tls/internal.h +39 -0
  178. data/third_party/boringssl/crypto/fipsmodule/tls/kdf.c +165 -0
  179. data/third_party/boringssl/crypto/internal.h +65 -2
  180. data/third_party/boringssl/crypto/mem.c +0 -2
  181. data/third_party/boringssl/crypto/obj/obj.c +6 -73
  182. data/third_party/boringssl/crypto/thread_pthread.c +35 -5
  183. data/third_party/boringssl/crypto/x509/a_strex.c +11 -11
  184. data/third_party/boringssl/crypto/x509/x_name.c +13 -0
  185. data/third_party/boringssl/include/openssl/aead.h +4 -0
  186. data/third_party/boringssl/include/openssl/asn1.h +1 -3
  187. data/third_party/boringssl/include/openssl/base.h +1 -14
  188. data/third_party/boringssl/include/openssl/bio.h +1 -1
  189. data/third_party/boringssl/include/openssl/bn.h +49 -15
  190. data/third_party/boringssl/include/openssl/bytestring.h +49 -24
  191. data/third_party/boringssl/include/openssl/crypto.h +4 -0
  192. data/third_party/boringssl/include/openssl/ec_key.h +7 -3
  193. data/third_party/boringssl/include/openssl/err.h +9 -9
  194. data/third_party/boringssl/include/openssl/evp.h +1 -1
  195. data/third_party/boringssl/include/openssl/rsa.h +34 -10
  196. data/third_party/boringssl/include/openssl/ssl.h +160 -17
  197. data/third_party/boringssl/include/openssl/stack.h +1 -1
  198. data/third_party/boringssl/include/openssl/tls1.h +10 -2
  199. data/third_party/boringssl/include/openssl/x509.h +3 -0
  200. data/third_party/boringssl/ssl/d1_both.cc +16 -2
  201. data/third_party/boringssl/ssl/dtls_method.cc +1 -1
  202. data/third_party/boringssl/ssl/handoff.cc +285 -0
  203. data/third_party/boringssl/ssl/handshake.cc +26 -12
  204. data/third_party/boringssl/ssl/handshake_client.cc +65 -31
  205. data/third_party/boringssl/ssl/handshake_server.cc +14 -2
  206. data/third_party/boringssl/ssl/internal.h +132 -79
  207. data/third_party/boringssl/ssl/s3_both.cc +2 -2
  208. data/third_party/boringssl/ssl/s3_lib.cc +3 -1
  209. data/third_party/boringssl/ssl/s3_pkt.cc +0 -18
  210. data/third_party/boringssl/ssl/ssl_aead_ctx.cc +1 -4
  211. data/third_party/boringssl/ssl/ssl_asn1.cc +47 -43
  212. data/third_party/boringssl/ssl/ssl_cipher.cc +8 -8
  213. data/third_party/boringssl/ssl/ssl_key_share.cc +3 -1
  214. data/third_party/boringssl/ssl/ssl_lib.cc +83 -14
  215. data/third_party/boringssl/ssl/ssl_privkey.cc +6 -0
  216. data/third_party/boringssl/ssl/ssl_stat.cc +6 -6
  217. data/third_party/boringssl/ssl/ssl_versions.cc +12 -85
  218. data/third_party/boringssl/ssl/ssl_x509.cc +59 -61
  219. data/third_party/boringssl/ssl/t1_enc.cc +73 -124
  220. data/third_party/boringssl/ssl/t1_lib.cc +367 -41
  221. data/third_party/boringssl/ssl/tls13_both.cc +8 -0
  222. data/third_party/boringssl/ssl/tls13_client.cc +98 -184
  223. data/third_party/boringssl/ssl/tls13_enc.cc +88 -158
  224. data/third_party/boringssl/ssl/tls13_server.cc +91 -137
  225. data/third_party/boringssl/ssl/tls_method.cc +0 -17
  226. data/third_party/boringssl/ssl/tls_record.cc +1 -10
  227. data/third_party/boringssl/third_party/fiat/curve25519.c +921 -2753
  228. data/third_party/boringssl/third_party/fiat/curve25519_tables.h +7880 -0
  229. data/third_party/boringssl/third_party/fiat/internal.h +32 -20
  230. data/third_party/boringssl/third_party/fiat/p256.c +1824 -0
  231. metadata +64 -64
  232. data/src/core/lib/channel/channel_trace_registry.cc +0 -80
  233. data/src/core/lib/channel/channel_trace_registry.h +0 -43
  234. data/src/core/lib/gpr/fork.cc +0 -78
  235. data/src/core/lib/gpr/fork.h +0 -35
  236. data/src/core/tsi/transport_security_adapter.cc +0 -235
  237. data/src/core/tsi/transport_security_adapter.h +0 -41
  238. data/src/ruby/bin/apis/google/protobuf/empty.rb +0 -29
  239. data/src/ruby/bin/apis/pubsub_demo.rb +0 -241
  240. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub.rb +0 -159
  241. data/src/ruby/bin/apis/tech/pubsub/proto/pubsub_services.rb +0 -88
  242. data/src/ruby/pb/test/client.rb +0 -764
  243. data/src/ruby/pb/test/server.rb +0 -252
  244. data/third_party/boringssl/crypto/curve25519/x25519-x86_64.c +0 -247
  245. data/third_party/boringssl/crypto/fipsmodule/ec/p256-64.c +0 -1674
@@ -77,7 +77,7 @@ BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
77
77
  }
78
78
 
79
79
  if (len == 0) {
80
- ret->top = 0;
80
+ ret->width = 0;
81
81
  return ret;
82
82
  }
83
83
 
@@ -93,7 +93,7 @@ BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
93
93
  // |bn_wexpand| must check bounds on |num_words| to write it into
94
94
  // |ret->dmax|.
95
95
  assert(num_words <= INT_MAX);
96
- ret->top = (int)num_words;
96
+ ret->width = (int)num_words;
97
97
  ret->neg = 0;
98
98
 
99
99
  while (len--) {
@@ -105,9 +105,6 @@ BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
105
105
  }
106
106
  }
107
107
 
108
- // need to call this due to clear byte at top if avoiding having the top bit
109
- // set (-ve number)
110
- bn_correct_top(ret);
111
108
  return ret;
112
109
  }
113
110
 
@@ -123,7 +120,7 @@ BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
123
120
  }
124
121
 
125
122
  if (len == 0) {
126
- ret->top = 0;
123
+ ret->width = 0;
127
124
  ret->neg = 0;
128
125
  return ret;
129
126
  }
@@ -134,7 +131,7 @@ BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
134
131
  BN_free(bn);
135
132
  return NULL;
136
133
  }
137
- ret->top = num_words;
134
+ ret->width = num_words;
138
135
 
139
136
  // Make sure the top bytes will be zeroed.
140
137
  ret->d[num_words - 1] = 0;
@@ -142,8 +139,6 @@ BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
142
139
  // We only support little-endian platforms, so we can simply memcpy the
143
140
  // internal representation.
144
141
  OPENSSL_memcpy(ret->d, in, len);
145
-
146
- bn_correct_top(ret);
147
142
  return ret;
148
143
  }
149
144
 
@@ -159,88 +154,54 @@ size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) {
159
154
  return n;
160
155
  }
161
156
 
157
+ static int fits_in_bytes(const uint8_t *bytes, size_t num_bytes, size_t len) {
158
+ uint8_t mask = 0;
159
+ for (size_t i = len; i < num_bytes; i++) {
160
+ mask |= bytes[i];
161
+ }
162
+ return mask == 0;
163
+ }
164
+
162
165
  int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
163
- // If we don't have enough space, fail out.
164
- size_t num_bytes = BN_num_bytes(in);
166
+ const uint8_t *bytes = (const uint8_t *)in->d;
167
+ size_t num_bytes = in->width * BN_BYTES;
165
168
  if (len < num_bytes) {
166
- return 0;
169
+ if (!fits_in_bytes(bytes, num_bytes, len)) {
170
+ return 0;
171
+ }
172
+ num_bytes = len;
167
173
  }
168
174
 
169
175
  // We only support little-endian platforms, so we can simply memcpy into the
170
176
  // internal representation.
171
- OPENSSL_memcpy(out, in->d, num_bytes);
172
-
177
+ OPENSSL_memcpy(out, bytes, num_bytes);
173
178
  // Pad out the rest of the buffer with zeroes.
174
179
  OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
175
-
176
180
  return 1;
177
181
  }
178
182
 
179
- // constant_time_select_ulong returns |x| if |v| is 1 and |y| if |v| is 0. Its
180
- // behavior is undefined if |v| takes any other value.
181
- static BN_ULONG constant_time_select_ulong(int v, BN_ULONG x, BN_ULONG y) {
182
- BN_ULONG mask = v;
183
- mask--;
184
-
185
- return (~mask & x) | (mask & y);
186
- }
187
-
188
- // constant_time_le_size_t returns 1 if |x| <= |y| and 0 otherwise. |x| and |y|
189
- // must not have their MSBs set.
190
- static int constant_time_le_size_t(size_t x, size_t y) {
191
- return ((x - y - 1) >> (sizeof(size_t) * 8 - 1)) & 1;
192
- }
193
-
194
- // read_word_padded returns the |i|'th word of |in|, if it is not out of
195
- // bounds. Otherwise, it returns 0. It does so without branches on the size of
196
- // |in|, however it necessarily does not have the same memory access pattern. If
197
- // the access would be out of bounds, it reads the last word of |in|. |in| must
198
- // not be zero.
199
- static BN_ULONG read_word_padded(const BIGNUM *in, size_t i) {
200
- // Read |in->d[i]| if valid. Otherwise, read the last word.
201
- BN_ULONG l = in->d[constant_time_select_ulong(
202
- constant_time_le_size_t(in->dmax, i), in->dmax - 1, i)];
203
-
204
- // Clamp to zero if above |d->top|.
205
- return constant_time_select_ulong(constant_time_le_size_t(in->top, i), 0, l);
206
- }
207
-
208
183
  int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
209
- // Special case for |in| = 0. Just branch as the probability is negligible.
210
- if (BN_is_zero(in)) {
211
- OPENSSL_memset(out, 0, len);
212
- return 1;
213
- }
214
-
215
- // Check if the integer is too big. This case can exit early in non-constant
216
- // time.
217
- if ((size_t)in->top > (len + (BN_BYTES - 1)) / BN_BYTES) {
218
- return 0;
219
- }
220
- if ((len % BN_BYTES) != 0) {
221
- BN_ULONG l = read_word_padded(in, len / BN_BYTES);
222
- if (l >> (8 * (len % BN_BYTES)) != 0) {
184
+ const uint8_t *bytes = (const uint8_t *)in->d;
185
+ size_t num_bytes = in->width * BN_BYTES;
186
+ if (len < num_bytes) {
187
+ if (!fits_in_bytes(bytes, num_bytes, len)) {
223
188
  return 0;
224
189
  }
190
+ num_bytes = len;
225
191
  }
226
192
 
227
- // Write the bytes out one by one. Serialization is done without branching on
228
- // the bits of |in| or on |in->top|, but if the routine would otherwise read
229
- // out of bounds, the memory access pattern can't be fixed. However, for an
230
- // RSA key of size a multiple of the word size, the probability of BN_BYTES
231
- // leading zero octets is low.
232
- //
233
- // See Falko Stenzke, "Manger's Attack revisited", ICICS 2010.
234
- size_t i = len;
235
- while (i--) {
236
- BN_ULONG l = read_word_padded(in, i / BN_BYTES);
237
- *(out++) = (uint8_t)(l >> (8 * (i % BN_BYTES))) & 0xff;
193
+ // We only support little-endian platforms, so we can simply write the buffer
194
+ // in reverse.
195
+ for (size_t i = 0; i < num_bytes; i++) {
196
+ out[len - i - 1] = bytes[i];
238
197
  }
198
+ // Pad out the rest of the buffer with zeroes.
199
+ OPENSSL_memset(out, 0, len - num_bytes);
239
200
  return 1;
240
201
  }
241
202
 
242
203
  BN_ULONG BN_get_word(const BIGNUM *bn) {
243
- switch (bn->top) {
204
+ switch (bn_minimal_width(bn)) {
244
205
  case 0:
245
206
  return 0;
246
207
  case 1:
@@ -251,7 +212,7 @@ BN_ULONG BN_get_word(const BIGNUM *bn) {
251
212
  }
252
213
 
253
214
  int BN_get_u64(const BIGNUM *bn, uint64_t *out) {
254
- switch (bn->top) {
215
+ switch (bn_minimal_width(bn)) {
255
216
  case 0:
256
217
  *out = 0;
257
218
  return 1;
@@ -63,33 +63,43 @@
63
63
  #include "../../internal.h"
64
64
 
65
65
 
66
- int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
67
- int i;
68
- BN_ULONG t1, t2, *ap, *bp;
69
-
70
- i = a->top - b->top;
71
- if (i != 0) {
72
- return i;
66
+ static int bn_cmp_words_consttime(const BN_ULONG *a, size_t a_len,
67
+ const BN_ULONG *b, size_t b_len) {
68
+ OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
69
+ crypto_word_t_too_small);
70
+ int ret = 0;
71
+ // Process the common words in little-endian order.
72
+ size_t min = a_len < b_len ? a_len : b_len;
73
+ for (size_t i = 0; i < min; i++) {
74
+ crypto_word_t eq = constant_time_eq_w(a[i], b[i]);
75
+ crypto_word_t lt = constant_time_lt_w(a[i], b[i]);
76
+ ret =
77
+ constant_time_select_int(eq, ret, constant_time_select_int(lt, -1, 1));
73
78
  }
74
79
 
75
- ap = a->d;
76
- bp = b->d;
77
- for (i = a->top - 1; i >= 0; i--) {
78
- t1 = ap[i];
79
- t2 = bp[i];
80
- if (t1 != t2) {
81
- return (t1 > t2) ? 1 : -1;
80
+ // If |a| or |b| has non-zero words beyond |min|, they take precedence.
81
+ if (a_len < b_len) {
82
+ crypto_word_t mask = 0;
83
+ for (size_t i = a_len; i < b_len; i++) {
84
+ mask |= b[i];
85
+ }
86
+ ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, -1);
87
+ } else if (b_len < a_len) {
88
+ crypto_word_t mask = 0;
89
+ for (size_t i = b_len; i < a_len; i++) {
90
+ mask |= a[i];
82
91
  }
92
+ ret = constant_time_select_int(constant_time_is_zero_w(mask), ret, 1);
83
93
  }
84
94
 
85
- return 0;
95
+ return ret;
86
96
  }
87
97
 
88
- int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
89
- int i;
90
- int gt, lt;
91
- BN_ULONG t1, t2;
98
+ int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
99
+ return bn_cmp_words_consttime(a->d, a->width, b->d, b->width);
100
+ }
92
101
 
102
+ int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
93
103
  if ((a == NULL) || (b == NULL)) {
94
104
  if (a != NULL) {
95
105
  return -1;
@@ -100,97 +110,25 @@ int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
100
110
  }
101
111
  }
102
112
 
113
+ // We do not attempt to process the sign bit in constant time. Negative
114
+ // |BIGNUM|s should never occur in crypto, only calculators.
103
115
  if (a->neg != b->neg) {
104
116
  if (a->neg) {
105
117
  return -1;
106
118
  }
107
119
  return 1;
108
120
  }
109
- if (a->neg == 0) {
110
- gt = 1;
111
- lt = -1;
112
- } else {
113
- gt = -1;
114
- lt = 1;
115
- }
116
-
117
- if (a->top > b->top) {
118
- return gt;
119
- }
120
- if (a->top < b->top) {
121
- return lt;
122
- }
123
-
124
- for (i = a->top - 1; i >= 0; i--) {
125
- t1 = a->d[i];
126
- t2 = b->d[i];
127
- if (t1 > t2) {
128
- return gt;
129
- } if (t1 < t2) {
130
- return lt;
131
- }
132
- }
133
-
134
- return 0;
135
- }
136
-
137
- int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) {
138
- int i;
139
- BN_ULONG aa, bb;
140
-
141
- aa = a[n - 1];
142
- bb = b[n - 1];
143
- if (aa != bb) {
144
- return (aa > bb) ? 1 : -1;
145
- }
146
-
147
- for (i = n - 2; i >= 0; i--) {
148
- aa = a[i];
149
- bb = b[i];
150
- if (aa != bb) {
151
- return (aa > bb) ? 1 : -1;
152
- }
153
- }
154
- return 0;
155
- }
156
-
157
- int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) {
158
- int n, i;
159
- n = cl - 1;
160
-
161
- if (dl < 0) {
162
- for (i = dl; i < 0; i++) {
163
- if (b[n - i] != 0) {
164
- return -1; // a < b
165
- }
166
- }
167
- }
168
- if (dl > 0) {
169
- for (i = dl; i > 0; i--) {
170
- if (a[n + i] != 0) {
171
- return 1; // a > b
172
- }
173
- }
174
- }
175
121
 
176
- return bn_cmp_words(a, b, cl);
122
+ int ret = BN_ucmp(a, b);
123
+ return a->neg ? -ret : ret;
177
124
  }
178
125
 
179
126
  int bn_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len) {
180
- OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
181
- crypto_word_t_too_small);
182
- int ret = 0;
183
- // Process the words in little-endian order.
184
- for (size_t i = 0; i < len; i++) {
185
- crypto_word_t eq = constant_time_eq_w(a[i], b[i]);
186
- crypto_word_t lt = constant_time_lt_w(a[i], b[i]);
187
- ret = constant_time_select_int(eq, ret, constant_time_select_int(lt, 1, 0));
188
- }
189
- return ret;
127
+ return bn_cmp_words_consttime(a, len, b, len) < 0;
190
128
  }
191
129
 
192
130
  int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w) {
193
- switch (bn->top) {
131
+ switch (bn_minimal_width(bn)) {
194
132
  case 1:
195
133
  return bn->d[0] == w;
196
134
  case 0:
@@ -205,14 +143,14 @@ int BN_cmp_word(const BIGNUM *a, BN_ULONG b) {
205
143
  BN_init(&b_bn);
206
144
 
207
145
  b_bn.d = &b;
208
- b_bn.top = b > 0;
146
+ b_bn.width = b > 0;
209
147
  b_bn.dmax = 1;
210
148
  b_bn.flags = BN_FLG_STATIC_DATA;
211
149
  return BN_cmp(a, &b_bn);
212
150
  }
213
151
 
214
152
  int BN_is_zero(const BIGNUM *bn) {
215
- return bn->top == 0;
153
+ return bn_minimal_width(bn) == 0;
216
154
  }
217
155
 
218
156
  int BN_is_one(const BIGNUM *bn) {
@@ -224,31 +162,39 @@ int BN_is_word(const BIGNUM *bn, BN_ULONG w) {
224
162
  }
225
163
 
226
164
  int BN_is_odd(const BIGNUM *bn) {
227
- return bn->top > 0 && (bn->d[0] & 1) == 1;
165
+ return bn->width > 0 && (bn->d[0] & 1) == 1;
228
166
  }
229
167
 
230
168
  int BN_is_pow2(const BIGNUM *bn) {
231
- if (bn->top == 0 || bn->neg) {
169
+ int width = bn_minimal_width(bn);
170
+ if (width == 0 || bn->neg) {
232
171
  return 0;
233
172
  }
234
173
 
235
- for (int i = 0; i < bn->top - 1; i++) {
174
+ for (int i = 0; i < width - 1; i++) {
236
175
  if (bn->d[i] != 0) {
237
176
  return 0;
238
177
  }
239
178
  }
240
179
 
241
- return 0 == (bn->d[bn->top-1] & (bn->d[bn->top-1] - 1));
180
+ return 0 == (bn->d[width-1] & (bn->d[width-1] - 1));
242
181
  }
243
182
 
244
183
  int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b) {
245
- if (a->top != b->top) {
246
- return 0;
247
- }
248
-
249
- int limbs_are_equal =
250
- CRYPTO_memcmp(a->d, b->d, (size_t)a->top * sizeof(a->d[0])) == 0;
251
-
252
- return constant_time_select_int(constant_time_eq_int(a->neg, b->neg),
253
- limbs_are_equal, 0);
184
+ BN_ULONG mask = 0;
185
+ // If |a| or |b| has more words than the other, all those words must be zero.
186
+ for (int i = a->width; i < b->width; i++) {
187
+ mask |= b->d[i];
188
+ }
189
+ for (int i = b->width; i < a->width; i++) {
190
+ mask |= a->d[i];
191
+ }
192
+ // Common words must match.
193
+ int min = a->width < b->width ? a->width : b->width;
194
+ for (int i = 0; i < min; i++) {
195
+ mask |= (a->d[i] ^ b->d[i]);
196
+ }
197
+ // The sign bit must match.
198
+ mask |= (a->neg ^ b->neg);
199
+ return mask == 0;
254
200
  }
@@ -155,18 +155,18 @@ static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out,
155
155
  //
156
156
  // These issues aren't specific to x86 and x86_64, so it might be worthwhile
157
157
  // to add more assembly language implementations.
158
- #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && defined(__GNUC__)
159
- __asm__ volatile (
160
- "divl %4"
161
- : "=a"(*quotient_out), "=d"(*rem_out)
162
- : "a"(n1), "d"(n0), "rm"(d0)
163
- : "cc" );
164
- #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__GNUC__)
165
- __asm__ volatile (
166
- "divq %4"
167
- : "=a"(*quotient_out), "=d"(*rem_out)
168
- : "a"(n1), "d"(n0), "rm"(d0)
169
- : "cc" );
158
+ #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && \
159
+ (defined(__GNUC__) || defined(__clang__))
160
+ __asm__ volatile("divl %4"
161
+ : "=a"(*quotient_out), "=d"(*rem_out)
162
+ : "a"(n1), "d"(n0), "rm"(d0)
163
+ : "cc");
164
+ #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && \
165
+ (defined(__GNUC__) || defined(__clang__))
166
+ __asm__ volatile("divq %4"
167
+ : "=a"(*quotient_out), "=d"(*rem_out)
168
+ : "a"(n1), "d"(n0), "rm"(d0)
169
+ : "cc");
170
170
  #else
171
171
  #if defined(BN_ULLONG)
172
172
  BN_ULLONG n = (((BN_ULLONG)n0) << BN_BITS2) | n1;
@@ -202,10 +202,16 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
202
202
  BN_ULONG d0, d1;
203
203
  int num_n, div_n;
204
204
 
205
- // Invalid zero-padding would have particularly bad consequences
206
- // so don't just rely on bn_check_top() here
207
- if ((numerator->top > 0 && numerator->d[numerator->top - 1] == 0) ||
208
- (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
205
+ // This function relies on the historical minimal-width |BIGNUM| invariant.
206
+ // It is already not constant-time (constant-time reductions should use
207
+ // Montgomery logic), so we shrink all inputs and intermediate values to
208
+ // retain the previous behavior.
209
+
210
+ // Invalid zero-padding would have particularly bad consequences.
211
+ int numerator_width = bn_minimal_width(numerator);
212
+ int divisor_width = bn_minimal_width(divisor);
213
+ if ((numerator_width > 0 && numerator->d[numerator_width - 1] == 0) ||
214
+ (divisor_width > 0 && divisor->d[divisor_width - 1] == 0)) {
209
215
  OPENSSL_PUT_ERROR(BN, BN_R_NOT_INITIALIZED);
210
216
  return 0;
211
217
  }
@@ -234,46 +240,48 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
234
240
  if (!BN_lshift(sdiv, divisor, norm_shift)) {
235
241
  goto err;
236
242
  }
243
+ bn_set_minimal_width(sdiv);
237
244
  sdiv->neg = 0;
238
245
  norm_shift += BN_BITS2;
239
246
  if (!BN_lshift(snum, numerator, norm_shift)) {
240
247
  goto err;
241
248
  }
249
+ bn_set_minimal_width(snum);
242
250
  snum->neg = 0;
243
251
 
244
252
  // Since we don't want to have special-case logic for the case where snum is
245
253
  // larger than sdiv, we pad snum with enough zeroes without changing its
246
254
  // value.
247
- if (snum->top <= sdiv->top + 1) {
248
- if (!bn_wexpand(snum, sdiv->top + 2)) {
255
+ if (snum->width <= sdiv->width + 1) {
256
+ if (!bn_wexpand(snum, sdiv->width + 2)) {
249
257
  goto err;
250
258
  }
251
- for (int i = snum->top; i < sdiv->top + 2; i++) {
259
+ for (int i = snum->width; i < sdiv->width + 2; i++) {
252
260
  snum->d[i] = 0;
253
261
  }
254
- snum->top = sdiv->top + 2;
262
+ snum->width = sdiv->width + 2;
255
263
  } else {
256
- if (!bn_wexpand(snum, snum->top + 1)) {
264
+ if (!bn_wexpand(snum, snum->width + 1)) {
257
265
  goto err;
258
266
  }
259
- snum->d[snum->top] = 0;
260
- snum->top++;
267
+ snum->d[snum->width] = 0;
268
+ snum->width++;
261
269
  }
262
270
 
263
- div_n = sdiv->top;
264
- num_n = snum->top;
271
+ div_n = sdiv->width;
272
+ num_n = snum->width;
265
273
  loop = num_n - div_n;
266
274
  // Lets setup a 'window' into snum
267
275
  // This is the part that corresponds to the current
268
276
  // 'area' being divided
269
277
  wnum.neg = 0;
270
278
  wnum.d = &(snum->d[loop]);
271
- wnum.top = div_n;
272
- // only needed when BN_ucmp messes up the values between top and max
279
+ wnum.width = div_n;
280
+ // only needed when BN_ucmp messes up the values between width and max
273
281
  wnum.dmax = snum->dmax - loop; // so we don't step out of bounds
274
282
 
275
283
  // Get the top 2 words of sdiv
276
- // div_n=sdiv->top;
284
+ // div_n=sdiv->width;
277
285
  d0 = sdiv->d[div_n - 1];
278
286
  d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
279
287
 
@@ -285,7 +293,7 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
285
293
  if (!bn_wexpand(res, loop + 1)) {
286
294
  goto err;
287
295
  }
288
- res->top = loop - 1;
296
+ res->width = loop - 1;
289
297
  resp = &(res->d[loop - 1]);
290
298
 
291
299
  // space for temp
@@ -293,9 +301,9 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
293
301
  goto err;
294
302
  }
295
303
 
296
- // if res->top == 0 then clear the neg value otherwise decrease
304
+ // if res->width == 0 then clear the neg value otherwise decrease
297
305
  // the resp pointer
298
- if (res->top == 0) {
306
+ if (res->width == 0) {
299
307
  res->neg = 0;
300
308
  } else {
301
309
  resp--;
@@ -371,7 +379,7 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
371
379
  *resp = q;
372
380
  }
373
381
 
374
- bn_correct_top(snum);
382
+ bn_set_minimal_width(snum);
375
383
 
376
384
  if (rem != NULL) {
377
385
  // Keep a copy of the neg flag in numerator because if |rem| == |numerator|
@@ -385,7 +393,7 @@ int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
385
393
  }
386
394
  }
387
395
 
388
- bn_correct_top(res);
396
+ bn_set_minimal_width(res);
389
397
  BN_CTX_end(ctx);
390
398
  return 1;
391
399
 
@@ -406,6 +414,78 @@ int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) {
406
414
  return (d->neg ? BN_sub : BN_add)(r, r, d);
407
415
  }
408
416
 
417
+ // bn_mod_sub_words sets |r| to |a| - |b| (mod |m|), using |tmp| as scratch
418
+ // space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of
419
+ // |r|, |a|, and |b| may alias.
420
+ static void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
421
+ const BN_ULONG *m, BN_ULONG *tmp, size_t num) {
422
+ // r = a - b
423
+ BN_ULONG borrow = bn_sub_words(r, a, b, num);
424
+ // tmp = a - b + m
425
+ bn_add_words(tmp, r, m, num);
426
+ bn_select_words(r, 0 - borrow, tmp /* r < 0 */, r /* r >= 0 */, num);
427
+ }
428
+
429
+ // bn_mod_add_words sets |r| to |a| + |b| (mod |m|), using |tmp| as scratch
430
+ // space. Each array is |num| words long. |a| and |b| must be < |m|. Any pair of
431
+ // |r|, |a|, and |b| may alias.
432
+ static void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b,
433
+ const BN_ULONG *m, BN_ULONG *tmp, size_t num) {
434
+ // tmp = a + b. Note the result fits in |num|+1 words. We store the extra word
435
+ // in |carry|.
436
+ BN_ULONG carry = bn_add_words(tmp, a, b, num);
437
+ // r = a + b - m. We use |bn_sub_words| to perform the bulk of the
438
+ // subtraction, and then apply the borrow to |carry|.
439
+ carry -= bn_sub_words(r, tmp, m, num);
440
+ // |a| and |b| were both fully-reduced, so we know:
441
+ //
442
+ // 0 + 0 - m <= r < m + m - m
443
+ // -m <= r < m
444
+ //
445
+ // If 0 <= |r| < |m|, |r| fits in |num| words and |carry| is zero. We then
446
+ // wish to select |r| as the answer. Otherwise -m <= r < 0 and we wish to
447
+ // return |r| + |m|, or |tmp|. |carry| must then be -1 or all ones. In both
448
+ // cases, |carry| is a suitable input to |bn_select_words|.
449
+ //
450
+ // Although |carry| may be one if |bn_add_words| returns one and
451
+ // |bn_sub_words| returns zero, this would give |r| > |m|, which violates are
452
+ // input assumptions.
453
+ assert(carry == 0 || carry == (BN_ULONG)-1);
454
+ bn_select_words(r, carry, tmp /* r < 0 */, r /* r >= 0 */, num);
455
+ }
456
+
457
+ static BIGNUM *bn_scratch_space_from_ctx(size_t width, BN_CTX *ctx) {
458
+ BIGNUM *ret = BN_CTX_get(ctx);
459
+ if (ret == NULL ||
460
+ !bn_wexpand(ret, width)) {
461
+ return NULL;
462
+ }
463
+ ret->neg = 0;
464
+ ret->width = width;
465
+ return ret;
466
+ }
467
+
468
+ // bn_resized_from_ctx returns |bn| with width at least |width| or NULL on
469
+ // error. This is so it may be used with low-level "words" functions. If
470
+ // necessary, it allocates a new |BIGNUM| with a lifetime of the current scope
471
+ // in |ctx|, so the caller does not need to explicitly free it. |bn| must fit in
472
+ // |width| words.
473
+ static const BIGNUM *bn_resized_from_ctx(const BIGNUM *bn, size_t width,
474
+ BN_CTX *ctx) {
475
+ if ((size_t)bn->width >= width) {
476
+ // Any excess words must be zero.
477
+ assert(bn_fits_in_words(bn, width));
478
+ return bn;
479
+ }
480
+ BIGNUM *ret = bn_scratch_space_from_ctx(width, ctx);
481
+ if (ret == NULL ||
482
+ !BN_copy(ret, bn) ||
483
+ !bn_resize_words(ret, width)) {
484
+ return NULL;
485
+ }
486
+ return ret;
487
+ }
488
+
409
489
  int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
410
490
  BN_CTX *ctx) {
411
491
  if (!BN_add(r, a, b)) {
@@ -416,13 +496,27 @@ int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
416
496
 
417
497
  int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
418
498
  const BIGNUM *m) {
419
- if (!BN_uadd(r, a, b)) {
420
- return 0;
421
- }
422
- if (BN_ucmp(r, m) >= 0) {
423
- return BN_usub(r, r, m);
499
+ BN_CTX *ctx = BN_CTX_new();
500
+ int ok = ctx != NULL &&
501
+ bn_mod_add_quick_ctx(r, a, b, m, ctx);
502
+ BN_CTX_free(ctx);
503
+ return ok;
504
+ }
505
+
506
+ int bn_mod_add_quick_ctx(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
507
+ const BIGNUM *m, BN_CTX *ctx) {
508
+ BN_CTX_start(ctx);
509
+ a = bn_resized_from_ctx(a, m->width, ctx);
510
+ b = bn_resized_from_ctx(b, m->width, ctx);
511
+ BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx);
512
+ int ok = a != NULL && b != NULL && tmp != NULL &&
513
+ bn_wexpand(r, m->width);
514
+ if (ok) {
515
+ bn_mod_add_words(r->d, a->d, b->d, m->d, tmp->d, m->width);
516
+ r->width = m->width;
424
517
  }
425
- return 1;
518
+ BN_CTX_end(ctx);
519
+ return ok;
426
520
  }
427
521
 
428
522
  int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
@@ -433,17 +527,29 @@ int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
433
527
  return BN_nnmod(r, r, m, ctx);
434
528
  }
435
529
 
436
- // BN_mod_sub variant that may be used if both a and b are non-negative
437
- // and less than m
530
+ int bn_mod_sub_quick_ctx(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
531
+ const BIGNUM *m, BN_CTX *ctx) {
532
+ BN_CTX_start(ctx);
533
+ a = bn_resized_from_ctx(a, m->width, ctx);
534
+ b = bn_resized_from_ctx(b, m->width, ctx);
535
+ BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx);
536
+ int ok = a != NULL && b != NULL && tmp != NULL &&
537
+ bn_wexpand(r, m->width);
538
+ if (ok) {
539
+ bn_mod_sub_words(r->d, a->d, b->d, m->d, tmp->d, m->width);
540
+ r->width = m->width;
541
+ }
542
+ BN_CTX_end(ctx);
543
+ return ok;
544
+ }
545
+
438
546
  int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
439
547
  const BIGNUM *m) {
440
- if (!BN_sub(r, a, b)) {
441
- return 0;
442
- }
443
- if (r->neg) {
444
- return BN_add(r, r, m);
445
- }
446
- return 1;
548
+ BN_CTX *ctx = BN_CTX_new();
549
+ int ok = ctx != NULL &&
550
+ bn_mod_sub_quick_ctx(r, a, b, m, ctx);
551
+ BN_CTX_free(ctx);
552
+ return ok;
447
553
  }
448
554
 
449
555
  int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
@@ -504,58 +610,33 @@ int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
504
610
  abs_m->neg = 0;
505
611
  }
506
612
 
507
- ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
613
+ ret = bn_mod_lshift_quick_ctx(r, r, n, (abs_m ? abs_m : m), ctx);
508
614
 
509
615
  BN_free(abs_m);
510
616
  return ret;
511
617
  }
512
618
 
513
- int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) {
514
- if (r != a) {
515
- if (BN_copy(r, a) == NULL) {
516
- return 0;
517
- }
619
+ int bn_mod_lshift_quick_ctx(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
620
+ BN_CTX *ctx) {
621
+ if (!BN_copy(r, a)) {
622
+ return 0;
518
623
  }
519
-
520
- while (n > 0) {
521
- int max_shift;
522
-
523
- // 0 < r < m
524
- max_shift = BN_num_bits(m) - BN_num_bits(r);
525
- // max_shift >= 0
526
-
527
- if (max_shift < 0) {
528
- OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
624
+ for (int i = 0; i < n; i++) {
625
+ if (!bn_mod_lshift1_quick_ctx(r, r, m, ctx)) {
529
626
  return 0;
530
627
  }
531
-
532
- if (max_shift > n) {
533
- max_shift = n;
534
- }
535
-
536
- if (max_shift) {
537
- if (!BN_lshift(r, r, max_shift)) {
538
- return 0;
539
- }
540
- n -= max_shift;
541
- } else {
542
- if (!BN_lshift1(r, r)) {
543
- return 0;
544
- }
545
- --n;
546
- }
547
-
548
- // BN_num_bits(r) <= BN_num_bits(m)
549
- if (BN_cmp(r, m) >= 0) {
550
- if (!BN_sub(r, r, m)) {
551
- return 0;
552
- }
553
- }
554
628
  }
555
-
556
629
  return 1;
557
630
  }
558
631
 
632
+ int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) {
633
+ BN_CTX *ctx = BN_CTX_new();
634
+ int ok = ctx != NULL &&
635
+ bn_mod_lshift_quick_ctx(r, a, n, m, ctx);
636
+ BN_CTX_free(ctx);
637
+ return ok;
638
+ }
639
+
559
640
  int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
560
641
  if (!BN_lshift1(r, a)) {
561
642
  return 0;
@@ -564,15 +645,17 @@ int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
564
645
  return BN_nnmod(r, r, m, ctx);
565
646
  }
566
647
 
567
- int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) {
568
- if (!BN_lshift1(r, a)) {
569
- return 0;
570
- }
571
- if (BN_cmp(r, m) >= 0) {
572
- return BN_sub(r, r, m);
573
- }
648
+ int bn_mod_lshift1_quick_ctx(BIGNUM *r, const BIGNUM *a, const BIGNUM *m,
649
+ BN_CTX *ctx) {
650
+ return bn_mod_add_quick_ctx(r, a, a, m, ctx);
651
+ }
574
652
 
575
- return 1;
653
+ int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) {
654
+ BN_CTX *ctx = BN_CTX_new();
655
+ int ok = ctx != NULL &&
656
+ bn_mod_lshift1_quick_ctx(r, a, m, ctx);
657
+ BN_CTX_free(ctx);
658
+ return ok;
576
659
  }
577
660
 
578
661
  BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
@@ -584,7 +667,7 @@ BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
584
667
  return (BN_ULONG) - 1;
585
668
  }
586
669
 
587
- if (a->top == 0) {
670
+ if (a->width == 0) {
588
671
  return 0;
589
672
  }
590
673
 
@@ -595,7 +678,7 @@ BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
595
678
  return (BN_ULONG) - 1;
596
679
  }
597
680
 
598
- for (i = a->top - 1; i >= 0; i--) {
681
+ for (i = a->width - 1; i >= 0; i--) {
599
682
  BN_ULONG l = a->d[i];
600
683
  BN_ULONG d;
601
684
  BN_ULONG unused_rem;
@@ -604,20 +687,13 @@ BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
604
687
  a->d[i] = d;
605
688
  }
606
689
 
607
- if ((a->top > 0) && (a->d[a->top - 1] == 0)) {
608
- a->top--;
609
- }
610
-
611
- if (a->top == 0) {
612
- a->neg = 0;
613
- }
614
-
690
+ bn_set_minimal_width(a);
615
691
  ret >>= j;
616
692
  return ret;
617
693
  }
618
694
 
619
695
  BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
620
- #ifndef BN_ULLONG
696
+ #ifndef BN_CAN_DIVIDE_ULLONG
621
697
  BN_ULONG ret = 0;
622
698
  #else
623
699
  BN_ULLONG ret = 0;
@@ -628,9 +704,9 @@ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
628
704
  return (BN_ULONG) -1;
629
705
  }
630
706
 
631
- #ifndef BN_ULLONG
632
- // If |w| is too long and we don't have |BN_ULLONG| then we need to fall back
633
- // to using |BN_div_word|.
707
+ #ifndef BN_CAN_DIVIDE_ULLONG
708
+ // If |w| is too long and we don't have |BN_ULLONG| division then we need to
709
+ // fall back to using |BN_div_word|.
634
710
  if (w > ((BN_ULONG)1 << BN_BITS4)) {
635
711
  BIGNUM *tmp = BN_dup(a);
636
712
  if (tmp == NULL) {
@@ -642,8 +718,8 @@ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
642
718
  }
643
719
  #endif
644
720
 
645
- for (i = a->top - 1; i >= 0; i--) {
646
- #ifndef BN_ULLONG
721
+ for (i = a->width - 1; i >= 0; i--) {
722
+ #ifndef BN_CAN_DIVIDE_ULLONG
647
723
  ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
648
724
  ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
649
725
  #else
@@ -654,7 +730,7 @@ BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
654
730
  }
655
731
 
656
732
  int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
657
- if (e == 0 || a->top == 0) {
733
+ if (e == 0 || a->width == 0) {
658
734
  BN_zero(r);
659
735
  return 1;
660
736
  }
@@ -662,7 +738,7 @@ int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
662
738
  size_t num_words = 1 + ((e - 1) / BN_BITS2);
663
739
 
664
740
  // If |a| definitely has less than |e| bits, just BN_copy.
665
- if ((size_t) a->top < num_words) {
741
+ if ((size_t) a->width < num_words) {
666
742
  return BN_copy(r, a) != NULL;
667
743
  }
668
744
 
@@ -683,8 +759,8 @@ int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
683
759
 
684
760
  // Fill in the remaining fields of |r|.
685
761
  r->neg = a->neg;
686
- r->top = (int) num_words;
687
- bn_correct_top(r);
762
+ r->width = (int) num_words;
763
+ bn_set_minimal_width(r);
688
764
  return 1;
689
765
  }
690
766
 
@@ -706,27 +782,27 @@ int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
706
782
  }
707
783
 
708
784
  // Clear the upper words of |r|.
709
- OPENSSL_memset(&r->d[r->top], 0, (num_words - r->top) * BN_BYTES);
785
+ OPENSSL_memset(&r->d[r->width], 0, (num_words - r->width) * BN_BYTES);
710
786
 
711
787
  // Set parameters of |r|.
712
788
  r->neg = 0;
713
- r->top = (int) num_words;
789
+ r->width = (int) num_words;
714
790
 
715
791
  // Now, invert every word. The idea here is that we want to compute 2^e-|x|,
716
792
  // which is actually equivalent to the twos-complement representation of |x|
717
793
  // in |e| bits, which is -x = ~x + 1.
718
- for (int i = 0; i < r->top; i++) {
794
+ for (int i = 0; i < r->width; i++) {
719
795
  r->d[i] = ~r->d[i];
720
796
  }
721
797
 
722
798
  // If our exponent doesn't span the top word, we have to mask the rest.
723
799
  size_t top_word_exponent = e % BN_BITS2;
724
800
  if (top_word_exponent != 0) {
725
- r->d[r->top - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
801
+ r->d[r->width - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
726
802
  }
727
803
 
728
- // Keep the correct_top invariant for BN_add.
729
- bn_correct_top(r);
804
+ // Keep the minimal-width invariant for |BIGNUM|.
805
+ bn_set_minimal_width(r);
730
806
 
731
807
  // Finally, add one, for the reason described above.
732
808
  return BN_add(r, r, BN_value_one());