1 /* 2 * Copyright (C) 2015-2016 Patrick Monnerat, D+H <patrick.monnerat@dh.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, 6 * with or without modification, are permitted provided 7 * that the following conditions are met: 8 * 9 * Redistributions of source code must retain the above 10 * copyright notice, this list of conditions and the 11 * following disclaimer. 12 * 13 * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials 16 * provided with the distribution. 17 * 18 * Neither the name of the copyright holder nor the names 19 * of any other contributors may be used to endorse or 20 * promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 35 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 36 * OF SUCH DAMAGE. 37 */ 38 39 #ifndef LIBSSH2_OS400QC3_H 40 #define LIBSSH2_OS400QC3_H 41 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include <qc3cci.h> 46 47 48 /* Redefine character/string literals as always EBCDIC. */ 49 #undef Qc3_Alg_Token 50 #define Qc3_Alg_Token "\xC1\xD3\xC7\xC4\xF0\xF1\xF0\xF0" /* ALGD0100 */ 51 #undef Qc3_Alg_Block_Cipher 52 #define Qc3_Alg_Block_Cipher "\xC1\xD3\xC7\xC4\xF0\xF2\xF0\xF0" /* ALGD0200 */ 53 #undef Qc3_Alg_Block_CipherAuth 54 #define Qc3_Alg_Block_CipherAuth \ 55 "\xC1\xD3\xC7\xC4\xF0\xF2\xF1\xF0" /* ALGD0210 */ 56 #undef Qc3_Alg_Stream_Cipher 57 #define Qc3_Alg_Stream_Cipher \ 58 "\xC1\xD3\xC7\xC4\xF0\xF3\xF0\xF0" /* ALGD0300 */ 59 #undef Qc3_Alg_Public_Key 60 #define Qc3_Alg_Public_Key "\xC1\xD3\xC7\xC4\xF0\xF4\xF0\xF0" /* ALGD0400 */ 61 #undef Qc3_Alg_Hash 62 #define Qc3_Alg_Hash "\xC1\xD3\xC7\xC4\xF0\xF5\xF0\xF0" /* ALGD0500 */ 63 #undef Qc3_Data 64 #define Qc3_Data "\xC4\xC1\xE3\xC1\xF0\xF1\xF0\xF0" /* DATA0100 */ 65 #undef Qc3_Array 66 #define Qc3_Array "\xC4\xC1\xE3\xC1\xF0\xF2\xF0\xF0" /* DATA0200 */ 67 #undef Qc3_Key_Token 68 #define Qc3_Key_Token "\xD2\xC5\xE8\xC4\xF0\xF1\xF0\xF0" /* KEYD0100 */ 69 #undef Qc3_Key_Parms 70 #define Qc3_Key_Parms "\xD2\xC5\xE8\xC4\xF0\xF2\xF0\xF0" /* KEYD0200 */ 71 #undef Qc3_Key_KSLabel 72 #define Qc3_Key_KSLabel "\xD2\xC5\xE8\xC4\xF0\xF4\xF0\xF0" /* KEYD0400 */ 73 #undef Qc3_Key_PKCS5 74 #define Qc3_Key_PKCS5 "\xD2\xC5\xE8\xC4\xF0\xF5\xF0\xF0" /* KEYD0500 */ 75 #undef Qc3_Key_PEMCert 76 #define Qc3_Key_PEMCert "\xD2\xC5\xE8\xC4\xF0\xF6\xF0\xF0" /* KEYD0600 */ 77 #undef Qc3_Key_CSLabel 78 #define Qc3_Key_CSLabel "\xD2\xC5\xE8\xC4\xF0\xF7\xF0\xF0" /* KEYD0700 */ 79 #undef Qc3_Key_CSDN 80 #define Qc3_Key_CSDN "\xD2\xC5\xE8\xC4\xF0\xF8\xF0\xF0" /* KEYD0800 */ 81 #undef Qc3_Key_AppID 82 #define Qc3_Key_AppID "\xD2\xC5\xE8\xC4\xF0\xF9\xF0\xF0" /* KEYD0900 */ 83 84 #undef Qc3_ECB 85 #define Qc3_ECB '\xF0' /* '0' */ 86 #undef Qc3_CBC 87 #define Qc3_CBC '\xF1' /* '1' */ 88 #undef Qc3_OFB 89 #define Qc3_OFB '\xF2' /* '2' */ 90 #undef Qc3_CFB1Bit 91 #define Qc3_CFB1Bit '\xF3' /* '3' */ 92 #undef Qc3_CFB8Bit 93 #define Qc3_CFB8Bit '\xF4' /* '4' */ 94 #undef Qc3_CFB64Bit 95 #define Qc3_CFB64Bit '\xF5' /* '5' */ 96 #undef Qc3_CUSP 97 #define Qc3_CUSP '\xF6' /* '6' */ 98 #undef Qc3_CTR 99 #define Qc3_CTR '\xF7' /* '7' */ 100 #undef Qc3_CCM 101 #define Qc3_CCM '\xF8' /* '8' */ 102 #undef Qc3_No_Pad 103 #define Qc3_No_Pad '\xF0' /* '0' */ 104 #undef Qc3_Pad_Char 105 #define Qc3_Pad_Char '\xF1' /* '1' */ 106 #undef Qc3_Pad_Counter 107 #define Qc3_Pad_Counter '\xF2' /* '2' */ 108 #undef Qc3_PKCS1_00 109 #define Qc3_PKCS1_00 '\xF0' /* '0' */ 110 #undef Qc3_PKCS1_01 111 #define Qc3_PKCS1_01 '\xF1' /* '1' */ 112 #undef Qc3_PKCS1_02 113 #define Qc3_PKCS1_02 '\xF2' /* '2' */ 114 #undef Qc3_ISO9796 115 #define Qc3_ISO9796 '\xF3' /* '3' */ 116 #undef Qc3_Zero_Pad 117 #define Qc3_Zero_Pad '\xF4' /* '4' */ 118 #undef Qc3_ANSI_X931 119 #define Qc3_ANSI_X931 '\xF5' /* '5' */ 120 #undef Qc3_OAEP 121 #define Qc3_OAEP '\xF6' /* '6' */ 122 #undef Qc3_Bin_String 123 #define Qc3_Bin_String '\xF0' /* '0' */ 124 #undef Qc3_BER_String 125 #define Qc3_BER_String '\xF1' /* '1' */ 126 #undef Qc3_MK_Struct 127 #define Qc3_MK_Struct '\xF3' /* '3' */ 128 #undef Qc3_KSLabel_Struct 129 #define Qc3_KSLabel_Struct '\xF4' /* '4' */ 130 #undef Qc3_PKCS5_Struct 131 #define Qc3_PKCS5_Struct '\xF5' /* '5' */ 132 #undef Qc3_PEMCert_String 133 #define Qc3_PEMCert_String '\xF6' /* '6' */ 134 #undef Qc3_CSLabel_String 135 #define Qc3_CSLabel_String '\xF7' /* '7' */ 136 #undef Qc3_CSDN_String 137 #define Qc3_CSDN_String '\xF8' /* '8' */ 138 #undef Qc3_Clear 139 #define Qc3_Clear '\xF0' /* '0' */ 140 #undef Qc3_Encrypted 141 #define Qc3_Encrypted '\xF1' /* '1' */ 142 #undef Qc3_MK_Encrypted 143 #define Qc3_MK_Encrypted '\xF2' /* '2' */ 144 #undef Qc3_Any_CSP 145 #define Qc3_Any_CSP '\xF0' /* '0' */ 146 #undef Qc3_Sfw_CSP 147 #define Qc3_Sfw_CSP '\xF1' /* '1' */ 148 #undef Qc3_Hdw_CSP 149 #define Qc3_Hdw_CSP '\xF2' /* '2' */ 150 #undef Qc3_Continue 151 #define Qc3_Continue '\xF0' /* '0' */ 152 #undef Qc3_Final 153 #define Qc3_Final '\xF1' /* '1' */ 154 #undef Qc3_MK_New 155 #define Qc3_MK_New '\xF0' /* '0' */ 156 #undef Qc3_MK_Current 157 #define Qc3_MK_Current '\xF1' /* '1' */ 158 #undef Qc3_MK_Old 159 #define Qc3_MK_Old '\xF2' /* '2' */ 160 #undef Qc3_MK_Pending 161 #define Qc3_MK_Pending '\xF3' /* '3' */ 162 163 164 /* Define which features are supported. */ 165 #define LIBSSH2_MD5 1 166 #define LIBSSH2_HMAC_RIPEMD 0 167 #define LIBSSH2_HMAC_SHA256 1 168 #define LIBSSH2_HMAC_SHA512 1 169 170 #define LIBSSH2_AES 1 171 #define LIBSSH2_AES_CTR 1 172 #define LIBSSH2_BLOWFISH 0 173 #define LIBSSH2_RC4 1 174 #define LIBSSH2_CAST 0 175 #define LIBSSH2_3DES 1 176 177 #define LIBSSH2_RSA 1 178 #define LIBSSH2_DSA 0 179 180 #define MD5_DIGEST_LENGTH 16 181 #define SHA_DIGEST_LENGTH 20 182 #define SHA256_DIGEST_LENGTH 32 183 #define SHA512_DIGEST_LENGTH 64 184 185 186 /******************************************************************* 187 * 188 * OS/400 QC3 crypto-library backend: global handles structures. 189 * 190 *******************************************************************/ 191 192 /* HMAC & private key algorithms support structure. */ 193 typedef struct _libssh2_os400qc3_crypto_ctx _libssh2_os400qc3_crypto_ctx; 194 struct _libssh2_os400qc3_crypto_ctx { 195 Qc3_Format_ALGD0100_T hash; /* Hash algorithm. */ 196 Qc3_Format_KEYD0100_T key; /* Key. */ 197 _libssh2_os400qc3_crypto_ctx * kek; /* Key encryption. */ 198 }; 199 200 typedef struct { /* Big number. */ 201 unsigned char * bignum; /* Number bits, little-endian. */ 202 unsigned int length; /* Length of bignum (# bytes). */ 203 } _libssh2_bn; 204 205 typedef struct { /* Algorithm description. */ 206 char * fmt; /* Format of Qc3 structure. */ 207 int algo; /* Algorithm identifier. */ 208 unsigned char size; /* Block length. */ 209 unsigned char mode; /* Block mode. */ 210 int keylen; /* Key length. */ 211 } _libssh2_os400qc3_cipher_t; 212 213 typedef struct { /* Diffie-Hellman context. */ 214 char token[8]; /* Context token. */ 215 } _libssh2_os400qc3_dh_ctx; 216 217 /******************************************************************* 218 * 219 * OS/400 QC3 crypto-library backend: Define global types/codes. 220 * 221 *******************************************************************/ 222 223 #define libssh2_crypto_init() 224 #define libssh2_crypto_exit() 225 226 #define libssh2_sha1_ctx Qc3_Format_ALGD0100_T 227 #define libssh2_sha256_ctx Qc3_Format_ALGD0100_T 228 #define libssh2_md5_ctx Qc3_Format_ALGD0100_T 229 #define libssh2_hmac_ctx _libssh2_os400qc3_crypto_ctx 230 #define _libssh2_cipher_ctx _libssh2_os400qc3_crypto_ctx 231 232 #define libssh2_sha1_init(x) libssh2_os400qc3_hash_init(x, Qc3_SHA1) 233 #define libssh2_sha1_update(ctx, data, len) \ 234 libssh2_os400qc3_hash_update(&(ctx), data, len) 235 #define libssh2_sha1_final(ctx, out) \ 236 libssh2_os400qc3_hash_final(&(ctx), out) 237 #define libssh2_sha256_init(x) libssh2_os400qc3_hash_init(x, Qc3_SHA256) 238 #define libssh2_sha256_update(ctx, data, len) \ 239 libssh2_os400qc3_hash_update(&(ctx), data, len) 240 #define libssh2_sha256_final(ctx, out) \ 241 libssh2_os400qc3_hash_final(&(ctx), out) 242 #define libssh2_sha256(message, len, out) \ 243 libssh2_os400qc3_hash(message, len, out, \ 244 Qc3_SHA256) 245 #define libssh2_md5_init(x) libssh2_os400qc3_hash_init(x, Qc3_MD5) 246 #define libssh2_md5_update(ctx, data, len) \ 247 libssh2_os400qc3_hash_update(&(ctx), data, len) 248 #define libssh2_md5_final(ctx, out) \ 249 libssh2_os400qc3_hash_final(&(ctx), out) 250 #define libssh2_hmac_ctx_init(ctx) \ 251 memset((char *) &(ctx), 0, \ 252 sizeof(libssh2_hmac_ctx)) 253 #define libssh2_hmac_md5_init(ctx, key, keylen) \ 254 libssh2_os400qc3_hmac_init(ctx, Qc3_MD5, \ 255 MD5_DIGEST_LENGTH, \ 256 key, keylen) 257 #define libssh2_hmac_sha1_init(ctx, key, keylen) \ 258 libssh2_os400qc3_hmac_init(ctx, Qc3_SHA1, \ 259 SHA_DIGEST_LENGTH, \ 260 key, keylen) 261 #define libssh2_hmac_sha256_init(ctx, key, keylen) \ 262 libssh2_os400qc3_hmac_init(ctx, Qc3_SHA256, \ 263 SHA256_DIGEST_LENGTH, \ 264 key, keylen) 265 #define libssh2_hmac_sha512_init(ctx, key, keylen) \ 266 libssh2_os400qc3_hmac_init(ctx, Qc3_SHA512, \ 267 SHA512_DIGEST_LENGTH, \ 268 key, keylen) 269 #define libssh2_hmac_update(ctx, data, datalen) \ 270 libssh2_os400qc3_hmac_update(&(ctx), \ 271 data, datalen) 272 #define libssh2_hmac_final(ctx, data) \ 273 libssh2_os400qc3_hmac_final(&(ctx), data) 274 #define libssh2_hmac_cleanup(ctx) \ 275 _libssh2_os400qc3_crypto_dtor(ctx) 276 277 278 #define _libssh2_bn_ctx int /* Not used. */ 279 280 #define _libssh2_bn_ctx_new() 0 281 #define _libssh2_bn_ctx_free(bnctx) ((void) 0) 282 283 #define _libssh2_bn_init_from_bin() _libssh2_bn_init() 284 #define _libssh2_bn_bytes(bn) ((bn)->length) 285 286 #define _libssh2_cipher_type(name) _libssh2_os400qc3_cipher_t name 287 #define _libssh2_cipher_aes128 {Qc3_Alg_Block_Cipher, Qc3_AES, 16, \ 288 Qc3_CBC, 16} 289 #define _libssh2_cipher_aes192 {Qc3_Alg_Block_Cipher, Qc3_AES, 24, \ 290 Qc3_CBC, 24} 291 #define _libssh2_cipher_aes256 {Qc3_Alg_Block_Cipher, Qc3_AES, 32, \ 292 Qc3_CBC, 32} 293 #define _libssh2_cipher_aes128ctr {Qc3_Alg_Block_Cipher, Qc3_AES, 16, \ 294 Qc3_CTR, 16} 295 #define _libssh2_cipher_aes192ctr {Qc3_Alg_Block_Cipher, Qc3_AES, 24, \ 296 Qc3_CTR, 24} 297 #define _libssh2_cipher_aes256ctr {Qc3_Alg_Block_Cipher, Qc3_AES, 32, \ 298 Qc3_CTR, 32} 299 #define _libssh2_cipher_3des {Qc3_Alg_Block_Cipher, Qc3_TDES, 0, \ 300 Qc3_CBC, 24} 301 #define _libssh2_cipher_arcfour {Qc3_Alg_Stream_Cipher, Qc3_RC4, 0, 0, 16} 302 303 #define _libssh2_cipher_dtor(ctx) _libssh2_os400qc3_crypto_dtor(ctx) 304 305 #define libssh2_rsa_ctx _libssh2_os400qc3_crypto_ctx 306 #define _libssh2_rsa_free(ctx) (_libssh2_os400qc3_crypto_dtor(ctx), \ 307 free((char *) ctx)) 308 #define libssh2_prepare_iovec(vec, len) memset((char *) (vec), 0, \ 309 (len) * sizeof(struct iovec)) 310 #define _libssh2_rsa_sha1_signv(session, sig, siglen, count, vector, ctx) \ 311 _libssh2_os400qc3_rsa_sha1_signv(session, sig, siglen, \ 312 count, vector, ctx) 313 314 #define _libssh2_dh_ctx _libssh2_os400qc3_dh_ctx 315 #define libssh2_dh_init(dhctx) _libssh2_os400qc3_dh_init(dhctx) 316 #define libssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ 317 _libssh2_os400qc3_dh_key_pair(dhctx, public, g, p, group_order) 318 #define libssh2_dh_secret(dhctx, secret, f, p, bnctx) \ 319 _libssh2_os400qc3_dh_secret(dhctx, secret, f, p) 320 #define libssh2_dh_dtor(dhctx) _libssh2_os400qc3_dh_dtor(dhctx) 321 322 323 /******************************************************************* 324 * 325 * OS/400 QC3 crypto-library backend: Support procedure prototypes. 326 * 327 *******************************************************************/ 328 329 extern _libssh2_bn * _libssh2_bn_init(void); 330 extern void _libssh2_bn_free(_libssh2_bn *bn); 331 extern unsigned long _libssh2_bn_bits(_libssh2_bn *bn); 332 extern int _libssh2_bn_from_bin(_libssh2_bn *bn, int len, 333 const unsigned char *v); 334 extern int _libssh2_bn_set_word(_libssh2_bn *bn, unsigned long val); 335 extern int _libssh2_bn_to_bin(_libssh2_bn *bn, unsigned char *val); 336 extern void _libssh2_random(unsigned char *buf, int len); 337 extern void _libssh2_os400qc3_crypto_dtor(_libssh2_os400qc3_crypto_ctx *x); 338 extern int libssh2_os400qc3_hash_init(Qc3_Format_ALGD0100_T *x, 339 unsigned int algo); 340 extern void libssh2_os400qc3_hash_update(Qc3_Format_ALGD0100_T *ctx, 341 unsigned char *data, int len); 342 extern void libssh2_os400qc3_hash_final(Qc3_Format_ALGD0100_T *ctx, 343 unsigned char *out); 344 extern int libssh2_os400qc3_hash(const unsigned char *message, 345 unsigned long len, unsigned char *out, 346 unsigned int algo); 347 extern void libssh2_os400qc3_hmac_init(_libssh2_os400qc3_crypto_ctx *x, 348 int algo, size_t minkeylen, 349 void *key, int keylen); 350 extern void libssh2_os400qc3_hmac_update(_libssh2_os400qc3_crypto_ctx *ctx, 351 const unsigned char *data, 352 int len); 353 extern void libssh2_os400qc3_hmac_final(_libssh2_os400qc3_crypto_ctx *ctx, 354 unsigned char *out); 355 extern int _libssh2_os400qc3_rsa_sha1_signv(LIBSSH2_SESSION *session, 356 unsigned char **signature, 357 size_t *signature_len, 358 int veccount, 359 const struct iovec vector[], 360 libssh2_rsa_ctx *ctx); 361 extern void _libssh2_os400qc3_dh_init(_libssh2_dh_ctx *dhctx); 362 extern int _libssh2_os400qc3_dh_key_pair(_libssh2_dh_ctx *dhctx, 363 _libssh2_bn *public, 364 _libssh2_bn *g, 365 _libssh2_bn *p, int group_order); 366 extern int _libssh2_os400qc3_dh_secret(_libssh2_dh_ctx *dhctx, 367 _libssh2_bn *secret, 368 _libssh2_bn *f, _libssh2_bn *p); 369 extern void _libssh2_os400qc3_dh_dtor(_libssh2_dh_ctx *dhctx); 370 371 #endif 372 373 /* vim: set expandtab ts=4 sw=4: */ 374