cipher_aes_gcm_hw_ppc.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. static inline u32 UTO32(unsigned char *buf)
  23. {
  24. return ((u32) buf[0] << 24) | ((u32) buf[1] << 16) | ((u32) buf[2] << 8) | ((u32) buf[3]);
  25. }
  26. static inline u32 add32TOU(unsigned char buf[4], u32 n)
  27. {
  28. u32 r;
  29. r = UTO32(buf);
  30. r += n;
  31. buf[0] = (unsigned char) (r >> 24) & 0xFF;
  32. buf[1] = (unsigned char) (r >> 16) & 0xFF;
  33. buf[2] = (unsigned char) (r >> 8) & 0xFF;
  34. buf[3] = (unsigned char) r & 0xFF;
  35. return r;
  36. }
  37. static size_t ppc_aes_gcm_crypt(const unsigned char *in, unsigned char *out, size_t len,
  38. const void *key, unsigned char ivec[16], u64 *Xi, int encrypt)
  39. {
  40. int s = 0;
  41. int ndone = 0;
  42. int ctr_reset = 0;
  43. u64 blocks_unused;
  44. u64 nb = len / 16;
  45. u64 next_ctr = 0;
  46. unsigned char ctr_saved[12];
  47. memcpy(ctr_saved, ivec, 12);
  48. while (nb) {
  49. blocks_unused = (u64) 0xffffffffU + 1 - (u64) UTO32 (ivec + 12);
  50. if (nb > blocks_unused) {
  51. len = blocks_unused * 16;
  52. nb -= blocks_unused;
  53. next_ctr = blocks_unused;
  54. ctr_reset = 1;
  55. } else {
  56. len = nb * 16;
  57. next_ctr = nb;
  58. nb = 0;
  59. }
  60. s = encrypt ? ppc_aes_gcm_encrypt(in, out, len, key, ivec, Xi)
  61. : ppc_aes_gcm_decrypt(in, out, len, key, ivec, Xi);
  62. /* add counter to ivec */
  63. add32TOU(ivec + 12, (u32) next_ctr);
  64. if (ctr_reset) {
  65. ctr_reset = 0;
  66. in += len;
  67. out += len;
  68. }
  69. memcpy(ivec, ctr_saved, 12);
  70. ndone += s;
  71. }
  72. return ndone;
  73. }
  74. static int ppc_aes_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
  75. size_t len, unsigned char *out)
  76. {
  77. if (ctx->enc) {
  78. if (ctx->ctr != NULL) {
  79. size_t bulk = 0;
  80. if (len >= AES_GCM_ENC_BYTES && AES_GCM_ASM_PPC(ctx)) {
  81. size_t res = (16 - ctx->gcm.mres) % 16;
  82. if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, res))
  83. return 0;
  84. bulk = ppc_aes_gcm_crypt(in + res, out + res, len - res,
  85. ctx->gcm.key,
  86. ctx->gcm.Yi.c, ctx->gcm.Xi.u, 1);
  87. ctx->gcm.len.u[1] += bulk;
  88. bulk += res;
  89. }
  90. if (CRYPTO_gcm128_encrypt_ctr32(&ctx->gcm, in + bulk, out + bulk,
  91. len - bulk, ctx->ctr))
  92. return 0;
  93. } else {
  94. if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len))
  95. return 0;
  96. }
  97. } else {
  98. if (ctx->ctr != NULL) {
  99. size_t bulk = 0;
  100. if (len >= AES_GCM_DEC_BYTES && AES_GCM_ASM_PPC(ctx)) {
  101. size_t res = (16 - ctx->gcm.mres) % 16;
  102. if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, res))
  103. return -1;
  104. bulk = ppc_aes_gcm_crypt(in + res, out + res, len - res,
  105. ctx->gcm.key,
  106. ctx->gcm.Yi.c, ctx->gcm.Xi.u, 0);
  107. ctx->gcm.len.u[1] += bulk;
  108. bulk += res;
  109. }
  110. if (CRYPTO_gcm128_decrypt_ctr32(&ctx->gcm, in + bulk, out + bulk,
  111. len - bulk, ctx->ctr))
  112. return 0;
  113. } else {
  114. if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len))
  115. return 0;
  116. }
  117. }
  118. return 1;
  119. }
  120. static const PROV_GCM_HW aes_ppc_gcm = {
  121. aes_ppc_gcm_initkey,
  122. ossl_gcm_setiv,
  123. ossl_gcm_aad_update,
  124. ppc_aes_gcm_cipher_update,
  125. ossl_gcm_cipher_final,
  126. ossl_gcm_one_shot
  127. };
  128. const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
  129. {
  130. return PPC_AES_GCM_CAPABLE ? &aes_ppc_gcm : &aes_gcm;
  131. }