SlideShare a Scribd company logo
1 of 40
Download to read offline
How to Design APIs for Cryptographic Protocols
Lior Malka
Crypto Group, University of Maryland, USA
All Rights Reserved, Lior Malka, 2010 1
What is a Cryptographic Protocol?
Usually two parties.
Terminology: sender - receiver / server – client.
Parties has input and output (possibly null).
Threat model.Threat model.
Security parameters.
Bob Alice
All Rights Reserved, Lior Malka, 2010 2
API – Application Programming Interface
A software library is a collection of modules that provides services to
independent programs via an API (Abstract Programming Interface)
Example: Class Encryption uses Class Vector.Example: Class Encryption uses Class Vector.
Class Vector provides services to Class Encryption.
Class Encryption provides services to other classes.
The methods and variables through which a class provides
services are the APIAPI of this class.
All Rights Reserved, Lior Malka, 2010 3
Software Design Without API
Pros
Fast to implement in small projects.
Agile – can serve as a starting point for API design.
No need to consider how code interfaces with other software.
Can be appropriate for small “dead end” projects.
Cons
Inappropriate for large projects.
Code has a limited (as opposed to general) functionality.
Code is not reusable.
Code is hard to maintain/modify.
Prone to errors and bugs.
All Rights Reserved, Lior Malka, 2010 4
Why a Good API is hard to Design
Forces designer to anticipate future usage of code.
Requirements are incomplete (may never be complete).
Requires abstraction.
Requires modularization.Requires modularization.
Requires skills in programming languages.
Requires code rewrites – time consuming and labor intensive.
All Rights Reserved, Lior Malka, 2010 5
The Benefits of API Driven Design
When an API is used in a project, it
Allows to focus on the project.
Saves development time.
Reduces errors and debugging.Reduces errors and debugging.
Facilitates modular design.
Provides a consistent development platform.
All Rights Reserved, Lior Malka, 2010 6
Case Study
Oblivious Transfer (OT)
All Rights Reserved, Lior Malka, 2010 7
Bob
Oblivious Transfer (OT) - Visually
Alice
X0 , X1b
nullXb
All Rights Reserved, Lior Malka, 2010 8
Oblivious Transfer (OT) - Formally
In an oblivious transfer protocol, Alice allows Bob to learn only one of
her inputs, but Bob does not tell Alice which one it is. Formally:
- The input of Alice is two strings: X0 and X1.
- The input of Bob is a bit b (0 or 1).- The input of Bob is a bit b (0 or 1).
At the end of the protocol:
- The output of Bob is Xb.
- The output of Alice is null.
Security properties:
Alice does not learn b, and Bob does not learn X1-b.
All Rights Reserved, Lior Malka, 2010 9
Implementing OT : 1st Attempt
class Alice {
String X0,X1;
encryption_key_length;
main(){
// open a socket and wait to hear from Bob..
:
send_first_message();
}
// methods for sending and receiving messages// methods for sending and receiving messages
void send(byte[] m) { … }
byte[] receive() { … }
/* methods for constructing the messages of Alice
and processing the replies of Bob */
void send_first_message(){
m1 = new byte[encryption_key_length];
:
send(m1);
receive_second_message();
}
void receive_second_message(){
byte[] m2 = receive();
:
}
}
All Rights Reserved, Lior Malka, 2010 10
What is Wrong With This Code?
Mixes several unrelated functions.
Has no API.
Exercise:
• List all the flaws in the OT protocol.
How would you modify the code so that it has an API?• How would you modify the code so that it has an API?
Next slides:
Improve design and provide an API.
We will only consider class Alice ; the same improvements apply to
class Bob .
All Rights Reserved, Lior Malka, 2010 11
Design Flaw: mixed functionality
class Alice {
String X0,X1;
encryption_key_length;
main(){
// open a socket and wait to hear from Bob..
:
send_first_message();
}
// methods for sending and receiving messages
Networking functions have
nothing to do with OT. They// methods for sending and receiving messages
void send(byte[] m) { … }
byte[] receive() { … }
/* methods for constructing the messages of Alice
and processing the replies of Bob */
void send_first_message(){
m1 = new byte[encryption_key_length];
:
send(m1);
receive_second_message();
}
void receive_second_message(){
byte[] m2 = receive();
:
}
}
nothing to do with OT. They
should be in a separate class
called “Server”
All Rights Reserved, Lior Malka, 2010 12
Redesign - Phase 1
First Objective
Separate networking functionality from the protocol.
All Rights Reserved, Lior Malka, 2010 13
Writing The Client and The Server
class Client {
Socket socket;
// methods for sending and receiving messages
void send(byte[] m) { … }
byte[] receive() { … }
}
class Server{
ServerSocket socket;
// methods for sending and receiving messages
void send(byte[] m) { … }
byte[] receive() { … }
}
All Rights Reserved, Lior Malka, 2010 14
Removing send & receive from class Alice
class Alice {
String X0,X1;
Server server;
encryption_key_length;
main(){
Alice alice = new Alice();
alice.server = new Server(..);
send_first_message();
}
// methods for sending and receiving messages
void send(byte[] m) { … }
byte[] receive() { … }
/* methods for constructing the messages of Alice
and processing the replies of Bob */
void send_first_message(){
m1 = new byte[encryption_key_length];
:
server.send(m1);
receive_second_message();
}
void receive_second_message(){
byte[] m2 = server.receive();
:
}
}
All Rights Reserved, Lior Malka, 2010 15
Implementing OT : 2nd Attempt
class Alice {
String X0,X1;
Server server;
encryption_key_length;
main(){
:
}
/* methods for constructing the messages of Alice
and processing the replies of Bob */
The problem with Class
Alice is that it can only be
used with class Server. It
cannot be used with any other
class, even if that class has
send and receive methods.
and processing the replies of Bob */
void send_first_message(){
m1 = new byte[encryption_key_length];
:
server.send(m1);
receive_second_message();
}
void receive_second_message(){
byte[] m2 = server.receive();
:
}
}
All Rights Reserved, Lior Malka, 2010 16
Fixing the Flaws
class Alice only needs the network support: send and
receive methods.
We do not want to tie users of class Alice to class
Server.
Solution: use an interface to define what we expect from a
network module. Then, replace class Server with the
interface.
All Rights Reserved, Lior Malka, 2010 17
Rewriting The Client and The Server
class Client implements Party {
Interface Party {
// Interface methods (notice: methods are declared, but not defined)
void send(byte[] m)
byte[] receive()
}
Interfaces and abstract classes (also
known as virtual classes) are a sign of
a healthy API driven design.
class Client implements Party {
Socket socket;
// Implementation of send and receive from class Party
void send(byte[] m) { … }
byte[] receive() { … }
}
class Server implements Party {
ServerSocket socket;
// Implementation of send and receive from class Party
void send(byte[] m) { … }
byte[] receive() { … }
}
All Rights Reserved, Lior Malka, 2010 18
Removing “Boilerplate Code”
class Client implements Party {
Socket socket;
Interface Party {
void send(byte[] m)
byte[] receive()
}
Socket socket;
// Implementation of send and receive from class Party
void send(byte[] m) { … }
byte[] receive() { … }
}
class Server implements Party {
ServerSocket socket;
// Implementation of send and receive from class Party
void send(byte[] m) { … }
byte[] receive() { … }
}
Redundancy: send and
receive methods of class
Client do the same as those
in class Server. We will fix
this using inheritance (next slide)
All Rights Reserved, Lior Malka, 2010 19
Removing Redundancy Using Inheritance
interface Party
send
receive
class MyParty implments Party
send and receive are
declared in the interface,
but not defined.
class MyParty provides a class MyParty implments Party
send {…}
receive {…}
class Client extends MyParty class Server extends MyParty
class Client and class
Server inherit send and
receive from class MyParty
class MyParty provides a
specific implementation of
send and receive
All Rights Reserved, Lior Malka, 2010 20
Finishing the Client and The Server
class MyParty implements Party {
// Implementation of send and receive
void send(byte[] m) { … }
Interface Party {
// Declaration of send and receive
void send(byte[] m);
byte[] receive();
}
void send(byte[] m) { … }
byte[] receive() { … }
}
class Client extends MyParty {
Socket socket;
:
}
class Server extends MyParty {
ServerSocket socket;
:
}
All Rights Reserved, Lior Malka, 2010 21
Summary of Improvements So Far
class MyParty implements Party {
// Implementation of send and receive
void send(byte[] m) { … }
Interface Party {
// Declaration of send and receive
void send(byte[] m);
byte[] receive();
}
class Alice can be used
from any client/server
implementing interface
Party.
Send and receive are easy
to maintain/modify because
they only appear in one place.void send(byte[] m) { … }
byte[] receive() { … }
}
class Client extends MyParty {
Socket socket;
:
}
class Server extends MyParty {
ServerSocket socket;
:
}
class Client and class
Server can be used in any
client/server application
they only appear in one place.
Network I/O exceptions can be
handled here, instead of in
class Alice.
All Rights Reserved, Lior Malka, 2010 22
Implementing OT : 3rd Attempt
class Alice {
String X0,X1;
Party party;
encryption_key_length;
main(){
Alice alice = new Alice();
alice.party = new Server(..);
send_first_message();
}
/* methods for constructing the messages of Alice
and processing the replies of Bob */
void send_first_message(){
m1 = new byte[encryption_key_length];
:
server.send(m1);
receive_second_message();
}
void receive_second_message(){
byte[] m2 = server.receive();
:
}
}
All Rights Reserved, Lior Malka, 2010 23
Redesign – Phase 2
Remove main(). We are building a library; not an application.
Decouple message construction from protocol flow.
Encapsulate the class; provide methods that set the input and
get the output.get the output.
All Rights Reserved, Lior Malka, 2010 24
Removing main()
class Alice {
String X0,X1;
Party party;
encryption_key_length;
main(){
Alice alice = new Alice();
alice.party = new Server(..);
send_first_message();
The server will be passed to the
constructor of class Alice,
and send_first_message
will be handled in a “run” method.send_first_message();
}
/* methods for constructing the messages of Alice
and processing the replies of Bob */
void send_first_message(){
m1 = new byte[encryption_key_length];
:
party.send(m1);
receive_second_message();
}
void receive_second_message(){
byte[] m2 = party.receive();
:
}
}
will be handled in a “run” method.
All Rights Reserved, Lior Malka, 2010 25
class Alice {
String X0,X1;
Party party;
encryption_key_length;
public Alice(Party party) {
this.party = party;
}
/* methods for constructing the messages of Alice
This is better. Now Alice can be
used with any class implementing
interface Party
Removing main()
/* methods for constructing the messages of Alice
and processing the replies of Bob */
void send_first_message(){
m1 = new byte[encryption_key_length];
:
party.send(m1);
receive_second_message();
}
void receive_second_message(){
byte[] m2 = party.receive();
:
}
}
This is not good. Messages are
constructed and sent / received in
various locations in the code.
All Rights Reserved, Lior Malka, 2010 26
class Alice {
String X0,X1;
Party party;
encryption_key_length;
public Alice(Party party) {
this.party = party;
}
Decoupling Message Construction
From Protocol Flow
void run() {
party.send(first_message());
process_second_message(party.receive());
:
}
void byte[] first_message(){
m1 = new byte[encryption_key_length];
:
return m1;
}
void process_second_message(byte[] m2){
:
}
}
Method run centrally defines message
flow in the protocol. This makes the code
easier to maintain and read.
Methods first_message and
process_second_message are
now defined purely in terms of input
and output. This enables us to test
their correctness independently
(without having to run the protocol).
All Rights Reserved, Lior Malka, 2010 27
class Alice {
private String X0,X1;
Party party;
encryption_key_length;
public Alice(Party party) {
this.party = party;
}
Encapsulating Data Members
void run() {
party.send(first_message());
process_second_message(party.receive());
:
}
void setInput(String X0, String X1) {
this.X0 = X0;
this.X1 = X1;
}
String getOutput() {
return null;
}
}
Method setInput sets Alice’s input.
It should be called before the protocol
starts (method run). Method
getOutput returns Alice’s output.
Alice could also return an error code
(0 or -1 depending on successful
completion).
All Rights Reserved, Lior Malka, 2010 28
class MyProjectServer {
main() {
Server server = new Server();
Alice alice = new Alice(server);
// Alice has two messages: “secret1” and “secret2”.
alice.setInput(“secret1”,”secret2”);
alice.run();
}
}
How The API Will be Used
}
class MyProjectClient {
main() {
Client client = new Client();
Bob bob = new Bob(client);
// Bob chooses message 0.
bob.setInput(0);
bob.run();
System.out.println(“Message 0 is “ + bob.getOutput());
}
}
All Rights Reserved, Lior Malka, 2010 29
Recap of Case Study
We turned class Alice from an application into a library.
Developers can integrate Alice in any project. They only
need to provides a class implementing interface Party.
We wrote general purpose classes Client and ServerWe wrote general purpose classes Client and Server
that can be reused in other projects.
The message functions of class Alice can be tested
independently, without having to run the full protocol.
The API driven design resulted in code that is easier to
maintain, read, use, and debug.
All Rights Reserved, Lior Malka, 2010 30
New Problems: A Growing Library
How can we guarantee consistency among our protocols?
It would be ideal if all protocols have a method run and
methods setInput and getOutput just like in ourmethods setInput and getOutput just like in our
oblivious transfer protocol.
All Rights Reserved, Lior Malka, 2010 31
Solution: Abstraction
We define Protocol as an abstract class.
Abstract classes play a similar role to that of interfaces.
Java interfaces can only declare functions. Java abstract classes can do that, and
in addition provide implementations and data members. A class can implement
several interfaces, but only extend one class (either abstract or not).
All Rights Reserved, Lior Malka, 2010 32
abstract class Protocol {
Party party;
Protocol(Party party) {
this.party = party;
Abstract Class Protocol
party is a data member because all
protocols need to have means for
sending and receiving messages.
All of our protocols will be of the following form:
this.party = party;
}
abstract void setInput(String[] input);
abstract void run();
abstract String getOutput();
}
Classes extending class Protocol
must implement these methods.
Class B can extend abstract class A without implementing all abstract methods of
class A. However, only sub classes of A (or B) implementing all abstract methods
of A can be instantiated.
All Rights Reserved, Lior Malka, 2010 33
Extending Class Protocol
We rewrite class Alice in terms of class Protocol.
class Alice extends Protocol {
private String X0,X1;
encryption_key_length;
public Alice(Party party) {
super(party); // invoking constructor of super classsuper(party); // invoking constructor of super class
}
void run() {
:
}
void setInput(String X0, String X1) {
this.X0 = X0;
this.X1 = X1;
}
String getOutput() {
return null;
}
}
Abstract classes are extended, whereas interfaces are implemented.
All Rights Reserved, Lior Malka, 2010 34
abstract class Protocol {
Party party;
Protocol(Party party) {
this.party = party;
}
abstract void setInput(String[] input);
Abstract Class Protocol – Not Generic
setInput and getOutput are defined
in terms of Strings. This works for
abstract void setInput(String[] input);
abstract void run();
abstract String getOutput();
}
in terms of Strings. This works for class
Alice, but may not work for protocols
with other input/output types.
All Rights Reserved, Lior Malka, 2010 35
abstract class Protocol<I, O> {
Party party;
Protocol(Party party) {
this.party = party;
}
Meta Programming
We define the class with non-fixed types:
I and O are parameters. They take a
type value during compilation time,
when we define class Alice.
}
abstract void setInput(I input);
abstract void run();
abstract O getOutput();
}
The input and output types are
defined in terms of the parameters
passed for I and O.
Java and C++ are strongly typed languages. They disallow writing code that
ignores types. In some cases this is a disadvantage. To overcome this issue, Java
provides Generics and C++ provides Templates.
All Rights Reserved, Lior Malka, 2010 36
Rewriting class Alice
class Alice extends Protocol<String[], String> {
private String X0,X1;
encryption_key_length;
public Alice(Party party) {
super(party); // invoking constructor of super class
}
void run() {
This is good. The type of I is String[] and
the type for O is String. Each protocol can
use different types as necessary.
void run() {
:
}
void setInput(String[] X) {
X0 = X[0];
X1 = X[1];
}
String getOutput() {
return null;
}
}
All Rights Reserved, Lior Malka, 2010 37
Advantages of The New Design
Modularity: all protocols extend class Protocol.
Consistency: all protocols are started with the run method.
Input is always set with setInput, and output is always
obtained with getOutput.obtained with getOutput.
Protocol input and output is managed via type safe functions.
All Rights Reserved, Lior Malka, 2010 38
Summary
We started with an Oblivious Transfer application. Then,
We wrote a generic client and a server.
Our client & server can be used in other projects.
We separated networking from the OT protocol.
Our OT protocol can be integrated in any project.
We separated protocol flow from message construction.
Our code is easier to maintain and understand.
We standardized all protocols using an abstract class.
Our library is type safe and consistent.
All Rights Reserved, Lior Malka, 2010 39
Conclusion
API driven design requires planning and programming skills.
API driven design is costly initially, but it pays in the long run.
Agile approach is still useful as a basis for API driven design.
All Rights Reserved, Lior Malka, 2010 40

More Related Content

What's hot

Distributes objects and Rmi
Distributes objects and RmiDistributes objects and Rmi
Distributes objects and Rmi
Mayank Jain
 

What's hot (20)

Java RMI
Java RMIJava RMI
Java RMI
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Java RMI Detailed Tutorial
Java RMI Detailed TutorialJava RMI Detailed Tutorial
Java RMI Detailed Tutorial
 
Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
 
Rmi
RmiRmi
Rmi
 
Rmi ppt
Rmi pptRmi ppt
Rmi ppt
 
Rmi
RmiRmi
Rmi
 
Java RMI
Java RMIJava RMI
Java RMI
 
Distributes objects and Rmi
Distributes objects and RmiDistributes objects and Rmi
Distributes objects and Rmi
 
Rmi
RmiRmi
Rmi
 
Remote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVARemote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVA
 
Rmi architecture
Rmi architectureRmi architecture
Rmi architecture
 
Rmi
RmiRmi
Rmi
 
COM Introduction
COM IntroductionCOM Introduction
COM Introduction
 
DCOM Comparison
DCOM ComparisonDCOM Comparison
DCOM Comparison
 
DS
DSDS
DS
 
javarmi
javarmijavarmi
javarmi
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 

Similar to learn design

Distributed Programming using RMI
 Distributed Programming using RMI Distributed Programming using RMI
Distributed Programming using RMI
backdoor
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
Kavita Sharma
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
Sasi Kala
 
Introduction to the OSI 7 layer model and Data Link Layer
Introduction to the OSI 7 layer model and Data Link LayerIntroduction to the OSI 7 layer model and Data Link Layer
Introduction to the OSI 7 layer model and Data Link Layer
VNIT-ACM Student Chapter
 

Similar to learn design (20)

Sockets
SocketsSockets
Sockets
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Network Sockets
Network SocketsNetwork Sockets
Network Sockets
 
Sockets
Sockets Sockets
Sockets
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Distributed Programming using RMI
 Distributed Programming using RMI Distributed Programming using RMI
Distributed Programming using RMI
 
Micro on NATS - Microservices with Messaging
Micro on NATS - Microservices with MessagingMicro on NATS - Microservices with Messaging
Micro on NATS - Microservices with Messaging
 
Layered Architecture
Layered ArchitectureLayered Architecture
Layered Architecture
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
 
Java Programming - 07 java networking
Java Programming - 07 java networkingJava Programming - 07 java networking
Java Programming - 07 java networking
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
Lecture 06
Lecture 06Lecture 06
Lecture 06
 
Introduction to the OSI 7 layer model and Data Link Layer
Introduction to the OSI 7 layer model and Data Link LayerIntroduction to the OSI 7 layer model and Data Link Layer
Introduction to the OSI 7 layer model and Data Link Layer
 
Communication middleware
Communication middlewareCommunication middleware
Communication middleware
 
D1 from interfaces to solid
D1 from interfaces to solidD1 from interfaces to solid
D1 from interfaces to solid
 
C++ Interface Versioning
C++ Interface VersioningC++ Interface Versioning
C++ Interface Versioning
 
NP-lab-manual.docx
NP-lab-manual.docxNP-lab-manual.docx
NP-lab-manual.docx
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
 
NP-lab-manual (1).pdf
NP-lab-manual (1).pdfNP-lab-manual (1).pdf
NP-lab-manual (1).pdf
 
NP-lab-manual.pdf
NP-lab-manual.pdfNP-lab-manual.pdf
NP-lab-manual.pdf
 

Recently uploaded

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 

learn design

