You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

231 lines
6.6 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/bn.h>
  57. #include <assert.h>
  58. #include <limits.h>
  59. #include "internal.h"
  60. BIGNUM *BN_bin2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
  61. size_t num_words;
  62. unsigned m;
  63. BN_ULONG word = 0;
  64. BIGNUM *bn = NULL;
  65. if (ret == NULL) {
  66. ret = bn = BN_new();
  67. }
  68. if (ret == NULL) {
  69. return NULL;
  70. }
  71. if (len == 0) {
  72. ret->width = 0;
  73. return ret;
  74. }
  75. num_words = ((len - 1) / BN_BYTES) + 1;
  76. m = (len - 1) % BN_BYTES;
  77. if (!bn_wexpand(ret, num_words)) {
  78. if (bn) {
  79. BN_free(bn);
  80. }
  81. return NULL;
  82. }
  83. // |bn_wexpand| must check bounds on |num_words| to write it into
  84. // |ret->dmax|.
  85. assert(num_words <= INT_MAX);
  86. ret->width = (int)num_words;
  87. ret->neg = 0;
  88. while (len--) {
  89. word = (word << 8) | *(in++);
  90. if (m-- == 0) {
  91. ret->d[--num_words] = word;
  92. word = 0;
  93. m = BN_BYTES - 1;
  94. }
  95. }
  96. return ret;
  97. }
  98. BIGNUM *BN_le2bn(const uint8_t *in, size_t len, BIGNUM *ret) {
  99. BIGNUM *bn = NULL;
  100. if (ret == NULL) {
  101. bn = BN_new();
  102. ret = bn;
  103. }
  104. if (ret == NULL) {
  105. return NULL;
  106. }
  107. if (len == 0) {
  108. ret->width = 0;
  109. ret->neg = 0;
  110. return ret;
  111. }
  112. // Reserve enough space in |ret|.
  113. size_t num_words = ((len - 1) / BN_BYTES) + 1;
  114. if (!bn_wexpand(ret, num_words)) {
  115. BN_free(bn);
  116. return NULL;
  117. }
  118. ret->width = num_words;
  119. // Make sure the top bytes will be zeroed.
  120. ret->d[num_words - 1] = 0;
  121. // We only support little-endian platforms, so we can simply memcpy the
  122. // internal representation.
  123. OPENSSL_memcpy(ret->d, in, len);
  124. return ret;
  125. }
  126. size_t BN_bn2bin(const BIGNUM *in, uint8_t *out) {
  127. size_t n, i;
  128. BN_ULONG l;
  129. n = i = BN_num_bytes(in);
  130. while (i--) {
  131. l = in->d[i / BN_BYTES];
  132. *(out++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
  133. }
  134. return n;
  135. }
  136. static int fits_in_bytes(const uint8_t *bytes, size_t num_bytes, size_t len) {
  137. uint8_t mask = 0;
  138. for (size_t i = len; i < num_bytes; i++) {
  139. mask |= bytes[i];
  140. }
  141. return mask == 0;
  142. }
  143. int BN_bn2le_padded(uint8_t *out, size_t len, const BIGNUM *in) {
  144. const uint8_t *bytes = (const uint8_t *)in->d;
  145. size_t num_bytes = in->width * BN_BYTES;
  146. if (len < num_bytes) {
  147. if (!fits_in_bytes(bytes, num_bytes, len)) {
  148. return 0;
  149. }
  150. num_bytes = len;
  151. }
  152. // We only support little-endian platforms, so we can simply memcpy into the
  153. // internal representation.
  154. OPENSSL_memcpy(out, bytes, num_bytes);
  155. // Pad out the rest of the buffer with zeroes.
  156. OPENSSL_memset(out + num_bytes, 0, len - num_bytes);
  157. return 1;
  158. }
  159. int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
  160. const uint8_t *bytes = (const uint8_t *)in->d;
  161. size_t num_bytes = in->width * BN_BYTES;
  162. if (len < num_bytes) {
  163. if (!fits_in_bytes(bytes, num_bytes, len)) {
  164. return 0;
  165. }
  166. num_bytes = len;
  167. }
  168. // We only support little-endian platforms, so we can simply write the buffer
  169. // in reverse.
  170. for (size_t i = 0; i < num_bytes; i++) {
  171. out[len - i - 1] = bytes[i];
  172. }
  173. // Pad out the rest of the buffer with zeroes.
  174. OPENSSL_memset(out, 0, len - num_bytes);
  175. return 1;
  176. }
  177. BN_ULONG BN_get_word(const BIGNUM *bn) {
  178. switch (bn_minimal_width(bn)) {
  179. case 0:
  180. return 0;
  181. case 1:
  182. return bn->d[0];
  183. default:
  184. return BN_MASK2;
  185. }
  186. }
  187. int BN_get_u64(const BIGNUM *bn, uint64_t *out) {
  188. switch (bn_minimal_width(bn)) {
  189. case 0:
  190. *out = 0;
  191. return 1;
  192. case 1:
  193. *out = bn->d[0];
  194. return 1;
  195. #if defined(OPENSSL_32_BIT)
  196. case 2:
  197. *out = (uint64_t) bn->d[0] | (((uint64_t) bn->d[1]) << 32);
  198. return 1;
  199. #endif
  200. default:
  201. return 0;
  202. }
  203. }