1 /* 2 * Copyright (c) 2014-2018, Andrew Romanenko <melanhit@gmail.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef AKMOS_H 30 #define AKMOS_H 31 32 /* version */ 33 #define AKMOS_MAJOR_VERSION 0 34 #define AKMOS_MINOR_VERSION 7 35 #define AKMOS_PATCH_VERSION 0 36 37 typedef enum { 38 /* block cipher algo */ 39 AKMOS_ALGO_ANUBIS = 0x00000001, 40 AKMOS_ALGO_BLOWFISH = 0x00000002, 41 AKMOS_ALGO_CAMELLIA = 0x00000003, 42 AKMOS_ALGO_CAST6 = 0x00000004, 43 AKMOS_ALGO_RC6 = 0x00000005, 44 AKMOS_ALGO_RIJNDAEL = 0x00000006, 45 AKMOS_ALGO_SEED = 0x00000007, 46 AKMOS_ALGO_SERPENT = 0x00000008, 47 AKMOS_ALGO_THREEFISH_256 = 0x00000009, 48 AKMOS_ALGO_THREEFISH_512 = 0x0000000a, 49 AKMOS_ALGO_THREEFISH_1024 = 0x0000000b, 50 AKMOS_ALGO_TWOFISH = 0x0000000c, 51 52 /* stream cipher algo */ 53 AKMOS_ALGO_SALSA = 0x00000100, 54 AKMOS_ALGO_CHACHA = 0x00000200, 55 56 /* digest algo */ 57 AKMOS_ALGO_RIPEMD_160 = 0x00001000, 58 AKMOS_ALGO_RIPEMD_256 = 0x00002000, 59 AKMOS_ALGO_RIPEMD_320 = 0x00003000, 60 AKMOS_ALGO_SHA1 = 0x00004000, 61 AKMOS_ALGO_SHA2_224 = 0x00005000, 62 AKMOS_ALGO_SHA2_256 = 0x00006000, 63 AKMOS_ALGO_SHA2_384 = 0x00007000, 64 AKMOS_ALGO_SHA2_512 = 0x00008000, 65 AKMOS_ALGO_SHA3_224 = 0x00009000, 66 AKMOS_ALGO_SHA3_256 = 0x0000a000, 67 AKMOS_ALGO_SHA3_384 = 0x0000b000, 68 AKMOS_ALGO_SHA3_512 = 0x0000c000, 69 AKMOS_ALGO_TIGER = 0x0000d000, 70 AKMOS_ALGO_WHIRLPOOL = 0x0000e000, 71 AKMOS_ALGO_SKEIN_256 = 0x0000f000, 72 AKMOS_ALGO_SKEIN_512 = 0x00010000, 73 AKMOS_ALGO_SKEIN_1024 = 0x00020000, 74 75 /* binary to text */ 76 AKMOS_ALGO_BASE64 = 0x00100000, 77 AKMOS_ALGO_BASE64URL = 0x00200000, 78 79 /* cipher algo flag */ 80 AKMOS_ALGO_FLAG_EDE = 0x10000000, 81 AKMOS_ALGO_FLAG_EEE = 0x20000000 82 } akmos_algo_id; 83 84 typedef enum { 85 /* block cipher mode */ 86 AKMOS_MODE_ECB = 0x00000001, 87 AKMOS_MODE_CBC = 0x00000002, 88 AKMOS_MODE_OFB = 0x00000003, 89 AKMOS_MODE_CTR = 0x00000004, 90 AKMOS_MODE_CFB = 0x00000005, 91 /* 1 byte */ 92 AKMOS_MODE_CFB1 = 0x00000006, 93 94 /* MAC mode */ 95 AKMOS_MODE_HMAC = 0x00000010, 96 AKMOS_MODE_CBCMAC = 0x00000020, 97 AKMOS_MODE_CMAC = 0x00000030, 98 99 /* cipher mode flag */ 100 AKMOS_MODE_ENCRYPT = 0x10000000, 101 AKMOS_MODE_DECRYPT = 0x20000000, 102 AKMOS_MODE_ENCODE = AKMOS_MODE_ENCRYPT, 103 AKMOS_MODE_DECODE = AKMOS_MODE_DECRYPT 104 } akmos_mode_id; 105 106 /* Cipher */ 107 typedef struct akmos_cipher_s *akmos_cipher_t; 108 109 typedef struct akmos_cipher_xdesc_s { 110 akmos_algo_id id; 111 char *name; 112 size_t blklen; 113 size_t ivlen; 114 size_t keymin; 115 size_t keymax; 116 size_t keystep; 117 } akmos_cipher_xdesc_t; 118 119 int akmos_cipher_init (akmos_cipher_t *, akmos_algo_id, akmos_mode_id); 120 int akmos_cipher_setkey (akmos_cipher_t, const uint8_t *, size_t); 121 void akmos_cipher_setiv (akmos_cipher_t, const uint8_t *); 122 void akmos_cipher_setcnt (akmos_cipher_t, const uint8_t *); 123 void akmos_cipher_crypt (akmos_cipher_t, const uint8_t *, size_t, uint8_t *); 124 void akmos_cipher_free (akmos_cipher_t); 125 int akmos_cipher (akmos_algo_id, akmos_mode_id, const uint8_t *, size_t, 126 const uint8_t *, const uint8_t *, size_t, uint8_t *); 127 128 const char *akmos_cipher_name(akmos_algo_id); 129 akmos_algo_id akmos_cipher_id(const char *); 130 size_t akmos_cipher_blklen (akmos_algo_id); 131 size_t akmos_cipher_ivlen (akmos_algo_id); 132 133 const akmos_cipher_xdesc_t *akmos_cipher_desc(akmos_algo_id); 134 135 /* Hashing */ 136 typedef struct akmos_digest_s *akmos_digest_t; 137 138 typedef struct akmos_digest_xdesc_s { 139 akmos_algo_id id; 140 char *name; 141 size_t blklen; 142 size_t outlen; 143 } akmos_digest_xdesc_t; 144 145 int akmos_digest_init (akmos_digest_t *, akmos_algo_id); 146 void akmos_digest_update(akmos_digest_t, const uint8_t *, size_t); 147 void akmos_digest_done (akmos_digest_t, uint8_t *); 148 int akmos_digest (akmos_algo_id, const uint8_t *, size_t, uint8_t *); 149 150 const char *akmos_digest_name(akmos_algo_id); 151 akmos_algo_id akmos_digest_id(const char *); 152 size_t akmos_digest_blklen (akmos_algo_id); 153 size_t akmos_digest_outlen (akmos_algo_id); 154 155 const akmos_digest_xdesc_t *akmos_digest_desc(akmos_algo_id); 156 157 /* Message authentication code (MAC) */ 158 typedef struct akmos_mac_s *akmos_mac_t; 159 160 int akmos_mac_init (akmos_mac_t *, akmos_algo_id, akmos_mode_id); 161 int akmos_mac_setkey(akmos_mac_t, const uint8_t *, size_t); 162 void akmos_mac_update(akmos_mac_t, const uint8_t *, size_t); 163 int akmos_mac_done (akmos_mac_t, uint8_t *); 164 int akmos_mac (akmos_algo_id, akmos_mode_id, const uint8_t *, size_t, const uint8_t *, size_t, uint8_t *); 165 166 /* Key derivation function */ 167 int akmos_kdf_pbkdf2(uint8_t *, size_t, const uint8_t *, size_t, const uint8_t *, size_t, akmos_algo_id, uint32_t); 168 int akmos_kdf_scrypt(uint8_t *, size_t, const uint8_t *, size_t, const uint8_t *, size_t, uint32_t, uint32_t); 169 170 /* Base64 binary to text */ 171 typedef struct akmos_base64_s *akmos_base64_t; 172 173 int akmos_base64_init (akmos_base64_t *, akmos_algo_id, akmos_mode_id); 174 int akmos_base64_update(akmos_base64_t, const uint8_t *, size_t, uint8_t *, size_t *); 175 int akmos_base64_done (akmos_base64_t, uint8_t *, size_t *); 176 177 int akmos_base64_encode(akmos_algo_id, const uint8_t *, size_t, uint8_t *, size_t *); 178 int akmos_base64_decode(akmos_algo_id, const uint8_t *, size_t, uint8_t *, size_t *); 179 180 size_t akmos_base64_enclen(size_t); 181 size_t akmos_base64_declen(size_t); 182 183 /* Misc */ 184 akmos_mode_id akmos_str2mode(const char *); 185 const char *akmos_mode2str(akmos_mode_id); 186 187 void akmos_memzero(volatile void *, size_t); 188 189 void akmos_padadd(const uint8_t *, size_t, uint8_t *, size_t); 190 size_t akmos_padrem(uint8_t *, size_t); 191 192 int akmos_perror(int); 193 194 const char *akmos_version(void); 195 196 #endif /* AKMOS_H */ 197