  • 1. How to Design APIs for Cryptographic Protocols Lior Malka Crypto Group, University of Maryland, USA All Rights Reserved, Lior Malka, 2010 1
  • 2. What is a Cryptographic Protocol? Usually two parties. Terminology: sender - receiver / server – client. Parties has input and output (possibly null). Threat model.Threat model. Security parameters. Bob Alice All Rights Reserved, Lior Malka, 2010 2
  • 3. API – Application Programming Interface A software library is a collection of modules that provides services to independent programs via an API (Abstract Programming Interface) Example: Class Encryption uses Class Vector.Example: Class Encryption uses Class Vector. Class Vector provides services to Class Encryption. Class Encryption provides services to other classes. The methods and variables through which a class provides services are the APIAPI of this class. All Rights Reserved, Lior Malka, 2010 3
  • 4. Software Design Without API Pros Fast to implement in small projects. Agile – can serve as a starting point for API design. No need to consider how code interfaces with other software. Can be appropriate for small “dead end” projects. Cons Inappropriate for large projects. Code has a limited (as opposed to general) functionality. Code is not reusable. Code is hard to maintain/modify. Prone to errors and bugs. All Rights Reserved, Lior Malka, 2010 4
  • 5. Why a Good API is hard to Design Forces designer to anticipate future usage of code. Requirements are incomplete (may never be complete). Requires abstraction. Requires modularization.Requires modularization. Requires skills in programming languages. Requires code rewrites – time consuming and labor intensive. All Rights Reserved, Lior Malka, 2010 5
  • 6. The Benefits of API Driven Design When an API is used in a project, it Allows to focus on the project. Saves development time. Reduces errors and debugging.Reduces errors and debugging. Facilitates modular design. Provides a consistent development platform. All Rights Reserved, Lior Malka, 2010 6
  • 7. Case Study Oblivious Transfer (OT) All Rights Reserved, Lior Malka, 2010 7
  • 8. Bob Oblivious Transfer (OT) - Visually Alice X0 , X1b nullXb All Rights Reserved, Lior Malka, 2010 8
  • 9. Oblivious Transfer (OT) - Formally In an oblivious transfer protocol, Alice allows Bob to learn only one of her inputs, but Bob does not tell Alice which one it is. Formally: - The input of Alice is two strings: X0 and X1. - The input of Bob is a bit b (0 or 1).- The input of Bob is a bit b (0 or 1). At the end of the protocol: - The output of Bob is Xb. - The output of Alice is null. Security properties: Alice does not learn b, and Bob does not learn X1-b. All Rights Reserved, Lior Malka, 2010 9
  • 10. Implementing OT : 1st Attempt class Alice { String X0,X1; encryption_key_length; main(){ // open a socket and wait to hear from Bob.. : send_first_message(); } // methods for sending and receiving messages// methods for sending and receiving messages void send(byte[] m) { … } byte[] receive() { … } /* methods for constructing the messages of Alice and processing the replies of Bob */ void send_first_message(){ m1 = new byte[encryption_key_length]; : send(m1); receive_second_message(); } void receive_second_message(){ byte[] m2 = receive(); : } } All Rights Reserved, Lior Malka, 2010 10
  • 11. What is Wrong With This Code? Mixes several unrelated functions. Has no API. Exercise: • List all the flaws in the OT protocol. How would you modify the code so that it has an API?• How would you modify the code so that it has an API? Next slides: Improve design and provide an API. We will only consider class Alice ; the same improvements apply to class Bob . All Rights Reserved, Lior Malka, 2010 11
  • 12. Design Flaw: mixed functionality class Alice { String X0,X1; encryption_key_length; main(){ // open a socket and wait to hear from Bob.. : send_first_message(); } // methods for sending and receiving messages Networking functions have nothing to do with OT. They// methods for sending and receiving messages void send(byte[] m) { … } byte[] receive() { … } /* methods for constructing the messages of Alice and processing the replies of Bob */ void send_first_message(){ m1 = new byte[encryption_key_length]; : send(m1); receive_second_message(); } void receive_second_message(){ byte[] m2 = receive(); : } } nothing to do with OT. They should be in a separate class called “Server” All Rights Reserved, Lior Malka, 2010 12
  • 13. Redesign - Phase 1 First Objective Separate networking functionality from the protocol. All Rights Reserved, Lior Malka, 2010 13
  • 14. Writing The Client and The Server class Client { Socket socket; // methods for sending and receiving messages void send(byte[] m) { … } byte[] receive() { … } } class Server{ ServerSocket socket; // methods for sending and receiving messages void send(byte[] m) { … } byte[] receive() { … } } All Rights Reserved, Lior Malka, 2010 14
  • 15. Removing send & receive from class Alice class Alice { String X0,X1; Server server; encryption_key_length; main(){ Alice alice = new Alice(); alice.server = new Server(..); send_first_message(); } // methods for sending and receiving messages void send(byte[] m) { … } byte[] receive() { … } /* methods for constructing the messages of Alice and processing the replies of Bob */ void send_first_message(){ m1 = new byte[encryption_key_length]; : server.send(m1); receive_second_message(); } void receive_second_message(){ byte[] m2 = server.receive(); : } } All Rights Reserved, Lior Malka, 2010 15
  • 16. Implementing OT : 2nd Attempt class Alice { String X0,X1; Server server; encryption_key_length; main(){ : } /* methods for constructing the messages of Alice and processing the replies of Bob */ The problem with Class Alice is that it can only be used with class Server. It cannot be used with any other class, even if that class has send and receive methods. and processing the replies of Bob */ void send_first_message(){ m1 = new byte[encryption_key_length]; : server.send(m1); receive_second_message(); } void receive_second_message(){ byte[] m2 = server.receive(); : } } All Rights Reserved, Lior Malka, 2010 16
  • 17. Fixing the Flaws class Alice only needs the network support: send and receive methods. We do not want to tie users of class Alice to class Server. Solution: use an interface to define what we expect from a network module. Then, replace class Server with the interface. All Rights Reserved, Lior Malka, 2010 17
  • 18. Rewriting The Client and The Server class Client implements Party { Interface Party { // Interface methods (notice: methods are declared, but not defined) void send(byte[] m) byte[] receive() } Interfaces and abstract classes (also known as virtual classes) are a sign of a healthy API driven design. class Client implements Party { Socket socket; // Implementation of send and receive from class Party void send(byte[] m) { … } byte[] receive() { … } } class Server implements Party { ServerSocket socket; // Implementation of send and receive from class Party void send(byte[] m) { … } byte[] receive() { … } } All Rights Reserved, Lior Malka, 2010 18
  • 19. Removing “Boilerplate Code” class Client implements Party { Socket socket; Interface Party { void send(byte[] m) byte[] receive() } Socket socket; // Implementation of send and receive from class Party void send(byte[] m) { … } byte[] receive() { … } } class Server implements Party { ServerSocket socket; // Implementation of send and receive from class Party void send(byte[] m) { … } byte[] receive() { … } } Redundancy: send and receive methods of class Client do the same as those in class Server. We will fix this using inheritance (next slide) All Rights Reserved, Lior Malka, 2010 19
  • 20. Removing Redundancy Using Inheritance interface Party send receive class MyParty implments Party send and receive are declared in the interface, but not defined. class MyParty provides a class MyParty implments Party send {…} receive {…} class Client extends MyParty class Server extends MyParty class Client and class Server inherit send and receive from class MyParty class MyParty provides a specific implementation of send and receive All Rights Reserved, Lior Malka, 2010 20
  • 21. Finishing the Client and The Server class MyParty implements Party { // Implementation of send and receive void send(byte[] m) { … } Interface Party { // Declaration of send and receive void send(byte[] m); byte[] receive(); } void send(byte[] m) { … } byte[] receive() { … } } class Client extends MyParty { Socket socket; : } class Server extends MyParty { ServerSocket socket; : } All Rights Reserved, Lior Malka, 2010 21
  • 22. Summary of Improvements So Far class MyParty implements Party { // Implementation of send and receive void send(byte[] m) { … } Interface Party { // Declaration of send and receive void send(byte[] m); byte[] receive(); } class Alice can be used from any client/server implementing interface Party. Send and receive are easy to maintain/modify because they only appear in one place.void send(byte[] m) { … } byte[] receive() { … } } class Client extends MyParty { Socket socket; : } class Server extends MyParty { ServerSocket socket; : } class Client and class Server can be used in any client/server application they only appear in one place. Network I/O exceptions can be handled here, instead of in class Alice. All Rights Reserved, Lior Malka, 2010 22
  • 23. Implementing OT : 3rd Attempt class Alice { String X0,X1; Party party; encryption_key_length; main(){ Alice alice = new Alice(); alice.party = new Server(..); send_first_message(); } /* methods for constructing the messages of Alice and processing the replies of Bob */ void send_first_message(){ m1 = new byte[encryption_key_length]; : server.send(m1); receive_second_message(); } void receive_second_message(){ byte[] m2 = server.receive(); : } } All Rights Reserved, Lior Malka, 2010 23
  • 24. Redesign – Phase 2 Remove main(). We are building a library; not an application. Decouple message construction from protocol flow. Encapsulate the class; provide methods that set the input and get the output.get the output. All Rights Reserved, Lior Malka, 2010 24
  • 25. Removing main() class Alice { String X0,X1; Party party; encryption_key_length; main(){ Alice alice = new Alice(); alice.party = new Server(..); send_first_message(); The server will be passed to the constructor of class Alice, and send_first_message will be handled in a “run” method.send_first_message(); } /* methods for constructing the messages of Alice and processing the replies of Bob */ void send_first_message(){ m1 = new byte[encryption_key_length]; : party.send(m1); receive_second_message(); } void receive_second_message(){ byte[] m2 = party.receive(); : } } will be handled in a “run” method. All Rights Reserved, Lior Malka, 2010 25
  • 26. class Alice { String X0,X1; Party party; encryption_key_length; public Alice(Party party) { this.party = party; } /* methods for constructing the messages of Alice This is better. Now Alice can be used with any class implementing interface Party Removing main() /* methods for constructing the messages of Alice and processing the replies of Bob */ void send_first_message(){ m1 = new byte[encryption_key_length]; : party.send(m1); receive_second_message(); } void receive_second_message(){ byte[] m2 = party.receive(); : } } This is not good. Messages are constructed and sent / received in various locations in the code. All Rights Reserved, Lior Malka, 2010 26
  • 27. class Alice { String X0,X1; Party party; encryption_key_length; public Alice(Party party) { this.party = party; } Decoupling Message Construction From Protocol Flow void run() { party.send(first_message()); process_second_message(party.receive()); : } void byte[] first_message(){ m1 = new byte[encryption_key_length]; : return m1; } void process_second_message(byte[] m2){ : } } Method run centrally defines message flow in the protocol. This makes the code easier to maintain and read. Methods first_message and process_second_message are now defined purely in terms of input and output. This enables us to test their correctness independently (without having to run the protocol). All Rights Reserved, Lior Malka, 2010 27
  • 28. class Alice { private String X0,X1; Party party; encryption_key_length; public Alice(Party party) { this.party = party; } Encapsulating Data Members void run() { party.send(first_message()); process_second_message(party.receive()); : } void setInput(String X0, String X1) { this.X0 = X0; this.X1 = X1; } String getOutput() { return null; } } Method setInput sets Alice’s input. It should be called before the protocol starts (method run). Method getOutput returns Alice’s output. Alice could also return an error code (0 or -1 depending on successful completion). All Rights Reserved, Lior Malka, 2010 28
  • 29. class MyProjectServer { main() { Server server = new Server(); Alice alice = new Alice(server); // Alice has two messages: “secret1” and “secret2”. alice.setInput(“secret1”,”secret2”); alice.run(); } } How The API Will be Used } class MyProjectClient { main() { Client client = new Client(); Bob bob = new Bob(client); // Bob chooses message 0. bob.setInput(0); bob.run(); System.out.println(“Message 0 is “ + bob.getOutput()); } } All Rights Reserved, Lior Malka, 2010 29
  • 30. Recap of Case Study We turned class Alice from an application into a library. Developers can integrate Alice in any project. They only need to provides a class implementing interface Party. We wrote general purpose classes Client and ServerWe wrote general purpose classes Client and Server that can be reused in other projects. The message functions of class Alice can be tested independently, without having to run the full protocol. The API driven design resulted in code that is easier to maintain, read, use, and debug. All Rights Reserved, Lior Malka, 2010 30
  • 31. New Problems: A Growing Library How can we guarantee consistency among our protocols? It would be ideal if all protocols have a method run and methods setInput and getOutput just like in ourmethods setInput and getOutput just like in our oblivious transfer protocol. All Rights Reserved, Lior Malka, 2010 31
  • 32. Solution: Abstraction We define Protocol as an abstract class. Abstract classes play a similar role to that of interfaces. Java interfaces can only declare functions. Java abstract classes can do that, and in addition provide implementations and data members. A class can implement several interfaces, but only extend one class (either abstract or not). All Rights Reserved, Lior Malka, 2010 32
  • 33. abstract class Protocol { Party party; Protocol(Party party) { this.party = party; Abstract Class Protocol party is a data member because all protocols need to have means for sending and receiving messages. All of our protocols will be of the following form: this.party = party; } abstract void setInput(String[] input); abstract void run(); abstract String getOutput(); } Classes extending class Protocol must implement these methods. Class B can extend abstract class A without implementing all abstract methods of class A. However, only sub classes of A (or B) implementing all abstract methods of A can be instantiated. All Rights Reserved, Lior Malka, 2010 33
  • 34. Extending Class Protocol We rewrite class Alice in terms of class Protocol. class Alice extends Protocol { private String X0,X1; encryption_key_length; public Alice(Party party) { super(party); // invoking constructor of super classsuper(party); // invoking constructor of super class } void run() { : } void setInput(String X0, String X1) { this.X0 = X0; this.X1 = X1; } String getOutput() { return null; } } Abstract classes are extended, whereas interfaces are implemented. All Rights Reserved, Lior Malka, 2010 34
  • 35. abstract class Protocol { Party party; Protocol(Party party) { this.party = party; } abstract void setInput(String[] input); Abstract Class Protocol – Not Generic setInput and getOutput are defined in terms of Strings. This works for abstract void setInput(String[] input); abstract void run(); abstract String getOutput(); } in terms of Strings. This works for class Alice, but may not work for protocols with other input/output types. All Rights Reserved, Lior Malka, 2010 35
  • 36. abstract class Protocol<I, O> { Party party; Protocol(Party party) { this.party = party; } Meta Programming We define the class with non-fixed types: I and O are parameters. They take a type value during compilation time, when we define class Alice. } abstract void setInput(I input); abstract void run(); abstract O getOutput(); } The input and output types are defined in terms of the parameters passed for I and O. Java and C++ are strongly typed languages. They disallow writing code that ignores types. In some cases this is a disadvantage. To overcome this issue, Java provides Generics and C++ provides Templates. All Rights Reserved, Lior Malka, 2010 36
  • 37. Rewriting class Alice class Alice extends Protocol<String[], String> { private String X0,X1; encryption_key_length; public Alice(Party party) { super(party); // invoking constructor of super class } void run() { This is good. The type of I is String[] and the type for O is String. Each protocol can use different types as necessary. void run() { : } void setInput(String[] X) { X0 = X[0]; X1 = X[1]; } String getOutput() { return null; } } All Rights Reserved, Lior Malka, 2010 37
  • 38. Advantages of The New Design Modularity: all protocols extend class Protocol. Consistency: all protocols are started with the run method. Input is always set with setInput, and output is always obtained with getOutput.obtained with getOutput. Protocol input and output is managed via type safe functions. All Rights Reserved, Lior Malka, 2010 38
  • 39. Summary We started with an Oblivious Transfer application. Then, We wrote a generic client and a server. Our client & server can be used in other projects. We separated networking from the OT protocol. Our OT protocol can be integrated in any project. We separated protocol flow from message construction. Our code is easier to maintain and understand. We standardized all protocols using an abstract class. Our library is type safe and consistent. All Rights Reserved, Lior Malka, 2010 39
  • 40. Conclusion API driven design requires planning and programming skills. API driven design is costly initially, but it pays in the long run. Agile approach is still useful as a basis for API driven design. All Rights Reserved, Lior Malka, 2010 40