cipher_aes_ocb_hw.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "cipher_aes_ocb.h"
  10. #define OCB_SET_KEY_FN(fn_set_enc_key, fn_set_dec_key, \
  11. fn_block_enc, fn_block_dec, \
  12. fn_stream_enc, fn_stream_dec) \
  13. fn_set_enc_key(key, keylen * 8, &ctx->ksenc.ks); \
  14. fn_set_dec_key(key, keylen * 8, &ctx->ksdec.ks); \
  15. if (!CRYPTO_ocb128_init(&ctx->ocb, &ctx->ksenc.ks, &ctx->ksdec.ks, \
  16. (block128_f)fn_block_enc, (block128_f)fn_block_dec, \
  17. ctx->base.enc ? (ocb128_f)fn_stream_enc : \
  18. (ocb128_f)fn_stream_dec)) \
  19. return 0; \
  20. ctx->key_set = 1
  21. static int cipher_hw_aes_ocb_generic_initkey(PROV_CIPHER_CTX *vctx,
  22. const unsigned char *key,
  23. size_t keylen)
  24. {
  25. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  26. /*
  27. * We set both the encrypt and decrypt key here because decrypt
  28. * needs both. (i.e- AAD uses encrypt).
  29. */
  30. # ifdef HWAES_CAPABLE
  31. if (HWAES_CAPABLE) {
  32. OCB_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_set_decrypt_key,
  33. HWAES_encrypt, HWAES_decrypt,
  34. HWAES_ocb_encrypt, HWAES_ocb_decrypt);
  35. } else
  36. # endif
  37. # ifdef VPAES_CAPABLE
  38. if (VPAES_CAPABLE) {
  39. OCB_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_set_decrypt_key,
  40. vpaes_encrypt, vpaes_decrypt, NULL, NULL);
  41. } else
  42. # endif
  43. {
  44. OCB_SET_KEY_FN(AES_set_encrypt_key, AES_set_decrypt_key,
  45. AES_encrypt, AES_decrypt, NULL, NULL);
  46. }
  47. return 1;
  48. }
  49. # if defined(AESNI_CAPABLE)
  50. static int cipher_hw_aes_ocb_aesni_initkey(PROV_CIPHER_CTX *vctx,
  51. const unsigned char *key,
  52. size_t keylen)
  53. {
  54. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  55. OCB_SET_KEY_FN(aesni_set_encrypt_key, aesni_set_decrypt_key,
  56. aesni_encrypt, aesni_decrypt,
  57. aesni_ocb_encrypt, aesni_ocb_decrypt);
  58. return 1;
  59. }
  60. # define PROV_CIPHER_HW_declare() \
  61. static const PROV_CIPHER_HW aesni_ocb = { \
  62. cipher_hw_aes_ocb_aesni_initkey, \
  63. NULL \
  64. };
  65. # define PROV_CIPHER_HW_select() \
  66. if (AESNI_CAPABLE) \
  67. return &aesni_ocb;
  68. #elif defined(SPARC_AES_CAPABLE)
  69. static int cipher_hw_aes_ocb_t4_initkey(PROV_CIPHER_CTX *vctx,
  70. const unsigned char *key,
  71. size_t keylen)
  72. {
  73. PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
  74. OCB_SET_KEY_FN(aes_t4_set_encrypt_key, aes_t4_set_decrypt_key,
  75. aes_t4_encrypt, aes_t4_decrypt, NULL, NULL);
  76. return 1;
  77. }
  78. # define PROV_CIPHER_HW_declare() \
  79. static const PROV_CIPHER_HW aes_t4_ocb = { \
  80. cipher_hw_aes_ocb_t4_initkey, \
  81. NULL \
  82. };
  83. # define PROV_CIPHER_HW_select() \
  84. if (SPARC_AES_CAPABLE) \
  85. return &aes_t4_ocb;
  86. #else
  87. # define PROV_CIPHER_HW_declare()
  88. # define PROV_CIPHER_HW_select()
  89. # endif
  90. static const PROV_CIPHER_HW aes_generic_ocb = {
  91. cipher_hw_aes_ocb_generic_initkey,
  92. NULL
  93. };
  94. PROV_CIPHER_HW_declare()
  95. const PROV_CIPHER_HW *PROV_CIPHER_HW_aes_ocb(size_t keybits)
  96. {
  97. PROV_CIPHER_HW_select()
  98. return &aes_generic_ocb;
  99. }