No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

224 líneas
10 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_H
  49. #define OPENSSL_HEADER_MODES_H
  50. #include <openssl/base.h>
  51. #if defined(__cplusplus)
  52. extern "C" {
  53. #endif
  54. /* modes.h contains functions that implement various block-cipher modes. */
  55. /* block128_f is the type of a 128-bit, block cipher. */
  56. typedef void (*block128_f)(const uint8_t in[16], uint8_t out[16],
  57. const void *key);
  58. /* CTR. */
  59. /* ctr128_f is the type of a function that performs CTR-mode encryption. */
  60. typedef void (*ctr128_f)(const uint8_t *in, uint8_t *out, size_t blocks,
  61. const void *key, const uint8_t ivec[16]);
  62. /* CRYPTO_ctr128_encrypt encrypts (or decrypts, it's the same in CTR mode)
  63. * |len| bytes from |in| to |out| using |block| in counter mode. There's no
  64. * requirement that |len| be a multiple of any value and any partial blocks are
  65. * stored in |ecount_buf| and |*num|, which must be zeroed before the initial
  66. * call. The counter is a 128-bit, big-endian value in |ivec| and is
  67. * incremented by this function. */
  68. OPENSSL_EXPORT void CRYPTO_ctr128_encrypt(const uint8_t *in, uint8_t *out,
  69. size_t len, const void *key,
  70. uint8_t ivec[16],
  71. uint8_t ecount_buf[16],
  72. unsigned int *num, block128_f block);
  73. /* CRYPTO_ctr128_encrypt_ctr32 acts like |CRYPTO_ctr128_encrypt| but takes
  74. * |ctr|, a function that performs CTR mode but only deals with the lower 32
  75. * bits of the counter. This is useful when |ctr| can be an optimised
  76. * function. */
  77. OPENSSL_EXPORT void CRYPTO_ctr128_encrypt_ctr32(
  78. const uint8_t *in, uint8_t *out, size_t len, const void *key,
  79. uint8_t ivec[16], uint8_t ecount_buf[16], unsigned int *num, ctr128_f ctr);
  80. /* GCM. */
  81. typedef struct gcm128_context GCM128_CONTEXT;
  82. /* CRYPTO_gcm128_new allocates a fresh |GCM128_CONTEXT| and calls
  83. * |CRYPTO_gcm128_init|. It returns the new context, or NULL on error. */
  84. OPENSSL_EXPORT GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block);
  85. /* CRYPTO_gcm128_init initialises |ctx| to use |block| (typically AES) with the
  86. * given key. */
  87. OPENSSL_EXPORT void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key,
  88. block128_f block);
  89. /* CRYPTO_gcm128_setiv sets the IV (nonce) for |ctx|. */
  90. OPENSSL_EXPORT void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const uint8_t *iv,
  91. size_t len);
  92. /* CRYPTO_gcm128_aad sets the authenticated data for an instance of GCM. This
  93. * must be called before and data is encrypted. It returns one on success and
  94. * zero otherwise. */
  95. OPENSSL_EXPORT int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad,
  96. size_t len);
  97. /* CRYPTO_gcm128_encrypt encrypts |len| bytes from |in| to |out|. It returns
  98. * one on success and zero otherwise. */
  99. OPENSSL_EXPORT int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const uint8_t *in,
  100. uint8_t *out, size_t len);
  101. /* CRYPTO_gcm128_decrypt decrypts |len| bytes from |in| to |out|. It returns
  102. * one on success and zero otherwise. */
  103. OPENSSL_EXPORT int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const uint8_t *in,
  104. uint8_t *out, size_t len);
  105. /* CRYPTO_gcm128_encrypt_ctr32 encrypts |len| bytes from |in| to |out| using a
  106. * CTR function that only handles the bottom 32 bits of the nonce, like
  107. * |CRYPTO_ctr128_encrypt_ctr32|. It returns one on success and zero
  108. * otherwise. */
  109. OPENSSL_EXPORT int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  110. const uint8_t *in, uint8_t *out,
  111. size_t len, ctr128_f stream);
  112. /* CRYPTO_gcm128_decrypt_ctr32 decrypts |len| bytes from |in| to |out| using a
  113. * CTR function that only handles the bottom 32 bits of the nonce, like
  114. * |CRYPTO_ctr128_encrypt_ctr32|. It returns one on success and zero
  115. * otherwise. */
  116. OPENSSL_EXPORT int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  117. const uint8_t *in, uint8_t *out,
  118. size_t len, ctr128_f stream);
  119. /* CRYPTO_gcm128_finish calculates the authenticator and compares it against
  120. * |len| bytes of |tag|. It returns one on success and zero otherwise. */
  121. OPENSSL_EXPORT int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag,
  122. size_t len);
  123. /* CRYPTO_gcm128_tag calculates the authenticator and copies it into |tag|. The
  124. * minimum of |len| and 16 bytes are copied into |tag|. */
  125. OPENSSL_EXPORT void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, uint8_t *tag,
  126. size_t len);
  127. /* CRYPTO_gcm128_release clears and frees |ctx|. */
  128. OPENSSL_EXPORT void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx);
  129. /* CBC. */
  130. /* cbc128_f is the type of a function that performs CBC-mode encryption. */
  131. typedef void (*cbc128_f)(const uint8_t *in, uint8_t *out, size_t len,
  132. const void *key, uint8_t ivec[16], int enc);
  133. /* CRYPTO_cbc128_encrypt encrypts |len| bytes from |in| to |out| using the
  134. * given IV and block cipher in CBC mode. The input need not be a multiple of
  135. * 128 bits long, but the output will round up to the nearest 128 bit multiple,
  136. * zero padding the input if needed. The IV will be updated on return. */
  137. void CRYPTO_cbc128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  138. const void *key, uint8_t ivec[16], block128_f block);
  139. /* CRYPTO_cbc128_decrypt decrypts |len| bytes from |in| to |out| using the
  140. * given IV and block cipher in CBC mode. If |len| is not a multiple of 128
  141. * bits then only that many bytes will be written, but a multiple of 128 bits
  142. * is always read from |in|. The IV will be updated on return. */
  143. void CRYPTO_cbc128_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  144. const void *key, uint8_t ivec[16], block128_f block);
  145. /* OFB. */
  146. /* CRYPTO_ofb128_encrypt encrypts (or decrypts, it's the same with OFB mode)
  147. * |len| bytes from |in| to |out| using |block| in OFB mode. There's no
  148. * requirement that |len| be a multiple of any value and any partial blocks are
  149. * stored in |ivec| and |*num|, the latter must be zero before the initial
  150. * call. */
  151. void CRYPTO_ofb128_encrypt(const uint8_t *in, uint8_t *out,
  152. size_t len, const void *key, uint8_t ivec[16],
  153. int *num, block128_f block);
  154. /* CFB. */
  155. /* CRYPTO_cfb128_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  156. * from |in| to |out| using |block| in CFB mode. There's no requirement that
  157. * |len| be a multiple of any value and any partial blocks are stored in |ivec|
  158. * and |*num|, the latter must be zero before the initial call. */
  159. void CRYPTO_cfb128_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  160. const void *key, uint8_t ivec[16], int *num, int enc,
  161. block128_f block);
  162. /* CRYPTO_cfb128_8_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  163. * from |in| to |out| using |block| in CFB-8 mode. Prior to the first call
  164. * |num| should be set to zero. */
  165. void CRYPTO_cfb128_8_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  166. const void *key, uint8_t ivec[16], int *num,
  167. int enc, block128_f block);
  168. /* CRYPTO_cfb128_1_encrypt encrypts (or decrypts, if |enc| is zero) |len| bytes
  169. * from |in| to |out| using |block| in CFB-1 mode. Prior to the first call
  170. * |num| should be set to zero. */
  171. void CRYPTO_cfb128_1_encrypt(const uint8_t *in, uint8_t *out, size_t bits,
  172. const void *key, uint8_t ivec[16], int *num,
  173. int enc, block128_f block);
  174. size_t CRYPTO_cts128_encrypt_block(const uint8_t *in, uint8_t *out, size_t len,
  175. const void *key, uint8_t ivec[16],
  176. block128_f block);
  177. #if defined(__cplusplus)
  178. } /* extern C */
  179. #endif
  180. #endif /* OPENSSL_HEADER_MODES_H */