cipher_aes_hw_s390x.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * 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. #define s390x_aes_cbc_initkey cipher_hw_aes_initkey
  15. #define s390x_aes_cfb1_initkey cipher_hw_aes_initkey
  16. #define s390x_aes_ctr_initkey cipher_hw_aes_initkey
  17. #define s390x_aes_cbc_cipher_hw ossl_cipher_hw_generic_cbc
  18. #define s390x_aes_cfb1_cipher_hw ossl_cipher_hw_generic_cfb1
  19. #define s390x_aes_ctr_cipher_hw ossl_cipher_hw_generic_ctr
  20. #define S390X_aes_128_ofb128_CAPABLE S390X_aes_128_ofb_CAPABLE
  21. #define S390X_aes_192_ofb128_CAPABLE S390X_aes_192_ofb_CAPABLE
  22. #define S390X_aes_256_ofb128_CAPABLE S390X_aes_256_ofb_CAPABLE
  23. #define S390X_aes_128_cfb128_CAPABLE S390X_aes_128_cfb_CAPABLE
  24. #define S390X_aes_192_cfb128_CAPABLE S390X_aes_192_cfb_CAPABLE
  25. #define S390X_aes_256_cfb128_CAPABLE S390X_aes_256_cfb_CAPABLE
  26. static int s390x_aes_ecb_initkey(PROV_CIPHER_CTX *dat,
  27. const unsigned char *key, size_t keylen)
  28. {
  29. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  30. adat->plat.s390x.fc = S390X_AES_FC(keylen);
  31. if (!dat->enc)
  32. adat->plat.s390x.fc |= S390X_DECRYPT;
  33. memcpy(adat->plat.s390x.param.km.k, key, keylen);
  34. return 1;
  35. }
  36. static int s390x_aes_ecb_cipher_hw(PROV_CIPHER_CTX *dat, unsigned char *out,
  37. const unsigned char *in, size_t len)
  38. {
  39. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  40. s390x_km(in, len, out, adat->plat.s390x.fc, &adat->plat.s390x.param.km);
  41. return 1;
  42. }
  43. static int s390x_aes_ofb128_initkey(PROV_CIPHER_CTX *dat,
  44. const unsigned char *key, size_t keylen)
  45. {
  46. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  47. memcpy(adat->plat.s390x.param.kmo_kmf.k, key, keylen);
  48. adat->plat.s390x.fc = S390X_AES_FC(keylen);
  49. adat->plat.s390x.res = 0;
  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 = adat->plat.s390x.res;
  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, adat->plat.s390x.fc,
  77. adat->plat.s390x.param.kmo_kmf.k);
  78. while (rem--) {
  79. out[n] = in[n] ^ adat->plat.s390x.param.kmo_kmf.cv[n];
  80. ++n;
  81. }
  82. }
  83. memcpy(dat->iv, adat->plat.s390x.param.kmo_kmf.cv, dat->ivlen);
  84. adat->plat.s390x.res = n;
  85. return 1;
  86. }
  87. static int s390x_aes_cfb128_initkey(PROV_CIPHER_CTX *dat,
  88. const unsigned char *key, size_t keylen)
  89. {
  90. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  91. adat->plat.s390x.fc = S390X_AES_FC(keylen);
  92. adat->plat.s390x.fc |= 16 << 24; /* 16 bytes cipher feedback */
  93. if (!dat->enc)
  94. adat->plat.s390x.fc |= S390X_DECRYPT;
  95. adat->plat.s390x.res = 0;
  96. memcpy(adat->plat.s390x.param.kmo_kmf.k, key, keylen);
  97. return 1;
  98. }
  99. static int s390x_aes_cfb128_cipher_hw(PROV_CIPHER_CTX *dat, unsigned char *out,
  100. const unsigned char *in, size_t len)
  101. {
  102. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  103. int n = adat->plat.s390x.res;
  104. int rem;
  105. unsigned char tmp;
  106. memcpy(adat->plat.s390x.param.kmo_kmf.cv, dat->iv, dat->ivlen);
  107. while (n && len) {
  108. tmp = *in;
  109. *out = adat->plat.s390x.param.kmo_kmf.cv[n] ^ tmp;
  110. adat->plat.s390x.param.kmo_kmf.cv[n] = dat->enc ? *out : tmp;
  111. n = (n + 1) & 0xf;
  112. --len;
  113. ++in;
  114. ++out;
  115. }
  116. rem = len & 0xf;
  117. len &= ~(size_t)0xf;
  118. if (len) {
  119. s390x_kmf(in, len, out, adat->plat.s390x.fc,
  120. &adat->plat.s390x.param.kmo_kmf);
  121. out += len;
  122. in += len;
  123. }
  124. if (rem) {
  125. s390x_km(adat->plat.s390x.param.kmo_kmf.cv, 16,
  126. adat->plat.s390x.param.kmo_kmf.cv,
  127. S390X_AES_FC(dat->keylen), 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. adat->plat.s390x.res = 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. if (!dat->enc)
  146. adat->plat.s390x.fc |= S390X_DECRYPT;
  147. memcpy(adat->plat.s390x.param.kmo_kmf.k, key, keylen);
  148. return 1;
  149. }
  150. static int s390x_aes_cfb8_cipher_hw(PROV_CIPHER_CTX *dat, unsigned char *out,
  151. const unsigned char *in, size_t len)
  152. {
  153. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  154. memcpy(adat->plat.s390x.param.kmo_kmf.cv, dat->iv, dat->ivlen);
  155. s390x_kmf(in, len, out, adat->plat.s390x.fc,
  156. &adat->plat.s390x.param.kmo_kmf);
  157. memcpy(dat->iv, adat->plat.s390x.param.kmo_kmf.cv, dat->ivlen);
  158. return 1;
  159. }
  160. #define PROV_CIPHER_HW_declare(mode) \
  161. static const PROV_CIPHER_HW s390x_aes_##mode = { \
  162. s390x_aes_##mode##_initkey, \
  163. s390x_aes_##mode##_cipher_hw, \
  164. cipher_hw_aes_copyctx \
  165. };
  166. #define PROV_CIPHER_HW_select(mode) \
  167. if ((keybits == 128 && S390X_aes_128_##mode##_CAPABLE) \
  168. || (keybits == 192 && S390X_aes_192_##mode##_CAPABLE) \
  169. || (keybits == 256 && S390X_aes_256_##mode##_CAPABLE)) \
  170. return &s390x_aes_##mode;