Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

382 lignes
16 KiB

  1. /* ====================================================================
  2. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ==================================================================== */
  48. #ifndef OPENSSL_HEADER_MODES_INTERNAL_H
  49. #define OPENSSL_HEADER_MODES_INTERNAL_H
  50. #include <openssl/base.h>
  51. #include <string.h>
  52. #include "../internal.h"
  53. #if defined(__cplusplus)
  54. extern "C" {
  55. #endif
  56. #define asm __asm__
  57. #define STRICT_ALIGNMENT 1
  58. #if defined(OPENSSL_X86_64) || defined(OPENSSL_X86) || defined(OPENSSL_AARCH64)
  59. #undef STRICT_ALIGNMENT
  60. #define STRICT_ALIGNMENT 0
  61. #endif
  62. #if defined(__GNUC__) && __GNUC__ >= 2
  63. static inline uint32_t CRYPTO_bswap4(uint32_t x) {
  64. return __builtin_bswap32(x);
  65. }
  66. static inline uint64_t CRYPTO_bswap8(uint64_t x) {
  67. return __builtin_bswap64(x);
  68. }
  69. #elif defined(_MSC_VER)
  70. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  71. #include <intrin.h>
  72. OPENSSL_MSVC_PRAGMA(warning(pop))
  73. #pragma intrinsic(_byteswap_uint64, _byteswap_ulong)
  74. static inline uint32_t CRYPTO_bswap4(uint32_t x) {
  75. return _byteswap_ulong(x);
  76. }
  77. static inline uint64_t CRYPTO_bswap8(uint64_t x) {
  78. return _byteswap_uint64(x);
  79. }
  80. #else
  81. static inline uint32_t CRYPTO_bswap4(uint32_t x) {
  82. x = (x >> 16) | (x << 16);
  83. x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);
  84. return x;
  85. }
  86. static inline uint64_t CRYPTO_bswap8(uint64_t x) {
  87. return CRYPTO_bswap4(x >> 32) | (((uint64_t)CRYPTO_bswap4(x)) << 32);
  88. }
  89. #endif
  90. static inline uint32_t GETU32(const void *in) {
  91. uint32_t v;
  92. OPENSSL_memcpy(&v, in, sizeof(v));
  93. return CRYPTO_bswap4(v);
  94. }
  95. static inline void PUTU32(void *out, uint32_t v) {
  96. v = CRYPTO_bswap4(v);
  97. OPENSSL_memcpy(out, &v, sizeof(v));
  98. }
  99. static inline uint32_t GETU32_aligned(const void *in) {
  100. const char *alias = (const char *) in;
  101. return CRYPTO_bswap4(*((const uint32_t *) alias));
  102. }
  103. static inline void PUTU32_aligned(void *in, uint32_t v) {
  104. char *alias = (char *) in;
  105. *((uint32_t *) alias) = CRYPTO_bswap4(v);
  106. }
  107. /* block128_f is the type of a 128-bit, block cipher. */
  108. typedef void (*block128_f)(const uint8_t in[16], uint8_t out[16],
  109. const void *key);
  110. /* GCM definitions */
  111. typedef struct { uint64_t hi,lo; } u128;
  112. /* gmult_func multiplies |Xi| by the GCM key and writes the result back to
  113. * |Xi|. */
  114. typedef void (*gmult_func)(uint64_t Xi[2], const u128 Htable[16]);
  115. /* ghash_func repeatedly multiplies |Xi| by the GCM key and adds in blocks from
  116. * |inp|. The result is written back to |Xi| and the |len| argument must be a
  117. * multiple of 16. */
  118. typedef void (*ghash_func)(uint64_t Xi[2], const u128 Htable[16],
  119. const uint8_t *inp, size_t len);
  120. /* This differs from upstream's |gcm128_context| in that it does not have the
  121. * |key| pointer, in order to make it |memcpy|-friendly. Rather the key is
  122. * passed into each call that needs it. */
  123. struct gcm128_context {
  124. /* Following 6 names follow names in GCM specification */
  125. union {
  126. uint64_t u[2];
  127. uint32_t d[4];
  128. uint8_t c[16];
  129. size_t t[16 / sizeof(size_t)];
  130. } Yi, EKi, EK0, len, Xi;
  131. /* Note that the order of |Xi|, |H| and |Htable| is fixed by the MOVBE-based,
  132. * x86-64, GHASH assembly. */
  133. u128 H;
  134. u128 Htable[16];
  135. gmult_func gmult;
  136. ghash_func ghash;
  137. unsigned int mres, ares;
  138. block128_f block;
  139. };
  140. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  141. /* crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is
  142. * used. */
  143. int crypto_gcm_clmul_enabled(void);
  144. #endif
  145. /* CTR. */
  146. /* ctr128_f is the type of a function that performs CTR-mode encryption. */
  147. typedef void (*ctr128_f)(const uint8_t *in, uint8_t *out, size_t blocks,
  148. const void *key, const uint8_t ivec[16]);
  149. /* CRYPTO_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode)
  150. * |len| bytes from |in| to |out| using |block| in counter mode. There's no
  151. * requirement that |len| be a multiple of any value and any partial blocks are
  152. * stored in |ecount_buf| and |*num|, which must be zeroed before the initial
  153. * call. The counter is a 128-bit, big-endian value in |ivec| and is
  154. * incremented by this function. */
  155. void CRYPTO_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  156. const void *key, uint8_t ivec[16],
  157. uint8_t ecount_buf[16], unsigned *num,
  158. block128_f block);
  159. /* CRYPTO_ctr128_encrypt_ctr32 acts like |CRYPTO_ctr128_encrypt| but takes
  160. * |ctr|, a function that performs CTR mode but only deals with the lower 32
  161. * bits of the counter. This is useful when |ctr| can be an optimised
  162. * function. */
  163. void CRYPTO_ctr128_encrypt_ctr32(const uint8_t *in, uint8_t *out, size_t len,
  164. const void *key, uint8_t ivec[16],
  165. uint8_t ecount_buf[16], unsigned *num,
  166. ctr128_f ctr);
  167. #if !defined(OPENSSL_NO_ASM) && \
  168. (defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
  169. void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t blocks,
  170. const void *key, const uint8_t *ivec);
  171. #endif
  172. /* GCM.
  173. *
  174. * This API differs from the upstream API slightly. The |GCM128_CONTEXT| does
  175. * not have a |key| pointer that points to the key as upstream's version does.
  176. * Instead, every function takes a |key| parameter. This way |GCM128_CONTEXT|
  177. * can be safely copied. */
  178. typedef struct gcm128_context GCM128_CONTEXT;
  179. /* CRYPTO_ghash_init writes a precomputed table of powers of |gcm_key| to
  180. * |out_table| and sets |*out_mult| and |*out_hash| to (potentially hardware
  181. * accelerated) functions for performing operations in the GHASH field. */
  182. void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash,
  183. u128 *out_key, u128 out_table[16],
  184. const uint8_t *gcm_key);
  185. /* CRYPTO_gcm128_init initialises |ctx| to use |block| (typically AES) with
  186. * the given key. */
  187. OPENSSL_EXPORT void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, const void *key,
  188. block128_f block);
  189. /* CRYPTO_gcm128_setiv sets the IV (nonce) for |ctx|. The |key| must be the
  190. * same key that was passed to |CRYPTO_gcm128_init|. */
  191. OPENSSL_EXPORT void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const void *key,
  192. const uint8_t *iv, size_t iv_len);
  193. /* CRYPTO_gcm128_aad sets the authenticated data for an instance of GCM.
  194. * This must be called before and data is encrypted. It returns one on success
  195. * and zero otherwise. */
  196. OPENSSL_EXPORT int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad,
  197. size_t len);
  198. /* CRYPTO_gcm128_encrypt encrypts |len| bytes from |in| to |out|. The |key|
  199. * must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one
  200. * on success and zero otherwise. */
  201. OPENSSL_EXPORT int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const void *key,
  202. const uint8_t *in, uint8_t *out,
  203. size_t len);
  204. /* CRYPTO_gcm128_decrypt decrypts |len| bytes from |in| to |out|. The |key|
  205. * must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one
  206. * on success and zero otherwise. */
  207. OPENSSL_EXPORT int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const void *key,
  208. const uint8_t *in, uint8_t *out,
  209. size_t len);
  210. /* CRYPTO_gcm128_encrypt_ctr32 encrypts |len| bytes from |in| to |out| using
  211. * a CTR function that only handles the bottom 32 bits of the nonce, like
  212. * |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was
  213. * passed to |CRYPTO_gcm128_init|. It returns one on success and zero
  214. * otherwise. */
  215. OPENSSL_EXPORT int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  216. const void *key,
  217. const uint8_t *in, uint8_t *out,
  218. size_t len, ctr128_f stream);
  219. /* CRYPTO_gcm128_decrypt_ctr32 decrypts |len| bytes from |in| to |out| using
  220. * a CTR function that only handles the bottom 32 bits of the nonce, like
  221. * |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was
  222. * passed to |CRYPTO_gcm128_init|. It returns one on success and zero
  223. * otherwise. */
  224. OPENSSL_EXPORT int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  225. const void *key,
  226. const uint8_t *in, uint8_t *out,
  227. size_t len, ctr128_f stream);
  228. /* CRYPTO_gcm128_finish calculates the authenticator and compares it against
  229. * |len| bytes of |tag|. It returns one on success and zero otherwise. */
  230. OPENSSL_EXPORT int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag,
  231. size_t len);
  232. /* CRYPTO_gcm128_tag calculates the authenticator and copies it into |tag|.
  233. * The minimum of |len| and 16 bytes are copied into |tag|. */
  234. OPENSSL_EXPORT void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, uint8_t *tag,
  235. size_t len);
  236. /* CBC. */
  237. /* cbc128_f is the type of a function that performs CBC-mode encryption. */
  238. typedef void (*cbc128_f)(const uint8_t *in, uint8_t *out, size_t len,
  239. const void *key, uint8_t ivec[16], int enc);
  240. /* CRYPTO_cbc128_encrypt encrypts |len| bytes from |in| to |out| using the
  241. * given IV and block cipher in CBC mode. The input need not be a multiple of
  242. * 128 bits long, but the output will round up to the nearest 128 bit multiple,
  243. * zero padding the input if needed. The IV will be updated on return. */
  244. void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  245. const void *key, uint8_t ivec[16], block128_f block);
  246. /* CRYPTO_cbc128_decrypt decrypts |len| bytes from |in| to |out| using the
  247. * given IV and block cipher in CBC mode. If |len| is not a multiple of 128
  248. * bits then only that many bytes will be written, but a multiple of 128 bits
  249. * is always read from |in|. The IV will be updated on return. */
  250. void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  251. const void *key, uint8_t ivec[16], block128_f block);
  252. /* OFB. */
  253. /* CRYPTO_ofb128_encrypt encrypts (or decrypts, it's the same with OFB mode)
  254. * |len| bytes from |in| to |out| using |block| in OFB mode. There's no
  255. * requirement that |len| be a multiple of any value and any partial blocks are
  256. * stored in |ivec| and |*num|, the latter must be zero before the initial
  257. * call. */
  258. void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out,
  259. size_t len, const void *key, uint8_t ivec[16],
  260. unsigned *num, block128_f block);
  261. /* CFB. */
  262. /* CRYPTO_cfb128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  263. * from |in| to |out| using |block| in CFB mode. There's no requirement that
  264. * |len| be a multiple of any value and any partial blocks are stored in |ivec|
  265. * and |*num|, the latter must be zero before the initial call. */
  266. void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  267. const void *key, uint8_t ivec[16], unsigned *num,
  268. int enc, block128_f block);
  269. /* CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  270. * from |in| to |out| using |block| in CFB-8 mode. Prior to the first call
  271. * |num| should be set to zero. */
  272. void CRYPTO_cfb128_8_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  273. const void *key, uint8_t ivec[16], unsigned *num,
  274. int enc, block128_f block);
  275. /* CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  276. * from |in| to |out| using |block| in CFB-1 mode. Prior to the first call
  277. * |num| should be set to zero. */
  278. void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
  279. const void *key, uint8_t ivec[16], unsigned *num,
  280. int enc, block128_f block);
  281. size_t CRYPTO_cts128_encrypt_block(const uint8_t *in, uint8_t *out, size_t len,
  282. const void *key, uint8_t ivec[16],
  283. block128_f block);
  284. /* POLYVAL.
  285. *
  286. * POLYVAL is a polynomial authenticator that operates over a field very
  287. * similar to the one that GHASH uses. See
  288. * https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02#section-3. */
  289. typedef union {
  290. uint64_t u[2];
  291. uint8_t c[16];
  292. } polyval_block;
  293. struct polyval_ctx {
  294. /* Note that the order of |S|, |H| and |Htable| is fixed by the MOVBE-based,
  295. * x86-64, GHASH assembly. */
  296. polyval_block S;
  297. u128 H;
  298. u128 Htable[16];
  299. gmult_func gmult;
  300. ghash_func ghash;
  301. };
  302. /* CRYPTO_POLYVAL_init initialises |ctx| using |key|. */
  303. void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16]);
  304. /* CRYPTO_POLYVAL_update_blocks updates the accumulator in |ctx| given the
  305. * blocks from |in|. Only a whole number of blocks can be processed so |in_len|
  306. * must be a multiple of 16. */
  307. void CRYPTO_POLYVAL_update_blocks(struct polyval_ctx *ctx, const uint8_t *in,
  308. size_t in_len);
  309. /* CRYPTO_POLYVAL_finish writes the accumulator from |ctx| to |out|. */
  310. void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16]);
  311. #if defined(__cplusplus)
  312. } /* extern C */
  313. #endif
  314. #endif /* OPENSSL_HEADER_MODES_INTERNAL_H */