2
0

ciphercommon_gcm.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2019-2022 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. #ifndef OSSL_PROV_CIPHERCOMMON_GCM_H
  10. # define OSSL_PROV_CIPHERCOMMON_GCM_H
  11. # pragma once
  12. # include <openssl/aes.h>
  13. # include "ciphercommon_aead.h"
  14. typedef struct prov_gcm_hw_st PROV_GCM_HW;
  15. # define GCM_IV_DEFAULT_SIZE 12 /* IV's for AES_GCM should normally be 12 bytes */
  16. # define GCM_IV_MAX_SIZE (1024 / 8)
  17. # define GCM_TAG_MAX_SIZE 16
  18. # if defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
  19. /*-
  20. * KMA-GCM-AES parameter block - begin
  21. * (see z/Architecture Principles of Operation >= SA22-7832-11)
  22. */
  23. typedef struct S390X_kma_params_st {
  24. unsigned char reserved[12];
  25. union {
  26. unsigned int w;
  27. unsigned char b[4];
  28. } cv; /* 32 bit counter value */
  29. union {
  30. unsigned long long g[2];
  31. unsigned char b[16];
  32. } t; /* tag */
  33. unsigned char h[16]; /* hash subkey */
  34. unsigned long long taadl; /* total AAD length */
  35. unsigned long long tpcl; /* total plaintxt/ciphertxt len */
  36. union {
  37. unsigned long long g[2];
  38. unsigned int w[4];
  39. } j0; /* initial counter value */
  40. unsigned char k[32]; /* key */
  41. } S390X_KMA_PARAMS;
  42. # endif
  43. typedef struct prov_gcm_ctx_st {
  44. unsigned int mode; /* The mode that we are using */
  45. size_t keylen;
  46. size_t ivlen;
  47. size_t taglen;
  48. size_t tls_aad_pad_sz;
  49. size_t tls_aad_len; /* TLS AAD length */
  50. uint64_t tls_enc_records; /* Number of TLS records encrypted */
  51. /*
  52. * num contains the number of bytes of |iv| which are valid for modes that
  53. * manage partial blocks themselves.
  54. */
  55. size_t num;
  56. size_t bufsz; /* Number of bytes in buf */
  57. uint64_t flags;
  58. unsigned int iv_state; /* set to one of IV_STATE_XXX */
  59. unsigned int enc:1; /* Set to 1 if we are encrypting or 0 otherwise */
  60. unsigned int pad:1; /* Whether padding should be used or not */
  61. unsigned int key_set:1; /* Set if key initialised */
  62. unsigned int iv_gen_rand:1; /* No IV was specified, so generate a rand IV */
  63. unsigned int iv_gen:1; /* It is OK to generate IVs */
  64. unsigned char iv[GCM_IV_MAX_SIZE]; /* Buffer to use for IV's */
  65. unsigned char buf[AES_BLOCK_SIZE]; /* Buffer of partial blocks processed via update calls */
  66. OSSL_LIB_CTX *libctx; /* needed for rand calls */
  67. const PROV_GCM_HW *hw; /* hardware specific methods */
  68. GCM128_CONTEXT gcm;
  69. ctr128_f ctr;
  70. const void *ks;
  71. } PROV_GCM_CTX;
  72. PROV_CIPHER_FUNC(int, GCM_setkey, (PROV_GCM_CTX *ctx, const unsigned char *key,
  73. size_t keylen));
  74. PROV_CIPHER_FUNC(int, GCM_setiv, (PROV_GCM_CTX *dat, const unsigned char *iv,
  75. size_t ivlen));
  76. PROV_CIPHER_FUNC(int, GCM_aadupdate, (PROV_GCM_CTX *ctx,
  77. const unsigned char *aad, size_t aadlen));
  78. PROV_CIPHER_FUNC(int, GCM_cipherupdate, (PROV_GCM_CTX *ctx,
  79. const unsigned char *in, size_t len,
  80. unsigned char *out));
  81. PROV_CIPHER_FUNC(int, GCM_cipherfinal, (PROV_GCM_CTX *ctx, unsigned char *tag));
  82. PROV_CIPHER_FUNC(int, GCM_oneshot, (PROV_GCM_CTX *ctx, unsigned char *aad,
  83. size_t aad_len, const unsigned char *in,
  84. size_t in_len, unsigned char *out,
  85. unsigned char *tag, size_t taglen));
  86. struct prov_gcm_hw_st {
  87. OSSL_GCM_setkey_fn setkey;
  88. OSSL_GCM_setiv_fn setiv;
  89. OSSL_GCM_aadupdate_fn aadupdate;
  90. OSSL_GCM_cipherupdate_fn cipherupdate;
  91. OSSL_GCM_cipherfinal_fn cipherfinal;
  92. OSSL_GCM_oneshot_fn oneshot;
  93. };
  94. OSSL_FUNC_cipher_encrypt_init_fn ossl_gcm_einit;
  95. OSSL_FUNC_cipher_decrypt_init_fn ossl_gcm_dinit;
  96. OSSL_FUNC_cipher_get_ctx_params_fn ossl_gcm_get_ctx_params;
  97. OSSL_FUNC_cipher_set_ctx_params_fn ossl_gcm_set_ctx_params;
  98. OSSL_FUNC_cipher_cipher_fn ossl_gcm_cipher;
  99. OSSL_FUNC_cipher_update_fn ossl_gcm_stream_update;
  100. OSSL_FUNC_cipher_final_fn ossl_gcm_stream_final;
  101. void ossl_gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
  102. const PROV_GCM_HW *hw);
  103. int ossl_gcm_setiv(PROV_GCM_CTX *ctx, const unsigned char *iv, size_t ivlen);
  104. int ossl_gcm_aad_update(PROV_GCM_CTX *ctx, const unsigned char *aad,
  105. size_t aad_len);
  106. int ossl_gcm_cipher_final(PROV_GCM_CTX *ctx, unsigned char *tag);
  107. int ossl_gcm_one_shot(PROV_GCM_CTX *ctx, unsigned char *aad, size_t aad_len,
  108. const unsigned char *in, size_t in_len,
  109. unsigned char *out, unsigned char *tag, size_t tag_len);
  110. int ossl_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
  111. size_t len, unsigned char *out);
  112. # define GCM_HW_SET_KEY_CTR_FN(ks, fn_set_enc_key, fn_block, fn_ctr) \
  113. ctx->ks = ks; \
  114. fn_set_enc_key(key, keylen * 8, ks); \
  115. CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f)fn_block); \
  116. ctx->ctr = (ctr128_f)fn_ctr; \
  117. ctx->key_set = 1;
  118. #endif