cipher_gcm.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2019 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 "cipher_aead.h"
  11. typedef struct prov_gcm_hw_st PROV_GCM_HW;
  12. #define GCM_IV_DEFAULT_SIZE 12 /* IV's for AES_GCM should normally be 12 bytes */
  13. #define GCM_IV_MAX_SIZE 64
  14. #define GCM_TAG_MAX_SIZE 16
  15. #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
  16. /*-
  17. * KMA-GCM-AES parameter block - begin
  18. * (see z/Architecture Principles of Operation >= SA22-7832-11)
  19. */
  20. typedef struct S390X_kma_params_st {
  21. unsigned char reserved[12];
  22. union {
  23. unsigned int w;
  24. unsigned char b[4];
  25. } cv; /* 32 bit counter value */
  26. union {
  27. unsigned long long g[2];
  28. unsigned char b[16];
  29. } t; /* tag */
  30. unsigned char h[16]; /* hash subkey */
  31. unsigned long long taadl; /* total AAD length */
  32. unsigned long long tpcl; /* total plaintxt/ciphertxt len */
  33. union {
  34. unsigned long long g[2];
  35. unsigned int w[4];
  36. } j0; /* initial counter value */
  37. unsigned char k[32]; /* key */
  38. } S390X_KMA_PARAMS;
  39. #endif
  40. typedef struct prov_gcm_ctx_st {
  41. unsigned int mode; /* The mode that we are using */
  42. size_t keylen;
  43. size_t ivlen;
  44. size_t ivlen_min;
  45. size_t taglen;
  46. size_t tls_aad_pad_sz;
  47. size_t tls_aad_len; /* TLS AAD length */
  48. uint64_t tls_enc_records; /* Number of TLS records encrypted */
  49. /*
  50. * num contains the number of bytes of |iv| which are valid for modes that
  51. * manage partial blocks themselves.
  52. */
  53. size_t num;
  54. size_t bufsz; /* Number of bytes in buf */
  55. uint64_t flags;
  56. unsigned int iv_state; /* set to one of IV_STATE_XXX */
  57. unsigned int enc:1; /* Set to 1 if we are encrypting or 0 otherwise */
  58. unsigned int pad:1; /* Whether padding should be used or not */
  59. unsigned int key_set:1; /* Set if key initialised */
  60. unsigned int iv_gen_rand:1; /* No IV was specified, so generate a rand IV */
  61. unsigned int iv_gen:1; /* It is OK to generate IVs */
  62. unsigned char iv[GCM_IV_MAX_SIZE]; /* Buffer to use for IV's */
  63. unsigned char buf[AES_BLOCK_SIZE]; /* Buffer of partial blocks processed via update calls */
  64. OPENSSL_CTX *libctx; /* needed for rand calls */
  65. const PROV_GCM_HW *hw; /* hardware specific methods */
  66. GCM128_CONTEXT gcm;
  67. ctr128_f ctr;
  68. const void *ks;
  69. } PROV_GCM_CTX;
  70. typedef struct prov_aes_gcm_ctx_st {
  71. PROV_GCM_CTX base; /* must be first entry in struct */
  72. union {
  73. OSSL_UNION_ALIGN;
  74. AES_KEY ks;
  75. } ks; /* AES key schedule to use */
  76. /* Platform specific data */
  77. union {
  78. int dummy;
  79. #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
  80. struct {
  81. union {
  82. OSSL_UNION_ALIGN;
  83. S390X_KMA_PARAMS kma;
  84. } param;
  85. unsigned int fc;
  86. unsigned char ares[16];
  87. unsigned char mres[16];
  88. unsigned char kres[16];
  89. int areslen;
  90. int mreslen;
  91. int kreslen;
  92. int res;
  93. } s390x;
  94. #endif /* defined(OPENSSL_CPUID_OBJ) && defined(__s390__) */
  95. } plat;
  96. } PROV_AES_GCM_CTX;
  97. PROV_CIPHER_FUNC(int, GCM_setkey, (PROV_GCM_CTX *ctx, const unsigned char *key,
  98. size_t keylen));
  99. PROV_CIPHER_FUNC(int, GCM_setiv, (PROV_GCM_CTX *dat, const unsigned char *iv,
  100. size_t ivlen));
  101. PROV_CIPHER_FUNC(int, GCM_aadupdate, (PROV_GCM_CTX *ctx,
  102. const unsigned char *aad, size_t aadlen));
  103. PROV_CIPHER_FUNC(int, GCM_cipherupdate, (PROV_GCM_CTX *ctx,
  104. const unsigned char *in, size_t len,
  105. unsigned char *out));
  106. PROV_CIPHER_FUNC(int, GCM_cipherfinal, (PROV_GCM_CTX *ctx, unsigned char *tag));
  107. PROV_CIPHER_FUNC(int, GCM_oneshot, (PROV_GCM_CTX *ctx, unsigned char *aad,
  108. size_t aad_len, const unsigned char *in,
  109. size_t in_len, unsigned char *out,
  110. unsigned char *tag, size_t taglen));
  111. struct prov_gcm_hw_st {
  112. OSSL_GCM_setkey_fn setkey;
  113. OSSL_GCM_setiv_fn setiv;
  114. OSSL_GCM_aadupdate_fn aadupdate;
  115. OSSL_GCM_cipherupdate_fn cipherupdate;
  116. OSSL_GCM_cipherfinal_fn cipherfinal;
  117. OSSL_GCM_oneshot_fn oneshot;
  118. };
  119. const PROV_GCM_HW *PROV_AES_HW_gcm(size_t keybits);
  120. OSSL_OP_cipher_encrypt_init_fn gcm_einit;
  121. OSSL_OP_cipher_decrypt_init_fn gcm_dinit;
  122. OSSL_OP_cipher_get_ctx_params_fn gcm_get_ctx_params;
  123. OSSL_OP_cipher_set_ctx_params_fn gcm_set_ctx_params;
  124. OSSL_OP_cipher_cipher_fn gcm_cipher;
  125. OSSL_OP_cipher_update_fn gcm_stream_update;
  126. OSSL_OP_cipher_final_fn gcm_stream_final;
  127. void gcm_deinitctx(PROV_GCM_CTX *ctx);
  128. void gcm_initctx(void *provctx, PROV_GCM_CTX *ctx, size_t keybits,
  129. const PROV_GCM_HW *hw, size_t ivlen_min);
  130. int gcm_setiv(PROV_GCM_CTX *ctx, const unsigned char *iv, size_t ivlen);
  131. int gcm_aad_update(PROV_GCM_CTX *ctx, const unsigned char *aad,
  132. size_t aad_len);
  133. int gcm_cipher_final(PROV_GCM_CTX *ctx, unsigned char *tag);
  134. int gcm_one_shot(PROV_GCM_CTX *ctx, unsigned char *aad, size_t aad_len,
  135. const unsigned char *in, size_t in_len,
  136. unsigned char *out, unsigned char *tag, size_t tag_len);
  137. int gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
  138. size_t len, unsigned char *out);
  139. #define GCM_HW_SET_KEY_CTR_FN(ks, fn_set_enc_key, fn_block, fn_ctr) \
  140. ctx->ks = ks; \
  141. fn_set_enc_key(key, keylen * 8, ks); \
  142. CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f)fn_block); \
  143. ctx->ctr = (ctr128_f)fn_ctr; \
  144. ctx->key_set = 1;