]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/lib/NSDESCryptor.m
Security-57337.20.44.tar.gz
[apple/security.git] / OSX / libsecurity_cryptkit / lib / NSDESCryptor.m
1 /* Copyright (c) 1998,2011,2014 Apple Inc. All Rights Reserved.
2 *
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
10 *
11 * NSDESCryptor.m - DES encrypt/decrypt class
12 *
13 * Revision History
14 * ----------------
15 * 28 Mar 97 at Apple
16 * Rewrote using feeDES module.
17 * 22 Feb 96 at NeXT
18 * Created.
19 */
20
21 #import <Foundation/Foundation.h>
22 #import "NSDESCryptor.h"
23 #import "feeDES.h"
24 #import "falloc.h"
25 #import "ckutilities.h"
26 #import "feeFunctions.h"
27
28 /*
29 * Note: Our _priv ivar is actuall a feeDES pointer.
30 */
31 @implementation NSDESCryptor
32
33 + cryptorWithState:(NSData *)s {
34 return [[[self alloc] initWithState:s] autorelease];
35 }
36
37 - (void)setCryptorState:(NSData *)state {
38 if(_priv == NULL) {
39 return;
40 }
41 feeDESSetState(_priv, [state bytes], [state length]);
42 }
43
44 - initWithState:(NSData *)state {
45 feeReturn frtn;
46
47 if(_priv == NULL) {
48 _priv = feeDESNewWithState([state bytes], [state length]);
49 }
50 else {
51 frtn = feeDESSetState(_priv, [state bytes], [state length]);
52 if(frtn) {
53 NSLog(@"NSDESCryptor: bad initial state\n");
54 return nil;
55 }
56 }
57 return self;
58 }
59
60 - (void)dealloc
61 {
62 if(_priv) {
63 feeDESFree(_priv);
64 }
65 [super dealloc];
66 }
67
68 - (void)setBlockMode:(BOOL)yorn {
69 if(_priv == NULL) {
70 return;
71 }
72 if(yorn) {
73 feeDESSetBlockMode(_priv);
74 }
75 else {
76 feeDESSetChainMode(_priv);
77 }
78 }
79
80 - (NSData *)encryptData:(NSData *)input {
81 NSData *result;
82 feeReturn frtn;
83 unsigned char *cipherText;
84 unsigned cipherTextLen;
85
86 if(_priv == NULL) {
87 return nil;
88 }
89 frtn = feeDESEncrypt(_priv,
90 [input bytes],
91 [input length],
92 &cipherText,
93 &cipherTextLen);
94 if(frtn) {
95 NSLog(@"NSDESCryptor encrypt: %s", feeReturnString(frtn));
96 return nil;
97 }
98 result = [NSData dataWithBytes:cipherText length:cipherTextLen];
99 ffree(cipherText);
100 return result;
101 }
102
103 - (NSData *)decryptData:(NSData *)input {
104 NSData *result;
105 feeReturn frtn;
106 unsigned char *plainText;
107 unsigned plainTextLen;
108
109 if(_priv == NULL) {
110 return nil;
111 }
112 frtn = feeDESDecrypt(_priv,
113 [input bytes],
114 [input length],
115 &plainText,
116 &plainTextLen);
117 if(frtn) {
118 NSLog(@"NSDESCryptor decrypt: %s", feeReturnString(frtn));
119 return nil;
120 }
121 result = [NSData dataWithBytes:plainText length:plainTextLen];
122 ffree(plainText);
123 return result;
124 }
125
126 - (unsigned)keyBitsize {
127 return feeDESKeySize(_priv);
128 }
129
130 @end