internal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 STRICT_ALIGNMENT 1
  57. #if defined(OPENSSL_X86_64) || defined(OPENSSL_X86) || defined(OPENSSL_AARCH64)
  58. #undef STRICT_ALIGNMENT
  59. #define STRICT_ALIGNMENT 0
  60. #endif
  61. static inline uint32_t GETU32(const void *in) {
  62. uint32_t v;
  63. OPENSSL_memcpy(&v, in, sizeof(v));
  64. return CRYPTO_bswap4(v);
  65. }
  66. static inline void PUTU32(void *out, uint32_t v) {
  67. v = CRYPTO_bswap4(v);
  68. OPENSSL_memcpy(out, &v, sizeof(v));
  69. }
  70. static inline size_t load_word_le(const void *in) {
  71. size_t v;
  72. OPENSSL_memcpy(&v, in, sizeof(v));
  73. return v;
  74. }
  75. static inline void store_word_le(void *out, size_t v) {
  76. OPENSSL_memcpy(out, &v, sizeof(v));
  77. }
  78. // block128_f is the type of a 128-bit, block cipher.
  79. typedef void (*block128_f)(const uint8_t in[16], uint8_t out[16],
  80. const void *key);
  81. // GCM definitions
  82. typedef struct { uint64_t hi,lo; } u128;
  83. // gmult_func multiplies |Xi| by the GCM key and writes the result back to
  84. // |Xi|.
  85. typedef void (*gmult_func)(uint64_t Xi[2], const u128 Htable[16]);
  86. // ghash_func repeatedly multiplies |Xi| by the GCM key and adds in blocks from
  87. // |inp|. The result is written back to |Xi| and the |len| argument must be a
  88. // multiple of 16.
  89. typedef void (*ghash_func)(uint64_t Xi[2], const u128 Htable[16],
  90. const uint8_t *inp, size_t len);
  91. // This differs from upstream's |gcm128_context| in that it does not have the
  92. // |key| pointer, in order to make it |memcpy|-friendly. Rather the key is
  93. // passed into each call that needs it.
  94. struct gcm128_context {
  95. // Following 6 names follow names in GCM specification
  96. union {
  97. uint64_t u[2];
  98. uint32_t d[4];
  99. uint8_t c[16];
  100. size_t t[16 / sizeof(size_t)];
  101. } Yi, EKi, EK0, len, Xi;
  102. // Note that the order of |Xi|, |H| and |Htable| is fixed by the MOVBE-based,
  103. // x86-64, GHASH assembly.
  104. u128 H;
  105. u128 Htable[16];
  106. gmult_func gmult;
  107. ghash_func ghash;
  108. unsigned int mres, ares;
  109. block128_f block;
  110. // use_aesni_gcm_crypt is true if this context should use the assembly
  111. // functions |aesni_gcm_encrypt| and |aesni_gcm_decrypt| to process data.
  112. unsigned use_aesni_gcm_crypt:1;
  113. };
  114. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  115. // crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is
  116. // used.
  117. int crypto_gcm_clmul_enabled(void);
  118. #endif
  119. // CTR.
  120. // ctr128_f is the type of a function that performs CTR-mode encryption.
  121. typedef void (*ctr128_f)(const uint8_t *in, uint8_t *out, size_t blocks,
  122. const void *key, const uint8_t ivec[16]);
  123. // CRYPTO_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode)
  124. // |len| bytes from |in| to |out| using |block| in counter mode. There's no
  125. // requirement that |len| be a multiple of any value and any partial blocks are
  126. // stored in |ecount_buf| and |*num|, which must be zeroed before the initial
  127. // call. The counter is a 128-bit, big-endian value in |ivec| and is
  128. // incremented by this function.
  129. void CRYPTO_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  130. const void *key, uint8_t ivec[16],
  131. uint8_t ecount_buf[16], unsigned *num,
  132. block128_f block);
  133. // CRYPTO_ctr128_encrypt_ctr32 acts like |CRYPTO_ctr128_encrypt| but takes
  134. // |ctr|, a function that performs CTR mode but only deals with the lower 32
  135. // bits of the counter. This is useful when |ctr| can be an optimised
  136. // function.
  137. void CRYPTO_ctr128_encrypt_ctr32(const uint8_t *in, uint8_t *out, size_t len,
  138. const void *key, uint8_t ivec[16],
  139. uint8_t ecount_buf[16], unsigned *num,
  140. ctr128_f ctr);
  141. #if !defined(OPENSSL_NO_ASM) && \
  142. (defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
  143. void aesni_ctr32_encrypt_blocks(const uint8_t *in, uint8_t *out, size_t blocks,
  144. const void *key, const uint8_t *ivec);
  145. #endif
  146. // GCM.
  147. //
  148. // This API differs from the upstream API slightly. The |GCM128_CONTEXT| does
  149. // not have a |key| pointer that points to the key as upstream's version does.
  150. // Instead, every function takes a |key| parameter. This way |GCM128_CONTEXT|
  151. // can be safely copied.
  152. typedef struct gcm128_context GCM128_CONTEXT;
  153. // CRYPTO_ghash_init writes a precomputed table of powers of |gcm_key| to
  154. // |out_table| and sets |*out_mult| and |*out_hash| to (potentially hardware
  155. // accelerated) functions for performing operations in the GHASH field. If the
  156. // AVX implementation was used |*out_is_avx| will be true.
  157. void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash,
  158. u128 *out_key, u128 out_table[16], int *out_is_avx,
  159. const uint8_t *gcm_key);
  160. // CRYPTO_gcm128_init initialises |ctx| to use |block| (typically AES) with
  161. // the given key. |is_aesni_encrypt| is one if |block| is |aesni_encrypt|.
  162. OPENSSL_EXPORT void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, const void *key,
  163. block128_f block, int is_aesni_encrypt);
  164. // CRYPTO_gcm128_setiv sets the IV (nonce) for |ctx|. The |key| must be the
  165. // same key that was passed to |CRYPTO_gcm128_init|.
  166. OPENSSL_EXPORT void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const void *key,
  167. const uint8_t *iv, size_t iv_len);
  168. // CRYPTO_gcm128_aad sets the authenticated data for an instance of GCM.
  169. // This must be called before and data is encrypted. It returns one on success
  170. // and zero otherwise.
  171. OPENSSL_EXPORT int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad,
  172. size_t len);
  173. // CRYPTO_gcm128_encrypt encrypts |len| bytes from |in| to |out|. The |key|
  174. // must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one
  175. // on success and zero otherwise.
  176. OPENSSL_EXPORT int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const void *key,
  177. const uint8_t *in, uint8_t *out,
  178. size_t len);
  179. // CRYPTO_gcm128_decrypt decrypts |len| bytes from |in| to |out|. The |key|
  180. // must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one
  181. // on success and zero otherwise.
  182. OPENSSL_EXPORT int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const void *key,
  183. const uint8_t *in, uint8_t *out,
  184. size_t len);
  185. // CRYPTO_gcm128_encrypt_ctr32 encrypts |len| bytes from |in| to |out| using
  186. // a CTR function that only handles the bottom 32 bits of the nonce, like
  187. // |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was
  188. // passed to |CRYPTO_gcm128_init|. It returns one on success and zero
  189. // otherwise.
  190. OPENSSL_EXPORT int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  191. const void *key,
  192. const uint8_t *in, uint8_t *out,
  193. size_t len, ctr128_f stream);
  194. // CRYPTO_gcm128_decrypt_ctr32 decrypts |len| bytes from |in| to |out| using
  195. // a CTR function that only handles the bottom 32 bits of the nonce, like
  196. // |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was
  197. // passed to |CRYPTO_gcm128_init|. It returns one on success and zero
  198. // otherwise.
  199. OPENSSL_EXPORT int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  200. const void *key,
  201. const uint8_t *in, uint8_t *out,
  202. size_t len, ctr128_f stream);
  203. // CRYPTO_gcm128_finish calculates the authenticator and compares it against
  204. // |len| bytes of |tag|. It returns one on success and zero otherwise.
  205. OPENSSL_EXPORT int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag,
  206. size_t len);
  207. // CRYPTO_gcm128_tag calculates the authenticator and copies it into |tag|.
  208. // The minimum of |len| and 16 bytes are copied into |tag|.
  209. OPENSSL_EXPORT void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, uint8_t *tag,
  210. size_t len);
  211. // CCM.
  212. typedef struct ccm128_context {
  213. block128_f block;
  214. ctr128_f ctr;
  215. unsigned M, L;
  216. } CCM128_CONTEXT;
  217. // CRYPTO_ccm128_init initialises |ctx| to use |block| (typically AES) with the
  218. // specified |M| and |L| parameters. It returns one on success and zero if |M|
  219. // or |L| is invalid.
  220. int CRYPTO_ccm128_init(CCM128_CONTEXT *ctx, const void *key, block128_f block,
  221. ctr128_f ctr, unsigned M, unsigned L);
  222. // CRYPTO_ccm128_max_input returns the maximum input length accepted by |ctx|.
  223. size_t CRYPTO_ccm128_max_input(const CCM128_CONTEXT *ctx);
  224. // CRYPTO_ccm128_encrypt encrypts |len| bytes from |in| to |out| writing the tag
  225. // to |out_tag|. |key| must be the same key that was passed to
  226. // |CRYPTO_ccm128_init|. It returns one on success and zero otherwise.
  227. int CRYPTO_ccm128_encrypt(const CCM128_CONTEXT *ctx, const void *key,
  228. uint8_t *out, uint8_t *out_tag, size_t tag_len,
  229. const uint8_t *nonce, size_t nonce_len,
  230. const uint8_t *in, size_t len, const uint8_t *aad,
  231. size_t aad_len);
  232. // CRYPTO_ccm128_decrypt decrypts |len| bytes from |in| to |out|, writing the
  233. // expected tag to |out_tag|. |key| must be the same key that was passed to
  234. // |CRYPTO_ccm128_init|. It returns one on success and zero otherwise.
  235. int CRYPTO_ccm128_decrypt(const CCM128_CONTEXT *ctx, const void *key,
  236. uint8_t *out, uint8_t *out_tag, size_t tag_len,
  237. const uint8_t *nonce, size_t nonce_len,
  238. const uint8_t *in, size_t len, const uint8_t *aad,
  239. size_t aad_len);
  240. // CBC.
  241. // cbc128_f is the type of a function that performs CBC-mode encryption.
  242. typedef void (*cbc128_f)(const uint8_t *in, uint8_t *out, size_t len,
  243. const void *key, uint8_t ivec[16], int enc);
  244. // CRYPTO_cbc128_encrypt encrypts |len| bytes from |in| to |out| using the
  245. // given IV and block cipher in CBC mode. The input need not be a multiple of
  246. // 128 bits long, but the output will round up to the nearest 128 bit multiple,
  247. // zero padding the input if needed. The IV will be updated on return.
  248. void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  249. const void *key, uint8_t ivec[16], block128_f block);
  250. // CRYPTO_cbc128_decrypt decrypts |len| bytes from |in| to |out| using the
  251. // given IV and block cipher in CBC mode. If |len| is not a multiple of 128
  252. // bits then only that many bytes will be written, but a multiple of 128 bits
  253. // is always read from |in|. The IV will be updated on return.
  254. void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  255. const void *key, uint8_t ivec[16], block128_f block);
  256. // OFB.
  257. // CRYPTO_ofb128_encrypt encrypts (or decrypts, it's the same with OFB mode)
  258. // |len| bytes from |in| to |out| using |block| in OFB mode. There's no
  259. // requirement that |len| be a multiple of any value and any partial blocks are
  260. // stored in |ivec| and |*num|, the latter must be zero before the initial
  261. // call.
  262. void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out,
  263. size_t len, const void *key, uint8_t ivec[16],
  264. unsigned *num, block128_f block);
  265. // CFB.
  266. // CRYPTO_cfb128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  267. // from |in| to |out| using |block| in CFB mode. There's no requirement that
  268. // |len| be a multiple of any value and any partial blocks are stored in |ivec|
  269. // and |*num|, the latter must be zero before the initial call.
  270. void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  271. const void *key, uint8_t ivec[16], unsigned *num,
  272. int enc, block128_f block);
  273. // CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  274. // from |in| to |out| using |block| in CFB-8 mode. Prior to the first call
  275. // |num| should be set to zero.
  276. void CRYPTO_cfb128_8_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  277. const void *key, uint8_t ivec[16], unsigned *num,
  278. int enc, block128_f block);
  279. // CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  280. // from |in| to |out| using |block| in CFB-1 mode. Prior to the first call
  281. // |num| should be set to zero.
  282. void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
  283. const void *key, uint8_t ivec[16], unsigned *num,
  284. int enc, block128_f block);
  285. size_t CRYPTO_cts128_encrypt_block(const uint8_t *in, uint8_t *out, size_t len,
  286. const void *key, uint8_t ivec[16],
  287. block128_f block);
  288. // POLYVAL.
  289. //
  290. // POLYVAL is a polynomial authenticator that operates over a field very
  291. // similar to the one that GHASH uses. See
  292. // https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02#section-3.
  293. typedef union {
  294. uint64_t u[2];
  295. uint8_t c[16];
  296. } polyval_block;
  297. struct polyval_ctx {
  298. // Note that the order of |S|, |H| and |Htable| is fixed by the MOVBE-based,
  299. // x86-64, GHASH assembly.
  300. polyval_block S;
  301. u128 H;
  302. u128 Htable[16];
  303. gmult_func gmult;
  304. ghash_func ghash;
  305. };
  306. // CRYPTO_POLYVAL_init initialises |ctx| using |key|.
  307. void CRYPTO_POLYVAL_init(struct polyval_ctx *ctx, const uint8_t key[16]);
  308. // CRYPTO_POLYVAL_update_blocks updates the accumulator in |ctx| given the
  309. // blocks from |in|. Only a whole number of blocks can be processed so |in_len|
  310. // must be a multiple of 16.
  311. void CRYPTO_POLYVAL_update_blocks(struct polyval_ctx *ctx, const uint8_t *in,
  312. size_t in_len);
  313. // CRYPTO_POLYVAL_finish writes the accumulator from |ctx| to |out|.
  314. void CRYPTO_POLYVAL_finish(const struct polyval_ctx *ctx, uint8_t out[16]);
  315. #if defined(__cplusplus)
  316. } // extern C
  317. #endif
  318. #endif // OPENSSL_HEADER_MODES_INTERNAL_H