3
0

tls.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2017 Denys Vlasenko
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  5. */
  6. /* Interface glue between bbox code and minimally tweaked matrixssl
  7. * code. All C files (matrixssl and bbox (ones which need TLS))
  8. * include this file, and guaranteed to see a consistent API,
  9. * defines, types, etc.
  10. */
  11. #include "libbb.h"
  12. /* Config tweaks */
  13. #define HAVE_NATIVE_INT64
  14. #undef USE_1024_KEY_SPEED_OPTIMIZATIONS
  15. #undef USE_2048_KEY_SPEED_OPTIMIZATIONS
  16. #define USE_AES
  17. #undef USE_AES_CBC_EXTERNAL
  18. #undef USE_AES_CCM
  19. #undef USE_AES_GCM
  20. #undef USE_3DES
  21. #undef USE_ARC4
  22. #undef USE_IDEA
  23. #undef USE_RC2
  24. #undef USE_SEED
  25. /* pstm: multiprecision numbers */
  26. #undef DISABLE_PSTM
  27. #if defined(__GNUC__) && defined(__i386__)
  28. /* PSTM_X86 works correctly. +25 bytes. */
  29. # define PSTM_32BIT
  30. # define PSTM_X86
  31. #endif
  32. //#if defined(__GNUC__) && defined(__x86_64__)
  33. // /* PSTM_X86_64 works correctly, but +782 bytes. */
  34. // /* Looks like most of the growth is because of PSTM_64BIT. */
  35. //# define PSTM_64BIT
  36. //# define PSTM_X86_64
  37. //#endif
  38. //#if SOME_COND #define PSTM_MIPS, #define PSTM_32BIT
  39. //#if SOME_COND #define PSTM_ARM, #define PSTM_32BIT
  40. #define PS_SUCCESS 0
  41. #define PS_FAILURE -1
  42. #define PS_ARG_FAIL -6 /* Failure due to bad function param */
  43. #define PS_PLATFORM_FAIL -7 /* Failure as a result of system call error */
  44. #define PS_MEM_FAIL -8 /* Failure to allocate requested memory */
  45. #define PS_LIMIT_FAIL -9 /* Failure on sanity/limit tests */
  46. #define PS_TRUE 1
  47. #define PS_FALSE 0
  48. #if BB_BIG_ENDIAN
  49. # define ENDIAN_BIG 1
  50. # undef ENDIAN_LITTLE
  51. //#???? ENDIAN_32BITWORD
  52. // controls only STORE32L, which we don't use
  53. #else
  54. # define ENDIAN_LITTLE 1
  55. # undef ENDIAN_BIG
  56. #endif
  57. typedef uint64_t uint64;
  58. typedef int64_t int64;
  59. typedef uint32_t uint32;
  60. typedef int32_t int32;
  61. typedef uint16_t uint16;
  62. typedef int16_t int16;
  63. //typedef char psPool_t;
  64. //#ifdef PS_PUBKEY_OPTIMIZE_FOR_SMALLER_RAM
  65. #define PS_EXPTMOD_WINSIZE 3
  66. //#ifdef PS_PUBKEY_OPTIMIZE_FOR_FASTER_SPEED
  67. //#define PS_EXPTMOD_WINSIZE 5
  68. #define PUBKEY_TYPE 0x01
  69. #define PRIVKEY_TYPE 0x02
  70. #define AES_BLOCK_SIZE 16
  71. void tls_get_random(void *buf, unsigned len) FAST_FUNC;
  72. void xorbuf(void* buf, const void* mask, unsigned count) FAST_FUNC;
  73. #define ALIGNED_long ALIGNED(sizeof(long))
  74. void xorbuf_aligned_AES_BLOCK_SIZE(void* buf, const void* mask) FAST_FUNC;
  75. #define matrixCryptoGetPrngData(buf, len, userPtr) (tls_get_random(buf, len), PS_SUCCESS)
  76. #define psFree(p, pool) free(p)
  77. #define psTraceCrypto(msg) bb_simple_error_msg_and_die(msg)
  78. /* Secure zerofill */
  79. #define memset_s(A,B,C,D) memset((A),(C),(D))
  80. /* Constant time memory comparison */
  81. #define memcmpct(s1, s2, len) memcmp((s1), (s2), (len))
  82. #undef min
  83. #define min(x, y) ((x) < (y) ? (x) : (y))
  84. #include "tls_pstm.h"
  85. #include "tls_aes.h"
  86. #include "tls_aesgcm.h"
  87. #include "tls_rsa.h"
  88. #define EC_CURVE_KEYSIZE 32
  89. #define P256_KEYSIZE 32
  90. #define CURVE25519_KEYSIZE 32
  91. void curve_x25519_compute_pubkey_and_premaster(
  92. uint8_t *pubkey32, uint8_t *premaster32,
  93. const uint8_t *peerkey32) FAST_FUNC;
  94. void curve_P256_compute_pubkey_and_premaster(
  95. uint8_t *pubkey2x32, uint8_t *premaster32,
  96. const uint8_t *peerkey2x32) FAST_FUNC;
  97. void curve_P256_compute_pubkey_and_premaster_NEW(
  98. uint8_t *pubkey2x32, uint8_t *premaster32,
  99. const uint8_t *peerkey2x32) FAST_FUNC;