| /***************************************************************************** |
| * Copyright Statement: |
| * -------------------- |
| * This software is protected by Copyright and the information contained |
| * herein is confidential. The software may not be copied and the information |
| * contained herein may not be used or disclosed except with the written |
| * permission of MediaTek Inc. (C) 2016 |
| * |
| * BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES |
| * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS (""MEDIATEK SOFTWARE"") |
| * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON |
| * AN ""AS-IS"" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES, |
| * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF |
| * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT. |
| * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE |
| * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR |
| * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH |
| * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO |
| * NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S |
| * SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM. |
| * |
| * BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE |
| * LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE, |
| * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE, |
| * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO |
| * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. |
| * |
| * THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE |
| * WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF |
| * LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND |
| * RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER |
| * THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC). |
| * |
| *****************************************************************************/ |
| /************************************************************* |
| * |
| * This Software is the property of VIA Telecom, Inc. and may only be used pursuant to a license from VIA Telecom, Inc. |
| * |
| * Any unauthorized use inconsistent with the terms of such license is strictly prohibited. |
| * |
| * Copyright (c) 2010 VIA Telecom, Inc. All rights reserved. |
| * |
| *************************************************************/ |
| /***************************************************************************** |
| * |
| * FILE NAME : pswrsa.h |
| * |
| * DESCRIPTION : |
| * |
| * This file contains the implementation of RSA encryption based on TomsFastMath. |
| * |
| * HISTORY : |
| * See Log at end of file |
| * |
| *****************************************************************************/ |
| /* TomsFastMath, a fast ISO C bignum library. |
| * |
| * This project is meant to fill in where LibTomMath |
| * falls short. That is speed ;-) |
| * |
| * This project is public domain and free for all purposes. |
| * |
| * Tom St Denis, tomstdenis@gmail.com |
| */ |
| #ifndef PSWRSA_H_ |
| #define PSWRSA_H_ |
| |
| #include <stdio.h> |
| #include <string.h> |
| #include <stdlib.h> |
| #include <ctype.h> |
| #include <limits.h> |
| |
| #ifndef MIN |
| #define MIN(x,y) (((x)<(y))?(x):(y)) |
| #endif |
| |
| #ifndef MAX |
| #define MAX(x,y) (((x)>(y))?(x):(y)) |
| #endif |
| |
| /* default configurations for ARM7 besed on test result |
| */ |
| |
| #if (CHAR_BIT & 7) |
| #error CHAR_BIT must be a multiple of eight. |
| #endif |
| |
| typedef unsigned short fp_digit; /* 16 bits */ |
| typedef unsigned int fp_word; /* 32 bits */ |
| |
| /* # of digits this is */ |
| #define DIGIT_BIT ((int)((CHAR_BIT) * sizeof(fp_digit))) |
| |
| /* Maximum Integer Precision in bits |
| * Basically the largest size you will be multiplying should be half [or smaller] |
| * of FP_MAX_SIZE-four_digit |
| * To support max 1024-bits computation need define: |
| */ |
| #define FP_MAX_SIZE (2048+(8*DIGIT_BIT)) |
| |
| #define FP_MASK (fp_digit)(-1) |
| #define FP_SIZE (FP_MAX_SIZE/DIGIT_BIT) |
| |
| /* signs */ |
| #define FP_ZPOS 0 |
| #define FP_NEG 1 |
| |
| /* return codes */ |
| #define FP_OKAY 0 |
| #define FP_VAL 1 |
| #define FP_MEM 2 |
| |
| /* equalities */ |
| #define FP_LT -1 /* less than */ |
| #define FP_EQ 0 /* equal to */ |
| #define FP_GT 1 /* greater than */ |
| |
| /* replies */ |
| #define FP_YES 1 /* yes response */ |
| #define FP_NO 0 /* no response */ |
| |
| /* Enable Optimizing computations based on length of the 'big int'(->used): |
| Right now, not need them ! |
| */ |
| /*#define TFM_MUL32 |
| #define TFM_SQR32 */ |
| |
| /* do we want some overflow checks |
| Not required if you make sure your numbers are within range (e.g. by default |
| a modulus for fp_exptmod() can only be upto 2048 bits long) |
| */ |
| /* #define TFM_CHECK */ |
| |
| /* this parameter decides the algorithm speed and the peak memory usage |
| because of the stack size limitation, this value can not be too large */ |
| #define MAXWINSIZE 1 |
| /*------------------------ data structure ------------------------- */ |
| |
| /* a FP type */ |
| typedef struct { |
| fp_digit dp[FP_SIZE]; |
| int used, sign; |
| } fp_int; |
| |
| /*--------------------- functions and macros -----------------------*/ |
| |
| /* initialize [or zero] an fp int */ |
| #define fp_init(a) (void)memset((a), 0, sizeof(fp_int)) |
| #define fp_zero(a) fp_init(a) |
| |
| /* zero/even/odd ? */ |
| #define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO) |
| #define fp_iseven(a) (((a)->used >= 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO) |
| #define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO) |
| |
| /* copy from a to b */ |
| #define fp_copy(a, b) (void)(((a) != (b)) && memcpy((b), (a), sizeof(fp_int))) |
| #define fp_init_copy(a, b) fp_copy(b, a) |
| |
| /* negate and absolute */ |
| #define fp_neg(a, b) { fp_copy(a, b); (b)->sign ^= 1; } |
| #define fp_abs(a, b) { fp_copy(a, b); (b)->sign = 0; } |
| |
| /* clamp digits */ |
| #define fp_clamp(a) { while ((a)->used && (a)->dp[(a)->used-1] == 0) --((a)->used); (a)->sign = (a)->used ? (a)->sign : FP_ZPOS; } |
| |
| |
| /*-------------------------------public interface-----------------------------*/ |
| /* error codes [will be expanded in future releases] */ |
| enum { |
| CRYPT_OK=0, /* Result OK */ |
| CRYPT_INVALID_ARG, /* Generic invalid argument */ |
| CRYPT_MEM, /* Out of memory */ |
| |
| CRYPT_ERROR, /* Generic Error */ |
| CRYPT_NOP, /* Not a failure but no operation was performed */ |
| CRYPT_INVALID_KEYSIZE, /* Invalid key size given */ |
| CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */ |
| CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */ |
| CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */ |
| CRYPT_INVALID_PACKET, /* Invalid input packet given */ |
| CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */ |
| CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */ |
| CRYPT_INVALID_CIPHER, /* Invalid cipher specified */ |
| CRYPT_INVALID_HASH, /* Invalid hash specified */ |
| CRYPT_INVALID_PRNG, /* Invalid PRNG specified */ |
| CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */ |
| CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */ |
| CRYPT_FILE_NOTFOUND, /* File Not Found */ |
| CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */ |
| CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */ |
| CRYPT_PK_DUP, /* Duplicate key already in key ring */ |
| CRYPT_PK_NOT_FOUND, /* Key not found in keyring */ |
| CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */ |
| CRYPT_INVALID_PRIME_SIZE/* Invalid size of prime requested */ |
| }; |
| |
| int rsa_exptmod(unsigned char *in, int inlen, |
| unsigned char *e, int elen, |
| unsigned char *n, int nlen, |
| unsigned char *out, int *outlen); |
| int fp_unsigned_bin_size(fp_int *a); |
| void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c); |
| void fp_to_unsigned_bin(fp_int *a, unsigned char *b); |
| |
| #endif /*PSWRSA_H_*/ |
| |
| |
| |