cipher_aes_hw_s390x.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2001-2024 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. * IBM S390X support for AES modes ecb, cbc, ofb, cfb, ctr.
  11. * This file is included by cipher_aes_hw.c
  12. */
  13. #include "s390x_arch.h"
  14. #include <stdio.h>
  15. #define s390x_aes_cbc_initkey cipher_hw_aes_initkey
  16. #define s390x_aes_cfb1_initkey cipher_hw_aes_initkey
  17. #define s390x_aes_ctr_initkey cipher_hw_aes_initkey
  18. #define s390x_aes_cbc_cipher_hw ossl_cipher_hw_generic_cbc
  19. #define s390x_aes_cfb1_cipher_hw ossl_cipher_hw_generic_cfb1
  20. #define s390x_aes_ctr_cipher_hw ossl_cipher_hw_generic_ctr
  21. #define S390X_aes_128_ofb128_CAPABLE S390X_aes_128_ofb_CAPABLE
  22. #define S390X_aes_192_ofb128_CAPABLE S390X_aes_192_ofb_CAPABLE
  23. #define S390X_aes_256_ofb128_CAPABLE S390X_aes_256_ofb_CAPABLE
  24. #define S390X_aes_128_cfb128_CAPABLE S390X_aes_128_cfb_CAPABLE
  25. #define S390X_aes_192_cfb128_CAPABLE S390X_aes_192_cfb_CAPABLE
  26. #define S390X_aes_256_cfb128_CAPABLE S390X_aes_256_cfb_CAPABLE
  27. static int s390x_aes_ecb_initkey(PROV_CIPHER_CTX *dat,
  28. const unsigned char *key, size_t keylen)
  29. {
  30. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  31. adat->plat.s390x.fc = S390X_AES_FC(keylen);
  32. memcpy(adat->plat.s390x.param.km.k, key, keylen);
  33. return 1;
  34. }
  35. static int s390x_aes_ecb_cipher_hw(PROV_CIPHER_CTX *dat, unsigned char *out,
  36. const unsigned char *in, size_t len)
  37. {
  38. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  39. unsigned int modifier = adat->base.enc ? 0 : S390X_DECRYPT;
  40. s390x_km(in, len, out, adat->plat.s390x.fc | modifier,
  41. &adat->plat.s390x.param.km);
  42. return 1;
  43. }
  44. static int s390x_aes_ofb128_initkey(PROV_CIPHER_CTX *dat,
  45. const unsigned char *key, size_t keylen)
  46. {
  47. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  48. memcpy(adat->plat.s390x.param.kmo_kmf.k, key, keylen);
  49. adat->plat.s390x.fc = S390X_AES_FC(keylen);
  50. return 1;
  51. }
  52. static int s390x_aes_ofb128_cipher_hw(PROV_CIPHER_CTX *dat, unsigned char *out,
  53. const unsigned char *in, size_t len)
  54. {
  55. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  56. int n = dat->num;
  57. int rem;
  58. memcpy(adat->plat.s390x.param.kmo_kmf.cv, dat->iv, dat->ivlen);
  59. while (n && len) {
  60. *out = *in ^ adat->plat.s390x.param.kmo_kmf.cv[n];
  61. n = (n + 1) & 0xf;
  62. --len;
  63. ++in;
  64. ++out;
  65. }
  66. rem = len & 0xf;
  67. len &= ~(size_t)0xf;
  68. if (len) {
  69. s390x_kmo(in, len, out, adat->plat.s390x.fc,
  70. &adat->plat.s390x.param.kmo_kmf);
  71. out += len;
  72. in += len;
  73. }
  74. if (rem) {
  75. s390x_km(adat->plat.s390x.param.kmo_kmf.cv, 16,
  76. adat->plat.s390x.param.kmo_kmf.cv,
  77. adat->plat.s390x.fc,
  78. adat->plat.s390x.param.kmo_kmf.k);
  79. while (rem--) {
  80. out[n] = in[n] ^ adat->plat.s390x.param.kmo_kmf.cv[n];
  81. ++n;
  82. }
  83. }
  84. memcpy(dat->iv, adat->plat.s390x.param.kmo_kmf.cv, dat->ivlen);
  85. dat->num = n;
  86. return 1;
  87. }
  88. static int s390x_aes_cfb128_initkey(PROV_CIPHER_CTX *dat,
  89. const unsigned char *key, size_t keylen)
  90. {
  91. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  92. adat->plat.s390x.fc = S390X_AES_FC(keylen);
  93. adat->plat.s390x.fc |= 16 << 24; /* 16 bytes cipher feedback */
  94. memcpy(adat->plat.s390x.param.kmo_kmf.k, key, keylen);
  95. return 1;
  96. }
  97. static int s390x_aes_cfb128_cipher_hw(PROV_CIPHER_CTX *dat, unsigned char *out,
  98. const unsigned char *in, size_t len)
  99. {
  100. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  101. unsigned int modifier = adat->base.enc ? 0 : S390X_DECRYPT;
  102. int n = dat->num;
  103. int rem;
  104. unsigned char tmp;
  105. memcpy(adat->plat.s390x.param.kmo_kmf.cv, dat->iv, dat->ivlen);
  106. while (n && len) {
  107. tmp = *in;
  108. *out = adat->plat.s390x.param.kmo_kmf.cv[n] ^ tmp;
  109. adat->plat.s390x.param.kmo_kmf.cv[n] = dat->enc ? *out : tmp;
  110. n = (n + 1) & 0xf;
  111. --len;
  112. ++in;
  113. ++out;
  114. }
  115. rem = len & 0xf;
  116. len &= ~(size_t)0xf;
  117. if (len) {
  118. s390x_kmf(in, len, out, adat->plat.s390x.fc | modifier,
  119. &adat->plat.s390x.param.kmo_kmf);
  120. out += len;
  121. in += len;
  122. }
  123. if (rem) {
  124. s390x_km(adat->plat.s390x.param.kmo_kmf.cv, 16,
  125. adat->plat.s390x.param.kmo_kmf.cv,
  126. S390X_AES_FC(dat->keylen),
  127. adat->plat.s390x.param.kmo_kmf.k);
  128. while (rem--) {
  129. tmp = in[n];
  130. out[n] = adat->plat.s390x.param.kmo_kmf.cv[n] ^ tmp;
  131. adat->plat.s390x.param.kmo_kmf.cv[n] = dat->enc ? out[n] : tmp;
  132. ++n;
  133. }
  134. }
  135. memcpy(dat->iv, adat->plat.s390x.param.kmo_kmf.cv, dat->ivlen);
  136. dat->num = n;
  137. return 1;
  138. }
  139. static int s390x_aes_cfb8_initkey(PROV_CIPHER_CTX *dat,
  140. const unsigned char *key, size_t keylen)
  141. {
  142. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  143. adat->plat.s390x.fc = S390X_AES_FC(keylen);
  144. adat->plat.s390x.fc |= 1 << 24; /* 1 byte cipher feedback */
  145. memcpy(adat->plat.s390x.param.kmo_kmf.k, key, keylen);
  146. return 1;
  147. }
  148. static int s390x_aes_cfb8_cipher_hw(PROV_CIPHER_CTX *dat, unsigned char *out,
  149. const unsigned char *in, size_t len)
  150. {
  151. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  152. unsigned int modifier = adat->base.enc ? 0 : S390X_DECRYPT;
  153. memcpy(adat->plat.s390x.param.kmo_kmf.cv, dat->iv, dat->ivlen);
  154. s390x_kmf(in, len, out, adat->plat.s390x.fc | modifier,
  155. &adat->plat.s390x.param.kmo_kmf);
  156. memcpy(dat->iv, adat->plat.s390x.param.kmo_kmf.cv, dat->ivlen);
  157. return 1;
  158. }
  159. #define PROV_CIPHER_HW_declare(mode) \
  160. static const PROV_CIPHER_HW s390x_aes_##mode = { \
  161. s390x_aes_##mode##_initkey, \
  162. s390x_aes_##mode##_cipher_hw, \
  163. cipher_hw_aes_copyctx \
  164. };
  165. #define PROV_CIPHER_HW_select(mode) \
  166. if ((keybits == 128 && S390X_aes_128_##mode##_CAPABLE) \
  167. || (keybits == 192 && S390X_aes_192_##mode##_CAPABLE) \
  168. || (keybits == 256 && S390X_aes_256_##mode##_CAPABLE)) \
  169. return &s390x_aes_##mode;