cipher_aes_hw_s390x.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #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. adat->plat.s390x.res = 0;
  51. return 1;
  52. }
  53. static int s390x_aes_ofb128_cipher_hw(PROV_CIPHER_CTX *dat, unsigned char *out,
  54. const unsigned char *in, size_t len)
  55. {
  56. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  57. int n = adat->plat.s390x.res;
  58. int rem;
  59. memcpy(adat->plat.s390x.param.kmo_kmf.cv, dat->iv, dat->ivlen);
  60. while (n && len) {
  61. *out = *in ^ adat->plat.s390x.param.kmo_kmf.cv[n];
  62. n = (n + 1) & 0xf;
  63. --len;
  64. ++in;
  65. ++out;
  66. }
  67. rem = len & 0xf;
  68. len &= ~(size_t)0xf;
  69. if (len) {
  70. s390x_kmo(in, len, out, adat->plat.s390x.fc,
  71. &adat->plat.s390x.param.kmo_kmf);
  72. out += len;
  73. in += len;
  74. }
  75. if (rem) {
  76. s390x_km(adat->plat.s390x.param.kmo_kmf.cv, 16,
  77. adat->plat.s390x.param.kmo_kmf.cv,
  78. adat->plat.s390x.fc,
  79. adat->plat.s390x.param.kmo_kmf.k);
  80. while (rem--) {
  81. out[n] = in[n] ^ adat->plat.s390x.param.kmo_kmf.cv[n];
  82. ++n;
  83. }
  84. }
  85. memcpy(dat->iv, adat->plat.s390x.param.kmo_kmf.cv, dat->ivlen);
  86. adat->plat.s390x.res = n;
  87. return 1;
  88. }
  89. static int s390x_aes_cfb128_initkey(PROV_CIPHER_CTX *dat,
  90. const unsigned char *key, size_t keylen)
  91. {
  92. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  93. adat->plat.s390x.fc = S390X_AES_FC(keylen);
  94. adat->plat.s390x.fc |= 16 << 24; /* 16 bytes cipher feedback */
  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. unsigned int modifier = adat->base.enc ? 0 : S390X_DECRYPT;
  104. int n = adat->plat.s390x.res;
  105. int rem;
  106. unsigned char tmp;
  107. memcpy(adat->plat.s390x.param.kmo_kmf.cv, dat->iv, dat->ivlen);
  108. while (n && len) {
  109. tmp = *in;
  110. *out = adat->plat.s390x.param.kmo_kmf.cv[n] ^ tmp;
  111. adat->plat.s390x.param.kmo_kmf.cv[n] = dat->enc ? *out : tmp;
  112. n = (n + 1) & 0xf;
  113. --len;
  114. ++in;
  115. ++out;
  116. }
  117. rem = len & 0xf;
  118. len &= ~(size_t)0xf;
  119. if (len) {
  120. s390x_kmf(in, len, out, adat->plat.s390x.fc | modifier,
  121. &adat->plat.s390x.param.kmo_kmf);
  122. out += len;
  123. in += len;
  124. }
  125. if (rem) {
  126. s390x_km(adat->plat.s390x.param.kmo_kmf.cv, 16,
  127. adat->plat.s390x.param.kmo_kmf.cv,
  128. S390X_AES_FC(dat->keylen),
  129. adat->plat.s390x.param.kmo_kmf.k);
  130. while (rem--) {
  131. tmp = in[n];
  132. out[n] = adat->plat.s390x.param.kmo_kmf.cv[n] ^ tmp;
  133. adat->plat.s390x.param.kmo_kmf.cv[n] = dat->enc ? out[n] : tmp;
  134. ++n;
  135. }
  136. }
  137. memcpy(dat->iv, adat->plat.s390x.param.kmo_kmf.cv, dat->ivlen);
  138. adat->plat.s390x.res = n;
  139. return 1;
  140. }
  141. static int s390x_aes_cfb8_initkey(PROV_CIPHER_CTX *dat,
  142. const unsigned char *key, size_t keylen)
  143. {
  144. PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
  145. adat->plat.s390x.fc = S390X_AES_FC(keylen);
  146. adat->plat.s390x.fc |= 1 << 24; /* 1 byte cipher feedback */
  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. unsigned int modifier = adat->base.enc ? 0 : S390X_DECRYPT;
  155. memcpy(adat->plat.s390x.param.kmo_kmf.cv, dat->iv, dat->ivlen);
  156. s390x_kmf(in, len, out, adat->plat.s390x.fc | modifier,
  157. &adat->plat.s390x.param.kmo_kmf);
  158. memcpy(dat->iv, adat->plat.s390x.param.kmo_kmf.cv, dat->ivlen);
  159. return 1;
  160. }
  161. #define PROV_CIPHER_HW_declare(mode) \
  162. static const PROV_CIPHER_HW s390x_aes_##mode = { \
  163. s390x_aes_##mode##_initkey, \
  164. s390x_aes_##mode##_cipher_hw, \
  165. cipher_hw_aes_copyctx \
  166. };
  167. #define PROV_CIPHER_HW_select(mode) \
  168. if ((keybits == 128 && S390X_aes_128_##mode##_CAPABLE) \
  169. || (keybits == 192 && S390X_aes_192_##mode##_CAPABLE) \
  170. || (keybits == 256 && S390X_aes_256_##mode##_CAPABLE)) \
  171. return &s390x_aes_##mode;