public byte[] GetPacket()
 {
     byte[] header = new byte[1]; header[0] = 1;
     byte[] message_length_encoded = CommonRoutines.EncodeBEWord((UInt32)message.Length);
     byte[] salt_length_encoded    = CommonRoutines.EncodeBEWord((UInt32)salt.Length);
     return(CommonRoutines.CombineByteArrays(header, message_length_encoded, salt_length_encoded, message, salt));
 }
Beispiel #2
0
 public byte[] GetPacket()
 {
     byte[] header                    = new byte[1]; header[0] = 2;
     byte[] encoded_public_key        = CommonRoutines.EncodePublicKey(key_pair);
     byte[] encoded_public_key_length = CommonRoutines.EncodeBEWord((UInt32)encoded_public_key.Length);
     return(CommonRoutines.CombineByteArrays(header, encoded_public_key_length, encoded_public_key));
 }
        public byte[] GetPacket()
        {
            byte[] header = new byte[1]; header[0] = 4;
            byte[] program_size_encoded = CommonRoutines.EncodeBEWord((UInt32)program.Length * 4);
            byte[] row_min_encoded      = CommonRoutines.EncodeBEWord(row_min);
            byte[] row_max_encoded      = CommonRoutines.EncodeBEWord(row_max);
            byte[] answer_units_encoded = CommonRoutines.EncodeBEWord(answer_units);
            byte[] answer_min_encoded   = CommonRoutines.EncodeBEWord(answer_min);
            byte[] answer_max_encoded   = CommonRoutines.EncodeBEWord(answer_max);
            byte[] alpha_num_encoded    = CommonRoutines.EncodeBEWord(alpha.num);
            byte[] alpha_den_encoded    = CommonRoutines.EncodeBEWord(alpha.den);
            byte[] beta_num_encoded     = CommonRoutines.EncodeBEWord(beta.num);
            byte[] beta_den_encoded     = CommonRoutines.EncodeBEWord(beta.den);

            byte[] program_encoded = new byte[program.Length * 4];
            for (int i = 0; i < program.Length; ++i)
            {
                byte[] instruction_encoded = CommonRoutines.EncodeBEWord(program[i]);
                instruction_encoded.CopyTo(program_encoded, i * 4);
            }

            return(CommonRoutines.CombineByteArrays(header, program_size_encoded, row_min_encoded, row_max_encoded,
                                                    answer_units_encoded, answer_min_encoded, answer_max_encoded,
                                                    alpha_num_encoded, alpha_den_encoded,
                                                    beta_num_encoded, beta_den_encoded, program_encoded));
        }
        public byte[] GetPacket(RSACryptoServiceProvider ironclad_public_key)
        {
            byte[] header = new byte[1]; header[0] = 3;
            byte[] row_nonce_size_encoded = CommonRoutines.EncodeBEWord((UInt32)nonce.Length);
            byte[] row_data_size_encoded  = CommonRoutines.EncodeBEWord((UInt32)values.Length * 4);
            byte[] max_budget_num_encoded = CommonRoutines.EncodeBEWord(max_budget.num);
            byte[] max_budget_den_encoded = CommonRoutines.EncodeBEWord(max_budget.den);

            byte[] row_encoded = new byte[values.Length * 4];
            for (int i = 0; i < values.Length; ++i)
            {
                byte[] value_encoded = CommonRoutines.EncodeBEWord(values[i]);
                value_encoded.CopyTo(row_encoded, i * 4);
            }

            byte[] plaintext = CommonRoutines.CombineByteArrays(
                row_nonce_size_encoded,
                row_data_size_encoded,
                max_budget_num_encoded,
                max_budget_den_encoded,
                nonce,
                row_encoded);
            byte[] encrypted = ironclad_public_key.Encrypt(plaintext, false);
            return(CommonRoutines.CombineByteArrays(header, encrypted));
        }
 public byte[] GetPacket()
 {
     byte[] header             = new byte[1]; header[0] = 2;
     byte[] encoded_budget_num = CommonRoutines.EncodeBEWord(budget.num);
     byte[] encoded_budget_den = CommonRoutines.EncodeBEWord(budget.den);
     return(CommonRoutines.CombineByteArrays(header, encoded_budget_num, encoded_budget_den));
 }
        public static byte[] EncodeInitializeDBResponse(UInt32 error_code)
        {
            byte[] header = new byte[1];
            header[0] = 2;

            byte[] error_code_encoded = CommonRoutines.EncodeBEWord(error_code);

            return(CommonRoutines.CombineByteArrays(header, error_code_encoded));
        }
        public static byte[] EncodeQueryResponse(UInt32 error_code, UInt32 response_value)
        {
            byte[] header = new byte[1];
            header[0] = 4;

            byte[] error_code_encoded     = CommonRoutines.EncodeBEWord(error_code);
            byte[] response_value_encoded = CommonRoutines.EncodeBEWord((uint)response_value);

            return(CommonRoutines.CombineByteArrays(header, error_code_encoded, response_value_encoded));
        }
