desblapi.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * desblapi.c
  3. *
  4. * core source file for DES-150 library
  5. * Implement DES Modes of Operation and Triple-DES.
  6. * Adapt DES-150 to blapi API.
  7. *
  8. * This Source Code Form is subject to the terms of the Mozilla Public
  9. * License, v. 2.0. If a copy of the MPL was not distributed with this
  10. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  11. #ifdef FREEBL_NO_DEPEND
  12. #include "stubs.h"
  13. #endif
  14. #include "des.h"
  15. #include "blapii.h"
  16. #include <stddef.h>
  17. #include "secerr.h"
  18. #if defined(NSS_X86_OR_X64)
  19. /* Intel X86 CPUs do unaligned loads and stores without complaint. */
  20. #define COPY8B(to, from, ptr) \
  21. HALFPTR(to) \
  22. [0] = HALFPTR(from)[0]; \
  23. HALFPTR(to) \
  24. [1] = HALFPTR(from)[1];
  25. #else
  26. #define COPY8B(to, from, ptr) memcpy(to, from, 8)
  27. #endif
  28. #define COPY8BTOHALF(to, from) COPY8B(to, from, from)
  29. #define COPY8BFROMHALF(to, from) COPY8B(to, from, to)
  30. static void
  31. DES_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  32. {
  33. while (len) {
  34. DES_Do1Block(cx->ks0, in, out);
  35. len -= 8;
  36. in += 8;
  37. out += 8;
  38. }
  39. }
  40. static void
  41. DES_EDE3_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  42. {
  43. while (len) {
  44. DES_Do1Block(cx->ks0, in, out);
  45. len -= 8;
  46. in += 8;
  47. DES_Do1Block(cx->ks1, out, out);
  48. DES_Do1Block(cx->ks2, out, out);
  49. out += 8;
  50. }
  51. }
  52. static void NO_SANITIZE_ALIGNMENT
  53. DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  54. {
  55. const BYTE *bufend = in + len;
  56. HALF vec[2];
  57. while (in != bufend) {
  58. COPY8BTOHALF(vec, in);
  59. in += 8;
  60. vec[0] ^= cx->iv[0];
  61. vec[1] ^= cx->iv[1];
  62. DES_Do1Block(cx->ks0, (BYTE *)vec, (BYTE *)cx->iv);
  63. COPY8BFROMHALF(out, cx->iv);
  64. out += 8;
  65. }
  66. }
  67. static void NO_SANITIZE_ALIGNMENT
  68. DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  69. {
  70. const BYTE *bufend;
  71. HALF oldciphertext[2];
  72. HALF plaintext[2];
  73. for (bufend = in + len; in != bufend;) {
  74. oldciphertext[0] = cx->iv[0];
  75. oldciphertext[1] = cx->iv[1];
  76. COPY8BTOHALF(cx->iv, in);
  77. in += 8;
  78. DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext);
  79. plaintext[0] ^= oldciphertext[0];
  80. plaintext[1] ^= oldciphertext[1];
  81. COPY8BFROMHALF(out, plaintext);
  82. out += 8;
  83. }
  84. }
  85. static void NO_SANITIZE_ALIGNMENT
  86. DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  87. {
  88. const BYTE *bufend = in + len;
  89. HALF vec[2];
  90. while (in != bufend) {
  91. COPY8BTOHALF(vec, in);
  92. in += 8;
  93. vec[0] ^= cx->iv[0];
  94. vec[1] ^= cx->iv[1];
  95. DES_Do1Block(cx->ks0, (BYTE *)vec, (BYTE *)cx->iv);
  96. DES_Do1Block(cx->ks1, (BYTE *)cx->iv, (BYTE *)cx->iv);
  97. DES_Do1Block(cx->ks2, (BYTE *)cx->iv, (BYTE *)cx->iv);
  98. COPY8BFROMHALF(out, cx->iv);
  99. out += 8;
  100. }
  101. }
  102. static void NO_SANITIZE_ALIGNMENT
  103. DES_EDE3CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  104. {
  105. const BYTE *bufend;
  106. HALF oldciphertext[2];
  107. HALF plaintext[2];
  108. for (bufend = in + len; in != bufend;) {
  109. oldciphertext[0] = cx->iv[0];
  110. oldciphertext[1] = cx->iv[1];
  111. COPY8BTOHALF(cx->iv, in);
  112. in += 8;
  113. DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext);
  114. DES_Do1Block(cx->ks1, (BYTE *)plaintext, (BYTE *)plaintext);
  115. DES_Do1Block(cx->ks2, (BYTE *)plaintext, (BYTE *)plaintext);
  116. plaintext[0] ^= oldciphertext[0];
  117. plaintext[1] ^= oldciphertext[1];
  118. COPY8BFROMHALF(out, plaintext);
  119. out += 8;
  120. }
  121. }
  122. DESContext *
  123. DES_AllocateContext(void)
  124. {
  125. return PORT_ZNew(DESContext);
  126. }
  127. SECStatus
  128. DES_InitContext(DESContext *cx, const unsigned char *key, unsigned int keylen,
  129. const unsigned char *iv, int mode, unsigned int encrypt,
  130. unsigned int unused)
  131. {
  132. DESDirection opposite;
  133. if (!cx) {
  134. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  135. return SECFailure;
  136. }
  137. cx->direction = encrypt ? DES_ENCRYPT : DES_DECRYPT;
  138. opposite = encrypt ? DES_DECRYPT : DES_ENCRYPT;
  139. switch (mode) {
  140. case NSS_DES: /* DES ECB */
  141. DES_MakeSchedule(cx->ks0, key, cx->direction);
  142. cx->worker = &DES_ECB;
  143. break;
  144. case NSS_DES_EDE3: /* DES EDE ECB */
  145. cx->worker = &DES_EDE3_ECB;
  146. if (encrypt) {
  147. DES_MakeSchedule(cx->ks0, key, cx->direction);
  148. DES_MakeSchedule(cx->ks1, key + 8, opposite);
  149. DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
  150. } else {
  151. DES_MakeSchedule(cx->ks2, key, cx->direction);
  152. DES_MakeSchedule(cx->ks1, key + 8, opposite);
  153. DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
  154. }
  155. break;
  156. case NSS_DES_CBC: /* DES CBC */
  157. COPY8BTOHALF(cx->iv, iv);
  158. cx->worker = encrypt ? &DES_CBCEn : &DES_CBCDe;
  159. DES_MakeSchedule(cx->ks0, key, cx->direction);
  160. break;
  161. case NSS_DES_EDE3_CBC: /* DES EDE CBC */
  162. COPY8BTOHALF(cx->iv, iv);
  163. if (encrypt) {
  164. cx->worker = &DES_EDE3CBCEn;
  165. DES_MakeSchedule(cx->ks0, key, cx->direction);
  166. DES_MakeSchedule(cx->ks1, key + 8, opposite);
  167. DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
  168. } else {
  169. cx->worker = &DES_EDE3CBCDe;
  170. DES_MakeSchedule(cx->ks2, key, cx->direction);
  171. DES_MakeSchedule(cx->ks1, key + 8, opposite);
  172. DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
  173. }
  174. break;
  175. default:
  176. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  177. return SECFailure;
  178. }
  179. return SECSuccess;
  180. }
  181. DESContext *
  182. DES_CreateContext(const BYTE *key, const BYTE *iv, int mode, PRBool encrypt)
  183. {
  184. DESContext *cx = PORT_ZNew(DESContext);
  185. SECStatus rv = DES_InitContext(cx, key, 0, iv, mode, encrypt, 0);
  186. if (rv != SECSuccess) {
  187. PORT_ZFree(cx, sizeof *cx);
  188. cx = NULL;
  189. }
  190. return cx;
  191. }
  192. void
  193. DES_DestroyContext(DESContext *cx, PRBool freeit)
  194. {
  195. if (cx) {
  196. memset(cx, 0, sizeof *cx);
  197. if (freeit)
  198. PORT_Free(cx);
  199. }
  200. }
  201. SECStatus
  202. DES_Encrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
  203. unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
  204. {
  205. if ((inLen % 8) != 0 || maxOutLen < inLen || !cx ||
  206. cx->direction != DES_ENCRYPT) {
  207. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  208. return SECFailure;
  209. }
  210. cx->worker(cx, out, in, inLen);
  211. if (outLen)
  212. *outLen = inLen;
  213. return SECSuccess;
  214. }
  215. SECStatus
  216. DES_Decrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
  217. unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
  218. {
  219. if ((inLen % 8) != 0 || maxOutLen < inLen || !cx ||
  220. cx->direction != DES_DECRYPT) {
  221. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  222. return SECFailure;
  223. }
  224. cx->worker(cx, out, in, inLen);
  225. if (outLen)
  226. *outLen = inLen;
  227. return SECSuccess;
  228. }