cipher_aes_hw_t4.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2001-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. * Sparc t4 support for AES modes ecb, cbc, ofb, cfb, ctr.
  11. * This file is included by cipher_aes_hw.c
  12. */
  13. static int cipher_hw_aes_t4_initkey(PROV_CIPHER_CTX *dat,
  14. const unsigned char *key, size_t keylen)
  15. {
  16. int ret, bits;
  17. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  18. AES_KEY *ks = &adat->ks.ks;
  19. dat->ks = (const void *)ks; /* used by cipher_hw_generic_XXX */
  20. bits = keylen * 8;
  21. if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
  22. && !dat->enc) {
  23. ret = 0;
  24. aes_t4_set_decrypt_key(key, bits, ks);
  25. dat->block = (block128_f)aes_t4_decrypt;
  26. switch (bits) {
  27. case 128:
  28. dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
  29. (cbc128_f)aes128_t4_cbc_decrypt : NULL;
  30. break;
  31. case 192:
  32. dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
  33. (cbc128_f)aes192_t4_cbc_decrypt : NULL;
  34. break;
  35. case 256:
  36. dat->stream.cbc = dat->mode == EVP_CIPH_CBC_MODE ?
  37. (cbc128_f)aes256_t4_cbc_decrypt : NULL;
  38. break;
  39. default:
  40. ret = -1;
  41. }
  42. } else {
  43. ret = 0;
  44. aes_t4_set_encrypt_key(key, bits, ks);
  45. dat->block = (block128_f)aes_t4_encrypt;
  46. switch (bits) {
  47. case 128:
  48. if (dat->mode == EVP_CIPH_CBC_MODE)
  49. dat->stream.cbc = (cbc128_f)aes128_t4_cbc_encrypt;
  50. else if (dat->mode == EVP_CIPH_CTR_MODE)
  51. dat->stream.ctr = (ctr128_f)aes128_t4_ctr32_encrypt;
  52. else
  53. dat->stream.cbc = NULL;
  54. break;
  55. case 192:
  56. if (dat->mode == EVP_CIPH_CBC_MODE)
  57. dat->stream.cbc = (cbc128_f)aes192_t4_cbc_encrypt;
  58. else if (dat->mode == EVP_CIPH_CTR_MODE)
  59. dat->stream.ctr = (ctr128_f)aes192_t4_ctr32_encrypt;
  60. else
  61. dat->stream.cbc = NULL;
  62. break;
  63. case 256:
  64. if (dat->mode == EVP_CIPH_CBC_MODE)
  65. dat->stream.cbc = (cbc128_f)aes256_t4_cbc_encrypt;
  66. else if (dat->mode == EVP_CIPH_CTR_MODE)
  67. dat->stream.ctr = (ctr128_f)aes256_t4_ctr32_encrypt;
  68. else
  69. dat->stream.cbc = NULL;
  70. break;
  71. default:
  72. ret = -1;
  73. }
  74. }
  75. if (ret < 0) {
  76. ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. #define PROV_CIPHER_HW_declare(mode) \
  82. static const PROV_CIPHER_HW aes_t4_##mode = { \
  83. cipher_hw_aes_t4_initkey, \
  84. ossl_cipher_hw_generic_##mode, \
  85. cipher_hw_aes_copyctx \
  86. };
  87. #define PROV_CIPHER_HW_select(mode) \
  88. if (SPARC_AES_CAPABLE) \
  89. return &aes_t4_##mode;