Beispiel #8
0
        public static byte[] EncodeCreateCounterResponse(UInt32 error_code, UInt32 counter_index)
        {
            byte[] header = new byte[1];
            header[0] = 2;

            byte[] error_code_encoded    = CommonRoutines.EncodeBEWord(error_code);
            byte[] counter_index_encoded = CommonRoutines.EncodeBEWord((uint)counter_index);

            return(CommonRoutines.CombineByteArrays(header, error_code_encoded, counter_index_encoded));
        }
Beispiel #9
0
        public static byte[] EncodeAdvanceCounterResponse(UInt32 error_code, byte[] trinc_statement, byte[] trinc_attestation)
        {
            byte[] header = new byte[1];
            header[0] = 3;

            byte[] error_code_encoded       = CommonRoutines.EncodeBEWord(error_code);
            byte[] trinc_statement_length   = CommonRoutines.EncodeBEWord((uint)trinc_statement.Length);
            byte[] trinc_attestation_length = CommonRoutines.EncodeBEWord((uint)trinc_attestation.Length);

            return(CommonRoutines.CombineByteArrays(header, error_code_encoded, trinc_statement_length, trinc_attestation_length, trinc_statement, trinc_attestation));
        }
Beispiel #10
0
        public byte[] GetPacket(SHA256Managed hasher)
        {
            byte[] header = new byte[1]; header[0] = 3;
            byte[] counter_index_encoded = CommonRoutines.EncodeBEWord(counter_index);

            byte[] new_counter_value_encoded        = CommonRoutines.EncodeMPInt(new_counter_value);
            byte[] new_counter_value_length_encoded = CommonRoutines.EncodeBEWord((UInt32)new_counter_value_encoded.Length);

            byte[] message_length_encoded = CommonRoutines.EncodeBEWord((UInt32)message.Length);

            byte[] request             = CommonRoutines.CombineByteArrays(new_counter_value_encoded, message);
            byte[] request_attestation = key_pair.SignData(request, CryptoConfig.MapNameToOID("SHA256"));
            byte[] request_attestation_length_encoded = CommonRoutines.EncodeBEWord((UInt32)request_attestation.Length);

            return(CommonRoutines.CombineByteArrays(header, counter_index_encoded, new_counter_value_length_encoded, message_length_encoded,
                                                    request_attestation_length_encoded, new_counter_value_encoded, message,
                                                    request_attestation));
        }
