[AMDGPU] Test codegen'ing True16 additions.
[llvm-project.git] / polly / lib / External / isl / imath / rsamath.c
blob25922cddffcde34d950fd8ac8cfd136b76fda429
1 /*
2 Name: rsamath.c
3 Purpose: Implements part of PKCS#1, v. 2.1, June 14, 2002 (RSA Labs)
4 Author: M. J. Fromberger
6 Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
27 #include "rsamath.h"
29 #include <limits.h>
30 #include <string.h>
32 static mp_result s_rsa_transform(mp_int msg, mp_int exp, mp_int mod,
33 mp_int out);
35 /* Convert integer to octet string, per PKCS#1 v.2.1 */
36 mp_result rsa_i2osp(mp_int z, unsigned char *out, int len) {
37 int excess_len = mp_int_binary_len(z);
39 if (excess_len < len) return MP_RANGE;
41 memset(out, 0, len);
43 excess_len -= len;
44 mp_int_to_binary(z, out + excess_len, len);
46 return MP_OK;
49 /* Convert octet string to integer, per PKCS#1 v.2.1 */
50 mp_result rsa_os2ip(mp_int z, unsigned char *in, int len) {
51 return mp_int_read_binary(z, in, len);
54 /* Primitive RSA encryption operation */
55 mp_result rsa_rsaep(mp_int msg, mp_int exp, mp_int mod, mp_int cipher) {
56 return s_rsa_transform(msg, exp, mod, cipher);
59 /* Primitive RSA decryption operation */
60 mp_result rsa_rsadp(mp_int cipher, mp_int exp, mp_int mod, mp_int msg) {
61 return s_rsa_transform(cipher, exp, mod, msg);
64 /* Primitive RSA signing operation */
65 mp_result rsa_rsasp(mp_int msg, mp_int exp, mp_int mod, mp_int signature) {
66 return s_rsa_transform(msg, exp, mod, signature);
69 /* Primitive RSA verification operation */
70 mp_result rsa_rsavp(mp_int signature, mp_int exp, mp_int mod, mp_int msg) {
71 return s_rsa_transform(signature, exp, mod, msg);
74 /* Compute the maximum length in bytes a message can have using PKCS#1
75 v.1.5 encoding with the given modulus */
76 int rsa_max_message_len(mp_int mod) {
77 int num_bits = mp_int_count_bits(mod);
78 int num_bytes = num_bits / CHAR_BIT;
80 if (num_bytes < 11) {
81 return 0; /* at least eleven bytes are required for padding */
82 } else {
83 return num_bytes - 11;
87 mp_result rsa_pkcs1v15_encode(unsigned char *buf, int msg_len, int buf_len,
88 int tag, random_f filler) {
89 /* Make sure there is enough space for the encoded output */
90 if (msg_len > (buf_len - 11)) return MP_RANGE;
92 int msg_start = buf_len - msg_len;
93 int pad_len = msg_start - 3;
95 /* Move message to top of buffer -- these might overlap, so we rely
96 on the semantics of memmove() here */
97 memmove(buf + msg_start, buf, msg_len);
99 /* Set initial bytes as required by the specification */
100 buf[0] = 0x00;
101 buf[1] = (unsigned char)tag;
103 /* Fill with random padding. We'll just assume the filler function
104 does the right thing and only writes the requested number of
105 nonzero bytes */
106 (filler)(buf + 2, pad_len);
108 /* Write separator between pad and message body */
109 buf[msg_start - 1] = 0x00;
111 return MP_OK;
114 mp_result rsa_pkcs1v15_decode(unsigned char *buf, int buf_len, int tag,
115 int *msg_len) {
116 /* Make sure the buffer is syntactically valid */
117 if (buf_len < 11 || buf[0] != 0x00 || buf[1] != (unsigned char)tag)
118 return MP_UNDEF;
120 /* Figure out how many bytes of random padding there are */
121 int i = 2;
122 int pad_len = 0;
123 while (buf[i++] != '\0') ++pad_len;
125 int data_start = i;
126 int data_len = buf_len - data_start;
128 /* Shift the message to the front of the buffer */
129 memmove(buf, buf + data_start, data_len);
131 /* Zero out the rest of the buffer */
132 memset(buf + data_len, 0, pad_len + 3);
134 *msg_len = data_len;
136 return MP_OK;
139 static mp_result s_rsa_transform(mp_int msg, mp_int exp, mp_int mod,
140 mp_int out) {
141 if (mp_int_compare_zero(msg) < 0 || mp_int_compare(msg, mod) >= 0) {
142 return MP_RANGE;
145 return mp_int_exptmod(msg, exp, mod, out);
148 /* Here there be dragons */