/security/nss/lib/freebl/desblapi.c

http://github.com/zpao/v8monkey · C · 305 lines · 234 code · 28 blank · 43 comment · 42 complexity · 21c3b84cc92f9caff97cefdd71062217 MD5 · raw file

  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. * ***** BEGIN LICENSE BLOCK *****
  9. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  10. *
  11. * The contents of this file are subject to the Mozilla Public License Version
  12. * 1.1 (the "License"); you may not use this file except in compliance with
  13. * the License. You may obtain a copy of the License at
  14. * http://www.mozilla.org/MPL/
  15. *
  16. * Software distributed under the License is distributed on an "AS IS" basis,
  17. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  18. * for the specific language governing rights and limitations under the
  19. * License.
  20. *
  21. * The Original Code is the DES-150 library.
  22. *
  23. * The Initial Developer of the Original Code is
  24. * Nelson B. Bolyard, nelsonb@iname.com.
  25. * Portions created by the Initial Developer are Copyright (C) 1990
  26. * the Initial Developer. All Rights Reserved.
  27. *
  28. * Contributor(s):
  29. *
  30. * Alternatively, the contents of this file may be used under the terms of
  31. * either the GNU General Public License Version 2 or later (the "GPL"), or
  32. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  33. * in which case the provisions of the GPL or the LGPL are applicable instead
  34. * of those above. If you wish to allow use of your version of this file only
  35. * under the terms of either the GPL or the LGPL, and not to allow others to
  36. * use your version of this file under the terms of the MPL, indicate your
  37. * decision by deleting the provisions above and replace them with the notice
  38. * and other provisions required by the GPL or the LGPL. If you do not delete
  39. * the provisions above, a recipient may use your version of this file under
  40. * the terms of any one of the MPL, the GPL or the LGPL.
  41. *
  42. * ***** END LICENSE BLOCK ***** */
  43. #ifdef FREEBL_NO_DEPEND
  44. #include "stubs.h"
  45. #endif
  46. #include "des.h"
  47. #include <stddef.h>
  48. #include "secerr.h"
  49. #if defined(NSS_X86_OR_X64)
  50. /* Intel X86 CPUs do unaligned loads and stores without complaint. */
  51. #define COPY8B(to, from, ptr) \
  52. HALFPTR(to)[0] = HALFPTR(from)[0]; \
  53. HALFPTR(to)[1] = HALFPTR(from)[1];
  54. #elif defined(USE_MEMCPY)
  55. #define COPY8B(to, from, ptr) memcpy(to, from, 8)
  56. #else
  57. #define COPY8B(to, from, ptr) \
  58. if (((ptrdiff_t)(ptr) & 0x3) == 0) { \
  59. HALFPTR(to)[0] = HALFPTR(from)[0]; \
  60. HALFPTR(to)[1] = HALFPTR(from)[1]; \
  61. } else if (((ptrdiff_t)(ptr) & 0x1) == 0) { \
  62. SHORTPTR(to)[0] = SHORTPTR(from)[0]; \
  63. SHORTPTR(to)[1] = SHORTPTR(from)[1]; \
  64. SHORTPTR(to)[2] = SHORTPTR(from)[2]; \
  65. SHORTPTR(to)[3] = SHORTPTR(from)[3]; \
  66. } else { \
  67. BYTEPTR(to)[0] = BYTEPTR(from)[0]; \
  68. BYTEPTR(to)[1] = BYTEPTR(from)[1]; \
  69. BYTEPTR(to)[2] = BYTEPTR(from)[2]; \
  70. BYTEPTR(to)[3] = BYTEPTR(from)[3]; \
  71. BYTEPTR(to)[4] = BYTEPTR(from)[4]; \
  72. BYTEPTR(to)[5] = BYTEPTR(from)[5]; \
  73. BYTEPTR(to)[6] = BYTEPTR(from)[6]; \
  74. BYTEPTR(to)[7] = BYTEPTR(from)[7]; \
  75. }
  76. #endif
  77. #define COPY8BTOHALF(to, from) COPY8B(to, from, from)
  78. #define COPY8BFROMHALF(to, from) COPY8B(to, from, to)
  79. static void
  80. DES_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  81. {
  82. while (len) {
  83. DES_Do1Block(cx->ks0, in, out);
  84. len -= 8;
  85. in += 8;
  86. out += 8;
  87. }
  88. }
  89. static void
  90. DES_EDE3_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  91. {
  92. while (len) {
  93. DES_Do1Block(cx->ks0, in, out);
  94. len -= 8;
  95. in += 8;
  96. DES_Do1Block(cx->ks1, out, out);
  97. DES_Do1Block(cx->ks2, out, out);
  98. out += 8;
  99. }
  100. }
  101. static void
  102. DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  103. {
  104. const BYTE * bufend = in + len;
  105. HALF vec[2];
  106. while (in != bufend) {
  107. COPY8BTOHALF(vec, in);
  108. in += 8;
  109. vec[0] ^= cx->iv[0];
  110. vec[1] ^= cx->iv[1];
  111. DES_Do1Block( cx->ks0, (BYTE *)vec, (BYTE *)cx->iv);
  112. COPY8BFROMHALF(out, cx->iv);
  113. out += 8;
  114. }
  115. }
  116. static void
  117. DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  118. {
  119. const BYTE * bufend;
  120. HALF oldciphertext[2];
  121. HALF plaintext [2];
  122. for (bufend = in + len; in != bufend; ) {
  123. oldciphertext[0] = cx->iv[0];
  124. oldciphertext[1] = cx->iv[1];
  125. COPY8BTOHALF(cx->iv, in);
  126. in += 8;
  127. DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext);
  128. plaintext[0] ^= oldciphertext[0];
  129. plaintext[1] ^= oldciphertext[1];
  130. COPY8BFROMHALF(out, plaintext);
  131. out += 8;
  132. }
  133. }
  134. static void
  135. DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  136. {
  137. const BYTE * bufend = in + len;
  138. HALF vec[2];
  139. while (in != bufend) {
  140. COPY8BTOHALF(vec, in);
  141. in += 8;
  142. vec[0] ^= cx->iv[0];
  143. vec[1] ^= cx->iv[1];
  144. DES_Do1Block( cx->ks0, (BYTE *)vec, (BYTE *)cx->iv);
  145. DES_Do1Block( cx->ks1, (BYTE *)cx->iv, (BYTE *)cx->iv);
  146. DES_Do1Block( cx->ks2, (BYTE *)cx->iv, (BYTE *)cx->iv);
  147. COPY8BFROMHALF(out, cx->iv);
  148. out += 8;
  149. }
  150. }
  151. static void
  152. DES_EDE3CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len)
  153. {
  154. const BYTE * bufend;
  155. HALF oldciphertext[2];
  156. HALF plaintext [2];
  157. for (bufend = in + len; in != bufend; ) {
  158. oldciphertext[0] = cx->iv[0];
  159. oldciphertext[1] = cx->iv[1];
  160. COPY8BTOHALF(cx->iv, in);
  161. in += 8;
  162. DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext);
  163. DES_Do1Block(cx->ks1, (BYTE *)plaintext, (BYTE *)plaintext);
  164. DES_Do1Block(cx->ks2, (BYTE *)plaintext, (BYTE *)plaintext);
  165. plaintext[0] ^= oldciphertext[0];
  166. plaintext[1] ^= oldciphertext[1];
  167. COPY8BFROMHALF(out, plaintext);
  168. out += 8;
  169. }
  170. }
  171. DESContext *
  172. DES_AllocateContext(void)
  173. {
  174. return PORT_ZNew(DESContext);
  175. }
  176. SECStatus
  177. DES_InitContext(DESContext *cx, const unsigned char *key, unsigned int keylen,
  178. const unsigned char *iv, int mode, unsigned int encrypt,
  179. unsigned int unused)
  180. {
  181. DESDirection opposite;
  182. if (!cx) {
  183. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  184. return SECFailure;
  185. }
  186. cx->direction = encrypt ? DES_ENCRYPT : DES_DECRYPT;
  187. opposite = encrypt ? DES_DECRYPT : DES_ENCRYPT;
  188. switch (mode) {
  189. case NSS_DES: /* DES ECB */
  190. DES_MakeSchedule( cx->ks0, key, cx->direction);
  191. cx->worker = &DES_ECB;
  192. break;
  193. case NSS_DES_EDE3: /* DES EDE ECB */
  194. cx->worker = &DES_EDE3_ECB;
  195. if (encrypt) {
  196. DES_MakeSchedule(cx->ks0, key, cx->direction);
  197. DES_MakeSchedule(cx->ks1, key + 8, opposite);
  198. DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
  199. } else {
  200. DES_MakeSchedule(cx->ks2, key, cx->direction);
  201. DES_MakeSchedule(cx->ks1, key + 8, opposite);
  202. DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
  203. }
  204. break;
  205. case NSS_DES_CBC: /* DES CBC */
  206. COPY8BTOHALF(cx->iv, iv);
  207. cx->worker = encrypt ? &DES_CBCEn : &DES_CBCDe;
  208. DES_MakeSchedule(cx->ks0, key, cx->direction);
  209. break;
  210. case NSS_DES_EDE3_CBC: /* DES EDE CBC */
  211. COPY8BTOHALF(cx->iv, iv);
  212. if (encrypt) {
  213. cx->worker = &DES_EDE3CBCEn;
  214. DES_MakeSchedule(cx->ks0, key, cx->direction);
  215. DES_MakeSchedule(cx->ks1, key + 8, opposite);
  216. DES_MakeSchedule(cx->ks2, key + 16, cx->direction);
  217. } else {
  218. cx->worker = &DES_EDE3CBCDe;
  219. DES_MakeSchedule(cx->ks2, key, cx->direction);
  220. DES_MakeSchedule(cx->ks1, key + 8, opposite);
  221. DES_MakeSchedule(cx->ks0, key + 16, cx->direction);
  222. }
  223. break;
  224. default:
  225. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  226. return SECFailure;
  227. }
  228. return SECSuccess;
  229. }
  230. DESContext *
  231. DES_CreateContext(const BYTE * key, const BYTE *iv, int mode, PRBool encrypt)
  232. {
  233. DESContext *cx = PORT_ZNew(DESContext);
  234. SECStatus rv = DES_InitContext(cx, key, 0, iv, mode, encrypt, 0);
  235. if (rv != SECSuccess) {
  236. PORT_ZFree(cx, sizeof *cx);
  237. cx = NULL;
  238. }
  239. return cx;
  240. }
  241. void
  242. DES_DestroyContext(DESContext *cx, PRBool freeit)
  243. {
  244. if (cx) {
  245. memset(cx, 0, sizeof *cx);
  246. if (freeit)
  247. PORT_Free(cx);
  248. }
  249. }
  250. SECStatus
  251. DES_Encrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
  252. unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
  253. {
  254. if (inLen < 0 || (inLen % 8) != 0 || maxOutLen < inLen || !cx ||
  255. cx->direction != DES_ENCRYPT) {
  256. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  257. return SECFailure;
  258. }
  259. cx->worker(cx, out, in, inLen);
  260. if (outLen)
  261. *outLen = inLen;
  262. return SECSuccess;
  263. }
  264. SECStatus
  265. DES_Decrypt(DESContext *cx, BYTE *out, unsigned int *outLen,
  266. unsigned int maxOutLen, const BYTE *in, unsigned int inLen)
  267. {
  268. if (inLen < 0 || (inLen % 8) != 0 || maxOutLen < inLen || !cx ||
  269. cx->direction != DES_DECRYPT) {
  270. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  271. return SECFailure;
  272. }
  273. cx->worker(cx, out, in, inLen);
  274. if (outLen)
  275. *outLen = inLen;
  276. return SECSuccess;
  277. }