Beispiel #11
0
        public byte[] HandleRequest(byte[] requestBytes)
        {
            PassHashRequest request = PassHashRequest.ParseRequest(requestBytes);

            if (request is PerformHashRequest)
            {
                PerformHashRequest performHashRequest = (PerformHashRequest)request;
                byte[]             message            = performHashRequest.message;
                byte[]             salt = performHashRequest.salt;

                byte[] mashup = CommonRoutines.CombineByteArrays(secret, salt, message);
                byte[] hash   = hasher.ComputeHash(mashup);
                return(PassHashResponse.EncodePerformHashResponse(0, hash));
            }
            else
            {
                return(Common.InvalidResponse.Encode());
            }
        }
        public byte[] HandleRequest(byte[] requestBytes)
        {
            object request = NotaryRequest.ParseRequest(requestBytes);

            if (request is Common.GetQuoteRequest)
            {
                GetQuoteResponse getQuoteResponse = new GetQuoteResponse(0, key_pair);
                return(getQuoteResponse.Encode());
            }
            if (request is AdvanceCounterRequest)
            {
                AdvanceCounterRequest r = (AdvanceCounterRequest)request;
                counter = counter + 1;
                byte[] header = new byte[1];
                header[0] = 34;
                byte[] new_counter_value_encoding = CommonRoutines.EncodeMPBigInteger(counter);
                byte[] notary_statement           = CommonRoutines.CombineByteArrays(header, new_counter_value_encoding, r.message);
                byte[] notary_attestation         = key_pair.SignData(notary_statement, CryptoConfig.MapNameToOID("SHA256"));
                return(NotarySrvResponse.EncodeAdvanceCounterResponse(0, notary_statement, notary_attestation));
            }
            return(InvalidResponse.Encode());
        }
        public byte[] HandleRequest(byte[] requestBytes)
        {
            object request = TrIncRequest.ParseRequest(requestBytes);

            if (request is Common.GetQuoteRequest)
            {
                GetQuoteResponse getQuoteResponse = new GetQuoteResponse(0, key_pair);
                return(getQuoteResponse.Encode());
            }
            if (request is CreateCounterRequest)
            {
                CreateCounterRequest     r          = (CreateCounterRequest)request;
                RSACryptoServiceProvider public_key = CommonRoutines.DecodePublicKey(r.public_key);
                if (public_key == null)
                {
                    return(TrIncSrvResponse.EncodeCreateCounterResponse(3, 0));
                }
                TrIncCounter counter = new TrIncCounter(public_key);
                counters.Add(counter);
                UInt32 counter_index = (UInt32)(counters.Count - 1);
                return(TrIncSrvResponse.EncodeCreateCounterResponse(0, counter_index));
            }
            if (request is AdvanceCounterRequest)
            {
                AdvanceCounterRequest r = (AdvanceCounterRequest)request;
                if (r.counter_index < 0 || r.counter_index >= counters.Count)
                {
                    Console.Error.WriteLine("Received request for invalid counter index {0}", r.counter_index);
                    return(TrIncSrvResponse.EncodeAdvanceCounterResponse(1, new byte[0], new byte[0]));
                }

                TrIncCounter counter = counters[(int)r.counter_index];
                byte[]       req     = CommonRoutines.CombineByteArrays(r.new_counter_value, r.message);
                if (!counter.PublicKey.VerifyData(req, CryptoConfig.MapNameToOID("SHA256"), r.request_attestation))
                {
                    Console.Error.WriteLine("Received invalid request attestation");
                    return(TrIncSrvResponse.EncodeAdvanceCounterResponse(5, new byte[0], new byte[0]));
                }

                int        offset            = 0;
                BigInteger new_counter_value = CommonRoutines.DecodeMPBigInteger(r.new_counter_value, ref offset);
                if (new_counter_value < 0 || offset != r.new_counter_value.Length)
                {
                    Console.Error.WriteLine("Received invalid new counter value encoding");
                    return(TrIncSrvResponse.EncodeAdvanceCounterResponse(6, new byte[0], new byte[0]));
                }

                if (new_counter_value < counter.Value)
                {
                    Console.Error.WriteLine("New counter value requested {0} is smaller than current counter value {1}", new_counter_value, counter.Value);
                    return(TrIncSrvResponse.EncodeAdvanceCounterResponse(7, new byte[0], new byte[0]));
                }

                BigInteger old_counter_value = counter.Value;
                counter.Value = new_counter_value;

                byte[] header = new byte[1];
                header[0] = 34;
                byte[] counter_index_encoding     = CommonRoutines.EncodeBEWord(r.counter_index);
                byte[] old_counter_value_encoding = CommonRoutines.EncodeMPBigInteger(old_counter_value);
                byte[] new_counter_value_encoding = CommonRoutines.EncodeMPBigInteger(new_counter_value);
                byte[] trinc_statement            = CommonRoutines.CombineByteArrays(header, counter_index_encoding, old_counter_value_encoding, new_counter_value_encoding, r.message);
                byte[] trinc_attestation          = key_pair.SignData(trinc_statement, CryptoConfig.MapNameToOID("SHA256"));
                return(TrIncSrvResponse.EncodeAdvanceCounterResponse(0, trinc_statement, trinc_attestation));
            }
            return(InvalidResponse.Encode());
        }