cipher_aes_siv_hw.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /*
  10. * This file uses the low level AES functions (which are deprecated for
  11. * non-internal use) in order to implement provider AES ciphers.
  12. */
  13. #include "internal/deprecated.h"
  14. #include "cipher_aes_siv.h"
  15. static void aes_siv_cleanup(void *vctx);
  16. static int aes_siv_initkey(void *vctx, const unsigned char *key, size_t keylen)
  17. {
  18. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  19. SIV128_CONTEXT *sctx = &ctx->siv;
  20. size_t klen = keylen / 2;
  21. OSSL_LIB_CTX *libctx = ctx->libctx;
  22. const char *propq = NULL;
  23. EVP_CIPHER_free(ctx->cbc);
  24. EVP_CIPHER_free(ctx->ctr);
  25. ctx->cbc = NULL;
  26. ctx->ctr = NULL;
  27. switch (klen) {
  28. case 16:
  29. ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", propq);
  30. ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-128-CTR", propq);
  31. break;
  32. case 24:
  33. ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-192-CBC", propq);
  34. ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-192-CTR", propq);
  35. break;
  36. case 32:
  37. ctx->cbc = EVP_CIPHER_fetch(libctx, "AES-256-CBC", propq);
  38. ctx->ctr = EVP_CIPHER_fetch(libctx, "AES-256-CTR", propq);
  39. break;
  40. default:
  41. break;
  42. }
  43. if (ctx->cbc == NULL || ctx->ctr == NULL)
  44. return 0;
  45. /*
  46. * klen is the length of the underlying cipher, not the input key,
  47. * which should be twice as long
  48. */
  49. return ossl_siv128_init(sctx, key, klen, ctx->cbc, ctx->ctr, libctx,
  50. propq);
  51. }
  52. static int aes_siv_dupctx(void *in_vctx, void *out_vctx)
  53. {
  54. PROV_AES_SIV_CTX *in = (PROV_AES_SIV_CTX *)in_vctx;
  55. PROV_AES_SIV_CTX *out = (PROV_AES_SIV_CTX *)out_vctx;
  56. *out = *in;
  57. out->siv.cipher_ctx = NULL;
  58. out->siv.mac_ctx_init = NULL;
  59. out->siv.mac = NULL;
  60. if (!ossl_siv128_copy_ctx(&out->siv, &in->siv))
  61. return 0;
  62. if (out->cbc != NULL)
  63. EVP_CIPHER_up_ref(out->cbc);
  64. if (out->ctr != NULL)
  65. EVP_CIPHER_up_ref(out->ctr);
  66. return 1;
  67. }
  68. static int aes_siv_settag(void *vctx, const unsigned char *tag, size_t tagl)
  69. {
  70. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  71. SIV128_CONTEXT *sctx = &ctx->siv;
  72. return ossl_siv128_set_tag(sctx, tag, tagl);
  73. }
  74. static void aes_siv_setspeed(void *vctx, int speed)
  75. {
  76. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  77. SIV128_CONTEXT *sctx = &ctx->siv;
  78. ossl_siv128_speed(sctx, (int)speed);
  79. }
  80. static void aes_siv_cleanup(void *vctx)
  81. {
  82. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  83. SIV128_CONTEXT *sctx = &ctx->siv;
  84. ossl_siv128_cleanup(sctx);
  85. EVP_CIPHER_free(ctx->cbc);
  86. EVP_CIPHER_free(ctx->ctr);
  87. }
  88. static int aes_siv_cipher(void *vctx, unsigned char *out,
  89. const unsigned char *in, size_t len)
  90. {
  91. PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx;
  92. SIV128_CONTEXT *sctx = &ctx->siv;
  93. /* EncryptFinal or DecryptFinal */
  94. if (in == NULL)
  95. return ossl_siv128_finish(sctx) == 0;
  96. /* Deal with associated data */
  97. if (out == NULL)
  98. return (ossl_siv128_aad(sctx, in, len) == 1);
  99. if (ctx->enc)
  100. return ossl_siv128_encrypt(sctx, in, out, len) > 0;
  101. return ossl_siv128_decrypt(sctx, in, out, len) > 0;
  102. }
  103. static const PROV_CIPHER_HW_AES_SIV aes_siv_hw =
  104. {
  105. aes_siv_initkey,
  106. aes_siv_cipher,
  107. aes_siv_setspeed,
  108. aes_siv_settag,
  109. aes_siv_cleanup,
  110. aes_siv_dupctx,
  111. };
  112. const PROV_CIPHER_HW_AES_SIV *ossl_prov_cipher_hw_aes_siv(size_t keybits)
  113. {
  114. return &aes_siv_hw;
  115. }