25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

371 lines
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. #if defined(__cplusplus)
  52. extern "C" {
  53. #endif
  54. #define asm __asm__
  55. #define STRICT_ALIGNMENT 1
  56. #if defined(OPENSSL_X86_64) || defined(OPENSSL_X86) || defined(OPENSSL_AARCH64)
  57. #undef STRICT_ALIGNMENT
  58. #define STRICT_ALIGNMENT 0
  59. #endif
  60. #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM)
  61. #if defined(__GNUC__) && __GNUC__ >= 2
  62. #if defined(OPENSSL_X86_64)
  63. #define BSWAP8(x) \
  64. ({ \
  65. uint64_t ret = (x); \
  66. asm("bswapq %0" : "+r"(ret)); \
  67. ret; \
  68. })
  69. #define BSWAP4(x) \
  70. ({ \
  71. uint32_t ret = (x); \
  72. asm("bswapl %0" : "+r"(ret)); \
  73. ret; \
  74. })
  75. #elif defined(OPENSSL_X86)
  76. #define BSWAP8(x) \
  77. ({ \
  78. uint32_t lo = (uint64_t)(x) >> 32, hi = (x); \
  79. asm("bswapl %0; bswapl %1" : "+r"(hi), "+r"(lo)); \
  80. (uint64_t) hi << 32 | lo; \
  81. })
  82. #define BSWAP4(x) \
  83. ({ \
  84. uint32_t ret = (x); \
  85. asm("bswapl %0" : "+r"(ret)); \
  86. ret; \
  87. })
  88. #elif defined(OPENSSL_AARCH64)
  89. #define BSWAP8(x) \
  90. ({ \
  91. uint64_t ret; \
  92. asm("rev %0,%1" : "=r"(ret) : "r"(x)); \
  93. ret; \
  94. })
  95. #define BSWAP4(x) \
  96. ({ \
  97. uint32_t ret; \
  98. asm("rev %w0,%w1" : "=r"(ret) : "r"(x)); \
  99. ret; \
  100. })
  101. #elif defined(OPENSSL_ARM) && !defined(STRICT_ALIGNMENT)
  102. #define BSWAP8(x) \
  103. ({ \
  104. uint32_t lo = (uint64_t)(x) >> 32, hi = (x); \
  105. asm("rev %0,%0; rev %1,%1" : "+r"(hi), "+r"(lo)); \
  106. (uint64_t) hi << 32 | lo; \
  107. })
  108. #define BSWAP4(x) \
  109. ({ \
  110. uint32_t ret; \
  111. asm("rev %0,%1" : "=r"(ret) : "r"((uint32_t)(x))); \
  112. ret; \
  113. })
  114. #endif
  115. #elif defined(_MSC_VER)
  116. #if _MSC_VER >= 1300
  117. #pragma warning(push, 3)
  118. #include <intrin.h>
  119. #pragma warning(pop)
  120. #pragma intrinsic(_byteswap_uint64, _byteswap_ulong)
  121. #define BSWAP8(x) _byteswap_uint64((uint64_t)(x))
  122. #define BSWAP4(x) _byteswap_ulong((uint32_t)(x))
  123. #elif defined(OPENSSL_X86)
  124. __inline uint32_t _bswap4(uint32_t val) {
  125. _asm mov eax, val
  126. _asm bswap eax
  127. }
  128. #define BSWAP4(x) _bswap4(x)
  129. #endif
  130. #endif
  131. #endif
  132. #if defined(BSWAP4) && !defined(STRICT_ALIGNMENT)
  133. #define GETU32(p) BSWAP4(*(const uint32_t *)(p))
  134. #define PUTU32(p, v) *(uint32_t *)(p) = BSWAP4(v)
  135. #else
  136. #define GETU32(p) \
  137. ((uint32_t)(p)[0] << 24 | (uint32_t)(p)[1] << 16 | (uint32_t)(p)[2] << 8 | (uint32_t)(p)[3])
  138. #define PUTU32(p, v) \
  139. ((p)[0] = (uint8_t)((v) >> 24), (p)[1] = (uint8_t)((v) >> 16), \
  140. (p)[2] = (uint8_t)((v) >> 8), (p)[3] = (uint8_t)(v))
  141. #endif
  142. /* block128_f is the type of a 128-bit, block cipher. */
  143. typedef void (*block128_f)(const uint8_t in[16], uint8_t out[16],
  144. const void *key);
  145. /* GCM definitions */
  146. typedef struct { uint64_t hi,lo; } u128;
  147. /* This differs from upstream's |gcm128_context| in that it does not have the
  148. * |key| pointer, in order to make it |memcpy|-friendly. Rather the key is
  149. * passed into each call that needs it. */
  150. struct gcm128_context {
  151. /* Following 6 names follow names in GCM specification */
  152. union {
  153. uint64_t u[2];
  154. uint32_t d[4];
  155. uint8_t c[16];
  156. size_t t[16 / sizeof(size_t)];
  157. } Yi, EKi, EK0, len, Xi, H;
  158. /* Relative position of Xi, H and pre-computed Htable is used in some
  159. * assembler modules, i.e. don't change the order! */
  160. u128 Htable[16];
  161. void (*gmult)(uint64_t Xi[2], const u128 Htable[16]);
  162. void (*ghash)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  163. size_t len);
  164. unsigned int mres, ares;
  165. block128_f block;
  166. };
  167. struct ccm128_context {
  168. union {
  169. uint64_t u[2];
  170. uint8_t c[16];
  171. } nonce, cmac;
  172. uint64_t blocks;
  173. block128_f block;
  174. void *key;
  175. };
  176. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  177. /* crypto_gcm_clmul_enabled returns one if the CLMUL implementation of GCM is
  178. * used. */
  179. int crypto_gcm_clmul_enabled(void);
  180. #endif
  181. /* CTR. */
  182. /* ctr128_f is the type of a function that performs CTR-mode encryption. */
  183. typedef void (*ctr128_f)(const uint8_t *in, uint8_t *out, size_t blocks,
  184. const void *key, const uint8_t ivec[16]);
  185. /* CRYPTO_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode)
  186. * |len| bytes from |in| to |out| using |block| in counter mode. There's no
  187. * requirement that |len| be a multiple of any value and any partial blocks are
  188. * stored in |ecount_buf| and |*num|, which must be zeroed before the initial
  189. * call. The counter is a 128-bit, big-endian value in |ivec| and is
  190. * incremented by this function. */
  191. void CRYPTO_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  192. const void *key, uint8_t ivec[16],
  193. uint8_t ecount_buf[16], unsigned int *num,
  194. block128_f block);
  195. /* CRYPTO_ctr128_encrypt_ctr32 acts like |CRYPTO_ctr128_encrypt| but takes
  196. * |ctr|, a function that performs CTR mode but only deals with the lower 32
  197. * bits of the counter. This is useful when |ctr| can be an optimised
  198. * function. */
  199. void CRYPTO_ctr128_encrypt_ctr32(const uint8_t *in, uint8_t *out, size_t len,
  200. const void *key, uint8_t ivec[16],
  201. uint8_t ecount_buf[16], unsigned int *num,
  202. ctr128_f ctr);
  203. /* GCM.
  204. *
  205. * This API differs from the upstream API slightly. The |GCM128_CONTEXT| does
  206. * not have a |key| pointer that points to the key as upstream's version does.
  207. * Instead, every function takes a |key| parameter. This way |GCM128_CONTEXT|
  208. * can be safely copied. */
  209. typedef struct gcm128_context GCM128_CONTEXT;
  210. /* CRYPTO_gcm128_new allocates a fresh |GCM128_CONTEXT| and calls
  211. * |CRYPTO_gcm128_init|. It returns the new context, or NULL on error. */
  212. OPENSSL_EXPORT GCM128_CONTEXT *CRYPTO_gcm128_new(const void *key,
  213. block128_f block);
  214. /* CRYPTO_gcm128_init initialises |ctx| to use |block| (typically AES) with
  215. * the given key. */
  216. OPENSSL_EXPORT void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, const void *key,
  217. block128_f block);
  218. /* CRYPTO_gcm128_setiv sets the IV (nonce) for |ctx|. The |key| must be the
  219. * same key that was passed to |CRYPTO_gcm128_init|. */
  220. OPENSSL_EXPORT void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const void *key,
  221. const uint8_t *iv, size_t iv_len);
  222. /* CRYPTO_gcm128_aad sets the authenticated data for an instance of GCM.
  223. * This must be called before and data is encrypted. It returns one on success
  224. * and zero otherwise. */
  225. OPENSSL_EXPORT int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad,
  226. size_t len);
  227. /* CRYPTO_gcm128_encrypt encrypts |len| bytes from |in| to |out|. The |key|
  228. * must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one
  229. * on success and zero otherwise. */
  230. OPENSSL_EXPORT int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const void *key,
  231. const uint8_t *in, uint8_t *out,
  232. size_t len);
  233. /* CRYPTO_gcm128_decrypt decrypts |len| bytes from |in| to |out|. The |key|
  234. * must be the same key that was passed to |CRYPTO_gcm128_init|. It returns one
  235. * on success and zero otherwise. */
  236. OPENSSL_EXPORT int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const void *key,
  237. const uint8_t *in, uint8_t *out,
  238. size_t len);
  239. /* CRYPTO_gcm128_encrypt_ctr32 encrypts |len| bytes from |in| to |out| using
  240. * a CTR function that only handles the bottom 32 bits of the nonce, like
  241. * |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was
  242. * passed to |CRYPTO_gcm128_init|. It returns one on success and zero
  243. * otherwise. */
  244. OPENSSL_EXPORT int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  245. const void *key,
  246. const uint8_t *in, uint8_t *out,
  247. size_t len, ctr128_f stream);
  248. /* CRYPTO_gcm128_decrypt_ctr32 decrypts |len| bytes from |in| to |out| using
  249. * a CTR function that only handles the bottom 32 bits of the nonce, like
  250. * |CRYPTO_ctr128_encrypt_ctr32|. The |key| must be the same key that was
  251. * passed to |CRYPTO_gcm128_init|. It returns one on success and zero
  252. * otherwise. */
  253. OPENSSL_EXPORT int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  254. const void *key,
  255. const uint8_t *in, uint8_t *out,
  256. size_t len, ctr128_f stream);
  257. /* CRYPTO_gcm128_finish calculates the authenticator and compares it against
  258. * |len| bytes of |tag|. It returns one on success and zero otherwise. */
  259. OPENSSL_EXPORT int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag,
  260. size_t len);
  261. /* CRYPTO_gcm128_tag calculates the authenticator and copies it into |tag|.
  262. * The minimum of |len| and 16 bytes are copied into |tag|. */
  263. OPENSSL_EXPORT void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, uint8_t *tag,
  264. size_t len);
  265. /* CRYPTO_gcm128_release clears and frees |ctx|. */
  266. OPENSSL_EXPORT void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx);
  267. /* CBC. */
  268. /* cbc128_f is the type of a function that performs CBC-mode encryption. */
  269. typedef void (*cbc128_f)(const uint8_t *in, uint8_t *out, size_t len,
  270. const void *key, uint8_t ivec[16], int enc);
  271. /* CRYPTO_cbc128_encrypt encrypts |len| bytes from |in| to |out| using the
  272. * given IV and block cipher in CBC mode. The input need not be a multiple of
  273. * 128 bits long, but the output will round up to the nearest 128 bit multiple,
  274. * zero padding the input if needed. The IV will be updated on return. */
  275. void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  276. const void *key, uint8_t ivec[16], block128_f block);
  277. /* CRYPTO_cbc128_decrypt decrypts |len| bytes from |in| to |out| using the
  278. * given IV and block cipher in CBC mode. If |len| is not a multiple of 128
  279. * bits then only that many bytes will be written, but a multiple of 128 bits
  280. * is always read from |in|. The IV will be updated on return. */
  281. void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  282. const void *key, uint8_t ivec[16], block128_f block);
  283. /* OFB. */
  284. /* CRYPTO_ofb128_encrypt encrypts (or decrypts, it's the same with OFB mode)
  285. * |len| bytes from |in| to |out| using |block| in OFB mode. There's no
  286. * requirement that |len| be a multiple of any value and any partial blocks are
  287. * stored in |ivec| and |*num|, the latter must be zero before the initial
  288. * call. */
  289. void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out,
  290. size_t len, const void *key, uint8_t ivec[16],
  291. int *num, block128_f block);
  292. /* CFB. */
  293. /* CRYPTO_cfb128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  294. * from |in| to |out| using |block| in CFB mode. There's no requirement that
  295. * |len| be a multiple of any value and any partial blocks are stored in |ivec|
  296. * and |*num|, the latter must be zero before the initial call. */
  297. void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  298. const void *key, uint8_t ivec[16], int *num, int enc,
  299. block128_f block);
  300. /* CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  301. * from |in| to |out| using |block| in CFB-8 mode. Prior to the first call
  302. * |num| should be set to zero. */
  303. void CRYPTO_cfb128_8_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  304. const void *key, uint8_t ivec[16], int *num,
  305. int enc, block128_f block);
  306. /* CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  307. * from |in| to |out| using |block| in CFB-1 mode. Prior to the first call
  308. * |num| should be set to zero. */
  309. void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
  310. const void *key, uint8_t ivec[16], int *num,
  311. int enc, block128_f block);
  312. size_t CRYPTO_cts128_encrypt_block(const uint8_t *in, uint8_t *out, size_t len,
  313. const void *key, uint8_t ivec[16],
  314. block128_f block);
  315. #if defined(__cplusplus)
  316. } /* extern C */
  317. #endif
  318. #endif /* OPENSSL_HEADER_MODES_INTERNAL_H */