cipher_aes_gcm_siv.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/aes.h>
  10. #include "prov/ciphercommon.h"
  11. #include "crypto/aes_platform.h"
  12. #define BLOCK_SIZE 16
  13. #define NONCE_SIZE 12
  14. #define TAG_SIZE 16
  15. /* AAD manipulation macros */
  16. #define UP16(x) (((x) + 15) & ~0x0F)
  17. #define DOWN16(x) ((x) & ~0x0F)
  18. #define REMAINDER16(x) ((x) & 0x0F)
  19. #define IS16(x) (((x) & 0x0F) == 0)
  20. typedef struct prov_cipher_hw_aes_gcm_siv_st {
  21. int (*initkey)(void *vctx);
  22. int (*cipher)(void *vctx, unsigned char *out, const unsigned char *in,
  23. size_t len);
  24. int (*dup_ctx)(void *vdst, void *vsrc);
  25. void (*clean_ctx)(void *vctx);
  26. } PROV_CIPHER_HW_AES_GCM_SIV;
  27. /* Arranged for alignment purposes */
  28. typedef struct prov_aes_gcm_siv_ctx_st {
  29. EVP_CIPHER_CTX *ecb_ctx;
  30. const PROV_CIPHER_HW_AES_GCM_SIV *hw; /* maybe not used, yet? */
  31. uint8_t *aad; /* Allocated, rounded up to 16 bytes, from user */
  32. OSSL_LIB_CTX *libctx;
  33. OSSL_PROVIDER *provctx;
  34. size_t aad_len; /* actual AAD length */
  35. size_t key_len;
  36. uint8_t key_gen_key[32]; /* from user */
  37. uint8_t msg_enc_key[32]; /* depends on key size */
  38. uint8_t msg_auth_key[BLOCK_SIZE];
  39. uint8_t tag[TAG_SIZE]; /* generated tag, given to user or compared to user */
  40. uint8_t user_tag[TAG_SIZE]; /* from user */
  41. uint8_t nonce[NONCE_SIZE]; /* from user */
  42. u128 Htable[16]; /* Polyval calculations via ghash */
  43. unsigned int enc : 1; /* Set to 1 if we are encrypting or 0 otherwise */
  44. unsigned int have_user_tag : 1;
  45. unsigned int generated_tag : 1;
  46. unsigned int used_enc : 1;
  47. unsigned int used_dec : 1;
  48. unsigned int speed : 1;
  49. } PROV_AES_GCM_SIV_CTX;
  50. const PROV_CIPHER_HW_AES_GCM_SIV *ossl_prov_cipher_hw_aes_gcm_siv(size_t keybits);
  51. void ossl_polyval_ghash_init(u128 Htable[16], const uint64_t H[2]);
  52. void ossl_polyval_ghash_hash(const u128 Htable[16], uint8_t *tag, const uint8_t *inp, size_t len);
  53. /* Define GSWAP8/GSWAP4 - used for BOTH little and big endian architectures */
  54. static ossl_inline uint32_t GSWAP4(uint32_t n)
  55. {
  56. return (((n & 0x000000FF) << 24)
  57. | ((n & 0x0000FF00) << 8)
  58. | ((n & 0x00FF0000) >> 8)
  59. | ((n & 0xFF000000) >> 24));
  60. }
  61. static ossl_inline uint64_t GSWAP8(uint64_t n)
  62. {
  63. uint64_t result;
  64. result = GSWAP4(n & 0x0FFFFFFFF);
  65. result <<= 32;
  66. return result | GSWAP4(n >> 32);
  67. }