2
0

ciphercommon_hw.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2019-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. #include "prov/ciphercommon.h"
  10. /*-
  11. * The generic cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr.
  12. * Used if there is no special hardware implementations.
  13. */
  14. int ossl_cipher_hw_generic_cbc(PROV_CIPHER_CTX *dat, unsigned char *out,
  15. const unsigned char *in, size_t len)
  16. {
  17. if (dat->stream.cbc)
  18. (*dat->stream.cbc) (in, out, len, dat->ks, dat->iv, dat->enc);
  19. else if (dat->enc)
  20. CRYPTO_cbc128_encrypt(in, out, len, dat->ks, dat->iv, dat->block);
  21. else
  22. CRYPTO_cbc128_decrypt(in, out, len, dat->ks, dat->iv, dat->block);
  23. return 1;
  24. }
  25. int ossl_cipher_hw_generic_ecb(PROV_CIPHER_CTX *dat, unsigned char *out,
  26. const unsigned char *in, size_t len)
  27. {
  28. size_t i, bl = dat->blocksize;
  29. if (len < bl)
  30. return 1;
  31. if (dat->stream.ecb) {
  32. (*dat->stream.ecb) (in, out, len, dat->ks, dat->enc);
  33. }
  34. else {
  35. for (i = 0, len -= bl; i <= len; i += bl)
  36. (*dat->block) (in + i, out + i, dat->ks);
  37. }
  38. return 1;
  39. }
  40. int ossl_cipher_hw_generic_ofb128(PROV_CIPHER_CTX *dat, unsigned char *out,
  41. const unsigned char *in, size_t len)
  42. {
  43. int num = dat->num;
  44. CRYPTO_ofb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->block);
  45. dat->num = num;
  46. return 1;
  47. }
  48. int ossl_cipher_hw_generic_cfb128(PROV_CIPHER_CTX *dat, unsigned char *out,
  49. const unsigned char *in, size_t len)
  50. {
  51. int num = dat->num;
  52. CRYPTO_cfb128_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
  53. dat->block);
  54. dat->num = num;
  55. return 1;
  56. }
  57. int ossl_cipher_hw_generic_cfb8(PROV_CIPHER_CTX *dat, unsigned char *out,
  58. const unsigned char *in, size_t len)
  59. {
  60. int num = dat->num;
  61. CRYPTO_cfb128_8_encrypt(in, out, len, dat->ks, dat->iv, &num, dat->enc,
  62. dat->block);
  63. dat->num = num;
  64. return 1;
  65. }
  66. int ossl_cipher_hw_generic_cfb1(PROV_CIPHER_CTX *dat, unsigned char *out,
  67. const unsigned char *in, size_t len)
  68. {
  69. int num = dat->num;
  70. if (dat->use_bits) {
  71. CRYPTO_cfb128_1_encrypt(in, out, len, dat->ks, dat->iv, &num,
  72. dat->enc, dat->block);
  73. dat->num = num;
  74. return 1;
  75. }
  76. while (len >= MAXBITCHUNK) {
  77. CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, dat->ks,
  78. dat->iv, &num, dat->enc, dat->block);
  79. len -= MAXBITCHUNK;
  80. out += MAXBITCHUNK;
  81. in += MAXBITCHUNK;
  82. }
  83. if (len)
  84. CRYPTO_cfb128_1_encrypt(in, out, len * 8, dat->ks, dat->iv, &num,
  85. dat->enc, dat->block);
  86. dat->num = num;
  87. return 1;
  88. }
  89. int ossl_cipher_hw_generic_ctr(PROV_CIPHER_CTX *dat, unsigned char *out,
  90. const unsigned char *in, size_t len)
  91. {
  92. unsigned int num = dat->num;
  93. if (dat->stream.ctr)
  94. CRYPTO_ctr128_encrypt_ctr32(in, out, len, dat->ks, dat->iv, dat->buf,
  95. &num, dat->stream.ctr);
  96. else
  97. CRYPTO_ctr128_encrypt(in, out, len, dat->ks, dat->iv, dat->buf,
  98. &num, dat->block);
  99. dat->num = num;
  100. return 1;
  101. }
  102. /*-
  103. * The chunked cipher functions for cipher modes cbc, ecb, ofb, cfb and ctr.
  104. * Used if there is no special hardware implementations.
  105. */
  106. int ossl_cipher_hw_chunked_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
  107. const unsigned char *in, size_t inl)
  108. {
  109. while (inl >= MAXCHUNK) {
  110. ossl_cipher_hw_generic_cbc(ctx, out, in, MAXCHUNK);
  111. inl -= MAXCHUNK;
  112. in += MAXCHUNK;
  113. out += MAXCHUNK;
  114. }
  115. if (inl > 0)
  116. ossl_cipher_hw_generic_cbc(ctx, out, in, inl);
  117. return 1;
  118. }
  119. int ossl_cipher_hw_chunked_cfb8(PROV_CIPHER_CTX *ctx, unsigned char *out,
  120. const unsigned char *in, size_t inl)
  121. {
  122. size_t chunk = MAXCHUNK;
  123. if (inl < chunk)
  124. chunk = inl;
  125. while (inl > 0 && inl >= chunk) {
  126. ossl_cipher_hw_generic_cfb8(ctx, out, in, inl);
  127. inl -= chunk;
  128. in += chunk;
  129. out += chunk;
  130. if (inl < chunk)
  131. chunk = inl;
  132. }
  133. return 1;
  134. }
  135. int ossl_cipher_hw_chunked_cfb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
  136. const unsigned char *in, size_t inl)
  137. {
  138. size_t chunk = MAXCHUNK;
  139. if (inl < chunk)
  140. chunk = inl;
  141. while (inl > 0 && inl >= chunk) {
  142. ossl_cipher_hw_generic_cfb128(ctx, out, in, inl);
  143. inl -= chunk;
  144. in += chunk;
  145. out += chunk;
  146. if (inl < chunk)
  147. chunk = inl;
  148. }
  149. return 1;
  150. }
  151. int ossl_cipher_hw_chunked_ofb128(PROV_CIPHER_CTX *ctx, unsigned char *out,
  152. const unsigned char *in, size_t inl)
  153. {
  154. while (inl >= MAXCHUNK) {
  155. ossl_cipher_hw_generic_ofb128(ctx, out, in, MAXCHUNK);
  156. inl -= MAXCHUNK;
  157. in += MAXCHUNK;
  158. out += MAXCHUNK;
  159. }
  160. if (inl > 0)
  161. ossl_cipher_hw_generic_ofb128(ctx, out, in, inl);
  162. return 1;
  163. }