The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sapphire.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * sapphire.h - the Saphire II stream cipher class
4  *
5  * $Id: sapphire.h 3786 2020-08-30 11:35:14Z scribe $
6  *
7  * Copyright 1999-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 /******************************************************************************
24  *
25  * Original license notice & credits:
26  * Dedicated to the Public Domain the author and inventor
27  * (Michael Paul Johnson). This code comes with no warranty.
28  * Use it at your own risk.
29  * Ported from the Pascal implementation of the Sapphire Stream
30  * Cipher 9 December 1994.
31  * Added hash-specific functions 27 December 1994.
32  * Made index variable initialization key-dependent,
33  * made the output function more resistant to cryptanalysis,
34  * and renamed to Sapphire II Stream Cipher 2 January 1995.
35  *
36  * unsigned char is assumed to be 8 bits. If it is not, the
37  * results of assignments need to be reduced to 8 bits with
38  * & 0xFF or % 0x100, whichever is faster.
39  */
40 
41 #ifndef NULL
42 #define NULL 0
43 #endif
44 
45 #include <defs.h>
46 
48 
49 class Sapphire {
50 
51 private:
52  // These variables comprise the state of the state machine.
53  unsigned char cards[256]; // A permutation of 0-255.
54  unsigned char rotor, // Index that rotates smoothly
55  ratchet, // Index that moves erratically
56  avalanche, // Index heavily data dependent
57  last_plain, // Last plain text byte
58  last_cipher; // Last cipher text byte
59 
60  // This function is used by initialize(), which is called by the
61  // constructor.
62  unsigned char keyrand(int limit, unsigned char *user_key,
63  unsigned char keysize, unsigned char *rsum,
64  unsigned *keypos);
65 public:
66  Sapphire(unsigned char *key = NULL, // Calls initialize if a real
67  unsigned char keysize = 0); // key is provided. If none
68  // is provided, call initialize
69  // before encrypt or decrypt.
70 
71  ~Sapphire(); // Destroy cipher state information.
72 
73  void initialize(unsigned char *key, // User key is used to set
74  unsigned char keysize); // up state information.
75  void hash_init(void); // Set up default hash.
76  unsigned char encrypt(unsigned char b = 0); // Encrypt byte
77  // or get a random byte.
78  unsigned char decrypt(unsigned char b); // Decrypt byte.
79  void hash_final(unsigned char *hash, // Copy hash value to hash
80  unsigned char hashlength = 20); // Hash length (16-32)
81  void burn(void); // Destroy cipher state information.
82 };
83 
84 
void burn(void)
Definition: sapphire.cpp:157
#define SWORD_NAMESPACE_START
Definition: defs.h:39
unsigned char avalanche
Definition: sapphire.h:54
unsigned char cards[256]
Definition: sapphire.h:53
unsigned char encrypt(unsigned char b=0)
Definition: sapphire.cpp:169
void hash_final(unsigned char *hash, unsigned char hashlength=20)
Definition: sapphire.cpp:225
#define NULL
Definition: regex.c:247
void initialize(unsigned char *key, unsigned char keysize)
Definition: sapphire.cpp:75
void hash_init(void)
Definition: sapphire.cpp:130
Sapphire(unsigned char *key=NULL, unsigned char keysize=0)
Definition: sapphire.cpp:151
unsigned char last_cipher
Definition: sapphire.h:54
unsigned char last_plain
Definition: sapphire.h:54
unsigned char decrypt(unsigned char b)
Definition: sapphire.cpp:200
unsigned char rotor
Definition: sapphire.h:54
unsigned char keyrand(int limit, unsigned char *user_key, unsigned char keysize, unsigned char *rsum, unsigned *keypos)
Definition: sapphire.cpp:44
#define SWORD_NAMESPACE_END
Definition: defs.h:40
unsigned char ratchet
Definition: sapphire.h:54