cipher_aes_gcm_hw_ppc.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2001-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. /*-
  10. * PPC support for AES GCM.
  11. * This file is included by cipher_aes_gcm_hw.c
  12. */
  13. static int aes_ppc_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
  14. size_t keylen)
  15. {
  16. PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
  17. AES_KEY *ks = &actx->ks.ks;
  18. GCM_HW_SET_KEY_CTR_FN(ks, aes_p8_set_encrypt_key, aes_p8_encrypt,
  19. aes_p8_ctr32_encrypt_blocks);
  20. return 1;
  21. }
  22. extern size_t ppc_aes_gcm_encrypt(const unsigned char *in, unsigned char *out, size_t len,
  23. const void *key, unsigned char ivec[16], u64 *Xi);
  24. extern size_t ppc_aes_gcm_decrypt(const unsigned char *in, unsigned char *out, size_t len,
  25. const void *key, unsigned char ivec[16], u64 *Xi);
  26. static inline u32 UTO32(unsigned char *buf)
  27. {
  28. return ((u32) buf[0] << 24) | ((u32) buf[1] << 16) | ((u32) buf[2] << 8) | ((u32) buf[3]);
  29. }
  30. static inline u32 add32TOU(unsigned char buf[4], u32 n)
  31. {
  32. u32 r;
  33. r = UTO32(buf);
  34. r += n;
  35. buf[0] = (unsigned char) (r >> 24) & 0xFF;
  36. buf[1] = (unsigned char) (r >> 16) & 0xFF;
  37. buf[2] = (unsigned char) (r >> 8) & 0xFF;
  38. buf[3] = (unsigned char) r & 0xFF;
  39. return r;
  40. }
  41. static size_t aes_p10_gcm_crypt(const unsigned char *in, unsigned char *out, size_t len,
  42. const void *key, unsigned char ivec[16], u64 *Xi, int encrypt)
  43. {
  44. int s = 0;
  45. int ndone = 0;
  46. int ctr_reset = 0;
  47. u64 blocks_unused;
  48. u64 nb = len / 16;
  49. u64 next_ctr = 0;
  50. unsigned char ctr_saved[12];
  51. memcpy(ctr_saved, ivec, 12);
  52. while (nb) {
  53. blocks_unused = (u64) 0xffffffffU + 1 - (u64) UTO32 (ivec + 12);
  54. if (nb > blocks_unused) {
  55. len = blocks_unused * 16;
  56. nb -= blocks_unused;
  57. next_ctr = blocks_unused;
  58. ctr_reset = 1;
  59. } else {
  60. len = nb * 16;
  61. next_ctr = nb;
  62. nb = 0;
  63. }
  64. s = encrypt ? ppc_aes_gcm_encrypt(in, out, len, key, ivec, Xi)
  65. : ppc_aes_gcm_decrypt(in, out, len, key, ivec, Xi);
  66. /* add counter to ivec */
  67. add32TOU(ivec + 12, (u32) next_ctr);
  68. if (ctr_reset) {
  69. ctr_reset = 0;
  70. in += len;
  71. out += len;
  72. }
  73. memcpy(ivec, ctr_saved, 12);
  74. ndone += s;
  75. }
  76. return ndone;
  77. }
  78. size_t ppc_aes_gcm_encrypt_wrap(const unsigned char *in, unsigned char *out, size_t len,
  79. const void *key, unsigned char ivec[16], u64 *Xi)
  80. {
  81. return aes_p10_gcm_crypt(in, out, len, key, ivec, Xi, 1);
  82. }
  83. size_t ppc_aes_gcm_decrypt_wrap(const unsigned char *in, unsigned char *out, size_t len,
  84. const void *key, unsigned char ivec[16], u64 *Xi)
  85. {
  86. return aes_p10_gcm_crypt(in, out, len, key, ivec, Xi, 0);
  87. }
  88. static const PROV_GCM_HW aes_ppc_gcm = {
  89. aes_ppc_gcm_initkey,
  90. ossl_gcm_setiv,
  91. ossl_gcm_aad_update,
  92. generic_aes_gcm_cipher_update,
  93. ossl_gcm_cipher_final,
  94. ossl_gcm_one_shot
  95. };
  96. const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
  97. {
  98. return PPC_AES_GCM_CAPABLE ? &aes_ppc_gcm : &aes_gcm;
  99. }